


























@@ -10,6 +10,7 @@ vi.mock("../logging/subsystem.js", () => ({
1010}));
11111212import { buildTimeoutAbortSignal, fetchWithTimeout } from "./fetch-timeout.js";
13+import { MAX_SAFE_TIMEOUT_DELAY_MS } from "./timer-delay.js";
13141415function requireWarnCall(callIndex: number): [string, Record<string, unknown>] {
1516const call = warn.mock.calls[callIndex];
@@ -165,6 +166,27 @@ describe("buildTimeoutAbortSignal", () => {
165166await assertion;
166167});
167168169+it("clamps oversized fetchWithTimeout delays before fetch starts", async () => {
170+const timeoutSpy = vi.spyOn(globalThis, "setTimeout");
171+const response = new Response("ok");
172+const fetchFn = vi.fn<typeof fetch>(async (_input, init) => {
173+expect(init?.signal?.aborted).toBe(false);
174+return response;
175+});
176+177+try {
178+await expect(
179+fetchWithTimeout("https://example.com/v1/slow", {}, MAX_SAFE_TIMEOUT_DELAY_MS + 1, fetchFn),
180+).resolves.toBe(response);
181+182+expect(fetchFn).toHaveBeenCalledTimes(1);
183+expect(timeoutSpy.mock.calls[0]?.[1]).toBe(MAX_SAFE_TIMEOUT_DELAY_MS);
184+expect(timeoutSpy.mock.calls[0]?.[3]).toBe(MAX_SAFE_TIMEOUT_DELAY_MS);
185+} finally {
186+timeoutSpy.mockRestore();
187+}
188+});
189+168190it("does not log when a parent signal aborts first", async () => {
169191const parent = new AbortController();
170192const { signal, cleanup } = buildTimeoutAbortSignal({
@@ -220,4 +242,25 @@ describe("buildTimeoutAbortSignal", () => {
220242221243cleanup();
222244});
245+246+it("clamps oversized timeouts before arming Node timers", async () => {
247+const timeoutSpy = vi.spyOn(globalThis, "setTimeout");
248+249+const { signal, cleanup } = buildTimeoutAbortSignal({
250+timeoutMs: MAX_SAFE_TIMEOUT_DELAY_MS + 1,
251+operation: "unit-test",
252+});
253+254+try {
255+expect(timeoutSpy.mock.calls[0]?.[1]).toBe(MAX_SAFE_TIMEOUT_DELAY_MS);
256+expect(timeoutSpy.mock.calls[0]?.[3]).toBe(MAX_SAFE_TIMEOUT_DELAY_MS);
257+258+await vi.advanceTimersByTimeAsync(1);
259+260+expect(signal?.aborted).toBe(false);
261+} finally {
262+cleanup();
263+timeoutSpy.mockRestore();
264+}
265+});
223266});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。