11// Completion-reply capture tests cover the short polling window used to collect
22// final child output after an announce waits for the subagent.
33import { describe , expect , it , vi } from "vitest" ;
4- import { captureSubagentCompletionReplyUsing } from "./subagent-announce-capture.js" ;
4+ import {
5+ captureSubagentCompletionReplyUsing ,
6+ readLatestSubagentOutputWithRetryUsing ,
7+ } from "./subagent-announce-capture.js" ;
58
69describe ( "captureSubagentCompletionReply" , ( ) => {
710 it ( "returns immediate assistant output from history without polling" , async ( ) => {
@@ -44,6 +47,59 @@ describe("captureSubagentCompletionReply", () => {
4447 vi . useRealTimers ( ) ;
4548 } ) ;
4649
50+ it ( "captures the final assistant reply when it becomes available at the deadline" , async ( ) => {
51+ vi . useFakeTimers ( ) ;
52+ try {
53+ const readSubagentOutput = vi
54+ . fn < ( sessionKey : string ) => Promise < string | undefined > > ( )
55+ . mockResolvedValueOnce ( undefined )
56+ . mockResolvedValueOnce ( undefined )
57+ . mockResolvedValueOnce ( "Requester-visible final result" ) ;
58+
59+ const pending = captureSubagentCompletionReplyUsing ( {
60+ sessionKey : "agent:main:subagent:child" ,
61+ maxWaitMs : 5 ,
62+ retryIntervalMs : 5 ,
63+ readSubagentOutput,
64+ } ) ;
65+ await vi . runAllTimersAsync ( ) ;
66+
67+ await expect ( pending ) . resolves . toBe ( "Requester-visible final result" ) ;
68+ expect ( readSubagentOutput ) . toHaveBeenCalledTimes ( 3 ) ;
69+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
70+ } finally {
71+ vi . useRealTimers ( ) ;
72+ }
73+ } ) ;
74+
75+ it ( "charges slow output reads against the bounded retry deadline" , async ( ) => {
76+ vi . useFakeTimers ( ) ;
77+ try {
78+ const startedAt = performance . now ( ) ;
79+ const readSubagentOutput = vi . fn ( async ( ) => {
80+ await new Promise < void > ( ( resolve ) => {
81+ setTimeout ( resolve , 15 ) ;
82+ } ) ;
83+ return undefined ;
84+ } ) ;
85+
86+ const pending = readLatestSubagentOutputWithRetryUsing ( {
87+ sessionKey : "agent:main:subagent:child" ,
88+ maxWaitMs : 25 ,
89+ retryIntervalMs : 10 ,
90+ readSubagentOutput,
91+ } ) ;
92+ await vi . runAllTimersAsync ( ) ;
93+
94+ await expect ( pending ) . resolves . toBeUndefined ( ) ;
95+ expect ( readSubagentOutput ) . toHaveBeenCalledTimes ( 2 ) ;
96+ expect ( performance . now ( ) - startedAt ) . toBe ( 40 ) ;
97+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
98+ } finally {
99+ vi . useRealTimers ( ) ;
100+ }
101+ } ) ;
102+
47103 it ( "returns undefined when no completion output arrives before retry window closes" , async ( ) => {
48104 vi . useFakeTimers ( ) ;
49105 const readSubagentOutput = vi
@@ -60,10 +116,53 @@ describe("captureSubagentCompletionReply", () => {
60116 const result = await pending ;
61117
62118 expect ( result ) . toBeUndefined ( ) ;
63- expect ( readSubagentOutput ) . toHaveBeenCalledTimes ( 8 ) ;
119+ expect ( readSubagentOutput ) . toHaveBeenCalledTimes ( 9 ) ;
64120 vi . useRealTimers ( ) ;
65121 } ) ;
66122
123+ it . each ( [ 0 , - 5 ] ) ( "does not poll when the retry budget is %i ms" , async ( maxWaitMs ) => {
124+ vi . useFakeTimers ( ) ;
125+ try {
126+ const readSubagentOutput = vi . fn ( ) . mockResolvedValue ( undefined ) ;
127+
128+ await expect (
129+ captureSubagentCompletionReplyUsing ( {
130+ sessionKey : "agent:main:subagent:child" ,
131+ maxWaitMs,
132+ retryIntervalMs : 5 ,
133+ readSubagentOutput,
134+ } ) ,
135+ ) . resolves . toBeUndefined ( ) ;
136+
137+ expect ( readSubagentOutput ) . toHaveBeenCalledTimes ( 1 ) ;
138+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
139+ } finally {
140+ vi . useRealTimers ( ) ;
141+ }
142+ } ) ;
143+
144+ it ( "does not poll when waiting for a completion reply is disabled" , async ( ) => {
145+ vi . useFakeTimers ( ) ;
146+ try {
147+ const readSubagentOutput = vi . fn ( ) . mockResolvedValue ( undefined ) ;
148+
149+ await expect (
150+ captureSubagentCompletionReplyUsing ( {
151+ sessionKey : "agent:main:subagent:child" ,
152+ waitForReply : false ,
153+ maxWaitMs : 25 ,
154+ retryIntervalMs : 5 ,
155+ readSubagentOutput,
156+ } ) ,
157+ ) . resolves . toBeUndefined ( ) ;
158+
159+ expect ( readSubagentOutput ) . toHaveBeenCalledTimes ( 1 ) ;
160+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
161+ } finally {
162+ vi . useRealTimers ( ) ;
163+ }
164+ } ) ;
165+
67166 it ( "returns partial assistant progress when the latest assistant turn is tool-only" , async ( ) => {
68167 const readSubagentOutput = vi
69168 . fn < ( sessionKey : string ) => Promise < string | undefined > > ( )
0 commit comments