Files
msd-core/sdk/src/query/template.test.ts
Rezolv c5b1445529 feat(sdk): golden parity harness and query handler CJS alignment (#2302 Track A) (#2341)
* feat(sdk): golden parity harness and query handler CJS alignment (#2302 Track A)

Golden/read-only parity tests and registry alignment, query handler fixes
(check-completion, state-mutation, commit, validate, summary, etc.), and
WAITING.json dual-write for .gsd/.planning readers.

Refs gsd-build/get-shit-done#2341

* fix(sdk): getMilestoneInfo matches GSD ROADMAP (🟡, last bold, STATE fallback)

- Recognize in-flight 🟡 milestone bullets like 🚧.
- Derive from last **vX.Y Title** before ## Phases when emoji absent.
- Fall back to STATE.md milestone when ROADMAP is missing; use last bare vX.Y
  in cleaned text instead of first (avoids v1.0 from shipped list).
- Fixes init.execute-phase milestone_version and buildStateFrontmatter after
  state.begin-phase (syncStateFrontmatter).

* feat(sdk): phase list, plan task structure, requirements extract handlers

- Register phase.list-plans, phase.list-artifacts, plan.task-structure,
  requirements.extract-from-plans (SDK-only; golden-policy exceptions).
- Add unit tests; document in QUERY-HANDLERS.md.
- writeProfile: honor --output, render dimensions, return profile_path and dimensions_scored.

* feat(sdk): centralize getGsdAgentsDir in query helpers

Extract agent directory resolution to helpers (GSD_AGENTS_DIR, primary
~/.claude/agents, legacy path). Use from init and docs-init init bundles.

docs(15): add 15-CONTEXT for autonomous phase-15 run.

* feat(sdk): query CLI CJS fallback and session correlation

- createRegistry(eventStream, sessionId) threads correlation into mutation events
- gsd-sdk query falls back to gsd-tools.cjs when no native handler matches
  (disable with GSD_QUERY_FALLBACK=off); stderr bridge warnings
- Export createRegistry from @gsd-build/sdk; add sdk/README.md
- Update QUERY-HANDLERS.md and registry module docs for fallback + sessionId
- Agents: prefer node dist/cli.js query over cat/grep for STATE and plans

* fix(sdk): init phase_found parity, docs-init agents path, state field extract

- Normalize findPhase not-found to null before roadmap fallback (matches findPhaseInternal)

- docs-init: use detectRuntime + resolveAgentsDir for checkAgentsInstalled

- state.cjs stateExtractField: horizontal whitespace only after colon (YAML progress guard)

- Tests: commit_docs default true; config-get golden uses temp config; golden integration green

Refs: #2302

* refactor(sdk): share SessionJsonlRecord in profile-extract-messages

CodeRabbit nit: dedupe JSONL record shape for isGenuineUserMessage and streamExtractMessages.

* fix(sdk): address CodeRabbit major threads (paths, gates, audit, verify)

- Resolve @file: and CLI JSON indirection relative to projectDir; guard empty normalized query command

- plan.task-structure + intel extract/patch-meta: resolvePathUnderProject containment

- check.config-gates: safe string booleans; plan_checker alias precedence over plan_check default

- state.validate/sync: phaseTokenMatches + comparePhaseNum ordering

- verify.schema-drift: token match phase dirs; files_modified from parsed frontmatter

- audit-open: has_scan_errors, unreadable rows, human report when scans fail

- requirements PLANNED key PLAN for root PLAN.md; gsd-tools timeout note

- ingest-docs: repo-root path containment; classifier output slug-hash

Golden parity test strips has_scan_errors until CJS adds field.

* fix: Resolve CodeRabbit security and quality findings
- Secure intel.ts and cli.ts against path traversal
- Catch and validate git add status in commit.ts
- Expand roadmap milestone marker extraction
- Fix parsing array-of-objects in frontmatter YAML
- Fix unhandled config evaluations
- Improve coverage test parity mapping

* test: raise planner character extraction limit to 48K

* fix(sdk): resolve TS build error in docs-init passing config
2026-04-20 18:09:02 -04:00

181 lines
7.2 KiB
TypeScript

/**
* Unit tests for template.ts — templateSelect and templateFill handlers.
*
* Also tests event emission wiring in createRegistry.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { mkdir, writeFile, readFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { templateSelect, templateFill } from './template.js';
import { createRegistry } from './index.js';
import { GSDEventStream } from '../event-stream.js';
import { GSDEventType } from '../types.js';
import type { GSDEvent } from '../types.js';
let tmpDir: string;
beforeEach(async () => {
tmpDir = join(tmpdir(), `gsd-template-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
await mkdir(join(tmpDir, '.planning', 'phases', '09-foundation'), { recursive: true });
// Create minimal STATE.md
await writeFile(join(tmpDir, '.planning', 'STATE.md'), '---\nstatus: executing\n---\n\n# Project State\n');
// Create minimal config.json
await writeFile(join(tmpDir, '.planning', 'config.json'), '{}');
});
afterEach(async () => {
await rm(tmpDir, { recursive: true, force: true });
});
describe('templateSelect', () => {
it('returns "plan" as default when phase dir has no plans', async () => {
const result = await templateSelect([], tmpDir);
expect((result.data as Record<string, unknown>).template).toBe('plan');
});
it('returns "summary" when PLAN exists but no SUMMARY', async () => {
const phaseDir = join(tmpDir, '.planning', 'phases', '09-foundation');
await writeFile(join(phaseDir, '09-01-PLAN.md'), '---\nphase: 09\n---\n# Plan');
const result = await templateSelect(['9'], tmpDir);
const data = result.data as Record<string, unknown>;
expect(data.template).toBe('summary');
});
it('returns "verification" when all plans have summaries', async () => {
const phaseDir = join(tmpDir, '.planning', 'phases', '09-foundation');
await writeFile(join(phaseDir, '09-01-PLAN.md'), '---\nphase: 09\n---\n# Plan');
await writeFile(join(phaseDir, '09-01-SUMMARY.md'), '---\nphase: 09\n---\n# Summary');
const result = await templateSelect(['9'], tmpDir);
const data = result.data as Record<string, unknown>;
expect(data.template).toBe('verification');
});
it('returns "plan" when phase dir not found', async () => {
const result = await templateSelect(['99'], tmpDir);
expect((result.data as Record<string, unknown>).template).toBe('plan');
});
});
describe('templateFill', () => {
it('creates summary file with expected frontmatter fields', async () => {
const outPath = join(tmpDir, 'test-summary.md');
const result = await templateFill(['summary', outPath], tmpDir);
const data = result.data as Record<string, unknown>;
expect(data.created).toBe(true);
const content = await readFile(outPath, 'utf-8');
expect(content).toContain('phase:');
expect(content).toContain('plan:');
expect(content).toContain('subsystem:');
expect(content).toContain('tags:');
expect(content).toContain('## Performance');
expect(content).toContain('## Accomplishments');
});
it('creates plan file with plan frontmatter skeleton', async () => {
const outPath = join(tmpDir, 'test-plan.md');
const result = await templateFill(['plan', outPath], tmpDir);
const data = result.data as Record<string, unknown>;
expect(data.created).toBe(true);
const content = await readFile(outPath, 'utf-8');
expect(content).toContain('type: execute');
expect(content).toContain('wave: 1');
expect(content).toContain('autonomous: true');
expect(content).toContain('<objective>');
expect(content).toContain('<tasks>');
});
it('creates verification file with verification skeleton', async () => {
const outPath = join(tmpDir, 'test-verification.md');
const result = await templateFill(['verification', outPath], tmpDir);
const data = result.data as Record<string, unknown>;
expect(data.created).toBe(true);
const content = await readFile(outPath, 'utf-8');
expect(content).toContain('status: pending');
expect(content).toContain('## Must-Have Checks');
expect(content).toContain('## Result');
});
it('applies key=value overrides to frontmatter', async () => {
const outPath = join(tmpDir, 'test-override.md');
const result = await templateFill(['summary', outPath, 'phase=11-testing', 'plan=02'], tmpDir);
const data = result.data as Record<string, unknown>;
expect(data.created).toBe(true);
const content = await readFile(outPath, 'utf-8');
expect(content).toContain('phase: 11-testing');
expect(content).toContain('plan: 02');
});
it('rejects path traversal attempts with .. segments', async () => {
const outPath = join(tmpDir, '..', 'escape.md');
await expect(templateFill(['summary', outPath], tmpDir)).rejects.toThrow();
});
});
describe('event emission wiring', () => {
it('emits StateMutation event for state.update dispatch', async () => {
// Create a proper STATE.md for state.update to work with
const stateContent = [
'---',
'status: executing',
'---',
'',
'# Project State',
'',
'## Current Position',
'',
'Status: Ready',
].join('\n');
await writeFile(join(tmpDir, '.planning', 'STATE.md'), stateContent);
const eventStream = new GSDEventStream();
const events: GSDEvent[] = [];
eventStream.on('event', (e: GSDEvent) => events.push(e));
const registry = createRegistry(eventStream, 'corr-xyz');
await registry.dispatch('state.update', ['status', 'Executing'], tmpDir);
const mutationEvents = events.filter(e => e.type === GSDEventType.StateMutation);
expect(mutationEvents.length).toBe(1);
const evt = mutationEvents[0] as { type: string; command: string; success: boolean; sessionId?: string };
expect(evt.command).toBe('state.update');
expect(evt.success).toBe(true);
expect(evt.sessionId).toBe('corr-xyz');
});
it('emits ConfigMutation event for config-set dispatch', async () => {
await writeFile(join(tmpDir, '.planning', 'config.json'), '{"model_profile":"balanced"}');
const eventStream = new GSDEventStream();
const events: GSDEvent[] = [];
eventStream.on('event', (e: GSDEvent) => events.push(e));
const registry = createRegistry(eventStream);
await registry.dispatch('config-set', ['model_profile', 'quality'], tmpDir);
const mutationEvents = events.filter(e => e.type === GSDEventType.ConfigMutation);
expect(mutationEvents.length).toBe(1);
const evt = mutationEvents[0] as { type: string; command: string; success: boolean };
expect(evt.command).toBe('config-set');
expect(evt.success).toBe(true);
});
it('emits TemplateFill event for template.fill dispatch', async () => {
const outPath = join(tmpDir, 'event-test.md');
const eventStream = new GSDEventStream();
const events: GSDEvent[] = [];
eventStream.on('event', (e: GSDEvent) => events.push(e));
const registry = createRegistry(eventStream);
await registry.dispatch('template.fill', ['summary', outPath], tmpDir);
const templateEvents = events.filter(e => e.type === GSDEventType.TemplateFill);
expect(templateEvents.length).toBe(1);
});
});