Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/voice/__serial__/rpc-package.test.ts

Raw
import { cp, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { basename, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
	createPiSession,
	type PiSession,
} from "../../../test/fixture/create-pi-session.ts";

const packageRoot = process.env["PI_VOICE_RPC_PACKAGE_ROOT"];
const fake = fileURLToPath(
	new URL("../__e2e__/fake-native.mjs", import.meta.url),
);
let temporary: string | undefined;
let session: PiSession | undefined;

afterEach(async () => {
	await session?.dispose();
	session = undefined;
	if (temporary) await rm(temporary, { recursive: true, force: true });
	temporary = undefined;
});

describe.runIf(packageRoot)("relocated Voice RPC package", () => {
	it("loads without native/model resources, rejects missing default command, and inspects an explicit fake", async () => {
		const source = resolve(packageRoot ?? "");
		temporary = await mkdtemp(join(tmpdir(), "pi-voice-package-"));
		const relocated = join(temporary, basename(source));
		await cp(source, relocated, { recursive: true, force: false });
		session = await createPiSession({
			extensionPaths: [join(relocated, "index.ts")],
			env: {
				PI_VOICE_COMMAND: JSON.stringify([process.execPath, fake]),
			},
		});
		expect(session.errors).toEqual([]);
		expect(session.extensions[0]?.commands.has("voice")).toBe(true);
		const runner = session.session.extensionRunner;
		const setWidget = vi.fn();
		const notify = vi.fn();
		runner.setUIContext({ ...runner.getUIContext(), setWidget, notify }, "tui");
		await session.session.prompt("/voice");
		expect(
			setWidget.mock.calls.some(
				([id, component]) => id === "voice" && typeof component === "function",
			),
		).toBe(true);

		expect(notify).toHaveBeenCalledWith(
			"Voice uses a fake test backend, not verified native speech.",
			"warning",
		);
		await session.session.prompt("/voice stop");
		notify.mockClear();
		process.env["PI_VOICE_COMMAND"] = JSON.stringify([
			"speak_to_me_not_installed_for_test",
		]);
		await session.session.prompt("/voice");
		expect(
			notify.mock.calls.some(([message]) =>
				String(message).includes("speak_to_me_not_installed_for_test"),
			),
		).toBe(true);
		expect(
			notify.mock.calls.some(([message]) =>
				String(message).includes("fake test backend"),
			),
		).toBe(false);
		expect(session.errors).toEqual([]);
	});
});