1- import { spawnSync } from "node:child_process" ;
1+ import { spawn , spawnSync , type ChildProcess } from "node:child_process" ;
22import fs from "node:fs" ;
33import os from "node:os" ;
44import path from "node:path" ;
@@ -10,6 +10,38 @@ import {
1010 shouldUseCmdExeForCommand ,
1111} from "../../scripts/ui.js" ;
1212
13+ async function waitFor ( predicate : ( ) => boolean , label : string , timeoutMs = 3_000 ) : Promise < void > {
14+ const startedAt = Date . now ( ) ;
15+ while ( ! predicate ( ) ) {
16+ if ( Date . now ( ) - startedAt > timeoutMs ) {
17+ throw new Error ( `timed out waiting for ${ label } ` ) ;
18+ }
19+ await new Promise < void > ( ( resolve ) => {
20+ setTimeout ( resolve , 25 ) ;
21+ } ) ;
22+ }
23+ }
24+
25+ async function waitForExit (
26+ child : ChildProcess ,
27+ timeoutMs = 3_000 ,
28+ ) : Promise < { code : number | null ; signal : NodeJS . Signals | null } > {
29+ return await new Promise ( ( resolve , reject ) => {
30+ const timer = setTimeout ( ( ) => {
31+ child . kill ( "SIGKILL" ) ;
32+ reject ( new Error ( "timed out waiting for child exit" ) ) ;
33+ } , timeoutMs ) ;
34+ child . once ( "exit" , ( code , signal ) => {
35+ clearTimeout ( timer ) ;
36+ resolve ( { code, signal } ) ;
37+ } ) ;
38+ child . once ( "error" , ( error ) => {
39+ clearTimeout ( timer ) ;
40+ reject ( error ) ;
41+ } ) ;
42+ } ) ;
43+ }
44+
1345describe ( "scripts/ui windows spawn behavior" , ( ) => {
1446 it ( "wraps Windows command launchers with cmd.exe without enabling shell mode" , ( ) => {
1547 expect (
@@ -160,4 +192,51 @@ describe("scripts/ui windows spawn behavior", () => {
160192 expect ( output ) . not . toContain ( "Missing UI runner" ) ;
161193 expect ( output ) . toContain ( "vite" ) ;
162194 } ) ;
195+
196+ it . runIf ( process . platform !== "win32" ) (
197+ "terminates the pnpm child on wrapper SIGTERM" ,
198+ async ( ) => {
199+ const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-ui-wrapper-signals-" ) ) ;
200+ const runnerPath = path . join ( tempDir , "pnpm.mjs" ) ;
201+ const readyFile = path . join ( tempDir , "ready" ) ;
202+ const signaledFile = path . join ( tempDir , "signaled" ) ;
203+
204+ fs . writeFileSync (
205+ runnerPath ,
206+ [
207+ "import fs from 'node:fs';" ,
208+ "process.on('SIGTERM', () => {" ,
209+ " fs.writeFileSync(process.env.SIGNALED_FILE, 'SIGTERM');" ,
210+ " setTimeout(() => process.exit(0), 25);" ,
211+ "});" ,
212+ "fs.writeFileSync(process.env.READY_FILE, process.argv.slice(2).join(' '));" ,
213+ "setInterval(() => {}, 1000);" ,
214+ ] . join ( "\n" ) ,
215+ ) ;
216+
217+ const wrapper = spawn ( process . execPath , [ "scripts/ui.js" , "install" ] , {
218+ cwd : path . resolve ( "." ) ,
219+ env : {
220+ ...process . env ,
221+ npm_execpath : runnerPath ,
222+ READY_FILE : readyFile ,
223+ SIGNALED_FILE : signaledFile ,
224+ } ,
225+ stdio : "ignore" ,
226+ } ) ;
227+
228+ try {
229+ await waitFor ( ( ) => fs . existsSync ( readyFile ) , "UI runner readiness" ) ;
230+ expect ( fs . readFileSync ( readyFile , "utf8" ) ) . toBe ( "install" ) ;
231+ wrapper . kill ( "SIGTERM" ) ;
232+
233+ const exit = await waitForExit ( wrapper ) ;
234+ expect ( exit ) . toEqual ( { code : null , signal : "SIGTERM" } ) ;
235+ expect ( fs . readFileSync ( signaledFile , "utf8" ) ) . toBe ( "SIGTERM" ) ;
236+ } finally {
237+ wrapper . kill ( "SIGKILL" ) ;
238+ fs . rmSync ( tempDir , { force : true , recursive : true } ) ;
239+ }
240+ } ,
241+ ) ;
163242} ) ;
0 commit comments