repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/__tests__/runtime.test.ts

Raw
File outline
import { afterEach, describe, expect, it } from "vitest";
import { openerCandidates } from "../open";
import { createUiRuntime, type UiRuntime } from "../runtime";

describe("ui browser opener", () => {
	it("uses platform openers", () => {
		expect(
			openerCandidates("http://x", { platform: "win32", env: {} })[0],
		).toEqual({
			command: "cmd.exe",
			args: ["/c", "start", "", "http://x"],
		});
		expect(
			openerCandidates("http://x", { platform: "darwin", env: {} })[0],
		).toEqual({
			command: "open",
			args: ["http://x"],
		});
		expect(
			openerCandidates("http://x", { platform: "linux", env: {} })[0],
		).toEqual({
			command: "xdg-open",
			args: ["http://x"],
		});
	});

	it("uses termux opener when available", () => {
		expect(
			openerCandidates("http://x", {
				platform: "linux",
				env: { TERMUX_VERSION: "1" },
			})[0],
		).toEqual({
			command: "termux-open-url",
			args: ["http://x"],
		});
	});
});

const runtimes: UiRuntime[] = [];

afterEach(async () => {
	await Promise.all(
		runtimes.splice(0).map((runtime) => runtime.stop("test cleanup")),
	);
});

describe("ui runtime", () => {
	it("starts, reports status, redraws QR, and stops", async () => {
		const runtime = await createUiRuntime({
			sessionLabel: "ui: test",
			address: "127.0.0.1",
			openBrowser: false,
		});
		runtimes.push(runtime);

		expect(runtime.status().active).toBe(true);
		expect(new URL(runtime.status().url ?? "").searchParams.has("token")).toBe(
			true,
		);
		expect(runtime.widgetLines().join("\n")).toContain("ui: test");

		await runtime.redrawQr();
		expect(runtime.widgetLines().join("\n")).toContain("mobile qr");

		await runtime.stop("test");
		expect(runtime.status().active).toBe(false);
	});

	it("is safe to stop twice", async () => {
		const runtime = await createUiRuntime({
			sessionLabel: "ui: test",
			address: "127.0.0.1",
			openBrowser: false,
		});
		await runtime.stop("first");
		await runtime.stop("second");
		expect(runtime.status().active).toBe(false);
	});
});