Files
get-shit-done/tests/bug-1967-cache-invalidation.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

106 lines
3.9 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 tests for #1967 cache invalidation.
*
* The disk scan cache in buildStateFrontmatter must be invalidated on
* writeStateMd to prevent stale reads if multiple state-mutating
* operations occur within the same Node process. This matters for:
* - SDK callers that require() gsd-tools.cjs as a module
* - Future dispatcher extensions that handle compound operations
* - Tests that import state.cjs directly
*/
'use strict';
const { test, describe, beforeEach, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const state = require('../get-shit-done/bin/lib/state.cjs');
describe('buildStateFrontmatter cache invalidation (#1967)', () => {
let tmpDir;
let planningDir;
let phasesDir;
let statePath;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-1967-cache-'));
planningDir = path.join(tmpDir, '.planning');
phasesDir = path.join(planningDir, 'phases');
fs.mkdirSync(phasesDir, { recursive: true });
// Create a minimal config and STATE.md
fs.writeFileSync(
path.join(planningDir, 'config.json'),
JSON.stringify({ project_code: 'TEST' })
);
statePath = path.join(planningDir, 'STATE.md');
fs.writeFileSync(statePath, [
'# State',
'',
'**Current Phase:** 1',
'**Status:** executing',
'**Total Phases:** 2',
'',
].join('\n'));
// Start with one phase directory containing one PLAN
const phase1 = path.join(phasesDir, '01-foo');
fs.mkdirSync(phase1);
fs.writeFileSync(path.join(phase1, '01-1-PLAN.md'), '---\nphase: 1\nplan: 1\n---\n# Plan\n');
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
test('writeStateMd invalidates cache so subsequent reads see new disk state', () => {
// First write — populates cache via buildStateFrontmatter
const content1 = fs.readFileSync(statePath, 'utf-8');
state.writeStateMd(statePath, content1, tmpDir);
// Create a NEW phase directory AFTER the first write
// Without cache invalidation, the second write would still see only 1 phase
const phase2 = path.join(phasesDir, '02-bar');
fs.mkdirSync(phase2);
fs.writeFileSync(path.join(phase2, '02-1-PLAN.md'), '---\nphase: 2\nplan: 1\n---\n# Plan\n');
fs.writeFileSync(path.join(phase2, '02-1-SUMMARY.md'), '---\nstatus: complete\n---\n# Summary\n');
// Second write in the SAME process — must see the new phase
const content2 = fs.readFileSync(statePath, 'utf-8');
state.writeStateMd(statePath, content2, tmpDir);
// Read back and parse frontmatter to verify it reflects 2 phases, not 1
const result = fs.readFileSync(statePath, 'utf-8');
const fmMatch = result.match(/^---\n([\s\S]*?)\n---/);
assert.ok(fmMatch, 'STATE.md should have frontmatter after writeStateMd');
const fm = fmMatch[1];
// Should show 2 total phases (the new disk state), not 1 (stale cache)
const totalPhasesMatch = fm.match(/total_phases:\s*(\d+)/);
assert.ok(totalPhasesMatch, 'frontmatter should contain total_phases');
assert.strictEqual(
parseInt(totalPhasesMatch[1], 10),
2,
'total_phases should reflect new disk state (2), not stale cache (1)'
);
// Should show 1 completed phase (phase 2 has SUMMARY)
const completedMatch = fm.match(/completed_phases:\s*(\d+)/);
assert.ok(completedMatch, 'frontmatter should contain completed_phases');
assert.strictEqual(
parseInt(completedMatch[1], 10),
1,
'completed_phases should reflect new disk state (1 complete), not stale cache (0)'
);
});
});