|
| 1 | +// The rule namespace holds one number to one rule, and the planted-red cases are the exact |
| 2 | +// collisions this catalog was introduced to reject: R11 named both package-boundaries and |
| 3 | +// contracts-implementation-authority, R13 named both platform-package-substrate and |
| 4 | +// device-inventory-cutover. |
| 5 | + |
| 6 | +import assert from 'node:assert/strict'; |
| 7 | +import path from 'node:path'; |
| 8 | +import { test } from 'node:test'; |
| 9 | +import { |
| 10 | + layeringPolicySources, |
| 11 | + ruleCatalogSummary, |
| 12 | + ruleDeclarations, |
| 13 | + ruleNamespaceCollisions, |
| 14 | + type LayeringPolicySource, |
| 15 | +} from './rule-catalog.ts'; |
| 16 | + |
| 17 | +const policyDir = import.meta.dirname; |
| 18 | + |
| 19 | +function sources(...entries: readonly (readonly [string, string])[]): LayeringPolicySource[] { |
| 20 | + return entries.map(([file, source]) => ({ path: file, source })); |
| 21 | +} |
| 22 | + |
| 23 | +function collisionsIn(...entries: readonly (readonly [string, string])[]) { |
| 24 | + return ruleNamespaceCollisions(ruleDeclarations(sources(...entries))); |
| 25 | +} |
| 26 | + |
| 27 | +test('the pre-fix R11 collision is rejected: one number cannot name two rules', () => { |
| 28 | + assert.deepEqual( |
| 29 | + collisionsIn( |
| 30 | + [ |
| 31 | + 'contracts-implementation-policy.ts', |
| 32 | + "const RULE = 'R11 contracts-implementation-authority';", |
| 33 | + ], |
| 34 | + ['package-boundaries.ts', "const violation = { rule: 'R11 package-boundaries' };"], |
| 35 | + ), |
| 36 | + [{ number: 11, names: ['contracts-implementation-authority', 'package-boundaries'] }], |
| 37 | + ); |
| 38 | +}); |
| 39 | + |
| 40 | +test('the pre-fix R13 collision is rejected across three declaring files', () => { |
| 41 | + assert.deepEqual( |
| 42 | + collisionsIn( |
| 43 | + ['device-inventory-cutover-policy.ts', "const RULE = 'R13 device-inventory-cutover';"], |
| 44 | + ['platform-package-policy.ts', "const RULE = 'R13 platform-package-substrate';"], |
| 45 | + ['platform-composition-policy.ts', "const RULE = 'R13 platform-package-substrate';"], |
| 46 | + ), |
| 47 | + [{ number: 13, names: ['device-inventory-cutover', 'platform-package-substrate'] }], |
| 48 | + ); |
| 49 | +}); |
| 50 | + |
| 51 | +test('the post-fix allocation of those same five rules is one-to-one', () => { |
| 52 | + assert.deepEqual( |
| 53 | + collisionsIn( |
| 54 | + [ |
| 55 | + 'contracts-implementation-policy.ts', |
| 56 | + "const RULE = 'R18 contracts-implementation-authority';", |
| 57 | + ], |
| 58 | + ['package-boundaries.ts', "const violation = { rule: 'R11 package-boundaries' };"], |
| 59 | + ['device-inventory-cutover-policy.ts', "const RULE = 'R17 device-inventory-cutover';"], |
| 60 | + ['platform-package-policy.ts', "const RULE = 'R13 platform-package-substrate';"], |
| 61 | + ['platform-composition-policy.ts', "const RULE = 'R13 platform-package-substrate';"], |
| 62 | + ), |
| 63 | + [], |
| 64 | + ); |
| 65 | +}); |
| 66 | + |
| 67 | +test('one rule declared under two numbers is allowed; only a shared number is drift', () => { |
| 68 | + // R14-R16 are three distinct cutover rules; repeating a NAME is not what breaks the report. |
| 69 | + assert.deepEqual( |
| 70 | + collisionsIn([ |
| 71 | + 'a.ts', |
| 72 | + "const rules = ['R14 logs-runtime-cutover', 'R15 logs-runtime-cutover'];", |
| 73 | + ]), |
| 74 | + [], |
| 75 | + ); |
| 76 | +}); |
| 77 | + |
| 78 | +test('declarations are found in every syntactic form, not just `const RULE =`', () => { |
| 79 | + const declarations = ruleDeclarations( |
| 80 | + sources([ |
| 81 | + 'mixed.ts', |
| 82 | + [ |
| 83 | + "const RULE = 'R2 commands-floor';", |
| 84 | + 'const inline = { rule: "R3 platforms-seam" };', |
| 85 | + "const nested = [{ rule: 'R4 value-import-cycle' }];", |
| 86 | + "function emit() { return { rule: 'R5 zero-back-edges' }; }", |
| 87 | + ].join('\n'), |
| 88 | + ]), |
| 89 | + ); |
| 90 | + |
| 91 | + assert.deepEqual( |
| 92 | + declarations.map(({ number, name }) => `R${number} ${name}`), |
| 93 | + ['R2 commands-floor', 'R3 platforms-seam', 'R4 value-import-cycle', 'R5 zero-back-edges'], |
| 94 | + ); |
| 95 | +}); |
| 96 | + |
| 97 | +test('summary prose that merely opens with a number is not a declaration', () => { |
| 98 | + // `R13 holds six private ...` and `R17 holds the devices command ...` are OK-line prose. |
| 99 | + assert.deepEqual( |
| 100 | + ruleDeclarations( |
| 101 | + sources([ |
| 102 | + 'summaries.ts', |
| 103 | + [ |
| 104 | + "const a = 'R13 holds six private implementation-lazy platform packages';", |
| 105 | + "const b = 'R17 holds the devices command to one gateway-owned inventory route';", |
| 106 | + "const c = 'R11 holds 17 workspace packages behind 39 exported subpaths';", |
| 107 | + ].join('\n'), |
| 108 | + ]), |
| 109 | + ), |
| 110 | + [], |
| 111 | + ); |
| 112 | +}); |
| 113 | + |
| 114 | +test('rule numbers named only in comments are not declarations', () => { |
| 115 | + assert.deepEqual( |
| 116 | + ruleDeclarations( |
| 117 | + sources([ |
| 118 | + 'commented.ts', |
| 119 | + [ |
| 120 | + '// R11 package-boundaries owns these physical seams, and R13 owns the substrate.', |
| 121 | + "/* superseded: 'R13 device-inventory-cutover' moved to R17 */", |
| 122 | + "const RULE = 'R17 device-inventory-cutover';", |
| 123 | + ].join('\n'), |
| 124 | + ]), |
| 125 | + ).map(({ name }) => name), |
| 126 | + ['device-inventory-cutover'], |
| 127 | + ); |
| 128 | +}); |
| 129 | + |
| 130 | +test('the real guard tree declares a one-to-one rule namespace', () => { |
| 131 | + const declarations = ruleDeclarations(layeringPolicySources(policyDir)); |
| 132 | + |
| 133 | + assert.deepEqual(ruleNamespaceCollisions(declarations), []); |
| 134 | + assert.ok(declarations.length > 0, 'expected the catalog to find real rule declarations'); |
| 135 | +}); |
| 136 | + |
| 137 | +test('the real tree still declares both renumbered rules under their own numbers', () => { |
| 138 | + const byName = new Map( |
| 139 | + ruleDeclarations(layeringPolicySources(policyDir)).map(({ name, number }) => [name, number]), |
| 140 | + ); |
| 141 | + |
| 142 | + assert.equal(byName.get('package-boundaries'), 11); |
| 143 | + assert.equal(byName.get('platform-package-substrate'), 13); |
| 144 | + assert.equal(byName.get('device-inventory-cutover'), 17); |
| 145 | + assert.equal(byName.get('contracts-implementation-authority'), 18); |
| 146 | +}); |
| 147 | + |
| 148 | +test('the catalog reads the guard sources and skips its own test fixtures', () => { |
| 149 | + const scanned = layeringPolicySources(policyDir).map(({ path: file }) => path.basename(file)); |
| 150 | + |
| 151 | + assert.ok(scanned.includes('package-boundaries.ts'), 'expected policy sources to be scanned'); |
| 152 | + assert.deepEqual( |
| 153 | + scanned.filter((file) => file.endsWith('.test.ts')), |
| 154 | + [], |
| 155 | + 'test fixtures declare rule strings as data and must not enter the catalog', |
| 156 | + ); |
| 157 | +}); |
| 158 | + |
| 159 | +test('the summary names the span the catalog actually found', () => { |
| 160 | + assert.equal( |
| 161 | + ruleCatalogSummary( |
| 162 | + ruleDeclarations( |
| 163 | + sources([ |
| 164 | + 'a.ts', |
| 165 | + "const rules = ['R2 commands-floor', 'R18 contracts-implementation-authority'];", |
| 166 | + ]), |
| 167 | + ), |
| 168 | + ), |
| 169 | + "the guard's own 2 rule ids each name exactly one rule (R2-R18)", |
| 170 | + ); |
| 171 | +}); |
0 commit comments