repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/prune/__tests__/format.test.ts

Raw
File outline
import { describe, expect, it } from "bun:test";
import { format } from "../src/format.ts";
import type { ExtractOutput } from "../src/types.ts";

function emptyData(): ExtractOutput {
	return {
		goals: [],
		scopeChanges: [],
		files: { modified: new Set(), created: new Set(), read: new Set() },
		context: [],
		preferences: [],
		transcript: [],
	};
}

describe("format", () => {
	it("renders spec section headers and prune_recall footer", () => {
		const data = emptyData();
		data.goals.push("Fix the auth bug");
		data.scopeChanges.push("also update token refresh logic");
		data.files.modified.add("session.ts");
		data.context.push("lint still failing");
		data.preferences.push("Prefer TypeScript strict mode");
		data.transcript.push({ role: "user", text: "Fix the auth bug", ref: "#1" });

		const out = format(data, {
			sourceEntriesProcessed: 10,
			tailKept: 3,
			tokenEstimate: 18900,
		}).summary;

		expect(out).toContain("[Session Goal]");
		expect(out).toContain("- [Scope change]");
		expect(out).toContain("[Files And Changes]");
		expect(out).toContain("[Outstanding Context]");
		expect(out).toContain("[User Preferences]");
		expect(out).toContain("[Continue Now]");
		expect(out).toContain("- Current request: Fix the auth bug (#1)");
		expect(out).toContain("[user]\nFix the auth bug (#1)");
		expect(out).toContain(
			"prune: 10 entries summarized; before 18.9k tok; raw kept 3 entries",
		);
		expect(out).toContain("Use prune_recall");
		expect(out).not.toContain("vcc_recall");
	});

	it("caps transcript tail", () => {
		const data = emptyData();
		for (let i = 1; i <= 5; i++) {
			data.transcript.push({
				role: "user",
				text: `message ${i}`,
				ref: `#${i}`,
			});
		}

		const out = format(data, { transcriptLimit: 2 }).summary;
		expect(out).toContain("...(3 earlier lines omitted)");
		expect(out).not.toContain("message 1 (#1)");
		expect(out).toContain("message 4 (#4)");
		expect(out).toContain("message 5 (#5)");
	});

	it("renders recall note once", () => {
		const data = emptyData();
		data.transcript.push({ role: "user", text: "hello", ref: "#1" });
		const out = format(data).summary;
		expect(out.match(/Use prune_recall/g)?.length).toBe(1);
	});

	it("renders assistant text, tool without cmd, and errors", () => {
		const data = emptyData();
		data.transcript.push(
			{ role: "assistant", text: "Done", ref: "#2" },
			{ role: "assistant", text: "", tool: "bash", ref: "#3" },
			{ role: "error", text: "[ERROR] bash", ref: "#4" },
		);
		const out = format(data).summary;
		expect(out).toContain("[assistant]\nDone (#2)");
		expect(out).toContain("* bash (#3)");
		expect(out).toContain("[tool_error] [ERROR] bash (#4)");
	});

	it("caps long file lists and omits token estimate when unavailable", () => {
		const data = emptyData();
		for (let i = 0; i < 12; i++) data.files.modified.add(`file${i}.ts`);
		const out = format(data, {
			sourceEntriesProcessed: 12,
			tailKept: 0,
		}).summary;
		expect(out).toContain("(+2 more)");
		expect(out).toContain("prune: 12 entries summarized; raw kept 0 entries");
		expect(out).not.toContain("; before");
	});

	it("formats small token estimates without k suffix", () => {
		const data = emptyData();
		const out = format(data, {
			sourceEntriesProcessed: 1,
			tailKept: 0,
			tokenEstimate: 999,
		}).summary;
		expect(out).toContain("before 999 tok");
	});
});