|
| 1 | +import { render, fireEvent } from "@testing-library/react" |
| 2 | +import { vi } from "vitest" |
| 3 | +import { ImageGenerationSettings } from "../ImageGenerationSettings" |
| 4 | +import type { ProviderSettings } from "@roo-code/types" |
| 5 | + |
| 6 | +// Mock the translation context |
| 7 | +vi.mock("@/i18n/TranslationContext", () => ({ |
| 8 | + useAppTranslation: () => ({ |
| 9 | + t: (key: string) => key, |
| 10 | + }), |
| 11 | +})) |
| 12 | + |
| 13 | +describe("ImageGenerationSettings", () => { |
| 14 | + const mockSetApiConfigurationField = vi.fn() |
| 15 | + const mockOnChange = vi.fn() |
| 16 | + |
| 17 | + const defaultProps = { |
| 18 | + enabled: false, |
| 19 | + onChange: mockOnChange, |
| 20 | + apiConfiguration: {} as ProviderSettings, |
| 21 | + setApiConfigurationField: mockSetApiConfigurationField, |
| 22 | + } |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + vi.clearAllMocks() |
| 26 | + }) |
| 27 | + |
| 28 | + describe("Initial Mount Behavior", () => { |
| 29 | + it("should not call setApiConfigurationField on initial mount with empty configuration", () => { |
| 30 | + render(<ImageGenerationSettings {...defaultProps} />) |
| 31 | + |
| 32 | + // Should NOT call setApiConfigurationField on initial mount to prevent dirty state |
| 33 | + expect(mockSetApiConfigurationField).not.toHaveBeenCalled() |
| 34 | + }) |
| 35 | + |
| 36 | + it("should not call setApiConfigurationField on initial mount with existing configuration", () => { |
| 37 | + const apiConfiguration = { |
| 38 | + openRouterImageGenerationSettings: { |
| 39 | + openRouterApiKey: "existing-key", |
| 40 | + selectedModel: "google/gemini-2.5-flash-image-preview:free", |
| 41 | + }, |
| 42 | + } as ProviderSettings |
| 43 | + |
| 44 | + render(<ImageGenerationSettings {...defaultProps} apiConfiguration={apiConfiguration} />) |
| 45 | + |
| 46 | + // Should NOT call setApiConfigurationField on initial mount to prevent dirty state |
| 47 | + expect(mockSetApiConfigurationField).not.toHaveBeenCalled() |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + describe("User Interaction Behavior", () => { |
| 52 | + it("should call setApiConfigurationField when user changes API key", async () => { |
| 53 | + const { getByPlaceholderText } = render(<ImageGenerationSettings {...defaultProps} enabled={true} />) |
| 54 | + |
| 55 | + const apiKeyInput = getByPlaceholderText( |
| 56 | + "settings:experimental.IMAGE_GENERATION.openRouterApiKeyPlaceholder", |
| 57 | + ) |
| 58 | + |
| 59 | + // Simulate user typing |
| 60 | + fireEvent.input(apiKeyInput, { target: { value: "new-api-key" } }) |
| 61 | + |
| 62 | + // Should call setApiConfigurationField with isUserAction=true |
| 63 | + expect(mockSetApiConfigurationField).toHaveBeenCalledWith( |
| 64 | + "openRouterImageGenerationSettings", |
| 65 | + { |
| 66 | + openRouterApiKey: "new-api-key", |
| 67 | + selectedModel: "google/gemini-2.5-flash-image-preview", |
| 68 | + }, |
| 69 | + true, // This should be true for user actions |
| 70 | + ) |
| 71 | + }) |
| 72 | + |
| 73 | + // Note: Testing VSCode dropdown components is complex due to their custom nature |
| 74 | + // The key functionality (not marking as dirty on initial mount) is already tested above |
| 75 | + }) |
| 76 | + |
| 77 | + describe("Conditional Rendering", () => { |
| 78 | + it("should render input fields when enabled is true", () => { |
| 79 | + const { getByPlaceholderText } = render(<ImageGenerationSettings {...defaultProps} enabled={true} />) |
| 80 | + |
| 81 | + expect( |
| 82 | + getByPlaceholderText("settings:experimental.IMAGE_GENERATION.openRouterApiKeyPlaceholder"), |
| 83 | + ).toBeInTheDocument() |
| 84 | + }) |
| 85 | + |
| 86 | + it("should not render input fields when enabled is false", () => { |
| 87 | + const { queryByPlaceholderText } = render(<ImageGenerationSettings {...defaultProps} enabled={false} />) |
| 88 | + |
| 89 | + expect( |
| 90 | + queryByPlaceholderText("settings:experimental.IMAGE_GENERATION.openRouterApiKeyPlaceholder"), |
| 91 | + ).not.toBeInTheDocument() |
| 92 | + }) |
| 93 | + }) |
| 94 | +}) |
0 commit comments