repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/__tests__/command-adapter.test.ts

Raw
File outline
import { describe, expect, it } from "vitest";
import {
	abortCurrent,
	compactSession,
	cycleModel,
	cycleThinking,
	runBash,
	runSlashCommand,
	sendPrompt,
} from "../command-adapter";

describe("ui rpc command adapters", () => {
	it("sends normal prompts", async () => {
		const rpc = fakeRpc();
		await sendPrompt(rpc, "hello");
		expect(rpc.sent).toEqual([{ type: "prompt", message: "hello" }]);
	});

	it("attaches digest once to the next prompt", async () => {
		const rpc = fakeRpc();
		let digest = "Browser digest:\n- click SEND";
		await sendPrompt(rpc, "hello", {
			digest: () => digest,
			clearDigest: () => {
				digest = "";
			},
		});
		await sendPrompt(rpc, "again", {
			digest: () => digest,
			clearDigest: () => {
				digest = "";
			},
		});
		expect(rpc.sent).toEqual([
			{
				type: "prompt",
				message: "Browser digest:\n- click SEND\n\nUser prompt:\nhello",
			},
			{ type: "prompt", message: "again" },
		]);
	});

	it("runs slash commands through prompt expansion", async () => {
		const rpc = fakeRpc();
		await runSlashCommand(rpc, "mode", "terse");
		expect(rpc.sent).toEqual([{ type: "prompt", message: "/mode terse" }]);
	});

	it("runs bash and preserves context exclusion", async () => {
		const rpc = fakeRpc();
		await runBash(rpc, "git status", true);
		expect(rpc.sent).toEqual([
			{ type: "bash", command: "git status", excludeFromContext: true },
		]);
	});

	it("cycles model and thinking", async () => {
		const rpc = fakeRpc();
		await cycleModel(rpc);
		await cycleThinking(rpc);
		expect(rpc.sent).toEqual([
			{ type: "cycle_model" },
			{ type: "cycle_thinking_level" },
		]);
	});

	it("compacts and aborts", async () => {
		const rpc = fakeRpc();
		await compactSession(rpc);
		await abortCurrent(rpc);
		expect(rpc.sent).toEqual([{ type: "compact" }, { type: "abort" }]);
	});
});

function fakeRpc() {
	const sent: unknown[] = [];
	return {
		sent,
		send(request: unknown) {
			sent.push(request);
			return Promise.resolve({ type: "response", success: true });
		},
	};
}