* 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).
157 lines
5.6 KiB
JavaScript
157 lines
5.6 KiB
JavaScript
/**
|
|
* Regression test for bug #2796
|
|
*
|
|
* roadmap.update-plan-progress used positional-only arg destructuring:
|
|
* `const phaseNum = args[0]`. When called with the flag form documented in
|
|
* execute-phase.md:228 (`--phase "TEST" --plan "01" --status "complete"`),
|
|
* args[0] was the literal string "--phase", which was passed to findPhase().
|
|
* findPhase found no phase named "--phase" and returned `updated: false` with
|
|
* `reason: "no matching checkbox found"`, silently no-oping. ROADMAP.md plan
|
|
* checkboxes never advanced.
|
|
*
|
|
* The stateBeginPhase handler already uses parseNamedArgs and is NOT affected.
|
|
*
|
|
* Fix: roadmap-update-plan-progress.ts now checks for --phase <value> before
|
|
* falling back to positional arg[0] (filtering out flag tokens).
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
const { describe, test, beforeEach, afterEach } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const { execFileSync } = require('node:child_process');
|
|
const { createTempGitProject, cleanup } = require('./helpers.cjs');
|
|
|
|
const REPO_ROOT = path.join(__dirname, '..');
|
|
const SDK_CLI = path.join(REPO_ROOT, 'sdk', 'dist', 'cli.js');
|
|
|
|
function runSdkQuery(subcommand, args, projectDir) {
|
|
const argv = ['query', subcommand, ...args, '--project-dir', projectDir];
|
|
let stdout = '';
|
|
let stderr = '';
|
|
let exitCode = 0;
|
|
try {
|
|
stdout = execFileSync(process.execPath, [SDK_CLI, ...argv], {
|
|
encoding: 'utf-8',
|
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
env: { ...process.env, GSD_SESSION_KEY: '' },
|
|
});
|
|
} catch (err) {
|
|
exitCode = err.status ?? 1;
|
|
stdout = err.stdout?.toString() ?? '';
|
|
stderr = err.stderr?.toString() ?? '';
|
|
}
|
|
let json = null;
|
|
try { json = JSON.parse(stdout.trim()); } catch { /* ok */ }
|
|
return { exitCode, stdout: stdout.trim(), stderr: stderr.trim(), json };
|
|
}
|
|
|
|
/** Create a minimal ROADMAP.md with a phase checkbox */
|
|
function createRoadmap(projectDir, phaseNum, planLabel) {
|
|
const planningDir = path.join(projectDir, '.planning');
|
|
fs.mkdirSync(path.join(planningDir, 'phases'), { recursive: true });
|
|
|
|
const roadmap = [
|
|
'# My Project Roadmap',
|
|
'',
|
|
'## v1.0 — MVP',
|
|
'',
|
|
`### Phase ${phaseNum}: Test Phase`,
|
|
'',
|
|
`**Plans:** 1/1 plans complete`,
|
|
'',
|
|
`| # | Phase | Plans | Status | Date |`,
|
|
`|---|-------|-------|--------|------|`,
|
|
`| ${phaseNum} | Test Phase | 0/1 | Planned | |`,
|
|
'',
|
|
`- [ ] ${planLabel}: Do the thing`,
|
|
'',
|
|
].join('\n');
|
|
|
|
fs.writeFileSync(path.join(planningDir, 'ROADMAP.md'), roadmap);
|
|
|
|
// Create the phase directory so findPhase finds it
|
|
const phaseDir = path.join(
|
|
planningDir, 'phases',
|
|
`${String(phaseNum).padStart(2, '0')}-test-phase`
|
|
);
|
|
fs.mkdirSync(phaseDir, { recursive: true });
|
|
|
|
// Create a plan file and a summary so progress = 1/1
|
|
fs.writeFileSync(path.join(phaseDir, `${planLabel}-PLAN.md`), '# Plan\n');
|
|
fs.writeFileSync(path.join(phaseDir, `${planLabel}-SUMMARY.md`), '# Summary\n');
|
|
|
|
return phaseDir;
|
|
}
|
|
|
|
describe('bug-2796: roadmap update-plan-progress accepts --phase flag', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createTempGitProject('gsd-test-2796-');
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(tmpDir);
|
|
});
|
|
|
|
test('flag form --phase <N> resolves the correct phase (not literal "--phase")', () => {
|
|
createRoadmap(tmpDir, '9', '01');
|
|
|
|
// Flag form: this is the form execute-phase.md:228 uses
|
|
const result = runSdkQuery(
|
|
'roadmap.update-plan-progress',
|
|
['--phase', '9'],
|
|
tmpDir
|
|
);
|
|
|
|
// Before fix: exitCode=1 with "phase --phase not found" or updated:false
|
|
// After fix: should succeed with phase="9" and updated:true
|
|
assert.strictEqual(result.exitCode, 0, `should exit 0; stderr: ${result.stderr}`);
|
|
assert.ok(result.json !== null, 'should emit JSON');
|
|
assert.ok(result.json.updated === true, 'updated should be true');
|
|
assert.strictEqual(String(result.json.phase), '9', 'phase should be "9", not "--phase"');
|
|
});
|
|
|
|
test('positional form still works (backward compat)', () => {
|
|
createRoadmap(tmpDir, '9', '01');
|
|
|
|
const result = runSdkQuery(
|
|
'roadmap.update-plan-progress',
|
|
['9'],
|
|
tmpDir
|
|
);
|
|
|
|
assert.strictEqual(result.exitCode, 0, `should exit 0; stderr: ${result.stderr}`);
|
|
assert.ok(result.json?.updated === true, 'updated should be true');
|
|
assert.strictEqual(String(result.json.phase), '9', 'phase should be "9"');
|
|
});
|
|
|
|
test('flag form does not pass "--phase" as the phase value to findPhase', () => {
|
|
// Before fix: findPhase("--phase") returned found:false, causing updated:false.
|
|
// Migrated #2974: assert on the typed JSON outcome (updated:true, exit 0)
|
|
// instead of grepping stderr for the failure message. If the parser had
|
|
// mis-fed "--phase" as the value, updated would be false and the structured
|
|
// result would surface the failure typed.
|
|
createRoadmap(tmpDir, '5', '01');
|
|
|
|
const result = runSdkQuery(
|
|
'roadmap.update-plan-progress',
|
|
['--phase', '5'],
|
|
tmpDir
|
|
);
|
|
|
|
assert.strictEqual(result.exitCode, 0,
|
|
`arg parser must accept --phase 5 cleanly; exitCode=${result.exitCode} stderr=${result.stderr}`);
|
|
assert.ok(result.json?.updated === true,
|
|
`expected updated:true (phase 5 found and progress updated); got json=${JSON.stringify(result.json)}`);
|
|
// The structured result also exposes the phase number that WAS resolved.
|
|
// It must be the numeric phase, not the flag name "--phase".
|
|
assert.strictEqual(String(result.json.phase), '5',
|
|
`result.phase must be the resolved phase value, not the flag literal; got ${result.json.phase}`);
|
|
});
|
|
});
|