1- import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox" ;
21import type { AnyRouter } from "@tanstack/react-router" ;
32import { EnvironmentId } from "@t3tools/contracts" ;
43import * as DateTime from "effect/DateTime" ;
54import * as Option from "effect/Option" ;
65import { Atom , AtomRegistry } from "effect/reactivity" ;
76import { expect , it , vi } from "vite-plus/test" ;
87
9- import { fakeTimers , stubGlobal } from "../../test/render" ;
8+ import { stubGlobal } from "../../test/render" ;
109
1110const state = vi . hoisted ( ( ) => ( {
1211 mode : "notifications" ,
@@ -241,24 +240,18 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
241240 } ,
242241) ;
243242
244- it ( "holds a completion through a continuation gap and notifies once it settles " , async ( ) => {
243+ it ( "presents completion notifications and sound as soon as the shell completes " , async ( ) => {
245244 await using app = setup ( ) ;
245+ state . mode = "notifications-and-sound" ;
246246 app . start ( ) ;
247- using _timers = fakeTimers ( ) ;
248- const finished = ( turnId : string , completedAt : string ) => ( {
249- latestTurn : { turnId, state : "completed" , completedAt } ,
250- } ) ;
251- state . shells . set ( "one" , shell ( finished ( "turn-1" , "2026-09-13T07:00:01.000Z" ) ) ) ;
252247 state . shells . set (
253248 "one" ,
254- shell ( { latestTurn : { turnId : "turn-2" , state : "running" , completedAt : null } } ) ,
249+ shell ( {
250+ latestTurn : { turnId : "turn" , state : "completed" , completedAt : "2026-09-13T07:00:01.000Z" } ,
251+ } ) ,
255252 ) ;
256- state . shells . set ( "one" , shell ( finished ( "turn-2" , "2026-09-13T07:00:02.000Z" ) ) ) ;
257- expect ( TestNotification . sent ) . toEqual ( [ ] ) ;
258- await vi . advanceTimersByTimeAsync ( WORKING_EXIT_GRACE_MS - 1 ) ;
259- expect ( TestNotification . sent ) . toEqual ( [ ] ) ;
260- await vi . advanceTimersByTimeAsync ( 1 ) ;
261253 expect ( TestNotification . sent . map ( ( sent ) => sent . title ) ) . toEqual ( [ "Thread completed" ] ) ;
254+ expect ( state . sound ) . toHaveBeenCalledWith ( "completion" , expect . any ( Function ) ) ;
262255} ) ;
263256
264257it . each ( [ "hasPendingApprovals" , "hasPendingUserInput" ] as const ) (
@@ -277,7 +270,6 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
277270 } ;
278271 state . shells . set ( "one" , shell ( { id : "working" } , { id : "asking" } , { id : "offline" } , done ) ) ;
279272 app . start ( ) ;
280- using _timers = fakeTimers ( ) ;
281273 state . shells . set ( "one" , shell ( { id : "working" } , asking , { id : "offline" } , done ) ) ;
282274 state . shells . set ( "one" , {
283275 ...shell ( { id : "working" } , asking , { id : "offline" } , done ) ,
@@ -296,37 +288,31 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
296288
297289 expect ( TestNotification . sent . map ( ( { title } ) => title ) ) . toEqual ( [
298290 "Approval needed" ,
291+ "Thread completed" ,
299292 flag === "hasPendingApprovals" ? "Approval needed" : "Input needed" ,
300293 ] ) ;
301- await vi . advanceTimersByTimeAsync ( WORKING_EXIT_GRACE_MS ) ;
302294 state . shells . set ( "one" , { ...shell ( completed , asking , offline , done ) , status : "cached" } ) ;
303295 state . shells . set ( "one" , shell ( completed , asking , offline , done ) ) ;
304- await vi . advanceTimersByTimeAsync ( WORKING_EXIT_GRACE_MS ) ;
305296 expect ( TestNotification . sent . map ( ( { title } ) => title ) ) . toEqual ( [
306297 "Approval needed" ,
307- flag === "hasPendingApprovals" ? "Approval needed" : "Input needed" ,
308298 "Thread completed" ,
299+ flag === "hasPendingApprovals" ? "Approval needed" : "Input needed" ,
309300 ] ) ;
310301 } ,
311302) ;
312303
313304it . each ( [ false , true ] ) (
314- "rechecks a pending completion after reconnect (continued: %s)" ,
305+ "does not replay a notified completion after reconnect (continued: %s)" ,
315306 async ( continued ) => {
316307 await using app = setup ( ) ;
317308 app . start ( ) ;
318- using _timers = fakeTimers ( ) ;
319309 const completed = shell ( {
320310 latestTurn : { turnId : "turn" , state : "completed" , completedAt : "2026-09-13T07:05:00.000Z" } ,
321311 } ) ;
322312 state . shells . set ( "one" , completed ) ;
323313 state . shells . set ( "one" , { ...completed , status : "cached" } ) ;
324- await vi . advanceTimersByTimeAsync ( WORKING_EXIT_GRACE_MS ) ;
325- expect ( TestNotification . sent ) . toEqual ( [ ] ) ;
314+ expect ( TestNotification . sent . map ( ( { title } ) => title ) ) . toEqual ( [ "Thread completed" ] ) ;
326315 state . shells . set ( "one" , continued ? shell ( ) : completed ) ;
327- await vi . advanceTimersByTimeAsync ( WORKING_EXIT_GRACE_MS ) ;
328- expect ( TestNotification . sent . map ( ( { title } ) => title ) ) . toEqual (
329- continued ? [ ] : [ "Thread completed" ] ,
330- ) ;
316+ expect ( TestNotification . sent . map ( ( { title } ) => title ) ) . toEqual ( [ "Thread completed" ] ) ;
331317 } ,
332318) ;
0 commit comments