|
| 1 | +// FILE: providerDiscoveryReactQuery.test.ts |
| 2 | +// Purpose: Verifies per-provider model discovery stays fault-isolated. |
| 3 | +// Layer: Web data fetching tests |
| 4 | +// Depends on: Vitest, React Query, and the native API bridge mock. |
| 5 | + |
| 6 | +import type { NativeApi, ProviderKind, ProviderListModelsInput } from "@t3tools/contracts"; |
| 7 | +import { QueryClient } from "@tanstack/react-query"; |
| 8 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 9 | +import { providerModelsQueryOptions } from "./providerDiscoveryReactQuery"; |
| 10 | +import * as nativeApi from "../nativeApi"; |
| 11 | + |
| 12 | +function mockListModels(impl: (input: ProviderListModelsInput) => Promise<unknown>) { |
| 13 | + const listModels = vi.fn(impl); |
| 14 | + vi.spyOn(nativeApi, "ensureNativeApi").mockReturnValue({ |
| 15 | + provider: { listModels }, |
| 16 | + } as unknown as NativeApi); |
| 17 | + return listModels; |
| 18 | +} |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + vi.restoreAllMocks(); |
| 22 | +}); |
| 23 | + |
| 24 | +describe("providerModelsQueryOptions", () => { |
| 25 | + it("degrades to an empty 'error' result when a provider's discovery fails", async () => { |
| 26 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 27 | + mockListModels(async () => { |
| 28 | + throw new Error("Cursor CLI is not installed."); |
| 29 | + }); |
| 30 | + |
| 31 | + const queryClient = new QueryClient(); |
| 32 | + const result = await queryClient.fetchQuery(providerModelsQueryOptions({ provider: "cursor" })); |
| 33 | + |
| 34 | + expect(result).toEqual({ models: [], source: "error", cached: false }); |
| 35 | + }); |
| 36 | + |
| 37 | + it("keeps other providers' models when one provider's discovery fails", async () => { |
| 38 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 39 | + const codexModels = [{ slug: "gpt-5-codex", name: "GPT-5 Codex" }]; |
| 40 | + mockListModels(async ({ provider }) => { |
| 41 | + if (provider === "cursor") { |
| 42 | + throw new Error("Cursor CLI is not authenticated."); |
| 43 | + } |
| 44 | + return { models: codexModels, source: "codex-app-server", cached: false }; |
| 45 | + }); |
| 46 | + |
| 47 | + const queryClient = new QueryClient(); |
| 48 | + const [cursorResult, codexResult] = await Promise.all([ |
| 49 | + queryClient.fetchQuery(providerModelsQueryOptions({ provider: "cursor" })), |
| 50 | + queryClient.fetchQuery(providerModelsQueryOptions({ provider: "codex" })), |
| 51 | + ]); |
| 52 | + |
| 53 | + // The failing provider degrades on its own... |
| 54 | + expect(cursorResult.models).toEqual([]); |
| 55 | + expect(cursorResult.source).toBe("error"); |
| 56 | + // ...while every other provider keeps its discovered models. |
| 57 | + expect(codexResult.models).toEqual(codexModels); |
| 58 | + expect(codexResult.source).toBe("codex-app-server"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("never rejects, so a failing CLI cannot blank the shared model picker", async () => { |
| 62 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 63 | + mockListModels(async () => { |
| 64 | + throw new Error("Timed out while discovering Cursor models via CLI."); |
| 65 | + }); |
| 66 | + |
| 67 | + const queryClient = new QueryClient(); |
| 68 | + await expect( |
| 69 | + queryClient.fetchQuery(providerModelsQueryOptions({ provider: "cursor" })), |
| 70 | + ).resolves.toMatchObject({ source: "error" }); |
| 71 | + |
| 72 | + expect(providerModelsQueryOptions({ provider: "cursor" }).retry).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it("forwards optional discovery inputs and returns discovered models on success", async () => { |
| 76 | + const listModels = mockListModels(async () => ({ |
| 77 | + models: [{ slug: "auto", name: "Auto" }], |
| 78 | + source: "cursor.cli", |
| 79 | + cached: false, |
| 80 | + })); |
| 81 | + |
| 82 | + const queryClient = new QueryClient(); |
| 83 | + const result = await queryClient.fetchQuery( |
| 84 | + providerModelsQueryOptions({ |
| 85 | + provider: "cursor", |
| 86 | + binaryPath: "/usr/bin/cursor-agent", |
| 87 | + apiEndpoint: "https://example.test", |
| 88 | + }), |
| 89 | + ); |
| 90 | + |
| 91 | + expect(listModels).toHaveBeenCalledWith({ |
| 92 | + provider: "cursor", |
| 93 | + binaryPath: "/usr/bin/cursor-agent", |
| 94 | + apiEndpoint: "https://example.test", |
| 95 | + }); |
| 96 | + expect(result.source).toBe("cursor.cli"); |
| 97 | + expect(result.models).toEqual([{ slug: "auto", name: "Auto" }]); |
| 98 | + }); |
| 99 | + |
| 100 | + it("scopes query keys per provider so discovery results never collide", () => { |
| 101 | + const cursorKey = providerModelsQueryOptions({ provider: "cursor" as ProviderKind }).queryKey; |
| 102 | + const codexKey = providerModelsQueryOptions({ provider: "codex" as ProviderKind }).queryKey; |
| 103 | + expect(cursorKey).not.toEqual(codexKey); |
| 104 | + }); |
| 105 | +}); |
0 commit comments