repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/q/__tests__/harness.test.ts

Raw
File outline
import { afterEach, describe, expect, it } from "vitest";
import { createTestSession, type TestSession } from "../../../test/harness";
import qExtension from "../index";

type Handler = (event: any, ctx: any) => unknown | Promise<unknown>;

function createHarness() {
	const handlers = new Map<string, Handler[]>();
	const commands = new Map<string, any>();
	const shortcuts = new Map<string, any>();
	const notifications: Array<[string, string | undefined]> = [];
	const ctx = {
		hasUI: true,
		ui: {
			notify: (message: string, severity?: string) =>
				notifications.push([message, severity]),
			setWidget() {},
		},
		sessionManager: { getBranch: () => [] },
	};
	const pi = {
		on(event: string, handler: Handler) {
			const list = handlers.get(event) ?? [];
			list.push(handler);
			handlers.set(event, list);
		},
		registerCommand(name: string, options: any) {
			commands.set(name, options);
		},
		registerShortcut(shortcut: string, options: any) {
			shortcuts.set(shortcut, options);
		},
		sendUserMessage() {},
	};
	qExtension(pi as any);
	return {
		commands,
		shortcuts,
		notifications,
		async emit(event: string, payload: any) {
			for (const handler of handlers.get(event) ?? [])
				await handler(payload, ctx);
		},
	};
}

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

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

	it("loads in real Pi extension runtime", async () => {
		t = await createTestSession({ extensionFactories: [qExtension] });

		expect(t.session.extensionRunner.extensions).toHaveLength(1);
	});

	it("registers Alt+Q shortcut", () => {
		const h = createHarness();

		expect(h.shortcuts.has("alt+q")).toBe(true);
	});

	it("Alt+Q works with shortcut context", async () => {
		const h = createHarness();
		const shortcut = h.shortcuts.get("alt+q");
		const notifications: Array<[string, string | undefined]> = [];
		const ctx = {
			hasUI: true,
			isIdle: () => true,
			ui: {
				custom: async () => ({ type: "cancel" }),
				notify: (message: string, severity?: string) =>
					notifications.push([message, severity]),
			},
			sessionManager: {
				getBranch: () => [
					{
						type: "message",
						id: "assistant-1",
						message: {
							role: "assistant",
							content: [
								{ type: "text", text: "Which DB?\n- Postgres\n- SQLite" },
							],
						},
					},
				],
			},
		};

		await shortcut.handler(ctx);

		expect(notifications).toEqual([["/q cancelled", "info"]]);
	});

	it("rejects incomplete latest assistant message", async () => {
		const h = createHarness();
		const shortcut = h.shortcuts.get("alt+q");
		const notifications: Array<[string, string | undefined]> = [];
		const ctx = {
			hasUI: true,
			isIdle: () => true,
			ui: {
				custom: async () => ({ type: "cancel" }),
				notify: (message: string, severity?: string) =>
					notifications.push([message, severity]),
			},
			sessionManager: {
				getBranch: () => [
					{
						type: "message",
						id: "assistant-1",
						message: {
							role: "assistant",
							stopReason: "tool_use",
							content: [{ type: "text", text: "Which DB?" }],
						},
					},
				],
			},
		};

		await shortcut.handler(ctx);

		expect(notifications).toEqual([
			["Last assistant message incomplete (tool_use)", "error"],
		]);
	});

	it("shows extraction UI before slow LLM suggestions", async () => {
		const h = createHarness();
		const shortcut = h.shortcuts.get("alt+q");
		const notifications: Array<[string, string | undefined]> = [];
		const customCalls: unknown[] = [];
		const ctx = {
			hasUI: true,
			isIdle: () => true,
			ui: {
				custom: async (factory: unknown) => {
					customCalls.push(factory);
					return customCalls.length === 1
						? {
								status: "ok",
								questions: [{ question: "Which DB?", suggestions: ["SQLite"] }],
							}
						: { type: "cancel" };
				},
				notify: (message: string, severity?: string) =>
					notifications.push([message, severity]),
			},
			sessionManager: {
				getBranch: () => [
					{
						type: "message",
						id: "assistant-1",
						message: {
							role: "assistant",
							content: [{ type: "text", text: "Which DB?" }],
						},
					},
				],
			},
		};

		await shortcut.handler(ctx);

		expect(customCalls).toHaveLength(2);
		expect(notifications).toEqual([["/q cancelled", "info"]]);
	});

	it("notifies when an assistant message has local questions", async () => {
		const h = createHarness();

		await h.emit("message_end", {
			message: {
				role: "assistant",
				content: [{ type: "text", text: "Which DB?\n- Postgres\n- SQLite" }],
			},
		});

		expect(h.notifications).toEqual([
			["Tip: press Alt+Q or run /q to render 1 question.", "info"],
		]);
	});

	it("does not notify when local extraction finds no questions", async () => {
		const h = createHarness();

		await h.emit("message_end", {
			message: {
				role: "assistant",
				content: [{ type: "text", text: "No questions found." }],
			},
		});

		expect(h.notifications).toEqual([]);
	});
});