|
1 | | -import { render, screen, fireEvent } from "@testing-library/react" |
| 1 | +import { render, screen, fireEvent, act } from "@testing-library/react" |
2 | 2 |
|
3 | 3 | import type { ProviderSettings, OrganizationAllowList } from "@roo-code/types" |
4 | 4 | import { opencodeGoDefaultModelId } from "@roo-code/types" |
@@ -38,6 +38,25 @@ vi.mock("../../ModelPicker", () => ({ |
38 | 38 | ), |
39 | 39 | })) |
40 | 40 |
|
| 41 | +const { postMessageMock } = vi.hoisted(() => ({ |
| 42 | + postMessageMock: vi.fn(), |
| 43 | +})) |
| 44 | + |
| 45 | +vi.mock("@src/utils/vscode", () => ({ |
| 46 | + vscode: { |
| 47 | + postMessage: postMessageMock, |
| 48 | + }, |
| 49 | +})) |
| 50 | + |
| 51 | +// Stub the shared Button so we can assert onClick/disabled without its styling deps. |
| 52 | +vi.mock("@src/components/ui", () => ({ |
| 53 | + Button: ({ children, onClick, disabled, className }: any) => ( |
| 54 | + <button onClick={onClick} disabled={disabled} className={className} data-testid="refresh-button"> |
| 55 | + {children} |
| 56 | + </button> |
| 57 | + ), |
| 58 | +})) |
| 59 | + |
41 | 60 | describe("OpenCodeGo", () => { |
42 | 61 | const organizationAllowList: OrganizationAllowList = { allowAll: true, providers: {} } |
43 | 62 | const mockSetApiConfigurationField = vi.fn() |
@@ -87,4 +106,128 @@ describe("OpenCodeGo", () => { |
87 | 106 | expect(picker).toHaveAttribute("data-model-id-key", "opencodeGoModelId") |
88 | 107 | expect(picker).toHaveAttribute("data-service-name", "Opencode Go") |
89 | 108 | }) |
| 109 | + |
| 110 | + describe("refresh models", () => { |
| 111 | + const dispatchMessage = (data: any) => |
| 112 | + act(() => { |
| 113 | + window.dispatchEvent(new MessageEvent("message", { data })) |
| 114 | + }) |
| 115 | + |
| 116 | + it("renders the refresh button in idle state", () => { |
| 117 | + renderComponent({ opencodeGoApiKey: "key" }) |
| 118 | + |
| 119 | + const button = screen.getByTestId("refresh-button") |
| 120 | + expect(button).not.toBeDisabled() |
| 121 | + expect(button.querySelector(".codicon-refresh")).not.toBeNull() |
| 122 | + expect(screen.getByText("settings:providers.refreshModels.label")).toBeInTheDocument() |
| 123 | + }) |
| 124 | + |
| 125 | + it("sends requestRouterModels with the current api key when clicked", () => { |
| 126 | + renderComponent({ opencodeGoApiKey: "my-key" }) |
| 127 | + |
| 128 | + fireEvent.click(screen.getByTestId("refresh-button")) |
| 129 | + |
| 130 | + expect(postMessageMock).toHaveBeenCalledWith({ |
| 131 | + type: "requestRouterModels", |
| 132 | + values: { provider: "opencode-go", refresh: true, opencodeGoApiKey: "my-key" }, |
| 133 | + }) |
| 134 | + }) |
| 135 | + |
| 136 | + it("enters loading state and disables the button while refreshing", () => { |
| 137 | + renderComponent({ opencodeGoApiKey: "key" }) |
| 138 | + |
| 139 | + fireEvent.click(screen.getByTestId("refresh-button")) |
| 140 | + |
| 141 | + const button = screen.getByTestId("refresh-button") |
| 142 | + expect(button).toBeDisabled() |
| 143 | + expect(button.querySelector(".codicon-loading")).not.toBeNull() |
| 144 | + expect(screen.getByText("settings:providers.refreshModels.loading")).toBeInTheDocument() |
| 145 | + }) |
| 146 | + |
| 147 | + it("shows success state when routerModels arrives while loading", () => { |
| 148 | + renderComponent({ opencodeGoApiKey: "key" }) |
| 149 | + |
| 150 | + fireEvent.click(screen.getByTestId("refresh-button")) |
| 151 | + dispatchMessage({ type: "routerModels" }) |
| 152 | + |
| 153 | + expect(screen.getByText("settings:providers.refreshModels.success")).toBeInTheDocument() |
| 154 | + }) |
| 155 | + |
| 156 | + it("shows error state with the received error message on fetch failure", () => { |
| 157 | + renderComponent({ opencodeGoApiKey: "key" }) |
| 158 | + |
| 159 | + fireEvent.click(screen.getByTestId("refresh-button")) |
| 160 | + dispatchMessage({ |
| 161 | + type: "singleRouterModelFetchResponse", |
| 162 | + success: false, |
| 163 | + values: { provider: "opencode-go" }, |
| 164 | + error: "Invalid API key", |
| 165 | + }) |
| 166 | + |
| 167 | + expect(screen.getByText("Invalid API key")).toBeInTheDocument() |
| 168 | + }) |
| 169 | + |
| 170 | + it("falls back to the default error translation when no error is provided", () => { |
| 171 | + renderComponent({ opencodeGoApiKey: "key" }) |
| 172 | + |
| 173 | + fireEvent.click(screen.getByTestId("refresh-button")) |
| 174 | + dispatchMessage({ |
| 175 | + type: "singleRouterModelFetchResponse", |
| 176 | + success: false, |
| 177 | + values: { provider: "opencode-go" }, |
| 178 | + }) |
| 179 | + |
| 180 | + expect(screen.getByText("settings:providers.refreshModels.error")).toBeInTheDocument() |
| 181 | + }) |
| 182 | + |
| 183 | + it("ignores fetch failures for other providers", () => { |
| 184 | + renderComponent({ opencodeGoApiKey: "key" }) |
| 185 | + |
| 186 | + fireEvent.click(screen.getByTestId("refresh-button")) |
| 187 | + dispatchMessage({ |
| 188 | + type: "singleRouterModelFetchResponse", |
| 189 | + success: false, |
| 190 | + values: { provider: "openrouter" }, |
| 191 | + error: "should not show", |
| 192 | + }) |
| 193 | + |
| 194 | + expect(screen.queryByText("should not show")).not.toBeInTheDocument() |
| 195 | + expect(screen.getByText("settings:providers.refreshModels.loading")).toBeInTheDocument() |
| 196 | + }) |
| 197 | + |
| 198 | + it("does not override an error with success when routerModels arrives after a failure", () => { |
| 199 | + renderComponent({ opencodeGoApiKey: "key" }) |
| 200 | + |
| 201 | + fireEvent.click(screen.getByTestId("refresh-button")) |
| 202 | + |
| 203 | + // Dispatch both within the same act batch so the handler still sees |
| 204 | + // refreshStatus === "loading" and the errorJustReceived guard is exercised. |
| 205 | + act(() => { |
| 206 | + window.dispatchEvent( |
| 207 | + new MessageEvent("message", { |
| 208 | + data: { |
| 209 | + type: "singleRouterModelFetchResponse", |
| 210 | + success: false, |
| 211 | + values: { provider: "opencode-go" }, |
| 212 | + error: "boom", |
| 213 | + }, |
| 214 | + }), |
| 215 | + ) |
| 216 | + window.dispatchEvent(new MessageEvent("message", { data: { type: "routerModels" } })) |
| 217 | + }) |
| 218 | + |
| 219 | + expect(screen.getByText("boom")).toBeInTheDocument() |
| 220 | + expect(screen.queryByText("settings:providers.refreshModels.success")).not.toBeInTheDocument() |
| 221 | + }) |
| 222 | + |
| 223 | + it("ignores routerModels messages when not in loading state", () => { |
| 224 | + renderComponent({ opencodeGoApiKey: "key" }) |
| 225 | + |
| 226 | + // No refresh initiated; an unsolicited routerModels message should be a no-op. |
| 227 | + dispatchMessage({ type: "routerModels" }) |
| 228 | + |
| 229 | + expect(screen.queryByText("settings:providers.refreshModels.success")).not.toBeInTheDocument() |
| 230 | + expect(screen.queryByText("settings:providers.refreshModels.loading")).not.toBeInTheDocument() |
| 231 | + }) |
| 232 | + }) |
90 | 233 | }) |
0 commit comments