@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 83 lines 3.14 KB
Raw
1 import { expect, it, vi } from "vite-plus/test";
2 import { requestMicrophone } from "./requestMicrophone";
3
4 function setup(input: {
5 readonly getUserMedia: ReturnType<typeof vi.fn>;
6 readonly platform?: string;
7 readonly restartAudioService?: () => Promise<boolean>;
8 }) {
9 const navigator = vi.spyOn(globalThis, "navigator", "get").mockReturnValue({
10 mediaDevices: { getUserMedia: input.getUserMedia },
11 } as unknown as Navigator);
12 vi.stubGlobal("window", {
13 desktopBridge: {
14 getClientPlatform: () => input.platform ?? "linux",
15 ...(input.restartAudioService ? { restartAudioService: input.restartAudioService } : {}),
16 },
17 });
18 return {
19 [Symbol.dispose]() {
20 navigator.mockRestore();
21 vi.unstubAllGlobals();
22 },
23 };
24 }
25
26 it.each(["NotFoundError", "NotReadableError"])(
27 "reopens the Linux desktop microphone once after %s",
28 async (name) => {
29 const stream = {} as MediaStream;
30 const getUserMedia = vi
31 .fn()
32 .mockRejectedValueOnce(new DOMException("Disconnected", name))
33 .mockResolvedValue(stream);
34 const restartAudioService = vi.fn().mockResolvedValue(true);
35 using _env = setup({ getUserMedia, restartAudioService });
36 expect(await requestMicrophone("")).toBe(stream);
37 expect(getUserMedia.mock.calls).toEqual([[{ audio: true }], [{ audio: true }]]);
38 expect(restartAudioService).toHaveBeenCalledOnce();
39 },
40 );
41
42 it.each([
43 { name: "NotAllowedError", platform: "linux", recovered: true, calls: 1 },
44 { name: "OverconstrainedError", platform: "linux", recovered: true, calls: 1 },
45 { name: "NotFoundError", platform: "darwin", recovered: true, calls: 1 },
46 { name: "NotFoundError", platform: "linux", recovered: false, calls: 1 },
47 { name: "NotFoundError", platform: "linux", recovered: undefined, calls: 1 },
48 { name: "NotReadableError", platform: "linux", recovered: true, calls: 2 },
49 ])(
50 "keeps $name on $platform (recovered: $recovered)",
51 async ({ name, platform, recovered, calls }) => {
52 const error = new DOMException("Unavailable", name);
53 const getUserMedia = vi.fn().mockRejectedValue(error);
54 using _env = setup({
55 getUserMedia,
56 platform,
57 ...(recovered === undefined ? {} : { restartAudioService: async () => recovered }),
58 });
59 await expect(requestMicrophone("")).rejects.toBe(error);
60 expect(getUserMedia).toHaveBeenCalledTimes(calls);
61 },
62 );
63
64 it("keeps the system-default fallback across one Linux audio recovery", async () => {
65 const missing = new DOMException("Disconnected", "NotFoundError");
66 const stream = {} as MediaStream;
67 const getUserMedia = vi
68 .fn()
69 .mockRejectedValueOnce(missing)
70 .mockRejectedValueOnce(missing)
71 .mockRejectedValueOnce(missing)
72 .mockResolvedValueOnce(stream);
73 const restartAudioService = vi.fn().mockResolvedValue(true);
74 using _env = setup({ getUserMedia, restartAudioService });
75 expect(await requestMicrophone("missing-mic")).toBe(stream);
76 expect(getUserMedia.mock.calls).toEqual([
77 [{ audio: { deviceId: { exact: "missing-mic" } } }],
78 [{ audio: true }],
79 [{ audio: { deviceId: { exact: "missing-mic" } } }],
80 [{ audio: true }],
81 ]);
82 expect(restartAudioService).toHaveBeenCalledOnce();
83 });