Files
msd-core/tests/orphaned-hooks.test.cjs
Tom Boucher 918f987a19 feat(#2982): extend no-source-grep lint to catch var-binding readFileSync.includes() (#2985)
* feat(#2982): extend no-source-grep lint to catch var-binding readFileSync.includes()

The base lint (scripts/lint-no-source-grep.cjs) only catches
readFileSync(...).<text-method>() chained directly. The much more
common var-binding form escapes it:

  const src = fs.readFileSync(p, 'utf8');
  // 50 lines later
  if (src.includes('foo')) {}        // ← still grep, lint missed it

Scan of the test suite found ~141 files using this pattern.

Implementation built TDD per #2982 with structured-IR assertions:

  scripts/lint-no-source-grep-extras.cjs
    - detectVarBindingViolations(src) — pure detector, two passes:
      pass 1 collects vars bound from readFileSync, pass 2 finds any
      <var>.<includes|startsWith|endsWith|match|search>( on those vars.
    - detectWrappedAssertOkMatch(src) — flags
      assert.ok(<expr>.match(...)) which escapes the assert.match rule.
    - VIOLATION enum exposes stable codes for tests to assert on.

  scripts/lint-no-source-grep.cjs
    - Wires the new detectors into the existing per-file check; one
      additional violation row per file with the first 3 sample tokens.

  tests/bug-2982-lint-var-binding.test.cjs
    - 13 tests, all assertions on typed VIOLATION enum / structured
      records. Covers all 5 text-match methods, multi-var, no-bind,
      string literal (must NOT trigger), wrapped assert.ok(.match),
      and assert.match (must NOT double-flag).

Migration backlog (#2974 expanded scope):

  - 42 files annotated `// allow-test-rule: source-text-is-the-product`
    (legitimate — they read .md/.json/.yml files whose deployed text
    IS the product)
  - 3 files annotated `// allow-test-rule: pending-migration-to-typed-ir [#2974]`
    (read .cjs/.js source — clear migration debt)
  - 95 files annotated `pending-migration-to-typed-ir [#2974]` with
    `Per-file review may reclassify as source-text-is-the-product
    during migration` (mixed — manual review under #2974)

After this lands the lint reports 0 violations on main; new
violations in PRs surface immediately.

Closes #2982
Refs #2974

* test(#2982): fix truncated test name per CR

The label ended with a bare '(' from a copy-paste mishap. Now reads
'does NOT flag .matchAll(...) — matchAll is not match, so
assert.ok(.matchAll(...)) is not flagged'.

* chore(#2982): add changeset fragment for PR #2985

* chore(#2982): add changeset fragment for PR #2985
2026-05-01 19:50:10 -04:00

94 lines
4.0 KiB
JavaScript

// allow-test-rule: pending-migration-to-typed-ir [#2974]
// Tracked in #2974 for migration to typed-IR assertions per CONTRIBUTING.md
// "Prohibited: Raw Text Matching on Test Outputs". Per-file review may
// reclassify some entries as source-text-is-the-product during migration.
/**
* Regression test for #1750: orphaned hook files from removed features
* (e.g., gsd-intel-*.js) should NOT be flagged as stale by gsd-check-update.js.
*
* The stale hooks scanner should only check hooks that are part of the current
* distribution, not every gsd-*.js file in the hooks directory.
*/
const { test, describe } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
// MANAGED_HOOKS lives in the worker file (extracted from inline -e code to eliminate
// template-literal regex-escaping concerns). Tests read the worker directly.
const CHECK_UPDATE_PATH = path.join(__dirname, '..', 'hooks', 'gsd-check-update.js');
const WORKER_PATH = path.join(__dirname, '..', 'hooks', 'gsd-check-update-worker.js');
const BUILD_HOOKS_PATH = path.join(__dirname, '..', 'scripts', 'build-hooks.js');
describe('orphaned hooks stale detection (#1750)', () => {
test('stale hook scanner uses an allowlist of managed hooks, not a wildcard', () => {
const content = fs.readFileSync(WORKER_PATH, 'utf8');
// The scanner MUST NOT use a broad `startsWith('gsd-')` filter that catches
// orphaned files from removed features (gsd-intel-index.js, gsd-intel-prune.js, etc.)
// Instead, it should reference a known set of managed hook filenames.
const hasBroadFilter = /readdirSync\([^)]+\)\.filter\([^)]*startsWith\('gsd-'\)\s*&&[^)]*endsWith\('\.js'\)/s.test(content);
assert.ok(!hasBroadFilter,
'scanner must NOT use broad startsWith("gsd-") && endsWith(".js") filter — ' +
'this catches orphaned hooks from removed features (e.g., gsd-intel-index.js). ' +
'Use a MANAGED_HOOKS allowlist instead.');
});
test('gsd-check-update.js spawns the worker by file path (not inline -e code)', () => {
// After the worker extraction, the main hook must spawn the worker file
// rather than embedding all logic in a template literal.
const content = fs.readFileSync(CHECK_UPDATE_PATH, 'utf8');
assert.ok(
content.includes('gsd-check-update-worker.js'),
'gsd-check-update.js must reference gsd-check-update-worker.js as the spawn target'
);
assert.ok(
!content.includes("'-e'"),
'gsd-check-update.js must not use node -e inline code (logic moved to worker file)'
);
});
test('managed hooks list in worker matches build-hooks HOOKS_TO_COPY JS entries', () => {
// Extract JS hooks from build-hooks.js HOOKS_TO_COPY
const buildContent = fs.readFileSync(BUILD_HOOKS_PATH, 'utf8');
const hooksArrayMatch = buildContent.match(/HOOKS_TO_COPY\s*=\s*\[([\s\S]*?)\]/);
assert.ok(hooksArrayMatch, 'should find HOOKS_TO_COPY array');
const jsHooks = [];
const hookEntries = hooksArrayMatch[1].matchAll(/'([^']+\.js)'/g);
for (const m of hookEntries) {
jsHooks.push(m[1]);
}
assert.ok(jsHooks.length >= 5, `expected at least 5 JS hooks in HOOKS_TO_COPY, got ${jsHooks.length}`);
// MANAGED_HOOKS in the worker must include each JS hook from HOOKS_TO_COPY
const workerContent = fs.readFileSync(WORKER_PATH, 'utf8');
for (const hook of jsHooks) {
assert.ok(
workerContent.includes(hook),
`MANAGED_HOOKS in worker should include '${hook}' from HOOKS_TO_COPY`
);
}
});
test('orphaned hook filenames are NOT in the MANAGED_HOOKS list', () => {
const workerContent = fs.readFileSync(WORKER_PATH, 'utf8');
// These are real orphaned hooks from the removed intel feature
const orphanedHooks = [
'gsd-intel-index.js',
'gsd-intel-prune.js',
'gsd-intel-session.js',
];
for (const orphan of orphanedHooks) {
assert.ok(
!workerContent.includes(orphan),
`orphaned hook '${orphan}' must NOT be in the MANAGED_HOOKS list`
);
}
});
});