@@ -3,11 +3,20 @@ import { EventEmitter } from "node:events";
33import { expectDefined } from "@openclaw/normalization-core" ;
44import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
55
6+ // Tracks spawned children by pid so the killProcessTree mock can emit close on them.
7+ const spawnRegistry = new Map < number , EventEmitter > ( ) ;
8+
69const mocks = vi . hoisted ( ( ) => ( {
710 hasBinary : vi . fn ( ( ) => true ) ,
811 resolveExecutable : vi . fn ( ( name : string ) => name ) ,
912 runCommandWithTimeout : vi . fn ( ) ,
1013 spawn : vi . fn ( ) ,
14+ killProcessTree : vi . fn ( ( pid : number ) => {
15+ const child = spawnRegistry . get ( pid ) ;
16+ if ( child ) {
17+ queueMicrotask ( ( ) => child . emit ( "close" , 0 , null ) ) ;
18+ }
19+ } ) ,
1120} ) ) ;
1221
1322vi . mock ( "node:child_process" , async ( ) => {
@@ -30,6 +39,10 @@ vi.mock("../process/exec.js", () => ({
3039 runCommandWithTimeout : mocks . runCommandWithTimeout ,
3140} ) ) ;
3241
42+ vi . mock ( "../process/kill-tree.js" , ( ) => ( {
43+ killProcessTree : mocks . killProcessTree ,
44+ } ) ) ;
45+
3346const { startGmailWatcher, stopGmailWatcher } = await import ( "./gmail-watcher.js" ) ;
3447
3548function createGmailConfig ( account = "[email protected] " , renewEveryMinutes ?:
number ) { @@ -61,16 +74,23 @@ type MockWatcherChild = EventEmitter & {
6174 stderr : EventEmitter ;
6275} ;
6376
77+ let nextMockPid = 1234 ;
78+
6479function createMockWatcherChild ( spawned = true ) : MockWatcherChild {
6580 const child = new EventEmitter ( ) ;
66- return Object . assign ( child , {
81+ const pid = spawned ? nextMockPid ++ : undefined ;
82+ const mockedChild = Object . assign ( child , {
6783 stderr : new EventEmitter ( ) ,
6884 kill : vi . fn ( ( ) => {
6985 queueMicrotask ( ( ) => child . emit ( "exit" , null , "SIGTERM" ) ) ;
7086 return true ;
7187 } ) ,
72- ...( spawned ? { pid : 1234 } : { } ) ,
88+ ...( pid !== undefined ? { pid } : { } ) ,
7389 } ) ;
90+ if ( pid !== undefined ) {
91+ spawnRegistry . set ( pid , mockedChild ) ;
92+ }
93+ return mockedChild ;
7494}
7595
7696async function startMockWatcher ( spawned = true ) : Promise < MockWatcherChild [ ] > {
@@ -87,11 +107,21 @@ async function startMockWatcher(spawned = true): Promise<MockWatcherChild[]> {
87107
88108describe ( "startGmailWatcher" , ( ) => {
89109 beforeEach ( async ( ) => {
110+ // stopGmailWatcher uses the killProcessTree mock from the previous beforeEach run,
111+ // which looks up spawnRegistry entries populated by that test's children.
90112 await stopGmailWatcher ( ) ;
113+ spawnRegistry . clear ( ) ;
91114 mocks . hasBinary . mockReturnValue ( true ) ;
92115 mocks . resolveExecutable . mockImplementation ( ( name : string ) => name ) ;
93116 mocks . runCommandWithTimeout . mockReset ( ) ;
94117 mocks . spawn . mockReset ( ) ;
118+ mocks . killProcessTree . mockReset ( ) ;
119+ mocks . killProcessTree . mockImplementation ( ( pid : number ) => {
120+ const child = spawnRegistry . get ( pid ) ;
121+ if ( child ) {
122+ queueMicrotask ( ( ) => child . emit ( "close" , 0 , null ) ) ;
123+ }
124+ } ) ;
95125 mocks . spawn . mockImplementation ( ( ) => {
96126 const child = new EventEmitter ( ) ;
97127 return Object . assign ( child , {
@@ -377,19 +407,16 @@ describe("startGmailWatcher", () => {
377407 }
378408 } ) ;
379409
380- it ( "escalates to SIGKILL and resolves on final timeout when process ignores signals" , async ( ) => {
410+ it ( "uses killProcessTree for gog shutdown and resolves on final timeout when process ignores signals" , async ( ) => {
381411 vi . useFakeTimers ( ) ;
382412 try {
383413 mocks . runCommandWithTimeout . mockResolvedValue ( { code : 0 , stdout : "" , stderr : "" } ) ;
384414
385- // Spawn a process that never emits exit/close/error
415+ // Spawn a process with a known pid that never emits exit/close/error
386416 const stubbornChild = new EventEmitter ( ) ;
387- const killCalls : string [ ] = [ ] ;
388417 Object . assign ( stubbornChild , {
389- kill : vi . fn ( ( sig : string ) => {
390- killCalls . push ( sig ) ;
391- return true ;
392- } ) ,
418+ pid : 9999 ,
419+ kill : vi . fn ( ( ) => true ) ,
393420 killed : false ,
394421 } ) ;
395422 mocks . spawn . mockReturnValueOnce ( stubbornChild ) ;
@@ -409,17 +436,17 @@ describe("startGmailWatcher", () => {
409436 } ) ;
410437 } ) ;
411438
412- // Re-entry starts settle on stubbornChild
439+ // Re-entry starts settle on stubbornChild; advance past the 8 s final
440+ // timeout (stubbornChild never emits exit), then verify the outcome.
413441 const startPromise = startGmailWatcher ( createGmailConfig ( ) ) ;
414-
415- // After 3s the escalation fires SIGKILL
416- await vi . advanceTimersByTimeAsync ( 3_000 ) ;
417- expect ( killCalls ) . toContain ( "SIGTERM" ) ;
418- expect ( killCalls ) . toContain ( "SIGKILL" ) ;
419-
420- // After 8s total the final timeout resolves even though exit never fired
421- await vi . advanceTimersByTimeAsync ( 5_000 ) ;
442+ await vi . advanceTimersByTimeAsync ( 8_000 ) ;
422443 await expect ( startPromise ) . resolves . toEqual ( { started : true } ) ;
444+
445+ // killProcessTree must have been called with stubbornChild's pid before settle gave up.
446+ expect ( mocks . killProcessTree ) . toHaveBeenCalledWith (
447+ 9999 ,
448+ expect . objectContaining ( { graceMs : 3_000 } ) ,
449+ ) ;
423450 } finally {
424451 vi . useRealTimers ( ) ;
425452 }
@@ -463,6 +490,24 @@ describe("startGmailWatcher", () => {
463490 }
464491 } ) ;
465492
493+ it ( "calls killProcessTree (not proc.kill) when stopping the gog watcher" , async ( ) => {
494+ mocks . runCommandWithTimeout . mockResolvedValue ( { code : 0 , stdout : "" , stderr : "" } ) ;
495+ const child = createMockWatcherChild ( ) ;
496+ mocks . spawn . mockReturnValueOnce ( child ) ;
497+
498+ await startGmailWatcher ( createGmailConfig ( ) ) ;
499+ expect ( mocks . spawn ) . toHaveBeenCalledTimes ( 1 ) ;
500+
501+ await stopGmailWatcher ( ) ;
502+
503+ expect ( mocks . killProcessTree ) . toHaveBeenCalledWith (
504+ child . pid ,
505+ expect . objectContaining ( { graceMs : 3_000 } ) ,
506+ ) ;
507+ // proc.kill should not be called — tree termination replaces the direct kill.
508+ expect ( child . kill ) . not . toHaveBeenCalled ( ) ;
509+ } ) ;
510+
466511 it ( "swallows stdout and stderr stream errors without crashing" , async ( ) => {
467512 mocks . runCommandWithTimeout . mockResolvedValue ( { code : 0 , stdout : "" , stderr : "" } ) ;
468513 let stdout : EventEmitter | undefined ;
0 commit comments