
















@@ -38,6 +38,27 @@ type MockRequestHandler = (
3838options: RequestOptions,
3939callback?: RequestCallback,
4040) => ClientRequest;
41+type MockHttpCall = [
42+string | URL,
43+RequestOptions & { rejectUnauthorized?: boolean },
44+RequestCallback?,
45+];
46+47+function firstHttpsRequestCall(label = "Synology Chat HTTPS request"): MockHttpCall {
48+const call = vi.mocked(https.request).mock.calls.at(0);
49+if (!call) {
50+throw new Error(`expected ${label}`);
51+}
52+return call as MockHttpCall;
53+}
54+55+function firstHttpsGetCall(label = "Synology Chat HTTPS get"): MockHttpCall {
56+const call = vi.mocked(https.get).mock.calls.at(0);
57+if (!call) {
58+throw new Error(`expected ${label}`);
59+}
60+return call as MockHttpCall;
61+}
41624263function createMockResponseEmitter(statusCode: number): IncomingMessage {
4364const res = new EventEmitter() as Partial<IncomingMessage>;
@@ -120,11 +141,7 @@ describe("Synology Chat TLS verification defaults", () => {
120141it.each(tlsVerificationDefaultCases)("$name verifies TLS by default", async ({ invoke }) => {
121142mockSuccessResponse();
122143await settleTimers(invoke());
123-const httpsRequest = vi.mocked(https.request);
124-const firstCall = httpsRequest.mock.calls[0];
125-if (!firstCall) {
126-throw new Error("expected Synology Chat HTTPS request");
127-}
144+const firstCall = firstHttpsRequestCall();
128145expect(firstCall[1]?.rejectUnauthorized).toBe(true);
129146});
130147});
@@ -147,20 +164,15 @@ describe("sendMessage", () => {
147164it("includes user_ids when userId is numeric", async () => {
148165mockSuccessResponse();
149166await settleTimers(sendMessage("https://nas.example.com/incoming", "Hello", 42));
150-const httpsRequest = vi.mocked(https.request);
151-expect(httpsRequest).toHaveBeenCalled();
152-const callArgs = httpsRequest.mock.calls[0];
167+expect(vi.mocked(https.request)).toHaveBeenCalled();
168+const callArgs = firstHttpsRequestCall();
153169expect(callArgs[0]).toBe("https://nas.example.com/incoming");
154170});
155171156172it("only disables TLS verification when explicitly requested", async () => {
157173mockSuccessResponse();
158174await settleTimers(sendMessage("https://nas.example.com/incoming", "Hello", undefined, true));
159-const httpsRequest = vi.mocked(https.request);
160-const firstCall = httpsRequest.mock.calls[0];
161-if (!firstCall) {
162-throw new Error("expected Synology Chat HTTPS request");
163-}
175+const firstCall = firstHttpsRequestCall();
164176expect(firstCall[1]?.rejectUnauthorized).toBe(false);
165177});
166178});
@@ -332,11 +344,7 @@ describe("resolveLegacyWebhookNameToChatUserId", () => {
332344incomingUrl: baseUrl,
333345mutableWebhookUsername: "anyone",
334346});
335-const httpsGet = vi.mocked(https.get);
336-const call = httpsGet.mock.calls[0];
337-if (!call) {
338-throw new Error("expected Synology Chat user_list request");
339-}
347+const call = firstHttpsGetCall("Synology Chat user_list request");
340348expect(String(call[0])).toBe(baseUrl.replace("method=chatbot", "method=user_list"));
341349expect(call[1]).toEqual({ rejectUnauthorized: true });
342350expect(typeof call[2]).toBe("function");
@@ -385,11 +393,7 @@ describe("fetchChatUsers", () => {
385393386394await fetchChatUsers(freshUrl);
387395388-const httpsGet = vi.mocked(https.get);
389-const firstCall = httpsGet.mock.calls[0];
390-if (!firstCall) {
391-throw new Error("expected Synology Chat HTTPS get");
392-}
396+const firstCall = firstHttpsGetCall();
393397expect(firstCall[1]?.rejectUnauthorized).toBe(true);
394398});
395399});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。