


























1-import { describe, expect, it } from "vitest";
1+import { afterEach, describe, expect, it, vi } from "vitest";
22import { detectZaiEndpoint } from "./detect.js";
334+const MAX_TIMER_TIMEOUT_MS = 2_147_000_000;
5+46type FetchResponse = { status: number; body?: unknown };
5768function makeFetch(map: Record<string, FetchResponse>) {
@@ -19,6 +21,10 @@ function makeFetch(map: Record<string, FetchResponse>) {
1921}
20222123describe("detectZaiEndpoint", () => {
24+afterEach(() => {
25+vi.restoreAllMocks();
26+});
27+2228it("resolves preferred/fallback endpoints and null when probes fail", async () => {
2329const scenarios: Array<{
2430endpoint?: "global" | "cn" | "coding-global" | "coding-cn";
@@ -114,4 +120,22 @@ describe("detectZaiEndpoint", () => {
114120}
115121}
116122});
123+124+it("caps oversized probe timeouts before scheduling", async () => {
125+const timeoutSpy = vi
126+.spyOn(globalThis, "setTimeout")
127+.mockImplementation((() => 1) as typeof setTimeout);
128+vi.spyOn(globalThis, "clearTimeout").mockImplementation(() => undefined);
129+const fetchFn = makeFetch({
130+"https://api.z.ai/api/paas/v4/chat/completions::glm-5.1": { status: 200 },
131+});
132+133+await detectZaiEndpoint({
134+apiKey: "sk-test", // pragma: allowlist secret
135+ fetchFn,
136+timeoutMs: MAX_TIMER_TIMEOUT_MS + 1_000_000,
137+});
138+139+expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
140+});
117141});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。