1+ import { RunId } from "@t3tools/contracts" ;
12import { describe , expect , it } from "vite-plus/test" ;
23
34import {
4- type BackgroundThreadEntry ,
55 type BackgroundThreadMemory ,
6+ type BackgroundThreadSnapshot ,
67 nextBackgroundThreadAnnouncement ,
8+ resolveBackgroundThreadRequest ,
79} from "./backgroundThreadAnnouncement" ;
810
9- const login = ( status : BackgroundThreadEntry [ "status" ] ) : BackgroundThreadEntry => ( {
10- key : "env:login" ,
11- title : "Fix login redirect" ,
12- status,
13- } ) ;
14- const docs = ( status : BackgroundThreadEntry [ "status" ] ) : BackgroundThreadEntry => ( {
15- key : "env:docs" ,
16- title : "Update docs" ,
17- status,
18- } ) ;
11+ const firstRun = RunId . make ( "run-1" ) ;
12+ const secondRun = RunId . make ( "run-2" ) ;
13+
14+ function thread (
15+ key : string ,
16+ title : string ,
17+ state : Partial < Omit < BackgroundThreadSnapshot , "key" | "title" > > = { } ,
18+ ) : BackgroundThreadSnapshot {
19+ return {
20+ key,
21+ title,
22+ request : null ,
23+ runId : firstRun ,
24+ runStatus : "running" ,
25+ snoozed : false ,
26+ ...state ,
27+ } ;
28+ }
29+ const login = ( state ?: Partial < BackgroundThreadSnapshot > ) =>
30+ thread ( "env:login" , "Fix login redirect" , state ) ;
31+ const docs = ( state ?: Partial < BackgroundThreadSnapshot > ) =>
32+ thread ( "env:docs" , "Update docs" , state ) ;
1933
2034function speak (
2135 frames : ReadonlyArray < {
22- readonly threads : ReadonlyArray < BackgroundThreadEntry > ;
36+ readonly threads : ReadonlyArray < BackgroundThreadSnapshot > ;
2337 readonly open ?: string ;
2438 } > ,
2539) {
@@ -37,51 +51,98 @@ describe("nextBackgroundThreadAnnouncement", () => {
3751 it ( "names the thread with the same words the open thread uses" , ( ) => {
3852 expect (
3953 speak ( [
40- { threads : [ login ( null ) ] } ,
41- { threads : [ login ( "approval" ) ] } ,
42- { threads : [ login ( null ) ] } ,
43- { threads : [ login ( "input" ) ] } ,
44- { threads : [ login ( "done" ) ] } ,
45- { threads : [ login ( null ) ] } ,
46- { threads : [ login ( "failed" ) ] } ,
54+ { threads : [ login ( ) ] } ,
55+ { threads : [ login ( { request : "approval" } ) ] } ,
56+ { threads : [ login ( ) ] } ,
57+ { threads : [ login ( { request : "input" } ) ] } ,
58+ { threads : [ login ( { runStatus : "completed" } ) ] } ,
59+ { threads : [ login ( { runId : secondRun , runStatus : "failed" } ) ] } ,
60+ { threads : [ login ( { runId : RunId . make ( "run-3" ) , runStatus : "interrupted" } ) ] } ,
4761 ] ) ,
4862 ) . toEqual ( [
4963 "Needs approval: Fix login redirect" ,
5064 "Needs your input: Fix login redirect" ,
5165 "Done: Fix login redirect" ,
5266 "Failed: Fix login redirect" ,
67+ "Stopped: Fix login redirect" ,
5368 ] ) ;
5469 } ) ;
5570
71+ it ( "announces each run's ending once" , ( ) => {
72+ expect (
73+ speak ( [
74+ { threads : [ login ( ) ] } ,
75+ { threads : [ login ( { runStatus : "completed" } ) ] } ,
76+ // Marking the thread unread or renaming it changes nothing about the run.
77+ { threads : [ login ( { runStatus : "completed" , title : "Fix the login redirect" } ) ] } ,
78+ // The next run can finish between two updates, so it never looks busy here.
79+ { threads : [ login ( { runId : secondRun , runStatus : "completed" } ) ] } ,
80+ { threads : [ login ( { runId : RunId . make ( "run-3" ) , runStatus : "rolled_back" } ) ] } ,
81+ ] ) ,
82+ ) . toEqual ( [ "Done: Fix login redirect" , "Done: Fix login redirect" ] ) ;
83+ } ) ;
84+
5685 it ( "stays quiet for threads it sees for the first time, like on launch or reconnect" , ( ) => {
5786 expect (
5887 speak ( [
59- { threads : [ login ( "done" ) , docs ( "approval" ) ] } ,
60- { threads : [ login ( "done" ) , docs ( "approval" ) ] } ,
88+ { threads : [ login ( { runStatus : "completed" } ) , docs ( { request : "approval" } ) ] } ,
89+ { threads : [ login ( { runStatus : "completed" } ) , docs ( { request : "approval" } ) ] } ,
6190 // The environment drops out while it resyncs, then comes back changed.
6291 { threads : [ ] } ,
63- { threads : [ login ( "failed" ) , docs ( "done" ) ] } ,
92+ {
93+ threads : [
94+ login ( { runId : secondRun , runStatus : "failed" } ) ,
95+ docs ( { runStatus : "completed" } ) ,
96+ ] ,
97+ } ,
6498 ] ) ,
6599 ) . toEqual ( [ ] ) ;
66100 } ) ;
67101
68102 it ( "leaves the open thread to its own announcer" , ( ) => {
69103 expect (
70104 speak ( [
71- { threads : [ login ( null ) , docs ( null ) ] , open : "env:login" } ,
72- { threads : [ login ( "done" ) , docs ( null ) ] , open : "env:login" } ,
73- // Switching away from a thread doesn't replay its status.
74- { threads : [ login ( "done" ) , docs ( null ) ] , open : "env:docs" } ,
105+ { threads : [ login ( ) , docs ( ) ] , open : "env:login" } ,
106+ { threads : [ login ( { runStatus : "completed" } ) , docs ( ) ] , open : "env:login" } ,
107+ // Switching away from a thread doesn't replay its ending.
108+ { threads : [ login ( { runStatus : "completed" } ) , docs ( ) ] , open : "env:docs" } ,
109+ {
110+ threads : [ login ( { runStatus : "completed" } ) , docs ( { request : "input" } ) ] ,
111+ open : "env:login" ,
112+ } ,
113+ ] ) ,
114+ ) . toEqual ( [ "Needs your input: Update docs" ] ) ;
115+ } ) ;
116+
117+ it ( "keeps a snoozed thread quiet, even after it wakes" , ( ) => {
118+ expect (
119+ speak ( [
120+ { threads : [ login ( { snoozed : true } ) ] } ,
121+ { threads : [ login ( { snoozed : true , runStatus : "completed" } ) ] } ,
122+ { threads : [ login ( { runStatus : "completed" } ) ] } ,
75123 ] ) ,
76124 ) . toEqual ( [ ] ) ;
77125 } ) ;
78126
79127 it ( "speaks threads that change together in one announcement" , ( ) => {
80128 expect (
81129 speak ( [
82- { threads : [ login ( null ) , docs ( null ) ] } ,
83- { threads : [ login ( "done" ) , docs ( "approval" ) ] } ,
130+ { threads : [ login ( ) , docs ( ) ] } ,
131+ { threads : [ login ( { runStatus : "completed" } ) , docs ( { request : "approval" } ) ] } ,
84132 ] ) ,
85133 ) . toEqual ( [ "Done: Fix login redirect. Needs approval: Update docs" ] ) ;
86134 } ) ;
87135} ) ;
136+
137+ describe ( "resolveBackgroundThreadRequest" , ( ) => {
138+ it ( "raises a subagent's request on the thread that owns its row" , ( ) => {
139+ const quiet = { hasPendingApprovals : false , hasPendingUserInput : false } ;
140+ expect ( resolveBackgroundThreadRequest ( { ...quiet , pendingSubagentRequest : "approval" } ) ) . toBe (
141+ "approval" ,
142+ ) ;
143+ expect ( resolveBackgroundThreadRequest ( { ...quiet , pendingSubagentRequest : "user_input" } ) ) . toBe (
144+ "input" ,
145+ ) ;
146+ expect ( resolveBackgroundThreadRequest ( { ...quiet , pendingSubagentRequest : null } ) ) . toBeNull ( ) ;
147+ } ) ;
148+ } ) ;
0 commit comments