|
| 1 | +import { render, screen } from "@testing-library/react" |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 3 | +import { FollowUpSuggest } from "../FollowUpSuggest" |
| 4 | +import { ExtensionStateContext, ExtensionStateContextType } from "@src/context/ExtensionStateContext" |
| 5 | +import { TooltipProvider } from "@radix-ui/react-tooltip" |
| 6 | + |
| 7 | +// Mock the translation hook |
| 8 | +vi.mock("@src/i18n/TranslationContext", () => ({ |
| 9 | + TranslationProvider: ({ children }: { children: React.ReactNode }) => children, |
| 10 | + useAppTranslation: () => ({ |
| 11 | + t: (key: string, options?: any) => { |
| 12 | + if (key === "chat:followUpSuggest.countdownDisplay" && options?.count !== undefined) { |
| 13 | + return `${options.count}s` |
| 14 | + } |
| 15 | + if (key === "chat:followUpSuggest.autoSelectCountdown" && options?.count !== undefined) { |
| 16 | + return `Auto-selecting in ${options.count} seconds` |
| 17 | + } |
| 18 | + if (key === "chat:followUpSuggest.copyToInput") { |
| 19 | + return "Copy to input" |
| 20 | + } |
| 21 | + return key |
| 22 | + }, |
| 23 | + }), |
| 24 | +})) |
| 25 | + |
| 26 | +// Mock the extension state |
| 27 | +const createMockExtensionState = (overrides?: Partial<ExtensionStateContextType>): ExtensionStateContextType => |
| 28 | + ({ |
| 29 | + version: "1.0.0", |
| 30 | + clineMessages: [], |
| 31 | + taskHistory: [], |
| 32 | + shouldShowAnnouncement: false, |
| 33 | + allowedCommands: [], |
| 34 | + soundEnabled: false, |
| 35 | + soundVolume: 0.5, |
| 36 | + ttsEnabled: false, |
| 37 | + ttsSpeed: 1.0, |
| 38 | + diffEnabled: false, |
| 39 | + enableCheckpoints: true, |
| 40 | + fuzzyMatchThreshold: 1.0, |
| 41 | + language: "en", |
| 42 | + writeDelayMs: 1000, |
| 43 | + browserViewportSize: "900x600", |
| 44 | + screenshotQuality: 75, |
| 45 | + terminalOutputLineLimit: 500, |
| 46 | + terminalShellIntegrationTimeout: 4000, |
| 47 | + mcpEnabled: true, |
| 48 | + enableMcpServerCreation: false, |
| 49 | + alwaysApproveResubmit: false, |
| 50 | + requestDelaySeconds: 5, |
| 51 | + currentApiConfigName: "default", |
| 52 | + listApiConfigMeta: [], |
| 53 | + mode: "code", |
| 54 | + customModePrompts: {}, |
| 55 | + customSupportPrompts: {}, |
| 56 | + experiments: {}, |
| 57 | + enhancementApiConfigId: "", |
| 58 | + condensingApiConfigId: "", |
| 59 | + customCondensingPrompt: "", |
| 60 | + hasOpenedModeSelector: false, |
| 61 | + autoApprovalEnabled: true, |
| 62 | + alwaysAllowFollowupQuestions: true, |
| 63 | + followupAutoApproveTimeoutMs: 3000, // 3 seconds for testing |
| 64 | + customModes: [], |
| 65 | + maxOpenTabsContext: 20, |
| 66 | + maxWorkspaceFiles: 200, |
| 67 | + cwd: "", |
| 68 | + browserToolEnabled: true, |
| 69 | + telemetrySetting: "unset", |
| 70 | + showRooIgnoredFiles: true, |
| 71 | + renderContext: "sidebar", |
| 72 | + maxReadFileLine: -1, |
| 73 | + pinnedApiConfigs: {}, |
| 74 | + didHydrateState: true, |
| 75 | + showWelcome: false, |
| 76 | + theme: {}, |
| 77 | + mcpServers: [], |
| 78 | + filePaths: [], |
| 79 | + openedTabs: [], |
| 80 | + organizationAllowList: { type: "all" }, |
| 81 | + cloudIsAuthenticated: false, |
| 82 | + sharingEnabled: false, |
| 83 | + mdmCompliant: true, |
| 84 | + autoCondenseContext: false, |
| 85 | + autoCondenseContextPercent: 50, |
| 86 | + setHasOpenedModeSelector: vi.fn(), |
| 87 | + setAlwaysAllowFollowupQuestions: vi.fn(), |
| 88 | + setFollowupAutoApproveTimeoutMs: vi.fn(), |
| 89 | + setCondensingApiConfigId: vi.fn(), |
| 90 | + setCustomCondensingPrompt: vi.fn(), |
| 91 | + setPinnedApiConfigs: vi.fn(), |
| 92 | + togglePinnedApiConfig: vi.fn(), |
| 93 | + setTerminalCompressProgressBar: vi.fn(), |
| 94 | + setHistoryPreviewCollapsed: vi.fn(), |
| 95 | + setAutoCondenseContext: vi.fn(), |
| 96 | + setAutoCondenseContextPercent: vi.fn(), |
| 97 | + ...overrides, |
| 98 | + }) as ExtensionStateContextType |
| 99 | + |
| 100 | +const renderWithProviders = (component: React.ReactElement, stateOverrides?: Partial<ExtensionStateContextType>) => { |
| 101 | + const mockState = createMockExtensionState(stateOverrides) |
| 102 | + |
| 103 | + return render( |
| 104 | + <ExtensionStateContext.Provider value={mockState}> |
| 105 | + <TooltipProvider>{component}</TooltipProvider> |
| 106 | + </ExtensionStateContext.Provider>, |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +describe("FollowUpSuggest", () => { |
| 111 | + const mockSuggestions = [{ answer: "First suggestion" }, { answer: "Second suggestion" }] |
| 112 | + |
| 113 | + const mockOnSuggestionClick = vi.fn() |
| 114 | + const mockOnUnmount = vi.fn() |
| 115 | + |
| 116 | + beforeEach(() => { |
| 117 | + vi.clearAllMocks() |
| 118 | + vi.useFakeTimers() |
| 119 | + }) |
| 120 | + |
| 121 | + afterEach(() => { |
| 122 | + vi.useRealTimers() |
| 123 | + }) |
| 124 | + |
| 125 | + it("should display countdown timer when auto-approval is enabled", () => { |
| 126 | + renderWithProviders( |
| 127 | + <FollowUpSuggest |
| 128 | + suggestions={mockSuggestions} |
| 129 | + onSuggestionClick={mockOnSuggestionClick} |
| 130 | + ts={123} |
| 131 | + onUnmount={mockOnUnmount} |
| 132 | + />, |
| 133 | + ) |
| 134 | + |
| 135 | + // Should show initial countdown (3 seconds) |
| 136 | + expect(screen.getByText(/3s/)).toBeInTheDocument() |
| 137 | + }) |
| 138 | + |
| 139 | + it("should not display countdown timer when isAnswered is true", () => { |
| 140 | + renderWithProviders( |
| 141 | + <FollowUpSuggest |
| 142 | + suggestions={mockSuggestions} |
| 143 | + onSuggestionClick={mockOnSuggestionClick} |
| 144 | + ts={123} |
| 145 | + onUnmount={mockOnUnmount} |
| 146 | + isAnswered={true} |
| 147 | + />, |
| 148 | + ) |
| 149 | + |
| 150 | + // Should not show countdown |
| 151 | + expect(screen.queryByText(/\d+s/)).not.toBeInTheDocument() |
| 152 | + }) |
| 153 | + |
| 154 | + it("should clear interval and call onUnmount when component unmounts", () => { |
| 155 | + const { unmount } = renderWithProviders( |
| 156 | + <FollowUpSuggest |
| 157 | + suggestions={mockSuggestions} |
| 158 | + onSuggestionClick={mockOnSuggestionClick} |
| 159 | + ts={123} |
| 160 | + onUnmount={mockOnUnmount} |
| 161 | + />, |
| 162 | + ) |
| 163 | + |
| 164 | + // Unmount the component |
| 165 | + unmount() |
| 166 | + |
| 167 | + // onUnmount should have been called |
| 168 | + expect(mockOnUnmount).toHaveBeenCalled() |
| 169 | + }) |
| 170 | + |
| 171 | + it("should not show countdown when auto-approval is disabled", () => { |
| 172 | + renderWithProviders( |
| 173 | + <FollowUpSuggest |
| 174 | + suggestions={mockSuggestions} |
| 175 | + onSuggestionClick={mockOnSuggestionClick} |
| 176 | + ts={123} |
| 177 | + onUnmount={mockOnUnmount} |
| 178 | + />, |
| 179 | + { autoApprovalEnabled: false }, |
| 180 | + ) |
| 181 | + |
| 182 | + // Should not show countdown |
| 183 | + expect(screen.queryByText(/\d+s/)).not.toBeInTheDocument() |
| 184 | + }) |
| 185 | + |
| 186 | + it("should not show countdown when alwaysAllowFollowupQuestions is false", () => { |
| 187 | + renderWithProviders( |
| 188 | + <FollowUpSuggest |
| 189 | + suggestions={mockSuggestions} |
| 190 | + onSuggestionClick={mockOnSuggestionClick} |
| 191 | + ts={123} |
| 192 | + onUnmount={mockOnUnmount} |
| 193 | + />, |
| 194 | + { alwaysAllowFollowupQuestions: false }, |
| 195 | + ) |
| 196 | + |
| 197 | + // Should not show countdown |
| 198 | + expect(screen.queryByText(/\d+s/)).not.toBeInTheDocument() |
| 199 | + }) |
| 200 | +}) |
0 commit comments