// The enforcement half of `scripts/node-imports.ts`: this file IS the build error. The gate's // `unit` step runs every `scripts/**/*.test.ts`, so a new unexplained `node:` import fails // `bun run verify` with no extra wiring. import { describe, expect, setDefaultTimeout, test } from 'bun:test'; import { NODE_IMPORT_PINS } from './lib/node-import-pins'; import { REPO_SCAN_TIMEOUT_MS, repoRoot } from './lib/run'; import { checkNodeImports, hasWhy, nodeImportFindingFor, nodeImportGaps, scanNodeImports, } from './node-imports'; // Every test below scans the whole tree, so the budget is the file's default rather than a third // argument per test — see `REPO_SCAN_TIMEOUT_MS`. This file ran on Bun's 5000ms default until // 2026-08-27 and went red on a runtime 1.3x slower, which is less than one noisy CI runner. setDefaultTimeout(REPO_SCAN_TIMEOUT_MS); const found = (source: string): readonly string[] => scanNodeImports('packages/x/src/a.ts', source).map((site) => site.specifier); describe('a node: import with nothing saying why', () => { test('a static import is reported, with the specifier', () => { expect(found("import { writeSync } from 'node:fs';")).toEqual(['node:fs']); }); test('every spelling that reaches a builtin', () => { expect(found("import 'node:crypto';")).toEqual(['node:crypto']); expect(found("const h = await import('node:async_hooks');")).toEqual(['node:async_hooks']); expect(found("const p = require('node:path');")).toEqual(['node:path']); expect(found("import { join } from 'node:path/posix';")).toEqual(['node:path/posix']); }); // Sweep 11 R4: two spellings that reach a builtin with no `from`, `import(` quote or `require`. test('a template-literal specifier and process.getBuiltinModule reach the same builtin', () => { expect(found('const fs = await import(`node:fs`);')).toEqual(['node:fs']); expect(found("const fs = process.getBuiltinModule('node:fs');")).toEqual(['node:fs']); expect(found('const os = process.getBuiltinModule("os");')).toEqual(['os']); // A computed specifier is not a static one, and names nothing this rule can check. // biome-ignore lint/suspicious/noTemplateCurlyInString: the input is source text — the literal ${…} is the case under test expect(found('const m = await import(`node:${name}`);')).toEqual([]); expect( found( "// why: Bun has no fork()\nconst cp = process.getBuiltinModule('node:child_process');", ), ).toEqual([]); }); test('the BARE spelling reaches the same builtin and owes the same why: (K17)', () => { expect(found("import { readFileSync } from 'fs';")).toEqual(['fs']); expect(found("import { readFile } from 'fs/promises';")).toEqual(['fs/promises']); expect(found("const cp = require('child_process');")).toEqual(['child_process']); expect(found("const h = await import('async_hooks');")).toEqual(['async_hooks']); expect(found("import 'crypto';")).toEqual(['crypto']); expect(found("// why: Bun has no fork()\nconst cp = require('child_process');")).toEqual([]); }); test('the finding names the token to write and gives a worked example', () => { const gaps = checkNodeImports({ files: [{ path: 'packages/x/src/log.ts', source: "import { writeSync } from 'node:fs';" }], pins: {}, }); const finding = nodeImportFindingFor(gaps[0] as never); expect(finding.code).toBe('X_NODE_IMPORT_UNEXPLAINED'); expect(finding.at).toBe('packages/x/src/log.ts:1'); expect(finding.fix).toContain('why:'); }); }); describe('what counts as saying why', () => { test('the token on the import own line', () => { expect( found("import { writeSync } from 'node:fs'; // why: Bun has no sync stdout write"), ).toEqual([]); }); test('a line comment directly above', () => { expect( found("// why: Bun ships no temp-directory API\nimport { mkdtemp } from 'node:fs';"), ).toEqual([]); }); test('a doc comment above, which is where this framework already writes the sentence', () => { const source = [ '/**', ' * The one synchronous stdout write there is.', ' * why: Bun has no synchronous stdout write of its own.', ' */', "import { writeSync } from 'node:fs';", ].join('\n'); expect(found(source)).toEqual([]); }); test('WHY: counts — the token, not its casing', () => { expect(found("// WHY: no Bun equivalent\nimport { tmpdir } from 'node:os';")).toEqual([]); }); /** * One `why:` covers the contiguous block of `node:` imports beneath it: `mkdtemp` and `tmpdir` * are reached for together and one sentence says why. Counting the second as unexplained made * authors repeat the sentence, which is noise the rule never asked for. */ test('a why: covers the contiguous node: import block beneath it', () => { const source = [ '// why: Bun ships no temp-directory API and no tmpdir()', "import { mkdtemp } from 'node:fs/promises';", "import { tmpdir } from 'node:os';", 'import {', ' join,', "} from 'node:path';", ].join('\n'); expect(found(source)).toEqual([]); }); /** What still makes "directly above" mean something: the block ends where it stops being one. */ test('a blank line, another statement or a non-node import ends the block', () => { const head = ['// why: Bun ships no temp-directory API', "import { mkdtemp } from 'node:fs';"]; const tail = "import { tmpdir } from 'node:os';"; expect(found([...head, '', tail].join('\n'))).toEqual(['node:os']); expect(found([...head, 'const a = 1;', tail].join('\n'))).toEqual(['node:os']); expect(found([...head, "import { x } from './x';", tail].join('\n'))).toEqual(['node:os']); }); test('a why: on one import`s own line is that import`s alone', () => { const source = [ "import { mkdtemp } from 'node:fs'; // why: Bun ships no temp-directory API", "import { tmpdir } from 'node:os';", ].join('\n'); expect(found(source)).toEqual(['node:os']); }); test('a why: above a wrapped import covers it, though the specifier is three lines down', () => { const source = ['// why: no Bun path joiner', 'import {', ' join,', "} from 'node:path';"]; expect(found(source.join('\n'))).toEqual([]); expect(found(source.slice(1).join('\n'))).toEqual(['node:path']); }); test('a blank line between the comment and the import is still directly above', () => { expect(found("// why: no Bun path joiner\n\nimport { join } from 'node:path';")).toEqual([]); }); test('hasWhy is asked of lines, so it can be reasoned about on its own', () => { expect(hasWhy(['// why: x', "import 'node:fs';"], 1)).toBe(true); expect(hasWhy(['const a = 1;', "import 'node:fs';"], 1)).toBe(false); }); }); describe('what the rule stays silent about', () => { test('a commented-out import is not an import', () => { expect(found("// import { writeSync } from 'node:fs';")).toEqual([]); expect(found("/**\n * import { writeSync } from 'node:fs';\n */")).toEqual([]); }); /** * The half a line-prefix test cannot see, and the reason the mask replaced one: the comment does * not start the line, so `trimStart().startsWith('//')` reads straight past it. */ test('a trailing comment on a line of real code is still a comment', () => { expect(found("const a = 1; // import { writeSync } from 'node:fs';")).toEqual([]); }); test('a package specifier that merely starts with the letters is not a builtin', () => { expect(found("import { x } from 'nodemailer';")).toEqual([]); expect(found("import { x } from './node-imports';")).toEqual([]); expect(found("import { x } from 'fsevents';")).toEqual([]); expect(found("import { x } from './fs';")).toEqual([]); expect(found("import { serve } from 'bun';")).toEqual([]); expect(found("import { t } from '@ultimat3/path';")).toEqual([]); }); test('a member call that shares the keyword is a method, not an import', () => { expect(found("const m = loader.require('fs');")).toEqual([]); expect(found("const m = await loader.import('fs');")).toEqual([]); expect(found("const m = loader?.require('node:fs');")).toEqual([]); expect(found("const m = loader\n .import('node:path');")).toEqual([]); expect(found("const b = Buffer.from('fs');")).toEqual([]); expect(found("const m = myrequire('fs');")).toEqual([]); expect(found("const m = $import('fs');")).toEqual([]); }); test('the direct call is still reported beside a member call', () => { expect(found("loader.require('fs');\nconst cp = require('child_process');")).toEqual([ 'child_process', ]); }); }); /** * INPUT vs IMPORT. A rule's own test spells the forbidden shape as DATA and hands it to the pure * function above — this file does it two dozen times — and a checker that reports its own fixtures * is a checker whose count nobody can drain. Both carriers are exempt, and the COMMENT one is the * half a string-literal exemption alone misses: `scripts/async-context-guard.test.ts:106` explains * the shape by quoting it in a doc comment. `dead-docs-host.ts` states the same carve-out — "a * comment naming the host as the thing that was removed cannot 404". */ describe('a fixture is not an import', () => { test('inside a string literal — the shape as this test own data', () => { expect(found(`const fixture = "import { tmpdir } from 'node:os';";`)).toEqual([]); expect(found("const fixture = `import { tmpdir } from 'node:os';`;")).toEqual([]); }); test('inside a comment — prose ABOUT the shape, which no process evaluates', () => { const source = [ '/**', " * A module may not write `import { tmpdir } from 'node:os';` without saying why.", ' */', 'export const RULE = 1;', ].join('\n'); expect(found(source)).toEqual([]); }); test('a real import in the same file as a fixture is still reported', () => { const source = [ `const fixture = "import { tmpdir } from 'node:os';";`, "import { join } from 'node:path';", ].join('\n'); expect(found(source)).toEqual(['node:path']); expect(scanNodeImports('packages/x/src/a.ts', source)[0]?.line).toBe(2); }); }); describe('a test file is source', () => { /** * The whole of #365. `checkNodeImports` opened `if (isTestPath(file.path)) continue`, so a * package could hold a dozen unexplained imports in its suites under a green gate — and one did: * `storage` was flagged by review on #364 with no row in the pin table at all. */ test('an unexplained node: import in a .test.ts is reported', () => { const gaps = checkNodeImports({ files: [{ path: 'packages/x/src/a.test.ts', source: "import { tmpdir } from 'node:os';" }], pins: {}, }); expect(gaps.map((gap) => `${gap.pkg}:${String(gap.found)}`)).toEqual(['x:1']); expect(nodeImportFindingFor(gaps[0] as never).at).toBe('packages/x/src/a.test.ts:1'); }); test('and a why: in a test file clears it, the same sentence a driver writes', () => { expect( checkNodeImports({ files: [ { path: 'packages/x/src/a.test.ts', source: "// why: Bun exposes no tmpdir()\nimport { tmpdir } from 'node:os';", }, ], pins: {}, }), ).toEqual([]); }); }); describe('the ratchet', () => { test('a pin above what the tree holds is stale, with the command that lowers it', () => { const gaps = checkNodeImports({ files: [{ path: 'packages/x/src/a.ts', source: 'const a = 1;' }], pins: { x: 2 }, }); expect(gaps.map((gap) => gap.kind)).toEqual(['stale']); expect(nodeImportFindingFor(gaps[0] as never).code).toBe('X_NODE_IMPORT_PIN_STALE'); }); test('an empty corpus is UNSCANNED, never a clean tree', () => { expect(nodeImportFindingFor(checkNodeImports({ files: [], pins: {} })[0] as never).code).toBe( 'X_NODE_IMPORT_UNSCANNED', ); }); /** * The pins are a MEASUREMENT and may only fall. A ceiling rather than an equality, so a slice * that writes ten `why:` lines does not have to come back here to be allowed to. */ test('no pin has been raised past what the widened corpus measured', () => { // 2026-08-26, the run in which a test file entered the corpus (#365) — 545 across 16 packages. // The 2026-08-23 ceiling it replaces (146 across 10) measured shipped source alone, so it is // not comparable and cannot be the ratchet's floor. This one may only fall. const dayOne: Readonly> = { admin: 1, ai: 9, cli: 360, core: 17, db: 8, entity: 1, i18n: 3, jobs: 2, manifest: 11, notify: 1, render: 15, scraping: 4, scripts: 99, testing: 12, time: 1, ui: 1, }; for (const [pkg, count] of Object.entries(NODE_IMPORT_PINS)) { expect(count).toBeLessThanOrEqual(dayOne[pkg] ?? 0); } }); test('the tree is on the ratchet', async () => { expect(await nodeImportGaps(repoRoot())).toEqual([]); }); });