The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { VoiceInputController, VoiceTranscriptionError } from "@t3tools/client-runtime/voice-input"; |
| 4 | import { resetVoiceInputGlobalsForTests } from "../../../../packages/client-runtime/src/voice-input/controller"; |
| 5 | |
| 6 | const mocks = vi.hoisted(() => ({ |
| 7 | isAvailable: vi.fn<() => boolean>(), |
| 8 | prepare: vi.fn<(locale: string) => Promise<string | null>>(), |
| 9 | transcribe: vi.fn<(uri: string, locale: string) => Promise<string>>(), |
| 10 | preferredLanguages: [] as unknown, |
| 11 | })); |
| 12 | |
| 13 | // iOS exposes the user's ordered Language & Region list through NSUserDefaults. |
| 14 | vi.mock("react-native", () => ({ |
| 15 | Settings: { get: (key: string) => (key === "AppleLanguages" ? mocks.preferredLanguages : null) }, |
| 16 | })); |
| 17 | |
| 18 | vi.mock("expo", () => ({ |
| 19 | requireOptionalNativeModule: () => ({ |
| 20 | isAppleSpeechAvailable: mocks.isAvailable, |
| 21 | prepareAppleSpeech: mocks.prepare, |
| 22 | transcribeWithAppleSpeech: mocks.transcribe, |
| 23 | }), |
| 24 | })); |
| 25 | |
| 26 | import { getLocalVoiceTranscriber } from "./voiceTranscription.ios"; |
| 27 | |
| 28 | function useLanguages(preferred: ReadonlyArray<string>, appLocale: string) { |
| 29 | mocks.preferredLanguages = preferred; |
| 30 | const resolvedOptions = Intl.DateTimeFormat().resolvedOptions(); |
| 31 | vi.spyOn(Intl.DateTimeFormat.prototype, "resolvedOptions").mockReturnValue({ |
| 32 | ...resolvedOptions, |
| 33 | locale: appLocale, |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | /** Prepares and transcribes one recording, resolving with the error when either step fails. */ |
| 38 | async function transcribeOnce(signal = new AbortController().signal) { |
| 39 | const transcriber = (await getLocalVoiceTranscriber())!; |
| 40 | return transcriber |
| 41 | .prepare({ signal }) |
| 42 | .then(async (prepared) => { |
| 43 | const text = await prepared.transcribe("file:///voice.m4a", { signal }); |
| 44 | return { text, locale: prepared.locale }; |
| 45 | }) |
| 46 | .catch((error: unknown) => error); |
| 47 | } |
| 48 | |
| 49 | /** Drives one on-device dictation through the controller the composer uses. */ |
| 50 | function createDictation() { |
| 51 | const recorder = { |
| 52 | uri: "file:///voice.m4a", |
| 53 | prepareToRecordAsync: async () => {}, |
| 54 | record: vi.fn(), |
| 55 | stop: vi.fn(async () => {}), |
| 56 | }; |
| 57 | const commits: string[] = []; |
| 58 | const deleted: string[] = []; |
| 59 | const ended = Promise.withResolvers<void>(); |
| 60 | const controller = new VoiceInputController({ |
| 61 | recorder, |
| 62 | getTranscriber: () => ({ |
| 63 | prepare: async (options) => (await getLocalVoiceTranscriber())!.prepare(options), |
| 64 | }), |
| 65 | requestPermission: async () => ({ granted: true, canAskAgain: true }), |
| 66 | configureRecording: async () => {}, |
| 67 | releaseRecording: async () => {}, |
| 68 | deleteRecording: (uri) => deleted.push(uri), |
| 69 | readDraft: () => ({ |
| 70 | ownerKey: "environment:thread", |
| 71 | text: "", |
| 72 | selection: { start: 0, end: 0 }, |
| 73 | revision: 1, |
| 74 | }), |
| 75 | commitDraft: (text) => commits.push(text), |
| 76 | onStateChange: (state) => { |
| 77 | if (state.phase === "idle" || state.phase === "error") ended.resolve(); |
| 78 | }, |
| 79 | }); |
| 80 | return { controller, recorder, commits, deleted, ended: ended.promise }; |
| 81 | } |
| 82 | |
| 83 | beforeEach(() => { |
| 84 | vi.resetAllMocks(); |
| 85 | resetVoiceInputGlobalsForTests(); |
| 86 | useLanguages([], "sv-SE"); |
| 87 | mocks.isAvailable.mockReturnValue(true); |
| 88 | mocks.prepare.mockResolvedValue("sv-SE"); |
| 89 | mocks.transcribe.mockResolvedValue("Hej världen."); |
| 90 | }); |
| 91 | |
| 92 | afterEach(() => { |
| 93 | vi.restoreAllMocks(); |
| 94 | }); |
| 95 | |
| 96 | describe("getLocalVoiceTranscriber", () => { |
| 97 | it.each([ |
| 98 | { |
| 99 | case: "a spoken language ahead of the English app locale", |
| 100 | preferred: ["ko-KR", "en-US"], |
| 101 | supported: ["ko-KR", "en-US"], |
| 102 | tried: ["ko-KR"], |
| 103 | }, |
| 104 | { |
| 105 | case: "the next preferred language after an unsupported one", |
| 106 | preferred: ["gsw-CH", "de-CH", "en-US"], |
| 107 | supported: ["de-CH", "en-US"], |
| 108 | tried: ["gsw-CH", "de-CH"], |
| 109 | }, |
| 110 | { |
| 111 | case: "the app locale when no preferred language is supported", |
| 112 | preferred: ["gsw-CH", "yi"], |
| 113 | supported: ["en-US"], |
| 114 | tried: ["gsw-CH", "yi", "en-US"], |
| 115 | }, |
| 116 | ])("prepares $case", async ({ preferred, supported, tried }) => { |
| 117 | useLanguages(preferred, "en-US"); |
| 118 | mocks.prepare.mockImplementation(async (candidate) => { |
| 119 | if (!supported.includes(candidate)) return null; |
| 120 | return candidate; |
| 121 | }); |
| 122 | |
| 123 | const locale = tried.at(-1)!; |
| 124 | expect(await transcribeOnce()).toEqual({ text: "Hej världen.", locale }); |
| 125 | expect(mocks.prepare.mock.calls.map(([candidate]) => candidate)).toEqual(tried); |
| 126 | expect(mocks.transcribe).toHaveBeenCalledWith("file:///voice.m4a", locale); |
| 127 | }); |
| 128 | |
| 129 | it("reports an unsupported language when neither preferred nor app locales are supported", async () => { |
| 130 | useLanguages(["gsw-CH"], "yi"); |
| 131 | mocks.prepare.mockResolvedValue(null); |
| 132 | |
| 133 | expect(await transcribeOnce()).toMatchObject({ code: "unsupported-locale" }); |
| 134 | expect(mocks.prepare.mock.calls.map(([candidate]) => candidate)).toEqual(["gsw-CH", "yi"]); |
| 135 | }); |
| 136 | |
| 137 | it.each(["cancellation", "a model failure"] as const)( |
| 138 | "does not fall back to another language after %s", |
| 139 | async (failure) => { |
| 140 | useLanguages(["ko-KR", "ja-JP"], "en-US"); |
| 141 | const entered = Promise.withResolvers<void>(); |
| 142 | const nativePrepare = Promise.withResolvers<string | null>(); |
| 143 | mocks.prepare.mockImplementation(() => { |
| 144 | entered.resolve(); |
| 145 | return nativePrepare.promise; |
| 146 | }); |
| 147 | const controller = new AbortController(); |
| 148 | const result = transcribeOnce(controller.signal); |
| 149 | |
| 150 | await entered.promise; |
| 151 | if (failure === "cancellation") { |
| 152 | controller.abort(); |
| 153 | nativePrepare.resolve(null); |
| 154 | } else { |
| 155 | nativePrepare.reject(Object.assign(new Error("download failed"), { code: "Network" })); |
| 156 | } |
| 157 | |
| 158 | expect(await result).toMatchObject({ |
| 159 | code: failure === "cancellation" ? "cancelled" : "preparation-failed", |
| 160 | }); |
| 161 | expect(mocks.prepare.mock.calls.map(([candidate]) => candidate)).toEqual(["ko-KR"]); |
| 162 | }, |
| 163 | ); |
| 164 | |
| 165 | it("records while Apple prepares its speech model and transcribes once it is ready", async () => { |
| 166 | const nativePrepare = Promise.withResolvers<string | null>(); |
| 167 | mocks.prepare.mockReturnValue(nativePrepare.promise); |
| 168 | const dictation = createDictation(); |
| 169 | |
| 170 | await dictation.controller.start(); |
| 171 | expect(dictation.recorder.record).toHaveBeenCalledOnce(); |
| 172 | expect(dictation.controller.currentState.phase).toBe("recording"); |
| 173 | |
| 174 | const stopping = dictation.controller.stop(); |
| 175 | nativePrepare.resolve("sv-SE"); |
| 176 | await stopping; |
| 177 | |
| 178 | expect(mocks.transcribe).toHaveBeenCalledOnce(); |
| 179 | expect(dictation.commits).toEqual(["Hej världen."]); |
| 180 | expect(dictation.deleted).toEqual(["file:///voice.m4a"]); |
| 181 | }); |
| 182 | |
| 183 | it.each(["cancel", "cancel after stop", "model failure", "model failure after stop"] as const)( |
| 184 | "discards early audio without a late commit after %s", |
| 185 | async (ending) => { |
| 186 | const nativePrepare = Promise.withResolvers<string | null>(); |
| 187 | mocks.prepare.mockReturnValue(nativePrepare.promise); |
| 188 | const dictation = createDictation(); |
| 189 | await dictation.controller.start(); |
| 190 | |
| 191 | let finishing: Promise<void> | undefined; |
| 192 | const cancelled = ending === "cancel" || ending === "cancel after stop"; |
| 193 | if (ending.endsWith("after stop")) finishing = dictation.controller.stop(); |
| 194 | if (cancelled) { |
| 195 | dictation.controller.cancel(); |
| 196 | } else { |
| 197 | nativePrepare.reject(Object.assign(new Error("download failed"), { code: "Network" })); |
| 198 | } |
| 199 | await finishing; |
| 200 | await dictation.ended; |
| 201 | |
| 202 | const next = createDictation(); |
| 203 | mocks.prepare.mockResolvedValue("sv-SE"); |
| 204 | await next.controller.start(); |
| 205 | expect(next.controller.currentState.phase).toBe("recording"); |
| 206 | expect(dictation.deleted).toEqual(["file:///voice.m4a"]); |
| 207 | expect(mocks.transcribe).not.toHaveBeenCalled(); |
| 208 | await next.controller.stop(); |
| 209 | expect(next.commits).toEqual(["Hej världen."]); |
| 210 | nativePrepare.resolve("sv-SE"); |
| 211 | |
| 212 | expect(dictation.commits).toEqual([]); |
| 213 | expect(dictation.recorder.stop).toHaveBeenCalled(); |
| 214 | expect(dictation.controller.currentState).toMatchObject( |
| 215 | cancelled |
| 216 | ? { phase: "idle" } |
| 217 | : { phase: "error", error: "Voice transcription could not prepare this language." }, |
| 218 | ); |
| 219 | }, |
| 220 | ); |
| 221 | |
| 222 | it("discards transcription if cancelled while the native module reads the recording", async () => { |
| 223 | const entered = Promise.withResolvers<void>(); |
| 224 | const transcription = Promise.withResolvers<string>(); |
| 225 | mocks.transcribe.mockImplementation(() => { |
| 226 | entered.resolve(); |
| 227 | return transcription.promise; |
| 228 | }); |
| 229 | const controller = new AbortController(); |
| 230 | const options = { signal: controller.signal }; |
| 231 | const prepared = await (await getLocalVoiceTranscriber())!.prepare(options); |
| 232 | const result = prepared |
| 233 | .transcribe("file:///voice.m4a", options) |
| 234 | .catch((error: unknown) => error); |
| 235 | |
| 236 | await entered.promise; |
| 237 | controller.abort(); |
| 238 | transcription.resolve("Hej världen."); |
| 239 | |
| 240 | const error = await result; |
| 241 | expect(error).toBeInstanceOf(VoiceTranscriptionError); |
| 242 | expect(error).toMatchObject({ code: "cancelled" }); |
| 243 | }); |
| 244 | }); |