1- import { spawn , type ChildProcessWithoutNullStreams } from "node:child_process" ;
1+ import { execFileSync , spawn , type ChildProcessWithoutNullStreams } from "node:child_process" ;
22import { randomUUID } from "node:crypto" ;
33import fs from "node:fs/promises" ;
4- import { afterEach , describe , expect , it , vi } from "vitest" ;
4+ import path from "node:path" ;
5+ import { describe , expect , it , vi } from "vitest" ;
6+ import { PROTOCOL_VERSION } from "../../packages/gateway-protocol/src/index.js" ;
57import { stopChild } from "../../scripts/lib/gateway-bench-child.js" ;
68import { getFreePort } from "../../scripts/lib/gateway-bench-probes.js" ;
9+ import { createGatewayWsClient } from "../../scripts/lib/gateway-ws-client.js" ;
10+ import {
11+ BUILD_STAMP_FILE ,
12+ RUNTIME_POSTBUILD_STAMP_FILE ,
13+ } from "../../scripts/lib/local-build-metadata-paths.mts" ;
14+ import { createOpenClawTestInstance } from "../../test/helpers/openclaw-test-instance.js" ;
715import type { OpenClawConfig } from "../config/types.openclaw.js" ;
816import type { AgentEventPayload } from "../infra/agent-events.js" ;
9- import { resetAgentEventsForTest } from "../infra/agent-events.js" ;
1017import { hasErrnoCode } from "../infra/errno.js" ;
11- import { resetLogger } from "../logging/logger.js" ;
12- import { createOpenClawTestState } from "../test-utils/openclaw-test-state.js" ;
13- import { disconnectGatewayClient , startGatewayWithClient } from "./test-helpers.e2e.js" ;
1418import { buildMockOpenAiResponsesProvider } from "./test-openai-responses-model.js" ;
1519
1620type StreamFrame = {
@@ -34,15 +38,27 @@ const cases = [
3438 } ,
3539] as const ;
3640
37- afterEach ( ( ) => {
38- resetAgentEventsForTest ( { preserveListeners : true } ) ;
39- resetLogger ( ) ;
40- } ) ;
41-
4241describe ( "Gateway concurrent HTTP streams" , ( ) => {
4342 it ( "keeps both streams isolated while global observers retain every run" , async ( ) => {
44- const state = await createOpenClawTestState ( {
45- label : "concurrent-streams" ,
43+ const cwd = process . cwd ( ) ;
44+ const checkoutSha = execFileSync ( "git" , [ "rev-parse" , "HEAD" ] , {
45+ cwd,
46+ encoding : "utf8" ,
47+ } ) . trim ( ) ;
48+ // Require this checkout's complete runtime before the shared helper can
49+ // prepare a source fallback. The child must own one coherent built graph.
50+ await fs . access ( path . join ( cwd , "dist/index.js" ) ) ;
51+ for ( const stamp of [ BUILD_STAMP_FILE , RUNTIME_POSTBUILD_STAMP_FILE ] ) {
52+ const metadata = JSON . parse ( await fs . readFile ( path . join ( cwd , "dist" , stamp ) , "utf8" ) ) ;
53+ expect ( metadata . head , stamp ) . toBe ( checkoutSha ) ;
54+ }
55+ const buildInfo = JSON . parse ( await fs . readFile ( path . join ( cwd , "dist/build-info.json" ) , "utf8" ) ) ;
56+ expect ( buildInfo . commit ) . toBe ( checkoutSha ) ;
57+ const token = `fanout-${ randomUUID ( ) } ` ;
58+ const gateway = await createOpenClawTestInstance ( {
59+ name : "concurrent-streams" ,
60+ cwd,
61+ gatewayToken : token ,
4662 env : {
4763 OPENCLAW_GATEWAY_TOKEN : undefined ,
4864 OPENCLAW_GATEWAY_PASSWORD : undefined ,
@@ -59,20 +75,12 @@ describe("Gateway concurrent HTTP streams", () => {
5975 OPENCLAW_DISABLE_BUNDLED_PLUGINS : undefined ,
6076 } ,
6177 } ) ;
62- // Startup spans use the subsystem logger; discard Vitest's cached silent
63- // settings after entering the isolated fixture so stalled startup is visible.
64- resetLogger ( ) ;
65- const mockPort = await getFreePort ( ) ;
78+ const { state, port } = gateway ;
6679 const controlPath = state . path ( "response-control.json" ) ;
6780 const requestLogPath = state . path ( "provider-requests.jsonl" ) ;
68- const provider = buildMockOpenAiResponsesProvider (
69- `http://127.0.0.1:${ mockPort } /v1` ,
70- "gpt-5.6-luna" ,
71- ) ;
72- const token = `fanout-${ randomUUID ( ) } ` ;
7381 const events : AgentEventPayload [ ] = [ ] ;
7482 const abort = new AbortController ( ) ;
75- let gateway : Awaited < ReturnType < typeof startGatewayWithClient > > | undefined ;
83+ let client : ReturnType < typeof createGatewayWsClient > | undefined ;
7684 let mock : ChildProcessWithoutNullStreams | undefined ;
7785 const streams : Array < {
7886 item : ( typeof cases ) [ number ] ;
@@ -104,6 +112,12 @@ describe("Gateway concurrent HTTP streams", () => {
104112 : [ ] ;
105113 } ;
106114 try {
115+ expect ( await gateway . entrypoint ( ) ) . toEqual ( [ "dist/index.js" ] ) ;
116+ const mockPort = await getFreePort ( ) ;
117+ const provider = buildMockOpenAiResponsesProvider (
118+ `http://127.0.0.1:${ mockPort } /v1` ,
119+ "gpt-5.6-luna" ,
120+ ) ;
107121 await writeControl ( true ) ;
108122 mock = spawn ( process . execPath , [ "scripts/e2e/mock-openai-server.mjs" ] , {
109123 cwd : process . cwd ( ) ,
@@ -122,11 +136,14 @@ describe("Gateway concurrent HTTP streams", () => {
122136 } ) ;
123137 const cfg = {
124138 gateway : {
139+ port,
125140 auth : { mode : "token" , token } ,
141+ controlUi : { enabled : false } ,
126142 http : {
127143 endpoints : { chatCompletions : { enabled : true } , responses : { enabled : true } } ,
128144 } ,
129145 } ,
146+ hooks : { enabled : false } ,
130147 agents : {
131148 defaults : {
132149 workspace : state . workspaceDir ,
@@ -151,20 +168,37 @@ describe("Gateway concurrent HTTP streams", () => {
151168 plugins : { slots : { memory : "none" } } ,
152169 tools : { profile : "minimal" } ,
153170 } satisfies OpenClawConfig ;
154- gateway = await startGatewayWithClient ( {
155- cfg,
156- configPath : state . configPath ,
157- token,
158- scopes : [ "operator.admin" , "operator.read" , "operator.write" ] ,
171+ await state . writeConfig ( cfg ) ;
172+ await gateway . startGateway ( ) ;
173+ client = createGatewayWsClient ( {
174+ url : gateway . url ,
159175 onEvent : ( event ) => {
160176 if ( event . event === "agent" ) {
161177 events . push ( event . payload as AgentEventPayload ) ;
162178 }
163179 } ,
164180 } ) ;
165- const { port, client } = gateway ;
181+ await client . waitOpen ( ) ;
182+ const connected = await client . request ( "connect" , {
183+ minProtocol : PROTOCOL_VERSION ,
184+ maxProtocol : PROTOCOL_VERSION ,
185+ client : {
186+ id : "gateway-client" ,
187+ displayName : "concurrent-stream-proof" ,
188+ version : "dev" ,
189+ platform : process . platform ,
190+ mode : "backend" ,
191+ } ,
192+ role : "operator" ,
193+ scopes : [ "operator.admin" , "operator.read" , "operator.write" ] ,
194+ auth : { token } ,
195+ } ) ;
196+ expect ( connected . ok , JSON . stringify ( connected . error ) ) . toBe ( true ) ;
166197 for ( const [ index , item ] of cases . entries ( ) ) {
167- await client . request ( "sessions.messages.subscribe" , { key : item . sessionKey } ) ;
198+ const subscribed = await client . request ( "sessions.messages.subscribe" , {
199+ key : item . sessionKey ,
200+ } ) ;
201+ expect ( subscribed . ok , JSON . stringify ( subscribed . error ) ) . toBe ( true ) ;
168202 const body = {
169203 model : "openclaw:main" ,
170204 stream : true ,
@@ -224,11 +258,12 @@ describe("Gateway concurrent HTTP streams", () => {
224258 frame . type === "response.completed" || frame . choices ?. [ 0 ] ?. finish_reason === "stop" ,
225259 ) ;
226260 expect ( terminals ) . toHaveLength ( 1 ) ;
227- const result = await client . request < { status : string } > ( "agent.wait" , {
261+ const result = await client . request ( "agent.wait" , {
228262 runId,
229263 timeoutMs : 10_000 ,
230264 } ) ;
231- expect ( result . status ) . toBe ( "ok" ) ;
265+ expect ( result . ok , JSON . stringify ( result . error ) ) . toBe ( true ) ;
266+ expect ( result . payload ) . toMatchObject ( { status : "ok" } ) ;
232267 await vi . waitFor ( ( ) => {
233268 const own = events . filter ( ( event ) => event . runId === runId ) ;
234269 const lifecycle = own . filter ( ( event ) => event . stream === "lifecycle" ) ;
@@ -241,24 +276,22 @@ describe("Gateway concurrent HTTP streams", () => {
241276 }
242277 } ) ;
243278 }
279+ } catch ( error ) {
280+ console . error ( gateway . logs ( ) ) ;
281+ throw error ;
244282 } finally {
245283 abort . abort ( ) ;
246284 try {
247- if ( gateway ) {
248- try {
249- await disconnectGatewayClient ( gateway . client ) ;
250- } finally {
251- await gateway . server . close ( { reason : "concurrent stream proof cleanup" } ) ;
252- }
253- }
285+ client ?. close ( ) ;
286+ await gateway . stopGateway ( ) ;
254287 } finally {
255288 try {
256289 if ( mock ) {
257290 await stopChild ( mock ) ;
258291 }
259292 await Promise . all ( streams . map ( ( stream ) => stream . settled ) ) ;
260293 } finally {
261- await state . cleanup ( ) ;
294+ await gateway . cleanup ( ) ;
262295 }
263296 }
264297 }
0 commit comments