@@ -7,7 +7,7 @@ import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-cha
77import { pairDeviceIdentity } from "./device-authz.test-helpers.js" ;
88import { describeWithGatewayServer } from "./server.node-pairing.test-support.js" ;
99import { connectGatewayClient } from "./test-helpers.e2e.js" ;
10- import { installGatewayTestHooks } from "./test-helpers.js" ;
10+ import { dispatchInboundMessageMock , installGatewayTestHooks } from "./test-helpers.js" ;
1111
1212installGatewayTestHooks ( { scope : "suite" } ) ;
1313
@@ -129,5 +129,136 @@ describe("gateway node chat subscriptions", () => {
129129 await reconnected ?. stopAndWait ( ) ;
130130 }
131131 } ) ;
132+
133+ test ( "delivers final and error terminals through one canonical node subscription" , async ( ) => {
134+ const paired = await pairDeviceIdentity ( {
135+ name : "canonical-chat-terminal-fanout" ,
136+ role : "node" ,
137+ scopes : [ ] ,
138+ clientId : GATEWAY_CLIENT_NAMES . NODE_HOST ,
139+ clientMode : GATEWAY_CLIENT_MODES . NODE ,
140+ } ) ;
141+ const pairing = await requestNodePairing ( {
142+ nodeId : paired . identity . deviceId ,
143+ platform : "macos" ,
144+ deviceFamily : "Mac" ,
145+ commands : [ ] ,
146+ } ) ;
147+ await approveNodePairing ( pairing . request . requestId , {
148+ callerScopes : [ "operator.pairing" , "operator.write" , "operator.admin" ] ,
149+ } ) ;
150+
151+ const events : ReceivedNodeEvent [ ] = [ ] ;
152+ let node : Awaited < ReturnType < typeof connectGatewayClient > > | undefined ;
153+ let operator : Awaited < ReturnType < typeof connectGatewayClient > > | undefined ;
154+ const terminalPayloads = ( runId : string ) =>
155+ events
156+ . filter (
157+ ( event ) =>
158+ event . event === "chat" &&
159+ ( event . payload as { runId ?: string } | undefined ) ?. runId === runId ,
160+ )
161+ . map ( ( event ) => event . payload as Record < string , unknown > ) ;
162+
163+ try {
164+ node = await connectGatewayClient ( {
165+ url : `ws://127.0.0.1:${ getStarted ( ) . port } ` ,
166+ token : "secret" ,
167+ role : "node" ,
168+ clientName : GATEWAY_CLIENT_NAMES . NODE_HOST ,
169+ clientDisplayName : "canonical terminal fanout node" ,
170+ clientVersion : "1.0.0" ,
171+ platform : "macos" ,
172+ deviceFamily : "Mac" ,
173+ mode : GATEWAY_CLIENT_MODES . NODE ,
174+ scopes : [ ] ,
175+ commands : [ ] ,
176+ deviceIdentity : paired . identity ,
177+ onEvent : ( event ) => events . push ( event ) ,
178+ } ) ;
179+ operator = await connectGatewayClient ( {
180+ url : `ws://127.0.0.1:${ getStarted ( ) . port } ` ,
181+ token : "secret" ,
182+ role : "operator" ,
183+ scopes : [ "operator.write" ] ,
184+ } ) ;
185+ await node . request ( "node.event" , {
186+ event : "chat.subscribe" ,
187+ payload : { sessionKey : " Main " } ,
188+ } ) ;
189+
190+ dispatchInboundMessageMock . mockImplementationOnce ( async ( ...args : unknown [ ] ) => {
191+ const [ params ] = args as [
192+ {
193+ dispatcher : {
194+ sendFinalReply : ( payload : { text : string } ) => boolean ;
195+ markComplete : ( ) => void ;
196+ waitForIdle : ( ) => Promise < void > ;
197+ getQueuedCounts : ( ) => { final : number ; block : number ; tool : number } ;
198+ } ;
199+ } ,
200+ ] ;
201+ params . dispatcher . sendFinalReply ( { text : "node subscription final" } ) ;
202+ params . dispatcher . markComplete ( ) ;
203+ await params . dispatcher . waitForIdle ( ) ;
204+ return { queuedFinal : true , counts : params . dispatcher . getQueuedCounts ( ) } ;
205+ } ) ;
206+ const finalRunId = "canonical-node-terminal-final" ;
207+ const finalStarted = await operator . request < { runId : string ; status : string } > (
208+ "chat.send" ,
209+ {
210+ sessionKey : "main" ,
211+ message : "deliver a final terminal" ,
212+ idempotencyKey : finalRunId ,
213+ } ,
214+ ) ;
215+ expect ( finalStarted ) . toMatchObject ( { runId : finalRunId , status : "started" } ) ;
216+ await vi . waitFor ( ( ) => expect ( terminalPayloads ( finalRunId ) ) . toHaveLength ( 1 ) ) ;
217+ expect ( terminalPayloads ( finalRunId ) [ 0 ] ) . toMatchObject ( {
218+ runId : finalRunId ,
219+ sessionKey : "agent:main:main" ,
220+ state : "final" ,
221+ message : {
222+ role : "assistant" ,
223+ content : [ { type : "text" , text : "node subscription final" } ] ,
224+ } ,
225+ } ) ;
226+
227+ const errorRunId = "canonical-node-terminal-error" ;
228+ dispatchInboundMessageMock . mockRejectedValueOnce ( new Error ( "node dispatch rejected" ) ) ;
229+ const errorStarted = await operator . request < { runId : string ; status : string } > (
230+ "chat.send" ,
231+ {
232+ sessionKey : "main" ,
233+ message : "deliver an error terminal" ,
234+ idempotencyKey : errorRunId ,
235+ } ,
236+ ) ;
237+ expect ( errorStarted ) . toMatchObject ( { runId : errorRunId , status : "started" } ) ;
238+ await vi . waitFor ( ( ) => expect ( terminalPayloads ( errorRunId ) ) . toHaveLength ( 1 ) ) ;
239+ await node . request ( "node.event" , {
240+ event : "chat.subscribe" ,
241+ payload : { sessionKey : "main" } ,
242+ } ) ;
243+
244+ expect ( terminalPayloads ( finalRunId ) ) . toHaveLength ( 1 ) ;
245+ expect ( terminalPayloads ( errorRunId ) ) . toHaveLength ( 1 ) ;
246+ const errorPayload = terminalPayloads ( errorRunId ) [ 0 ] ;
247+ if ( ! errorPayload ) {
248+ throw new Error ( "expected the node error terminal" ) ;
249+ }
250+ expect ( errorPayload ) . toMatchObject ( {
251+ runId : errorRunId ,
252+ sessionKey : "agent:main:main" ,
253+ state : "error" ,
254+ } ) ;
255+ expect ( errorPayload . errorMessage ) . toContain ( "node dispatch rejected" ) ;
256+ expect ( errorPayload ) . not . toHaveProperty ( "message" ) ;
257+ } finally {
258+ dispatchInboundMessageMock . mockReset ( ) ;
259+ await operator ?. stopAndWait ( ) ;
260+ await node ?. stopAndWait ( ) ;
261+ }
262+ } ) ;
132263 } ) ;
133264} ) ;
0 commit comments