* fix(#1572): preserve must_haves object-lists across frontmatter set/merge spliceFrontmatter round-tripped the WHOLE frontmatter through extractFrontmatter (a scalar-only parser) then reconstructFrontmatter (a lossy serializer), so any must_haves object-list — artifacts {path, provides}, prohibitions {statement, status} — was flattened to scalar strings and re-emitted as a malformed inline array whenever an UNRELATED field changed, silently dropping every provides:/ status: value. The write now preserves the original raw text for any top-level key whose value is structurally unchanged between the original parse and the new object (generalizing the existing whole-document no-op guard to per-key fidelity), and regenerates only the key that actually changed. The key set is still defined by newObj (the cmdSet/cmdMerge flow always passes the full merged object). spliceFrontmatter's only callers are cmdFrontmatterSet/Merge — the STATE.md read-modify-write family calls reconstructFrontmatter directly and is unaffected. Regression cases folded into tests/frontmatter-cli.test.cjs: artifacts/prohibitions object-lists survive set and merge; idempotent on repeat sets. Asserted via parseMustHavesBlock (the structure-preserving parser). * chore(#1572): backfill changeset pr ref to 1656 * fix(#1572): fail-closed when set/merge would emit [object Object] (codex review) Adversarial review (codex, gpt-5.5/high) flagged that directly setting a must_haves object-list (a CHANGED key) still routed through the lossy reconstructFrontmatter, emitting literal "[object Object]" and destroying the data. The reported case (mutating an UNRELATED field) was already fixed by per-key raw-text preservation, but the changed-object-list path was still silently lossy. Add fail-closed: when a regenerated key's text contains the "[object Object]" sentinel, spliceFrontmatter throws — cmdFrontmatterSet/Merge error out WITHOUT writing, directing the user to edit the file directly. The no-frontmatter (generate-from-scratch) path is guarded the same way. Adds a test that a refused set leaves the file unchanged and the original object-list intact. Codex finding #2 (a contrived flattened-projection no-op) is a deeper limitation noted in the PR — non-destructive, and the fail-closed message already directs users to edit object-list blocks directly. * test(#1572): add spliceFrontmatter per-key preservation + fail-closed unit coverage Stryker mutates gsd-core/bin/lib/frontmatter.cjs against tests/frontmatter.{property,unit}.test.cjs (MinScore 62). The #1572 regression cases live in frontmatter-cli.test.cjs, which is NOT in Stryker's test set, so the new functions (sliceTopLevelFrontmatterSegments, the per-key preserve/regenerate/drop/append loop, regenerateFrontmatterKey's [object Object] fail-closed) had surviving mutants that dropped the module below threshold. Add unit-level coverage in frontmatter.unit.test.cjs exercising every new branch directly via spliceFrontmatter: unchanged object-list preserved (provides survives) when a scalar sibling changes; changed scalar regenerates only that key; orphan keys dropped; new keys appended; indented nested block stays attached to its parent key; whole-document no-op returns input verbatim; both fail-closed paths (changed object-list + no-frontmatter) throw.
405 lines
17 KiB
JavaScript
405 lines
17 KiB
JavaScript
// allow-test-rule: source-text-is-the-product
|
|
// Workflow .md / agent .md / command .md / reference .md files — their text
|
|
// IS what the runtime loads. Testing text content tests the deployed contract.
|
|
// Per CONTRIBUTING.md exception matrix.
|
|
|
|
/**
|
|
* GSD Tools Tests - frontmatter CLI integration
|
|
*
|
|
* Integration tests for the 4 frontmatter subcommands (get, set, merge, validate)
|
|
* exercised through gsd-tools.cjs via execSync.
|
|
*
|
|
* Each test creates its own temp file, runs the CLI command, asserts output,
|
|
* and cleans up in afterEach (per-test cleanup with individual temp files).
|
|
*/
|
|
|
|
const { test, describe, afterEach } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
const { runGsdTools } = require('./helpers.cjs');
|
|
|
|
// Track temp files for cleanup
|
|
let tempFiles = [];
|
|
|
|
function writeTempFile(content) {
|
|
const tmpFile = path.join(os.tmpdir(), `gsd-fm-test-${Date.now()}-${Math.random().toString(36).slice(2)}.md`);
|
|
fs.writeFileSync(tmpFile, content, 'utf-8');
|
|
tempFiles.push(tmpFile);
|
|
return tmpFile;
|
|
}
|
|
|
|
afterEach(() => {
|
|
for (const f of tempFiles) {
|
|
try { fs.unlinkSync(f); } catch { /* already cleaned */ }
|
|
}
|
|
tempFiles = [];
|
|
});
|
|
|
|
// ─── frontmatter get ────────────────────────────────────────────────────────
|
|
|
|
describe('frontmatter get', () => {
|
|
test('returns all fields as JSON', () => {
|
|
const file = writeTempFile('---\nphase: 01\nplan: 01\ntype: execute\n---\nbody text');
|
|
const result = runGsdTools(['frontmatter', 'get', file]);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
const parsed = JSON.parse(result.output);
|
|
assert.strictEqual(parsed.phase, '01');
|
|
assert.strictEqual(parsed.plan, '01');
|
|
assert.strictEqual(parsed.type, 'execute');
|
|
});
|
|
|
|
test('returns specific field with --field', () => {
|
|
const file = writeTempFile('---\nphase: 01\nplan: 02\ntype: tdd\n---\nbody');
|
|
const result = runGsdTools(['frontmatter', 'get', file, '--field', 'phase']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
const parsed = JSON.parse(result.output);
|
|
assert.strictEqual(parsed.phase, '01');
|
|
});
|
|
|
|
test('returns error for missing field', () => {
|
|
const file = writeTempFile('---\nphase: 01\n---\n');
|
|
const result = runGsdTools(['frontmatter', 'get', file, '--field', 'nonexistent']);
|
|
// The command succeeds (exit 0) but returns an error object in JSON
|
|
assert.ok(result.success, 'Command should exit 0');
|
|
const parsed = JSON.parse(result.output);
|
|
assert.ok(parsed.error, 'Should have error field');
|
|
assert.ok(parsed.error.includes('Field not found'), 'Error should mention "Field not found"');
|
|
});
|
|
|
|
test('returns error for missing file', () => {
|
|
const result = runGsdTools('frontmatter get /nonexistent/path/file.md');
|
|
assert.ok(result.success, 'Command should exit 0 with error JSON');
|
|
const parsed = JSON.parse(result.output);
|
|
assert.ok(parsed.error, 'Should have error field');
|
|
});
|
|
|
|
test('handles file with no frontmatter', () => {
|
|
const file = writeTempFile('Plain text with no frontmatter delimiters.');
|
|
const result = runGsdTools(['frontmatter', 'get', file]);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
const parsed = JSON.parse(result.output);
|
|
assert.deepStrictEqual(parsed, {}, 'Should return empty object for no frontmatter');
|
|
});
|
|
});
|
|
|
|
// ─── frontmatter set ────────────────────────────────────────────────────────
|
|
|
|
describe('frontmatter set', () => {
|
|
test('updates existing field', () => {
|
|
const file = writeTempFile('---\nphase: 01\ntype: execute\n---\nbody');
|
|
const result = runGsdTools(['frontmatter', 'set', file, '--field', 'phase', '--value', '02']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
// Read back and verify
|
|
const content = fs.readFileSync(file, 'utf-8');
|
|
const { extractFrontmatter } = require('../gsd-core/bin/lib/frontmatter.cjs');
|
|
const fm = extractFrontmatter(content);
|
|
assert.strictEqual(fm.phase, '02');
|
|
});
|
|
|
|
test('adds new field', () => {
|
|
const file = writeTempFile('---\nphase: 01\n---\nbody');
|
|
const result = runGsdTools(['frontmatter', 'set', file, '--field', 'status', '--value', 'active']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const content = fs.readFileSync(file, 'utf-8');
|
|
const { extractFrontmatter } = require('../gsd-core/bin/lib/frontmatter.cjs');
|
|
const fm = extractFrontmatter(content);
|
|
assert.strictEqual(fm.status, 'active');
|
|
});
|
|
|
|
test('handles JSON array value', () => {
|
|
const file = writeTempFile('---\nphase: 01\n---\nbody');
|
|
const result = runGsdTools(['frontmatter', 'set', file, '--field', 'tags', '--value', '["a","b"]']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const content = fs.readFileSync(file, 'utf-8');
|
|
const { extractFrontmatter } = require('../gsd-core/bin/lib/frontmatter.cjs');
|
|
const fm = extractFrontmatter(content);
|
|
assert.ok(Array.isArray(fm.tags), 'tags should be an array');
|
|
assert.deepStrictEqual(fm.tags, ['a', 'b']);
|
|
});
|
|
|
|
test('returns error for missing file', () => {
|
|
const result = runGsdTools('frontmatter set /nonexistent/file.md --field phase --value "01"');
|
|
assert.ok(result.success, 'Command should exit 0 with error JSON');
|
|
const parsed = JSON.parse(result.output);
|
|
assert.ok(parsed.error, 'Should have error field');
|
|
});
|
|
|
|
test('preserves body content after set', () => {
|
|
const bodyText = '\n\n# My Heading\n\nSome paragraph with special chars: $, %, &.';
|
|
const file = writeTempFile('---\nphase: 01\n---' + bodyText);
|
|
runGsdTools(['frontmatter', 'set', file, '--field', 'phase', '--value', '02']);
|
|
|
|
const content = fs.readFileSync(file, 'utf-8');
|
|
assert.ok(content.includes('# My Heading'), 'heading should be preserved');
|
|
assert.ok(content.includes('Some paragraph with special chars: $, %, &.'), 'body content should be preserved');
|
|
});
|
|
});
|
|
|
|
// ─── frontmatter merge ──────────────────────────────────────────────────────
|
|
|
|
describe('frontmatter merge', () => {
|
|
test('merges multiple fields into frontmatter', () => {
|
|
const file = writeTempFile('---\nphase: 01\n---\nbody');
|
|
const result = runGsdTools(['frontmatter', 'merge', file, '--data', '{"plan":"02","type":"tdd"}']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const content = fs.readFileSync(file, 'utf-8');
|
|
const { extractFrontmatter } = require('../gsd-core/bin/lib/frontmatter.cjs');
|
|
const fm = extractFrontmatter(content);
|
|
assert.strictEqual(fm.phase, '01', 'original field should be preserved');
|
|
assert.strictEqual(fm.plan, '02', 'merged field should be present');
|
|
assert.strictEqual(fm.type, 'tdd', 'merged field should be present');
|
|
});
|
|
|
|
test('overwrites existing fields on conflict', () => {
|
|
const file = writeTempFile('---\nphase: 01\ntype: execute\n---\nbody');
|
|
const result = runGsdTools(['frontmatter', 'merge', file, '--data', '{"phase":"02"}']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const content = fs.readFileSync(file, 'utf-8');
|
|
const { extractFrontmatter } = require('../gsd-core/bin/lib/frontmatter.cjs');
|
|
const fm = extractFrontmatter(content);
|
|
assert.strictEqual(fm.phase, '02', 'conflicting field should be overwritten');
|
|
assert.strictEqual(fm.type, 'execute', 'non-conflicting field should be preserved');
|
|
});
|
|
|
|
test('returns error for missing file', () => {
|
|
const result = runGsdTools(`frontmatter merge /nonexistent/file.md --data '{"phase":"01"}'`);
|
|
assert.ok(result.success, 'Command should exit 0 with error JSON');
|
|
const parsed = JSON.parse(result.output);
|
|
assert.ok(parsed.error, 'Should have error field');
|
|
});
|
|
|
|
test('returns error for invalid JSON data', () => {
|
|
const file = writeTempFile('---\nphase: 01\n---\nbody');
|
|
const result = runGsdTools(['frontmatter', 'merge', file, '--data', 'not json']);
|
|
// cmdFrontmatterMerge calls error() which exits with code 1
|
|
assert.ok(!result.success, 'Command should fail with non-zero exit code');
|
|
assert.ok(result.error.includes('Invalid JSON'), 'Error should mention invalid JSON');
|
|
});
|
|
});
|
|
|
|
// ─── frontmatter validate ───────────────────────────────────────────────────
|
|
|
|
describe('frontmatter validate', () => {
|
|
test('reports valid for complete plan frontmatter', () => {
|
|
const content = `---
|
|
phase: 01
|
|
plan: 01
|
|
type: execute
|
|
wave: 1
|
|
depends_on: []
|
|
files_modified: [src/auth.ts]
|
|
autonomous: true
|
|
must_haves:
|
|
truths:
|
|
- "All tests pass"
|
|
---
|
|
body`;
|
|
const file = writeTempFile(content);
|
|
const result = runGsdTools(['frontmatter', 'validate', file, '--schema', 'plan']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
const parsed = JSON.parse(result.output);
|
|
assert.strictEqual(parsed.valid, true, 'Should be valid');
|
|
assert.deepStrictEqual(parsed.missing, [], 'No fields should be missing');
|
|
assert.strictEqual(parsed.schema, 'plan');
|
|
});
|
|
|
|
test('reports invalid with missing fields', () => {
|
|
const file = writeTempFile('---\nphase: 01\n---\nbody');
|
|
const result = runGsdTools(['frontmatter', 'validate', file, '--schema', 'plan']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
const parsed = JSON.parse(result.output);
|
|
assert.strictEqual(parsed.valid, false, 'Should be invalid');
|
|
assert.ok(parsed.missing.length > 0, 'Should have missing fields');
|
|
// plan schema requires: phase, plan, type, wave, depends_on, files_modified, autonomous, must_haves
|
|
// phase is present, so 7 should be missing
|
|
assert.strictEqual(parsed.missing.length, 7, 'Should have 7 missing required fields');
|
|
assert.ok(parsed.missing.includes('plan'), 'plan should be in missing');
|
|
assert.ok(parsed.missing.includes('type'), 'type should be in missing');
|
|
assert.ok(parsed.missing.includes('must_haves'), 'must_haves should be in missing');
|
|
});
|
|
|
|
test('validates against summary schema', () => {
|
|
const content = `---
|
|
phase: 01
|
|
plan: 01
|
|
subsystem: testing
|
|
tags: [unit-tests, yaml]
|
|
duration: 5min
|
|
completed: 2026-02-25
|
|
---
|
|
body`;
|
|
const file = writeTempFile(content);
|
|
const result = runGsdTools(['frontmatter', 'validate', file, '--schema', 'summary']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
const parsed = JSON.parse(result.output);
|
|
assert.strictEqual(parsed.valid, true, 'Should be valid for summary schema');
|
|
assert.strictEqual(parsed.schema, 'summary');
|
|
});
|
|
|
|
test('validates against verification schema', () => {
|
|
const content = `---
|
|
phase: 01
|
|
verified: 2026-02-25
|
|
status: passed
|
|
score: 5/5
|
|
---
|
|
body`;
|
|
const file = writeTempFile(content);
|
|
const result = runGsdTools(['frontmatter', 'validate', file, '--schema', 'verification']);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
const parsed = JSON.parse(result.output);
|
|
assert.strictEqual(parsed.valid, true, 'Should be valid for verification schema');
|
|
assert.strictEqual(parsed.schema, 'verification');
|
|
});
|
|
|
|
test('returns error for unknown schema', () => {
|
|
const file = writeTempFile('---\nphase: 01\n---\n');
|
|
const result = runGsdTools(['frontmatter', 'validate', file, '--schema', 'unknown']);
|
|
// cmdFrontmatterValidate calls error() which exits with code 1
|
|
assert.ok(!result.success, 'Command should fail with non-zero exit code');
|
|
assert.ok(result.error.includes('Unknown schema'), 'Error should mention unknown schema');
|
|
});
|
|
|
|
test('returns error for missing file', () => {
|
|
const result = runGsdTools('frontmatter validate /nonexistent/file.md --schema plan');
|
|
assert.ok(result.success, 'Command should exit 0 with error JSON');
|
|
const parsed = JSON.parse(result.output);
|
|
assert.ok(parsed.error, 'Should have error field');
|
|
});
|
|
});
|
|
|
|
// ─── frontmatter set/merge: must_haves object-list preservation (#1572) ──────
|
|
// `frontmatter set`/`merge` round-tripped the WHOLE frontmatter through the lossy
|
|
// extractFrontmatter → reconstructFrontmatter pair, which flattens must_haves
|
|
// object-list items ({path, provides} maps) to scalar strings and re-emits them as a
|
|
// malformed inline array — destroying `provides:` whenever an UNRELATED field changed.
|
|
// The fix preserves the original raw text for any structurally-unchanged top-level key.
|
|
const { parseMustHavesBlock } = require('../gsd-core/bin/lib/frontmatter.cjs');
|
|
|
|
describe('frontmatter set/merge preserves must_haves object-lists (#1572)', () => {
|
|
const ARTIFACTS_PLAN = [
|
|
'---',
|
|
'phase: 1',
|
|
'wave: 1',
|
|
'plan: 01-01',
|
|
'type: implementation',
|
|
'depends_on: []',
|
|
'files_modified: []',
|
|
'autonomous: true',
|
|
'must_haves:',
|
|
' artifacts:',
|
|
' - path: src/foo.ts',
|
|
' provides: the foo',
|
|
' - path: src/bar.ts',
|
|
' provides: the bar',
|
|
'---',
|
|
'# body',
|
|
'',
|
|
].join('\n');
|
|
|
|
const PROHIBITIONS_PLAN = [
|
|
'---',
|
|
'phase: 1',
|
|
'wave: 1',
|
|
'must_haves:',
|
|
' prohibitions:',
|
|
' - statement: no direct DB calls',
|
|
' status: enforced',
|
|
' - statement: no print statements',
|
|
' status: pending',
|
|
'---',
|
|
'# body',
|
|
'',
|
|
].join('\n');
|
|
|
|
function runAndParse(plan, cmdArgsForFile) {
|
|
const file = writeTempFile(plan);
|
|
runGsdTools(cmdArgsForFile(file));
|
|
const after = fs.readFileSync(file, 'utf-8');
|
|
return after;
|
|
}
|
|
|
|
test('set on an unrelated scalar preserves every must_haves.artifacts entry (path + provides)', () => {
|
|
const after = runAndParse(ARTIFACTS_PLAN, f => ['frontmatter', 'set', f, '--field', 'wave', '--value', '2']);
|
|
assert.deepEqual(
|
|
parseMustHavesBlock(after, 'artifacts'),
|
|
[
|
|
{ path: 'src/foo.ts', provides: 'the foo' },
|
|
{ path: 'src/bar.ts', provides: 'the bar' },
|
|
],
|
|
'must_haves.artifacts object-list must survive a set on an unrelated field (#1572)',
|
|
);
|
|
});
|
|
|
|
test('merge of an unrelated field preserves every must_haves.artifacts entry', () => {
|
|
const after = runAndParse(ARTIFACTS_PLAN, f => ['frontmatter', 'merge', f, '--data', JSON.stringify({ wave: 2 })]);
|
|
assert.deepEqual(
|
|
parseMustHavesBlock(after, 'artifacts'),
|
|
[
|
|
{ path: 'src/foo.ts', provides: 'the foo' },
|
|
{ path: 'src/bar.ts', provides: 'the bar' },
|
|
],
|
|
'must_haves.artifacts object-list must survive a merge of an unrelated field (#1572)',
|
|
);
|
|
});
|
|
|
|
test('must_haves.prohibitions object-list is preserved on an unrelated set (same code path)', () => {
|
|
const after = runAndParse(PROHIBITIONS_PLAN, f => ['frontmatter', 'set', f, '--field', 'wave', '--value', '2']);
|
|
assert.deepEqual(
|
|
parseMustHavesBlock(after, 'prohibitions'),
|
|
[
|
|
{ statement: 'no direct DB calls', status: 'enforced' },
|
|
{ statement: 'no print statements', status: 'pending' },
|
|
],
|
|
'must_haves.prohibitions object-list must survive a set on an unrelated field (#1572)',
|
|
);
|
|
});
|
|
|
|
test('round-trip is stable: setting wave twice still preserves artifacts (per-key preservation is idempotent)', () => {
|
|
const file = writeTempFile(ARTIFACTS_PLAN);
|
|
runGsdTools(['frontmatter', 'set', file, '--field', 'wave', '--value', '2']);
|
|
runGsdTools(['frontmatter', 'set', file, '--field', 'wave', '--value', '3']);
|
|
const after = fs.readFileSync(file, 'utf-8');
|
|
assert.deepEqual(
|
|
parseMustHavesBlock(after, 'artifacts'),
|
|
[
|
|
{ path: 'src/foo.ts', provides: 'the foo' },
|
|
{ path: 'src/bar.ts', provides: 'the bar' },
|
|
],
|
|
'must_haves.artifacts must survive repeated sets on an unrelated field',
|
|
);
|
|
});
|
|
|
|
test('directly setting must_haves to a new object-list fails closed instead of emitting [object Object] (#1572 codex review)', () => {
|
|
// A CHANGED key whose value is an object-list cannot be faithfully serialized by the
|
|
// lossy writer (it would emit "[object Object]"). Rather than silently destroy the
|
|
// data, spliceFrontmatter throws — the command fails and the file is left unchanged.
|
|
const file = writeTempFile(ARTIFACTS_PLAN);
|
|
const result = runGsdTools([
|
|
'frontmatter', 'set', file, '--field', 'must_haves',
|
|
'--value', JSON.stringify({ artifacts: [{ path: 'src/new.ts', provides: 'new thing' }] }),
|
|
]);
|
|
assert.ok(
|
|
!result.success,
|
|
'frontmatter set of a must_haves object-list must fail closed (refuse to emit "[object Object]")',
|
|
);
|
|
const after = fs.readFileSync(file, 'utf-8');
|
|
assert.ok(!/\[object Object\]/.test(after), 'the file must not contain "[object Object]" after a refused set');
|
|
assert.deepEqual(
|
|
parseMustHavesBlock(after, 'artifacts'),
|
|
[
|
|
{ path: 'src/foo.ts', provides: 'the foo' },
|
|
{ path: 'src/bar.ts', provides: 'the bar' },
|
|
],
|
|
'the original must_haves.artifacts must be intact after the refused set',
|
|
);
|
|
});
|
|
});
|