repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/overkill/__tests__/commands.test.ts

Raw
File outline
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import overkill, { parseArguments } from "../index.ts";

type Command = {
	handler: (args: string, ctx: any) => Promise<void>;
	getArgumentCompletions?: (prefix: string) => unknown;
};

function setup(branch: unknown[] = [], loadedCommands: unknown[] = []) {
	const commands = new Map<string, Command>();
	const notices: Array<[string, string]> = [];
	let next = 0;
	const pi = {
		on() {},
		registerCommand(name: string, command: Command) {
			commands.set(name, command);
		},
		registerEntryRenderer() {},
		appendEntry(customType: string, data: unknown) {
			branch.push({ id: `entry-${next++}`, type: "custom", customType, data });
		},
		getCommands() {
			return loadedCommands;
		},
		getThinkingLevel() {
			return "off";
		},
	};
	overkill(pi as never);
	const ctx = {
		cwd: "/project",
		model: undefined,
		modelRegistry: {
			async getAvailable() {
				return [];
			},
		},
		isProjectTrusted() {
			return true;
		},
		sessionManager: {
			getBranch() {
				return branch;
			},
			getSessionId() {
				return "session";
			},
		},
		ui: {
			notify(message: string, level: string) {
				notices.push([message, level]);
			},
		},
	};
	return { branch, commands, ctx, notices };
}

function overkillCommand(state: ReturnType<typeof setup>): Command {
	const command = state.commands.get("overkill");
	if (!command) throw new Error("overkill command was not registered");
	return command;
}

describe("/overkill Phase 3 command fixtures", () => {
	it("preserves Windows UNC paths", () => {
		for (const input of [
			"\\\\server\\share\\SKILL.md suite",
			'"\\\\server\\share\\SKILL.md" suite',
		]) {
			expect(parseArguments(input)).toEqual([
				"\\\\server\\share\\SKILL.md",
				"suite",
			]);
		}
	});

	it("reports complete branch-local suite status without exposing private data", async () => {
		const state = setup([
			{
				id: "run",
				type: "custom",
				customType: "overkill-run",
				data: {
					version: 1,
					runId: "123e4567-e89b-12d3-a456-426614174000",
					kind: "suite",
					stage: "validation",
					terminal: "staged",
					completed: 2,
					total: 3,
					currentScore: 0.5,
					bestScore: 0.75,
					sourcePath: "/source/SKILL.md",
					reportPath: null,
					stagedPath: "/private/staged.md",
				},
			},
		]);
		await overkillCommand(state).handler("status", state.ctx);
		expect(state.notices).toEqual([
			[
				"Overkill: validation | completed: 2/3 | current score: 0.5 | best score: 0.75 | terminal: staged | staged: /private/staged.md",
				"info",
			],
		]);
	});

	it("reports checkpointed training and final-test progress for staged review without private evidence", async () => {
		const state = setup([
			{
				id: "initial-link",
				type: "custom",
				customType: "overkill-run",
				data: {
					version: 1,
					runId: "123e4567-e89b-12d3-a456-426614174000",
					kind: "suite",
					stage: "final_test",
					terminal: null,
					completed: 1,
					total: 2,
					currentScore: 0.75,
					bestScore: 0.75,
					reportPath: null,
					stagedPath: null,
				},
			},
			{
				id: "staged",
				type: "custom",
				customType: "overkill-run",
				data: {
					version: 1,
					runId: "123e4567-e89b-12d3-a456-426614174000",
					kind: "suite",
					stage: "complete",
					terminal: "staged",
					currentScore: 0.75,
					bestScore: 0.75,
					reportPath: "/private/report.json",
					stagedPath: "/private/staged-skill.md",
				},
			},
		]);
		await overkillCommand(state).handler("status", state.ctx);
		expect(state.notices.at(-1)).toEqual([
			"Overkill: complete | current score: 0.75 | best score: 0.75 | terminal: staged | staged: /private/staged-skill.md",
			"info",
		]);
		await overkillCommand(state).handler("cancel", state.ctx);
		expect(state.notices.at(-1)).toEqual([
			"No Overkill rollout is active.",
			"info",
		]);
	});

	it("confirms the canonical cost contract before creating a run", async () => {
		const root = await mkdtemp(join(tmpdir(), "overkill-command-"));
		try {
			const skill = join(root, "SKILL.md");
			const suite = join(root, "suite");
			await writeFile(skill, "---\nname: demo\ndescription: Demo\n---\nBody\n");
			for (const split of ["train", "val", "test"]) {
				await mkdir(join(suite, split), { recursive: true });
				await writeFile(
					join(suite, split, "items.json"),
					JSON.stringify([{ id: split, prompt: `${split} task` }]),
				);
			}
			await writeFile(join(suite, "check.mjs"), "process.exit(0)");
			const state = setup(
				[],
				[
					{
						name: "skill:demo",
						source: "skill",
						sourceInfo: { path: skill, scope: "user" },
					},
				],
			);
			const model = { provider: "test", id: "model" };
			state.ctx.cwd = root;
			state.ctx.mode = "tui";
			state.ctx.model = model;
			state.ctx.modelRegistry.find = () => model;
			state.ctx.modelRegistry.getAvailable = async () => [model];
			let confirmation = "";
			state.ctx.ui.confirm = async (_title: string, message: string) => {
				confirmation = message;
				return false;
			};
			await overkillCommand(state).handler(`${skill} ${suite}`, state.ctx);
			expect(confirmation).toContain(`Source: ${skill}`);
			expect(confirmation).toContain(`Suite: ${suite}`);
			expect(confirmation).toContain(
				"Upper bound: 8 target calls, 12 optimizer calls.",
			);
			expect(confirmation).toContain("trusted checker executes project code");
			expect(confirmation).toContain(
				"will not write the source skill or suite",
			);
			expect(state.branch).toEqual([]);
		} finally {
			await rm(root, { force: true, recursive: true });
		}
	});

	it("emits headless status as a normal run entry instead of a no-op notification", async () => {
		const state = setup([
			{
				id: "run",
				type: "custom",
				customType: "overkill-run",
				data: {
					version: 1,
					runId: "123e4567-e89b-12d3-a456-426614174000",
					kind: "suite",
					stage: "training",
					terminal: null,
					reportPath: null,
					stagedPath: null,
				},
			},
		]);
		state.ctx.mode = "json";
		await overkillCommand(state).handler("status", state.ctx);
		expect(state.notices).toEqual([]);
		expect(state.branch).toHaveLength(2);
		expect((state.branch.at(-1) as any).data).toMatchObject({
			stage: "training",
			terminal: null,
		});
	});

	it("offers subcommand completion and rejects new runs without both explicit paths", async () => {
		const state = setup();
		expect(overkillCommand(state).getArgumentCompletions?.("st")).toEqual([
			{ value: "status", label: "status" },
		]);
		await overkillCommand(state).handler("only-one-path", state.ctx);
		expect(state.notices.at(-1)).toEqual([
			"Usage: /overkill <skill-path> <suite-dir>",
			"error",
		]);
	});
});