test(#2974): migrate 8 test files to typed-IR assertions (#3016)

* test(#2974): migrate 8 test files to typed-IR assertions

Replaces raw stdout/stderr substring matching with structured-field
assertions per CONTRIBUTING.md "Prohibited: Raw Text Matching on Test
Outputs". Adds shared infrastructure for typed error emission so this
pattern is the easy path going forward.

Shared infrastructure:
- core.cjs: ERROR_REASON frozen enum + setJsonErrorMode/getJsonErrorMode
- gsd-tools.cjs: --json-errors CLI flag, parsed before subcommand dispatch
- config.cjs: typed reasons at all 7 error sites
- graphify.cjs: GRAPHIFY_REASON enum + reason/timeout_ms in execGraphify result
- bin/install.js: pure buildSdkFailFastReport() IR builder + renderer
- hooks/gsd-session-state.sh, gsd-phase-boundary.sh: emit Claude Code
  hookSpecificOutput JSON envelope with typed state_present/config_mode/
  planning_modified/file_path fields (no-op when hooks.community is off)

Test migrations (all pass, 171 tests across the 8 files):
- bug-2649-sdk-fail-fast: assert on ir.reason / ir.context / ir.fix_command
- bug-2687-config-read-warning-parity: assert.equal stderr === ''
- bug-2796-arg-parsing-regression: assert on result.json.updated/.phase
- bug-2838-summary-rescue: parse rescue footer, assert mtime invariant
- bug-2943-config-get-context-window: parse JSON, assert ERROR_REASON.CONFIG_KEY_NOT_FOUND
- graphify: assert reason === GRAPHIFY_REASON.ENOENT/TIMEOUT
- hooks-opt-in: parse hookSpecificOutput, assert typed fields
- security-scan: reclassified as source-text-is-the-product (scan label
  output and CI workflow YAML ARE the deployed contract)

Verification: lint-no-source-grep clean (0 violations), full suite
6741/6741 pass.

Closes #2974

* test(#2974): address CR feedback — typed code field, robust idempotency

Two CodeRabbit findings on #3016 addressed:

1. tests/hooks-opt-in.test.cjs:355 (Minor, inline) —
   parsed.reason.includes('Conventional Commits') was still substring
   matching after the typed-IR migration. Fixed at the source: the
   gsd-validate-commit hook now emits a typed `code` field
   ('CONVENTIONAL_COMMITS_VIOLATION', 'COMMIT_SUBJECT_TOO_LONG')
   alongside the human-readable `reason`. Test asserts strictEqual
   on the code; the prose copy is no longer part of the test contract.

2. tests/bug-2838-summary-rescue-gitignored-planning.test.cjs:224-250
   (Outside-diff) — mtimeMs alone can stay unchanged on coarse-grained
   filesystems (HFS+, FAT) when two rewrites land within the same
   timestamp tick, falsely passing the idempotency assertion.
   Replaced with a full snapshot (mtimeMs, ctimeMs, size, ino, sha256
   of contents) compared via assert.deepStrictEqual — the hash
   catches any rewrite the timestamp would miss.

Verification: 30/30 pass on the two affected files; lint-no-source-grep
clean (0 violations across 368 test files).
This commit is contained in:
Tom Boucher
2026-05-02 09:27:23 -04:00
committed by GitHub
parent de25400b70
commit f55069ecbf
17 changed files with 470 additions and 178 deletions

View File

@@ -24,9 +24,30 @@
'use strict';
// 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". Do not copy this pattern.
// Migrated to typed-IR (#2974):
// - The "rescued: yes" text contract is now parsed into a typed
// { rescued: 'yes' | 'no' | null } record by parseRescueFooter().
// Tests assert on the parsed key, not on regex against raw content.
// - The idempotent-rescue test no longer greps stdout/stderr for
// "Rescued ..." prose. Instead it asserts the filesystem-level
// invariant: the pre-existing file's mtime is unchanged after the
// rescue runs (a true no-op on disk).
/**
* Parse a SUMMARY.md's footer-style key:value lines into a typed record.
* The rescue script appends `rescued: yes` and similar metadata; tests
* assert on the parsed values rather than regex-matching the raw content.
*
* Returns: { [key: string]: string }. Unknown lines are ignored.
*/
function parseRescueFooter(content) {
const out = {};
for (const line of content.split('\n')) {
const m = line.match(/^([a-z_][\w-]*):\s*(.+?)\s*$/);
if (m) out[m[1]] = m[2];
}
return out;
}
const { describe, test, before, after } = require('node:test');
const assert = require('node:assert/strict');
@@ -155,7 +176,9 @@ describe('bug-2838: SUMMARY rescue handles gitignored .planning/', () => {
`SUMMARY was lost — rescue did not surface the file into main repo.\nRescue output:\n${rescueOut}`
);
const content = fs.readFileSync(summaryFinalPath, 'utf-8');
assert.match(content, /rescued: yes/);
const footer = parseRescueFooter(content);
assert.equal(footer.rescued, 'yes',
`expected typed footer.rescued === 'yes', got ${JSON.stringify(footer)}`);
} finally {
cleanup(tmp);
}
@@ -170,7 +193,9 @@ describe('bug-2838: SUMMARY rescue handles gitignored .planning/', () => {
`SUMMARY was lost — rescue did not surface the file into main repo.\nRescue output:\n${rescueOut}`
);
const content = fs.readFileSync(summaryFinalPath, 'utf-8');
assert.match(content, /rescued: yes/);
const footer = parseRescueFooter(content);
assert.equal(footer.rescued, 'yes',
`expected typed footer.rescued === 'yes', got ${JSON.stringify(footer)}`);
} finally {
cleanup(tmp);
}
@@ -196,7 +221,24 @@ describe('bug-2838: SUMMARY rescue handles gitignored .planning/', () => {
// Pre-place the same content in main repo
const mainDir = path.join(tmp, '.planning', 'quick', 'x');
fs.mkdirSync(mainDir, { recursive: true });
fs.writeFileSync(path.join(mainDir, 'x-SUMMARY.md'), body);
const mainSummary = path.join(mainDir, 'x-SUMMARY.md');
fs.writeFileSync(mainSummary, body);
// Capture a full filesystem snapshot BEFORE the rescue runs.
// Idempotent contract: when content already matches, the rescue must
// not touch the file. Migrated from a console-output grep
// (`stdout+stderr` did not contain "Rescued") to a typed on-disk
// check. mtimeMs alone is insufficient on coarse-grained filesystems
// (HFS+, FAT) where two rewrites within ~1s share an mtime — CR
// outside-diff finding (#3016). Snapshot includes mtime, ctime,
// size, ino, and a sha256 of contents so a rewrite is detectable
// even when the timestamp aliases.
const crypto = require('crypto');
const snapshotFile = (p) => {
const st = fs.statSync(p);
const hash = crypto.createHash('sha256').update(fs.readFileSync(p)).digest('hex');
return { mtimeMs: st.mtimeMs, ctimeMs: st.ctimeMs, size: st.size, ino: st.ino, hash };
};
const snapBefore = snapshotFile(mainSummary);
const script = `
set -u
@@ -211,9 +253,13 @@ ${block}
0,
`Rescue block failed unexpectedly.\nstdout: ${r.stdout}\nstderr: ${r.stderr}`
);
// No "Rescued" message expected because cmp -s matches.
assert.doesNotMatch(r.stdout + r.stderr, /Rescued .*x-SUMMARY\.md/);
assert.strictEqual(fs.readFileSync(path.join(mainDir, 'x-SUMMARY.md'), 'utf-8'), body);
// Typed-IR idempotency check (#2974): full snapshot unchanged. The
// sha256 hash catches rewrites that mtimeMs would miss on
// coarse-grained filesystems.
const snapAfter = snapshotFile(mainSummary);
assert.deepStrictEqual(snapAfter, snapBefore,
'rescue must not touch the file when content already matches (idempotent)');
assert.strictEqual(fs.readFileSync(mainSummary, 'utf-8'), body);
} finally {
try { sh(tmp, `git worktree remove "${wt}" --force`); } catch (_) {}
cleanup(tmp);