11import type { TranscriptionResult } from "@react-native-ai/apple/src/NativeAppleTranscription" ;
22import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
33
4- import { VoiceTranscriptionError } from "@t3tools/client-runtime/voice-input" ;
4+ import { VoiceInputController , VoiceTranscriptionError } from "@t3tools/client-runtime/voice-input" ;
5+ import { resetVoiceInputGlobalsForTests } from "../../../../packages/client-runtime/src/voice-input/controller" ;
56
67const mocks = vi . hoisted ( ( ) => ( {
78 isAvailable : vi . fn < ( locale : string ) => boolean > ( ) ,
89 prepare : vi . fn < ( locale : string ) => Promise < string > > ( ) ,
910 transcribe : vi . fn < ( audio : ArrayBufferLike , locale : string ) => Promise < TranscriptionResult > > ( ) ,
1011 readAudio : vi . fn < ( ) => Promise < ArrayBuffer > > ( ) ,
12+ preferredLanguages : [ ] as unknown ,
13+ } ) ) ;
14+
15+ // iOS exposes the user's ordered Language & Region list through NSUserDefaults.
16+ vi . mock ( "react-native" , ( ) => ( {
17+ Settings : { get : ( key : string ) => ( key === "AppleLanguages" ? mocks . preferredLanguages : null ) } ,
1118} ) ) ;
1219
1320vi . mock ( "@react-native-ai/apple/src/NativeAppleTranscription" , ( ) => ( {
@@ -35,16 +42,68 @@ const nativeTranscript: TranscriptionResult = {
3542 ] ,
3643} ;
3744
38- function deferred < T > ( ) {
39- let resolve ! : ( value : T ) => void ;
40- const promise = new Promise < T > ( ( next ) => {
41- resolve = next ;
45+ const unsupportedLocale = ( ) =>
46+ Object . assign ( new Error ( "unsupported" ) , { code : "AppleTranscriptionUnsupportedLocale" } ) ;
47+
48+ function 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. */
58+ async 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. */
70+ function 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+ } ,
4299 } ) ;
43- return { promise , resolve } ;
100+ return { controller , recorder , commits , deleted , ended : ended . promise } ;
44101}
45102
46103beforeEach ( ( ) => {
47104 vi . resetAllMocks ( ) ;
105+ resetVoiceInputGlobalsForTests ( ) ;
106+ useLanguages ( [ ] , "sv-SE" ) ;
48107 mocks . isAvailable . mockReturnValue ( true ) ;
49108 mocks . prepare . mockResolvedValue ( "sv-SE" ) ;
50109 mocks . readAudio . mockResolvedValue ( audio ) ;
@@ -56,9 +115,134 @@ afterEach(() => {
56115} ) ;
57116
58117describe ( "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+
59243 it ( "does not start native transcription after cancellation during a file read" , async ( ) => {
60- const enteredRead = deferred < void > ( ) ;
61- const readResult = deferred < ArrayBuffer > ( ) ;
244+ const enteredRead = Promise . withResolvers < void > ( ) ;
245+ const readResult = Promise . withResolvers < ArrayBuffer > ( ) ;
62246 mocks . readAudio . mockImplementation ( ( ) => {
63247 enteredRead . resolve ( ) ;
64248 return readResult . promise ;
0 commit comments