508 lines
18 KiB
JavaScript
508 lines
18 KiB
JavaScript
/**
|
|
* GSD Tools Tests - State
|
|
*/
|
|
|
|
const { test, describe, beforeEach, afterEach } = require('node:test');
|
|
const assert = require('node:assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { runGsdTools, createTempProject, cleanup } = require('./helpers.cjs');
|
|
|
|
describe('state-snapshot command', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createTempProject();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(tmpDir);
|
|
});
|
|
|
|
test('missing STATE.md returns error', () => {
|
|
const result = runGsdTools('state-snapshot', tmpDir);
|
|
assert.ok(result.success, `Command should succeed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.error, 'STATE.md not found', 'should report missing file');
|
|
});
|
|
|
|
test('extracts basic fields from STATE.md', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 03
|
|
**Current Phase Name:** API Layer
|
|
**Total Phases:** 6
|
|
**Current Plan:** 03-02
|
|
**Total Plans in Phase:** 3
|
|
**Status:** In progress
|
|
**Progress:** 45%
|
|
**Last Activity:** 2024-01-15
|
|
**Last Activity Description:** Completed 03-01-PLAN.md
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools('state-snapshot', tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.current_phase, '03', 'current phase extracted');
|
|
assert.strictEqual(output.current_phase_name, 'API Layer', 'phase name extracted');
|
|
assert.strictEqual(output.total_phases, 6, 'total phases extracted');
|
|
assert.strictEqual(output.current_plan, '03-02', 'current plan extracted');
|
|
assert.strictEqual(output.total_plans_in_phase, 3, 'total plans extracted');
|
|
assert.strictEqual(output.status, 'In progress', 'status extracted');
|
|
assert.strictEqual(output.progress_percent, 45, 'progress extracted');
|
|
assert.strictEqual(output.last_activity, '2024-01-15', 'last activity date extracted');
|
|
});
|
|
|
|
test('extracts decisions table', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 01
|
|
|
|
## Decisions Made
|
|
|
|
| Phase | Decision | Rationale |
|
|
|-------|----------|-----------|
|
|
| 01 | Use Prisma | Better DX than raw SQL |
|
|
| 02 | JWT auth | Stateless authentication |
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools('state-snapshot', tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.decisions.length, 2, 'should have 2 decisions');
|
|
assert.strictEqual(output.decisions[0].phase, '01', 'first decision phase');
|
|
assert.strictEqual(output.decisions[0].summary, 'Use Prisma', 'first decision summary');
|
|
assert.strictEqual(output.decisions[0].rationale, 'Better DX than raw SQL', 'first decision rationale');
|
|
});
|
|
|
|
test('extracts blockers list', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 03
|
|
|
|
## Blockers
|
|
|
|
- Waiting for API credentials
|
|
- Need design review for dashboard
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools('state-snapshot', tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.deepStrictEqual(output.blockers, [
|
|
'Waiting for API credentials',
|
|
'Need design review for dashboard',
|
|
], 'blockers extracted');
|
|
});
|
|
|
|
test('extracts session continuity info', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 03
|
|
|
|
## Session
|
|
|
|
**Last Date:** 2024-01-15
|
|
**Stopped At:** Phase 3, Plan 2, Task 1
|
|
**Resume File:** .planning/phases/03-api/03-02-PLAN.md
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools('state-snapshot', tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.session.last_date, '2024-01-15', 'session date extracted');
|
|
assert.strictEqual(output.session.stopped_at, 'Phase 3, Plan 2, Task 1', 'stopped at extracted');
|
|
assert.strictEqual(output.session.resume_file, '.planning/phases/03-api/03-02-PLAN.md', 'resume file extracted');
|
|
});
|
|
|
|
test('handles paused_at field', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 03
|
|
**Paused At:** Phase 3, Plan 1, Task 2 - mid-implementation
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools('state-snapshot', tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.paused_at, 'Phase 3, Plan 1, Task 2 - mid-implementation', 'paused_at extracted');
|
|
});
|
|
|
|
test('supports --cwd override when command runs outside project root', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Session State
|
|
|
|
**Current Phase:** 03
|
|
**Status:** Ready to plan
|
|
`
|
|
);
|
|
const outsideDir = fs.mkdtempSync(path.join(require('os').tmpdir(), 'gsd-test-outside-'));
|
|
|
|
try {
|
|
const result = runGsdTools(`state-snapshot --cwd "${tmpDir}"`, outsideDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.current_phase, '03', 'should read STATE.md from overridden cwd');
|
|
assert.strictEqual(output.status, 'Ready to plan', 'should parse status from overridden cwd');
|
|
} finally {
|
|
cleanup(outsideDir);
|
|
}
|
|
});
|
|
|
|
test('returns error for invalid --cwd path', () => {
|
|
const invalid = path.join(tmpDir, 'does-not-exist');
|
|
const result = runGsdTools(`state-snapshot --cwd "${invalid}"`, tmpDir);
|
|
assert.ok(!result.success, 'should fail for invalid --cwd');
|
|
assert.ok(result.error.includes('Invalid --cwd'), 'error should mention invalid --cwd');
|
|
});
|
|
});
|
|
|
|
describe('state mutation commands', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createTempProject();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(tmpDir);
|
|
});
|
|
|
|
test('add-decision preserves dollar amounts without corrupting Decisions section', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
## Decisions
|
|
No decisions yet.
|
|
|
|
## Blockers
|
|
None
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools(
|
|
"state add-decision --phase 11-01 --summary 'Benchmark prices moved from $0.50 to $2.00 to $5.00' --rationale 'track cost growth'",
|
|
tmpDir
|
|
);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const state = fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
|
|
assert.match(
|
|
state,
|
|
/- \[Phase 11-01\]: Benchmark prices moved from \$0\.50 to \$2\.00 to \$5\.00 — track cost growth/,
|
|
'decision entry should preserve literal dollar values'
|
|
);
|
|
assert.strictEqual((state.match(/^## Decisions$/gm) || []).length, 1, 'Decisions heading should not be duplicated');
|
|
assert.ok(!state.includes('No decisions yet.'), 'placeholder should be removed');
|
|
});
|
|
|
|
test('add-blocker preserves dollar strings without corrupting Blockers section', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
## Decisions
|
|
None
|
|
|
|
## Blockers
|
|
None
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools("state add-blocker --text 'Waiting on vendor quote $1.00 before approval'", tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const state = fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
|
|
assert.match(state, /- Waiting on vendor quote \$1\.00 before approval/, 'blocker entry should preserve literal dollar values');
|
|
assert.strictEqual((state.match(/^## Blockers$/gm) || []).length, 1, 'Blockers heading should not be duplicated');
|
|
});
|
|
|
|
test('add-decision supports file inputs to preserve shell-sensitive dollar text', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
## Decisions
|
|
No decisions yet.
|
|
|
|
## Blockers
|
|
None
|
|
`
|
|
);
|
|
|
|
const summaryPath = path.join(tmpDir, 'decision-summary.txt');
|
|
const rationalePath = path.join(tmpDir, 'decision-rationale.txt');
|
|
fs.writeFileSync(summaryPath, 'Price tiers: $0.50, $2.00, else $5.00\n');
|
|
fs.writeFileSync(rationalePath, 'Keep exact currency literals for budgeting\n');
|
|
|
|
const result = runGsdTools(
|
|
`state add-decision --phase 11-02 --summary-file "${summaryPath}" --rationale-file "${rationalePath}"`,
|
|
tmpDir
|
|
);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const state = fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
|
|
assert.match(
|
|
state,
|
|
/- \[Phase 11-02\]: Price tiers: \$0\.50, \$2\.00, else \$5\.00 — Keep exact currency literals for budgeting/,
|
|
'file-based decision input should preserve literal dollar values'
|
|
);
|
|
});
|
|
|
|
test('add-blocker supports --text-file for shell-sensitive text', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
## Decisions
|
|
None
|
|
|
|
## Blockers
|
|
None
|
|
`
|
|
);
|
|
|
|
const blockerPath = path.join(tmpDir, 'blocker.txt');
|
|
fs.writeFileSync(blockerPath, 'Vendor quote updated from $1.00 to $2.00 pending approval\n');
|
|
|
|
const result = runGsdTools(`state add-blocker --text-file "${blockerPath}"`, tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const state = fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
|
|
assert.match(state, /- Vendor quote updated from \$1\.00 to \$2\.00 pending approval/);
|
|
});
|
|
});
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
// state json command (machine-readable STATE.md frontmatter)
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
describe('state json command', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createTempProject();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(tmpDir);
|
|
});
|
|
|
|
test('missing STATE.md returns error', () => {
|
|
const result = runGsdTools('state json', tmpDir);
|
|
assert.ok(result.success, `Command should succeed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.error, 'STATE.md not found', 'should report missing file');
|
|
});
|
|
|
|
test('builds frontmatter on-the-fly from body when no frontmatter exists', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 05
|
|
**Current Phase Name:** Deployment
|
|
**Total Phases:** 8
|
|
**Current Plan:** 05-03
|
|
**Total Plans in Phase:** 4
|
|
**Status:** In progress
|
|
**Progress:** 60%
|
|
**Last Activity:** 2026-01-20
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools('state json', tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.gsd_state_version, '1.0', 'should have version 1.0');
|
|
assert.strictEqual(output.current_phase, '05', 'current phase extracted');
|
|
assert.strictEqual(output.current_phase_name, 'Deployment', 'phase name extracted');
|
|
assert.strictEqual(output.current_plan, '05-03', 'current plan extracted');
|
|
assert.strictEqual(output.status, 'executing', 'status normalized to executing');
|
|
assert.ok(output.last_updated, 'should have last_updated timestamp');
|
|
assert.strictEqual(output.last_activity, '2026-01-20', 'last activity extracted');
|
|
assert.ok(output.progress, 'should have progress object');
|
|
assert.strictEqual(output.progress.percent, 60, 'progress percent extracted');
|
|
});
|
|
|
|
test('reads existing frontmatter when present', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`---
|
|
gsd_state_version: 1.0
|
|
current_phase: 03
|
|
status: paused
|
|
stopped_at: Plan 2 of Phase 3
|
|
---
|
|
|
|
# Project State
|
|
|
|
**Current Phase:** 03
|
|
**Status:** Paused
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools('state json', tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.gsd_state_version, '1.0', 'version from frontmatter');
|
|
assert.strictEqual(output.current_phase, '03', 'phase from frontmatter');
|
|
assert.strictEqual(output.status, 'paused', 'status from frontmatter');
|
|
assert.strictEqual(output.stopped_at, 'Plan 2 of Phase 3', 'stopped_at from frontmatter');
|
|
});
|
|
|
|
test('normalizes various status values', () => {
|
|
const statusTests = [
|
|
{ input: 'In progress', expected: 'executing' },
|
|
{ input: 'Ready to execute', expected: 'executing' },
|
|
{ input: 'Paused at Plan 3', expected: 'paused' },
|
|
{ input: 'Ready to plan', expected: 'planning' },
|
|
{ input: 'Phase complete — ready for verification', expected: 'verifying' },
|
|
{ input: 'Milestone complete', expected: 'completed' },
|
|
];
|
|
|
|
for (const { input, expected } of statusTests) {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# State\n\n**Current Phase:** 01\n**Status:** ${input}\n`
|
|
);
|
|
|
|
const result = runGsdTools('state json', tmpDir);
|
|
assert.ok(result.success, `Command failed for status "${input}": ${result.error}`);
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.status, expected, `"${input}" should normalize to "${expected}"`);
|
|
}
|
|
});
|
|
});
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
// STATE.md frontmatter sync (write operations add frontmatter)
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
describe('STATE.md frontmatter sync', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createTempProject();
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup(tmpDir);
|
|
});
|
|
|
|
test('state update adds frontmatter to STATE.md', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 02
|
|
**Status:** Ready to execute
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools('state update Status "Executing Plan 1"', tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const content = fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
|
|
assert.ok(content.startsWith('---\n'), 'should start with frontmatter delimiter');
|
|
assert.ok(content.includes('gsd_state_version: 1.0'), 'should have version field');
|
|
assert.ok(content.includes('current_phase: 02'), 'frontmatter should have current phase');
|
|
assert.ok(content.includes('**Current Phase:** 02'), 'body field should be preserved');
|
|
assert.ok(content.includes('**Status:** Executing Plan 1'), 'updated field in body');
|
|
});
|
|
|
|
test('state patch adds frontmatter', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 04
|
|
**Status:** Planning
|
|
**Current Plan:** 04-01
|
|
`
|
|
);
|
|
|
|
const result = runGsdTools('state patch --Status "In progress" --"Current Plan" 04-02', tmpDir);
|
|
assert.ok(result.success, `Command failed: ${result.error}`);
|
|
|
|
const content = fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
|
|
assert.ok(content.startsWith('---\n'), 'should have frontmatter after patch');
|
|
});
|
|
|
|
test('frontmatter is idempotent on multiple writes', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 01
|
|
**Status:** Ready to execute
|
|
`
|
|
);
|
|
|
|
runGsdTools('state update Status "In progress"', tmpDir);
|
|
runGsdTools('state update Status "Paused"', tmpDir);
|
|
|
|
const content = fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
|
|
const delimiterCount = (content.match(/^---$/gm) || []).length;
|
|
assert.strictEqual(delimiterCount, 2, 'should have exactly one frontmatter block (2 delimiters)');
|
|
assert.ok(content.includes('status: paused'), 'frontmatter should reflect latest status');
|
|
});
|
|
|
|
test('round-trip: write then read via state json', () => {
|
|
fs.writeFileSync(
|
|
path.join(tmpDir, '.planning', 'STATE.md'),
|
|
`# Project State
|
|
|
|
**Current Phase:** 07
|
|
**Current Phase Name:** Production
|
|
**Total Phases:** 10
|
|
**Status:** In progress
|
|
**Current Plan:** 07-05
|
|
**Progress:** 70%
|
|
`
|
|
);
|
|
|
|
runGsdTools('state update Status "Executing Plan 5"', tmpDir);
|
|
|
|
const result = runGsdTools('state json', tmpDir);
|
|
assert.ok(result.success, `state json failed: ${result.error}`);
|
|
|
|
const output = JSON.parse(result.output);
|
|
assert.strictEqual(output.current_phase, '07', 'round-trip: phase preserved');
|
|
assert.strictEqual(output.current_phase_name, 'Production', 'round-trip: phase name preserved');
|
|
assert.strictEqual(output.status, 'executing', 'round-trip: status normalized');
|
|
assert.ok(output.last_updated, 'round-trip: timestamp present');
|
|
});
|
|
});
|
|
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
// summary-extract command
|
|
// ─────────────────────────────────────────────────────────────────────────────
|