repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/__tests__/harness.test.ts

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

describe("ui notifications", () => {
	it("omits token params from notification URLs", () => {
		expect(notifyUrl("http://127.0.0.1:123/?token=abc")).toBe(
			"http://127.0.0.1:123/",
		);
	});
});

describe("ui 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("registers the /ui command without starting a runtime", async () => {
		const extensionPath = path.resolve(import.meta.dirname, "../index.ts");
		t = await createTestSession({ extensions: [extensionPath] });
		const extension = t.session.extensionRunner.extensions[0];

		expect([...extension.commands.keys()]).toEqual(["ui"]);
		expect([...extension.tools.keys()]).toEqual([
			"ui_card",
			"ui_table",
			"ui_diagram",
			"ui_form",
			"ui_mockup",
			"ui_html",
			"ui_update",
			"ui_wait_action",
		]);
		expect(t.events.uiCallsFor("setWidget")).toHaveLength(0);
	});

	it("reports an actionable seed error in an empty session", async () => {
		const extensionPath = path.resolve(import.meta.dirname, "../index.ts");
		t = await createTestSession({ extensions: [extensionPath] });
		const command = t.session.extensionRunner.extensions[0].commands.get("ui");
		expect(command).toBeDefined();

		await command?.handler(
			"",
			t.session.extensionRunner.createCommandContext(),
		);

		expect(latestNotify(t)).toContain("Cannot seed UI session");
		expect(latestNotify(t)).toContain("Action:");
		expect(t.events.uiCallsFor("setWidget").at(-1)?.args).toEqual([
			"ui",
			undefined,
		]);
	});
});

function latestNotify(t: TestSession): string {
	return String(t.events.uiCallsFor("notify").at(-1)?.args[0] ?? "");
}