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
| 1 | import { expect, it, vi } from "vite-plus/test"; |
| 2 | import { createBrowserPcmCapture } from "./browserPcmCapture"; |
| 3 | import { requestMicrophone } from "./requestMicrophone"; |
| 4 | |
| 5 | it("records from the chosen microphone, cancels an unanswered prompt, and stops a late stream", async () => { |
| 6 | let grantPermission!: (media: MediaStream) => void; |
| 7 | const permission = new Promise<MediaStream>((resolve) => { |
| 8 | grantPermission = resolve; |
| 9 | }); |
| 10 | using microphone = vi.spyOn(navigator.mediaDevices, "getUserMedia").mockReturnValue(permission); |
| 11 | const capture = createBrowserPcmCapture( |
| 12 | { |
| 13 | getMicrophoneId: async () => "blue-yeti", |
| 14 | onLevel: vi.fn(), |
| 15 | onDurationLimit: vi.fn(), |
| 16 | onError: vi.fn(), |
| 17 | }, |
| 18 | async () => ({ locale: "en", transcribe: async () => "" }), |
| 19 | ); |
| 20 | const abort = new AbortController(); |
| 21 | await capture.transcriber.prepare({ signal: abort.signal }); |
| 22 | const rejected = vi.fn(); |
| 23 | const preparing = capture.recorder.prepareToRecordAsync(abort.signal).catch(rejected); |
| 24 | await vi.waitFor(() => |
| 25 | expect(microphone).toHaveBeenCalledExactlyOnceWith({ |
| 26 | audio: { deviceId: { exact: "blue-yeti" } }, |
| 27 | }), |
| 28 | ); |
| 29 | abort.abort(); |
| 30 | const stop = vi.fn(); |
| 31 | try { |
| 32 | await vi.waitFor(() => expect(rejected).toHaveBeenCalledWith(abort.signal.reason)); |
| 33 | } finally { |
| 34 | grantPermission({ getTracks: () => [{ stop }] } as unknown as MediaStream); |
| 35 | await preparing; |
| 36 | } |
| 37 | await vi.waitFor(() => expect(stop).toHaveBeenCalledOnce()); |
| 38 | }); |
| 39 | |
| 40 | it("falls back to the system default when the saved microphone is gone, and reports other failures", async () => { |
| 41 | const media = {} as MediaStream; |
| 42 | using microphone = vi |
| 43 | .spyOn(navigator.mediaDevices, "getUserMedia") |
| 44 | .mockRejectedValueOnce(new DOMException("gone", "OverconstrainedError")) |
| 45 | .mockResolvedValueOnce(media); |
| 46 | await expect(requestMicrophone("blue-yeti")).resolves.toBe(media); |
| 47 | expect(microphone).toHaveBeenLastCalledWith({ audio: true }); |
| 48 | |
| 49 | const denied = new DOMException("denied", "NotAllowedError"); |
| 50 | microphone.mockRejectedValueOnce(denied); |
| 51 | await expect(requestMicrophone("blue-yeti")).rejects.toBe(denied); |
| 52 | expect(microphone).toHaveBeenCalledTimes(3); |
| 53 | }); |