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, 03:37 UTC
| 1 | import { expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | const mocks = vi.hoisted(() => ({ |
| 4 | checkAvailability: vi.fn<() => Promise<boolean>>(), |
| 5 | prepare: vi.fn<(id: number) => Promise<{ locale: string; mode: number }>>(), |
| 6 | transcribe: vi.fn<(id: number, uri: string, locale: string, mode: number) => Promise<string>>(), |
| 7 | cancel: vi.fn<(id: number) => Promise<void>>(), |
| 8 | LiveVoiceRecorder: class { |
| 9 | readonly uri = "file:///live.pcm"; |
| 10 | }, |
| 11 | })); |
| 12 | |
| 13 | vi.mock("react-native", () => ({ |
| 14 | Platform: { |
| 15 | get Version() { |
| 16 | return 31; |
| 17 | }, |
| 18 | }, |
| 19 | })); |
| 20 | |
| 21 | vi.mock("expo", () => ({ |
| 22 | requireOptionalNativeModule: () => ({ isAvailable: () => true, ...mocks }), |
| 23 | })); |
| 24 | |
| 25 | import { |
| 26 | VoiceInputController, |
| 27 | resetVoiceInputGlobalsForTests, |
| 28 | } from "../../../../packages/client-runtime/src/voice-input/controller"; |
| 29 | import { Platform } from "react-native"; |
| 30 | |
| 31 | import { createLiveVoiceRecorder, getLocalVoiceTranscriber } from "./voiceTranscription.android"; |
| 32 | |
| 33 | it.each([ |
| 34 | [30, null], |
| 35 | [31, "file:///live.pcm"], |
| 36 | ] as const)("offers live recording only on supported Android versions (%s)", (version, uri) => { |
| 37 | using _version = vi.spyOn(Platform, "Version", "get").mockReturnValue(version); |
| 38 | expect(createLiveVoiceRecorder()?.uri ?? null).toBe(uri); |
| 39 | }); |
| 40 | |
| 41 | it.each([false, true])( |
| 42 | "waits for device support before offering a transcriber (%s)", |
| 43 | async (supported) => { |
| 44 | vi.resetAllMocks(); |
| 45 | const probe = Promise.withResolvers<boolean>(); |
| 46 | mocks.checkAvailability.mockReturnValue(probe.promise); |
| 47 | let settled = false; |
| 48 | const result = Promise.resolve(getLocalVoiceTranscriber()).then((transcriber) => { |
| 49 | settled = true; |
| 50 | return transcriber; |
| 51 | }); |
| 52 | await Promise.resolve(); |
| 53 | expect(settled).toBe(false); |
| 54 | probe.resolve(supported); |
| 55 | expect((await result) !== null).toBe(supported); |
| 56 | }, |
| 57 | ); |
| 58 | |
| 59 | it.each([ |
| 60 | ["preparation", "storage", /disk|storage|space/i], |
| 61 | ["preparation", "quota", /quota|limit/i], |
| 62 | ["transcription", "storage", /disk|storage|space/i], |
| 63 | ["transcription", "quota", /quota|limit/i], |
| 64 | ] as const)("preserves an actionable %s error: %s", async (phase, code, expected) => { |
| 65 | vi.resetAllMocks(); |
| 66 | resetVoiceInputGlobalsForTests(); |
| 67 | mocks.checkAvailability.mockResolvedValue(true); |
| 68 | mocks.prepare.mockResolvedValue({ locale: "en-US", mode: 0 }); |
| 69 | mocks.transcribe.mockResolvedValue("Complete transcript."); |
| 70 | mocks.cancel.mockResolvedValue(undefined); |
| 71 | const rejection = Object.assign(new Error("Native SDK failure"), { code }); |
| 72 | (phase === "preparation" ? mocks.prepare : mocks.transcribe).mockRejectedValue(rejection); |
| 73 | const transcriber = await getLocalVoiceTranscriber(); |
| 74 | const commit = vi.fn(); |
| 75 | const draft = { |
| 76 | ownerKey: "acceptance:dictation", |
| 77 | text: "Preserved draft.", |
| 78 | selection: { start: 16, end: 16 }, |
| 79 | revision: 1, |
| 80 | }; |
| 81 | const prepareToRecordAsync = vi.fn(async () => {}); |
| 82 | const controller = new VoiceInputController({ |
| 83 | recorder: { |
| 84 | uri: "file:///acceptance.m4a", |
| 85 | prepareToRecordAsync, |
| 86 | record: () => {}, |
| 87 | stop: async () => {}, |
| 88 | }, |
| 89 | getTranscriber: () => transcriber, |
| 90 | requestPermission: async () => ({ granted: true, canAskAgain: true }), |
| 91 | configureRecording: async () => {}, |
| 92 | releaseRecording: async () => {}, |
| 93 | deleteRecording: () => {}, |
| 94 | readDraft: () => draft, |
| 95 | commitDraft: commit, |
| 96 | onStateChange: () => {}, |
| 97 | }); |
| 98 | await controller.start(); |
| 99 | if (phase === "transcription") await controller.stop(); |
| 100 | |
| 101 | expect(controller.currentState).toMatchObject({ phase: "error", errorAction: "retry" }); |
| 102 | expect(commit).not.toHaveBeenCalled(); |
| 103 | expect(draft.text).toBe("Preserved draft."); |
| 104 | expect(controller.currentState.error).toMatch(expected); |
| 105 | expect(prepareToRecordAsync).toHaveBeenCalledTimes(phase === "preparation" ? 0 : 1); |
| 106 | }); |