Files
msd-core/tests/lint-allow-test-rule-refs.test.cjs
Tom Boucher 107eb8c1d9 feat(#3753): run docs guards on the PR that changes the docs they read (#3787)
A PR whose diff is entirely under docs/ runs zero tests, so a guard whose INPUT
is shipped prose cannot protect the PR lane of the diffs it exists to check. Its
only firing opportunity is after merge, on the shared branch -- which is how next
went red on dacae9273 while the PR that caused it (#3746) was green on every
check.

The docs-lint job in .github/workflows/docs-required.yml -- an ALREADY-REQUIRED
context -- now selects and runs the docs guards that read the specific docs files
the PR changed.

  scripts/docs-guard-registry.cjs    test file -> the docs paths it reads (63)
  scripts/select-docs-guards.cjs     pure (changedPaths, registry) -> test files
  scripts/lint-docs-guard-registration.cjs   drift guard, wired into lint:ci

scripts/ci-test-scope.cjs is NOT touched -- `git diff origin/next --` on it is
empty -- so #764's saving stands and its 21 pinning tests are untouched.

Selection: exact path; trailing-slash directory prefix (boundary-checked --
docs/adrenaline.md does NOT match docs/adr/, which a naive startsWith gets
wrong); and '*' for the 6 entries that walk docs/ generally or read a computed
path. Unknown maps to '*' -- guessing narrow is how a guard silently stops
running. Measured: a typo fix selects 6 of 63; docs/AGENTS.md selects 12;
docs/COMMANDS.md selects 18.

Four things this got wrong first, each found by an independent reviewer or by
probe, and each having been asserted safe in a comment:

1. The registry started as a RULE in ci-test-scope.cjs's RULES, on the theory
   that classify()'s !codeChanged normalization made it inert. True for
   docs-ONLY diffs; false for MIXED docs+code diffs, where codeChanged is true
   and the normalization never runs:

     node scripts/ci-test-scope.cjs --files "docs/a.md src/semver.cts"
       with the RULE:  25 targeted_tests
       origin/next:     3 targeted_tests

   Category error: RULES is the scoped lane's input; a docs-guard registry is a
   lane manifest for a consumer that never calls classify(). Extracted; pinned
   by value.

2. The second attempt was a dedicated workflow with paths: [docs/**]. Such a
   workflow never reports on a non-docs PR, so it can never be a required
   context without hanging every non-docs PR -- and a non-required check does not
   block a merge, so the guard would have been advisory and #3753 unfixed.
   docs-required.yml already has no paths: filter, already supplies the required
   docs-lint context, already computes docs_changed, and already ran one docs
   guard gated on it. Generalizing that step needs no ruleset edit at all.

3. The registry and the drift lint were built from ONE path-segment heuristic, so
   both were blind identically -- and blind at the guard that motivated the issue.
   The reader-call regex required a character BEFORE its keyword, so a callee
   named exactly read( / load( / parse( / doc( / file( / content( could never
   match; and only an INLINE path.join(ROOT,'docs','X.md') argument was caught,
   missing the two-step-via-variable form -- the MAJORITY spelling -- plus
   template literals and concatenation. Detector 1 fired on 14 of ~450 files, so
   35 genuine guards sat unregistered while the lint reported 0 violations,
   including cursor-reviewer (reads docs/COMMANDS.md, asserts
   .includes('--cursor')) and inventory-headings-countfree. The "accepted blind
   spot" this shipped with was the common case, not a fringe.

4. With detection fixed the true population is 115 files: 63 genuine guards, 52
   incidental. Running all 63 in a REQUIRED check on a one-line typo fix is the
   cost #764 exists to avoid -- install.test.cjs is 7840 lines and reads exactly
   one docs file, docs/AGENTS.md, for its frontmatter. Dropping it reproduces the
   bug; running it for a typo elsewhere is waste. Hence the map.

Then a second review round found six more, all fixed here:

- fragment-single-edit-propagation.install.test.cjs was EXEMPTED as
  "overlay fixture only". False: it reads the real docs/registries/eos.json and
  asserts on a registry entry name, and reads the real ADR-0001 and asserts its
  H1. A docs-only PR touching either would have gone green and red next -- #3753
  shipping again, from inside the fix for it. Now registered against both paths,
  and all 52 remaining exemptions were re-audited one by one.
- The SUITES-collision guard compared RAW registry keys, but run-tests.cjs strips
  a leading `tests/` BEFORE its suite check. So it caught 'all' and missed
  'tests/all' -- the only spelling that can actually occur, since every key
  carries the prefix. One typo would have run all 824 test files inside the
  required job. Now normalized the same way run-tests.cjs normalizes.
- The lint failed OPEN on an unreadable tests dir or candidate file: 0 violations,
  ok:true. A guard that cannot read its input must never report success.
- The exemption ratchet gated identity only, so a baselined file that later
  STARTED asserting on shipped docs stayed exempt silently -- 52 permanently blind
  files. The baseline now fingerprints the docs paths each exempted file
  references and fails when that set changes, naming what changed.
- The exemption marker was still honored inside a multi-line template literal in
  the header window. The scanner now tracks template-literal and block-comment
  state.
- `git diff --name-only | grep '^docs/'` silently dropped C-quoted non-ASCII docs
  paths, making docs_changed=false a green zero-guard check. Both call sites now
  pass -c core.quotepath=false.
- The run step was gated on hashFiles(), which a force-committed
  .docs-guard-tests.txt would satisfy. The step now rm -f's both scratch files
  first and gates on an output it sets itself.

Three empty states, deliberately distinct, because conflating them rebuilds
#3753: an empty or malformed registry HARD-FAILS; docs changed with no guard
covering them logs and skips; no docs change is already gated. The middle state
must never be expressed as an empty --files-from, which prints `no tests in suite
"all"` and exits 0 -- a green check that guarded nothing. With the current
registry that state is unreachable, because the six '*' entries always match;
the branch is kept as defensive handling for a future registry and says so.

timeout-minutes: 15 bounds the required job against a hanging fork-supplied test;
it had none. npm ci was added because the job never installed dependencies -- the
previous single-file step got away without it, the registry does not.

docs/contributing/docs-guard-registration.md documents the rule, following its
sibling cross-platform-portability-rules.md, and CONTRIBUTING.md's CI Test
Quality Checks table links to it. It is also load-bearing: without a docs/ file
in the diff this PR would not have triggered its own lane, shipping an
unexercised change to a required check.

One unrelated fix, included because this PR surfaced it and CLAUDE.md forbids
deferring a defect found while working. On this branch's first CI run,
`full test (windows-latest, 24, shard 3/3)` was CANCELLED at exactly 30 minutes;
tests were still passing 0.8s before the cancel, so it is a wall-clock timeout,
not a hang, and a cancelled job reddens `Required tests`.

The cause is not this PR's test file, which costs ~60ms. Shard composition is
unstable: adding ONE file to the unit suite reshuffled 115 of 268 files between
shards, and shard 3 drew a heavier mix. Underneath that is a real pre-existing
defect. tests/ci-test-job-timeout-budget.test.cjs requires every lane's budget to
be >= 1.5x its MEASURED cost -- "a lane that got slower must be re-budgeted, not
excused" -- and its test-full entry recorded 19m from a windows-22 shard. That is
stale. Measured on `next` with none of this PR's changes present: 26m18s (run
32614439702, windows-latest/24 shard 3/3), 23m36s and 23m17s on shard 2/3. So the
lane costs ~26m and the 30-minute cap carried 1.14x headroom, not 1.5x. The gate
had been out of compliance with its own rule; this PR was merely the file
addition that reshuffled shard 3 past the cliff.

Fixed as that file prescribes: measuredMinutes 19 -> 27 with fresh evidence, and
test-full timeout-minutes 30 -> 45. The rule's minimum for 27m is 41; 45 is
deliberately above it because the reshuffle means per-shard worst case moves run
to run, and a budget pinned to the exact minimum would be re-breached by the next
test file anyone adds. Only that one job's timeout changed; test.yml's scope,
matrix and steps are untouched, so #764's saving is unaffected.

Raising that cap let the Windows shard finish (28m45s, inside 45) and uncovered
a real failure the 30-minute cancel had been masking:
`new quick-task branch branches off origin/main (#2916)` died with
`outcome=timed_out exitCode=null`, SIGTERM, at the 15000ms bound.

tests/quick-branching.test.cjs:149 `runStep` runs a `#!/usr/bin/env bash` script
executing MULTIPLE git commands, but was bound to GIT_TIMEOUT_MS (15000) -- the
norm for a SINGLE git plumbing call. tests/helpers/timeouts.cjs already documents
this exact failure and exists to fix it: HOOK_FANOUT_TIMEOUT_MS was created after
PR #3285 recorded "outcome=timed_out exitCode=null at exactly the 15000ms probe
bound while every other lane passed the same commit", and calls that "a bound
sized for the wrong class, not a slow machine". Our failure is that case
verbatim, so both sites move to the class norm rather than to a bigger number.

The same class also failed on `next` itself 21 hours earlier -- run 32608945654,
windows-latest/24 shard 1/3, `plan touching only src/ in a submodule project
keeps worktree isolation ENABLED` -- where tests/worktree-safety.test.cjs:5845
`runGate` fans out to `git config --file .gitmodules` under a hardcoded 30000.
Fixed too, since it is a defect in the tree regardless of which branch surfaced
it.

A survey of the whole tests/ tree found the same class-mismatch at further
bash fan-out sites bound under 60000ms, and the maintainer approved sweeping
them rather than leaving them latent to surface the same way one at a time. 16
fan-out sites across 16 files now use the class norm.

The sweep is class-correctness, not raising numbers until things pass. Sites
were moved ONLY where the bash body demonstrably spawns something (git, node,
npm, a CLI); self-contained shell snippets were left where they are, and are
listed as deliberately unchanged: pure if/printf bodies (copilot-install), pure
array/case builtins (code-review-pipeline-regression:638), a documented
pure-shell gsd_run stub (host-integration), single-process hook calls
(workflow-guard:222/271/302), and a deliberately tight 5000ms fast-check hook
(gsd-write-guard.property). Nothing was lowered. process-seam.test.cjs:513
(literal 300) is untouched on purpose -- it tests timeout BEHAVIOR, so raising
it would destroy what it asserts.

Shared file-level constants were the trap here, and were handled per file rather
than by redefinition: GIT_TIMEOUT_MS has ~15 users in git-base-branch and only 1
is a fan-out; WORKTREE_TIMEOUT_MS has 16 users in worktree.test.cjs and 3 are;
PROBE_TIMEOUT_MS has several in three more files. In each the CALL SITE was
changed and the constant left alone, so no single-plumbing-call site silently
inherited a 60s bound. The one exception is hooks-opt-in.test.cjs, where
HOOK_TIMEOUT_MS has exactly one consumer -- spawnHook, the fan-out itself -- so
redefining it is identical in effect and reads better.

Only two of these sites have actually been observed failing. The rest cite that
shared class and those two run ids rather than inventing evidence of their own.

Co-authored-by: sim <sim@local>
2026-08-23 21:21:21 -04:00

757 lines
36 KiB
JavaScript

'use strict';
// docs-guard-exempt: 'docs/readme.md' appears only inside literal fixture
// `code` strings written to synthetic files and fed to the no-source-grep
// lint under test — this file never itself reads a real docs/ file off disk.
// Tests for scripts/lint-allow-test-rule-refs.cjs — the guard that (a)
// ratchets exemption-marker comments on IDENTITY (uncited comments must
// carry a tracking-issue ref or be grandfathered), (b) ratchets EFFECTIVE
// exemption SITES (a marker actually suppressing a violation the
// `no-source-grep` rule detects there) against a tight ceiling, and (c)
// tracks UNVERIFIED marker-bearing files (a marker with no detectable
// violation nearby) against a loose ceiling that never fails on a drop
// (#3520 / epic #3464 phase 5). Uses the script's env overrides to point at
// sandbox fixture dirs/files; never touches the real tests/ dir or the real
// allowlist/ceiling JSON.
//
// Identifier note: the script computes `relpath = path.relative(ROOT, full)`
// where ROOT is the repo root (path.join(__dirname, '..') inside the script),
// NOT relative to the fixture tests dir. Since fixtures live in a temp dir
// outside the repo, the identifiers the script produces are relative paths
// like `../../../../tmp/xyz/tests-0/foo.test.cjs`, not `tests/foo.test.cjs`.
// This file mirrors that exact computation (relToRoot below) rather than
// hardcoding a `tests/...`-shaped string, so assertions match reality
// regardless of where the OS places the temp dir.
const { describe, test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const { Linter } = require('eslint');
const { createTempDir, cleanup } = require('./helpers.cjs');
const { runNode } = require('./helpers/process-seam.cjs');
const { toLegacyResult } = require('./helpers/git-fixture.cjs');
const { PROBE_TIMEOUT_MS } = require('./helpers/timeouts.cjs');
const ROOT = path.join(__dirname, '..');
const SCRIPT = path.join(ROOT, 'scripts', 'lint-allow-test-rule-refs.cjs');
// The "repo baseline" row below (unlike every other row in this file) points
// the script at the REAL repo tests/scripts/eslint-rules/etc trees with no
// sandbox override, so it drives ESLint's `Linter` over every real
// marker-bearing file the script finds (~294 files after the #3464 perf
// follow-up narrowed the Linter pass off the full ~1200-file glob walk).
// That is a heavier class than `PROBE_TIMEOUT_MS` describes ("a single short
// CLI query or `node -e` probe against a temp fixture") — measured ~2.3-3.3s
// locally post-narrowing, down from ~7-12s pre-narrowing, which is what
// previously died at exactly the `PROBE_TIMEOUT_MS=15000` bound under CI
// load (empty stdout/stderr, exitCode null — SIGKILLed by the harness
// timeout, not a real assertion failure). 30000ms is ~10x the measured local
// runtime, leaving real headroom for slower CI runners without hand-waving
// the bound back up to "whatever makes it pass this once."
const REPO_BASELINE_LINT_TIMEOUT_MS = 30000;
// Deliberately split so this file's OWN source never contains the contiguous
// exemption-marker substring the script under test scans for — the script
// does a raw-text scan (not AST/comment parsing) for the marker-inventory
// check, so an unsplit literal here would make THIS test file register as
// its own offender when the real lint:ci run scans tests/.
const MARKER = 'allow' + '-test-rule:';
// Row 13/14/15 (structural guard) import the rule and the script under test
// directly by reference, never re-declaring their logic.
const noSourceGrepRule = require('../eslint-rules/no-source-grep.cjs');
const scriptUnderTest = require('../scripts/lint-allow-test-rule-refs.cjs');
let sandbox;
let fixtureCount = 0;
// Mirrors the script's own `path.relative(ROOT, full).split(path.sep).join('/')`
// computation (scripts/lint-allow-test-rule-refs.cjs's walkTestFiles) so
// expected identifiers are derived, never hardcoded as `tests/...`.
function relToRoot(fullPath) {
return path.relative(ROOT, fullPath).split(path.sep).join('/');
}
/**
* Writes `files` (name -> content) into a fresh sandbox tests dir, plus an
* allowlist JSON and the two ceiling JSONs, then invokes the script via its
* env-var overrides.
*
* @param {object} opts
* @param {Object<string,string>} opts.files - filename (may include `/` for
* a nested subdirectory, e.g. `helpers/foo.cjs`) -> file content.
* @param {string[]} [opts.allowlist] - allowlist array (default []).
* @param {{maxSites:number,grace:number}} [opts.effectiveCeiling] - default
* is deliberately generous so a case not targeting the effective-sites
* ratchet does not accidentally also trip it.
* @param {{maxFiles:number}} [opts.unverifiedCeiling] - default is
* deliberately generous, same reasoning.
* @param {string[]} [opts.args] - extra CLI argv.
* @param {Object<string,string>} [opts.extraFiles] - filename (relative to a
* FRESH per-call `extraRoot`, e.g. `scripts/fixture.cjs`) -> file content,
* for exercising the non-`tests/` glob blocks (scripts/** , eslint-rules/** ,
* etc. — #3464 phase 5 BLOCKER fix widened scan scope) in isolation. Left
* empty by default, which keeps every existing row's runtime unaffected: a
* per-call `extraRoot` is always created (never shared/reused across
* calls, so nothing leaks between rows) and set as
* `GSD_LINT_ALLOW_TEST_RULE_EXTRA_ROOT`; with no `extraFiles` it stays an
* empty directory, so every non-`tests/` glob costs one cheap empty
* `readdirSync`, never a real-repo scan.
*/
function runLint({
files,
allowlist = [],
effectiveCeiling = { maxSites: 1000, grace: 1000 },
unverifiedCeiling = { maxFiles: 1000 },
args = [],
extraFiles = {},
}) {
const testsDir = path.join(sandbox, `tests-${fixtureCount}`);
fs.mkdirSync(testsDir, { recursive: true });
const relpaths = {};
for (const [name, content] of Object.entries(files)) {
const full = path.join(testsDir, name);
fs.mkdirSync(path.dirname(full), { recursive: true });
fs.writeFileSync(full, content);
relpaths[name] = relToRoot(full);
}
const extraRoot = path.join(sandbox, `extra-${fixtureCount}`);
fs.mkdirSync(extraRoot, { recursive: true });
for (const [name, content] of Object.entries(extraFiles)) {
const full = path.join(extraRoot, name);
fs.mkdirSync(path.dirname(full), { recursive: true });
fs.writeFileSync(full, content);
relpaths[`extra:${name}`] = relToRoot(full);
}
const allowlistPath = path.join(sandbox, `allowlist-${fixtureCount}.json`);
fs.writeFileSync(allowlistPath, JSON.stringify(allowlist));
const effectiveCeilingPath = path.join(sandbox, `effective-ceiling-${fixtureCount}.json`);
fs.writeFileSync(effectiveCeilingPath, JSON.stringify(effectiveCeiling));
const unverifiedCeilingPath = path.join(sandbox, `unverified-ceiling-${fixtureCount}.json`);
fs.writeFileSync(unverifiedCeilingPath, JSON.stringify(unverifiedCeiling));
fixtureCount += 1;
const result = runNode([SCRIPT, ...args], {
cwd: ROOT,
timeoutMs: PROBE_TIMEOUT_MS,
env: {
...process.env,
GSD_LINT_ALLOW_TEST_RULE_TESTS_DIR: testsDir,
GSD_LINT_ALLOW_TEST_RULE_EXTRA_ROOT: extraRoot,
GSD_LINT_ALLOW_TEST_RULE_ALLOWLIST: allowlistPath,
GSD_LINT_ALLOW_TEST_RULE_EFFECTIVE_CEILING: effectiveCeilingPath,
GSD_LINT_ALLOW_TEST_RULE_UNVERIFIED_CEILING: unverifiedCeilingPath,
},
});
return { ...toLegacyResult(result), relpaths, testsDir, extraRoot };
}
describe('lint-allow-test-rule-refs', () => {
before(() => {
sandbox = createTempDir('gsd-lint-allow-test-rule-');
});
after(() => {
cleanup(sandbox);
});
// ─── citation check (unchanged contract) ───────────────────────────────
test('passes when a known uncited exemption is grandfathered', () => {
// runLint's testsDir naming (`tests-${fixtureCount}`) is deterministic
// and only increments once files are written, so the relpath the script
// will compute can be predicted before the run.
const testsDir = path.join(sandbox, `tests-${fixtureCount}`);
const relpath = relToRoot(path.join(testsDir, 'foo.test.cjs'));
const r = runLint({
files: { 'foo.test.cjs': `// ${MARKER} some-reason\n` },
allowlist: [`${relpath} :: some-reason`],
});
assert.strictEqual(r.status, 0, `stderr: ${r.stderr}`);
});
test('fails on a novel uncited exemption not in the allowlist (test-matrix row 10)', () => {
const r = runLint({
files: { 'foo.test.cjs': `// ${MARKER} mystery-reason\n` },
allowlist: [],
});
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /mystery-reason/);
});
test('fails on a stale allowlist entry (ratchet-down enforcement)', () => {
const r = runLint({
// Must be VALID JS (parsed by the real Linter, not just grepped) — a
// bare "no marker here" is not valid syntax and would now correctly
// throw a loud parse failure (the SECOND FIX below) rather than
// silently exercising the ratchet-down path this test targets.
files: { 'foo.test.cjs': '// no marker here\n' },
allowlist: ['tests/definitely-stale-file.test.cjs :: stale-reason'],
});
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /stale-reason/);
assert.match(r.stderr, /definitely-stale-file\.test\.cjs/);
});
test('a cited exemption passes the citation check with an empty allowlist', () => {
const r = runLint({
files: { 'cited.test.cjs': `// ${MARKER} see #456\n` },
allowlist: [],
});
assert.strictEqual(r.status, 0, `stderr: ${r.stderr}`);
});
test('the SAME cited exemption (no adjacent violation) still counts toward the unverified-files ceiling', () => {
// This marker has NOTHING to suppress (no fs.readFileSync at all in the
// file), so under the effective/unverified split it lands in the
// UNVERIFIED pool, not the effective one — distinct from the old
// single-ceiling behavior this replaces.
const r = runLint({
files: { 'cited.test.cjs': `// ${MARKER} see #456\n` },
allowlist: [],
unverifiedCeiling: { maxFiles: 0 },
});
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /allow-test-rule-unverified-markers/);
});
test('a duplicate uncited reason in one file dedupes to one identifier', () => {
const testsDir = path.join(sandbox, `tests-${fixtureCount}`);
const relpath = relToRoot(path.join(testsDir, 'dup.test.cjs'));
// A SINGLE allowlist entry suffices — if the dedupe regressed (two
// identifiers produced), this single-entry allowlist would leave one
// novel offender and fail.
const r = runLint({
files: { 'dup.test.cjs': `// ${MARKER} dup-reason\n// ${MARKER} dup-reason\n` },
allowlist: [`${relpath} :: dup-reason`],
});
assert.strictEqual(r.status, 0, `stderr: ${r.stderr}`);
});
test('unknown CLI arguments are rejected with exit code 2', () => {
const r = runLint({ files: {}, allowlist: [], args: ['--bogus'] });
assert.strictEqual(r.status, 2);
assert.match(r.stderr, /unknown argument/);
assert.match(r.stderr, /--bogus/);
});
test('repo baseline passes (real tests/ dir against real allowlist + ceilings)', () => {
const r = runNode([SCRIPT], { cwd: ROOT, timeoutMs: REPO_BASELINE_LINT_TIMEOUT_MS });
assert.strictEqual(r.exitCode, 0, `stderr: ${r.stderr}\nstdout: ${r.stdout}`);
assert.match(r.stdout, /effective exemptions:/);
assert.match(r.stdout, /unverified markers:/);
assert.match(r.stdout, /Known limit:/);
});
// ─── #3520 test-matrix rows 1-12 ────────────────────────────────────────
test('row 1: marker adjacent to a detected violation counts as effective, not unverified', () => {
const code = [
"const fs = require('fs');",
"const path = require('path');",
`// ${MARKER} reason (#1)`,
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); src.includes('x');",
].join('\n');
// Pin the exact effective-site count via a boundary ceiling: passes at
// exactly 1, fails at 0 — that is how this file proves "counted as
// effective" without needing to parse the ok-message text.
const pass = runLint({
files: { 'row1.test.cjs': code },
effectiveCeiling: { maxSites: 1, grace: 0 },
unverifiedCeiling: { maxFiles: 0 }, // must NOT also count as unverified
});
assert.strictEqual(pass.status, 0, `stderr: ${pass.stderr}`);
const failsAtZero = runLint({
files: { 'row1.test.cjs': code },
effectiveCeiling: { maxSites: 0, grace: 0 },
});
assert.notStrictEqual(failsAtZero.status, 0);
assert.match(failsAtZero.stderr, /allow-test-rule-effective-sites/);
});
test('row 2: marker with no detectable violation counts as unverified, not effective', () => {
const code = [
`// ${MARKER} reason (#2)`,
"const fs = require('fs');",
"const path = require('path');",
"const content = fs.readFileSync(path.join(__dirname, '..', 'docs', 'readme.md'), 'utf-8');",
"content.includes('hello');",
].join('\n');
const pass = runLint({
files: { 'row2.test.cjs': code },
effectiveCeiling: { maxSites: 0, grace: 0 }, // must NOT count as effective
unverifiedCeiling: { maxFiles: 1 },
});
assert.strictEqual(pass.status, 0, `stderr: ${pass.stderr}`);
const failsAtZeroUnverified = runLint({
files: { 'row2.test.cjs': code },
unverifiedCeiling: { maxFiles: 0 },
});
assert.notStrictEqual(failsAtZeroUnverified.status, 0);
assert.match(failsAtZeroUnverified.stderr, /allow-test-rule-unverified-markers/);
});
test('row 3: no marker, no violation — counted in neither pool', () => {
const code = [
"const assert = require('node:assert/strict');",
"assert.strictEqual(1 + 1, 2);",
].join('\n');
const r = runLint({
files: { 'row3.test.cjs': code },
effectiveCeiling: { maxSites: 0, grace: 0 },
unverifiedCeiling: { maxFiles: 0 },
});
assert.strictEqual(r.status, 0, `stderr: ${r.stderr}`);
});
test('row 4: a violation with NO marker anywhere in the file is not flagged by this gate (#3464 perf follow-up narrowed live-violation detection to marker-bearing files; npm run lint / npm run lint:ci enforce unmarked files separately)', () => {
const code = [
"const fs = require('fs');",
"const path = require('path');",
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); src.includes('x');",
].join('\n');
const r = runLint({ files: { 'row4.test.cjs': code } });
assert.strictEqual(r.status, 0, `stderr: ${r.stderr}`);
});
test('row 4b: a live violation in a file that DOES carry a marker (elsewhere, unrelated) still fails the gate', () => {
// Same shape as the parity corpus's "marker far above an unrelated later
// violation" fixture: M1 suppresses V1, but V2 is far enough away (and
// has no marker of its own) to stay live. Proves the marker-bearing
// narrowing (row 4 above) does not also let a genuine live violation
// slip through in a file this gate DOES still lint.
const code = [
"const fs = require('fs');",
"const path = require('path');",
`// ${MARKER} reason for V1 (#1)`,
"const s1 = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); s1.includes('x');",
...Array.from({ length: 20 }, (_, i) => `// unrelated filler line ${i + 1}`),
"const s2 = fs.readFileSync(path.join(__dirname, '..', 'lib', 'b.cjs'), 'utf-8'); s2.includes('y');",
].join('\n');
const r = runLint({ files: { 'row4b.test.cjs': code } });
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /allow-test-rule-live-violations/);
assert.match(r.stderr, /row4b\.test\.cjs/);
});
test('row 5: a file with one effective site and one inert marker (nothing nearby to suppress) counts as effective only', () => {
// "One suppressing, one not" per test-matrix row 5, realized without an
// actual unsuppressed violation (which would make row 5 indistinguishable
// from row 4's assertion): M1 suppresses a real V1; M2 sits above a .md
// read the rule never flags, so it suppresses nothing because there is
// nothing there to suppress. The file must still classify as effective
// (>=1 effective site), never also unverified — no double counting.
const code = [
"const fs = require('fs');",
"const path = require('path');",
`// ${MARKER} effective site (#1)`,
"const s1 = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); s1.includes('x');",
'',
`// ${MARKER} inert marker, nothing nearby (#2)`,
"const content = fs.readFileSync(path.join(__dirname, '..', 'docs', 'readme.md'), 'utf-8');",
"content.includes('hello');",
].join('\n');
const r = runLint({
files: { 'row5.test.cjs': code },
effectiveCeiling: { maxSites: 1, grace: 0 },
unverifiedCeiling: { maxFiles: 0 }, // the file must NOT also appear here
});
assert.strictEqual(r.status, 0, `stderr: ${r.stderr}`);
});
test('row 6: effective count exceeds its ceiling fails, message names effective sites', () => {
const code = [
"const fs = require('fs');",
"const path = require('path');",
`// ${MARKER} reason (#1)`,
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); src.includes('x');",
].join('\n');
const r = runLint({
files: { 'row6.test.cjs': code },
effectiveCeiling: { maxSites: 0, grace: 0 },
});
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /allow-test-rule-effective-sites/);
assert.match(r.stderr, /exceeds budget ceiling/);
});
test('row 7: effective count far below ceiling (slack beyond grace) fails — ratchet-down enforced', () => {
const code = [
"const fs = require('fs');",
"const path = require('path');",
`// ${MARKER} reason (#1)`,
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); src.includes('x');",
].join('\n');
// actualMax=1, ceiling=10, grace=2 -> slack=9 > grace -> fails.
const r = runLint({
files: { 'row7.test.cjs': code },
effectiveCeiling: { maxSites: 10, grace: 2 },
});
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /allow-test-rule-effective-sites/);
assert.match(r.stderr, /Tighten the ceiling/);
});
test('row 8: unverified count exceeds its ceiling fails with a message distinct from row 6', () => {
const code = [
`// ${MARKER} reason (#2)`,
"const fs = require('fs');",
"const path = require('path');",
"const content = fs.readFileSync(path.join(__dirname, '..', 'docs', 'readme.md'), 'utf-8');",
"content.includes('hello');",
].join('\n');
const r = runLint({
files: { 'row8.test.cjs': code },
unverifiedCeiling: { maxFiles: 0 },
});
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /allow-test-rule-unverified-markers/);
assert.doesNotMatch(r.stderr, /allow-test-rule-effective-sites/);
});
test('row 9: unverified count dropping below its ceiling still passes (not tightly ratcheted)', () => {
const code = [
"const assert = require('node:assert/strict');",
"assert.ok(true);",
].join('\n');
// Zero unverified files, ceiling deliberately loose (100) -- a real
// "tight" ratchet (like effective-sites) would force this down; the
// unverified ceiling must not.
const r = runLint({
files: { 'row9.test.cjs': code },
unverifiedCeiling: { maxFiles: 100 },
});
assert.strictEqual(r.status, 0, `stderr: ${r.stderr}`);
});
test('row 10: uncited marker is still an allowlist offender (citation contract unchanged)', () => {
const r = runLint({
files: { 'row10.test.cjs': `// ${MARKER} no citation here\n` },
allowlist: [],
});
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /no citation here/);
});
test('row 11: marker text inside a string literal is not a directive — the violation it sits above stays live', () => {
const code = [
"const fs = require('fs');",
"const path = require('path');",
`const note = 'not a directive: ${MARKER} fake reason';`,
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); src.includes(note);",
].join('\n');
const r = runLint({ files: { 'row11.test.cjs': code } });
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /allow-test-rule-live-violations/);
assert.match(r.stderr, /row11\.test\.cjs/);
});
test('row 12: a NUL-byte file is discovered and classified without a shell grep or a crash', () => {
const testsDir = path.join(sandbox, `tests-${fixtureCount}`);
fs.mkdirSync(testsDir, { recursive: true });
const full = path.join(testsDir, 'row12.test.cjs');
// A literal NUL byte embedded in otherwise-valid-looking source, plus a
// marker comment, written via a real byte buffer (never a shell
// redirect/grep, which would choke on the NUL differently than Node's
// own fs + parser do).
const buf = Buffer.concat([
Buffer.from(`// ${MARKER} reason (#1)\nconst x = '`),
Buffer.from([0]),
Buffer.from("';\n"),
]);
fs.writeFileSync(full, buf);
const extraRoot = path.join(sandbox, `extra-${fixtureCount}`);
fs.mkdirSync(extraRoot, { recursive: true });
const effectiveCeilingPath = path.join(sandbox, `effective-ceiling-${fixtureCount}.json`);
fs.writeFileSync(effectiveCeilingPath, JSON.stringify({ maxSites: 1000, grace: 1000 }));
const unverifiedCeilingPath = path.join(sandbox, `unverified-ceiling-${fixtureCount}.json`);
fs.writeFileSync(unverifiedCeilingPath, JSON.stringify({ maxFiles: 1000 }));
const allowlistPath = path.join(sandbox, `allowlist-${fixtureCount}.json`);
fs.writeFileSync(allowlistPath, JSON.stringify([]));
fixtureCount += 1;
const result = runNode([SCRIPT], {
cwd: ROOT,
timeoutMs: PROBE_TIMEOUT_MS,
env: {
...process.env,
GSD_LINT_ALLOW_TEST_RULE_TESTS_DIR: testsDir,
GSD_LINT_ALLOW_TEST_RULE_EXTRA_ROOT: extraRoot,
GSD_LINT_ALLOW_TEST_RULE_ALLOWLIST: allowlistPath,
GSD_LINT_ALLOW_TEST_RULE_EFFECTIVE_CEILING: effectiveCeilingPath,
GSD_LINT_ALLOW_TEST_RULE_UNVERIFIED_CEILING: unverifiedCeilingPath,
},
});
const r = toLegacyResult(result);
// Never crashes with an uncaught exception (no raw Node stack trace) —
// it either passes or fails cleanly through the script's own messaging.
assert.ok(
r.status === 0 || r.status === 1,
`unexpected exit status ${r.status}; stderr: ${r.stderr}`
);
assert.doesNotMatch(r.stderr, /at Object\.<anonymous>/);
assert.doesNotMatch(r.stderr, /SyntaxError/);
});
// ─── #3464 phase 5 BLOCKER fix rows: widened scan scope + loud parse failure ───
test('row 16: a non-.test.cjs file nested under tests/ carrying a marker is counted (unverified) — the live-command-registry.cjs case', () => {
// A prior version of the scan only walked `tests/**/*.test.cjs`, so a
// marker in a `tests/helpers/*.cjs`-shaped file (this repo's real
// tests/helpers/live-command-registry.cjs instance) was invisible to
// BOTH reported numbers. This file has no adjacent readFileSync+search
// call, so it lands in the unverified pool, not effective.
const code = [
`// ${MARKER} reason (#2)`,
"const fs = require('fs');",
"const path = require('path');",
"const content = fs.readFileSync(path.join(__dirname, '..', 'docs', 'readme.md'), 'utf-8');",
"content.includes('hello');",
].join('\n');
const pass = runLint({
files: { 'helpers/row16.cjs': code },
effectiveCeiling: { maxSites: 0, grace: 0 }, // must NOT count as effective
unverifiedCeiling: { maxFiles: 1 },
});
assert.strictEqual(pass.status, 0, `stderr: ${pass.stderr}`);
const failsAtZero = runLint({
files: { 'helpers/row16.cjs': code },
unverifiedCeiling: { maxFiles: 0 },
});
assert.notStrictEqual(failsAtZero.status, 0);
assert.match(failsAtZero.stderr, /allow-test-rule-unverified-markers/);
});
test('row 17: a marker in a NON-tests registered glob (scripts/**/*.cjs-shaped sandbox path) is counted as effective', () => {
// Exercises the widened scan scope itself (not just the tests/ side):
// `eslint.config.mjs` also registers local/no-source-grep on
// scripts/**/*.cjs (among others), and this must now actually be
// scanned, not merely assumed reachable in theory.
const code = [
"const fs = require('fs');",
"const path = require('path');",
`// ${MARKER} reason (#1)`,
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); src.includes('x');",
].join('\n');
const pass = runLint({
files: {},
extraFiles: { 'scripts/row17-fixture.cjs': code },
effectiveCeiling: { maxSites: 1, grace: 0 },
unverifiedCeiling: { maxFiles: 0 },
});
assert.strictEqual(pass.status, 0, `stderr: ${pass.stderr}`);
const failsAtZero = runLint({
files: {},
extraFiles: { 'scripts/row17-fixture.cjs': code },
effectiveCeiling: { maxSites: 0, grace: 0 },
});
assert.notStrictEqual(failsAtZero.status, 0);
assert.match(failsAtZero.stderr, /allow-test-rule-effective-sites/);
});
test('row 18: an unparseable file makes the gate throw loudly instead of counting it as clean', () => {
// Sibling to the "No matching configuration found" case, which already
// throws — before this fix, a parse/verify failure inside
// classifySiteLines was silently swallowed into `{effectiveLines: [],
// liveLines: []}`, indistinguishable from a genuinely clean file. Genuine
// invalid JS syntax (unbalanced braces / stray token), not just an
// embedded NUL byte (row 12 proves NUL bytes alone parse fine).
//
// Must carry a marker (#3464 perf follow-up): the script now only drives
// the Linter over marker-bearing files (a cheap substring pre-filter
// over raw content — see the script's "Linter scope" doc comment), so an
// unparseable file with NO marker at all is out of scope for THIS gate
// entirely (an unparseable file fails `npm run lint` on its own, via
// ESLint's own parse error, independent of this script). The marker
// comment itself is valid syntax; only what follows it is broken.
const code = `// ${MARKER} reason (#1)\nconst x = {{{ this is not valid javascript syntax ]`;
const r = runLint({ files: { 'row18.test.cjs': code } });
assert.notStrictEqual(r.status, 0);
assert.match(r.stderr, /failed to parse\/verify/);
assert.match(r.stderr, /row18\.test\.cjs/);
// Never the "ok" success message — a parse failure must never present as
// a clean run.
assert.doesNotMatch(r.stdout, /^ok lint-allow-test-rule-refs/);
});
// ─── #3520 test-matrix rows 13-15: structural guard (parity + teeth) ───
test('row 13: the script imports the predicate/detection from the rule module — no second copy of the adjacency arithmetic', () => {
const scriptSource = fs.readFileSync(SCRIPT, 'utf8');
// Imports the rule module directly.
assert.match(scriptSource, /require\(['"]\.\.\/eslint-rules\/no-source-grep\.cjs['"]\)/);
// Calls the rule's own exported predicate/collector, not a re-derivation.
assert.match(scriptSource, /\.isSuppressedAt\(/);
assert.match(scriptSource, /\.collectMarkerAndCommentLines\(/);
// No duplicated lookahead constant or adjacency loop in the script's own
// source — the tell for a hand-rolled second copy of isSuppressedAt's
// arithmetic would be a re-declared "MAX_MARKER_LOOKAHEAD_LINES ="
// assignment or a hand-written `markerLine + 1; l < violationLine`
// purity loop.
assert.doesNotMatch(scriptSource, /MAX_MARKER_LOOKAHEAD_LINES\s*=\s*\d/);
assert.doesNotMatch(scriptSource, /markerLine \+ 1;\s*l\s*<\s*violationLine/);
// Reference identity: what the script requires IS the same object the
// rule module exports (not a structurally-similar copy).
assert.strictEqual(
require('../eslint-rules/no-source-grep.cjs').isSuppressedAt,
noSourceGrepRule.isSuppressedAt
);
});
test('row 14: parity — the script\'s suppressed verdict equals the real rule\'s independent report/no-report outcome, for every site in a fixture corpus', () => {
// Ground truth, computed WITHOUT using isSuppressedAt or
// collectMarkerAndCommentLines at all: run the REAL (non-neutralized)
// `no-source-grep` rule through ESLint's Linter and record which lines
// it reports. This is an independent code path from
// scriptUnderTest.classifySiteLines (which calls the rule's exported
// predicate directly) — the two must still agree everywhere, because
// both are ultimately driven by the same rule.
function realRuleReportedLines(code, filename) {
const linter = new Linter({ configType: 'flat' });
const config = {
languageOptions: { ecmaVersion: 2022, sourceType: 'commonjs' },
plugins: { local: { rules: { 'no-source-grep': noSourceGrepRule } } },
rules: { 'local/no-source-grep': 'error' },
};
return linter
.verify(code, config, filename)
.filter((m) => m.messageId === 'noSourceGrep')
.map((m) => m.line)
.sort((a, b) => a - b);
}
const AT = MARKER; // already split above; reuse for fixture text
const corpus = [
// adjacent marker suppresses its violation
[
"const fs = require('fs');",
"const path = require('path');",
`// ${AT} reason (#1)`,
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); src.includes('x');",
].join('\n'),
// no marker: violation stays live
[
"const fs = require('fs');",
"const path = require('path');",
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'b.cjs'), 'utf-8'); src.includes('x');",
].join('\n'),
// marker far above an unrelated later violation: does not reach it
[
"const fs = require('fs');",
"const path = require('path');",
`// ${AT} reason for V1 (#1)`,
"const s1 = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); s1.includes('x');",
...Array.from({ length: 20 }, (_, i) => `// unrelated filler line ${i + 1}`),
"const s2 = fs.readFileSync(path.join(__dirname, '..', 'lib', 'b.cjs'), 'utf-8'); s2.includes('y');",
].join('\n'),
// marker in a string literal does not suppress
[
"const fs = require('fs');",
"const path = require('path');",
`const note = 'not a directive: ${AT} fake reason';`,
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf-8'); src.includes(note);",
].join('\n'),
// marker directly above the read, search several comment-pure lines later
[
"const fs = require('fs');",
"const path = require('path');",
`// ${AT} reason (#1)`,
"const src = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf8');",
'// comment-pure line one',
'// comment-pure line two',
"src.includes('x');",
].join('\n'),
// boundary: marker exactly MAX_MARKER_LOOKAHEAD_LINES (8) above is suppressed
[
"const fs = require('fs');",
"const path = require('path');",
`// ${AT} reason (#1)`,
...Array.from({ length: 7 }, (_, i) => `// filler comment line ${i + 1}`),
"const s = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf8'); s.includes('x');",
].join('\n'),
// boundary: marker one line beyond the lookahead is not suppressed
[
"const fs = require('fs');",
"const path = require('path');",
`// ${AT} reason (#1)`,
...Array.from({ length: 8 }, (_, i) => `// filler comment line ${i + 1}`),
"const s = fs.readFileSync(path.join(__dirname, '..', 'lib', 'a.cjs'), 'utf8'); s.includes('x');",
].join('\n'),
// no violation at all
[
"const assert = require('node:assert/strict');",
"assert.ok(true);",
].join('\n'),
];
for (const [i, code] of corpus.entries()) {
const filename = `tests/parity-fixture-${i}.test.cjs`;
const { liveLines } = scriptUnderTest.classifyCode(code, filename);
const realLines = realRuleReportedLines(code, filename);
assert.deepStrictEqual(
[...liveLines].sort((a, b) => a - b),
realLines,
`fixture ${i}: script's live-line verdict disagreed with the real rule's report/no-report outcome`
);
}
});
test('row 15 (teeth for row 14, by construction): the exported isSuppressedAt is the SAME function the rule calls internally, so its default lookahead cannot diverge from the rule\'s', () => {
// Runtime mutation of MAX_MARKER_LOOKAHEAD_LINES is not possible: it is
// declared `const` at module scope inside eslint-rules/no-source-grep.cjs
// and isSuppressedAt's default parameter closes over that lexical
// binding directly, not over a mutable exported property — reassigning
// `noSourceGrepRule.MAX_MARKER_LOOKAHEAD_LINES` would silently do
// nothing to the real default. That immutability is deliberate (the
// constant is not meant to be a runtime knob), so per the brief this
// teeth check is asserted BY CONSTRUCTION rather than by faking a
// mutation:
//
// 1. isSuppressedAt is a single exported function (verified by
// reference identity in row 13) — the script never has its own copy.
// 2. Its `maxLookahead` parameter is genuinely load-bearing (not a
// decorative unused default): explicitly overriding it changes the
// verdict at the exact boundary where the DEFAULT (backed by
// MAX_MARKER_LOOKAHEAD_LINES) says "suppressed".
// 3. Because the rule's own internal isSuppressed() call site and the
// script's classification call site both invoke this identical
// function with NO override (so both fall back to the same
// MAX_MARKER_LOOKAHEAD_LINES default), any future edit to that
// constant necessarily moves both call sites together — there is no
// second constant anywhere to drift out of step with it.
// Default (no override) -> uses MAX_MARKER_LOOKAHEAD_LINES: land exactly
// at the real boundary using the exported constant itself, so this test
// tracks the constant's actual value rather than hardcoding "8".
const gapAtBoundary = noSourceGrepRule.MAX_MARKER_LOOKAHEAD_LINES;
const atBoundary = noSourceGrepRule.isSuppressedAt({
markerLines: [1],
violationLine: 1 + gapAtBoundary,
commentLineSet: new Set(Array.from({ length: gapAtBoundary }, (_, i) => i + 2)),
lines: Array.from({ length: gapAtBoundary + 1 }, () => '// filler'),
});
assert.strictEqual(atBoundary, true, 'default lookahead should suppress exactly at the boundary');
// Same fixture, EXPLICIT override tighter than the real default: proves
// the parameter actually drives the verdict (not ignored), which is the
// load-bearing fact that makes "both call sites use the same default"
// meaningful rather than vacuous.
const overridden = noSourceGrepRule.isSuppressedAt({
markerLines: [1],
violationLine: 1 + gapAtBoundary,
commentLineSet: new Set(Array.from({ length: gapAtBoundary }, (_, i) => i + 2)),
lines: Array.from({ length: gapAtBoundary + 1 }, () => '// filler'),
maxLookahead: gapAtBoundary - 1,
});
assert.strictEqual(overridden, false, 'an explicit tighter lookahead must change the verdict');
});
});