From 8ccce4d7688fcf48371d78283a25c4e09656cb25 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 10 May 2026 23:56:01 +0100 Subject: [PATCH] test: tighten event loop health assertions --- src/gateway/server/event-loop-health.test.ts | 28 +++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/gateway/server/event-loop-health.test.ts b/src/gateway/server/event-loop-health.test.ts index 37d0e2e8eb3..625a54c7c37 100644 --- a/src/gateway/server/event-loop-health.test.ts +++ b/src/gateway/server/event-loop-health.test.ts @@ -78,6 +78,16 @@ function createMonitorHarness(params?: { cpuMsPerWallMs?: number; utilization?: }; } +function expectSnapshotFields(snapshot: unknown, expected: Record) { + expect(typeof snapshot).toBe("object"); + expect(snapshot).not.toBeNull(); + const actual = snapshot as Record; + for (const [key, value] of Object.entries(expected)) { + expect(actual[key]).toEqual(value); + } + return actual; +} + describe("classifyGatewayEventLoopHealthReasons", () => { it("does not degrade on utilization or CPU from a sub-second sample", () => { expect( @@ -172,7 +182,7 @@ describe("createGatewayEventLoopHealthMonitor", () => { expect(harness.eventLoopUtilization).toHaveBeenCalledTimes(1); harness.setNow(1_000); - expect(harness.monitor.snapshot()).toMatchObject({ + expectSnapshotFields(harness.monitor.snapshot(), { degraded: false, reasons: [], intervalMs: 1_000, @@ -188,7 +198,7 @@ describe("createGatewayEventLoopHealthMonitor", () => { harness.setDelay({ p99Ms: 30 }); harness.setNow(1_000); - expect(harness.monitor.snapshot()).toMatchObject({ + expectSnapshotFields(harness.monitor.snapshot(), { degraded: true, reasons: ["event_loop_utilization", "cpu"], intervalMs: 1_000, @@ -204,7 +214,7 @@ describe("createGatewayEventLoopHealthMonitor", () => { harness.setDelay({ maxMs: 1_500 }); harness.setNow(42); - expect(harness.monitor.snapshot()).toMatchObject({ + expectSnapshotFields(harness.monitor.snapshot(), { degraded: true, reasons: ["event_loop_delay"], intervalMs: 42, @@ -217,7 +227,7 @@ describe("createGatewayEventLoopHealthMonitor", () => { const harness = createMonitorHarness({ cpuMsPerWallMs: 0.1, utilization: 0.2 }); harness.setNow(1_000); - expect(harness.monitor.snapshot()).toMatchObject({ + expectSnapshotFields(harness.monitor.snapshot(), { degraded: false, reasons: [], intervalMs: 1_000, @@ -231,7 +241,7 @@ describe("createGatewayEventLoopHealthMonitor", () => { harness.setNow(1_000); const first = harness.monitor.snapshot(); - expect(first).toEqual(expect.objectContaining({ intervalMs: 1_000 })); + expectSnapshotFields(first, { intervalMs: 1_000 }); expect(harness.cpuUsage).toHaveBeenCalledTimes(3); expect(harness.eventLoopUtilization).toHaveBeenCalledTimes(3); @@ -243,7 +253,7 @@ describe("createGatewayEventLoopHealthMonitor", () => { harness.setNow(2_000); const second = harness.monitor.snapshot(); - expect(second).toEqual(expect.objectContaining({ intervalMs: 1_000 })); + expectSnapshotFields(second, { intervalMs: 1_000 }); expect(second).not.toBe(first); }); @@ -257,7 +267,7 @@ describe("createGatewayEventLoopHealthMonitor", () => { expect(harness.monitor.snapshot()).toBe(first); harness.setNow(2_000); - expect(harness.monitor.snapshot()).toMatchObject({ + expectSnapshotFields(harness.monitor.snapshot(), { degraded: true, reasons: ["event_loop_utilization", "cpu"], intervalMs: 1_000, @@ -272,9 +282,7 @@ describe("createGatewayEventLoopHealthMonitor", () => { const harness = createMonitorHarness({ cpuMsPerWallMs: 0.1, utilization: 0.2 }); harness.setNow(1_000); - expect(harness.monitor.snapshot()).toEqual( - expect.objectContaining({ degraded: false, intervalMs: 1_000 }), - ); + expectSnapshotFields(harness.monitor.snapshot(), { degraded: false, intervalMs: 1_000 }); harness.setNow(1_250); harness.monitor.stop();