import { afterEach, describe, expect, it, vi } from "vitest"; import * as codex from "../codex"; import * as google from "../google"; import * as minimax from "../minimax"; import * as parallel from "../parallel"; import { searchWeb } from "../search"; import * as zai from "../zai"; afterEach(() => vi.restoreAllMocks()); describe("search deadline", () => { it("interrupts credential preflight before starting network providers", async () => { vi.spyOn(codex, "isCodexAvailable").mockImplementation( async () => await new Promise(() => {}), ); const fetchMock = vi.spyOn(globalThis, "fetch"); await expect( searchWeb({ query: "docs", count: 1, timeout: 5, ctx: {} as never, }), ).rejects.toThrow(); expect(fetchMock).not.toHaveBeenCalled(); }); it("does not return provider results when signal aborts before resolution", async () => { vi.spyOn(codex, "isCodexAvailable").mockResolvedValue(false); vi.spyOn(minimax, "isMinimaxAvailable").mockResolvedValue(false); vi.spyOn(zai, "isZaiAvailable").mockResolvedValue(false); const controller = new AbortController(); vi.spyOn(google, "googleSearch").mockImplementation(async () => { controller.abort(new Error("finished too late")); return []; }); await expect( searchWeb({ query: "docs", count: 1, timeout: 1_000, signal: controller.signal, }), ).rejects.toThrow("finished too late"); }); it("interrupts a stalled provider even when it ignores cancellation", async () => { vi.spyOn(codex, "isCodexAvailable").mockResolvedValue(false); vi.spyOn(minimax, "isMinimaxAvailable").mockResolvedValue(false); vi.spyOn(zai, "isZaiAvailable").mockResolvedValue(false); vi.spyOn(google, "googleSearch").mockImplementation( () => new Promise(() => {}), ); const next = vi.spyOn(parallel, "parallelSearch"); await expect( searchWeb({ query: "docs", count: 1, timeout: 5 }), ).rejects.toThrow(); expect(next).not.toHaveBeenCalled(); }); it("does not start fallback after overall deadline aborts a provider", async () => { vi.spyOn(codex, "isCodexAvailable").mockResolvedValue(false); vi.spyOn(minimax, "isMinimaxAvailable").mockResolvedValue(false); vi.spyOn(zai, "isZaiAvailable").mockResolvedValue(false); vi.spyOn(google, "googleSearch").mockImplementation( async (_query, signal) => await new Promise((_, reject) => { signal?.addEventListener("abort", () => reject(signal.reason), { once: true, }); }), ); const next = vi.spyOn(parallel, "parallelSearch"); await expect( searchWeb({ query: "docs", count: 1, timeout: 5 }), ).rejects.toThrow(); expect(next).not.toHaveBeenCalled(); }); });