voyage/tests/lib/plan-schema.test.mjs
Kjell Tore Guttormsen 7cfce2b996 fix(voyage): S26 — parse plan_version prose form (S22 defect #2/#3)
The plan template (templates/plan-template.md:21) emits plan_version ONLY in
the prose blockquote metadata line:

  > Generated by trekplan v{version} on {YYYY-MM-DD} — `plan_version: 1.7`

It carries no frontmatter plan_version (the frontmatter block is trekreview-only
and holds source_findings). But PLAN_VERSION_REGEX in lib/parsers/plan-schema.mjs
was `^`-anchored (/m), matching ONLY line-start (frontmatter). The backtick-
wrapped prose form never matched → extractPlanVersion returned null → every real
generated plan got a spurious PLAN_NO_VERSION warning from plan-validator.

Not caught earlier because no test ran the parser against the actual template;
the synthetic fixtures all carry frontmatter plan_version and parse fine.

Internal source contradiction surfaced: the parser comment documents intent
"frontmatter OR doc body", planning-orchestrator.md:228 + the template place it
in the prose line, while HANDOVER-CONTRACTS.md calls it a frontmatter field.

Operator-chosen fix (parser, not template): relax the regex to
`/(?:^|`)plan_version:.../m` so it matches line-start (frontmatter) OR the
backtick-prefixed prose form. Honors the parser's own documented contract,
fixes all existing + future plans, changes no plan output, touches one code
file — vs the template-fix which would alter every plan's shape, contradict
planning-orchestrator.md, and force a 3-file doc reconciliation (scope creep).

TDD (red first): added prose-form + canonical-template regression pins in
plan-schema.test.mjs and a no-PLAN_NO_VERSION pin in plan-validator.test.mjs;
all 3 red on the `^`-anchored regex, green after the relax. The template pin
ties the parser directly to the real generated artifact.

S22 defect closed in docs/S22-happy-path-dogfood.md (§Pipeline defects + log).

Tests: 728 (726 pass / 2 skip / 0 fail; live `node --test`, = 725 baseline +3).
plugin validate passes (1 accepted CLAUDE.md-at-root warning).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LqBYc8Ltrk7LipyJmGxXiB
2026-06-19 21:59:31 +02:00

154 lines
4.7 KiB
JavaScript

import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { readFileSync } from 'node:fs';
import {
findSteps,
findForbiddenHeadings,
sliceSteps,
validatePlanHeadings,
extractPlanVersion,
} from '../../lib/parsers/plan-schema.mjs';
const GOOD_PLAN = `---
plan_version: "1.7"
---
## Implementation Plan
### Step 1: First step
- Files: a.ts
### Step 2: Second step
- Files: b.ts
### Step 3: Third step
- Files: c.ts
`;
const FORBIDDEN_FASE = `## Implementation Plan
## Fase 1: Forberedelse
content here
## Fase 2: Implementering
more content
`;
const FORBIDDEN_PHASE = `### Phase 1: Setup
content
`;
const FORBIDDEN_STAGE = `### Stage 1: Initial work
content
`;
const FORBIDDEN_STEG = `### Steg 1: Norsk drift
content
`;
test('findSteps — locates all canonical step headings', () => {
const steps = findSteps(GOOD_PLAN);
assert.equal(steps.length, 3);
assert.equal(steps[0].n, 1);
assert.equal(steps[0].title, 'First step');
assert.equal(steps[2].n, 3);
assert.equal(steps[2].title, 'Third step');
});
test('findSteps — empty for plan without steps', () => {
assert.deepEqual(findSteps('## Implementation Plan\n\nno steps yet'), []);
});
test('findForbiddenHeadings — Fase (Norwegian)', () => {
const f = findForbiddenHeadings(FORBIDDEN_FASE);
assert.equal(f.length, 2);
assert.match(f[0].raw, /Fase 1/);
});
test('findForbiddenHeadings — Phase (English)', () => {
const f = findForbiddenHeadings(FORBIDDEN_PHASE);
assert.equal(f.length, 1);
});
test('findForbiddenHeadings — Stage', () => {
assert.equal(findForbiddenHeadings(FORBIDDEN_STAGE).length, 1);
});
test('findForbiddenHeadings — Steg (Norwegian variant)', () => {
assert.equal(findForbiddenHeadings(FORBIDDEN_STEG).length, 1);
});
test('findForbiddenHeadings — clean plan has zero', () => {
assert.equal(findForbiddenHeadings(GOOD_PLAN).length, 0);
});
test('sliceSteps — body bounded by next step', () => {
const sections = sliceSteps(GOOD_PLAN);
assert.equal(sections.length, 3);
assert.match(sections[0].body, /First step/);
assert.match(sections[0].body, /Files: a\.ts/);
assert.ok(!sections[0].body.includes('Second step'));
});
test('validatePlanHeadings — strict accepts good plan', () => {
const r = validatePlanHeadings(GOOD_PLAN, { strict: true });
assert.equal(r.valid, true);
assert.equal(r.parsed.steps.length, 3);
});
test('validatePlanHeadings — strict rejects forbidden Fase form', () => {
const r = validatePlanHeadings(FORBIDDEN_FASE, { strict: true });
assert.equal(r.valid, false);
assert.ok(r.errors.find(e => e.code === 'PLAN_FORBIDDEN_HEADING'));
});
test('validatePlanHeadings — soft mode demotes forbidden to warning', () => {
const r = validatePlanHeadings(`### Step 1: ok\n\n### Phase 2: drift\n`, { strict: false });
assert.equal(r.errors.find(e => e.code === 'PLAN_FORBIDDEN_HEADING'), undefined);
assert.ok(r.warnings.find(w => w.code === 'PLAN_FORBIDDEN_HEADING'));
});
test('validatePlanHeadings — non-contiguous numbering is an error', () => {
const broken = '### Step 1: ok\ncontent\n\n### Step 3: skip\ncontent\n';
const r = validatePlanHeadings(broken, { strict: true });
assert.equal(r.valid, false);
assert.ok(r.errors.find(e => e.code === 'PLAN_STEP_NUMBERING'));
});
test('validatePlanHeadings — empty plan errors with PLAN_NO_STEPS', () => {
const r = validatePlanHeadings('## Implementation Plan\n\nno steps\n');
assert.ok(r.errors.find(e => e.code === 'PLAN_NO_STEPS'));
});
test('extractPlanVersion — from frontmatter', () => {
assert.equal(extractPlanVersion('plan_version: "1.7"\nfoo: bar\n'), '1.7');
assert.equal(extractPlanVersion('plan_version: 1.8\n'), '1.8');
});
test('extractPlanVersion — null when absent', () => {
assert.equal(extractPlanVersion('foo: bar\n'), null);
});
test('extractPlanVersion — from prose "Generated by" metadata line (defect S26)', () => {
// The plan template emits plan_version only in the prose blockquote line
// (`> Generated by ... — `plan_version: 1.7``), not as frontmatter.
// extractPlanVersion must honor its documented "frontmatter or doc body"
// contract and parse the backtick-wrapped prose form.
const prose = '> Generated by trekplan v4.1.0 on 2026-05-10 — `plan_version: 1.7`\n';
assert.equal(extractPlanVersion(prose), '1.7');
});
test('extractPlanVersion — recognizes the canonical plan template (regression pin S26)', () => {
// The real generated artifact: no test exercised the parser against the
// actual template, which is why defect S26 went unnoticed.
const tpl = readFileSync(new URL('../../templates/plan-template.md', import.meta.url), 'utf-8');
assert.equal(extractPlanVersion(tpl), '1.7');
});