ktg-plugin-marketplace/plugins/voyage/tests/lib/doc-consistency.test.mjs

627 lines
24 KiB
JavaScript

// tests/lib/doc-consistency.test.mjs
// Pin invariants between prose (CLAUDE.md, README.md) and source files
// (agents/*.md, commands/*.md, templates/, settings.json).
//
// When this test fails, fix the source-of-truth — do NOT rewrite the test to
// hide drift. Borrowed pattern from llm-security commit 97c5c9d.
import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { readFileSync, readdirSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseDocument } from '../../lib/util/frontmatter.mjs';
const HERE = dirname(fileURLToPath(import.meta.url));
const ROOT = join(HERE, '..', '..');
function read(rel) { return readFileSync(join(ROOT, rel), 'utf-8'); }
function listMd(rel) { return readdirSync(join(ROOT, rel)).filter(f => f.endsWith('.md')); }
test('CLAUDE.md agents table row count == agents/*.md file count', () => {
const md = read('CLAUDE.md');
const agentFiles = listMd('agents');
const agentTable = md.split('## Agents')[1] || '';
const tableSection = agentTable.split('\n## ')[0];
const dataRows = tableSection
.split('\n')
.filter(l => l.startsWith('|') && !l.match(/^\|[\s-]+\|/) && !l.match(/^\|\s*Agent\s*\|/));
assert.equal(
dataRows.length,
agentFiles.length,
`Drift: ${agentFiles.length} agent files vs ${dataRows.length} CLAUDE.md table rows. ` +
`Sync agents/ ↔ CLAUDE.md.`,
);
});
test('CLAUDE.md commands table mentions every commands/*.md file', () => {
const md = read('CLAUDE.md');
const commandFiles = listMd('commands');
for (const f of commandFiles) {
const cmdName = `/${f.replace(/\.md$/, '')}`;
assert.ok(
md.includes(cmdName),
`commands/${f} not mentioned in CLAUDE.md (looked for ${cmdName})`,
);
}
});
test('every command frontmatter name matches its filename', () => {
for (const f of listMd('commands')) {
const text = read(`commands/${f}`);
const doc = parseDocument(text);
if (!doc.valid) continue;
const expected = f.replace(/\.md$/, '');
if (doc.parsed.frontmatter && doc.parsed.frontmatter.name !== undefined) {
assert.equal(
doc.parsed.frontmatter.name,
expected,
`commands/${f} frontmatter.name="${doc.parsed.frontmatter.name}" should be "${expected}"`,
);
}
}
});
test('templates/plan-template.md declares plan_version: 1.7', () => {
const tpl = read('templates/plan-template.md');
assert.match(tpl, /plan_version:\s*['"]?1\.7['"]?/);
});
test('commands/trekexecute.md still parses v1.7 plan schema', () => {
const cmd = read('commands/trekexecute.md');
const tpl = read('templates/plan-template.md');
const tplVersion = (tpl.match(/plan_version:\s*['"]?([\d.]+)['"]?/) || [])[1];
assert.ok(tplVersion, 'templates/plan-template.md missing plan_version');
assert.ok(
cmd.includes(`plan_version`) || cmd.includes(`Step N:`) || cmd.includes('### Step '),
'commands/trekexecute.md should reference v1.7 plan-schema parsing',
);
});
test('settings.json has only known top-level scopes after Spor 0 cleanup', () => {
const cfg = JSON.parse(read('settings.json'));
const known = ['trekplan', 'trekresearch', 'trekrevise'];
for (const k of Object.keys(cfg)) {
assert.ok(known.includes(k), `Unknown top-level scope in settings.json: ${k}`);
}
});
test('settings.json no longer carries vestigial exploration block', () => {
const cfg = JSON.parse(read('settings.json'));
assert.equal(cfg.trekplan?.exploration, undefined,
'exploration block was vestigial — should be deleted in v3.1.0 Spor 0');
assert.equal(cfg.trekplan?.agentTeam, undefined,
'agentTeam block was vestigial — should be deleted in v3.1.0 Spor 0');
});
test('CLAUDE.md mentions all seven pipeline commands', () => {
// v4.1 Step 21 — added /trekcontinue to coverage (was 5/6 before).
// v4.2 Step 12 — added /trekrevise (Handover 8 producer), bringing the
// canonical pipeline to seven commands.
const md = read('CLAUDE.md');
for (const c of [
'/trekbrief',
'/trekresearch',
'/trekplan',
'/trekexecute',
'/trekreview',
'/trekrevise',
'/trekcontinue',
]) {
assert.ok(md.includes(c), `CLAUDE.md missing reference to ${c}`);
}
});
test('HANDOVER-CONTRACTS.md contains Handover 6 section', () => {
const text = read('docs/HANDOVER-CONTRACTS.md');
assert.ok(
text.includes('## Handover 6'),
'docs/HANDOVER-CONTRACTS.md should document Handover 6 (review → plan)',
);
});
test('HANDOVER-CONTRACTS.md contains Handover 7 section (session-state)', () => {
const text = read('docs/HANDOVER-CONTRACTS.md');
assert.ok(
text.includes('## Handover 7'),
'docs/HANDOVER-CONTRACTS.md should document Handover 7 (.session-state.local.json) ' +
'consumed by /trekcontinue',
);
assert.ok(
text.includes('.session-state.local.json'),
'Handover 7 section should name the artifact path',
);
});
test('review-validator has CLI shim', () => {
const text = read('lib/validators/review-validator.mjs');
assert.ok(
text.includes('import.meta.url === '),
'lib/validators/review-validator.mjs should expose the standard CLI shim ' +
'(if (import.meta.url === `file://${process.argv[1]}`)) so commands can call it from Bash',
);
});
test('session-state-validator has CLI shim', () => {
const text = read('lib/validators/session-state-validator.mjs');
assert.ok(
text.includes('import.meta.url === '),
'lib/validators/session-state-validator.mjs should expose the standard CLI shim ' +
'(if (import.meta.url === `file://${process.argv[1]}`)) so /trekcontinue can call it from Bash',
);
});
test('next-session-prompt-validator has CLI shim', () => {
const text = read('lib/validators/next-session-prompt-validator.mjs');
assert.ok(
text.includes('import.meta.url === '),
'lib/validators/next-session-prompt-validator.mjs should expose the standard CLI shim ' +
'(if (import.meta.url === `file://${process.argv[1]}`)) so /trekcontinue Phase 1.5 can call it from Bash',
);
});
test('HANDOVER-CONTRACTS.md Handover 7 documents § Lifecycle subsection', () => {
const text = read('docs/HANDOVER-CONTRACTS.md');
const h7Start = text.indexOf('## Handover 7');
assert.ok(h7Start >= 0, 'Handover 7 heading missing');
const h7End = text.indexOf('## Stability summary', h7Start);
assert.ok(h7End > h7Start, 'Stability summary heading missing — could not bound Handover 7');
const h7 = text.slice(h7Start, h7End);
assert.ok(
h7.includes('Lifecycle'),
'Handover 7 section should include a § Lifecycle subsection (SC-5 stale-file principle)',
);
});
test('HANDOVER-CONTRACTS.md Handover 7 § Lifecycle names --cleanup and produced_by contract', () => {
const text = read('docs/HANDOVER-CONTRACTS.md');
const h7Start = text.indexOf('## Handover 7');
const h7End = text.indexOf('## Stability summary', h7Start);
const h7 = text.slice(h7Start, h7End);
assert.ok(
h7.includes('--cleanup'),
'Handover 7 § Lifecycle should mention --cleanup as the operator-invoked stale-file remover',
);
assert.ok(
h7.includes('produced_by'),
'Handover 7 § Lifecycle should document the produced_by frontmatter contract for NEXT-SESSION-PROMPT.local.md',
);
});
test('CLAUDE.md mentions /trekcontinue command', () => {
const md = read('CLAUDE.md');
assert.ok(
md.includes('/trekcontinue') || md.includes('trekcontinue'),
'CLAUDE.md should document /trekcontinue in the Commands table ' +
'(added in v3.3.0 alongside the new command file)',
);
});
test('rule-catalogue has exactly 12 entries', async () => {
const mod = await import('../../lib/review/rule-catalogue.mjs');
assert.strictEqual(
mod.RULE_CATALOGUE.length,
12,
'lib/review/rule-catalogue.mjs RULE_CATALOGUE size invariant: must be 12 (v1.0 baseline)',
);
});
test('headless-launch-template.md mirrors Phase 2.6 hardenings', () => {
const tpl = read('templates/headless-launch-template.md');
for (const needle of [
'GIT_OPTIONAL_LOCKS',
'--max-turns',
'--max-budget-usd',
'--append-system-prompt-file',
'SHARED_CONTEXT_FILE',
'SAFETY_PREAMBLE',
'git push origin',
'GH #36071',
'push-before-cleanup',
]) {
assert.ok(
tpl.includes(needle),
`templates/headless-launch-template.md should include "${needle}" (Step 10 mirrors Phase 2.6)`,
);
}
});
test('Phase 9 prose mandates parallel single-message dispatch + inline dedup', () => {
const cmd = read('commands/trekplan.md');
const orch = read('agents/planning-orchestrator.md');
// Single-message reinforcement appears in both (command + orchestrator)
assert.ok(
cmd.includes('single assistant message turn'),
'commands/trekplan.md Phase 9 should reinforce single-message parallel dispatch',
);
assert.ok(
orch.includes('single assistant message turn'),
'agents/planning-orchestrator.md Phase 6 should mirror the single-message parallel-dispatch contract',
);
// Dedup CLI shim is wired in both
assert.ok(
cmd.includes('plan-review-dedup.mjs'),
'commands/trekplan.md Phase 9 should call lib/review/plan-review-dedup.mjs after both reviewers complete',
);
assert.ok(
orch.includes('plan-review-dedup.mjs'),
'agents/planning-orchestrator.md Phase 6 should reference the dedup helper',
);
});
// --- v4.1 Step 21 — pin --profile + phase_models on the 6 commands ---
//
// CLAUDE.md / README.md pinning is deferred to Step 22 (post-write of
// those documents). Step 21 only verifies command-file content, which
// was written in Step 7 (Wave 3).
const PIPELINE_COMMANDS = [
'trekbrief.md',
'trekresearch.md',
'trekplan.md',
'trekexecute.md',
'trekreview.md',
'trekrevise.md',
'trekcontinue.md',
];
test('every pipeline command-file documents the --profile flag (SC #20)', () => {
for (const f of PIPELINE_COMMANDS) {
const text = read(`commands/${f}`);
assert.match(
text,
/--profile\b/,
`commands/${f}: --profile flag is required documentation in v4.1`,
);
}
});
test('command-files mentioning model profiles use canonical name `phase_models`', () => {
// Reject legacy / brainstormed alternatives that would confuse readers.
const FORBIDDEN = ['model_per_phase', 'phase_to_model', 'profile_phase_models'];
for (const f of PIPELINE_COMMANDS) {
const text = read(`commands/${f}`);
for (const bad of FORBIDDEN) {
assert.ok(
!text.includes(bad),
`commands/${f}: forbidden alias "${bad}" — canonical name is "phase_models"`,
);
}
}
});
test('at least one pipeline command-file references `phase_models` canonical name', () => {
// Sanity: not every command has to enumerate phase_models inline (e.g.
// trekbrief and trekcontinue may only mention --profile), but ≥ 1
// command-file must spell out the canonical name so the regression test
// pins drift.
let mentioned = 0;
for (const f of PIPELINE_COMMANDS) {
if (read(`commands/${f}`).includes('phase_models')) mentioned += 1;
}
assert.ok(
mentioned >= 1,
`expected ≥ 1 command-file to mention canonical name "phase_models", got ${mentioned}`,
);
});
// --- v4.1 Step 22 — post-write CLAUDE.md / README.md pinning ---
//
// Plan-critic Blocker 2 fix: Step 21 only pinned commands/*.md (which
// are written in Step 7 / Wave 3). Step 22 writes the top-level docs
// and extends pinning here so doc-consistency stays green AFTER Step 22.
test('CLAUDE.md documents --profile flag', () => {
const md = read('CLAUDE.md');
assert.match(
md,
/--profile\b/,
'CLAUDE.md must document the --profile flag (v4.1 SC #20)',
);
});
test('CLAUDE.md uses canonical name `phase_models`', () => {
const md = read('CLAUDE.md');
assert.match(
md,
/phase_models/,
'CLAUDE.md must use canonical name "phase_models" (v4.1 SC #20)',
);
for (const bad of ['model_per_phase', 'phase_to_model', 'profile_phase_models']) {
assert.ok(
!md.includes(bad),
`CLAUDE.md must NOT use legacy alias "${bad}"`,
);
}
});
test('README.md documents --profile flag for all 6 commands', () => {
// SG1: README flag-table coverage is gating for SC #20. README is the
// primary discovery surface for new users.
const md = read('README.md');
// Top-level Profile system section is required so the flag is
// discoverable independent of per-command tables.
assert.match(md, /## Profile system/, 'README.md missing top-level "## Profile system" section');
// Every per-command Modes table must include --profile (count of
// --profile occurrences should be ≥ 6 — one per command + Profile
// system section).
const profileMentions = (md.match(/--profile\b/g) || []).length;
assert.ok(
profileMentions >= 6,
`README.md must mention --profile ≥ 6 times (one per command + section), got ${profileMentions}`,
);
});
test('CHANGELOG.md has v4.1.0 entry', () => {
const cl = read('CHANGELOG.md');
assert.match(
cl,
/## v4\.1\.0\b/,
'CHANGELOG.md must include "## v4.1.0" entry per Keep-a-Changelog 1.1.0',
);
});
test('docs/profiles.md exists and documents Custom.yaml authoring', () => {
const dp = read('docs/profiles.md');
assert.ok(dp.length > 1000, 'docs/profiles.md must be substantive (> 1000 chars)');
// Must document custom-profile authoring (Step 22 manifest must_contain
// pattern: "Custom.yaml" — case-insensitive match handled here as
// /[Cc]ustom[. ]/ to allow either "custom.yaml" or "Custom profile" prose).
assert.match(
dp,
/[Cc]ustom\.yaml|[Cc]ustom profile|<custom>\.yaml/,
'docs/profiles.md must document custom profile authoring',
);
});
test('commands/trekplan.md Phase 8 seals Opus-4.7 schema-drift defense', () => {
const cmd = read('commands/trekplan.md');
// Locate Phase 8 section
const phase8Start = cmd.indexOf('## Phase 8');
assert.ok(phase8Start >= 0, 'Phase 8 heading missing');
const phase8End = cmd.indexOf('## Phase 9', phase8Start);
assert.ok(phase8End > phase8Start, 'Phase 9 heading missing — could not bound Phase 8');
const phase8 = cmd.slice(phase8Start, phase8End);
// Required regex source-of-truth references
assert.ok(
phase8.includes('STEP_HEADING_REGEX'),
'Phase 8 should inline STEP_HEADING_REGEX so format contract survives without orchestrator-doc loading',
);
assert.ok(
phase8.includes('FORBIDDEN_HEADING_REGEX'),
'Phase 8 should inline FORBIDDEN_HEADING_REGEX (Step 7 — schema-drift seal)',
);
// Required validator self-check
assert.ok(
phase8.includes('plan-validator.mjs --strict'),
'Phase 8 should mandate post-write `plan-validator.mjs --strict` self-check',
);
// Forbidden-headings list (literal "FORBIDDEN" appears more than once: in regex const + in human-readable list)
assert.ok(
/FORBIDDEN/.test(phase8),
'Phase 8 should explicitly enumerate FORBIDDEN headings',
);
});
// --- v4.2 Step 12 — Handover 8 + annotation pipeline pins ---
//
// CLAUDE.md / README.md / CHANGELOG / annotation-quickstart pins are deferred
// to Step 13 (post-write of those files). Step 12 only pins HANDOVER-CONTRACTS,
// templates, scaffold-files, and the parseAnchors round-trip on the example
// fixture.
import { existsSync, statSync } from 'node:fs';
test('HANDOVER-CONTRACTS.md contains Handover 8 section (annotation → revision)', () => {
const text = read('docs/HANDOVER-CONTRACTS.md');
assert.ok(
text.includes('## Handover 8'),
'docs/HANDOVER-CONTRACTS.md should document Handover 8 (annotation → revision) — added in v4.2',
);
});
test('HANDOVER-CONTRACTS.md Handover 8 names annotation_digest and source_annotations', () => {
const text = read('docs/HANDOVER-CONTRACTS.md');
const h8Start = text.indexOf('## Handover 8');
assert.ok(h8Start >= 0, 'Handover 8 heading missing');
const h8End = text.indexOf('## Stability summary', h8Start);
assert.ok(h8End > h8Start, 'Stability summary heading missing — could not bound Handover 8');
const h8 = text.slice(h8Start, h8End);
assert.ok(
h8.includes('annotation_digest'),
'Handover 8 section should document the annotation_digest frontmatter field',
);
assert.ok(
h8.includes('source_annotations'),
'Handover 8 section should document the source_annotations frontmatter field',
);
assert.ok(
h8.includes('revision'),
'Handover 8 section should document the revision counter field',
);
});
test('templates/plan-template.md documents annotation revision fields', () => {
const tpl = read('templates/plan-template.md');
assert.ok(
tpl.includes('revision:'),
'plan-template.md must document optional revision counter (Handover 8)',
);
assert.ok(
tpl.includes('source_annotations:'),
'plan-template.md must document optional source_annotations list (Handover 8)',
);
assert.ok(
tpl.includes('annotation_digest'),
'plan-template.md must document optional annotation_digest field (Handover 8)',
);
});
test('templates/trekbrief-template.md documents annotation revision fields', () => {
const tpl = read('templates/trekbrief-template.md');
assert.ok(
tpl.includes('revision:'),
'trekbrief-template.md must document optional revision counter (Handover 8)',
);
assert.ok(
tpl.includes('source_annotations:'),
'trekbrief-template.md must document optional source_annotations list (Handover 8)',
);
assert.ok(
tpl.includes('annotation_digest'),
'trekbrief-template.md must document optional annotation_digest field (Handover 8)',
);
});
test('templates/trekreview-template.md documents annotation revision fields', () => {
const tpl = read('templates/trekreview-template.md');
assert.ok(
tpl.includes('revision:'),
'trekreview-template.md must document optional revision counter (Handover 8)',
);
assert.ok(
tpl.includes('source_annotations:'),
'trekreview-template.md must document optional source_annotations list (Handover 8)',
);
assert.ok(
tpl.includes('annotation_digest'),
'trekreview-template.md must document optional annotation_digest field (Handover 8)',
);
});
test('playground/ directory exists at voyage root (Handover 8 producer surface)', () => {
const playgroundDir = join(ROOT, 'playground');
assert.ok(existsSync(playgroundDir), 'playground/ directory missing');
assert.ok(statSync(playgroundDir).isDirectory(), 'playground/ is not a directory');
// Self-contained HTML must exist
assert.ok(
existsSync(join(playgroundDir, 'voyage-playground.html')),
'playground/voyage-playground.html missing — operator-facing entry point',
);
});
test('playground/ files do NOT import or reference `marked` (risk-assessor H1)', () => {
// Walk playground/ recursively. Exclude vendor/playground-design-system
// (consumed via the shared design system; not part of voyage's playground
// markdown renderer). Exclude any *MANIFEST.json files. Assert no file
// contains the standalone identifier `marked` (case-sensitive, word-boundary).
// markdown-it is the locked renderer per research-03 + alternatives table.
const playgroundDir = join(ROOT, 'playground');
assert.ok(existsSync(playgroundDir), 'playground/ directory missing — cannot verify marked-ban');
const offenders = [];
function walk(dir) {
for (const entry of readdirSync(dir)) {
const p = join(dir, entry);
const s = statSync(p);
if (s.isDirectory()) {
// Skip vendor design-system trees (shared infra, not voyage's renderer)
if (entry === 'playground-design-system') continue;
walk(p);
} else if (s.isFile()) {
// Skip vendor manifest JSONs
if (entry.endsWith('MANIFEST.json')) continue;
if (entry === 'VENDOR-MANIFEST.json') continue;
const txt = readFileSync(p, 'utf-8');
if (/\bmarked\b/.test(txt)) {
offenders.push(p.slice(ROOT.length + 1));
}
}
}
}
walk(playgroundDir);
assert.deepStrictEqual(
offenders,
[],
`playground/ files contain banned identifier "marked": ${offenders.join(', ')}. ` +
`Use markdown-it instead — see plan Alternatives table (Issue #3515 disqualifies marked).`,
);
});
test('scripts/render-artifact.mjs exists (SC1/SC11 self-render gate)', () => {
assert.ok(
existsSync(join(ROOT, 'scripts/render-artifact.mjs')),
'scripts/render-artifact.mjs missing — required by SC1 (offline render) and SC11 (pipeline-self-eat)',
);
});
test('lib/util/revision-guard.mjs exists (plan-critic M4 — atomic-write rollback guard)', () => {
assert.ok(
existsSync(join(ROOT, 'lib/util/revision-guard.mjs')),
'lib/util/revision-guard.mjs missing — required for /trekrevise rollback hygiene',
);
});
test('tests/fixtures/annotation/annotation-example.md parses cleanly via parseAnchors (ESM)', async () => {
// Plan-critic m4 — fix the SC12 require/import mixup. Use ESM dynamic import,
// not require(). The parser is pure — no I/O, no side effects.
const { parseAnchors } = await import('../../lib/parsers/anchor-parser.mjs');
const fixturePath = join(ROOT, 'tests/fixtures/annotation/annotation-example.md');
assert.ok(existsSync(fixturePath), 'tests/fixtures/annotation/annotation-example.md missing');
const result = parseAnchors(readFileSync(fixturePath, 'utf-8'));
assert.ok(
result.valid,
`parseAnchors failed on annotation-example.md fixture: ${JSON.stringify(result.errors || [])}`,
);
});
// --- v4.2 Step 13 — late doc-consistency pins (post-write of CLAUDE / READMEs / CHANGELOG / quickstart) ---
//
// These were deferred from Step 12 per plan-critic M1 ordering finding —
// Step 13 is where these files are written, so pins go here.
test('plugin README.md mentions /trekrevise in commands section', () => {
// Already covered for CLAUDE.md by the "all seven pipeline commands" test;
// this pin extends coverage to the plugin-level README.
const md = read('README.md');
assert.ok(
md.includes('/trekrevise'),
'plugin README.md must reference /trekrevise (added in v4.2 Step 13)',
);
});
test('marketplace root README.md mentions /trekrevise and v4.2.0', () => {
// ../../README.md is the marketplace landing — must surface v4.2 ship.
// Path traversal is allowed here per feedback_plugin_scope_strict
// (root README updates are explicitly in Step 13's scope).
const md = read('../../README.md');
assert.ok(
md.includes('/trekrevise') || md.includes('trekrevise'),
'marketplace root README.md must reference /trekrevise (v4.2)',
);
assert.ok(
md.includes('v4.2.0'),
'marketplace root README.md must reference voyage v4.2.0',
);
});
test('CHANGELOG.md has v4.2.0 entry', () => {
const cl = read('CHANGELOG.md');
assert.match(
cl,
/## v4\.2\.0\b/,
'CHANGELOG.md must include "## v4.2.0" entry per Keep-a-Changelog 1.1.0',
);
});
test('docs/annotation-quickstart.md exists with ≤7 numbered steps and example-fixture reference', () => {
// SC12 — operator-facing quickstart. The plan caps numbered steps at 7
// to keep cognitive load minimal; reference to the example fixture
// anchors the doc to a concrete artifact operators can replay.
const path = 'docs/annotation-quickstart.md';
assert.ok(existsSync(join(ROOT, path)), `${path} missing`);
const text = read(path);
// Numbered top-level steps: lines starting with "1." through "7." at
// line-start. Forbid 8.+ line-starts.
const numberedSteps = (text.match(/^[1-9]\./gm) || []);
for (const s of numberedSteps) {
const n = parseInt(s, 10);
assert.ok(
n >= 1 && n <= 7,
`${path} contains step ${s} — only 1.-7. permitted (single-screen quickstart)`,
);
}
assert.ok(
text.includes('tests/fixtures/annotation/annotation-example.md'),
`${path} must reference the canonical example fixture for hands-on verification`,
);
});