repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/overkill/__tests__/modes.test.ts

Raw
File outline
import { describe, expect, it } from "vitest";
import {
	buildConfirmation,
	buildReviewDiff,
	buildReviewDiffPreview,
	formatProgress,
	formatResultPaths,
	upperBoundCalls,
} from "../lib/modes.ts";

describe("Phase 4 mode contracts", () => {
	it("reports exact call estimates and the canonical confirmation contract", () => {
		expect(upperBoundCalls({ train: 2, val: 3, test: 4 })).toEqual({
			target: 3 + 3 * (2 + 3) + 4,
			optimizer: 12,
		});
		expect(
			buildConfirmation({
				sourcePath: "/source/SKILL.md",
				suitePath: "/project/suite",
				splitCounts: { train: 2, val: 3, test: 4 },
			}),
		).toBe(
			"Source: /source/SKILL.md\nSuite: /project/suite\nSplits: train 2, validation 3, test 4\nUpper bound: 22 target calls, 12 optimizer calls.\nThe trusted checker executes project code with your process environment.\nOverkill will not write the source skill or suite. Continue?",
		);
	});

	it("keeps progress and final path output compact and absolute", () => {
		expect(
			formatProgress({
				stage: "validation",
				completed: 1,
				total: 3,
				bestScore: 0.5,
			}),
		).toBe("Overkill validation 1/3 best 0.5");
		expect(
			formatProgress({
				stage: "complete",
				terminal: "no_improvement",
				bestScore: 0.35000000000000003,
			}),
		).toBe("Overkill no improvement best 0.35");
		expect(
			formatProgress({ stage: "complete", terminal: "staged", bestScore: 1 }),
		).toBe("Overkill candidate staged best 1");
		expect(
			formatResultPaths({
				sourcePath: "/source/SKILL.md",
				reportPath: "/agent/run/report.json",
				stagedPath: "/agent/run/staged-skill.md",
			}),
		).toBe(
			"Source: /source/SKILL.md\nReport: /agent/run/report.json\nStaged: /agent/run/staged-skill.md",
		);
	});

	it("bounds unified review diffs by lines and bytes with one marker", () => {
		const huge = Array.from(
			{ length: 250 },
			(_, index) => `line-${index}`,
		).join("\n");
		const diff = buildReviewDiff(
			"/source/SKILL.md",
			"/run/staged-skill.md",
			"",
			huge,
		);
		expect(diff).toContain("--- /source/SKILL.md");
		expect(diff).toContain("+++ /run/staged-skill.md");
		expect(diff).toMatch(/^@@ -0,0 \+1,\d+ @@$/m);
		expect(diff.split("\n").length).toBeLessThanOrEqual(200);
		expect(Buffer.byteLength(diff, "utf8")).toBeLessThanOrEqual(32 * 1024);
		expect(diff).toMatch(/…\[diff truncated\]\n$/);
	});

	it("builds a compact changed-line preview for immediate completion feedback", () => {
		const preview = buildReviewDiffPreview(
			buildReviewDiff(
				"/source/SKILL.md",
				"/run/staged-skill.md",
				"same\nold one\nold two\nold three",
				"same\nnew one\nnew two\nnew three",
			),
		);
		expect(preview).toBe(
			"-old one\n-old two\n-old three\n+new one\n… 2 more changed lines",
		);
		expect(preview).not.toContain("/source/SKILL.md");
	});

	it("neutralizes ANSI and OSC terminal controls without changing ordinary Unicode", () => {
		const diff = buildReviewDiff(
			"/source/\u001b]0;owned\u0007SKILL.md",
			"/staged/\u009b31mSKILL.md",
			"# Café 😀\nold \u001b[31mred\u001b[0m",
			"# Café 😀\nnew \u001b]52;c;payload\u0007",
		);
		expect(diff).toContain("Café 😀");
		expect(diff).toContain("\\u001b");
		expect(diff).toContain("\\u009b");
		expect(diff).not.toMatch(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/u);
		expect(
			formatResultPaths({
				sourcePath: "/source/\u001b]0;owned\u0007",
				reportPath: "/report/\u009b31m",
			}),
		).toContain("\\u001b");
	});

	it("uses a bounded coarse diff before allocating a quadratic line matrix", () => {
		const manyLines = Array.from({ length: 2_048 }, (_, index) =>
			String(index),
		).join("\n");
		const diff = buildReviewDiff(
			"/source",
			"/staged",
			manyLines,
			`${manyLines}\nchanged`,
		);
		expect(diff).toContain("diff omitted");
		expect(Buffer.byteLength(diff, "utf8")).toBeLessThanOrEqual(32 * 1024);
	});

	it("emits a contextual unified hunk without relabeling unchanged interior lines", () => {
		const diff = buildReviewDiff(
			"/run/baseline-snapshot.md",
			"/run/staged-skill.md",
			"first\nshared\nold\nlast",
			"first\nshared\nnew\nlast",
		);
		expect(diff).toContain("@@ -1,4 +1,4 @@");
		expect(diff).toContain(" first\n shared\n-old\n+new\n last");
		expect(diff).not.toContain("-shared\n+shared");
	});
});