|
| 1 | +import { act, render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { Id } from "@opencom/convex/dataModel"; |
| 4 | +import { AIAgentSection } from "./AIAgentSection"; |
| 5 | +import { useWebAction, useWebMutation, useWebQuery } from "@/lib/convex/hooks"; |
| 6 | + |
| 7 | +vi.mock("@/lib/convex/hooks", () => ({ |
| 8 | + useWebAction: vi.fn(), |
| 9 | + useWebMutation: vi.fn(), |
| 10 | + useWebQuery: vi.fn(), |
| 11 | + webActionRef: vi.fn((functionName: string) => functionName), |
| 12 | + webMutationRef: vi.fn((functionName: string) => functionName), |
| 13 | + webQueryRef: vi.fn((functionName: string) => functionName), |
| 14 | +})); |
| 15 | + |
| 16 | +describe("AIAgentSection model discovery fallbacks", () => { |
| 17 | + const workspaceId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" as unknown as Id<"workspaces">; |
| 18 | + const aiSettingsFixture = { |
| 19 | + enabled: true, |
| 20 | + model: "openai/gpt-5-nano", |
| 21 | + confidenceThreshold: 0.6, |
| 22 | + knowledgeSources: ["articles"], |
| 23 | + personality: "", |
| 24 | + handoffMessage: "", |
| 25 | + suggestionsEnabled: false, |
| 26 | + embeddingModel: "text-embedding-3-small", |
| 27 | + lastConfigError: null, |
| 28 | + } as const; |
| 29 | + |
| 30 | + let listAvailableModelsMock: ReturnType<typeof vi.fn>; |
| 31 | + let rejectDiscovery: ((reason?: unknown) => void) | undefined; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | + vi.spyOn(console, "error").mockImplementation(() => {}); |
| 36 | + |
| 37 | + const mockedUseWebQuery = useWebQuery as unknown as ReturnType<typeof vi.fn>; |
| 38 | + mockedUseWebQuery.mockImplementation((_: unknown, args: unknown) => { |
| 39 | + if (args === "skip") { |
| 40 | + return undefined; |
| 41 | + } |
| 42 | + |
| 43 | + return aiSettingsFixture; |
| 44 | + }); |
| 45 | + |
| 46 | + listAvailableModelsMock = vi.fn( |
| 47 | + () => |
| 48 | + new Promise((_, reject) => { |
| 49 | + rejectDiscovery = reject; |
| 50 | + }) |
| 51 | + ); |
| 52 | + |
| 53 | + const mockedUseWebAction = useWebAction as unknown as ReturnType<typeof vi.fn>; |
| 54 | + mockedUseWebAction.mockReturnValue(listAvailableModelsMock); |
| 55 | + |
| 56 | + const mockedUseWebMutation = useWebMutation as unknown as ReturnType<typeof vi.fn>; |
| 57 | + mockedUseWebMutation.mockReturnValue(vi.fn().mockResolvedValue(undefined)); |
| 58 | + }); |
| 59 | + |
| 60 | + it("stops showing the loading placeholder when model discovery fails", async () => { |
| 61 | + render(<AIAgentSection workspaceId={workspaceId} />); |
| 62 | + |
| 63 | + await waitFor(() => { |
| 64 | + expect(listAvailableModelsMock).toHaveBeenCalledWith({ |
| 65 | + workspaceId, |
| 66 | + selectedModel: aiSettingsFixture.model, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + expect(screen.getByRole("option", { name: /loading discovered models/i })).toBeInTheDocument(); |
| 71 | + |
| 72 | + await act(async () => { |
| 73 | + rejectDiscovery?.(new Error("Discovery failed")); |
| 74 | + }); |
| 75 | + |
| 76 | + await waitFor(() => { |
| 77 | + expect(screen.getByRole("option", { name: /model discovery unavailable/i })).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + expect( |
| 81 | + screen.getByText(/model discovery is currently unavailable\. enter a model id manually/i) |
| 82 | + ).toBeInTheDocument(); |
| 83 | + expect( |
| 84 | + screen.queryByRole("option", { name: /loading discovered models/i }) |
| 85 | + ).not.toBeInTheDocument(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments