Files
msd-core/tests/state-prune.test.cjs
Tibsfox 01f0b4b540 feat(state): add --dry-run mode and resolved blocker pruning (#1970)
Review feedback from @trek-e — address scope gaps:

1. **--dry-run mode** — New flag that computes what would be pruned
   without modifying STATE.md. Returns structured output showing
   per-section counts so users can verify before committing.

2. **Resolved blocker pruning** — In addition to decisions and
   recently-completed entries, now prunes entries in the Blockers
   section that are marked resolved (~~strikethrough~~ or [RESOLVED]
   prefix) AND reference a phase older than the cutoff. Unresolved
   blockers are preserved regardless of age.

3. **Tests** — Added tests/state-prune.test.cjs (4 cases):
   - Prunes decisions older than cutoff, keeps recent
   - --dry-run reports changes without modifying STATE.md
   - Prunes resolved blockers, keeps unresolved regardless of age
   - Returns pruned:false when nothing exceeds cutoff

Scope items still deferred (to be filed as follow-up):
- Performance Metrics "By Phase" table row pruning — needs different
  regex handling than prose lines
- Auto-prune via workflow.auto_prune_state at phase completion — needs
  integration into cmdPhaseComplete

Also: the pre-existing test failure (2918/2919) is
tests/stale-colon-refs.test.cjs:83:3 "No stale /gsd: colon references
(#1748)". Verified failing on main, not introduced by this PR.
2026-04-11 03:43:46 -07:00

153 lines
4.5 KiB
JavaScript

/**
* Tests for `state prune` command (#1970).
*/
'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 { runGsdTools, createTempProject, cleanup } = require('./helpers.cjs');
function writeStateMd(tmpDir, content) {
fs.writeFileSync(path.join(tmpDir, '.planning', 'STATE.md'), content);
}
function readStateMd(tmpDir) {
return fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
}
function archiveExists(tmpDir) {
return fs.existsSync(path.join(tmpDir, '.planning', 'STATE-ARCHIVE.md'));
}
function readArchive(tmpDir) {
return fs.readFileSync(path.join(tmpDir, '.planning', 'STATE-ARCHIVE.md'), 'utf-8');
}
describe('state prune (#1970)', () => {
let tmpDir;
beforeEach(() => {
tmpDir = createTempProject();
});
afterEach(() => {
cleanup(tmpDir);
});
test('prunes decisions older than cutoff', () => {
writeStateMd(tmpDir, [
'# Session State',
'',
'**Current Phase:** 10',
'',
'## Decisions',
'',
'- [Phase 1]: Old decision',
'- [Phase 3]: Old decision 3',
'- [Phase 8]: Recent decision',
'- [Phase 10]: Current decision',
'',
].join('\n'));
const result = runGsdTools('state prune --keep-recent 3', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.pruned, true);
assert.strictEqual(output.cutoff_phase, 7);
const newState = readStateMd(tmpDir);
assert.match(newState, /\[Phase 8\]: Recent decision/);
assert.match(newState, /\[Phase 10\]: Current decision/);
assert.doesNotMatch(newState, /\[Phase 1\]: Old decision/);
assert.doesNotMatch(newState, /\[Phase 3\]: Old decision 3/);
assert.ok(archiveExists(tmpDir), 'STATE-ARCHIVE.md should exist');
const archive = readArchive(tmpDir);
assert.match(archive, /\[Phase 1\]: Old decision/);
assert.match(archive, /\[Phase 3\]: Old decision 3/);
});
test('--dry-run reports what would be pruned without modifying STATE.md', () => {
const originalContent = [
'# Session State',
'',
'**Current Phase:** 10',
'',
'## Decisions',
'',
'- [Phase 1]: Old decision',
'- [Phase 2]: Another old decision',
'- [Phase 9]: Recent decision',
'',
].join('\n');
writeStateMd(tmpDir, originalContent);
const result = runGsdTools('state prune --keep-recent 3 --dry-run', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.pruned, false);
assert.strictEqual(output.dry_run, true);
assert.strictEqual(output.total_would_archive, 2);
// STATE.md should be unchanged
const unchanged = readStateMd(tmpDir);
assert.strictEqual(unchanged, originalContent);
// No archive file should be created
assert.ok(!archiveExists(tmpDir), 'dry-run should not create archive');
});
test('prunes resolved blockers older than cutoff', () => {
writeStateMd(tmpDir, [
'# Session State',
'',
'**Current Phase:** 10',
'',
'## Blockers',
'',
'- ~~Phase 1: Old resolved issue~~',
'- [RESOLVED] Phase 2: Another old issue',
'- Phase 9: Current blocker (unresolved)',
'',
].join('\n'));
const result = runGsdTools('state prune --keep-recent 3', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.pruned, true);
const blockerSection = output.sections.find(s => /Blockers/i.test(s.section));
assert.ok(blockerSection, 'should report Blockers section');
assert.strictEqual(blockerSection.entries_archived, 2);
const newState = readStateMd(tmpDir);
assert.match(newState, /Phase 9: Current blocker/);
assert.doesNotMatch(newState, /Phase 1: Old resolved issue/);
});
test('returns pruned:false when nothing to prune', () => {
writeStateMd(tmpDir, [
'# Session State',
'',
'**Current Phase:** 2',
'',
'## Decisions',
'',
'- [Phase 1]: Recent decision',
'- [Phase 2]: Current decision',
'',
].join('\n'));
const result = runGsdTools('state prune --keep-recent 3', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.pruned, false);
});
});