Files
msd-core/tests/smart-entry.unit.test.cjs
Jeremy McSpadden e5ef323b15 feat(#1787): add /gsd:next smart entry workflow (#1798)
* docs: design spec for /gsd smart-entry command

Hybrid approach porting gsd-pi's smart-entry wizard to gsd-core:
deterministic classifier (gsd-tools smart-entry --json) + markdown
command/workflow with AskUserQuestion + --text fallback. Routing-first
('what now?' menu), 10 situations redesigned for gsd-core's phase loop.

* feat: add /gsd-start smart-entry command

State-aware front door adapted from gsd-pi's smart-entry wizard,
redesigned for gsd-core's markdown-first, multi-runtime architecture.

- src/smart-entry.cts: deterministic situation classifier (no-project,
  paused, blocked, verify-failed, needs-first-phase, planning, executing,
  verify-pending, idle-stranded, complete, unknown). Reads STATE.md,
  ROADMAP.md, git, and verify signals; emits JSON the workflow consumes.
- gsd-tools.cjs: wire  case + help listing.
- commands/gsd/start.md + gsd-core/workflows/gsd.md: thin markdown
  dispatcher presenting an AskUserQuestion menu (with --text fallback for
  non-Claude runtimes) and dispatching to existing commands. Falls back
  to /gsd:progress if detection is unavailable.
- help.md: document /gsd:start (parity with bug-2954).
- tests: smart-entry.unit.test.cjs (classifier behavior across all
  situations + priority + JSON shape) and gsd-workflow.structure.test.cjs
  (markdown-layer invariants + every emitted command resolves to a real
  slash command).

Spec: docs/superpowers/specs/2026-06-27-gsd-smart-entry-design.md
Note: command-contract (ADR-0002) requires a gsd:* prefix, so the bare
/gsd from the spec surfaces as /gsd-start.

* refactor: rename smart-entry command to /gsd:next

Rename the command from /gsd:start to /gsd:next per feedback. The
command file is now commands/gsd/next.md (name: gsd:next) and the
backing workflow is gsd-core/workflows/smart-entry.md (named for the
smart-entry classifier and gsd-tools smart-entry subcommand; does not
collide with the existing workflows/next.md, which is the progress
--next sub-workflow). help.md and the spec updated to match.

All affected tests (188) pass; lint:ci clean.

* fix: smart-entry reads real STATE.md schema (nested progress YAML + body Phase field)

Codex review found the classifier misread this repo's own STATE.md: it
looked only for scalar current_phase/total_phases frontmatter and body
fields named 'Current Phase'/'Total Phases', but real STATE.md stores
the phase as body 'Phase: N' and total_phases/percent under a nested
'progress:' YAML object. Both came back null, so active projects
(e.g. this repo at Phase 3 / verifying) wrongly classified as
needs-first-phase.

- detectSignals now reads total_phases + percent from nested progress{}
  first, then scalar fm, then body; current_phase falls back to the
  body 'Phase:' field (parseProsePhaseField lineage).
- Add regression tests against the real schema (nested progress YAML +
  body Phase field) covering verify-pending + executing situations.

Verified against this repo: now classifies verify-pending (was
needs-first-phase). Coverage 93.25% lines / 86.99% branches.

* fix(workflow): tiered fallback when gsd-tools is broken (not just smart-entry)

Live test exposed a self-defeating fallback: when smart-entry --json
failed because gsd-tools itself was broken (missing
markdown-sectionizer.cjs), the workflow fell back to /gsd:progress —
which also depends on gsd-tools and would dead-end too.

Replace the single /gsd:progress fallback with a tiered recovery:
1. Probe gsd_run state-snapshot. If it ALSO errors, the whole tool
   layer is down — read .planning/STATE.md directly with the Read tool
   and synthesize a minimal situation + actions menu so /gsd:next stays
   useful. Surface a rebuild hint.
2. Only if smart-entry alone is missing (older gsd-core), fall back to
   /gsd:progress as before.

Matches the direct-read resilience the live agent already did by hand.

* docs: add gsd-next skill surface

* chore: trigger no-mistakes validation

* no-mistakes(review): Fix smart-entry phase ordering

* no-mistakes(review): Fix decimal smart-entry phase ordering

* no-mistakes(test): Fix smart-entry next test contracts

* no-mistakes(document): Docs synced for smart entry

* chore: add changeset fragment for #1798 (/gsd:next smart-entry workflow)

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>

* fix: shorten next.md description and update golden install parity fixtures

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>

* fix: update /gsd-next refs to /gsd:next in docs and add Smart Entry topic alias

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>

* chore: trigger no-mistakes validation

* fix: regenerate INVENTORY-MANIFEST.json for new /gsd-next files

Full CI caught that adding commands/gsd/next.md + gsd-core/workflows/smart-entry.md
left docs/INVENTORY-MANIFEST.json stale (not in the affected-test scope that
no-mistakes' test gate runs, so it surfaced in CI). Regenerated via
node scripts/gen-inventory-manifest.cjs --write; inventory-manifest-sync
test now passes.

* fix: add 'next' to core_loop cluster, update INVENTORY-MANIFEST, fix gates.md ref

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>

* fix: regenerate golden install parity fixtures for /gsd:next

Full CI (shard 3/3) caught that adding commands/gsd/next.md + the
smart-entry workflow/lib made the per-runtime golden install parity
fixtures stale across all 16 runtimes. Regenerated via
UPDATE_GOLDEN=1 node --test tests/golden-install-parity.test.cjs.
All 16 fixtures + inventory-manifest-sync now pass.

* Fix smart-entry verify-failed phase scoping and empty resolve shim step

Scope detectVerifyFailed to STATE.md's current phase so leftover higher
phase directories cannot force verify-failed routing. Move the gsd_run
shim resolver into the workflow resolve step so agents define gsd_run
before the detect step runs smart-entry.

* fix: recapture golden fixtures with updated gates.md hash (/gsd:next)

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>

* fix: recapture all 16 golden fixtures with updated smart-entry.md hash

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>

* chore: regenerate fixtures + inventory manifest after rebase onto next

Rebased onto next which adopted #1837 (package-version normalization to
<VERSION> in golden-install-parity hashes). Recaptured the golden fixture
that needed it (hermes), re-sorted INVENTORY-MANIFEST.json, and regenerated
the gsd-next / ns-workflow skill descriptions to match the command surface.

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>

* refactor(#1787): delegate /gsd:next in-project advancement to gated /gsd:progress --next

Reconciles the /gsd:next smart-entry front door with the existing
/gsd:progress --next engine (davesienkowski review on PR #1798). The
classifier previously recommended /gsd:execute-phase directly for the
`executing` situation, bypassing workflows/next.md Route 0
(resume-incomplete-phase invariant, #160) and Gates 1-3 — reproducing the
duplication that got the old flat /gsd-next removed (#3054), plus a
correctness hazard (executing the recorded current phase while an earlier
phase is silently incomplete).

Now planning/executing/verify-pending recommend `/gsd:progress --next`
(single gated engine); the specific command stays an explicit secondary.
Off-path states (no-project, paused, blocked, verify-failed,
idle-stranded, complete) keep direct recommendations — smart-entry's
distinct value over --next. Adds docs/adr/1787-gsd-next-smart-entry.md and
a regression test locking the delegation contract.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* docs(#1787): avoid literal /gsd-next token in ADR (bug-3054 guard)

The repo-invariants #3054 guard bans the removed /gsd-next slash form in
docs surfaces. Refer to the removed command as `gsd-next` (prose) — the
historical reference is unchanged, just the banned token is dropped.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* chore: gitignore compiled host-integration-sdk + handshake-serialized .cjs

Pre-existing gap from #1683: these two src/*.cts modules compile to
gsd-core/bin/lib/*.cjs but were omitted from the per-file ignore list, so
`npm run build`/`npm test` left them as untracked build artifacts (dirty
tree + accidental-commit footgun). Adds them alongside their siblings
(host-integration.cjs, mcp-server.cjs, …). Found while finishing #1798.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* test(#1787): lock per-situation action invariants for all 11 situations + ADR typo

Adversarial-review follow-ups:
- Add a test asserting every situation's action set has exactly one
  recommended action, 1-4 unique-id /gsd:* actions (previously the
  one-recommended/1-4 invariant was only sampled for 6 of 11 situations).
- Fix ADR typo: /gsd-progress → /gsd:progress.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(#1798): split oversized test chunks so a slow shard can't trip the per-chunk timeout

Root-cause of the intermittent `full test (windows-latest, 22, shard 1/3)`
failure. It was NOT a leaked handle (the runner's kill message guesses that,
but --test-force-exit already exits leaks cleanly). Diagnosis:

- Ran every shard-1/3 file WITHOUT --test-force-exit + a 45s kill-timer:
  zero hangs, zero leaks — every file self-exits. So no leaked handle / hang.
- CI activity profile: output kept flowing (slowly) right up to the 600.0s
  kill — a dead hang would go silent. => pure slowness.
- Per-file timing: install-minimal-hooks.test.cjs is a 4987-line / 250-case
  consolidation file doing dozens of real installs — 41s even on a fast Mac
  (much worse on the slow Windows I/O path), plus an install-heavy cluster.

Mechanism: MAX_FILES_PER_CHUNK=180 packed the whole ~171-file shard into ONE
`node --test` chunk, so the entire shard's wall-clock ran against a single
600s per-chunk backstop. On slow Windows runners that single chunk crossed
600s and was killed mid-run — an intermittent false-negative gate that also
hits `next` directly.

Fix: lower MAX_FILES_PER_CHUNK 180 -> 90 so each shard splits into ~2 chunks,
each with its own fresh 600s budget and a fresh node process (also relieves
per-process memory pressure). Verified locally: shard 1/3 now runs as
chunk 1/2 (90 files) + chunk 2/2 (81 files), 5323 tests, 0 fail. Also made the
timeout kill-message name slowness as a cause instead of asserting a leak, so
the next debugger isn't sent hunting a nonexistent handle leak.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Codesmith <codesmith-bot@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Tom Boucher <trekkie@nomorestars.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 12:18:25 -04:00

431 lines
19 KiB
JavaScript

'use strict';
/**
* Unit tests for the smart-entry situation classifier.
*
* Spec: docs/superpowers/specs/2026-06-27-gsd-smart-entry-design.md
*
* Covers: all 11 situations, priority ordering (paused beats blocked), JSON
* shape invariants (exactly one recommended, commands are /gsd:* slash forms),
* and the --json / human output modes.
*/
const { describe, test, afterEach } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { execFileSync } = require('node:child_process');
const { cleanup } = require('./helpers.cjs');
const smartEntry = require('../gsd-core/bin/lib/smart-entry.cjs');
const { classify, classifyProject, detectSignals, SITUATIONS } = smartEntry;
const TOOLS = path.join(__dirname, '..', 'gsd-core', 'bin', 'gsd-tools.cjs');
// ─── Fixture helpers ─────────────────────────────────────────────────────────
/**
* Create a temp dir with a `.planning/` and optional STATE.md / ROADMAP.md.
* @returns {string} tmpDir path
*/
function makeProject({ state, roadmap = false, git = false, verifyFail = false } = {}) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-se-'));
fs.mkdirSync(path.join(tmpDir, '.planning', 'phases'), { recursive: true });
if (state !== undefined) {
fs.writeFileSync(path.join(tmpDir, '.planning', 'STATE.md'), state);
}
if (roadmap) {
fs.writeFileSync(path.join(tmpDir, '.planning', 'ROADMAP.md'), '# Roadmap\n');
}
if (git) {
execFileSync('git', ['init'], { cwd: tmpDir, stdio: 'pipe' });
execFileSync('git', ['config', 'user.email', 't@t.com'], { cwd: tmpDir, stdio: 'pipe' });
execFileSync('git', ['config', 'user.name', 'T'], { cwd: tmpDir, stdio: 'pipe' });
}
if (verifyFail) {
const phaseDir = path.join(tmpDir, '.planning', 'phases', '01-feat');
fs.mkdirSync(phaseDir, { recursive: true });
fs.writeFileSync(
path.join(phaseDir, 'SUMMARY.md'),
'# Summary\n\nSTATUS: blocked\n',
);
}
return tmpDir;
}
/** Minimal STATE.md frontmatter body. */
function state(opts) {
const fm = { ...opts };
// status goes in frontmatter as `status`; we also mirror as a body table for
// robustness against either format the real STATE.md uses.
const lines = ['---'];
for (const [k, v] of Object.entries(fm)) lines.push(`${k}: ${v}`);
lines.push('---', '', '# State', '');
if (fm.status) lines.push(`**Status:** ${fm.status}`);
return lines.join('\n') + '\n';
}
const CLEANUP = [];
function track(dir) {
CLEANUP.push(dir);
return dir;
}
function removeAll() {
for (const dir of CLEANUP) {
cleanup(dir);
}
CLEANUP.length = 0;
}
// ─── Tests ─────────────────────────────────────────────────────────────────────
describe('smart-entry: situation coverage', () => {
afterEach(removeAll);
const CASES = [
['no-project', () => {
// Truly empty dir — no .planning at all.
const d = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-se-none-'));
return track(d);
}],
['paused', () => track(makeProject({ state: state({ status: 'planning', paused_at: '2026-06-01T00:00:00Z' }), roadmap: true }))],
['blocked', () => track(makeProject({ state: state({ status: 'executing', total_phases: 5, current_phase: 2 }) + '\n## Blockers\n\n- Need API key\n', roadmap: true }))],
['verify-failed', () => track(makeProject({ state: state({ status: 'verify-failed', total_phases: 5, current_phase: 2 }), roadmap: true, verifyFail: true }))],
['needs-first-phase', () => track(makeProject({ state: state({ status: 'planning', total_phases: 0, current_phase: 0 }) }))],
['planning', () => track(makeProject({ state: state({ status: 'planning', total_phases: 5, current_phase: 2 }), roadmap: true }))],
['executing', () => track(makeProject({ state: state({ status: 'executing', total_phases: 5, current_phase: 2, progress: 60 }), roadmap: true }))],
['verify-pending', () => track(makeProject({ state: state({ status: 'needs-review', total_phases: 5, current_phase: 2 }), roadmap: true }))],
['complete', () => track(makeProject({ state: state({ status: 'complete', total_phases: 5, current_phase: 5 }), roadmap: true }))],
['unknown', () => track(makeProject({ state: state({ status: '', total_phases: 5, current_phase: 2 }), roadmap: true }))],
];
for (const [expected, factory] of CASES) {
test(`classifies "${expected}"`, () => {
const dir = factory();
const result = classifyProject(dir);
assert.equal(result.situation, expected);
});
}
test('SITUATIONS constant lists all 11 (incl unknown) and is frozen', () => {
assert.equal(SITUATIONS.length, 11);
assert.ok(SITUATIONS.includes('unknown'));
assert.ok(Object.isFrozen(SITUATIONS));
});
});
describe('smart-entry: idle-stranded (git-dependent)', () => {
afterEach(removeAll);
test('clean tree + unpushed commits → idle-stranded, recommended ship', () => {
// idle-stranded is the fallback AFTER the status predicates: it fires for an
// ambiguous status (matches none of planning/executing/verify/complete) with
// unpushed committed work. Use an empty status to land here deterministically.
const dir = track(makeProject({
state: state({ status: '', total_phases: 5, current_phase: 2 }),
roadmap: true,
git: true,
}));
// Commit the .planning files so the working tree is clean (untracked files
// would make git_dirty true and mask the stranded signal).
execFileSync('git', ['add', '-A'], { cwd: dir, stdio: 'pipe' });
execFileSync('git', ['commit', '-m', 'init'], { cwd: dir, stdio: 'pipe' });
const base = detectSignals(dir);
assert.equal(base.git_dirty, false);
assert.equal(base.git_unpushed, false);
// Force the stranded signal and assert the situation + recommendation.
const forced = { ...base, git_unpushed: true };
assert.equal(classify(forced), 'idle-stranded');
});
test('idle-stranded action set recommends ship', () => {
const dir = track(makeProject({
state: state({ status: '', total_phases: 5, current_phase: 2 }),
roadmap: true,
}));
const base = detectSignals(dir);
const forced = { ...base, git_unpushed: true };
const situation = classify(forced);
assert.equal(situation, 'idle-stranded');
const actions = smartEntry.actionsFor(situation, forced);
assert.equal(actions[0].id, 'ship');
assert.equal(actions[0].recommended, true);
assert.equal(actions[0].command, '/gsd:ship');
});
});
describe('smart-entry: priority ordering', () => {
afterEach(removeAll);
test('verify-failed inspects current phase verify artifact', () => {
const dir = track(makeProject({
state: state({ status: 'executing', total_phases: 10, current_phase: 100 }),
roadmap: true,
}));
const phaseNinetyNine = path.join(dir, '.planning', 'phases', '99-old-phase');
const phaseOneHundred = path.join(dir, '.planning', 'phases', '100-current-phase');
fs.mkdirSync(phaseNinetyNine, { recursive: true });
fs.mkdirSync(phaseOneHundred, { recursive: true });
fs.writeFileSync(path.join(phaseNinetyNine, '99-VERIFICATION.md'), 'STATUS: passed\n');
fs.writeFileSync(path.join(phaseOneHundred, '100-VERIFICATION.md'), 'STATUS: failed\n');
const result = classifyProject(dir);
assert.equal(result.situation, 'verify-failed');
assert.equal(result.signals.verify_failed, true);
});
test('verify-failed ignores failure in a higher phase when state is on an earlier phase', () => {
const dir = track(makeProject({
state: state({ status: 'executing', total_phases: 10, current_phase: 2 }),
roadmap: true,
}));
const phaseTwo = path.join(dir, '.planning', 'phases', '02-active-phase');
const phaseOneHundred = path.join(dir, '.planning', 'phases', '100-leftover-phase');
fs.mkdirSync(phaseTwo, { recursive: true });
fs.mkdirSync(phaseOneHundred, { recursive: true });
fs.writeFileSync(path.join(phaseTwo, '02-VERIFICATION.md'), 'STATUS: passed\n');
fs.writeFileSync(path.join(phaseOneHundred, '100-VERIFICATION.md'), 'STATUS: failed\n');
const result = classifyProject(dir);
assert.equal(result.situation, 'executing');
assert.equal(result.signals.verify_failed, false);
});
test('verify-failed includes decimal phase directories for the current phase', () => {
const dir = track(makeProject({
state: state({ status: 'executing', total_phases: 10, current_phase: '7.1' }),
roadmap: true,
}));
const phaseSeven = path.join(dir, '.planning', 'phases', '07-base-phase');
const phaseSevenOne = path.join(dir, '.planning', 'phases', '07.1-inserted-phase');
fs.mkdirSync(phaseSeven, { recursive: true });
fs.mkdirSync(phaseSevenOne, { recursive: true });
fs.writeFileSync(path.join(phaseSeven, '07-VERIFICATION.md'), 'STATUS: passed\n');
fs.writeFileSync(path.join(phaseSevenOne, '07.1-VERIFICATION.md'), 'STATUS: failed\n');
const result = classifyProject(dir);
assert.equal(result.situation, 'verify-failed');
assert.equal(result.signals.verify_failed, true);
});
test('paused beats blocked (earlier row wins)', () => {
const dir = track(makeProject({
// paused_at set AND blockers present AND a phase loop: must resolve paused.
state: state({ status: 'executing', total_phases: 5, current_phase: 2, paused_at: '2026-06-01T00:00:00Z' })
+ '\n## Blockers\n\n- blocker one\n',
roadmap: true,
}));
const result = classifyProject(dir);
assert.equal(result.situation, 'paused');
});
test('blocked beats planning (earlier row wins)', () => {
const dir = track(makeProject({
state: state({ status: 'planning', total_phases: 5, current_phase: 2 })
+ '\n## Blockers\n\n- blocker one\n',
roadmap: true,
}));
const result = classifyProject(dir);
assert.equal(result.situation, 'blocked');
});
test('no-project beats everything (no .planning)', () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-se-empty-'));
track(tmpDir);
const result = classifyProject(tmpDir);
assert.equal(result.situation, 'no-project');
});
});
describe('smart-entry: real STATE.md schema (nested progress YAML + body Phase field)', () => {
afterEach(removeAll);
// Mirrors this repo's actual .planning/STATE.md: status + nested progress{}
// in frontmatter, phase number in the body as `Phase: N`. Codex review found
// the classifier originally misread this as needs-first-phase (#P1).
function realState({ status, phase, totalPhases, percent }) {
const fm = [
'---',
'gsd_state_version: 1.0',
`status: ${status}`,
'last_activity: 2026-06-13',
'progress:',
` total_phases: ${totalPhases}`,
` percent: ${percent}`,
'---',
'',
'# Project State',
'',
`Phase: ${phase}`,
'',
`**Status:** ${status}`,
'',
].join('\n');
return fm;
}
test('reads current_phase from body `Phase:` + total_phases/percent from nested progress{}', () => {
const dir = track(makeProject({
state: realState({ status: 'verifying', phase: 3, totalPhases: 5, percent: 40 }),
roadmap: true,
}));
const signals = detectSignals(dir);
assert.equal(signals.current_phase, 3, 'current_phase from body Phase: field');
assert.equal(signals.total_phases, 5, 'total_phases from nested progress.total_phases');
assert.equal(signals.progress, 40, 'percent from nested progress.percent');
assert.equal(signals.status, 'verifying');
});
test('classifies verify-pending (not needs-first-phase) for an active real-schema project', () => {
const dir = track(makeProject({
state: realState({ status: 'verifying', phase: 3, totalPhases: 5, percent: 40 }),
roadmap: true,
}));
const result = classifyProject(dir);
assert.equal(result.situation, 'verify-pending');
// Forward motion delegates to the gated engine; verify-work stays available.
assert.equal(result.recommended, 'progress-next');
assert.match(result.summary, /Phase 3 of 5/);
});
test('executing status with nested progress schema classifies executing', () => {
const dir = track(makeProject({
state: realState({ status: 'executing', phase: 2, totalPhases: 5, percent: 60 }),
roadmap: true,
}));
const result = classifyProject(dir);
assert.equal(result.situation, 'executing');
// Forward motion delegates to /gsd:progress --next, not a raw execute-phase.
assert.equal(result.recommended, 'progress-next');
});
});
describe('smart-entry: in-project advancement delegates to the gated engine', () => {
afterEach(removeAll);
// Reconciliation guard (#1787): /gsd:next must not re-implement forward routing.
// For every in-project forward-motion situation the recommended action is
// `/gsd:progress --next` (workflows/next.md), so Route 0's resume-incomplete
// -phase invariant + Gates 1-3 are never bypassed. Re-deriving advancement here
// is what got the old flat /gsd-next removed (#3054). The specific command
// (execute-phase / plan-phase / verify-work) stays as an explicit secondary.
for (const situation of ['planning', 'executing', 'verify-pending']) {
test(`${situation}: recommended action is /gsd:progress --next`, () => {
const signals = {
current_phase: 2, total_phases: 5, status: situation, progress: 40,
has_planning: true, has_roadmap: true, git_dirty: false, git_unpushed: false,
paused: false, blockers: [], has_git: true, verify_failed: false, stale_activity: false,
};
const actions = smartEntry.actionsFor(situation, signals);
const recommended = actions.filter((a) => a.recommended);
assert.equal(recommended.length, 1, 'exactly one recommended action');
assert.equal(recommended[0].command, '/gsd:progress --next');
});
}
// Remediation / lifecycle situations are OFF the linear advance path — they
// keep direct specific recommendations (their distinct value over --next).
const DIRECT = {
'no-project': '/gsd:new-project',
paused: '/gsd:resume-work',
blocked: '/gsd:debug',
'verify-failed': '/gsd:verify-work',
'idle-stranded': '/gsd:ship',
complete: '/gsd:new-milestone',
};
for (const [situation, command] of Object.entries(DIRECT)) {
test(`${situation}: keeps its direct recommendation (${command})`, () => {
const signals = {
current_phase: 2, total_phases: 5, status: situation, progress: 40,
has_planning: true, has_roadmap: true, git_dirty: false, git_unpushed: false,
paused: false, blockers: [], has_git: true, verify_failed: false, stale_activity: false,
};
const actions = smartEntry.actionsFor(situation, signals);
const recommended = actions.find((a) => a.recommended);
assert.equal(recommended.command, command);
});
}
});
describe('smart-entry: per-situation action invariants (all 11)', () => {
// Lock the action-set contract for EVERY situation, not just the 6 sampled by
// the JSON-shape test: exactly one recommended, 1-4 actions, unique ids, and
// /gsd:* command forms. Guards the reconciliation (and future edits) against
// silently breaking these for a less-common situation.
const sampleSignals = {
current_phase: 2, total_phases: 5, status: 'executing', progress: 60,
has_planning: true, has_roadmap: true, git_dirty: false, git_unpushed: false,
paused: false, blockers: [], has_git: true, verify_failed: false, stale_activity: false,
};
for (const situation of SITUATIONS) {
test(`${situation}: exactly one recommended, 1-4 unique-id /gsd:* actions`, () => {
const actions = smartEntry.actionsFor(situation, sampleSignals);
assert.ok(actions.length >= 1 && actions.length <= 4, `${situation}: 1-4 actions (got ${actions.length})`);
assert.equal(actions.filter((a) => a.recommended).length, 1, `${situation}: exactly one recommended`);
const ids = actions.map((a) => a.id);
assert.equal(new Set(ids).size, ids.length, `${situation}: action ids are unique`);
for (const a of actions) {
assert.ok(a.command.startsWith('/gsd:'), `${situation}/${a.id}: command is a /gsd: slash form`);
}
});
}
});
describe('smart-entry: JSON shape invariants', () => {
afterEach(removeAll);
test('every situation yields exactly one recommended action and /gsd:* commands', () => {
const dirs = [
track(makeProject()), // no-project
track(makeProject({ state: state({ status: 'planning', paused_at: '2026-06-01T00:00:00Z' }), roadmap: true })), // paused
track(makeProject({ state: state({ status: 'executing', total_phases: 5, current_phase: 2 }) + '\n## Blockers\n- b\n', roadmap: true })), // blocked
track(makeProject({ state: state({ status: 'executing', total_phases: 5, current_phase: 2, progress: 60 }), roadmap: true })), // executing
track(makeProject({ state: state({ status: 'complete', total_phases: 5, current_phase: 5 }), roadmap: true })), // complete
track(makeProject({ state: state({ status: '', total_phases: 5, current_phase: 2 }), roadmap: true })), // unknown
];
for (const dir of dirs) {
const result = classifyProject(dir);
const recommended = result.actions.filter((a) => a.recommended);
assert.equal(recommended.length, 1, `${result.situation}: exactly one recommended`);
assert.equal(recommended[0].id, result.recommended, `${result.situation}: recommended id matches`);
for (const a of result.actions) {
assert.ok(a.command.startsWith('/gsd:'), `${result.situation}/${a.id}: command is a slash form`);
}
assert.ok(result.summary.length > 0, `${result.situation}: summary non-empty`);
assert.ok(result.actions.length >= 1 && result.actions.length <= 4, `${result.situation}: 1-4 actions`);
}
});
});
describe('smart-entry: CLI dispatch (gsd-tools smart-entry)', () => {
afterEach(removeAll);
test('--json in an empty dir returns no-project machine JSON', () => {
// A bare tmpdir with no .planning is a true no-project.
const bare = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-se-bare-'));
track(bare);
const out = execFileSync(process.execPath, [TOOLS, 'smart-entry', '--json', '--cwd', bare], {
encoding: 'utf-8',
});
const j = JSON.parse(out);
assert.equal(j.situation, 'no-project');
assert.equal(j.recommended, 'new-project');
assert.equal(j.actions[0].command, '/gsd:new-project');
});
test('default (human) mode prints a plain summary line, not JSON', () => {
const bare = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-se-human-'));
track(bare);
const out = execFileSync(process.execPath, [TOOLS, 'smart-entry', '--cwd', bare], {
encoding: 'utf-8',
});
assert.ok(!out.startsWith('{'), 'human mode is not JSON');
assert.match(out, /No project yet/);
assert.match(out, /Recommended:/);
});
});