|
| 1 | +import React from "react" |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react" |
| 3 | +import { Gemini } from "../Gemini" |
| 4 | +import type { ProviderSettings } from "@roo-code/types" |
| 5 | +import { geminiModels, geminiDefaultModelId, type GeminiModelId } from "@roo-code/types" |
| 6 | + |
| 7 | +vi.mock("@vscode/webview-ui-toolkit/react", () => ({ |
| 8 | + VSCodeTextField: ({ children, value, onInput, type }: any) => ( |
| 9 | + <div> |
| 10 | + {children} |
| 11 | + <input type={type} value={value} onChange={(e) => onInput(e)} /> |
| 12 | + </div> |
| 13 | + ), |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock("vscrui", () => ({ |
| 17 | + Checkbox: ({ children, checked, onChange }: any) => ( |
| 18 | + <label data-testid="checkbox-custom-context-limit"> |
| 19 | + <input type="checkbox" checked={checked} onChange={(e) => onChange(e.target.checked)} /> |
| 20 | + {children} |
| 21 | + </label> |
| 22 | + ), |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock("@src/components/ui", () => ({ |
| 26 | + Slider: ({ min, max, step, value, onValueChange }: any) => ( |
| 27 | + <input |
| 28 | + data-testid="slider" |
| 29 | + type="range" |
| 30 | + min={min} |
| 31 | + max={max} |
| 32 | + step={step} |
| 33 | + value={value[0]} |
| 34 | + onChange={(e) => onValueChange([Number(e.target.value)])} |
| 35 | + /> |
| 36 | + ), |
| 37 | +})) |
| 38 | + |
| 39 | +vi.mock("@src/i18n/TranslationContext", () => ({ |
| 40 | + useAppTranslation: () => ({ t: (key: string) => key }), |
| 41 | +})) |
| 42 | + |
| 43 | +vi.mock("@src/components/common/VSCodeButtonLink", () => ({ |
| 44 | + VSCodeButtonLink: ({ children, href }: any) => <a href={href}>{children}</a>, |
| 45 | +})) |
| 46 | + |
| 47 | +const defaultModelId: GeminiModelId = geminiDefaultModelId |
| 48 | +const defaultContextWindow = geminiModels[defaultModelId].contextWindow |
| 49 | + |
| 50 | +describe("Gemini provider settings", () => { |
| 51 | + it("does not render context limit slider when custom context limit is not enabled", () => { |
| 52 | + const setApiField = vi.fn() |
| 53 | + const config: ProviderSettings = {} |
| 54 | + render( |
| 55 | + <Gemini apiConfiguration={config} setApiConfigurationField={setApiField} currentModelId={defaultModelId} />, |
| 56 | + ) |
| 57 | + expect(screen.queryByTestId("slider")).toBeNull() |
| 58 | + }) |
| 59 | + |
| 60 | + it("enables custom context limit on checkbox toggle and shows slider with default value", () => { |
| 61 | + const setApiField = vi.fn() |
| 62 | + const config: ProviderSettings = {} |
| 63 | + render( |
| 64 | + <Gemini apiConfiguration={config} setApiConfigurationField={setApiField} currentModelId={defaultModelId} />, |
| 65 | + ) |
| 66 | + const checkbox = screen.getByTestId("checkbox-custom-context-limit") |
| 67 | + fireEvent.click(checkbox) |
| 68 | + expect(setApiField).toHaveBeenCalledWith("contextLimit", defaultContextWindow) |
| 69 | + const slider = screen.getByTestId("slider") |
| 70 | + expect(slider).toHaveValue(defaultContextWindow.toString()) |
| 71 | + }) |
| 72 | + |
| 73 | + it("renders slider when contextLimit already set and updates on slider change", () => { |
| 74 | + const setApiField = vi.fn() |
| 75 | + const initialLimit = 100000 |
| 76 | + const config: ProviderSettings = { contextLimit: initialLimit } |
| 77 | + render( |
| 78 | + <Gemini apiConfiguration={config} setApiConfigurationField={setApiField} currentModelId={defaultModelId} />, |
| 79 | + ) |
| 80 | + const slider = screen.getByTestId("slider") |
| 81 | + expect(slider).toHaveValue(initialLimit.toString()) |
| 82 | + fireEvent.change(slider, { target: { value: "50000" } }) |
| 83 | + expect(setApiField).toHaveBeenCalledWith("contextLimit", 50000) |
| 84 | + }) |
| 85 | +}) |
0 commit comments