Open History to see the last commit
1import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
2import { expect, it, vi } from "vite-plus/test";
3import { render } from "../../test/render";
4import { useDictationShortcut } from "./useDictationShortcut";
5
6vi.mock("../hooks/useSettings", () => ({ useClientSettings: () => "hold" }));
7
8function Probe({ speech }: { speech: Parameters<typeof useDictationShortcut>[0]["speech"] }) {
9 useDictationShortcut({
10 keybindings: DEFAULT_RESOLVED_KEYBINDINGS,
11 speech,
12 disabled: false,
13 terminalOpen: false,
14 modelPickerOpen: false,
15 });
16 return null;
17}
18
19const press = (repeat = false) =>
20 window.dispatchEvent(
21 new KeyboardEvent("keydown", {
22 key: "D",
23 code: "KeyD",
24 ctrlKey: !/Mac/i.test(navigator.platform),
25 metaKey: /Mac/i.test(navigator.platform),
26 shiftKey: true,
27 cancelable: true,
28 repeat,
29 }),
30 );
31
32function actions(available: boolean) {
33 return {
34 available,
35 state: { phase: "idle" as const, error: null, errorAction: null },
36 start: vi.fn(async () => {}),
37 stop: vi.fn(async () => {}),
38 cancel: vi.fn(),
39 };
40}
41
42it("leaves the browser shortcut alone when dictation is off", async () => {
43 const speech = actions(false);
44 await using _screen = await render(<Probe speech={speech} />);
45 expect(press()).toBe(true);
46 expect(speech.start).not.toHaveBeenCalled();
47});
48
49it("stops on a new press when the OS omitted keyup, but ignores autorepeat", async () => {
50 const speech = actions(true);
51 await using screen = await render(<Probe speech={speech} />);
52 expect(press()).toBe(false);
53 expect(speech.start).toHaveBeenCalledOnce();
54 await screen.rerender(
55 <Probe speech={{ ...speech, state: { ...speech.state, phase: "recording" } }} />,
56 );
57 press(true);
58 expect(speech.stop).not.toHaveBeenCalled();
59 press();
60 expect(speech.stop).toHaveBeenCalledOnce();
61});
62
63it("finishes a released hold after startup even if the chord was pressed again while preparing", async () => {
64 const speech = actions(true);
65 let finishStart!: () => void;
66 const startup = new Promise<void>((resolve) => {
67 finishStart = resolve;
68 });
69 speech.start.mockReturnValue(startup);
70 await using screen = await render(<Probe speech={speech} />);
71 press();
72 await screen.rerender(
73 <Probe speech={{ ...speech, state: { ...speech.state, phase: "preparing" } }} />,
74 );
75 window.dispatchEvent(new KeyboardEvent("keyup", { code: "KeyD" }));
76 press();
77 window.dispatchEvent(new KeyboardEvent("keyup", { code: "KeyD" }));
78 finishStart();
79 await startup;
80 expect(speech.start).toHaveBeenCalledOnce();
81 expect(speech.stop).toHaveBeenCalledOnce();
82});