fix(#4096): honor --dry-run in todo complete and write completion keys inside the frontmatter fence (#4325)
* fix(#4096): honor --dry-run in todo complete and upsert completion keys inside the frontmatter fence * review(#4096): tighten todo complete flag rejection to any dash-prefixed token * chore(#4096): backfill PR number in changeset --------- Co-authored-by: sim <sim@local>
This commit is contained in:
@@ -14,6 +14,7 @@ const assert = require('node:assert/strict');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { runGsdTools, createTempProject, createTempDir, cleanup } = require('./helpers.cjs');
|
||||
const { splitLines } = require('../gsd-core/bin/lib/text-lines.cjs');
|
||||
const fc = require('./helpers/fast-check-setup.cjs');
|
||||
const { gitOrThrow, throwIfFailed } = require('./helpers/git-fixture.cjs');
|
||||
const { runNode } = require('./helpers/process-seam.cjs');
|
||||
@@ -652,12 +653,15 @@ describe('todo complete command', () => {
|
||||
'should be in completed'
|
||||
);
|
||||
|
||||
// Verify completion timestamp added
|
||||
// Verify completion timestamp added — #4096: the completed/status keys must
|
||||
// live INSIDE a well-formed frontmatter block (line 1 is the opening fence),
|
||||
// never above it.
|
||||
const content = fs.readFileSync(
|
||||
path.join(tmpDir, '.planning', 'todos', 'completed', 'add-dark-mode.md'),
|
||||
'utf-8'
|
||||
);
|
||||
assert.ok(content.startsWith('completed:'), 'should have completed timestamp');
|
||||
assert.ok(content.startsWith('---\n'), 'should open with a frontmatter fence');
|
||||
assert.match(content, /^completed: \d{4}-\d{2}-\d{2}$/m);
|
||||
});
|
||||
|
||||
test('fails for nonexistent todo', () => {
|
||||
@@ -665,6 +669,132 @@ describe('todo complete command', () => {
|
||||
assert.ok(!result.success, 'should fail');
|
||||
assert.ok(result.error.includes('not found'), 'error mentions not found');
|
||||
});
|
||||
|
||||
// #4096 regressions — --dry-run must not mutate, and completion keys must be
|
||||
// written inside the frontmatter fence.
|
||||
test('--dry-run previews without moving the file or mutating anything', () => {
|
||||
const pendingDir = path.join(tmpDir, '.planning', 'todos', 'pending');
|
||||
fs.mkdirSync(pendingDir, { recursive: true });
|
||||
const source = path.join(pendingDir, 'dry-run-probe.md');
|
||||
fs.writeFileSync(source, '---\ntitle: probe\nstatus: pending\n---\n\n# body\n');
|
||||
|
||||
const result = runGsdTools('todo complete dry-run-probe.md --dry-run', tmpDir);
|
||||
assert.ok(result.success, `Command failed: ${result.error}`);
|
||||
|
||||
const output = JSON.parse(result.output);
|
||||
assert.strictEqual(output.dry_run, true, 'payload must be preview-shaped (dry_run)');
|
||||
assert.strictEqual(output.would_complete, true, 'payload must say would_complete');
|
||||
assert.strictEqual('completed' in output, false, 'preview must never report completed:true');
|
||||
assert.strictEqual(output.file, 'dry-run-probe.md');
|
||||
assert.match(output.date, /^\d{4}-\d{2}-\d{2}$/);
|
||||
|
||||
// File untouched, in place; nothing written to completed/.
|
||||
assert.ok(fs.existsSync(source), 'pending file must still exist under --dry-run');
|
||||
const completedPath = path.join(tmpDir, '.planning', 'todos', 'completed', 'dry-run-probe.md');
|
||||
assert.ok(!fs.existsSync(completedPath), 'no completed copy under --dry-run');
|
||||
assert.strictEqual(
|
||||
fs.readFileSync(source, 'utf-8'),
|
||||
'---\ntitle: probe\nstatus: pending\n---\n\n# body\n',
|
||||
'source bytes must be unchanged under --dry-run'
|
||||
);
|
||||
});
|
||||
|
||||
test('--dry-run still fails for nonexistent todo', () => {
|
||||
const result = runGsdTools('todo complete nonexistent.md --dry-run', tmpDir);
|
||||
assert.ok(!result.success, 'dry-run must still run existence checks');
|
||||
assert.ok(result.error.includes('not found'), 'error mentions not found');
|
||||
});
|
||||
|
||||
test('writes completed and status: completed inside the frontmatter fence', () => {
|
||||
const pendingDir = path.join(tmpDir, '.planning', 'todos', 'pending');
|
||||
fs.mkdirSync(pendingDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(pendingDir, 'fence-probe.md'),
|
||||
'---\ntitle: fence probe\nstatus: pending\ncreated: 2025-01-01\n---\n\n# body\n'
|
||||
);
|
||||
|
||||
const result = runGsdTools('todo complete fence-probe.md', tmpDir);
|
||||
assert.ok(result.success, `Command failed: ${result.error}`);
|
||||
|
||||
const content = fs.readFileSync(
|
||||
path.join(tmpDir, '.planning', 'todos', 'completed', 'fence-probe.md'),
|
||||
'utf-8'
|
||||
);
|
||||
// Line 1 must remain the opening fence (#4096 defect 2).
|
||||
assert.ok(content.startsWith('---\n'), 'opening fence must stay on line 1');
|
||||
const lines = splitLines(content);
|
||||
const closeIdx = lines.indexOf('---', 1);
|
||||
assert.ok(closeIdx > 0, 'closing fence must exist');
|
||||
const fm = lines.slice(1, closeIdx);
|
||||
assert.ok(fm.includes('status: completed'), 'status: completed must be inside the fence');
|
||||
assert.strictEqual(fm.filter(l => /^completed: /.test(l)).length, 1,
|
||||
'exactly one completed: line, inside the fence');
|
||||
// Other frontmatter keys survive.
|
||||
assert.ok(fm.some(l => l.startsWith('title:')), 'existing keys preserved');
|
||||
// Body preserved after the block.
|
||||
assert.ok(content.includes('# body'), 'body preserved');
|
||||
});
|
||||
|
||||
test('upserts an existing completed field instead of duplicating it', () => {
|
||||
const pendingDir = path.join(tmpDir, '.planning', 'todos', 'pending');
|
||||
fs.mkdirSync(pendingDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(pendingDir, 'recomplete-probe.md'),
|
||||
'---\ntitle: recomplete\nstatus: pending\ncompleted: 2020-01-01\n---\n\n# body\n'
|
||||
);
|
||||
|
||||
const result = runGsdTools('todo complete recomplete-probe.md', tmpDir);
|
||||
assert.ok(result.success, `Command failed: ${result.error}`);
|
||||
|
||||
const content = fs.readFileSync(
|
||||
path.join(tmpDir, '.planning', 'todos', 'completed', 'recomplete-probe.md'),
|
||||
'utf-8'
|
||||
);
|
||||
assert.ok(content.startsWith('---\n'), 'opening fence must stay on line 1');
|
||||
assert.ok(!content.includes('completed: 2020-01-01'), 'stale completed value replaced');
|
||||
assert.strictEqual(
|
||||
(content.match(/^completed: /gm) || []).length, 1,
|
||||
'exactly one completed: occurrence'
|
||||
);
|
||||
});
|
||||
|
||||
test('wraps a frontmatter-less todo in a complete frontmatter block', () => {
|
||||
const pendingDir = path.join(tmpDir, '.planning', 'todos', 'pending');
|
||||
fs.mkdirSync(pendingDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(pendingDir, 'bare.md'), '# just a body\n');
|
||||
|
||||
const result = runGsdTools('todo complete bare.md', tmpDir);
|
||||
assert.ok(result.success, `Command failed: ${result.error}`);
|
||||
|
||||
const content = fs.readFileSync(
|
||||
path.join(tmpDir, '.planning', 'todos', 'completed', 'bare.md'),
|
||||
'utf-8'
|
||||
);
|
||||
// #4096 fix-2: no bare prefix line — a complete frontmatter block instead.
|
||||
assert.ok(content.startsWith('---\n'), 'frontmatter block must open the file');
|
||||
const lines = splitLines(content);
|
||||
const closeIdx = lines.indexOf('---', 1);
|
||||
assert.ok(closeIdx > 0, 'closing fence must exist');
|
||||
const fm = lines.slice(1, closeIdx);
|
||||
assert.ok(fm.some(l => /^completed: \d{4}-\d{2}-\d{2}$/.test(l)), 'completed inside block');
|
||||
assert.ok(fm.includes('status: completed'), 'status: completed inside block');
|
||||
assert.ok(content.includes('# just a body'), 'body preserved');
|
||||
});
|
||||
|
||||
test('rejects unknown flags instead of silently completing', () => {
|
||||
const pendingDir = path.join(tmpDir, '.planning', 'todos', 'pending');
|
||||
fs.mkdirSync(pendingDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(pendingDir, 'flag-probe.md'), '---\nstatus: pending\n---\n');
|
||||
|
||||
const result = runGsdTools('todo complete flag-probe.md --bogus-flag', tmpDir);
|
||||
assert.ok(!result.success, 'unknown flag must fail loudly');
|
||||
assert.ok(result.error.includes('Unknown flag'), 'error mentions Unknown flag');
|
||||
// And crucially: nothing moved.
|
||||
assert.ok(
|
||||
fs.existsSync(path.join(pendingDir, 'flag-probe.md')),
|
||||
'file must not move when a flag is rejected'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user