|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { test } from 'node:test'; |
| 5 | +import { ARCHITECTURE_OWNERSHIP, matchesDeclaredRoot } from './architecture-ownership.ts'; |
| 6 | +import { resolveImportEdges } from './model.ts'; |
| 7 | +import { workspaceSpecifierTargets } from './package-boundaries.ts'; |
| 8 | +import { listTrackedTypeScriptFiles } from './tracked-sources.ts'; |
| 9 | + |
| 10 | +const repoRoot = path.resolve(import.meta.dirname, '../..'); |
| 11 | + |
| 12 | +/** |
| 13 | + * ADR 0038: the snapshot source router in `packages/capture-kit/src/snapshot-router/` is platform |
| 14 | + * neutral. A platform or provider package declares its channels and binds them to the router; the |
| 15 | + * router never imports one back. The positive controls below exist because a clean tree looks the |
| 16 | + * same as a filter that stopped matching. |
| 17 | + */ |
| 18 | +function routerPolicy() { |
| 19 | + const policy = ARCHITECTURE_OWNERSHIP.executablePolicies.find( |
| 20 | + ({ name }) => name === 'snapshot-router', |
| 21 | + ); |
| 22 | + if (!policy) throw new Error('snapshot-router ownership declaration is missing'); |
| 23 | + return policy; |
| 24 | +} |
| 25 | + |
| 26 | +function platformImportsFromRouter( |
| 27 | + sources: ReadonlyMap<string, string>, |
| 28 | + workspaceTargets?: ReadonlyMap<string, string>, |
| 29 | +): string[] { |
| 30 | + const policy = routerPolicy(); |
| 31 | + return resolveImportEdges(sources, workspaceTargets) |
| 32 | + .filter( |
| 33 | + (edge) => |
| 34 | + policy.roots.some((root) => matchesDeclaredRoot(edge.file, root)) && |
| 35 | + policy.forbiddenTargetRoots.some((root) => matchesDeclaredRoot(edge.target, root)), |
| 36 | + ) |
| 37 | + .map((edge) => `${edge.file}:${edge.line} -> ${edge.target}`); |
| 38 | +} |
| 39 | + |
| 40 | +test('the snapshot router imports no platform or provider package', () => { |
| 41 | + const files = listTrackedTypeScriptFiles(repoRoot); |
| 42 | + const sources = new Map( |
| 43 | + files.map((file) => [file, fs.readFileSync(path.join(repoRoot, file), 'utf8')]), |
| 44 | + ); |
| 45 | + |
| 46 | + assert.deepEqual(platformImportsFromRouter(sources, workspaceSpecifierTargets(repoRoot)), []); |
| 47 | +}); |
| 48 | + |
| 49 | +test('every platform and provider package is a forbidden router target', () => { |
| 50 | + const packages = fs |
| 51 | + .readdirSync(path.join(repoRoot, 'packages')) |
| 52 | + .filter((name) => /^(platform|provider)-/.test(name)) |
| 53 | + .map((name) => `packages/${name}/`); |
| 54 | + |
| 55 | + assert.ok(packages.length > 0, 'expected platform and provider packages'); |
| 56 | + for (const root of packages) { |
| 57 | + assert.ok(routerPolicy().forbiddenTargetRoots.includes(root), `${root} is not forbidden`); |
| 58 | + } |
| 59 | +}); |
| 60 | + |
| 61 | +test('the boundary reports a router module that imports a platform or provider package', () => { |
| 62 | + const sources = new Map([ |
| 63 | + ['packages/platform-apple/src/snapshot-target.ts', 'export const resolve = 1;\n'], |
| 64 | + ['packages/provider-limrun/src/index.ts', 'export const limrun = 1;\n'], |
| 65 | + [ |
| 66 | + 'packages/capture-kit/src/snapshot-router/router.ts', |
| 67 | + [ |
| 68 | + "import { resolve } from '@agent-device/platform-apple/snapshot-target';", |
| 69 | + "const { limrun } = await import('../../../provider-limrun/src/index.ts');", |
| 70 | + 'export const x = [resolve, limrun];', |
| 71 | + '', |
| 72 | + ].join('\n'), |
| 73 | + ], |
| 74 | + ]); |
| 75 | + const workspaceTargets = new Map([ |
| 76 | + [ |
| 77 | + '@agent-device/platform-apple/snapshot-target', |
| 78 | + 'packages/platform-apple/src/snapshot-target.ts', |
| 79 | + ], |
| 80 | + ]); |
| 81 | + |
| 82 | + assert.deepEqual(platformImportsFromRouter(sources, workspaceTargets), [ |
| 83 | + 'packages/capture-kit/src/snapshot-router/router.ts:1 -> packages/platform-apple/src/snapshot-target.ts', |
| 84 | + 'packages/capture-kit/src/snapshot-router/router.ts:2 -> packages/provider-limrun/src/index.ts', |
| 85 | + ]); |
| 86 | +}); |
0 commit comments