@@ -5,13 +5,16 @@ import {
55 spawnSync ,
66 type ChildProcessWithoutNullStreams ,
77} from "node:child_process" ;
8- import { mkdtempSync , rmSync } from "node:fs" ;
98import { createServer } from "node:http" ;
10- import { tmpdir } from "node:os" ;
119import path from "node:path" ;
1210import { promisify } from "node:util" ;
1311import { describe , expect , it } from "vitest" ;
1412import { type RawData , WebSocketServer } from "ws" ;
13+ import {
14+ closeOpenClawStateDatabaseByPath ,
15+ openOpenClawStateDatabase ,
16+ } from "../state/openclaw-state-db.js" ;
17+ import { createOpenClawTestState } from "../test-utils/openclaw-test-state.js" ;
1518
1619const execFileAsync = promisify ( execFile ) ;
1720const CHILD_PROCESS_TIMEOUT_MS = 30_000 ;
@@ -29,16 +32,32 @@ const INITIALIZE_FRAME = {
2932 } ,
3033} ;
3134
32- function createAcpProcessEnv ( stateDir ?: string ) : NodeJS . ProcessEnv {
35+ async function createPreparedAcpProcessState ( ) {
36+ const state = await createOpenClawTestState ( {
37+ applyEnv : false ,
38+ label : "acp-process" ,
39+ scenario : "minimal" ,
40+ } ) ;
41+ try {
42+ // These cases assert bridge stderr after normal startup. Prepare canonical
43+ // shared state so the one-time migration diagnostic is not part of that signal.
44+ const database = openOpenClawStateDatabase ( { env : state . env } ) ;
45+ closeOpenClawStateDatabaseByPath ( database . path ) ;
46+ return state ;
47+ } catch ( error ) {
48+ await state . cleanup ( ) ;
49+ throw error ;
50+ }
51+ }
52+
53+ function createAcpProcessEnv ( baseEnv : NodeJS . ProcessEnv ) : NodeJS . ProcessEnv {
3354 return {
34- ...process . env ,
55+ ...baseEnv ,
3556 NODE_ENV : undefined ,
3657 NODE_OPTIONS : "--use-openssl-ca" ,
3758 NODE_USE_SYSTEM_CA : "0" ,
3859 OPENCLAW_DISABLE_BUNDLED_PLUGINS : "1" ,
39- OPENCLAW_CONFIG_PATH : stateDir ? path . join ( stateDir , "openclaw.json" ) : undefined ,
4060 OPENCLAW_NO_RESPAWN : "1" ,
41- OPENCLAW_STATE_DIR : stateDir ,
4261 VITEST : undefined ,
4362 } ;
4463}
@@ -102,24 +121,29 @@ describe("ACP CLI process exit", () => {
102121 ] ) (
103122 "exits promptly after $args" ,
104123 async ( { args, usage } ) => {
105- const result = await execFileAsync (
106- process . execPath ,
107- [ "--import" , "tsx" , "src/entry.ts" , ...args ] ,
108- {
109- cwd : path . resolve ( "." ) ,
110- encoding : "utf8" ,
111- env : {
112- ...createAcpProcessEnv ( ) ,
113- NODE_OPTIONS : process . platform === "darwin" ? "--use-system-ca" : undefined ,
114- NODE_USE_SYSTEM_CA : undefined ,
124+ const state = await createPreparedAcpProcessState ( ) ;
125+ try {
126+ const result = await execFileAsync (
127+ process . execPath ,
128+ [ "--import" , "tsx" , "src/entry.ts" , ...args ] ,
129+ {
130+ cwd : path . resolve ( "." ) ,
131+ encoding : "utf8" ,
132+ env : {
133+ ...createAcpProcessEnv ( state . env ) ,
134+ NODE_OPTIONS : process . platform === "darwin" ? "--use-system-ca" : undefined ,
135+ NODE_USE_SYSTEM_CA : undefined ,
136+ } ,
137+ killSignal : "SIGKILL" ,
138+ timeout : CHILD_PROCESS_TIMEOUT_MS ,
115139 } ,
116- killSignal : "SIGKILL" ,
117- timeout : CHILD_PROCESS_TIMEOUT_MS ,
118- } ,
119- ) ;
140+ ) ;
120141
121- expect ( result . stderr ) . toBe ( "" ) ;
122- expect ( result . stdout ) . toContain ( usage ) ;
142+ expect ( result . stderr ) . toBe ( "" ) ;
143+ expect ( result . stdout ) . toContain ( usage ) ;
144+ } finally {
145+ await state . cleanup ( ) ;
146+ }
123147 } ,
124148 CHILD_PROCESS_TIMEOUT_MS + 5_000 ,
125149 ) ;
@@ -130,28 +154,33 @@ describe("ACP CLI process exit", () => {
130154 name : "an initialize frame" ,
131155 input : `${ JSON . stringify ( INITIALIZE_FRAME ) } \n` ,
132156 } ,
133- ] ) ( "exits when the bridge starts with $name and the client disconnects" , ( { input } ) => {
134- const result = spawnSync (
135- process . execPath ,
136- [ "--import" , "tsx" , "src/entry.ts" , "acp" , "--require-existing" ] ,
137- {
138- cwd : path . resolve ( "." ) ,
139- encoding : "utf8" ,
140- env : createAcpProcessEnv ( ) ,
141- input,
142- killSignal : "SIGKILL" ,
143- timeout : CHILD_PROCESS_TIMEOUT_MS ,
144- } ,
145- ) ;
157+ ] ) ( "exits when the bridge starts with $name and the client disconnects" , async ( { input } ) => {
158+ const state = await createPreparedAcpProcessState ( ) ;
159+ try {
160+ const result = spawnSync (
161+ process . execPath ,
162+ [ "--import" , "tsx" , "src/entry.ts" , "acp" , "--require-existing" ] ,
163+ {
164+ cwd : path . resolve ( "." ) ,
165+ encoding : "utf8" ,
166+ env : createAcpProcessEnv ( state . env ) ,
167+ input,
168+ killSignal : "SIGKILL" ,
169+ timeout : CHILD_PROCESS_TIMEOUT_MS ,
170+ } ,
171+ ) ;
146172
147- expect ( result . error ) . toBeUndefined ( ) ;
148- expect ( result . signal ) . toBeNull ( ) ;
149- expect ( result . status ) . toBe ( 0 ) ;
150- expect ( result . stderr ) . toBe ( "" ) ;
173+ expect ( result . error ) . toBeUndefined ( ) ;
174+ expect ( result . signal ) . toBeNull ( ) ;
175+ expect ( result . status ) . toBe ( 0 ) ;
176+ expect ( result . stderr ) . toBe ( "" ) ;
177+ } finally {
178+ await state . cleanup ( ) ;
179+ }
151180 } ) ;
152181
153182 it ( "processes an initialize frame buffered before Gateway hello" , async ( ) => {
154- const stateDir = mkdtempSync ( path . join ( tmpdir ( ) , "openclaw-acp-exit-" ) ) ;
183+ const state = await createPreparedAcpProcessState ( ) ;
155184 const server = createServer ( ) ;
156185 const wss = new WebSocketServer ( { server } ) ;
157186 let child : ChildProcessWithoutNullStreams | undefined ;
@@ -219,7 +248,7 @@ describe("ACP CLI process exit", () => {
219248 ] ,
220249 {
221250 cwd : path . resolve ( "." ) ,
222- env : createAcpProcessEnv ( stateDir ) ,
251+ env : createAcpProcessEnv ( state . env ) ,
223252 stdio : [ "pipe" , "pipe" , "pipe" ] ,
224253 } ,
225254 ) ;
@@ -255,7 +284,7 @@ describe("ACP CLI process exit", () => {
255284 await new Promise < void > ( ( resolve , reject ) => {
256285 server . close ( ( error ) => ( error ? reject ( error ) : resolve ( ) ) ) ;
257286 } ) ;
258- rmSync ( stateDir , { force : true , recursive : true } ) ;
287+ await state . cleanup ( ) ;
259288 }
260289 } , 40_000 ) ;
261290} ) ;
0 commit comments