|
| 1 | +import { describe, test, expect, vi } from "vitest" |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react" |
| 3 | +import { MaxTokensControl } from "../MaxTokensControl" |
| 4 | +import { ModelInfo } from "@roo-code/types" |
| 5 | + |
| 6 | +// Mock the translation hook |
| 7 | +vi.mock("@/i18n/TranslationContext", () => ({ |
| 8 | + useAppTranslation: () => ({ |
| 9 | + t: (key: string, params?: any) => { |
| 10 | + if (params) { |
| 11 | + return key.replace(/\{\{(\w+)\}\}/g, (_, p) => params[p]) |
| 12 | + } |
| 13 | + return key |
| 14 | + }, |
| 15 | + }), |
| 16 | +})) |
| 17 | + |
| 18 | +describe("MaxTokensControl", () => { |
| 19 | + const mockOnChange = vi.fn() |
| 20 | + const defaultProps = { |
| 21 | + onChange: mockOnChange, |
| 22 | + } |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + mockOnChange.mockClear() |
| 26 | + }) |
| 27 | + |
| 28 | + test("should render with default value of 8192", () => { |
| 29 | + render(<MaxTokensControl {...defaultProps} />) |
| 30 | + |
| 31 | + const input = screen.getByRole("spinbutton") as HTMLInputElement |
| 32 | + expect(input.value).toBe("8192") |
| 33 | + }) |
| 34 | + |
| 35 | + test("should render with provided value", () => { |
| 36 | + render(<MaxTokensControl {...defaultProps} value={16000} />) |
| 37 | + |
| 38 | + const input = screen.getByRole("spinbutton") as HTMLInputElement |
| 39 | + expect(input.value).toBe("16000") |
| 40 | + }) |
| 41 | + |
| 42 | + test("should call onChange when value changes", () => { |
| 43 | + render(<MaxTokensControl {...defaultProps} />) |
| 44 | + |
| 45 | + const input = screen.getByRole("spinbutton") |
| 46 | + fireEvent.change(input, { target: { value: "20000" } }) |
| 47 | + |
| 48 | + expect(mockOnChange).toHaveBeenCalledWith(20000) |
| 49 | + }) |
| 50 | + |
| 51 | + test("should call onChange with undefined when input is cleared", () => { |
| 52 | + render(<MaxTokensControl {...defaultProps} value={16000} />) |
| 53 | + |
| 54 | + const input = screen.getByRole("spinbutton") |
| 55 | + fireEvent.change(input, { target: { value: "" } }) |
| 56 | + |
| 57 | + expect(mockOnChange).toHaveBeenCalledWith(undefined) |
| 58 | + }) |
| 59 | + |
| 60 | + test("should show validation error when value exceeds model max", () => { |
| 61 | + const modelInfo: ModelInfo = { |
| 62 | + maxTokens: 10000, |
| 63 | + contextWindow: 100000, |
| 64 | + supportsPromptCache: true, |
| 65 | + } |
| 66 | + |
| 67 | + render(<MaxTokensControl {...defaultProps} value={15000} modelInfo={modelInfo} />) |
| 68 | + |
| 69 | + expect(screen.getByText("settings:providers.maxOutputTokens.validation.tooHigh")).toBeInTheDocument() |
| 70 | + }) |
| 71 | + |
| 72 | + test("should show validation error when value is below minimum", () => { |
| 73 | + render(<MaxTokensControl {...defaultProps} value={500} minValue={1000} />) |
| 74 | + |
| 75 | + expect(screen.getByText("settings:providers.maxOutputTokens.validation.tooLow")).toBeInTheDocument() |
| 76 | + }) |
| 77 | + |
| 78 | + test("should show model support message when valid", () => { |
| 79 | + const modelInfo: ModelInfo = { |
| 80 | + maxTokens: 64000, |
| 81 | + contextWindow: 200000, |
| 82 | + supportsPromptCache: true, |
| 83 | + } |
| 84 | + |
| 85 | + render(<MaxTokensControl {...defaultProps} value={8192} modelInfo={modelInfo} />) |
| 86 | + |
| 87 | + expect(screen.getByText("settings:providers.maxOutputTokens.modelSupports")).toBeInTheDocument() |
| 88 | + }) |
| 89 | + |
| 90 | + test("should use custom min and max values", () => { |
| 91 | + render(<MaxTokensControl {...defaultProps} minValue={2000} maxValue={50000} />) |
| 92 | + |
| 93 | + const input = screen.getByRole("spinbutton") as HTMLInputElement |
| 94 | + expect(input.min).toBe("2000") |
| 95 | + expect(input.max).toBe("50000") |
| 96 | + }) |
| 97 | + |
| 98 | + test("should use model's maxTokens as max value when available", () => { |
| 99 | + const modelInfo: ModelInfo = { |
| 100 | + maxTokens: 32000, |
| 101 | + contextWindow: 100000, |
| 102 | + supportsPromptCache: true, |
| 103 | + } |
| 104 | + |
| 105 | + render(<MaxTokensControl {...defaultProps} modelInfo={modelInfo} />) |
| 106 | + |
| 107 | + const input = screen.getByRole("spinbutton") as HTMLInputElement |
| 108 | + expect(input.max).toBe("32000") |
| 109 | + }) |
| 110 | + |
| 111 | + test("should apply error styling when validation fails", () => { |
| 112 | + render(<MaxTokensControl {...defaultProps} value={500} minValue={1000} />) |
| 113 | + |
| 114 | + const input = screen.getByRole("spinbutton") |
| 115 | + expect(input.className).toContain("border-red-500") |
| 116 | + }) |
| 117 | +}) |
0 commit comments