From 1bdb59e201a26b53f5ed8b2eb870941ca4b788f7 Mon Sep 17 00:00:00 2001 From: N0zoM1z0 <1705004398@qq.com> Date: Tue, 14 Jul 2026 21:20:24 +0000 Subject: [PATCH] test(runtime): cover idempotent handler cleanup --- tests/mainErrors.test.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/mainErrors.test.ts b/tests/mainErrors.test.ts index b11614c6b0..b0ee88447e 100644 --- a/tests/mainErrors.test.ts +++ b/tests/mainErrors.test.ts @@ -61,6 +61,41 @@ describe("MCP runtime errors", () => { expect(shutdownUnregistrations).toEqual(["shutdown"]); }); + it("unregisters every process-lifetime handler during idempotent shutdown", async () => { + const shutdown: Array<() => void> = []; + const reload: Array<() => void> = []; + const unregistrations: string[] = []; + let closeCalls = 0; + const runtime: RuntimeDependencies = { + ...dependencies( + () => ({ + close: async () => { + closeCalls += 1; + }, + }), + [], + shutdown, + [], + unregistrations, + ), + registerReload: (handler) => { + reload.push(handler); + return () => unregistrations.push("reload"); + }, + }; + + expect(await run(runtime)).toBe(0); + expect(shutdown).toHaveLength(1); + expect(reload).toHaveLength(1); + + shutdown[0]?.(); + shutdown[0]?.(); + await nextTurn(); + + expect(closeCalls).toBe(1); + expect(unregistrations).toEqual(["reload", "shutdown"]); + }); + it("reports transport startup failure without its cause", async () => { const output: string[] = []; expect(