|
| 1 | +import fs from 'node:fs'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | +import { parseSync } from 'oxc-parser'; |
| 4 | +import { describe, expect, test } from 'vitest'; |
| 5 | +import { INTERNAL_COMMANDS, PUBLIC_COMMANDS } from '../../../command-catalog.ts'; |
| 6 | +import { commandDescriptors } from '../registry.ts'; |
| 7 | + |
| 8 | +/** |
| 9 | + * ADR 0019 §6 coherence gate for delegated platform execution. |
| 10 | + * |
| 11 | + * The registry entry gate inspects one descriptor at a time, so it cannot see a |
| 12 | + * command whose own module holds no platform code while its CLI route injects a |
| 13 | + * callback that dispatches a platform-executing command. Mode dominance closes |
| 14 | + * that: if the CLI route for command R dispatches command D, R may declare `none` |
| 15 | + * only when D is `none` too. |
| 16 | + * |
| 17 | + * Scope is `src/cli.ts`, the composition root where injected callbacks are built. |
| 18 | + * The scan is total within it — a dispatch no route can claim fails the gate. |
| 19 | + */ |
| 20 | + |
| 21 | +type AstNode = { type: string } & Record<string, unknown>; |
| 22 | + |
| 23 | +const CLI_COMPOSITION_ROOT = fileURLToPath(new URL('../../../cli.ts', import.meta.url)); |
| 24 | + |
| 25 | +const COMMAND_CATALOGS: Record<string, Record<string, string>> = { |
| 26 | + INTERNAL_COMMANDS, |
| 27 | + PUBLIC_COMMANDS, |
| 28 | +}; |
| 29 | + |
| 30 | +const COMMAND_NAMES = new Set<string>(commandDescriptors.map(({ name }) => name)); |
| 31 | + |
| 32 | +type PlatformExecutionKindOf = (command: string) => string | undefined; |
| 33 | + |
| 34 | +const registryKind: PlatformExecutionKindOf = (command) => |
| 35 | + commandDescriptors.find(({ name }) => name === command)?.platformExecution.kind; |
| 36 | + |
| 37 | +type RouteDispatch = Readonly<{ route: string; dispatched: string }>; |
| 38 | + |
| 39 | +type RouteScan = Readonly<{ |
| 40 | + dispatches: readonly RouteDispatch[]; |
| 41 | + /** Dispatch literals no `command === '<name>'` route could claim. */ |
| 42 | + unattributed: readonly string[]; |
| 43 | +}>; |
| 44 | + |
| 45 | +function isAstNode(value: unknown): value is AstNode { |
| 46 | + return typeof value === 'object' && value !== null && typeof (value as AstNode).type === 'string'; |
| 47 | +} |
| 48 | + |
| 49 | +function walk(root: unknown, visit: (node: AstNode) => void): void { |
| 50 | + if (Array.isArray(root)) { |
| 51 | + for (const child of root) walk(child, visit); |
| 52 | + return; |
| 53 | + } |
| 54 | + if (!isAstNode(root)) return; |
| 55 | + visit(root); |
| 56 | + for (const value of Object.values(root)) walk(value, visit); |
| 57 | +} |
| 58 | + |
| 59 | +function identifierName(node: unknown): string | undefined { |
| 60 | + return isAstNode(node) && node.type === 'Identifier' ? String(node['name']) : undefined; |
| 61 | +} |
| 62 | + |
| 63 | +function stringLiteral(node: unknown): string | undefined { |
| 64 | + return isAstNode(node) && node.type === 'Literal' && typeof node['value'] === 'string' |
| 65 | + ? node['value'] |
| 66 | + : undefined; |
| 67 | +} |
| 68 | + |
| 69 | +function knownCommand(name: string | undefined): string | undefined { |
| 70 | + return name !== undefined && COMMAND_NAMES.has(name) ? name : undefined; |
| 71 | +} |
| 72 | + |
| 73 | +/** `command === 'react-devtools'`, either operand order, names a routed command. */ |
| 74 | +function routedCommandOf(test: unknown): string | undefined { |
| 75 | + if (!isAstNode(test) || test.type !== 'BinaryExpression' || test['operator'] !== '===') { |
| 76 | + return undefined; |
| 77 | + } |
| 78 | + const operands = [ |
| 79 | + [test['left'], test['right']], |
| 80 | + [test['right'], test['left']], |
| 81 | + ] as const; |
| 82 | + for (const [subject, literal] of operands) { |
| 83 | + if (identifierName(subject) !== 'command') continue; |
| 84 | + const routed = knownCommand(stringLiteral(literal)); |
| 85 | + if (routed !== undefined) return routed; |
| 86 | + } |
| 87 | + return undefined; |
| 88 | +} |
| 89 | + |
| 90 | +/** `command: 'x'`, `command: INTERNAL_COMMANDS.x`, `command: PUBLIC_COMMANDS.x`. */ |
| 91 | +function dispatchedCommandOf(node: AstNode): string | undefined { |
| 92 | + if (node.type !== 'Property' || node['computed'] === true) return undefined; |
| 93 | + if (identifierName(node['key']) !== 'command') return undefined; |
| 94 | + return dispatchTargetOf(node['value']); |
| 95 | +} |
| 96 | + |
| 97 | +function dispatchTargetOf(value: unknown): string | undefined { |
| 98 | + const literal = stringLiteral(value); |
| 99 | + if (literal !== undefined) return knownCommand(literal); |
| 100 | + if (!isAstNode(value) || value.type !== 'MemberExpression' || value['computed'] === true) { |
| 101 | + return undefined; |
| 102 | + } |
| 103 | + const catalog = identifierName(value['object']); |
| 104 | + const key = identifierName(value['property']); |
| 105 | + return catalog === undefined || key === undefined ? undefined : COMMAND_CATALOGS[catalog]?.[key]; |
| 106 | +} |
| 107 | + |
| 108 | +function localFunctionsByName(program: unknown): Map<string, AstNode> { |
| 109 | + const functions = new Map<string, AstNode>(); |
| 110 | + walk(program, (node) => { |
| 111 | + if (node.type !== 'FunctionDeclaration') return; |
| 112 | + const name = identifierName(node['id']); |
| 113 | + if (name !== undefined) functions.set(name, node); |
| 114 | + }); |
| 115 | + return functions; |
| 116 | +} |
| 117 | + |
| 118 | +/** Dispatch literals reachable from `scope`, following calls to same-module functions. */ |
| 119 | +function reachableDispatches( |
| 120 | + scope: unknown, |
| 121 | + functions: ReadonlyMap<string, AstNode>, |
| 122 | + visited: Set<string>, |
| 123 | +): Set<string> { |
| 124 | + const found = new Set<string>(); |
| 125 | + const calls: string[] = []; |
| 126 | + walk(scope, (node) => { |
| 127 | + const dispatched = dispatchedCommandOf(node); |
| 128 | + if (dispatched !== undefined) found.add(dispatched); |
| 129 | + if (node.type !== 'CallExpression') return; |
| 130 | + const callee = identifierName(node['callee']); |
| 131 | + if (callee !== undefined && functions.has(callee)) calls.push(callee); |
| 132 | + }); |
| 133 | + for (const name of calls) { |
| 134 | + if (visited.has(name)) continue; |
| 135 | + visited.add(name); |
| 136 | + for (const dispatched of reachableDispatches(functions.get(name), functions, visited)) { |
| 137 | + found.add(dispatched); |
| 138 | + } |
| 139 | + } |
| 140 | + return found; |
| 141 | +} |
| 142 | + |
| 143 | +function scanCliRouteDispatches(sourceText: string): RouteScan { |
| 144 | + const program = parseSync('cli.ts', sourceText).program; |
| 145 | + const functions = localFunctionsByName(program); |
| 146 | + const dispatches: RouteDispatch[] = []; |
| 147 | + const attributed = new Set<string>(); |
| 148 | + |
| 149 | + walk(program, (node) => { |
| 150 | + if (node.type !== 'IfStatement') return; |
| 151 | + const route = routedCommandOf(node['test']); |
| 152 | + if (route === undefined) return; |
| 153 | + for (const dispatched of reachableDispatches(node['consequent'], functions, new Set())) { |
| 154 | + dispatches.push({ route, dispatched }); |
| 155 | + attributed.add(dispatched); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + const all = new Set<string>(); |
| 160 | + walk(program, (node) => { |
| 161 | + const dispatched = dispatchedCommandOf(node); |
| 162 | + if (dispatched !== undefined) all.add(dispatched); |
| 163 | + }); |
| 164 | + |
| 165 | + return { |
| 166 | + dispatches, |
| 167 | + unattributed: [...all].filter((command) => !attributed.has(command)).sort(), |
| 168 | + }; |
| 169 | +} |
| 170 | + |
| 171 | +function dominanceFailures(scan: RouteScan, kindOf: PlatformExecutionKindOf): string[] { |
| 172 | + return scan.dispatches |
| 173 | + .filter(({ route, dispatched }) => kindOf(route) === 'none' && kindOf(dispatched) !== 'none') |
| 174 | + .map( |
| 175 | + ({ route, dispatched }) => |
| 176 | + `${route} declares platformExecution none but its CLI route dispatches ${dispatched} (${String(kindOf(dispatched))})`, |
| 177 | + ) |
| 178 | + .sort(); |
| 179 | +} |
| 180 | + |
| 181 | +function scanCompositionRoot(): RouteScan { |
| 182 | + return scanCliRouteDispatches(fs.readFileSync(CLI_COMPOSITION_ROOT, 'utf8')); |
| 183 | +} |
| 184 | + |
| 185 | +describe('platform-execution coherence across CLI route delegation', () => { |
| 186 | + test('no none command dispatches a platform-executing command', () => { |
| 187 | + expect(dominanceFailures(scanCompositionRoot(), registryKind)).toEqual([]); |
| 188 | + }); |
| 189 | + |
| 190 | + test('every CLI daemon dispatch is attributable to a routed command', () => { |
| 191 | + expect(scanCompositionRoot().unattributed).toEqual([]); |
| 192 | + }); |
| 193 | + |
| 194 | + test('the composition root still carries the react-devtools runtime delegation', () => { |
| 195 | + expect(scanCompositionRoot().dispatches).toContainEqual({ |
| 196 | + route: 'react-devtools', |
| 197 | + dispatched: 'runtime', |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + test('planted red: react-devtools declared none is rejected by the real route', () => { |
| 202 | + const plantedNone: PlatformExecutionKindOf = (command) => |
| 203 | + command === 'react-devtools' ? 'none' : registryKind(command); |
| 204 | + |
| 205 | + expect(dominanceFailures(scanCompositionRoot(), plantedNone)).toEqual([ |
| 206 | + 'react-devtools declares platformExecution none but its CLI route dispatches runtime (legacy)', |
| 207 | + ]); |
| 208 | + }); |
| 209 | + |
| 210 | + test('planted red: an unroutable dispatch is reported rather than ignored', () => { |
| 211 | + const planted = ` |
| 212 | + async function runCli(argv, deps) { |
| 213 | + await deps.sendToDaemon({ command: INTERNAL_COMMANDS.runtime, positionals: [] }); |
| 214 | + } |
| 215 | + `; |
| 216 | + expect(scanCliRouteDispatches(planted).unattributed).toEqual(['runtime']); |
| 217 | + }); |
| 218 | +}); |
0 commit comments