repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/overkill/__tests__/preflight.test.ts

Raw
File outline
import { access, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { fingerprintSource } from "../lib/checker.ts";
import { inspectSuiteRun, prepareSuiteRun } from "../lib/preflight.ts";

const temporaryPaths: string[] = [];

afterEach(async () => {
	await Promise.all(
		temporaryPaths
			.splice(0)
			.map((path) => rm(path, { force: true, recursive: true })),
	);
});

async function fixture(): Promise<{
	agentDir: string;
	root: string;
	skill: string;
	suite: string;
}> {
	const root = await mkdtemp(join(tmpdir(), "overkill-preflight-"));
	temporaryPaths.push(root);
	const skill = join(root, "SKILL.md");
	const suite = join(root, "suite");
	const agentDir = join(root, "agent");
	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.stdout.write(JSON.stringify({ score: 1, passed: true }));",
	);
	return { agentDir, root, skill, suite };
}

const loadedCommands = (skill: string) => [
	{
		name: "skill:demo",
		source: "skill",
		sourceInfo: { path: skill, scope: "user" },
	},
];

describe("Phase 1 preflight", () => {
	it("validates model and auth before creating any run artifact", async () => {
		const data = await fixture();
		await expect(
			prepareSuiteRun({
				...data,
				cwd: data.root,
				commands: loadedCommands(data.skill),
				projectTrusted: true,
				sourcePath: data.skill,
				suitePath: data.suite,
				sessionId: "session",
				model: undefined,
				authAvailable: false,
			}),
		).rejects.toThrow("model");
		await expect(
			access(join(data.agentDir, "overkill-runs")),
		).rejects.toThrow();
	});

	it("rejects source or suite changes after confirmation before artifact creation", async () => {
		const data = await fixture();
		const options = {
			...data,
			cwd: data.root,
			commands: loadedCommands(data.skill),
			projectTrusted: true,
			sourcePath: data.skill,
			suitePath: data.suite,
			sessionId: "session",
			model: { provider: "test", id: "model", thinking: "off" },
			authAvailable: true,
		};
		const inspected = await inspectSuiteRun(options);
		await writeFile(
			join(data.suite, "val", "items.json"),
			JSON.stringify([{ id: "changed", prompt: "changed" }]),
		);
		await expect(
			prepareSuiteRun(options, {
				sourceFingerprint: fingerprintSource(inspected.sourceBytes),
				suiteFingerprint: inspected.suite.fingerprint ?? "",
			}),
		).rejects.toThrow("changed after confirmation");
		await expect(
			access(join(data.agentDir, "overkill-runs")),
		).rejects.toThrow();
	});

	it("rejects source changes after confirmation before artifact creation", async () => {
		const data = await fixture();
		const options = {
			...data,
			cwd: data.root,
			commands: loadedCommands(data.skill),
			projectTrusted: true,
			sourcePath: data.skill,
			suitePath: data.suite,
			sessionId: "session",
			model: { provider: "test", id: "model", thinking: "off" },
			authAvailable: true,
		};
		const inspected = await inspectSuiteRun(options);
		await writeFile(
			data.skill,
			"---\nname: demo\ndescription: Demo\n---\nChanged\n",
		);
		await expect(
			prepareSuiteRun(options, {
				sourceFingerprint: fingerprintSource(inspected.sourceBytes),
				suiteFingerprint: inspected.suite.fingerprint ?? "",
			}),
		).rejects.toThrow("changed after confirmation");
		await expect(
			access(join(data.agentDir, "overkill-runs")),
		).rejects.toThrow();
	});

	it("creates only private metadata after canonical source and trusted suite validation", async () => {
		const data = await fixture();
		const run = await prepareSuiteRun({
			...data,
			cwd: data.root,
			commands: loadedCommands(data.skill),
			projectTrusted: true,
			sourcePath: data.skill,
			suitePath: data.suite,
			sessionId: "session",
			model: { provider: "test", id: "model", thinking: "off" },
			authAvailable: true,
		});
		expect(run.source.name).toBe("demo");
		expect(run.suite.splitCounts).toEqual({ test: 1, train: 1, val: 1 });
		expect(run.manifest.source.path).toBe(run.sourcePath);
		expect(run.directory).toContain(join(data.agentDir, "overkill-runs"));
		expect(run.manifest).not.toHaveProperty("items");
	});
});