@boyingliu01/xp-gate 0.38.5 → 0.38.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/build-integrity/gate-10.ts +45 -27
  2. package/hooks/gate-entries/gate-10-entry.sh +7 -0
  3. package/hooks/gate-entries/gate-11-entry.sh +7 -0
  4. package/hooks/gate-entries/gate-12-entry.sh +7 -0
  5. package/hooks/gate-entries/gate-12b-entry.sh +7 -0
  6. package/hooks/gate-entries/gate-12c-entry.sh +7 -0
  7. package/hooks/gate-entries/gate-13-entry.sh +7 -0
  8. package/hooks/gate-entries/gate-5-entry.sh +48 -2
  9. package/hooks/gate-entries/gate-6-entry.sh +93 -2
  10. package/hooks/gate-entries/gate-7-entry.sh +7 -0
  11. package/hooks/gate-entries/gate-8-entry.sh +7 -0
  12. package/hooks/gate-entries/gate-9-entry.sh +7 -0
  13. package/lib/mattpocock-license-verdict.json +1 -1
  14. package/mock-policy/AGENTS.md +1 -1
  15. package/mutation/AGENTS.md +1 -1
  16. package/package.json +1 -1
  17. package/plugins/claude-code/.claude-plugin/plugin.json +1 -1
  18. package/plugins/claude-code/skills/delphi-review/AGENTS.md +1 -1
  19. package/plugins/claude-code/skills/sprint-flow/AGENTS.md +1 -1
  20. package/plugins/claude-code/skills/test-specification-alignment/AGENTS.md +1 -1
  21. package/plugins/dsh/skills/delphi-review/AGENTS.md +1 -1
  22. package/plugins/dsh/skills/sprint-flow/AGENTS.md +1 -1
  23. package/plugins/dsh/skills/test-specification-alignment/AGENTS.md +1 -1
  24. package/plugins/opencode/package.json +1 -1
  25. package/plugins/opencode/skills/delphi-review/AGENTS.md +1 -1
  26. package/plugins/opencode/skills/sprint-flow/AGENTS.md +1 -1
  27. package/plugins/opencode/skills/test-specification-alignment/AGENTS.md +1 -1
  28. package/plugins/qoder/skills/delphi-review/AGENTS.md +1 -1
  29. package/plugins/qoder/skills/sprint-flow/AGENTS.md +1 -1
  30. package/plugins/qoder/skills/test-specification-alignment/AGENTS.md +1 -1
  31. package/principles/AGENTS.md +1 -1
  32. package/scripts/generate-mirrors.cjs +85 -28
  33. package/scripts/sync-package-content.js +5 -0
  34. package/skills/delphi-review/AGENTS.md +1 -1
  35. package/skills/sprint-flow/AGENTS.md +1 -1
  36. package/skills/test-specification-alignment/AGENTS.md +1 -1
  37. package/build-integrity/__tests__/ccn-refactor-gate10.test.ts +0 -133
  38. package/build-integrity/__tests__/gate-10.test.ts +0 -340
  39. package/build-integrity/__tests__/import-resolver.test.ts +0 -747
  40. package/build-integrity/__tests__/orchestrator.test.ts +0 -302
  41. package/build-integrity/__tests__/quote-masker.test.ts +0 -177
  42. package/skills/sprint-flow/__tests__/sprint-flow.test.cjs +0 -457
  43. package/skills/sprint-flow/__tests__/verify-sprint-state.test.bats +0 -70
@@ -1,747 +0,0 @@
1
- /**
2
- * @test Gate 10 import resolver — extractImports, resolveImportPath, runImportCheck
3
- * @intent Verify that relative imports are extracted, resolved, and checked against
4
- * the project boundary. Catches the original bug where tui-plugin.ts imported
5
- * ../../src/... which exists in the repo but not in the published npm package.
6
- * @covers extractImports, resolveImportPath, runImportCheck
7
- */
8
-
9
- import { describe, it, expect, beforeEach, afterEach } from 'vitest';
10
- import fs from 'fs/promises';
11
- import path from 'path';
12
- import os from 'os';
13
- import { extractImports, resolveImportPath, runImportCheck } from '../gate-10';
14
-
15
- // Named timeout for the runImportCheck calls that assert verdicts; keeps the
16
- // Boy Scout magic-number count from growing with each new test case.
17
- const IMPORT_CHECK_TIMEOUT_MS = 30000;
18
-
19
- /**
20
- * ─── extractImports ────────────────────────────────────────────────────────────
21
- */
22
- describe('extractImports', () => {
23
- it('extracts named import with single quotes', () => {
24
- const content = `import { foo } from './bar';`;
25
- const result = extractImports(content);
26
- expect(result).toEqual([{ path: './bar', line: 1 }]);
27
- });
28
-
29
- it('extracts default import', () => {
30
- const content = `import foo from './bar';`;
31
- const result = extractImports(content);
32
- expect(result).toEqual([{ path: './bar', line: 1 }]);
33
- });
34
-
35
- it('extracts namespace import', () => {
36
- const content = `import * as foo from './bar';`;
37
- const result = extractImports(content);
38
- expect(result).toEqual([{ path: './bar', line: 1 }]);
39
- });
40
-
41
- it('extracts require() call', () => {
42
- const content = `const foo = require('./bar');`;
43
- const result = extractImports(content);
44
- expect(result).toEqual([{ path: './bar', line: 1 }]);
45
- });
46
-
47
- it('extracts dynamic import()', () => {
48
- const content = `const foo = import('./bar');`;
49
- const result = extractImports(content);
50
- expect(result).toEqual([{ path: './bar', line: 1 }]);
51
- });
52
-
53
- it('extracts named export from', () => {
54
- const content = `export { foo } from './bar';`;
55
- const result = extractImports(content);
56
- expect(result).toEqual([{ path: './bar', line: 1 }]);
57
- });
58
-
59
- it('extracts star export from', () => {
60
- const content = `export * from './bar';`;
61
- const result = extractImports(content);
62
- expect(result).toEqual([{ path: './bar', line: 1 }]);
63
- });
64
-
65
- it('extracts multiple imports from multiple lines', () => {
66
- const content = [
67
- `import { a } from './a';`,
68
- `import b from './b';`,
69
- `const c = require('./c');`,
70
- `export * from './d';`,
71
- ].join('\n');
72
- const result = extractImports(content);
73
- expect(result).toEqual([
74
- { path: './a', line: 1 },
75
- { path: './b', line: 2 },
76
- { path: './c', line: 3 },
77
- { path: './d', line: 4 },
78
- ]);
79
- });
80
-
81
- it('skips bare npm package imports', () => {
82
- const content = [
83
- `import { foo } from 'lodash';`,
84
- `import path from 'path';`,
85
- `import { bar } from './local';`,
86
- ].join('\n');
87
- const result = extractImports(content);
88
- expect(result).toEqual([{ path: './local', line: 3 }]);
89
- });
90
-
91
- it('skips node: protocol imports', () => {
92
- const content = [
93
- `import fs from 'node:fs';`,
94
- `import { readFile } from 'node:fs/promises';`,
95
- `import { bar } from './local';`,
96
- ].join('\n');
97
- const result = extractImports(content);
98
- expect(result).toEqual([{ path: './local', line: 3 }]);
99
- });
100
-
101
- it('handles double-quoted import paths', () => {
102
- const content = `import { foo } from "./bar";`;
103
- const result = extractImports(content);
104
- expect(result).toEqual([{ path: './bar', line: 1 }]);
105
- });
106
-
107
- it('handles parent-relative imports', () => {
108
- const content = `import { foo } from '../parent/bar';`;
109
- const result = extractImports(content);
110
- expect(result).toEqual([{ path: '../parent/bar', line: 1 }]);
111
- });
112
-
113
- it('handles deeply nested relative imports', () => {
114
- const content = `import { foo } from '../../../deep/path';`;
115
- const result = extractImports(content);
116
- expect(result).toEqual([{ path: '../../../deep/path', line: 1 }]);
117
- });
118
-
119
- it('returns empty array for content with no imports', () => {
120
- const content = `const x = 42;\nconsole.log(x);`;
121
- const result = extractImports(content);
122
- expect(result).toEqual([]);
123
- });
124
-
125
- it('handles import with .js extension', () => {
126
- const content = `import { foo } from './bar.js';`;
127
- const result = extractImports(content);
128
- expect(result).toEqual([{ path: './bar.js', line: 1 }]);
129
- });
130
-
131
- it('handles side-effect import', () => {
132
- const content = `import './side-effect';`;
133
- const result = extractImports(content);
134
- expect(result).toEqual([{ path: './side-effect', line: 1 }]);
135
- });
136
-
137
- /**
138
- * Issue #759 — import TEXT that is not import CODE.
139
- *
140
- * Every case below is a shape that a real project contains on purpose (a test
141
- * pinning a scanner's own behaviour, a doc block, a parser fixture) and that
142
- * the scanner used to report as a violation for a path that never existed.
143
- * Eight of these eleven were reproduced in dialog-survey, where they blocked
144
- * pushes.
145
- *
146
- * The fixture strings are built with template literals so the nesting is
147
- * visible; `BS` keeps a literal backslash unambiguous, because writing `\\'`
148
- * inside a template literal silently yields just `'` and turns the test into
149
- * one that passes against the bug.
150
- */
151
- describe('ignores import text inside strings, templates and comments (#759)', () => {
152
- const BS = String.fromCharCode(92);
153
- // Built from a code point so a backtick in the fixture cannot end the
154
- // surrounding template literal by accident (same trap as `BS` above).
155
- const BT = String.fromCharCode(96);
156
-
157
- it('ignores import text inside a double-quoted string', () => {
158
- const content = `const x = "import foo from './missing';";`;
159
- expect(extractImports(content)).toEqual([]);
160
- });
161
-
162
- it('ignores import text inside a single-quoted string', () => {
163
- const content = `const x = 'import foo from "./missing";';`;
164
- expect(extractImports(content)).toEqual([]);
165
- });
166
-
167
- it('ignores require() text inside a string', () => {
168
- const content = `const x = "require('./missing')";`;
169
- expect(extractImports(content)).toEqual([]);
170
- });
171
-
172
- it('ignores dynamic import text inside a string', () => {
173
- const content = `const x = "import('./missing')";`;
174
- expect(extractImports(content)).toEqual([]);
175
- });
176
-
177
- it('ignores export-from text inside a string', () => {
178
- const content = `const x = "export * from './missing'";`;
179
- expect(extractImports(content)).toEqual([]);
180
- });
181
-
182
- it('ignores import text inside a single-line template literal', () => {
183
- const content = "const x = `import foo from './missing';`;";
184
- expect(extractImports(content)).toEqual([]);
185
- });
186
-
187
- it('ignores import text inside a multi-line template literal', () => {
188
- const content = "const x = `\nimport foo from './missing';\n`;";
189
- expect(extractImports(content)).toEqual([]);
190
- });
191
-
192
- it('ignores import text inside a trailing line comment', () => {
193
- const content = `const y = 1; // import foo from './missing';`;
194
- expect(extractImports(content)).toEqual([]);
195
- });
196
-
197
- it('ignores import text inside a block comment', () => {
198
- const content = `/* import foo from './missing'; */`;
199
- expect(extractImports(content)).toEqual([]);
200
- });
201
-
202
- it('ignores import text inside JSDoc', () => {
203
- const content = `/**\n * import foo from './missing';\n */`;
204
- expect(extractImports(content)).toEqual([]);
205
- });
206
-
207
- /**
208
- * The other half of a comment: that it ENDS.
209
- *
210
- * The two cases above only prove text INSIDE a comment is ignored. A masker
211
- * that never leaves block-comment mode keeps both green while swallowing
212
- * every real import after the closing delimiter — found by mutation: making
213
- * the closing check unreachable survived all 58 tests. These pin the resume.
214
- */
215
- it('resumes scanning after a block comment closes', () => {
216
- const content = `/* import gone from './missing'; */\nimport real from './real';`;
217
- expect(extractImports(content)).toEqual([{ path: './real', line: 2 }]);
218
- });
219
-
220
- it('resumes scanning after a JSDoc block closes', () => {
221
- const content = `/**\n * import gone from './missing';\n */\nimport real from './real';`;
222
- expect(extractImports(content)).toEqual([{ path: './real', line: 4 }]);
223
- });
224
-
225
- it('resumes scanning after a line comment ends at the newline', () => {
226
- const content = `// import gone from './missing';\nimport real from './real';`;
227
- expect(extractImports(content)).toEqual([{ path: './real', line: 2 }]);
228
- });
229
-
230
- it('still treats a template interpolation as code', () => {
231
- const content = "const x = `${import('./real')}`;";
232
- expect(extractImports(content)).toEqual([{ path: './real', line: 1 }]);
233
- });
234
-
235
- /**
236
- * Nested interpolations must return to the INNER template, not to code.
237
- *
238
- * A single integer depth counter cannot express that: on entering the inner
239
- * `${`, assigning (instead of pushing) made the inner `}` look like the
240
- * close of the outer one, so the masker stayed in template mode and every
241
- * real import after the line was masked. Measured against the counter
242
- * version, `extractImports` returned `[]` for the case below.
243
- */
244
- it('stays aligned through a nested template interpolation', () => {
245
- const Q = BT;
246
- const content =
247
- 'const a = ' + Q + '${' + Q + '${x}' + Q + '}' + Q + ';\n' +
248
- "import real from './real';\n";
249
- expect(extractImports(content)).toEqual([{ path: './real', line: 2 }]);
250
- });
251
-
252
- it('stays aligned through nested interpolation containing an import', () => {
253
- const Q = BT;
254
- const content =
255
- 'const a = ' + Q + '${' + Q + '${import("./inner")}' + Q + '}' + Q + ';\n' +
256
- "import real from './real';\n";
257
- // The inner import is inside code (an interpolation), so it counts; the
258
- // outer import must survive the nesting too.
259
- expect(extractImports(content)).toEqual([
260
- { path: './inner', line: 1 },
261
- { path: './real', line: 2 },
262
- ]);
263
- });
264
-
265
- /**
266
- * A regex literal that contains `//` used to open a phantom line comment.
267
- * The masker does not parse regex literals (documented limitation), but a
268
- * line holding both a URL-ish regex and a real import must not lose the
269
- * import. The `/` of `//` inside a regex is escaped in real code, and an
270
- * unescaped `//` in a regex is a syntax error, so the remaining risk is
271
- * confined to generated/minified text — pinned here so it stays a KNOWN
272
- * limitation rather than an untested assumption.
273
- */
274
- it('keeps a real import on the same line as a regex literal', () => {
275
- const content = 'const re = /(?:a)/; import real from "./real";\n';
276
- expect(extractImports(content)).toEqual([{ path: './real', line: 1 }]);
277
- });
278
-
279
- /**
280
- * A line that STARTS with a comment can still carry a real import.
281
- *
282
- * The original guard asked `trimmed.startsWith('/*')` on the raw line and
283
- * skipped the whole line, so `/* eslint-disable */ import real from ...`
284
- * was never scanned — a MISS, which is the one direction a compliance gate
285
- * must not fail in. Measured before the fix: `[]`.
286
- */
287
- it('keeps an import that follows a leading block comment', () => {
288
- const content = '/* eslint-disable */ import real from "./real";\n';
289
- expect(extractImports(content)).toEqual([{ path: './real', line: 1 }]);
290
- });
291
-
292
- it('still ignores import text that is only a comment', () => {
293
- const content = '/* import fake from "./fake" */\n';
294
- expect(extractImports(content)).toEqual([]);
295
- });
296
-
297
- it('still ignores import text on a JSDoc continuation line', () => {
298
- const content = ' * import fake from "./fake"\n';
299
- expect(extractImports(content)).toEqual([]);
300
- });
301
-
302
- /**
303
- * A static import whose path sits on a LATER line. Multi-line imports are
304
- * ordinary formatting, and a strictly per-line scan misses them entirely —
305
- * measured `[]` before joining, which is a MISS in the direction that
306
- * matters (a real boundary violation would not be reported).
307
- */
308
- it('finds an import whose path is on a continuation line', () => {
309
- const content = 'import {\n foo,\n} from "./bar";\n';
310
- expect(extractImports(content)).toEqual([{ path: './bar', line: 1 }]);
311
- });
312
-
313
- it('does not double-report a multi-line import, and keeps the next line', () => {
314
- const content = 'import {\n a,\n} from "./b";\nimport c from "./c";\n';
315
- expect(extractImports(content)).toEqual([
316
- { path: './b', line: 1 },
317
- { path: './c', line: 4 },
318
- ]);
319
- });
320
-
321
- it('finds a dynamic import written with a space before the paren', () => {
322
- // `import ('./x')` is legal and appears in formatted code; the pattern
323
- // required `import(` exactly and measured `[]`.
324
- const content = "const m = import ('./x');\n";
325
- expect(extractImports(content)).toEqual([{ path: './x', line: 1 }]);
326
- });
327
-
328
- it('does not report import.meta as a dynamic import', () => {
329
- const content = 'const u = import.meta.url;\n';
330
- expect(extractImports(content)).toEqual([]);
331
- });
332
-
333
- /**
334
- * KNOWN LIMITATION, pinned so it cannot drift unnoticed.
335
- *
336
- * A quote inside a regex literal opens a phantom string. Measured output for
337
- * the fixture below is `[{ path: './fake', line: 2 }]`: the import text
338
- * inside the real string is reported as if it were code, and the genuine
339
- * import on line 3 is missed. Both directions, which is why the masker's
340
- * header no longer claims "can only cause a MISS".
341
- *
342
- * It is tolerable because a regex holding an unescaped quote is a SYNTAX
343
- * ERROR — no file that parses can contain line 1. This test exists to make
344
- * that reasoning explicit: if it ever starts failing differently, the
345
- * limitation changed and the header comment must be re-measured.
346
- */
347
- it('documents the regex-quote limitation instead of pretending it cannot happen', () => {
348
- const SQ = String.fromCharCode(39);
349
- const DQ = String.fromCharCode(34);
350
- const content =
351
- 'const re = /' + SQ + '/;\n' +
352
- 'const s = ' + DQ + 'import x from ' + SQ + './fake' + SQ + DQ + ';\n' +
353
- 'import real from ' + DQ + './real' + DQ + ';\n';
354
- expect(extractImports(content)).toEqual([{ path: './fake', line: 2 }]);
355
- });
356
-
357
- it('keeps line numbers truthful across a line continuation', () => {
358
- // A backslash immediately followed by a newline is a legal continuation
359
- // inside a string literal. Masking it as a plain space deleted a line, so
360
- // an import on line 3 was reported as line 2 — measured against the
361
- // version that emitted a space for the newline.
362
- const NL = String.fromCharCode(10);
363
- const content =
364
- "const s = 'aaa" + BS + NL + "bbb';" + NL +
365
- "import real from './real';" + NL;
366
- expect(extractImports(content)).toEqual([{ path: './real', line: 3 }]);
367
- });
368
-
369
- it('resumes scanning after an unterminated-looking line comment', () => {
370
- const content = "const u = 'http://x'; import real from './real';\n";
371
- // The `//` sits inside a string, so it is text — the import is real code.
372
- expect(extractImports(content)).toEqual([{ path: './real', line: 1 }]);
373
- });
374
-
375
- it('does not end a string early on an escaped quote', () => {
376
- const content = `const x = 'it${BS}'s import foo from "./missing"';`;
377
- expect(extractImports(content)).toEqual([]);
378
- });
379
-
380
- it('does not end a string early on an escaped double quote', () => {
381
- const content = `const x = "say ${BS}"hi import foo from './missing'";`;
382
- expect(extractImports(content)).toEqual([]);
383
- });
384
-
385
- it('reports the real import on a line that also holds import text', () => {
386
- const content = [
387
- `import real from './real';`,
388
- `const doc = "import fake from './fake';";`,
389
- ].join('\n');
390
- expect(extractImports(content)).toEqual([{ path: './real', line: 1 }]);
391
- });
392
- });
393
- });
394
-
395
- /**
396
- * ─── resolveImportPath ─────────────────────────────────────────────────────────
397
- */
398
- describe('resolveImportPath', () => {
399
- let tmpDir: string;
400
-
401
- beforeEach(async () => {
402
- tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'import-resolver-test-'));
403
- });
404
-
405
- afterEach(async () => {
406
- await fs.rm(tmpDir, { recursive: true, force: true });
407
- });
408
-
409
- it('returns null for bare npm package imports', () => {
410
- const result = resolveImportPath('lodash', '/some/file.ts');
411
- expect(result).toBeNull();
412
- });
413
-
414
- it('returns null for node: protocol imports', () => {
415
- const result = resolveImportPath('node:fs', '/some/file.ts');
416
- expect(result).toBeNull();
417
- });
418
-
419
- it('resolves relative import to existing .ts file', async () => {
420
- const targetFile = path.join(tmpDir, 'bar.ts');
421
- await fs.writeFile(targetFile, 'export const x = 1;');
422
-
423
- const fromFile = path.join(tmpDir, 'foo.ts');
424
- const result = resolveImportPath('./bar', fromFile);
425
- expect(result).toBe(targetFile);
426
- });
427
-
428
- it('resolves relative import to existing .tsx file', async () => {
429
- const targetFile = path.join(tmpDir, 'component.tsx');
430
- await fs.writeFile(targetFile, 'export const X = () => null;');
431
-
432
- const fromFile = path.join(tmpDir, 'app.ts');
433
- const result = resolveImportPath('./component', fromFile);
434
- expect(result).toBe(targetFile);
435
- });
436
-
437
- it('resolves relative import to existing .js file', async () => {
438
- const targetFile = path.join(tmpDir, 'util.js');
439
- await fs.writeFile(targetFile, 'module.exports = {};');
440
-
441
- const fromFile = path.join(tmpDir, 'main.ts');
442
- const result = resolveImportPath('./util', fromFile);
443
- expect(result).toBe(targetFile);
444
- });
445
-
446
- it('resolves relative import with explicit .js extension to .ts file', async () => {
447
- // TypeScript allows `import './bar.js'` to resolve to `bar.ts`
448
- const targetFile = path.join(tmpDir, 'bar.ts');
449
- await fs.writeFile(targetFile, 'export const x = 1;');
450
-
451
- const fromFile = path.join(tmpDir, 'foo.ts');
452
- const result = resolveImportPath('./bar.js', fromFile);
453
- expect(result).toBe(targetFile);
454
- });
455
-
456
- it('resolves relative import to index.ts in directory', async () => {
457
- const subDir = path.join(tmpDir, 'mydir');
458
- await fs.mkdir(subDir);
459
- const indexFile = path.join(subDir, 'index.ts');
460
- await fs.writeFile(indexFile, 'export const x = 1;');
461
-
462
- const fromFile = path.join(tmpDir, 'foo.ts');
463
- const result = resolveImportPath('./mydir', fromFile);
464
- expect(result).toBe(indexFile);
465
- });
466
-
467
- it('resolves relative import to index.js in directory', async () => {
468
- const subDir = path.join(tmpDir, 'mydir');
469
- await fs.mkdir(subDir);
470
- const indexFile = path.join(subDir, 'index.js');
471
- await fs.writeFile(indexFile, 'module.exports = {};');
472
-
473
- const fromFile = path.join(tmpDir, 'foo.ts');
474
- const result = resolveImportPath('./mydir', fromFile);
475
- expect(result).toBe(indexFile);
476
- });
477
-
478
- it('resolves parent-relative import', async () => {
479
- const subDir = path.join(tmpDir, 'sub');
480
- await fs.mkdir(subDir);
481
- const targetFile = path.join(tmpDir, 'bar.ts');
482
- await fs.writeFile(targetFile, 'export const x = 1;');
483
-
484
- const fromFile = path.join(subDir, 'foo.ts');
485
- const result = resolveImportPath('../bar', fromFile);
486
- expect(result).toBe(targetFile);
487
- });
488
-
489
- it('returns null when target file does not exist', () => {
490
- const fromFile = path.join(tmpDir, 'foo.ts');
491
- const result = resolveImportPath('./nonexistent', fromFile);
492
- expect(result).toBeNull();
493
- });
494
-
495
- it('resolves import with .mjs extension', async () => {
496
- const targetFile = path.join(tmpDir, 'mod.mjs');
497
- await fs.writeFile(targetFile, 'export const x = 1;');
498
-
499
- const fromFile = path.join(tmpDir, 'foo.ts');
500
- const result = resolveImportPath('./mod.mjs', fromFile);
501
- expect(result).toBe(targetFile);
502
- });
503
-
504
- it('resolves import with .cjs extension', async () => {
505
- const targetFile = path.join(tmpDir, 'mod.cjs');
506
- await fs.writeFile(targetFile, 'module.exports = {};');
507
-
508
- const fromFile = path.join(tmpDir, 'foo.ts');
509
- const result = resolveImportPath('./mod.cjs', fromFile);
510
- expect(result).toBe(targetFile);
511
- });
512
-
513
- it('resolves import with .jsx extension', async () => {
514
- const targetFile = path.join(tmpDir, 'comp.jsx');
515
- await fs.writeFile(targetFile, 'export default function() { return null; }');
516
-
517
- const fromFile = path.join(tmpDir, 'app.ts');
518
- const result = resolveImportPath('./comp', fromFile);
519
- expect(result).toBe(targetFile);
520
- });
521
- });
522
-
523
- /**
524
- * ─── runImportCheck ────────────────────────────────────────────────────────────
525
- */
526
- describe('runImportCheck', () => {
527
- let tmpDir: string;
528
-
529
- beforeEach(async () => {
530
- tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'import-check-test-'));
531
- });
532
-
533
- afterEach(async () => {
534
- await fs.rm(tmpDir, { recursive: true, force: true });
535
- });
536
-
537
- it('returns pass when all relative imports resolve within project', async () => {
538
- // Create project structure
539
- const srcDir = path.join(tmpDir, 'src');
540
- await fs.mkdir(srcDir);
541
-
542
- const barFile = path.join(srcDir, 'bar.ts');
543
- await fs.writeFile(barFile, 'export const x = 1;');
544
-
545
- const fooFile = path.join(srcDir, 'foo.ts');
546
- await fs.writeFile(fooFile, `import { x } from './bar';\nconsole.log(x);\n`);
547
-
548
- const result = await runImportCheck([fooFile], tmpDir, 30000);
549
- expect(result.status).toBe('pass');
550
- expect(result.violations).toEqual([]);
551
- });
552
-
553
- it('returns fail when import resolves outside project root (original bug)', async () => {
554
- // Simulate the original bug: a deeply nested file imports a path that
555
- // resolves outside the project root. From pkg/dist/sub/, the import
556
- // ../../../../outside escapes tmpDir (the project root).
557
- const nestedDir = path.join(tmpDir, 'pkg', 'dist', 'sub');
558
- await fs.mkdir(nestedDir, { recursive: true });
559
-
560
- const badFile = path.join(nestedDir, 'file.ts');
561
- await fs.writeFile(
562
- badFile,
563
- `import { x } from '../../../../outside';\n`
564
- );
565
-
566
- const result = await runImportCheck([badFile], tmpDir, 30000);
567
- expect(result.status).toBe('fail');
568
- expect(result.violations.length).toBeGreaterThan(0);
569
- expect(result.violations[0].reason).toMatch(/escapes|outside|boundary/i);
570
- });
571
-
572
- it('returns fail when imported file does not exist', async () => {
573
- const srcDir = path.join(tmpDir, 'src');
574
- await fs.mkdir(srcDir);
575
-
576
- const fooFile = path.join(srcDir, 'foo.ts');
577
- await fs.writeFile(fooFile, `import { x } from './nonexistent';\n`);
578
-
579
- const result = await runImportCheck([fooFile], tmpDir, 30000);
580
- expect(result.status).toBe('fail');
581
- expect(result.violations.length).toBeGreaterThan(0);
582
- expect(result.violations[0].reason).toMatch(/not found|does not exist|missing/i);
583
- });
584
-
585
- it('passes when projectRoot spelling differs in drive case/slashes (Windows, #508)', async () => {
586
- // git rev-parse --show-toplevel reports "D:/repo" while path.resolve of a
587
- // repo-relative changed file inherits process.cwd()'s "d:\repo" drive case.
588
- // The case-sensitive startsWith judged every import as boundary-escaping,
589
- // blocking any TS-bearing push. The same root spelled the other way must
590
- // pass on Windows; drive letters do not exist on POSIX so skip there.
591
- if (process.platform !== 'win32') return;
592
-
593
- let fooFile: string;
594
- try {
595
- const srcDir = path.join(tmpDir, 'src');
596
- await fs.mkdir(srcDir);
597
-
598
- const barFile = path.join(srcDir, 'bar.ts');
599
- await fs.writeFile(barFile, 'export const x = 1;');
600
-
601
- fooFile = path.join(srcDir, 'foo.ts');
602
- await fs.writeFile(fooFile, `import { x } from './bar';\nconsole.log(x);\n`);
603
- } catch (error) {
604
- throw new Error(`Fixture setup failed: ${error}`);
605
- }
606
-
607
- const altRoot = tmpDir.replace(/\\/g, '/').replace(/^[a-zA-Z]:/, (m) => m.toLowerCase());
608
- expect(altRoot).not.toBe(tmpDir);
609
-
610
- const result = await runImportCheck([fooFile], altRoot, IMPORT_CHECK_TIMEOUT_MS);
611
- expect(result.status).toBe('pass');
612
- expect(result.violations).toEqual([]);
613
- });
614
-
615
- it('still fails a true boundary escape after case normalization (#508 follow-up)', async () => {
616
- // Normalization must only absorb case/spelling differences, never widen the
617
- // boundary: an import that genuinely leaves the project root keeps failing
618
- // even when the root is spelled with a different drive-letter case.
619
- if (process.platform !== 'win32') return;
620
-
621
- let badFile: string;
622
- try {
623
- const nestedDir = path.join(tmpDir, 'pkg', 'dist', 'sub');
624
- await fs.mkdir(nestedDir, { recursive: true });
625
-
626
- badFile = path.join(nestedDir, 'file.ts');
627
- await fs.writeFile(badFile, `import { x } from '../../../../outside';\n`);
628
- } catch (error) {
629
- throw new Error(`Fixture setup failed: ${error}`);
630
- }
631
-
632
- const altRoot = tmpDir.replace(/\\/g, '/').replace(/^[a-zA-Z]:/, (m) => m.toLowerCase());
633
-
634
- const result = await runImportCheck([badFile], altRoot, IMPORT_CHECK_TIMEOUT_MS);
635
- expect(result.status).toBe('fail');
636
- expect(result.violations.length).toBeGreaterThan(0);
637
- expect(result.violations[0].reason).toMatch(/escapes|outside|boundary/i);
638
- });
639
-
640
- it('skips YAML files containing JavaScript-like require text', async () => {
641
- const workflowDir = path.join(tmpDir, '.github', 'workflows');
642
- await fs.mkdir(workflowDir, { recursive: true });
643
-
644
- const workflowFile = path.join(workflowDir, 'cross-platform-ci.yml');
645
- await fs.writeFile(workflowFile, `run: node -e "require('../test')"\n`);
646
-
647
- const result = await runImportCheck([workflowFile], tmpDir, 30000);
648
- expect(result.status).toBe('pass');
649
- expect(result.violations).toEqual([]);
650
- });
651
-
652
- it('still reports missing relative imports in supported JavaScript modules', async () => {
653
- const sourceFile = path.join(tmpDir, 'source.mjs');
654
- await fs.writeFile(sourceFile, `import './missing.js';\n`);
655
-
656
- const result = await runImportCheck([sourceFile], tmpDir, 30000);
657
- expect(result.status).toBe('fail');
658
- expect(result.violations).toHaveLength(1);
659
- expect(result.violations[0].importPath).toBe('./missing.js');
660
- });
661
-
662
- it('returns pass when only bare npm imports are used', async () => {
663
- const srcDir = path.join(tmpDir, 'src');
664
- await fs.mkdir(srcDir);
665
-
666
- const fooFile = path.join(srcDir, 'foo.ts');
667
- await fs.writeFile(
668
- fooFile,
669
- `import path from 'path';\nimport fs from 'node:fs';\nimport _ from 'lodash';\n`
670
- );
671
-
672
- const result = await runImportCheck([fooFile], tmpDir, 30000);
673
- expect(result.status).toBe('pass');
674
- expect(result.violations).toEqual([]);
675
- });
676
-
677
- it('returns pass when no changed files are provided', async () => {
678
- const result = await runImportCheck([], tmpDir, 30000);
679
- expect(result.status).toBe('pass');
680
- expect(result.violations).toEqual([]);
681
- });
682
-
683
- it('checks multiple changed files', async () => {
684
- const srcDir = path.join(tmpDir, 'src');
685
- await fs.mkdir(srcDir);
686
-
687
- const barFile = path.join(srcDir, 'bar.ts');
688
- await fs.writeFile(barFile, 'export const x = 1;');
689
-
690
- const goodFile = path.join(srcDir, 'good.ts');
691
- await fs.writeFile(goodFile, `import { x } from './bar';\n`);
692
-
693
- const badFile = path.join(srcDir, 'bad.ts');
694
- await fs.writeFile(badFile, `import { y } from './missing';\n`);
695
-
696
- const result = await runImportCheck([goodFile, badFile], tmpDir, 30000);
697
- expect(result.status).toBe('fail');
698
- expect(result.violations.length).toBe(1);
699
- expect(result.violations[0].file).toBe(badFile);
700
- });
701
-
702
- it('skips files that do not exist on disk', async () => {
703
- const result = await runImportCheck(['/nonexistent/file.ts'], tmpDir, 30000);
704
- expect(result.status).toBe('pass');
705
- expect(result.violations).toEqual([]);
706
- });
707
-
708
- it('reports correct line number for violation', async () => {
709
- const srcDir = path.join(tmpDir, 'src');
710
- await fs.mkdir(srcDir);
711
-
712
- const fooFile = path.join(srcDir, 'foo.ts');
713
- await fs.writeFile(
714
- fooFile,
715
- [`const x = 1;`, `const y = 2;`, `import { z } from './missing';`, `console.log(x, y);`].join('\n')
716
- );
717
-
718
- const result = await runImportCheck([fooFile], tmpDir, 30000);
719
- expect(result.status).toBe('fail');
720
- expect(result.violations[0].line).toBe(3);
721
- });
722
-
723
- it('handles import with explicit .js extension resolving to .ts', async () => {
724
- const srcDir = path.join(tmpDir, 'src');
725
- await fs.mkdir(srcDir);
726
-
727
- const barFile = path.join(srcDir, 'bar.ts');
728
- await fs.writeFile(barFile, 'export const x = 1;');
729
-
730
- const fooFile = path.join(srcDir, 'foo.ts');
731
- await fs.writeFile(fooFile, `import { x } from './bar.js';\n`);
732
-
733
- const result = await runImportCheck([fooFile], tmpDir, 30000);
734
- expect(result.status).toBe('pass');
735
- expect(result.violations).toEqual([]);
736
- });
737
-
738
- it('includes durationMs in result', async () => {
739
- const result = await runImportCheck([], tmpDir, 30000);
740
- expect(result.durationMs).toBeGreaterThanOrEqual(0);
741
- });
742
-
743
- it('includes message in result', async () => {
744
- const result = await runImportCheck([], tmpDir, 30000);
745
- expect(typeof result.message).toBe('string');
746
- });
747
- });