ktg-plugin-marketplace/plugins/ultraplan-local/tests/lib/source-findings.test.mjs

63 lines
2.7 KiB
JavaScript

// tests/lib/source-findings.test.mjs
// SC3(b) structural test for Handover 6.
//
// The brief requires `plan.md` produced from a `type: ultrareview` brief to
// contain `source_findings: [<id>, ...]` in its frontmatter. Without an
// automated test, SC3(b) is unverified.
//
// This test exercises the STRUCTURAL contract:
// 1. plan-validator accepts a plan with source_findings (additive optional field)
// 2. frontmatter parser extracts source_findings as an array of strings
// 3. each ID is 40-char hex (matches lib/parsers/finding-id.mjs format)
//
// LLM behavior (the planner actually emitting source_findings when it consumes
// a review.md) is non-testable without live invocation — this test only covers
// the schema half.
import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseDocument } from '../../lib/util/frontmatter.mjs';
import { validatePlan } from '../../lib/validators/plan-validator.mjs';
const HERE = dirname(fileURLToPath(import.meta.url));
const ROOT = join(HERE, '..', '..');
const FIXTURE = join(ROOT, 'tests/fixtures/trekreview/plan-with-source-findings.md');
const HEX_ID_RE = /^[0-9a-f]{40}$/;
test('plan-validator accepts plan.md with source_findings field', () => {
const result = validatePlan(FIXTURE, { strict: true });
assert.ok(
result.valid,
`plan-validator rejected synthetic plan with source_findings: ` +
`${(result.errors || []).map(e => `[${e.code}] ${e.message}`).join('; ')}`,
);
});
test('frontmatter parser extracts source_findings as array of strings', () => {
const text = readFileSync(FIXTURE, 'utf-8');
const doc = parseDocument(text);
assert.ok(doc.valid, `frontmatter did not parse: ${(doc.errors || []).map(e => e.message).join(', ')}`);
const sf = doc.parsed.frontmatter && doc.parsed.frontmatter.source_findings;
assert.ok(Array.isArray(sf), `frontmatter.source_findings is not an array (got ${typeof sf})`);
assert.ok(sf.length > 0, 'frontmatter.source_findings is empty — fixture should carry at least one ID');
for (const id of sf) {
assert.strictEqual(typeof id, 'string', `source_findings entry is not a string: ${JSON.stringify(id)}`);
}
});
test('source_findings IDs match the format from finding-id.mjs (40-char hex)', () => {
const text = readFileSync(FIXTURE, 'utf-8');
const doc = parseDocument(text);
const sf = doc.parsed.frontmatter.source_findings;
for (const id of sf) {
assert.ok(
HEX_ID_RE.test(id),
`source_findings ID ${JSON.stringify(id)} is not 40-char lowercase hex ` +
`(format produced by lib/parsers/finding-id.mjs computeFindingId)`,
);
}
});