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