@@ -4,6 +4,7 @@ import { page, userEvent } from "vite-plus/test/browser";
44import * as Deferred from "effect/Deferred" ;
55import * as Effect from "effect/Effect" ;
66import * as Option from "effect/Option" ;
7+ import { AsyncResult , Atom } from "effect/reactivity" ;
78import type { PreparedConnection } from "@t3tools/client-runtime/connection" ;
89import { getEnvironmentSpeechModels } from "@t3tools/client-runtime/voice-input" ;
910import {
@@ -17,7 +18,7 @@ import {
1718 type SpeechTranscriptionOptions ,
1819} from "@t3tools/contracts" ;
1920
20- import { act , render , stubGlobal } from "../../test/render" ;
21+ import { act , fakeTimers , render , stubGlobal } from "../../test/render" ;
2122import { VoiceInputSetup } from "../components/chat/VoiceInputSetup" ;
2223import type { createBrowserVoiceInputPlatform } from "./browserVoiceInput" ;
2324import { useEnvironmentSpeechInput } from "./useEnvironmentSpeechInput" ;
@@ -28,6 +29,7 @@ import {
2829 pendingCloudAudioChoice ,
2930} from "./voiceConsent" ;
3031import { writeBrowserClientSettings } from "../clientPersistenceStorage" ;
32+ import { AppAtomRegistryProvider } from "../rpc/atomRegistry" ;
3133
3234const mocks = vi . hoisted ( ( ) => ( {
3335 voice : undefined as unknown as ReturnType < typeof useEnvironmentSpeechInput > ,
@@ -36,6 +38,7 @@ const mocks = vi.hoisted(() => ({
3638 voiceCapable : new Set < string > ( ) ,
3739 cloudVoiceCapable : new Set < string > ( ) ,
3840 canDictate : new Set < string > ( ) ,
41+ sessionAtoms : new Map < string , Atom . Atom < AsyncResult . AsyncResult < boolean > > > ( ) ,
3942 prepared : new Map < string , unknown > ( ) ,
4043 primaryEnvironmentId : "primary-env" ,
4144 statusRequests : [ ] as string [ ] ,
@@ -62,18 +65,33 @@ const mocks = vi.hoisted(() => ({
6265 drafts : { } as Record < string , string > ,
6366} ) ) ;
6467
65- vi . mock ( "../state/session" , ( ) => ( {
66- useEnvironmentScope : ( id : string ) => mocks . canDictate . has ( id ) ,
67- usePreparedConnection : ( environmentId : string | null ) => {
68- if ( ! environmentId || ! mocks . connected . has ( environmentId ) ) return Option . none ( ) ;
69- let prepared = mocks . prepared . get ( environmentId ) ;
70- if ( ! prepared ) {
71- prepared = { environmentId , label : environmentId } ;
72- mocks . prepared . set ( environmentId , prepared ) ;
68+ vi . mock ( "../state/session" , async ( ) => {
69+ const { useAtomValue } = await import ( "@effect/atom-react" ) ;
70+ const { AsyncResult , Atom } = await import ( "effect/reactivity" ) ;
71+ const sessionStateAtom = ( id : string ) => {
72+ let atom = mocks . sessionAtoms . get ( id ) ;
73+ if ( ! atom ) {
74+ atom = Atom . make ( AsyncResult . success ( true ) ) ;
75+ mocks . sessionAtoms . set ( id , atom ) ;
7376 }
74- return Option . some ( prepared ) ;
75- } ,
76- } ) ) ;
77+ return atom ;
78+ } ;
79+ return {
80+ environmentSession : { sessionStateAtom } ,
81+ useEnvironmentScope : ( id : string ) =>
82+ AsyncResult . getOrElse ( useAtomValue ( sessionStateAtom ( id ) ) , ( ) => false ) &&
83+ mocks . canDictate . has ( id ) ,
84+ usePreparedConnection : ( environmentId : string | null ) => {
85+ if ( ! environmentId || ! mocks . connected . has ( environmentId ) ) return Option . none ( ) ;
86+ let prepared = mocks . prepared . get ( environmentId ) ;
87+ if ( ! prepared ) {
88+ prepared = { environmentId, label : environmentId } ;
89+ mocks . prepared . set ( environmentId , prepared ) ;
90+ }
91+ return Option . some ( prepared ) ;
92+ } ,
93+ } ;
94+ } ) ;
7795vi . mock ( "../state/environments" , ( ) => ( {
7896 usePrimaryEnvironmentId : ( ) => mocks . primaryEnvironmentId ,
7997 useEnvironment : ( environmentId : string | null ) =>
@@ -215,6 +233,7 @@ function setup(options: { readonly destination?: string | null } = {}) {
215233 mocks . voiceCapable = new Set ( [ "primary-env" , THREAD_ENV , "env-a" , "env-b" ] ) ;
216234 mocks . cloudVoiceCapable . clear ( ) ;
217235 mocks . canDictate = new Set ( mocks . voiceCapable ) ;
236+ mocks . sessionAtoms . clear ( ) ;
218237 mocks . prepared . clear ( ) ;
219238 mocks . statusRequests = [ ] ;
220239 mocks . statusUnavailable = false ;
@@ -365,7 +384,15 @@ it.each([
365384 expect ( mocks . committed ) . not . toHaveBeenCalled ( ) ;
366385} ) ;
367386
368- function SubmissionProbe ( {
387+ function SubmissionProbe ( props : Parameters < typeof SubmissionProbeContent > [ 0 ] ) {
388+ return (
389+ < AppAtomRegistryProvider >
390+ < SubmissionProbeContent { ...props } />
391+ </ AppAtomRegistryProvider >
392+ ) ;
393+ }
394+
395+ function SubmissionProbeContent ( {
369396 owner,
370397 environmentId = THREAD_ENV ,
371398} : {
@@ -439,10 +466,21 @@ it.each([
439466 [ "transcribing" , "cancel" ] ,
440467 [ "post-processing" , "cancel" ] ,
441468 [ "post-processing" , "skip" ] ,
469+ [ "transcribing" , "finish after session expiry" ] ,
442470] as const ) (
443471 "reattaches a confirmed %s submission after remount to %s it" ,
444472 async ( phase , action ) => {
445473 setup ( { destination : "env-a" } ) ;
474+ const expires = action === "finish after session expiry" ;
475+ if ( expires ) {
476+ let reads = 0 ;
477+ mocks . sessionAtoms . set (
478+ "env-a" ,
479+ Atom . make ( Effect . suspend ( ( ) => ( ++ reads === 1 ? Effect . succeed ( true ) : Effect . never ) ) ) . pipe (
480+ Atom . setIdleTTL ( 5 * 60_000 ) ,
481+ ) ,
482+ ) ;
483+ }
446484 let finish ! : ( text : string ) => void ;
447485 const result = new Promise < string > ( ( resolve ) => {
448486 finish = resolve ;
@@ -461,10 +499,12 @@ it.each([
461499 const submit = vi . fn ( ) ;
462500 const cancel = vi . fn ( ) ;
463501 await using screen = await render ( < SubmissionProbe owner = "thread-a" /> ) ;
502+ using _timers = expires ? fakeTimers ( ) : { [ Symbol . dispose ] ( ) { } } ;
464503 await act ( ( ) => mocks . voice . start ( ) ) ;
465504 await act ( ( ) => mocks . voice . submitAfterDictation ( submit , cancel ) ) ;
466505 await vi . waitFor ( ( ) => expect ( mocks . voice . state . phase ) . toBe ( phase ) ) ;
467506 await screen . rerender ( < div /> ) ;
507+ if ( expires ) await act ( ( ) => vi . advanceTimersByTimeAsync ( 6 * 60_000 ) ) ;
468508 await screen . rerender ( < SubmissionProbe owner = "thread-b" /> ) ;
469509 expect ( mocks . voice . state . phase ) . toBe ( "idle" ) ;
470510 await act ( ( ) => mocks . voice . cancel ( ) ) ;
0 commit comments