@@ -49,11 +49,13 @@ function createRuntime() {
4949 } satisfies GoogleChatRuntimeEnv ;
5050}
5151
52+ let createGoogleChatTypingMessage : typeof import ( "./monitor-reply-delivery.js" ) . createGoogleChatTypingMessage ;
5253let deliverGoogleChatReply : typeof import ( "./monitor-reply-delivery.js" ) . deliverGoogleChatReply ;
5354
5455beforeEach ( async ( ) => {
5556 vi . clearAllMocks ( ) ;
56- ( { deliverGoogleChatReply } = await import ( "./monitor-reply-delivery.js" ) ) ;
57+ ( { createGoogleChatTypingMessage, deliverGoogleChatReply } =
58+ await import ( "./monitor-reply-delivery.js" ) ) ;
5759} ) ;
5860
5961afterAll ( ( ) => {
@@ -78,8 +80,10 @@ describe("Google Chat reply delivery", () => {
7880 config,
7981 statusSink,
8082 typingMessage : {
83+ placement : "thread" ,
8184 name : "spaces/AAA/messages/typing" ,
82- thread : "spaces/AAA/threads/root" ,
85+ requestedThreadName : "spaces/AAA/threads/root" ,
86+ deliveredThreadName : "spaces/AAA/threads/root" ,
8387 } ,
8488 } ) ;
8589
@@ -107,6 +111,123 @@ describe("Google Chat reply delivery", () => {
107111 ) ;
108112 } ) ;
109113
114+ it ( "continues later chunks in the provider fallback thread" , async ( ) => {
115+ const core = createCore ( { chunks : [ "first chunk" , "second chunk" ] } ) ;
116+ const runtime = createRuntime ( ) ;
117+ mocks . sendGoogleChatMessage
118+ . mockResolvedValueOnce ( {
119+ messageName : "spaces/AAA/messages/first" ,
120+ threadName : "spaces/AAA/threads/fallback" ,
121+ } )
122+ . mockResolvedValueOnce ( {
123+ messageName : "spaces/AAA/messages/second" ,
124+ threadName : "spaces/AAA/threads/fallback" ,
125+ } ) ;
126+
127+ await deliverGoogleChatReply ( {
128+ payload : { text : "two chunks" , replyToId : "spaces/AAA/threads/requested" } ,
129+ account,
130+ spaceId : "spaces/AAA" ,
131+ runtime,
132+ core,
133+ config,
134+ } ) ;
135+
136+ expect ( mocks . sendGoogleChatMessage ) . toHaveBeenNthCalledWith ( 1 , {
137+ account,
138+ space : "spaces/AAA" ,
139+ text : "first chunk" ,
140+ thread : "spaces/AAA/threads/requested" ,
141+ } ) ;
142+ expect ( mocks . sendGoogleChatMessage ) . toHaveBeenNthCalledWith ( 2 , {
143+ account,
144+ space : "spaces/AAA" ,
145+ text : "second chunk" ,
146+ thread : "spaces/AAA/threads/fallback" ,
147+ } ) ;
148+ } ) ;
149+
150+ it ( "continues after a fallback typing placeholder in its delivered thread" , async ( ) => {
151+ const core = createCore ( { chunks : [ "first chunk" , "second chunk" ] } ) ;
152+ const runtime = createRuntime ( ) ;
153+ mocks . sendGoogleChatMessage . mockResolvedValueOnce ( {
154+ messageName : "spaces/AAA/messages/second" ,
155+ threadName : "spaces/AAA/threads/fallback" ,
156+ } ) ;
157+
158+ await deliverGoogleChatReply ( {
159+ payload : { text : "two chunks" , replyToId : "spaces/AAA/threads/requested" } ,
160+ account,
161+ spaceId : "spaces/AAA" ,
162+ runtime,
163+ core,
164+ config,
165+ typingMessage : createGoogleChatTypingMessage ( {
166+ messageName : "spaces/AAA/messages/typing" ,
167+ requestedThreadName : "spaces/AAA/threads/requested" ,
168+ deliveredThreadName : "spaces/AAA/threads/fallback" ,
169+ } ) ,
170+ } ) ;
171+
172+ expect ( mocks . updateGoogleChatMessage ) . toHaveBeenCalledWith ( {
173+ account,
174+ messageName : "spaces/AAA/messages/typing" ,
175+ text : "first chunk" ,
176+ } ) ;
177+ expect ( mocks . sendGoogleChatMessage ) . toHaveBeenCalledOnce ( ) ;
178+ expect ( mocks . sendGoogleChatMessage ) . toHaveBeenCalledWith ( {
179+ account,
180+ space : "spaces/AAA" ,
181+ text : "second chunk" ,
182+ thread : "spaces/AAA/threads/fallback" ,
183+ } ) ;
184+ } ) ;
185+
186+ it ( "keeps the requested thread when the provider omits thread metadata" , async ( ) => {
187+ const core = createCore ( { chunks : [ "first chunk" , "second chunk" ] } ) ;
188+ const runtime = createRuntime ( ) ;
189+ mocks . sendGoogleChatMessage . mockResolvedValue ( {
190+ messageName : "spaces/AAA/messages/sent" ,
191+ } ) ;
192+
193+ await deliverGoogleChatReply ( {
194+ payload : { text : "two chunks" , replyToId : "spaces/AAA/threads/requested" } ,
195+ account,
196+ spaceId : "spaces/AAA" ,
197+ runtime,
198+ core,
199+ config,
200+ } ) ;
201+
202+ expect ( mocks . sendGoogleChatMessage ) . toHaveBeenCalledTimes ( 2 ) ;
203+ for ( const call of mocks . sendGoogleChatMessage . mock . calls ) {
204+ expect ( call [ 0 ] ?. thread ) . toBe ( "spaces/AAA/threads/requested" ) ;
205+ }
206+ } ) ;
207+
208+ it ( "keeps top-level chunks top-level when Google returns a thread name" , async ( ) => {
209+ const core = createCore ( { chunks : [ "first chunk" , "second chunk" ] } ) ;
210+ const runtime = createRuntime ( ) ;
211+ mocks . sendGoogleChatMessage . mockResolvedValue ( {
212+ messageName : "spaces/AAA/messages/sent" ,
213+ threadName : "spaces/AAA/threads/provider-created" ,
214+ } ) ;
215+
216+ await deliverGoogleChatReply ( {
217+ payload : { text : "two top-level chunks" } ,
218+ account,
219+ spaceId : "spaces/AAA" ,
220+ runtime,
221+ core,
222+ config,
223+ } ) ;
224+
225+ expect ( mocks . sendGoogleChatMessage ) . toHaveBeenCalledTimes ( 2 ) ;
226+ for ( const call of mocks . sendGoogleChatMessage . mock . calls ) {
227+ expect ( call [ 0 ] ?. thread ) . toBeUndefined ( ) ;
228+ }
229+ } ) ;
230+
110231 it ( "rejects when a later text chunk send fails instead of dropping it silently" , async ( ) => {
111232 const core = createCore ( { chunks : [ "first chunk" , "second chunk" , "third chunk" ] } ) ;
112233 const runtime = createRuntime ( ) ;
@@ -145,8 +266,10 @@ describe("Google Chat reply delivery", () => {
145266 core,
146267 config,
147268 typingMessage : {
269+ placement : "thread" ,
148270 name : "spaces/AAA/messages/typing" ,
149- thread : "spaces/AAA/threads/root" ,
271+ requestedThreadName : "spaces/AAA/threads/root" ,
272+ deliveredThreadName : "spaces/AAA/threads/root" ,
150273 } ,
151274 } ) ,
152275 ) . rejects . toBe ( fallbackError ) ;
@@ -167,8 +290,10 @@ describe("Google Chat reply delivery", () => {
167290 core,
168291 config,
169292 typingMessage : {
293+ placement : "thread" ,
170294 name : "spaces/AAA/messages/typing" ,
171- thread : "spaces/AAA/threads/root" ,
295+ requestedThreadName : "spaces/AAA/threads/root" ,
296+ deliveredThreadName : "spaces/AAA/threads/root" ,
172297 } ,
173298 } ) ;
174299
@@ -203,8 +328,10 @@ describe("Google Chat reply delivery", () => {
203328 core,
204329 config,
205330 typingMessage : {
331+ placement : "thread" ,
206332 name : "spaces/AAA/messages/typing" ,
207- thread : "spaces/AAA/threads/root" ,
333+ requestedThreadName : "spaces/AAA/threads/root" ,
334+ deliveredThreadName : "spaces/AAA/threads/root" ,
208335 } ,
209336 } ) ;
210337
@@ -237,8 +364,10 @@ describe("Google Chat reply delivery", () => {
237364 core,
238365 config,
239366 typingMessage : {
367+ placement : "thread" ,
240368 name : "spaces/AAA/messages/typing" ,
241- thread : "spaces/AAA/threads/root" ,
369+ requestedThreadName : "spaces/AAA/threads/root" ,
370+ deliveredThreadName : "spaces/AAA/threads/root" ,
242371 } ,
243372 } ) ,
244373 ) . rejects . toThrow (
0 commit comments