From 185ace0155c371392bb7f8979db3f7cc91c8b01c Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 12 May 2026 15:12:36 +0100 Subject: [PATCH] test: dedupe synology chat http mock calls --- extensions/synology-chat/src/client.test.ts | 50 +++++++++++---------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/extensions/synology-chat/src/client.test.ts b/extensions/synology-chat/src/client.test.ts index 9bcba9178ce..7c9fc365cfd 100644 --- a/extensions/synology-chat/src/client.test.ts +++ b/extensions/synology-chat/src/client.test.ts @@ -38,6 +38,27 @@ type MockRequestHandler = ( options: RequestOptions, callback?: RequestCallback, ) => ClientRequest; +type MockHttpCall = [ + string | URL, + RequestOptions & { rejectUnauthorized?: boolean }, + RequestCallback?, +]; + +function firstHttpsRequestCall(label = "Synology Chat HTTPS request"): MockHttpCall { + const call = vi.mocked(https.request).mock.calls.at(0); + if (!call) { + throw new Error(`expected ${label}`); + } + return call as MockHttpCall; +} + +function firstHttpsGetCall(label = "Synology Chat HTTPS get"): MockHttpCall { + const call = vi.mocked(https.get).mock.calls.at(0); + if (!call) { + throw new Error(`expected ${label}`); + } + return call as MockHttpCall; +} function createMockResponseEmitter(statusCode: number): IncomingMessage { const res = new EventEmitter() as Partial; @@ -120,11 +141,7 @@ describe("Synology Chat TLS verification defaults", () => { it.each(tlsVerificationDefaultCases)("$name verifies TLS by default", async ({ invoke }) => { mockSuccessResponse(); await settleTimers(invoke()); - const httpsRequest = vi.mocked(https.request); - const firstCall = httpsRequest.mock.calls[0]; - if (!firstCall) { - throw new Error("expected Synology Chat HTTPS request"); - } + const firstCall = firstHttpsRequestCall(); expect(firstCall[1]?.rejectUnauthorized).toBe(true); }); }); @@ -147,20 +164,15 @@ describe("sendMessage", () => { it("includes user_ids when userId is numeric", async () => { mockSuccessResponse(); await settleTimers(sendMessage("https://nas.example.com/incoming", "Hello", 42)); - const httpsRequest = vi.mocked(https.request); - expect(httpsRequest).toHaveBeenCalled(); - const callArgs = httpsRequest.mock.calls[0]; + expect(vi.mocked(https.request)).toHaveBeenCalled(); + const callArgs = firstHttpsRequestCall(); expect(callArgs[0]).toBe("https://nas.example.com/incoming"); }); it("only disables TLS verification when explicitly requested", async () => { mockSuccessResponse(); await settleTimers(sendMessage("https://nas.example.com/incoming", "Hello", undefined, true)); - const httpsRequest = vi.mocked(https.request); - const firstCall = httpsRequest.mock.calls[0]; - if (!firstCall) { - throw new Error("expected Synology Chat HTTPS request"); - } + const firstCall = firstHttpsRequestCall(); expect(firstCall[1]?.rejectUnauthorized).toBe(false); }); }); @@ -332,11 +344,7 @@ describe("resolveLegacyWebhookNameToChatUserId", () => { incomingUrl: baseUrl, mutableWebhookUsername: "anyone", }); - const httpsGet = vi.mocked(https.get); - const call = httpsGet.mock.calls[0]; - if (!call) { - throw new Error("expected Synology Chat user_list request"); - } + const call = firstHttpsGetCall("Synology Chat user_list request"); expect(String(call[0])).toBe(baseUrl.replace("method=chatbot", "method=user_list")); expect(call[1]).toEqual({ rejectUnauthorized: true }); expect(typeof call[2]).toBe("function"); @@ -385,11 +393,7 @@ describe("fetchChatUsers", () => { await fetchChatUsers(freshUrl); - const httpsGet = vi.mocked(https.get); - const firstCall = httpsGet.mock.calls[0]; - if (!firstCall) { - throw new Error("expected Synology Chat HTTPS get"); - } + const firstCall = firstHttpsGetCall(); expect(firstCall[1]?.rejectUnauthorized).toBe(true); }); });