From e8dcdded123b31fbc6bcd0fb6b40cf5f2d552b80 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Fri, 29 May 2026 16:32:05 +0900 Subject: [PATCH] fix(background-agent): mock process.exit in forced-exit tests Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- .../background-agent/process-cleanup.test.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/features/background-agent/process-cleanup.test.ts b/src/features/background-agent/process-cleanup.test.ts index f67456274..5dbf48bc0 100644 --- a/src/features/background-agent/process-cleanup.test.ts +++ b/src/features/background-agent/process-cleanup.test.ts @@ -89,6 +89,7 @@ describe("#given process cleanup registration", () => { const sigintListenersBefore = process.listeners("SIGINT") const setTimeoutSpy = spyOn(globalThis, "setTimeout") const clearTimeoutSpy = spyOn(globalThis, "clearTimeout") + const exitSpy = spyOn(process, "exit").mockImplementation((() => undefined) as never) // Re-enable forced exit so we can verify setTimeout/clearTimeout are called __enableScheduledForcedExitForTesting() @@ -109,13 +110,42 @@ describe("#given process cleanup registration", () => { expect(setTimeoutSpy).toHaveBeenCalledTimes(1) expect(clearTimeoutSpy).toHaveBeenCalledTimes(1) + expect(exitSpy).toHaveBeenCalledWith(0) } finally { + exitSpy.mockRestore() setTimeoutSpy.mockRestore() clearTimeoutSpy.mockRestore() __disableScheduledForcedExitForTesting() process.exitCode = 0 } }) + + test("#when cleanup finishes after SIGTERM #then process exits after cleanup", async () => { + const sigtermListenersBefore = process.listeners("SIGTERM") + const exitSpy = spyOn(process, "exit").mockImplementation((() => undefined) as never) + const shutdown = mock(async () => { + await Promise.resolve() + }) + const manager = { shutdown } + registeredManagers.push(manager) + __enableScheduledForcedExitForTesting() + + try { + registerManagerForCleanup(manager) + + const sigtermListener = getNewListener("SIGTERM", sigtermListenersBefore) + + sigtermListener() + await flushMicrotasks() + + expect(shutdown).toHaveBeenCalledTimes(1) + expect(exitSpy).toHaveBeenCalledWith(0) + } finally { + exitSpy.mockRestore() + __disableScheduledForcedExitForTesting() + process.exitCode = 0 + } + }) }) describe("#given multiple cleanup managers", () => {