repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/the-system/__tests__/harness.test.ts

Raw
File outline
import { mkdirSync, writeFileSync } from "node:fs";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createTestSession, type TestSession } from "../../../test/harness";
import { applyInitialization } from "../init.ts";

describe("the-system pi-test-harness", () => {
	let t: TestSession | undefined;

	afterEach(() => {
		t?.dispose();
		t = undefined;
	});

	it("loads in real Pi extension runtime", async () => {
		const extensionPath = path.resolve(import.meta.dirname, "../index.ts");
		t = await createTestSession({ extensions: [extensionPath] });

		expect(t.session.extensionRunner.extensions).toHaveLength(1);
		expect(t.session.extensionRunner.extensions[0].path).toBe(extensionPath);
	});

	it("keeps system_slot through clarification and cancels it on the next lifecycle", async () => {
		const extensionPath = path.resolve(import.meta.dirname, "../index.ts");
		t = await createTestSession({ extensions: [extensionPath] });
		mkdirSync(path.join(t.cwd, ".git"));
		applyInitialization(t.cwd, "PX");
		const [extension] = t.session.extensionRunner.extensions;
		const sendUserMessage = vi
			.spyOn(t.session, "sendUserMessage")
			.mockResolvedValue();
		expect(extension.tools.has("system_slot")).toBe(true);
		expect(t.session.getActiveToolNames()).not.toContain("system_slot");
		await extension.commands
			.get("system")
			.handler(
				"spec let us discuss caching",
				t.session.extensionRunner.createCommandContext(),
			);
		expect(sendUserMessage).toHaveBeenCalledOnce();
		expect(t.session.getActiveToolNames()).toContain("system_slot");
		expect(t.session.systemPrompt).toContain(
			"- system_slot: Reserve an ID and path for a System bundle",
		);
		expect(t.session.systemPrompt).toContain(
			"- Resolve needed clarifications before reserving slots.",
		);
		expect(t.session.systemPrompt).toContain(
			"- Call system_slot once per required bundle after choosing its final type and title; use each returned ID and path exactly.",
		);
		for (const handler of extension.handlers.get("agent_settled"))
			await handler(
				{ type: "agent_settled", messages: [] },
				t.session.extensionRunner.createCommandContext(),
			);
		expect(t.session.getActiveToolNames()).toContain("system_slot");
		await extension.commands
			.get("system")
			.handler(
				"exec continue",
				t.session.extensionRunner.createCommandContext(),
			);
		expect(t.session.getActiveToolNames()).not.toContain("system_slot");
		expect(t.session.systemPrompt).not.toContain("- system_slot:");
	});

	it("requests one-shot approval for an existing core mutation", async () => {
		const extensionPath = path.resolve(import.meta.dirname, "../index.ts");
		t = await createTestSession({ extensions: [extensionPath] });
		mkdirSync(path.join(t.cwd, ".git"));
		mkdirSync(path.join(t.cwd, ".system"));
		writeFileSync(path.join(t.cwd, ".system", "SYSTEM.md"), "Approved.\n");

		const blocked = await t.session.extensionRunner.emitToolCall({
			type: "tool_call",
			toolName: "edit",
			toolCallId: "blocked-core-edit",
			input: {
				path: ".system/SYSTEM.md",
				edits: [{ oldText: "Approved.", newText: "Human-approved." }],
			},
		});
		expect(blocked).toBeUndefined();
		const [approval] = t.events.uiCallsFor("select");
		expect(approval?.args).toEqual([
			"The System ownership approval",
			[
				"Approve once: edit .system/SYSTEM.md",
				"Deny: keep .system/SYSTEM.md unchanged",
			],
		]);
	});
});