|
| 1 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query" |
| 2 | +import { act, fireEvent, render, screen, waitFor } from "@testing-library/react" |
| 3 | + |
| 4 | +import { providerIdentifiers, type OrganizationAllowList, type ProviderSettings } from "@roo-code/types" |
| 5 | + |
| 6 | +import { LiteLLM } from "../LiteLLM" |
| 7 | + |
| 8 | +const { postMessageMock, mockUseExtensionState } = vi.hoisted(() => ({ |
| 9 | + postMessageMock: vi.fn(), |
| 10 | + mockUseExtensionState: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock("@src/utils/vscode", () => ({ |
| 14 | + vscode: { postMessage: postMessageMock }, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock("@src/context/ExtensionStateContext", () => ({ |
| 18 | + useExtensionState: mockUseExtensionState, |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock("@src/i18n/TranslationContext", () => ({ |
| 22 | + useAppTranslation: () => ({ t: (key: string) => key }), |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock("@vscode/webview-ui-toolkit/react", () => ({ |
| 26 | + VSCodeTextField: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 27 | + VSCodeCheckbox: ({ children }: { children: React.ReactNode }) => <div>{children}</div>, |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock("@src/components/ui", () => ({ |
| 31 | + Button: ({ children, onClick, disabled }: React.ComponentProps<"button">) => ( |
| 32 | + <button data-testid="refresh-button" onClick={onClick} disabled={disabled}> |
| 33 | + {children} |
| 34 | + </button> |
| 35 | + ), |
| 36 | +})) |
| 37 | + |
| 38 | +vi.mock("../../ModelPicker", () => ({ |
| 39 | + ModelPicker: () => <div data-testid="model-picker" />, |
| 40 | +})) |
| 41 | + |
| 42 | +describe("LiteLLM", () => { |
| 43 | + const organizationAllowList: OrganizationAllowList = { allowAll: true, providers: {} } |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + vi.clearAllMocks() |
| 47 | + mockUseExtensionState.mockReturnValue({ routerModels: { [providerIdentifiers.litellm]: {} } }) |
| 48 | + }) |
| 49 | + |
| 50 | + it("invalidates both LiteLLM caches after a successful model refresh", async () => { |
| 51 | + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) |
| 52 | + const invalidateQueries = vi.spyOn(queryClient, "invalidateQueries") |
| 53 | + const apiConfiguration: ProviderSettings = { |
| 54 | + apiProvider: providerIdentifiers.litellm, |
| 55 | + litellmApiKey: "test-key", |
| 56 | + litellmBaseUrl: "http://localhost:4000", |
| 57 | + } |
| 58 | + |
| 59 | + render( |
| 60 | + <QueryClientProvider client={queryClient}> |
| 61 | + <LiteLLM |
| 62 | + apiConfiguration={apiConfiguration} |
| 63 | + setApiConfigurationField={vi.fn()} |
| 64 | + organizationAllowList={organizationAllowList} |
| 65 | + /> |
| 66 | + </QueryClientProvider>, |
| 67 | + ) |
| 68 | + |
| 69 | + fireEvent.click(screen.getByTestId("refresh-button")) |
| 70 | + act(() => { |
| 71 | + window.dispatchEvent(new MessageEvent("message", { data: { type: "routerModels" } })) |
| 72 | + }) |
| 73 | + |
| 74 | + await waitFor(() => { |
| 75 | + expect(invalidateQueries).toHaveBeenCalledWith({ |
| 76 | + queryKey: ["routerModels", providerIdentifiers.litellm], |
| 77 | + }) |
| 78 | + expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ["routerModels", "all"] }) |
| 79 | + }) |
| 80 | + }) |
| 81 | +}) |
0 commit comments