|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test } from 'node:test'; |
| 3 | +import { checkSeams, findSeamMatches } from './model.ts'; |
| 4 | + |
| 5 | +test('findSeamMatches finds an optional typeof field with its line number', () => { |
| 6 | + const matches = findSeamMatches([ |
| 7 | + { path: 'a.ts', source: 'type T = {\n dispatch?: typeof dispatchCommand;\n};\n' }, |
| 8 | + ]); |
| 9 | + assert.deepEqual(matches, [ |
| 10 | + { |
| 11 | + file: 'a.ts', |
| 12 | + line: 2, |
| 13 | + field: 'dispatch', |
| 14 | + target: 'dispatchCommand', |
| 15 | + text: 'dispatch?: typeof dispatchCommand;', |
| 16 | + }, |
| 17 | + ]); |
| 18 | +}); |
| 19 | + |
| 20 | +test('findSeamMatches ignores a required (non-optional) field', () => { |
| 21 | + const matches = findSeamMatches([ |
| 22 | + { path: 'a.ts', source: 'type T = { dispatch: typeof dispatchCommand };' }, |
| 23 | + ]); |
| 24 | + assert.deepEqual(matches, []); |
| 25 | +}); |
| 26 | + |
| 27 | +test('checkSeams passes a match whose exact (file, field, target) triple is approved', () => { |
| 28 | + const matches = findSeamMatches([{ path: 'src/x.ts', source: 'fetchImpl?: typeof fetch;' }]); |
| 29 | + const { violations, staleApprovals } = checkSeams(matches, [ |
| 30 | + { file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' }, |
| 31 | + ]); |
| 32 | + assert.deepEqual(violations, []); |
| 33 | + assert.deepEqual(staleApprovals, []); |
| 34 | +}); |
| 35 | + |
| 36 | +// This is the exact failure mode PR #2006's review flagged in the earlier name-based version: |
| 37 | +// approving `typeof fetch` in one file must not silently approve it everywhere. |
| 38 | +test('checkSeams flags the same field/target seam in an unapproved file', () => { |
| 39 | + const matches = findSeamMatches([{ path: 'src/y.ts', source: 'fetchImpl?: typeof fetch;' }]); |
| 40 | + const { violations } = checkSeams(matches, [ |
| 41 | + { file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' }, |
| 42 | + ]); |
| 43 | + assert.equal(violations.length, 1); |
| 44 | + assert.equal(violations[0]?.file, 'src/y.ts'); |
| 45 | +}); |
| 46 | + |
| 47 | +test('checkSeams flags an unapproved field name in an approved file, even for an approved target', () => { |
| 48 | + const matches = findSeamMatches([{ path: 'src/x.ts', source: 'otherField?: typeof fetch;' }]); |
| 49 | + const { violations } = checkSeams(matches, [ |
| 50 | + { file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' }, |
| 51 | + ]); |
| 52 | + assert.equal(violations.length, 1); |
| 53 | + assert.equal(violations[0]?.field, 'otherField'); |
| 54 | +}); |
| 55 | + |
| 56 | +test('checkSeams flags a seam under a different target, even for an approved field name', () => { |
| 57 | + const matches = findSeamMatches([ |
| 58 | + { path: 'src/x.ts', source: 'fetchImpl?: typeof otherFn;' }, |
| 59 | + ]); |
| 60 | + const { violations } = checkSeams(matches, [ |
| 61 | + { file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' }, |
| 62 | + ]); |
| 63 | + assert.equal(violations.length, 1); |
| 64 | + assert.equal(violations[0]?.target, 'otherFn'); |
| 65 | +}); |
| 66 | + |
| 67 | +test('checkSeams reports an approval as stale when its triple matches nothing', () => { |
| 68 | + const { staleApprovals, violations } = checkSeams([], [ |
| 69 | + { file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' }, |
| 70 | + ]); |
| 71 | + assert.equal(violations.length, 0); |
| 72 | + assert.equal(staleApprovals.length, 1); |
| 73 | + assert.equal(staleApprovals[0]?.field, 'fetchImpl'); |
| 74 | +}); |
| 75 | + |
| 76 | +test('checkSeams approves every occurrence of the same triple within a file', () => { |
| 77 | + const matches = findSeamMatches([ |
| 78 | + { |
| 79 | + path: 'src/x.ts', |
| 80 | + source: 'fetchImpl?: typeof fetch;\n// ...\nfetchImpl?: typeof fetch;\n', |
| 81 | + }, |
| 82 | + ]); |
| 83 | + const { violations, staleApprovals } = checkSeams(matches, [ |
| 84 | + { file: 'src/x.ts', field: 'fetchImpl', target: 'fetch', reason: 'test' }, |
| 85 | + ]); |
| 86 | + assert.deepEqual(violations, []); |
| 87 | + assert.deepEqual(staleApprovals, []); |
| 88 | +}); |
0 commit comments