From b92388b7083850e5de2a799a28fb662eba6bf2bb Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 11 May 2026 05:23:50 +0100 Subject: [PATCH] test: tighten fatal rejection assertions --- ...handled-rejections.fatal-detection.test.ts | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/infra/unhandled-rejections.fatal-detection.test.ts b/src/infra/unhandled-rejections.fatal-detection.test.ts index f010dfbbd527..e694e2c71e44 100644 --- a/src/infra/unhandled-rejections.fatal-detection.test.ts +++ b/src/infra/unhandled-rejections.fatal-detection.test.ts @@ -54,6 +54,16 @@ describe("installUnhandledRejectionHandler - fatal detection", () => { process.emit("unhandledRejection", reason, Promise.resolve()); } + function expectConsoleLogWithMessage( + spy: ReturnType, + label: string, + message: string, + ): void { + const call = spy.mock.calls.find((entry: unknown[]) => entry[0] === label); + expect(call?.[0]).toBe(label); + expect(String(call?.[1])).toContain(message); + } + function expectExitCodeFromUnhandled( reason: unknown, expected: number[], @@ -88,9 +98,10 @@ describe("installUnhandledRejectionHandler - fatal detection", () => { ); } - expect(consoleErrorSpy).toHaveBeenCalledWith( + expectConsoleLogWithMessage( + consoleErrorSpy, "[openclaw] FATAL unhandled rejection:", - expect.stringContaining("Out of memory"), + "Out of memory", ); }); }); @@ -124,9 +135,10 @@ describe("installUnhandledRejectionHandler - fatal detection", () => { ); } - expect(consoleErrorSpy).toHaveBeenCalledWith( + expectConsoleLogWithMessage( + consoleErrorSpy, "[openclaw] CONFIGURATION ERROR - requires fix:", - expect.stringContaining("Invalid config"), + "Invalid config", ); }); }); @@ -167,9 +179,10 @@ describe("installUnhandledRejectionHandler - fatal detection", () => { expectExitCodeFromUnhandled(transientErr, []); } - expect(consoleWarnSpy).toHaveBeenCalledWith( + expectConsoleLogWithMessage( + consoleWarnSpy, "[openclaw] Non-fatal unhandled rejection (continuing):", - expect.stringContaining("fetch failed"), + "fetch failed", ); }); @@ -190,9 +203,10 @@ describe("installUnhandledRejectionHandler - fatal detection", () => { expectExitCodeFromUnhandled(sqliteErr, []); } - expect(consoleWarnSpy).toHaveBeenCalledWith( + expectConsoleLogWithMessage( + consoleWarnSpy, "[openclaw] Non-fatal unhandled rejection (continuing):", - expect.stringContaining("unable to open database file"), + "unable to open database file", ); }); @@ -200,9 +214,10 @@ describe("installUnhandledRejectionHandler - fatal detection", () => { const genericErr = new Error("Something went wrong"); expectExitCodeFromUnhandled(genericErr, [1], "unhandled rejection"); - expect(consoleErrorSpy).toHaveBeenCalledWith( + expectConsoleLogWithMessage( + consoleErrorSpy, "[openclaw] Unhandled promise rejection:", - expect.stringContaining("Something went wrong"), + "Something went wrong", ); }); @@ -222,9 +237,10 @@ describe("installUnhandledRejectionHandler - fatal detection", () => { abortErr.name = "AbortError"; expectExitCodeFromUnhandled(abortErr, []); - expect(consoleWarnSpy).toHaveBeenCalledWith( + expectConsoleLogWithMessage( + consoleWarnSpy, "[openclaw] Suppressed AbortError:", - expect.stringContaining("This operation was aborted"), + "This operation was aborted", ); }); });