|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'node:test'; |
| 3 | +import { resolveImportEdges, type LayeringViolation } from './model.ts'; |
| 4 | +import { |
| 5 | + checkDaemonPlatformRuntimeInventory, |
| 6 | + DAEMON_PLATFORM_RUNTIME_EDGES, |
| 7 | + DAEMON_PLATFORM_RUNTIME_RULE, |
| 8 | + isRootPlatformRuntimeTarget, |
| 9 | +} from './daemon-platform-runtime-inventory.ts'; |
| 10 | + |
| 11 | +const DEVICE_READY_TARGET = 'src/platform-runtime-device-ready.ts'; |
| 12 | +const DEVICE_READY_STUB = |
| 13 | + 'export async function ensureLocalPlatformDeviceReady(device: unknown) { return false; }\n'; |
| 14 | + |
| 15 | +function violations(sources: Record<string, string>): LayeringViolation[] { |
| 16 | + return checkDaemonPlatformRuntimeInventory(resolveImportEdges(new Map(Object.entries(sources)))); |
| 17 | +} |
| 18 | + |
| 19 | +function edgeViolations(sources: Record<string, string>, file: string): LayeringViolation[] { |
| 20 | + return violations(sources).filter((violation) => violation.file === file); |
| 21 | +} |
| 22 | + |
| 23 | +test('R74 accepts a classified edge with the exact recorded symbols', () => { |
| 24 | + const sources = { |
| 25 | + [DEVICE_READY_TARGET]: DEVICE_READY_STUB, |
| 26 | + 'src/daemon/device-ready.ts': |
| 27 | + "import { ensureLocalPlatformDeviceReady } from '../platform-runtime-device-ready.ts';\n" + |
| 28 | + 'void ensureLocalPlatformDeviceReady;\n', |
| 29 | + }; |
| 30 | + assert.deepEqual(edgeViolations(sources, 'src/daemon/device-ready.ts'), []); |
| 31 | +}); |
| 32 | + |
| 33 | +test('R74 reports every classified edge missing from the tree as stale, not the other way around', () => { |
| 34 | + const sources = { |
| 35 | + [DEVICE_READY_TARGET]: DEVICE_READY_STUB, |
| 36 | + 'src/daemon/device-ready.ts': |
| 37 | + "import { ensureLocalPlatformDeviceReady } from '../platform-runtime-device-ready.ts';\n" + |
| 38 | + 'void ensureLocalPlatformDeviceReady;\n', |
| 39 | + }; |
| 40 | + const stale = violations(sources).filter( |
| 41 | + (violation) => violation.file === 'scripts/layering/daemon-platform-runtime-inventory.ts', |
| 42 | + ); |
| 43 | + assert.equal(stale.length, DAEMON_PLATFORM_RUNTIME_EDGES.length - 1); |
| 44 | + assert.ok(stale.every((violation) => violation.message.includes('stale classified edge'))); |
| 45 | + const deviceReadyStale = stale.find((violation) => |
| 46 | + violation.message.includes(DEVICE_READY_TARGET), |
| 47 | + ); |
| 48 | + assert.equal(deviceReadyStale, undefined); |
| 49 | +}); |
| 50 | + |
| 51 | +test('R74 rejects an unclassified edge with the pair and its line', () => { |
| 52 | + const sources = { |
| 53 | + 'src/platform-runtime-android-tool-host.ts': 'export function createAndroidToolHost() {}\n', |
| 54 | + 'src/daemon/fixture.ts': |
| 55 | + "import { createAndroidToolHost } from '../platform-runtime-android-tool-host.ts';\n" + |
| 56 | + 'void createAndroidToolHost;\n', |
| 57 | + }; |
| 58 | + const found = edgeViolations(sources, 'src/daemon/fixture.ts'); |
| 59 | + assert.equal(found.length, 1); |
| 60 | + assert.equal(found[0]!.rule, DAEMON_PLATFORM_RUNTIME_RULE); |
| 61 | + assert.equal(found[0]!.line, 1); |
| 62 | + assert.match(found[0]!.message, /unclassified daemon-to-root platform-runtime coupling/); |
| 63 | + assert.match( |
| 64 | + found[0]!.message, |
| 65 | + /src\/daemon\/fixture\.ts -> src\/platform-runtime-android-tool-host\.ts/, |
| 66 | + ); |
| 67 | +}); |
| 68 | + |
| 69 | +test('R74 rejects new symbols on a classified edge', () => { |
| 70 | + const sources = { |
| 71 | + [DEVICE_READY_TARGET]: DEVICE_READY_STUB + 'export function extraReadiness() {}\n', |
| 72 | + 'src/daemon/device-ready.ts': |
| 73 | + "import { ensureLocalPlatformDeviceReady, extraReadiness } from '../platform-runtime-device-ready.ts';\n" + |
| 74 | + 'void [ensureLocalPlatformDeviceReady, extraReadiness];\n', |
| 75 | + }; |
| 76 | + const found = edgeViolations(sources, 'src/daemon/device-ready.ts'); |
| 77 | + assert.equal(found.length, 1); |
| 78 | + assert.equal(found[0]!.rule, DAEMON_PLATFORM_RUNTIME_RULE); |
| 79 | + assert.match(found[0]!.message, /classified symbols drifted/); |
| 80 | + assert.match(found[0]!.message, /ensureLocalPlatformDeviceReady, extraReadiness/); |
| 81 | +}); |
| 82 | + |
| 83 | +test('R74 matches a dynamic import by target with empty symbols', () => { |
| 84 | + const sources = { |
| 85 | + 'src/platform-runtime-operation-host.ts': |
| 86 | + 'export async function recoverLegacyAppLogMarkersAfterDaemonLock() { return {}; }\n', |
| 87 | + 'src/daemon/server/daemon-runtime.ts': |
| 88 | + "const mod = await import('../../platform-runtime-operation-host.ts');\nvoid mod;\n", |
| 89 | + }; |
| 90 | + assert.deepEqual(edgeViolations(sources, 'src/daemon/server/daemon-runtime.ts'), []); |
| 91 | +}); |
| 92 | + |
| 93 | +test('R74 treats the import and re-export of one classified pair as one entry', () => { |
| 94 | + const sources = { |
| 95 | + 'src/platform-runtime-open-target.ts': |
| 96 | + 'export async function resolveSoleForegroundIosApp() { return undefined; }\n', |
| 97 | + 'src/daemon/ios-app-session-hint.ts': |
| 98 | + "import { resolveSoleForegroundIosApp } from '../platform-runtime-open-target.ts';\n" + |
| 99 | + "export { resolveSoleForegroundIosApp } from '../platform-runtime-open-target.ts';\n" + |
| 100 | + 'void resolveSoleForegroundIosApp;\n', |
| 101 | + }; |
| 102 | + assert.deepEqual(edgeViolations(sources, 'src/daemon/ios-app-session-hint.ts'), []); |
| 103 | +}); |
| 104 | + |
| 105 | +test('R74 ignores test-shaped and non-daemon importers', () => { |
| 106 | + const sources = { |
| 107 | + 'src/platform-runtime-android-tool-host.ts': 'export function createAndroidToolHost() {}\n', |
| 108 | + 'src/daemon/__tests__/fixture.test.ts': |
| 109 | + "import { createAndroidToolHost } from '../platform-runtime-android-tool-host.ts';\n" + |
| 110 | + 'void createAndroidToolHost;\n', |
| 111 | + 'src/cli.ts': |
| 112 | + "import { createAndroidToolHost } from './platform-runtime-android-tool-host.ts';\n" + |
| 113 | + 'void createAndroidToolHost;\n', |
| 114 | + }; |
| 115 | + assert.deepEqual( |
| 116 | + violations(sources).filter( |
| 117 | + (violation) => violation.file !== 'scripts/layering/daemon-platform-runtime-inventory.ts', |
| 118 | + ), |
| 119 | + [], |
| 120 | + ); |
| 121 | +}); |
| 122 | + |
| 123 | +test('R74 ignores retired-zone targets, which R65 owns', () => { |
| 124 | + const sources = { |
| 125 | + 'src/platforms/android.ts': 'export const legacy = 1;\n', |
| 126 | + 'src/daemon/fixture.ts': 'import { legacy } from "../platforms/android.ts";\nvoid legacy;\n', |
| 127 | + }; |
| 128 | + assert.deepEqual(edgeViolations(sources, 'src/daemon/fixture.ts'), []); |
| 129 | +}); |
| 130 | + |
| 131 | +test('the root composition family is src/platform-runtime.ts plus src/platform-runtime-*.ts only', () => { |
| 132 | + assert.equal(isRootPlatformRuntimeTarget('src/platform-runtime.ts'), true); |
| 133 | + assert.equal(isRootPlatformRuntimeTarget('src/platform-runtime-android.ts'), true); |
| 134 | + assert.equal(isRootPlatformRuntimeTarget('src/platform-runtime-gateway.ts'), true); |
| 135 | + assert.equal(isRootPlatformRuntimeTarget('src/platform-runtime-android.tsx'), false); |
| 136 | + assert.equal(isRootPlatformRuntimeTarget('src/platform-runtime.ts.bak'), false); |
| 137 | + assert.equal(isRootPlatformRuntimeTarget('src/platforms/runtime.ts'), false); |
| 138 | + assert.equal(isRootPlatformRuntimeTarget('src/daemon/platform-runtime.ts'), false); |
| 139 | +}); |
0 commit comments