|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { describe, test } from "node:test"; |
| 3 | +import { |
| 4 | + runShutdownSequence, |
| 5 | + type ShutdownDeps, |
| 6 | +} from "../src/main/shutdown.js"; |
| 7 | + |
| 8 | +/** Build stub deps that record call order. */ |
| 9 | +function makeStubDeps(overrides?: Partial<ShutdownDeps>) { |
| 10 | + const calls: string[] = []; |
| 11 | + const deps: ShutdownDeps = { |
| 12 | + updateCheckTimer: null, |
| 13 | + clearUpdateCheckTimer: () => { |
| 14 | + calls.push("clearUpdateCheckTimer"); |
| 15 | + }, |
| 16 | + cloudSocket: { |
| 17 | + stop: () => { |
| 18 | + calls.push("cloudSocket.stop"); |
| 19 | + }, |
| 20 | + }, |
| 21 | + commandExecutor: { |
| 22 | + dispose: () => { |
| 23 | + calls.push("commandExecutor.dispose"); |
| 24 | + }, |
| 25 | + }, |
| 26 | + server: { |
| 27 | + stop: async () => { |
| 28 | + calls.push("server.stop"); |
| 29 | + }, |
| 30 | + }, |
| 31 | + desktopWindow: { |
| 32 | + dispose: () => { |
| 33 | + calls.push("desktopWindow.dispose"); |
| 34 | + }, |
| 35 | + }, |
| 36 | + tray: { |
| 37 | + dispose: () => { |
| 38 | + calls.push("tray.dispose"); |
| 39 | + }, |
| 40 | + }, |
| 41 | + ...overrides, |
| 42 | + }; |
| 43 | + return { deps, calls }; |
| 44 | +} |
| 45 | + |
| 46 | +describe("runShutdownSequence", () => { |
| 47 | + test("clean path: all deps succeed, cleanup steps called in order", async () => { |
| 48 | + const { deps, calls } = makeStubDeps(); |
| 49 | + |
| 50 | + const result = await runShutdownSequence(deps); |
| 51 | + |
| 52 | + assert.equal(result, "clean"); |
| 53 | + assert.deepEqual(calls, [ |
| 54 | + "clearUpdateCheckTimer", |
| 55 | + "cloudSocket.stop", |
| 56 | + "commandExecutor.dispose", |
| 57 | + "server.stop", |
| 58 | + "desktopWindow.dispose", |
| 59 | + "tray.dispose", |
| 60 | + ]); |
| 61 | + }); |
| 62 | + |
| 63 | + test("timeout path: result is 'timed_out' when server.stop never resolves", async () => { |
| 64 | + const { deps } = makeStubDeps({ |
| 65 | + server: { |
| 66 | + stop: () => new Promise<void>(() => {}), // never resolves |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + // Stub setTimeoutFn that fires the callback immediately |
| 71 | + const stubSetTimeout = ((cb: () => void) => { |
| 72 | + cb(); |
| 73 | + return 999 as unknown as ReturnType<typeof setTimeout>; |
| 74 | + }) as unknown as typeof setTimeout; |
| 75 | + |
| 76 | + const result = await runShutdownSequence(deps, { |
| 77 | + setTimeoutFn: stubSetTimeout, |
| 78 | + }); |
| 79 | + |
| 80 | + assert.equal(result, "timed_out"); |
| 81 | + }); |
| 82 | + |
| 83 | + test("failed path: server.stop rejects with an error", async () => { |
| 84 | + const { deps } = makeStubDeps({ |
| 85 | + server: { |
| 86 | + stop: () => Promise.reject(new Error("stop failed")), |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + // Use a setTimeoutFn that never fires so timeout doesn't win |
| 91 | + const neverTimeout = (() => |
| 92 | + 42 as unknown as ReturnType<typeof setTimeout>) as unknown as typeof setTimeout; |
| 93 | + |
| 94 | + const result = await runShutdownSequence(deps, { |
| 95 | + setTimeoutFn: neverTimeout, |
| 96 | + }); |
| 97 | + |
| 98 | + assert.equal(result, "failed"); |
| 99 | + }); |
| 100 | + |
| 101 | + test("timer is cleared after cleanup resolves (no leaked handles)", async () => { |
| 102 | + const { deps } = makeStubDeps(); |
| 103 | + |
| 104 | + let capturedTimerId: ReturnType<typeof setTimeout> | null = null; |
| 105 | + let clearTimeoutCalledWith: unknown = null; |
| 106 | + |
| 107 | + // Monkey-patch clearTimeout to observe the call |
| 108 | + const origClearTimeout = globalThis.clearTimeout; |
| 109 | + globalThis.clearTimeout = ((id: unknown) => { |
| 110 | + clearTimeoutCalledWith = id; |
| 111 | + origClearTimeout(id as ReturnType<typeof setTimeout>); |
| 112 | + }) as typeof clearTimeout; |
| 113 | + |
| 114 | + try { |
| 115 | + // Use a real-ish setTimeoutFn that returns a recognizable timer id |
| 116 | + const stubSetTimeout = ((_cb: () => void, _ms: number) => { |
| 117 | + const id = origClearTimeout.bind( |
| 118 | + null |
| 119 | + ) as unknown as ReturnType<typeof setTimeout>; |
| 120 | + capturedTimerId = 12345 as unknown as ReturnType<typeof setTimeout>; |
| 121 | + return capturedTimerId; |
| 122 | + }) as unknown as typeof setTimeout; |
| 123 | + |
| 124 | + const result = await runShutdownSequence(deps, { |
| 125 | + setTimeoutFn: stubSetTimeout, |
| 126 | + }); |
| 127 | + |
| 128 | + assert.equal(result, "clean"); |
| 129 | + assert.equal( |
| 130 | + clearTimeoutCalledWith, |
| 131 | + capturedTimerId, |
| 132 | + "clearTimeout should be called with the timer id returned by setTimeoutFn" |
| 133 | + ); |
| 134 | + } finally { |
| 135 | + globalThis.clearTimeout = origClearTimeout; |
| 136 | + } |
| 137 | + }); |
| 138 | +}); |
0 commit comments