repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/voice/__serial__/voice-shortcuts.test.ts
Rawimport type {
ExtensionAPI,
ExtensionContext,
} from "@earendil-works/pi-coding-agent";
import { afterEach, expect, it, vi } from "vitest";
const fixtures = vi.hoisted(() => ({ setup: vi.fn() }));
vi.mock("../src/native-setup-ui.ts", () => ({
runNativeSetup: fixtures.setup,
}));
import voice from "../index.ts";
import { NativeVoiceCall } from "../src/native-call.ts";
const prepared = {
client: { close: vi.fn() },
command: ["node", "fake.mjs"],
profile: {
id: "fake",
label: "fake",
description: "fake",
installed: true,
capabilities: {},
},
preview: true,
} as never;
afterEach(() => {
vi.restoreAllMocks();
vi.clearAllMocks();
});
function setup() {
const shortcuts = new Map<
string,
{ handler: (ctx: ExtensionContext) => unknown }
>();
const pi = {
on: vi.fn(),
registerCommand: vi.fn(),
registerShortcut: (
key: string,
value: { handler: (ctx: ExtensionContext) => unknown },
) => shortcuts.set(key, value),
} as unknown as ExtensionAPI;
const ctx = {
hasUI: true,
cwd: "/fixture",
isProjectTrusted: () => false,
ui: { notify: vi.fn(), setWidget: vi.fn() },
} as unknown as ExtensionContext;
voice(pi);
return { shortcuts, ctx };
}
it("registers default shortcuts and forwards configured service setup", async () => {
fixtures.setup.mockResolvedValue(prepared);
const start = vi
.spyOn(NativeVoiceCall.prototype, "start")
.mockResolvedValue();
const stop = vi.spyOn(NativeVoiceCall.prototype, "stop").mockResolvedValue();
const { shortcuts, ctx } = setup();
await shortcuts.get("ctrl+alt+v")?.handler(ctx);
expect(start).toHaveBeenCalledWith(
ctx,
expect.objectContaining({
backend: prepared,
historyDirectory: expect.stringMatching(/history$/),
}),
);
expect(fixtures.setup).toHaveBeenCalledWith(
ctx,
expect.objectContaining({
command: ["speak_to_me"],
preview: true,
cancelShortcut: "ctrl+alt+v",
}),
);
vi.spyOn(NativeVoiceCall.prototype, "active", "get").mockReturnValue(true);
await shortcuts.get("ctrl+alt+v")?.handler(ctx);
expect(stop).toHaveBeenCalled();
});
it("mute shortcut reports inactive use and serializes transitions", async () => {
const { shortcuts, ctx } = setup();
const mute = vi
.spyOn(NativeVoiceCall.prototype, "mute")
.mockRejectedValue(new Error("Start native voice before changing mute."));
await shortcuts.get("ctrl+alt+b")?.handler(ctx);
expect(mute).toHaveBeenCalledWith(true);
expect(ctx.ui.notify).toHaveBeenCalledWith(
expect.stringContaining("shortcut failed"),
"error",
);
});