|
| 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 { |
| 6 | + SNAPSHOT_ASSEMBLY_FILES, |
| 7 | + SNAPSHOT_ASSEMBLY_PRESENTATION_RULE, |
| 8 | + snapshotAssemblyPresentationViolations, |
| 9 | +} from './snapshot-assembly-presentation-policy.ts'; |
| 10 | +import { resolveImportEdges } from './model.ts'; |
| 11 | +import { workspaceSpecifierTargets } from './package-boundaries.ts'; |
| 12 | +import { listTrackedProductionSources } from './tracked-sources.ts'; |
| 13 | + |
| 14 | +const repoRoot = path.resolve(import.meta.dirname, '../..'); |
| 15 | +const assemblyFile = 'src/core/snapshot-state.ts'; |
| 16 | +const producerAdapter = 'packages/platform-apple/src/snapshot-source/adapter.ts'; |
| 17 | + |
| 18 | +function currentSources(overrides: ReadonlyMap<string, string> = new Map()): Map<string, string> { |
| 19 | + const sources = new Map( |
| 20 | + listTrackedProductionSources(repoRoot).map((file) => [ |
| 21 | + file, |
| 22 | + fs.readFileSync(path.join(repoRoot, file), 'utf8'), |
| 23 | + ]), |
| 24 | + ); |
| 25 | + for (const [file, source] of overrides) sources.set(file, source); |
| 26 | + return sources; |
| 27 | +} |
| 28 | + |
| 29 | +function violations(overrides: ReadonlyMap<string, string> = new Map()) { |
| 30 | + const sources = currentSources(overrides); |
| 31 | + return snapshotAssemblyPresentationViolations( |
| 32 | + sources, |
| 33 | + resolveImportEdges(sources, workspaceSpecifierTargets(repoRoot)), |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +function appended(file: string, addition: string): ReadonlyMap<string, string> { |
| 38 | + return new Map([[file, `${fs.readFileSync(path.join(repoRoot, file), 'utf8')}${addition}`]]); |
| 39 | +} |
| 40 | + |
| 41 | +test('the snapshot assembly and the producer adapters are presentation-neutral', () => { |
| 42 | + assert.deepEqual(violations(), []); |
| 43 | +}); |
| 44 | + |
| 45 | +test('R74 rejects the assembly importing the iOS snapshot engine', () => { |
| 46 | + const result = violations( |
| 47 | + appended( |
| 48 | + assemblyFile, |
| 49 | + `\nimport { presentIosInteractiveSnapshot } from '@agent-device/capture-kit/ios-snapshot-engine';\nvoid presentIosInteractiveSnapshot;\n`, |
| 50 | + ), |
| 51 | + ); |
| 52 | + |
| 53 | + assert.ok( |
| 54 | + result.some( |
| 55 | + (entry) => |
| 56 | + entry.rule === SNAPSHOT_ASSEMBLY_PRESENTATION_RULE && |
| 57 | + entry.file === assemblyFile && |
| 58 | + entry.message.includes('must not import iOS presentation'), |
| 59 | + ), |
| 60 | + JSON.stringify(result), |
| 61 | + ); |
| 62 | +}); |
| 63 | + |
| 64 | +test('R74 rejects the assembly reading the producer capability table', () => { |
| 65 | + const result = violations( |
| 66 | + appended( |
| 67 | + assemblyFile, |
| 68 | + `\nimport { IOS_SNAPSHOT_PRODUCER_CAPABILITIES } from '@agent-device/capture-kit/ios-snapshot-acquisition';\nvoid IOS_SNAPSHOT_PRODUCER_CAPABILITIES;\n`, |
| 69 | + ), |
| 70 | + ); |
| 71 | + |
| 72 | + assert.ok( |
| 73 | + result.some( |
| 74 | + (entry) => |
| 75 | + entry.rule === SNAPSHOT_ASSEMBLY_PRESENTATION_RULE && |
| 76 | + entry.file === assemblyFile && |
| 77 | + entry.message.includes('ios-snapshot-acquisition'), |
| 78 | + ), |
| 79 | + JSON.stringify(result), |
| 80 | + ); |
| 81 | +}); |
| 82 | + |
| 83 | +test('R74 rejects a backend-name presentation branch in the assembly', () => { |
| 84 | + const result = violations( |
| 85 | + appended( |
| 86 | + assemblyFile, |
| 87 | + `\nexport function presentsHere(backend: string): boolean {\n return backend === 'xctest';\n}\n`, |
| 88 | + ), |
| 89 | + ); |
| 90 | + |
| 91 | + assert.ok( |
| 92 | + result.some( |
| 93 | + (entry) => |
| 94 | + entry.rule === SNAPSHOT_ASSEMBLY_PRESENTATION_RULE && |
| 95 | + entry.file === assemblyFile && |
| 96 | + entry.message.includes("('xctest')"), |
| 97 | + ), |
| 98 | + JSON.stringify(result), |
| 99 | + ); |
| 100 | +}); |
| 101 | + |
| 102 | +test('R74 rejects an assembly branch on a producer name', () => { |
| 103 | + const result = violations( |
| 104 | + appended(assemblyFile, `\nexport const legacyOwner = 'simulator-ax-bridge';\n`), |
| 105 | + ); |
| 106 | + |
| 107 | + assert.ok( |
| 108 | + result.some( |
| 109 | + (entry) => |
| 110 | + entry.rule === SNAPSHOT_ASSEMBLY_PRESENTATION_RULE && |
| 111 | + entry.file === assemblyFile && |
| 112 | + entry.message.includes("('simulator-ax-bridge')"), |
| 113 | + ), |
| 114 | + JSON.stringify(result), |
| 115 | + ); |
| 116 | +}); |
| 117 | + |
| 118 | +test('R74 rejects a producer adapter importing iOS presentation', () => { |
| 119 | + const result = violations( |
| 120 | + appended( |
| 121 | + producerAdapter, |
| 122 | + `\nimport { presentIosSnapshot } from '@agent-device/capture-kit/ios-snapshot-engine';\nvoid presentIosSnapshot;\n`, |
| 123 | + ), |
| 124 | + ); |
| 125 | + |
| 126 | + assert.ok( |
| 127 | + result.some( |
| 128 | + (entry) => |
| 129 | + entry.rule === SNAPSHOT_ASSEMBLY_PRESENTATION_RULE && |
| 130 | + entry.file === producerAdapter && |
| 131 | + entry.message.includes('producers report acquisition facts'), |
| 132 | + ), |
| 133 | + JSON.stringify(result), |
| 134 | + ); |
| 135 | +}); |
| 136 | + |
| 137 | +test('R74 fails closed when the assembly moves out from under it', () => { |
| 138 | + const sources = currentSources(); |
| 139 | + for (const file of SNAPSHOT_ASSEMBLY_FILES) sources.delete(file); |
| 140 | + |
| 141 | + const result = snapshotAssemblyPresentationViolations( |
| 142 | + sources, |
| 143 | + resolveImportEdges(sources, workspaceSpecifierTargets(repoRoot)), |
| 144 | + ); |
| 145 | + |
| 146 | + assert.deepEqual( |
| 147 | + result.filter((entry) => entry.message.includes('is missing')).map((entry) => entry.file), |
| 148 | + [...SNAPSHOT_ASSEMBLY_FILES], |
| 149 | + ); |
| 150 | +}); |
0 commit comments