|
| 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 unapproved optional typeof property 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 | + approvalReason: null, |
| 17 | + }, |
| 18 | + ]); |
| 19 | +}); |
| 20 | + |
| 21 | +test('findSeamMatches ignores a required (non-optional) field', () => { |
| 22 | + const matches = findSeamMatches([ |
| 23 | + { path: 'a.ts', source: 'type T = { dispatch: typeof dispatchCommand };' }, |
| 24 | + ]); |
| 25 | + assert.deepEqual(matches, []); |
| 26 | +}); |
| 27 | + |
| 28 | +test('findSeamMatches finds a bare optional function parameter, not just object-type fields', () => { |
| 29 | + const matches = findSeamMatches([ |
| 30 | + { path: 'a.ts', source: 'function f(dispatch?: typeof dispatchCommand) {}\n' }, |
| 31 | + ]); |
| 32 | + assert.equal(matches.length, 1); |
| 33 | + assert.equal(matches[0]?.field, 'dispatch'); |
| 34 | + assert.equal(matches[0]?.target, 'dispatchCommand'); |
| 35 | +}); |
| 36 | + |
| 37 | +// AST-based matching finds this for free — no multiline-specific handling needed, unlike a |
| 38 | +// text-based scan (PR #2006 review, round 2). |
| 39 | +test('findSeamMatches finds a declaration whose `?:` and `typeof` land on different lines', () => { |
| 40 | + const matches = findSeamMatches([ |
| 41 | + { path: 'a.ts', source: 'type T = {\n dispatch?:\n typeof dispatchCommand;\n};\n' }, |
| 42 | + ]); |
| 43 | + assert.equal(matches.length, 1); |
| 44 | + assert.equal(matches[0]?.field, 'dispatch'); |
| 45 | + assert.equal(matches[0]?.target, 'dispatchCommand'); |
| 46 | +}); |
| 47 | + |
| 48 | +test('findSeamMatches attaches an immediately-preceding di-seam-approved comment', () => { |
| 49 | + const matches = findSeamMatches([ |
| 50 | + { |
| 51 | + path: 'a.ts', |
| 52 | + source: |
| 53 | + 'type T = {\n // di-seam-approved: legitimate, reviewed\n dispatch?: typeof dispatchCommand;\n};\n', |
| 54 | + }, |
| 55 | + ]); |
| 56 | + assert.equal(matches.length, 1); |
| 57 | + assert.equal(matches[0]?.approvalReason, 'legitimate, reviewed'); |
| 58 | +}); |
| 59 | + |
| 60 | +test('findSeamMatches joins a multi-line di-seam-approved comment block into one reason', () => { |
| 61 | + const matches = findSeamMatches([ |
| 62 | + { |
| 63 | + path: 'a.ts', |
| 64 | + source: |
| 65 | + 'type T = {\n // di-seam-approved: first line of the reason\n // second line of the reason\n dispatch?: typeof dispatchCommand;\n};\n', |
| 66 | + }, |
| 67 | + ]); |
| 68 | + assert.equal(matches[0]?.approvalReason, 'first line of the reason second line of the reason'); |
| 69 | +}); |
| 70 | + |
| 71 | +test('findSeamMatches does not treat a marker on an earlier, unrelated field as approving this one', () => { |
| 72 | + const matches = findSeamMatches([ |
| 73 | + { |
| 74 | + path: 'a.ts', |
| 75 | + source: |
| 76 | + 'type T = {\n // di-seam-approved: approves otherField only\n otherField: string;\n dispatch?: typeof dispatchCommand;\n};\n', |
| 77 | + }, |
| 78 | + ]); |
| 79 | + assert.equal(matches.length, 1); |
| 80 | + assert.equal(matches[0]?.field, 'dispatch'); |
| 81 | + assert.equal(matches[0]?.approvalReason, null); |
| 82 | +}); |
| 83 | + |
| 84 | +test('findSeamMatches requires the marker text itself, not just a nearby comment', () => { |
| 85 | + const matches = findSeamMatches([ |
| 86 | + { |
| 87 | + path: 'a.ts', |
| 88 | + source: |
| 89 | + 'type T = {\n // just a plain comment, not a marker\n dispatch?: typeof dispatchCommand;\n};\n', |
| 90 | + }, |
| 91 | + ]); |
| 92 | + assert.equal(matches[0]?.approvalReason, null); |
| 93 | +}); |
| 94 | + |
| 95 | +// PR #2006 review: a bare marker with no reason is a bypass, not a review — must not approve. |
| 96 | +test('findSeamMatches rejects a di-seam-approved marker with no reason text', () => { |
| 97 | + const matches = findSeamMatches([ |
| 98 | + { |
| 99 | + path: 'a.ts', |
| 100 | + source: 'type T = {\n // di-seam-approved:\n dispatch?: typeof dispatchCommand;\n};\n', |
| 101 | + }, |
| 102 | + ]); |
| 103 | + assert.equal(matches[0]?.approvalReason, null); |
| 104 | +}); |
| 105 | + |
| 106 | +test('findSeamMatches rejects a di-seam-approved marker whose reason is only whitespace', () => { |
| 107 | + const matches = findSeamMatches([ |
| 108 | + { |
| 109 | + path: 'a.ts', |
| 110 | + source: 'type T = {\n // di-seam-approved: \n dispatch?: typeof dispatchCommand;\n};\n', |
| 111 | + }, |
| 112 | + ]); |
| 113 | + assert.equal(matches[0]?.approvalReason, null); |
| 114 | +}); |
| 115 | + |
| 116 | +test('checkSeams flags a declaration whose only marker has no reason text', () => { |
| 117 | + const matches = findSeamMatches([ |
| 118 | + { |
| 119 | + path: 'src/x.ts', |
| 120 | + source: 'type T = {\n // di-seam-approved:\n fetchImpl?: typeof fetch;\n};\n', |
| 121 | + }, |
| 122 | + ]); |
| 123 | + const { violations } = checkSeams(matches); |
| 124 | + assert.equal(violations.length, 1); |
| 125 | + assert.equal(violations[0]?.field, 'fetchImpl'); |
| 126 | +}); |
| 127 | + |
| 128 | +test('checkSeams passes an approved match and flags an unapproved one', () => { |
| 129 | + const matches = findSeamMatches([ |
| 130 | + { |
| 131 | + path: 'src/x.ts', |
| 132 | + source: |
| 133 | + 'type T = {\n // di-seam-approved: reviewed\n fetchImpl?: typeof fetch;\n dispatch?: typeof dispatchCommand;\n};\n', |
| 134 | + }, |
| 135 | + ]); |
| 136 | + const { violations } = checkSeams(matches); |
| 137 | + assert.equal(violations.length, 1); |
| 138 | + assert.equal(violations[0]?.field, 'dispatch'); |
| 139 | +}); |
| 140 | + |
| 141 | +// This is the exact failure mode PR #2006's review flagged in the name-based version: approving |
| 142 | +// one `fetchImpl?: typeof fetch` must not silently approve a second, different one. |
| 143 | +test('checkSeams flags a second, unmarked occurrence of an approved field/target pair', () => { |
| 144 | + const matches = findSeamMatches([ |
| 145 | + { |
| 146 | + path: 'src/x.ts', |
| 147 | + source: |
| 148 | + 'type T = {\n // di-seam-approved: reviewed\n fetchImpl?: typeof fetch;\n};\ntype U = {\n fetchImpl?: typeof fetch;\n};\n', |
| 149 | + }, |
| 150 | + ]); |
| 151 | + const { violations } = checkSeams(matches); |
| 152 | + assert.equal(violations.length, 1); |
| 153 | + assert.equal(violations[0]?.line, 6); |
| 154 | +}); |
| 155 | + |
| 156 | +// PR #2006 review, round 3: a global positional table breaks on any unrelated line shift. A |
| 157 | +// code-local marker has nothing to resync — reordering unrelated declarations around an approved |
| 158 | +// one must not affect it. |
| 159 | +test('checkSeams stays passing when unrelated code is inserted above an approved declaration', () => { |
| 160 | + const before = findSeamMatches([ |
| 161 | + { |
| 162 | + path: 'src/x.ts', |
| 163 | + source: 'type T = {\n // di-seam-approved: reviewed\n fetchImpl?: typeof fetch;\n};\n', |
| 164 | + }, |
| 165 | + ]); |
| 166 | + const after = findSeamMatches([ |
| 167 | + { |
| 168 | + path: 'src/x.ts', |
| 169 | + source: |
| 170 | + '// an unrelated new import or declaration lands here\nconst unrelated = 1;\n\ntype T = {\n // di-seam-approved: reviewed\n fetchImpl?: typeof fetch;\n};\n', |
| 171 | + }, |
| 172 | + ]); |
| 173 | + assert.deepEqual(checkSeams(before).violations, []); |
| 174 | + assert.deepEqual(checkSeams(after).violations, []); |
| 175 | + assert.notEqual(before[0]?.line, after[0]?.line); |
| 176 | +}); |
0 commit comments