| 1 | import { fireEvent, render, screen } from "@testing-library/react"; |
| 2 | import { ApplyState } from "core"; |
| 3 | import { EditOperation } from "core/tools/definitions/multiEdit"; |
| 4 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | import { FindAndReplaceDisplay } from "./FindAndReplace"; |
| 6 | |
| 7 | // Mock the dependencies |
| 8 | vi.mock("../../../context/IdeMessenger", () => ({ |
| 9 | IdeMessengerContext: { |
| 10 | _currentValue: { post: vi.fn() }, |
| 11 | }, |
| 12 | })); |
| 13 | |
| 14 | vi.mock("../../../redux/hooks", () => ({ |
| 15 | useAppSelector: vi.fn(), |
| 16 | })); |
| 17 | |
| 18 | vi.mock("../../../components/ui", () => ({ |
| 19 | useFontSize: () => 14, |
| 20 | })); |
| 21 | |
| 22 | vi.mock("../../../util/clientTools/findAndReplaceUtils", async () => { |
| 23 | const actual = await vi.importActual( |
| 24 | "../../../util/clientTools/findAndReplaceUtils", |
| 25 | ); |
| 26 | |
| 27 | return { |
| 28 | performFindAndReplace: vi.fn(), |
| 29 | trimEmptyLines: actual.trimEmptyLines, |
| 30 | }; |
| 31 | }); |
| 32 | |
| 33 | vi.mock("./utils", () => ({ |
| 34 | getStatusIcon: vi.fn(() => <div data-testid="status-icon">✓</div>), |
| 35 | })); |
| 36 | |
| 37 | vi.mock("react", async () => { |
| 38 | const actual = await vi.importActual("react"); |
| 39 | return { |
| 40 | ...actual, |
| 41 | useContext: () => ({ post: vi.fn() }), |
| 42 | }; |
| 43 | }); |
| 44 | |
| 45 | // Import mocked modules |
| 46 | import { useAppSelector } from "../../../redux/hooks"; |
| 47 | import { performFindAndReplace } from "../../../util/clientTools/findAndReplaceUtils"; |
| 48 | |
| 49 | const mockPost = vi.fn(); |
| 50 | const mockUseAppSelector = useAppSelector as any; |
| 51 | const mockPerformFindAndReplace = performFindAndReplace as any; |
| 52 | |
| 53 | describe("FindAndReplaceDisplay", () => { |
| 54 | const defaultProps = { |
| 55 | fileUri: "file:///test/file.ts", |
| 56 | relativeFilePath: "test/file.ts", |
| 57 | editingFileContents: "const old = 'value';\nconst other = 'test';", |
| 58 | edits: [ |
| 59 | { |
| 60 | old_string: "const old = 'value';", |
| 61 | new_string: "const new = 'value';", |
| 62 | replace_all: false, |
| 63 | }, |
| 64 | ] as EditOperation[], |
| 65 | toolCallId: "test-tool-call-id", |
| 66 | historyIndex: 0, |
| 67 | }; |
| 68 | |
| 69 | const mockToolCallState = { |
| 70 | status: "done" as const, |
| 71 | output: null, |
| 72 | }; |
| 73 | |
| 74 | const mockConfig = { |
| 75 | ui: { |
| 76 | showChatScrollbar: true, |
| 77 | codeWrap: false, |
| 78 | }, |
| 79 | }; |
| 80 | |
| 81 | beforeEach(() => { |
| 82 | vi.clearAllMocks(); |
| 83 | |
| 84 | mockUseAppSelector.mockImplementation((selector: any) => { |
| 85 | const mockState = { |
| 86 | config: { config: mockConfig }, |
| 87 | session: { |
| 88 | history: [], |
| 89 | codeBlockApplyStates: { states: [] }, |
| 90 | }, |
| 91 | }; |
| 92 | |
| 93 | if (selector.toString().includes("selectApplyStateByToolCallId")) { |
| 94 | return undefined; // No apply state by default |
| 95 | } |
| 96 | if (selector.toString().includes("selectToolCallById")) { |
| 97 | return mockToolCallState; |
| 98 | } |
| 99 | |
| 100 | return selector(mockState); |
| 101 | }); |
| 102 | |
| 103 | mockPerformFindAndReplace.mockImplementation( |
| 104 | (content: string, oldStr: string, newStr: string) => { |
| 105 | return content.replace(oldStr, newStr); |
| 106 | }, |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | describe("basic rendering", () => { |
| 111 | it("should render with collapsed state by default", () => { |
| 112 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 113 | |
| 114 | expect(screen.getByText("file.ts")).toBeInTheDocument(); |
| 115 | expect( |
| 116 | screen.getByTestId("toggle-find-and-replace-diff"), |
| 117 | ).toBeInTheDocument(); |
| 118 | |
| 119 | // Should not show diff content when collapsed |
| 120 | const diffContent = screen.queryByText("-"); |
| 121 | expect(diffContent).not.toBeInTheDocument(); |
| 122 | }); |
| 123 | |
| 124 | it("should display file name from fileUri", () => { |
| 125 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 126 | expect(screen.getByText("file.ts")).toBeInTheDocument(); |
| 127 | }); |
| 128 | |
| 129 | it("should display file name from relativeFilePath when no fileUri", () => { |
| 130 | render( |
| 131 | <FindAndReplaceDisplay |
| 132 | {...defaultProps} |
| 133 | fileUri={undefined} |
| 134 | relativeFilePath="src/components/test.tsx" |
| 135 | />, |
| 136 | ); |
| 137 | expect(screen.getByText("test.tsx")).toBeInTheDocument(); |
| 138 | }); |
| 139 | |
| 140 | it("should handle missing file paths gracefully", () => { |
| 141 | render( |
| 142 | <FindAndReplaceDisplay |
| 143 | {...defaultProps} |
| 144 | fileUri={undefined} |
| 145 | relativeFilePath={undefined} |
| 146 | />, |
| 147 | ); |
| 148 | |
| 149 | // Should still render the component structure |
| 150 | expect( |
| 151 | screen.getByTestId("toggle-find-and-replace-diff"), |
| 152 | ).toBeInTheDocument(); |
| 153 | }); |
| 154 | }); |
| 155 | |
| 156 | describe("expand/collapse functionality", () => { |
| 157 | it("should expand when clicked", () => { |
| 158 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 159 | |
| 160 | const toggleButton = screen.getByTestId("toggle-find-and-replace-diff"); |
| 161 | fireEvent.click(toggleButton); |
| 162 | |
| 163 | // Should show diff content when expanded |
| 164 | expect(screen.getByText("-")).toBeInTheDocument(); |
| 165 | expect(screen.getByText("+")).toBeInTheDocument(); |
| 166 | }); |
| 167 | |
| 168 | it("should show content when tool call status is 'generated'", () => { |
| 169 | mockUseAppSelector.mockImplementation((selector: any) => { |
| 170 | const mockState = { |
| 171 | config: { config: mockConfig }, |
| 172 | session: { |
| 173 | history: [], |
| 174 | codeBlockApplyStates: { states: [] }, |
| 175 | }, |
| 176 | }; |
| 177 | |
| 178 | if (selector.toString().includes("selectToolCallById")) { |
| 179 | return { ...mockToolCallState, status: "generated" }; |
| 180 | } |
| 181 | if (selector.toString().includes("selectApplyStateByToolCallId")) { |
| 182 | return undefined; |
| 183 | } |
| 184 | |
| 185 | return selector(mockState); |
| 186 | }); |
| 187 | |
| 188 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 189 | |
| 190 | // Should show diff content without expanding |
| 191 | expect(screen.getByText("-")).toBeInTheDocument(); |
| 192 | expect(screen.getByText("+")).toBeInTheDocument(); |
| 193 | }); |
| 194 | }); |
| 195 | |
| 196 | describe("diff generation", () => { |
| 197 | it("should generate and display diff correctly", () => { |
| 198 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 199 | |
| 200 | const toggleButton = screen.getByTestId("toggle-find-and-replace-diff"); |
| 201 | fireEvent.click(toggleButton); |
| 202 | |
| 203 | // Should show removed line |
| 204 | expect(screen.getByText("const old = 'value';")).toBeInTheDocument(); |
| 205 | |
| 206 | // Should show added line |
| 207 | expect(screen.getByText("const new = 'value';")).toBeInTheDocument(); |
| 208 | |
| 209 | // Should show unchanged line |
| 210 | expect(screen.getByText("const other = 'test';")).toBeInTheDocument(); |
| 211 | }); |
| 212 | |
| 213 | it("should handle multiple edits", () => { |
| 214 | const multipleEdits = [ |
| 215 | { |
| 216 | old_string: "const old = 'value';", |
| 217 | new_string: "const new = 'value';", |
| 218 | replace_all: false, |
| 219 | }, |
| 220 | { |
| 221 | old_string: "const other = 'test';", |
| 222 | new_string: "const other = 'updated';", |
| 223 | replace_all: false, |
| 224 | }, |
| 225 | ] as EditOperation[]; |
| 226 | |
| 227 | mockPerformFindAndReplace |
| 228 | .mockReturnValueOnce("const new = 'value';\nconst other = 'test';") |
| 229 | .mockReturnValueOnce("const new = 'value';\nconst other = 'updated';"); |
| 230 | |
| 231 | render(<FindAndReplaceDisplay {...defaultProps} edits={multipleEdits} />); |
| 232 | |
| 233 | const toggleButton = screen.getByTestId("toggle-find-and-replace-diff"); |
| 234 | fireEvent.click(toggleButton); |
| 235 | |
| 236 | expect(mockPerformFindAndReplace).toHaveBeenCalledTimes(2); |
| 237 | }); |
| 238 | |
| 239 | it("should handle diff generation errors", () => { |
| 240 | mockPerformFindAndReplace.mockImplementation(() => { |
| 241 | throw new Error("Test error"); |
| 242 | }); |
| 243 | |
| 244 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 245 | |
| 246 | // When diff generation errors, component shows a friendly message |
| 247 | // without rendering the expand/collapse container |
| 248 | expect( |
| 249 | screen.getByText("The searched string was not found in the file"), |
| 250 | ).toBeInTheDocument(); |
| 251 | }); |
| 252 | |
| 253 | it("should show 'No changes to display' when diff is empty", () => { |
| 254 | // Mock the function to return the exact same content (no changes) |
| 255 | mockPerformFindAndReplace.mockReturnValue( |
| 256 | defaultProps.editingFileContents, |
| 257 | ); |
| 258 | |
| 259 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 260 | |
| 261 | const toggleButton = screen.getByTestId("toggle-find-and-replace-diff"); |
| 262 | fireEvent.click(toggleButton); |
| 263 | |
| 264 | expect(screen.getByText("No changes to display")).toBeInTheDocument(); |
| 265 | }); |
| 266 | }); |
| 267 | |
| 268 | describe("apply actions", () => { |
| 269 | const mockApplyState: ApplyState = { |
| 270 | streamId: "test-stream-id", |
| 271 | status: "streaming", |
| 272 | numDiffs: 1, |
| 273 | fileContent: "test content", |
| 274 | }; |
| 275 | |
| 276 | beforeEach(() => { |
| 277 | mockUseAppSelector.mockImplementation((selector: any) => { |
| 278 | const mockState = { |
| 279 | config: { config: mockConfig }, |
| 280 | session: { |
| 281 | history: [], |
| 282 | codeBlockApplyStates: { states: [mockApplyState] }, |
| 283 | }, |
| 284 | }; |
| 285 | |
| 286 | if (selector.toString().includes("selectApplyStateByToolCallId")) { |
| 287 | return mockApplyState; |
| 288 | } |
| 289 | if (selector.toString().includes("selectToolCallById")) { |
| 290 | return mockToolCallState; |
| 291 | } |
| 292 | |
| 293 | return selector(mockState); |
| 294 | }); |
| 295 | }); |
| 296 | |
| 297 | it("should show apply actions when applyState exists", () => { |
| 298 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 299 | |
| 300 | // ApplyActions component should be rendered (we can test by looking for its structure) |
| 301 | // Since we don't have the exact structure, we test that the container is there |
| 302 | expect( |
| 303 | screen.getByTestId("toggle-find-and-replace-diff"), |
| 304 | ).toBeInTheDocument(); |
| 305 | }); |
| 306 | |
| 307 | it("should handle accept action", () => { |
| 308 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 309 | |
| 310 | // This would need more detailed testing if ApplyActions was properly mocked |
| 311 | // For now, we verify the component renders without errors |
| 312 | expect( |
| 313 | screen.getByTestId("toggle-find-and-replace-diff"), |
| 314 | ).toBeInTheDocument(); |
| 315 | }); |
| 316 | }); |
| 317 | |
| 318 | describe("content handling", () => { |
| 319 | it("should use editingFileContents when provided", () => { |
| 320 | render(<FindAndReplaceDisplay {...defaultProps} />); |
| 321 | |
| 322 | const toggleButton = screen.getByTestId("toggle-find-and-replace-diff"); |
| 323 | fireEvent.click(toggleButton); |
| 324 | |
| 325 | expect(mockPerformFindAndReplace).toHaveBeenCalledWith( |
| 326 | "const old = 'value';\nconst other = 'test';", |
| 327 | "const old = 'value';", |
| 328 | "const new = 'value';", |
| 329 | false, |
| 330 | 0, |
| 331 | ); |
| 332 | }); |
| 333 | |
| 334 | it("should fallback to edit old_strings when no editingFileContents", () => { |
| 335 | render( |
| 336 | <FindAndReplaceDisplay |
| 337 | {...defaultProps} |
| 338 | editingFileContents={undefined} |
| 339 | />, |
| 340 | ); |
| 341 | |
| 342 | const toggleButton = screen.getByTestId("toggle-find-and-replace-diff"); |
| 343 | fireEvent.click(toggleButton); |
| 344 | |
| 345 | expect(mockPerformFindAndReplace).toHaveBeenCalledWith( |
| 346 | "const old = 'value';", |
| 347 | "const old = 'value';", |
| 348 | "const new = 'value';", |
| 349 | false, |
| 350 | 0, |
| 351 | ); |
| 352 | }); |
| 353 | }); |
| 354 | }); |