|
| 1 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | +import { beforeEach, expect, test, vi } from "vitest"; |
| 4 | +import { TooltipProvider } from "@coursemap/ui/primitives/tooltip"; |
| 5 | +import { ImportModelCard } from "../ui/admin/imports/import-model-card"; |
| 6 | +const actions = vi.hoisted(() => ({ |
| 7 | + setImportModel: vi.fn(), |
| 8 | + saveImportModel: vi.fn(), |
| 9 | + removeImportModel: vi.fn(), |
| 10 | + setImportModelVisibility: vi.fn(), |
| 11 | + refresh: vi.fn(), |
| 12 | +})); |
| 13 | +vi.mock("@/lib/admin/settings-actions", () => actions); |
| 14 | +vi.mock("next/navigation", () => ({ |
| 15 | + useRouter: () => ({ refresh: actions.refresh }), |
| 16 | +})); |
| 17 | +const models = [ |
| 18 | + { |
| 19 | + id: "google/test", |
| 20 | + name: "Gemini Test", |
| 21 | + provider: "Google", |
| 22 | + enabled: true, |
| 23 | + visible: true, |
| 24 | + input_usd_per_million: 0.25, |
| 25 | + output_usd_per_million: 1.5, |
| 26 | + pricing_updated_at: "2026-09-07T00:00:00Z", |
| 27 | + }, |
| 28 | + { |
| 29 | + id: "anthropic/test", |
| 30 | + name: "Claude Test", |
| 31 | + provider: "Anthropic", |
| 32 | + enabled: true, |
| 33 | + visible: true, |
| 34 | + input_usd_per_million: 1, |
| 35 | + output_usd_per_million: 5, |
| 36 | + pricing_updated_at: "2026-09-07T00:00:00Z", |
| 37 | + }, |
| 38 | +]; |
| 39 | +beforeEach(() => vi.resetAllMocks()); |
| 40 | +function setup(canManage = true) { |
| 41 | + render( |
| 42 | + <TooltipProvider> |
| 43 | + <ImportModelCard |
| 44 | + canManage={canManage} |
| 45 | + model="google/test" |
| 46 | + models={models} |
| 47 | + updatedAt={null} |
| 48 | + /> |
| 49 | + </TooltipProvider>, |
| 50 | + ); |
| 51 | + return userEvent.setup(); |
| 52 | +} |
| 53 | +test("shows compact pricing and selects a model through the workspace menu", async () => { |
| 54 | + actions.setImportModel.mockResolvedValue({ ok: true, message: "Saved." }); |
| 55 | + const user = setup(); |
| 56 | + expect(screen.getByText("0.55¢")).toBeInTheDocument(); |
| 57 | + await user.click(screen.getByRole("button", { name: "Import model" })); |
| 58 | + await user.click( |
| 59 | + screen.getByRole("menuitem", { name: "Claude Test, Anthropic" }), |
| 60 | + ); |
| 61 | + expect(actions.setImportModel).toHaveBeenCalledWith("anthropic/test"); |
| 62 | + expect(actions.refresh).toHaveBeenCalled(); |
| 63 | +}); |
| 64 | +test("model management prevents removing the default and preserves failed additions", async () => { |
| 65 | + actions.saveImportModel.mockResolvedValue({ |
| 66 | + ok: false, |
| 67 | + message: "The model was not found.", |
| 68 | + }); |
| 69 | + const user = setup(); |
| 70 | + await user.click(screen.getByRole("button", { name: "Import model" })); |
| 71 | + await user.click(screen.getByRole("menuitem", { name: "Manage models" })); |
| 72 | + await user.click( |
| 73 | + screen.getByRole("button", { name: "Actions for Gemini Test" }), |
| 74 | + ); |
| 75 | + expect( |
| 76 | + screen.getByRole("button", { name: "Refresh pricing" }), |
| 77 | + ).toBeInTheDocument(); |
| 78 | + expect( |
| 79 | + screen.queryByRole("button", { name: "Remove model" }), |
| 80 | + ).not.toBeInTheDocument(); |
| 81 | + await user.keyboard("{Escape}"); |
| 82 | + await user.type(screen.getByLabelText("OpenRouter model ID"), "test/missing"); |
| 83 | + await user.click(screen.getByRole("button", { name: "Add model" })); |
| 84 | + expect(await screen.findByRole("alert")).toHaveTextContent( |
| 85 | + "The model was not found.", |
| 86 | + ); |
| 87 | + expect(screen.getByLabelText("OpenRouter model ID")).toHaveValue( |
| 88 | + "test/missing", |
| 89 | + ); |
| 90 | +}); |
| 91 | +test("read-only viewers cannot change the catalogue", () => { |
| 92 | + setup(false); |
| 93 | + expect(screen.getByRole("button", { name: "Import model" })).toBeDisabled(); |
| 94 | +}); |
| 95 | + |
| 96 | +test("Escape returns focus from model management to its selector", async () => { |
| 97 | + const user = setup(); |
| 98 | + const trigger = screen.getByRole("button", { name: "Import model" }); |
| 99 | + await user.click(trigger); |
| 100 | + await user.click(screen.getByRole("menuitem", { name: "Manage models" })); |
| 101 | + await user.keyboard("{Escape}"); |
| 102 | + await waitFor(() => expect(trigger).toHaveFocus()); |
| 103 | +}); |
| 104 | + |
| 105 | +test("hides a model from selection while keeping it manageable", async () => { |
| 106 | + actions.setImportModelVisibility.mockResolvedValue({ |
| 107 | + ok: true, |
| 108 | + message: "Hidden.", |
| 109 | + }); |
| 110 | + const user = setup(); |
| 111 | + await user.click(screen.getByRole("button", { name: "Import model" })); |
| 112 | + await user.click(screen.getByRole("menuitem", { name: "Manage models" })); |
| 113 | + await user.click( |
| 114 | + screen.getByRole("button", { name: "Actions for Claude Test" }), |
| 115 | + ); |
| 116 | + await user.click(screen.getByRole("button", { name: "Hide model" })); |
| 117 | + expect(actions.setImportModelVisibility).toHaveBeenCalledWith( |
| 118 | + "anthropic/test", |
| 119 | + false, |
| 120 | + ); |
| 121 | + expect(actions.refresh).toHaveBeenCalled(); |
| 122 | +}); |
| 123 | + |
| 124 | +test("hidden models can be shown again and are absent from the selector", async () => { |
| 125 | + actions.setImportModelVisibility.mockResolvedValue({ |
| 126 | + ok: true, |
| 127 | + message: "Visible.", |
| 128 | + }); |
| 129 | + render( |
| 130 | + <TooltipProvider> |
| 131 | + <ImportModelCard |
| 132 | + canManage |
| 133 | + model="google/test" |
| 134 | + models={models.map((model) => ({ |
| 135 | + ...model, |
| 136 | + visible: model.id === "google/test", |
| 137 | + }))} |
| 138 | + updatedAt={null} |
| 139 | + /> |
| 140 | + </TooltipProvider>, |
| 141 | + ); |
| 142 | + const user = userEvent.setup(); |
| 143 | + await user.click(screen.getByRole("button", { name: "Import model" })); |
| 144 | + expect( |
| 145 | + screen.queryByRole("menuitem", { name: "Claude Test, Anthropic" }), |
| 146 | + ).not.toBeInTheDocument(); |
| 147 | + await user.click(screen.getByRole("menuitem", { name: "Manage models" })); |
| 148 | + expect(screen.getByText("Hidden").closest("details")).not.toHaveAttribute( |
| 149 | + "open", |
| 150 | + ); |
| 151 | + await user.click(screen.getByText("Hidden")); |
| 152 | + expect(screen.getByText("Hidden").closest("details")).toHaveAttribute("open"); |
| 153 | + await user.click( |
| 154 | + screen.getByRole("button", { name: "Actions for Claude Test" }), |
| 155 | + ); |
| 156 | + await user.click(screen.getByRole("button", { name: "Show model" })); |
| 157 | + expect(actions.setImportModelVisibility).toHaveBeenCalledWith( |
| 158 | + "anthropic/test", |
| 159 | + true, |
| 160 | + ); |
| 161 | +}); |
0 commit comments