11import { beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
22import type { PreparedVoiceTranscription } from "@t3tools/client-runtime/voice-input" ;
3- import { Atom , AtomRegistry } from "effect/reactivity" ;
43import { resetVoiceInputGlobalsForTests } from "../../../../../packages/client-runtime/src/voice-input/controller" ;
54import {
65 acquireAudioSession ,
@@ -23,19 +22,9 @@ import {
2322 createVoiceInputTarget ,
2423 VoiceInputSession ,
2524 type VoiceInputSessionDependencies ,
26- type VoiceInputTarget ,
2725 type VoiceRecordingSample ,
2826} from "./voiceInputSession" ;
2927
30- function createTarget (
31- ownerKey : string ,
32- readText : ( ) => string | null ,
33- commit : VoiceInputTarget [ "commitDraft" ] ,
34- selection : { start : number ; end : number } ,
35- ) {
36- return createVoiceInputTarget ( ownerKey , readText , commit , selection , ( ) => ( ) => { } ) ;
37- }
38-
3928function createSession (
4029 overrides ?: Partial <
4130 Pick <
@@ -116,7 +105,7 @@ describe("global voice input", () => {
116105 if ( outcome === "activation failure" ) {
117106 audio . setActive . mockRejectedValueOnce ( new Error ( "Audio activation failed" ) ) ;
118107 }
119- await session . start ( createTarget ( "first" , ( ) => "" , vi . fn ( ) , { start : 0 , end : 0 } ) ) ;
108+ await session . start ( createVoiceInputTarget ( "first" , ( ) => "" , vi . fn ( ) , { start : 0 , end : 0 } ) ) ;
120109 expect ( pause ) . toHaveBeenCalledOnce ( ) ;
121110 expect ( isAudioSessionOwner ( playback ) ) . toBe ( false ) ;
122111 if ( outcome === "complete" ) {
@@ -154,9 +143,9 @@ describe("global voice input", () => {
154143 async ( { selection, expected, cursor } ) => {
155144 const { session } = createSession ( ) ;
156145 const commit = vi . fn ( ) ;
157- await session . start ( createTarget ( "first" , ( ) => "hello world" , commit , selection ) ) ;
146+ await session . start ( createVoiceInputTarget ( "first" , ( ) => "hello world" , commit , selection ) ) ;
158147 await session . start (
159- createTarget ( "second" , ( ) => "other prompt" , vi . fn ( ) , { start : 12 , end : 12 } ) ,
148+ createVoiceInputTarget ( "second" , ( ) => "other prompt" , vi . fn ( ) , { start : 12 , end : 12 } ) ,
160149 ) ;
161150 await session . controller . stop ( ) ;
162151 expect ( commit ) . toHaveBeenCalledWith ( expected , {
@@ -175,7 +164,7 @@ describe("global voice input", () => {
175164 let visibleDraft = "first" ;
176165 const targetKey = visibleDraft ;
177166 await session . start (
178- createTarget (
167+ createVoiceInputTarget (
179168 targetKey ,
180169 ( ) => drafts . get ( targetKey ) ?? null ,
181170 ( text ) => drafts . set ( targetKey , text ) ,
@@ -207,7 +196,7 @@ describe("global voice input", () => {
207196 const firstCommit = vi . fn ( ) ;
208197 const secondCommit = vi . fn ( ) ;
209198 const starting = session . start (
210- createTarget ( "first" , ( ) => "first" , firstCommit , { start : 5 , end : 5 } ) ,
199+ createVoiceInputTarget ( "first" , ( ) => "first" , firstCommit , { start : 5 , end : 5 } ) ,
211200 ) ;
212201 await preparationEntered . promise ;
213202 let stopping : Promise < void > | null = null ;
@@ -226,7 +215,7 @@ describe("global voice input", () => {
226215 await transcriptionEntered . promise ;
227216 }
228217 await session . start (
229- createTarget ( "second" , ( ) => "second" , secondCommit , { start : 6 , end : 6 } ) ,
218+ createVoiceInputTarget ( "second" , ( ) => "second" , secondCommit , { start : 6 , end : 6 } ) ,
230219 ) ;
231220 expect ( session . ownerKey ) . toBe ( "first" ) ;
232221 expect ( session . controller . currentState . phase ) . toBe ( phase ) ;
@@ -247,18 +236,36 @@ describe("global voice input", () => {
247236 const { session } = createSession ( ) ;
248237 let text : string | null = "first" ;
249238 const commit = vi . fn ( ) ;
250- await session . start ( createTarget ( "first" , ( ) => text , commit , { start : 5 , end : 5 } ) ) ;
239+ await session . start (
240+ createVoiceInputTarget ( "first" , ( ) => text , commit , { start : 5 , end : 5 } ) ,
241+ ) ;
251242 text = change === "removed" ? null : "edited prompt" ;
252243 await session . controller . stop ( ) ;
253244 expect ( commit ) . not . toHaveBeenCalled ( ) ;
254245 expect ( session . controller . currentState . error ) . toContain ( "draft changed" ) ;
255246 } ,
256247 ) ;
257248
249+ it ( "retries a failed start against the draft as edited since" , async ( ) => {
250+ const { session, prepare } = createSession ( ) ;
251+ prepare . mockRejectedValueOnce ( new Error ( "model unavailable" ) ) ;
252+ let text = "first" ;
253+ const commit = vi . fn ( ) ;
254+ await session . start ( createVoiceInputTarget ( "first" , ( ) => text , commit , { start : 5 , end : 5 } ) ) ;
255+ expect ( session . controller . currentState . phase ) . toBe ( "error" ) ;
256+ text = "first draft" ;
257+ await session . retry ( ) ;
258+ await session . controller . stop ( ) ;
259+ expect ( prepare ) . toHaveBeenCalledTimes ( 2 ) ;
260+ expect ( commit ) . toHaveBeenCalledWith ( "first spoken text draft" , { start : 17 , end : 17 } ) ;
261+ } ) ;
262+
258263 it ( "finishes the original draft at the recording limit while it is off screen" , async ( ) => {
259264 const { session, recorder } = createSession ( ) ;
260265 const commit = vi . fn ( ) ;
261- await session . start ( createTarget ( "first" , ( ) => "first" , commit , { start : 5 , end : 5 } ) ) ;
266+ await session . start (
267+ createVoiceInputTarget ( "first" , ( ) => "first" , commit , { start : 5 , end : 5 } ) ,
268+ ) ;
262269 await session . controller . handleRecorderStatus ( {
263270 isFinished : true ,
264271 hasError : false ,
@@ -269,41 +276,14 @@ describe("global voice input", () => {
269276 expect ( session . controller . currentState . phase ) . toBe ( "idle" ) ;
270277 } ) ;
271278
272- it ( "rejects a transcript when its off-screen draft changes and returns to the original text" , async ( ) => {
273- const registry = AtomRegistry . make ( ) ;
274- const draft = Atom . make ( "hello world" ) ;
275- const unsubscribe = vi . fn ( ) ;
276- const { session } = createSession ( ) ;
277- const commit = vi . fn ( ) ;
278- const target = createVoiceInputTarget (
279- "first" ,
280- ( ) => registry . get ( draft ) ,
281- commit ,
282- { start : 6 , end : 6 } ,
283- ( onChange ) => {
284- const stop = registry . subscribe ( draft , onChange ) ;
285- return ( ) => {
286- stop ( ) ;
287- unsubscribe ( ) ;
288- } ;
289- } ,
290- ) ;
291- await session . start ( target ) ;
292- registry . set ( draft , "changed" ) ;
293- registry . set ( draft , "hello world" ) ;
294- await session . controller . stop ( ) ;
295- expect ( commit ) . not . toHaveBeenCalled ( ) ;
296- expect ( session . controller . currentState . error ) . toContain ( "draft changed" ) ;
297- expect ( unsubscribe ) . toHaveBeenCalledTimes ( 1 ) ;
298- registry . dispose ( ) ;
299- } ) ;
300-
301279 it ( "stops recording when its queued edit is discarded" , async ( ) => {
302280 const { session, recorder } = createSession ( ) ;
303281 const commit = vi . fn ( ) ;
304282 let draft : string | null = "queued prompt" ;
305283 const ownerKey = "thread~queued-edit~run" ;
306- await session . start ( createTarget ( ownerKey , ( ) => draft , commit , { start : 13 , end : 13 } ) ) ;
284+ await session . start (
285+ createVoiceInputTarget ( ownerKey , ( ) => draft , commit , { start : 13 , end : 13 } ) ,
286+ ) ;
307287 session . cancel ( ownerKey ) ;
308288 draft = null ;
309289 await session . controller . stop ( ) ;
@@ -319,7 +299,9 @@ describe("global voice input", () => {
319299 it ( "keeps another prompt's recording when a queued edit is discarded" , async ( ) => {
320300 const { session, recorder } = createSession ( ) ;
321301 const commit = vi . fn ( ) ;
322- await session . start ( createTarget ( "other prompt" , ( ) => "hello" , commit , { start : 5 , end : 5 } ) ) ;
302+ await session . start (
303+ createVoiceInputTarget ( "other prompt" , ( ) => "hello" , commit , { start : 5 , end : 5 } ) ,
304+ ) ;
323305 session . cancel ( "thread~queued-edit~run" ) ;
324306 expect ( recorder . stop ) . not . toHaveBeenCalled ( ) ;
325307 expect ( session . controller . currentState . phase ) . toBe ( "recording" ) ;
@@ -338,12 +320,12 @@ describe("global voice input", () => {
338320 const oldCommit = vi . fn ( ) ;
339321 const nextCommit = vi . fn ( ) ;
340322 const firstStart = session . start (
341- createTarget ( "first" , ( ) => "first" , oldCommit , { start : 5 , end : 5 } ) ,
323+ createVoiceInputTarget ( "first" , ( ) => "first" , oldCommit , { start : 5 , end : 5 } ) ,
342324 ) ;
343325 await preparationEntered . promise ;
344326 session . cancel ( "first" ) ;
345327 const nextStart = session . start (
346- createTarget ( "second" , ( ) => "second" , nextCommit , { start : 6 , end : 6 } ) ,
328+ createVoiceInputTarget ( "second" , ( ) => "second" , nextCommit , { start : 6 , end : 6 } ) ,
347329 ) ;
348330 expect ( prepare ) . toHaveBeenCalledTimes ( 1 ) ;
349331 expect ( recorder . record ) . not . toHaveBeenCalled ( ) ;
@@ -366,7 +348,9 @@ describe("global voice input", () => {
366348 const snapshots : number [ ] = [ ] ;
367349 session . subscribe ( ( ) => snapshots . push ( session . getSnapshot ( ) . elapsedSeconds ) ) ;
368350
369- await session . start ( createTarget ( "first" , ( ) => "first" , vi . fn ( ) , { start : 5 , end : 5 } ) ) ;
351+ await session . start (
352+ createVoiceInputTarget ( "first" , ( ) => "first" , vi . fn ( ) , { start : 5 , end : 5 } ) ,
353+ ) ;
370354 expect ( session . getSnapshot ( ) . state . phase ) . toBe ( "recording" ) ;
371355 expect ( keepAwake ) . toHaveBeenCalledTimes ( 1 ) ;
372356 // Silence on a silent history publishes nothing.
@@ -388,7 +372,9 @@ describe("global voice input", () => {
388372 expect ( session . getSnapshot ( ) . elapsedSeconds ) . toBe ( 2 ) ;
389373
390374 sample = { durationMillis : 0 , metering : - 100 } ;
391- await session . start ( createTarget ( "first" , ( ) => "first" , vi . fn ( ) , { start : 5 , end : 5 } ) ) ;
375+ await session . start (
376+ createVoiceInputTarget ( "first" , ( ) => "first" , vi . fn ( ) , { start : 5 , end : 5 } ) ,
377+ ) ;
392378 expect ( session . getSnapshot ( ) . elapsedSeconds ) . toBe ( 0 ) ;
393379 expect ( publishAudioLevels ) . toHaveBeenLastCalledWith ( expect . not . arrayContaining ( [ 1 ] ) ) ;
394380 expect ( keepAwake ) . toHaveBeenCalledTimes ( 2 ) ;
0 commit comments