repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/leaks/__tests__/ledger.test.ts

Raw
File outline
import { readFileSync } from "node:fs";
import path from "node:path";
import { describe, expect, it } from "vitest";

const specPath = path.resolve(import.meta.dirname, "../SPEC.md");
const spec = readFileSync(specPath, "utf8");

const expectedHeadings = [
	"Authority",
	"Mission",
	"Core requirements",
	"Core model",
	"Pi integration strategy",
	"Settings",
	"Built-in detection",
	"Dotenv and environment values",
	"False positives",
	"UX and commands",
	"Validation ledger",
	"Non-goals",
];

const batches = [
	"V0 scope",
	"V1 provider boundary",
	"V2 placeholders",
	"V3 settings",
	"V4 built-ins",
	"V5 learned values",
	"V6 component parsers",
	"V7 false positives",
	"V8 UX",
	"V9 performance/failure",
];

const sectionBatchMap = new Map<string, string[]>([
	["Authority", ["V0 scope"]],
	["Mission", ["V0 scope", "V1 provider boundary"]],
	[
		"Core requirements",
		["V1 provider boundary", "V8 UX", "V9 performance/failure"],
	],
	["Core model", ["V2 placeholders"]],
	["Pi integration strategy", ["V1 provider boundary"]],
	["Settings", ["V3 settings", "V9 performance/failure"]],
	["Built-in detection", ["V4 built-ins"]],
	[
		"Dotenv and environment values",
		["V5 learned values", "V6 component parsers", "V7 false positives"],
	],
	["False positives", ["V6 component parsers", "V7 false positives"]],
	["UX and commands", ["V8 UX"]],
	["Validation ledger", ["V0 scope"]],
	["Non-goals", ["V0 scope"]],
]);

const normativeMarker =
	/\b(must|must not|should|do not|never|only|requires?|required|accepted|planned|rejected|out of scope|non-goals?|always|at worst|when|on)\b/i;

function sections() {
	const result: Array<{ name: string; body: string }> = [];
	const matches = [...spec.matchAll(/^## (.+)$/gm)];
	for (let index = 0; index < matches.length; index++) {
		const match = matches[index];
		const next = matches[index + 1];
		const start = match.index ?? 0;
		result.push({
			name: String(match[1]),
			body: spec.slice(start + match[0].length, next?.index),
		});
	}
	return result;
}

function normativeSentences(body: string) {
	return body
		.replace(/```[\s\S]*?```/g, "")
		.split(/(?<=[.!?])\s+|\n+-\s+/g)
		.map((line) => line.trim())
		.filter((line) => normativeMarker.test(line));
}

describe("leaks spec validation ledger", () => {
	it("keeps the spec section map stable", () => {
		const headings = sections().map((section) => section.name);
		expect(headings).toEqual(expectedHeadings);
	});

	it("records every validation batch in the ledger and evidence table", () => {
		for (const batch of batches) {
			expect(spec).toContain(`| ${batch}`);
		}
	});

	it("maps every normative sentence to at least one validation batch", () => {
		const misses = sections().flatMap((section) => {
			const mappedBatches = sectionBatchMap.get(section.name) ?? [];
			return normativeSentences(section.body)
				.filter(() => mappedBatches.length === 0)
				.map((sentence) => ({ section: section.name, sentence }));
		});
		expect(misses).toEqual([]);
	});

	it("keeps V0 authority and scope requirements explicit", () => {
		expect(spec).toContain("This spec is the source of truth");
		expect(spec).toContain("current implementation is a prototype/reference");
		expect(spec).toContain("Implementation must be fresh and spec-shaped");
		expect(spec).toContain("Avoid creating `extensions/leaks-ref`");
		expect(spec).toContain("Non-goals");
	});
});