| 1 | import { ApplyState, ApplyToFilePayload, ToolCallState } from "core"; |
| 2 | import { EDIT_MODE_STREAM_ID } from "core/edit/constants"; |
| 3 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | import { logAgentModeEditOutcome } from "../../util/editOutcomeLogger"; |
| 5 | import { |
| 6 | selectApplyStateByToolCallId, |
| 7 | selectToolCallById, |
| 8 | } from "../selectors/selectToolCalls"; |
| 9 | import { updateEditStateApplyState } from "../slices/editState"; |
| 10 | import { |
| 11 | acceptToolCall, |
| 12 | errorToolCall, |
| 13 | updateApplyState, |
| 14 | updateToolCallOutput, |
| 15 | } from "../slices/sessionSlice"; |
| 16 | import { findToolCallById, logToolUsage } from "../util"; |
| 17 | import { exitEdit } from "./edit"; |
| 18 | import { |
| 19 | applyForEditTool, |
| 20 | handleApplyStateUpdate, |
| 21 | } from "./handleApplyStateUpdate"; |
| 22 | import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall"; |
| 23 | |
| 24 | // Mock dependencies |
| 25 | vi.mock("../../util/editOutcomeLogger", () => ({ |
| 26 | logAgentModeEditOutcome: vi.fn(), |
| 27 | })); |
| 28 | |
| 29 | vi.mock("../selectors/selectToolCalls", () => ({ |
| 30 | selectApplyStateByToolCallId: vi.fn(), |
| 31 | selectToolCallById: vi.fn(), |
| 32 | })); |
| 33 | |
| 34 | vi.mock("../slices/editState", () => ({ |
| 35 | updateEditStateApplyState: vi.fn(() => ({ |
| 36 | type: "editState/updateApplyState", |
| 37 | })), |
| 38 | })); |
| 39 | |
| 40 | vi.mock("../slices/sessionSlice", () => ({ |
| 41 | acceptToolCall: vi.fn(() => ({ type: "session/acceptToolCall" })), |
| 42 | errorToolCall: vi.fn(() => ({ type: "session/errorToolCall" })), |
| 43 | updateApplyState: vi.fn(() => ({ type: "session/updateApplyState" })), |
| 44 | updateToolCallOutput: vi.fn(() => ({ type: "session/updateToolCallOutput" })), |
| 45 | })); |
| 46 | |
| 47 | vi.mock("../util", () => ({ |
| 48 | findToolCallById: vi.fn(), |
| 49 | logToolUsage: vi.fn(), |
| 50 | })); |
| 51 | |
| 52 | vi.mock("./edit", () => ({ |
| 53 | exitEdit: vi.fn(() => ({ type: "edit/exitEdit" })), |
| 54 | })); |
| 55 | |
| 56 | vi.mock("./streamResponseAfterToolCall", () => ({ |
| 57 | streamResponseAfterToolCall: vi.fn(() => ({ |
| 58 | type: "session/streamResponseAfterToolCall", |
| 59 | })), |
| 60 | })); |
| 61 | |
| 62 | const UNUSED_TOOL_CALL_PARAMS = { |
| 63 | toolCall: { |
| 64 | id: "name", |
| 65 | function: { |
| 66 | name: "unused", |
| 67 | arguments: "unused", |
| 68 | }, |
| 69 | type: "function" as const, |
| 70 | }, |
| 71 | parsedArgs: {}, |
| 72 | }; |
| 73 | |
| 74 | describe("handleApplyStateUpdate", () => { |
| 75 | let mockDispatch: any; |
| 76 | let mockGetState: any; |
| 77 | let mockExtra: any; |
| 78 | |
| 79 | beforeEach(() => { |
| 80 | vi.clearAllMocks(); |
| 81 | mockDispatch = vi.fn(); |
| 82 | mockGetState = vi.fn(); |
| 83 | mockExtra = { |
| 84 | ideMessenger: { |
| 85 | post: vi.fn(), |
| 86 | request: vi.fn(), |
| 87 | }, |
| 88 | }; |
| 89 | }); |
| 90 | |
| 91 | describe("edit mode handling", () => { |
| 92 | it("should handle edit mode apply state updates", async () => { |
| 93 | const applyState: ApplyState = { |
| 94 | streamId: EDIT_MODE_STREAM_ID, |
| 95 | toolCallId: "test-tool-call", |
| 96 | status: "streaming", |
| 97 | filepath: "test.txt", |
| 98 | numDiffs: 1, |
| 99 | }; |
| 100 | |
| 101 | const thunk = handleApplyStateUpdate(applyState); |
| 102 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 103 | |
| 104 | expect(updateEditStateApplyState).toHaveBeenCalledWith(applyState); |
| 105 | expect(mockDispatch).toHaveBeenCalledWith( |
| 106 | expect.objectContaining({ type: "editState/updateApplyState" }), |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | it("should exit edit mode when status is closed", async () => { |
| 111 | const toolCallState: ToolCallState = { |
| 112 | toolCallId: "test-tool-call", |
| 113 | status: "done", |
| 114 | ...UNUSED_TOOL_CALL_PARAMS, |
| 115 | }; |
| 116 | vi.mocked(findToolCallById).mockReturnValue(toolCallState); |
| 117 | |
| 118 | mockGetState.mockReturnValue({ |
| 119 | session: { history: [] }, |
| 120 | }); |
| 121 | |
| 122 | const applyState: ApplyState = { |
| 123 | streamId: EDIT_MODE_STREAM_ID, |
| 124 | toolCallId: "test-tool-call", |
| 125 | status: "closed", |
| 126 | filepath: "test.txt", |
| 127 | numDiffs: 1, |
| 128 | }; |
| 129 | |
| 130 | const thunk = handleApplyStateUpdate(applyState); |
| 131 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 132 | |
| 133 | expect(logToolUsage).toHaveBeenCalledWith( |
| 134 | toolCallState, |
| 135 | true, |
| 136 | true, |
| 137 | mockExtra.ideMessenger, |
| 138 | ); |
| 139 | expect(exitEdit).toHaveBeenCalledWith({}); |
| 140 | expect(mockDispatch).toHaveBeenCalledWith( |
| 141 | expect.objectContaining({ type: "edit/exitEdit" }), |
| 142 | ); |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | describe("chat/agent mode handling", () => { |
| 147 | it("should handle non-edit mode apply state updates", async () => { |
| 148 | const applyState: ApplyState = { |
| 149 | streamId: "chat-stream", |
| 150 | toolCallId: "test-tool-call", |
| 151 | status: "streaming", |
| 152 | filepath: "test.txt", |
| 153 | numDiffs: 1, |
| 154 | }; |
| 155 | |
| 156 | const thunk = handleApplyStateUpdate(applyState); |
| 157 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 158 | |
| 159 | expect(updateApplyState).toHaveBeenCalledWith(applyState); |
| 160 | expect(mockDispatch).toHaveBeenCalledWith( |
| 161 | expect.objectContaining({ type: "session/updateApplyState" }), |
| 162 | ); |
| 163 | }); |
| 164 | |
| 165 | it("should auto-accept diffs when configured and status is done", async () => { |
| 166 | mockGetState.mockReturnValue({ |
| 167 | config: { |
| 168 | config: { |
| 169 | ui: { |
| 170 | autoAcceptEditToolDiffs: true, |
| 171 | }, |
| 172 | }, |
| 173 | }, |
| 174 | session: { history: [] }, |
| 175 | }); |
| 176 | |
| 177 | const applyState: ApplyState = { |
| 178 | streamId: "chat-stream", |
| 179 | toolCallId: "test-tool-call", |
| 180 | status: "done", |
| 181 | filepath: "test.txt", |
| 182 | numDiffs: 1, |
| 183 | }; |
| 184 | |
| 185 | const thunk = handleApplyStateUpdate(applyState); |
| 186 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 187 | |
| 188 | expect(mockExtra.ideMessenger.post).toHaveBeenCalledWith("acceptDiff", { |
| 189 | streamId: "chat-stream", |
| 190 | filepath: "test.txt", |
| 191 | }); |
| 192 | }); |
| 193 | |
| 194 | it("should not auto-accept when config is disabled", async () => { |
| 195 | mockGetState.mockReturnValue({ |
| 196 | config: { |
| 197 | config: { |
| 198 | ui: { |
| 199 | autoAcceptEditToolDiffs: false, |
| 200 | }, |
| 201 | }, |
| 202 | }, |
| 203 | session: { history: [] }, |
| 204 | }); |
| 205 | |
| 206 | const applyState: ApplyState = { |
| 207 | streamId: "chat-stream", |
| 208 | toolCallId: "test-tool-call", |
| 209 | status: "done", |
| 210 | filepath: "test.txt", |
| 211 | numDiffs: 1, |
| 212 | }; |
| 213 | |
| 214 | const thunk = handleApplyStateUpdate(applyState); |
| 215 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 216 | |
| 217 | expect(mockExtra.ideMessenger.post).not.toHaveBeenCalled(); |
| 218 | }); |
| 219 | }); |
| 220 | |
| 221 | describe("closed status handling", () => { |
| 222 | it("should handle accepted tool call closure", async () => { |
| 223 | const toolCallState: ToolCallState = { |
| 224 | toolCallId: "test-tool-call", |
| 225 | status: "done", |
| 226 | ...UNUSED_TOOL_CALL_PARAMS, |
| 227 | }; |
| 228 | const newApplyState = { streamId: "chat-stream" }; |
| 229 | |
| 230 | vi.mocked(findToolCallById).mockReturnValue(toolCallState); |
| 231 | mockGetState.mockReturnValue({ |
| 232 | session: { |
| 233 | history: [], |
| 234 | codeBlockApplyStates: { |
| 235 | states: [newApplyState], |
| 236 | }, |
| 237 | }, |
| 238 | config: { config: {} }, |
| 239 | }); |
| 240 | |
| 241 | const applyState: ApplyState = { |
| 242 | streamId: "chat-stream", |
| 243 | toolCallId: "test-tool-call", |
| 244 | status: "closed", |
| 245 | filepath: "test.txt", |
| 246 | numDiffs: 1, |
| 247 | }; |
| 248 | |
| 249 | const thunk = handleApplyStateUpdate(applyState); |
| 250 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 251 | |
| 252 | expect(logToolUsage).toHaveBeenCalledWith( |
| 253 | toolCallState, |
| 254 | true, |
| 255 | true, |
| 256 | mockExtra.ideMessenger, |
| 257 | ); |
| 258 | expect(logAgentModeEditOutcome).toHaveBeenCalledWith( |
| 259 | [], |
| 260 | {}, |
| 261 | toolCallState, |
| 262 | newApplyState, |
| 263 | true, |
| 264 | mockExtra.ideMessenger, |
| 265 | ); |
| 266 | expect(acceptToolCall).toHaveBeenCalledWith({ |
| 267 | toolCallId: "test-tool-call", |
| 268 | }); |
| 269 | expect(streamResponseAfterToolCall).toHaveBeenCalledWith({ |
| 270 | toolCallId: "test-tool-call", |
| 271 | }); |
| 272 | }); |
| 273 | |
| 274 | it("should handle canceled tool call closure", async () => { |
| 275 | const toolCallState: ToolCallState = { |
| 276 | toolCallId: "test-tool-call", |
| 277 | status: "canceled", |
| 278 | ...UNUSED_TOOL_CALL_PARAMS, |
| 279 | }; |
| 280 | const newApplyState = { streamId: "chat-stream" }; |
| 281 | |
| 282 | vi.mocked(findToolCallById).mockReturnValue(toolCallState); |
| 283 | mockGetState.mockReturnValue({ |
| 284 | session: { |
| 285 | history: [], |
| 286 | codeBlockApplyStates: { |
| 287 | states: [newApplyState], |
| 288 | }, |
| 289 | }, |
| 290 | config: { config: {} }, |
| 291 | }); |
| 292 | |
| 293 | const applyState: ApplyState = { |
| 294 | streamId: "chat-stream", |
| 295 | toolCallId: "test-tool-call", |
| 296 | status: "closed", |
| 297 | filepath: "test.txt", |
| 298 | numDiffs: 1, |
| 299 | }; |
| 300 | |
| 301 | const thunk = handleApplyStateUpdate(applyState); |
| 302 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 303 | |
| 304 | expect(logToolUsage).toHaveBeenCalledWith( |
| 305 | toolCallState, |
| 306 | false, |
| 307 | true, |
| 308 | mockExtra.ideMessenger, |
| 309 | ); |
| 310 | expect(logAgentModeEditOutcome).toHaveBeenCalledWith( |
| 311 | [], |
| 312 | {}, |
| 313 | toolCallState, |
| 314 | newApplyState, |
| 315 | false, |
| 316 | mockExtra.ideMessenger, |
| 317 | ); |
| 318 | expect(acceptToolCall).not.toHaveBeenCalled(); |
| 319 | expect(streamResponseAfterToolCall).not.toHaveBeenCalled(); |
| 320 | }); |
| 321 | |
| 322 | it("should handle errored tool call closure", async () => { |
| 323 | const toolCallState: ToolCallState = { |
| 324 | toolCallId: "test-tool-call", |
| 325 | status: "errored", |
| 326 | ...UNUSED_TOOL_CALL_PARAMS, |
| 327 | }; |
| 328 | const newApplyState = { streamId: "chat-stream" }; |
| 329 | |
| 330 | vi.mocked(findToolCallById).mockReturnValue(toolCallState); |
| 331 | mockGetState.mockReturnValue({ |
| 332 | session: { |
| 333 | history: [], |
| 334 | codeBlockApplyStates: { |
| 335 | states: [newApplyState], |
| 336 | }, |
| 337 | }, |
| 338 | config: { config: {} }, |
| 339 | }); |
| 340 | |
| 341 | const applyState: ApplyState = { |
| 342 | streamId: "chat-stream", |
| 343 | toolCallId: "test-tool-call", |
| 344 | status: "closed", |
| 345 | filepath: "test.txt", |
| 346 | numDiffs: 1, |
| 347 | }; |
| 348 | |
| 349 | const thunk = handleApplyStateUpdate(applyState); |
| 350 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 351 | |
| 352 | expect(acceptToolCall).not.toHaveBeenCalled(); |
| 353 | expect(streamResponseAfterToolCall).toHaveBeenCalledWith({ |
| 354 | toolCallId: "test-tool-call", |
| 355 | }); |
| 356 | }); |
| 357 | |
| 358 | it("should handle closure when no tool call found", async () => { |
| 359 | vi.mocked(findToolCallById).mockReturnValue(undefined); |
| 360 | mockGetState.mockReturnValue({ |
| 361 | session: { |
| 362 | history: [], |
| 363 | codeBlockApplyStates: { |
| 364 | states: [], |
| 365 | }, |
| 366 | }, |
| 367 | }); |
| 368 | |
| 369 | const applyState: ApplyState = { |
| 370 | streamId: "chat-stream", |
| 371 | toolCallId: "test-tool-call", |
| 372 | status: "closed", |
| 373 | filepath: "test.txt", |
| 374 | numDiffs: 1, |
| 375 | }; |
| 376 | |
| 377 | const thunk = handleApplyStateUpdate(applyState); |
| 378 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 379 | |
| 380 | expect(logToolUsage).not.toHaveBeenCalled(); |
| 381 | expect(acceptToolCall).not.toHaveBeenCalled(); |
| 382 | expect(streamResponseAfterToolCall).not.toHaveBeenCalled(); |
| 383 | }); |
| 384 | |
| 385 | it("should handle closure when no apply state found", async () => { |
| 386 | const toolCallState: ToolCallState = { |
| 387 | toolCallId: "test-tool-call", |
| 388 | status: "done", |
| 389 | ...UNUSED_TOOL_CALL_PARAMS, |
| 390 | }; |
| 391 | |
| 392 | vi.mocked(findToolCallById).mockReturnValue(toolCallState); |
| 393 | mockGetState.mockReturnValue({ |
| 394 | session: { |
| 395 | history: [], |
| 396 | codeBlockApplyStates: { |
| 397 | states: [], |
| 398 | }, |
| 399 | }, |
| 400 | config: { config: {} }, |
| 401 | }); |
| 402 | |
| 403 | const applyState: ApplyState = { |
| 404 | streamId: "chat-stream", |
| 405 | toolCallId: "test-tool-call", |
| 406 | status: "closed", |
| 407 | filepath: "test.txt", |
| 408 | numDiffs: 1, |
| 409 | }; |
| 410 | |
| 411 | const thunk = handleApplyStateUpdate(applyState); |
| 412 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 413 | |
| 414 | expect(logAgentModeEditOutcome).not.toHaveBeenCalled(); |
| 415 | expect(acceptToolCall).toHaveBeenCalledWith({ |
| 416 | toolCallId: "test-tool-call", |
| 417 | }); |
| 418 | }); |
| 419 | }); |
| 420 | |
| 421 | describe("edge cases", () => { |
| 422 | it("should handle apply state without toolCallId", async () => { |
| 423 | const applyState: ApplyState = { |
| 424 | streamId: "chat-stream", |
| 425 | status: "streaming", |
| 426 | filepath: "test.txt", |
| 427 | numDiffs: 1, |
| 428 | }; |
| 429 | |
| 430 | const thunk = handleApplyStateUpdate(applyState); |
| 431 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 432 | |
| 433 | expect(updateApplyState).toHaveBeenCalledWith(applyState); |
| 434 | expect(acceptToolCall).not.toHaveBeenCalled(); |
| 435 | expect(streamResponseAfterToolCall).not.toHaveBeenCalled(); |
| 436 | }); |
| 437 | |
| 438 | it("should handle different status values", async () => { |
| 439 | const statusValues: ApplyState["status"][] = [ |
| 440 | "not-started", |
| 441 | "streaming", |
| 442 | "done", |
| 443 | "closed", |
| 444 | ]; |
| 445 | |
| 446 | for (const status of statusValues) { |
| 447 | vi.clearAllMocks(); |
| 448 | |
| 449 | const applyState: ApplyState = { |
| 450 | streamId: "chat-stream", |
| 451 | toolCallId: "test-tool-call", |
| 452 | status, |
| 453 | filepath: "test.txt", |
| 454 | numDiffs: 1, |
| 455 | }; |
| 456 | |
| 457 | const thunk = handleApplyStateUpdate(applyState); |
| 458 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 459 | |
| 460 | expect(updateApplyState).toHaveBeenCalledWith(applyState); |
| 461 | } |
| 462 | }); |
| 463 | }); |
| 464 | }); |
| 465 | |
| 466 | describe("applyForEditTool", () => { |
| 467 | let mockDispatch: any; |
| 468 | let mockGetState: any; |
| 469 | let mockExtra: any; |
| 470 | |
| 471 | beforeEach(() => { |
| 472 | vi.clearAllMocks(); |
| 473 | mockDispatch = vi.fn(); |
| 474 | mockGetState = vi.fn(); |
| 475 | mockExtra = { |
| 476 | ideMessenger: { |
| 477 | request: vi.fn(), |
| 478 | }, |
| 479 | }; |
| 480 | }); |
| 481 | |
| 482 | describe("successful application", () => { |
| 483 | it("should successfully apply changes to file", async () => { |
| 484 | const payload: ApplyToFilePayload & { toolCallId: string } = { |
| 485 | toolCallId: "test-tool-call", |
| 486 | streamId: "test-stream", |
| 487 | filepath: "test.txt", |
| 488 | text: "new content", |
| 489 | isSearchAndReplace: true, |
| 490 | }; |
| 491 | |
| 492 | mockExtra.ideMessenger.request.mockResolvedValue({ status: "success" }); |
| 493 | |
| 494 | const thunk = applyForEditTool(payload); |
| 495 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 496 | |
| 497 | expect(updateApplyState).toHaveBeenCalledWith({ |
| 498 | streamId: "test-stream", |
| 499 | toolCallId: "test-tool-call", |
| 500 | status: "not-started", |
| 501 | }); |
| 502 | expect(mockDispatch).toHaveBeenCalledWith( |
| 503 | expect.objectContaining({ type: "session/updateApplyState" }), |
| 504 | ); |
| 505 | expect(mockExtra.ideMessenger.request).toHaveBeenCalledWith( |
| 506 | "applyToFile", |
| 507 | payload, |
| 508 | ); |
| 509 | }); |
| 510 | }); |
| 511 | |
| 512 | describe("error handling", () => { |
| 513 | it("should handle IDE messenger request failure", async () => { |
| 514 | const payload: ApplyToFilePayload & { toolCallId: string } = { |
| 515 | toolCallId: "test-tool-call", |
| 516 | streamId: "test-stream", |
| 517 | filepath: "test.txt", |
| 518 | text: "new content", |
| 519 | isSearchAndReplace: true, |
| 520 | }; |
| 521 | |
| 522 | const toolCallState: ToolCallState = { |
| 523 | toolCallId: "test-tool-call", |
| 524 | status: "calling", |
| 525 | ...UNUSED_TOOL_CALL_PARAMS, |
| 526 | }; |
| 527 | const applyState: ApplyState = { |
| 528 | status: "streaming", |
| 529 | streamId: "test-stream", |
| 530 | }; |
| 531 | |
| 532 | vi.mocked(selectToolCallById).mockReturnValue(toolCallState); |
| 533 | vi.mocked(selectApplyStateByToolCallId).mockReturnValue(applyState); |
| 534 | mockGetState.mockReturnValue({}); |
| 535 | |
| 536 | mockExtra.ideMessenger.request.mockRejectedValue( |
| 537 | new Error("Request failed"), |
| 538 | ); |
| 539 | |
| 540 | const thunk = applyForEditTool(payload); |
| 541 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 542 | |
| 543 | expect(errorToolCall).toHaveBeenCalledWith({ |
| 544 | toolCallId: "test-tool-call", |
| 545 | }); |
| 546 | expect(updateToolCallOutput).toHaveBeenCalledWith({ |
| 547 | toolCallId: "test-tool-call", |
| 548 | contextItems: [ |
| 549 | { |
| 550 | icon: "problems", |
| 551 | name: "Apply Error", |
| 552 | description: "Failed to apply changes", |
| 553 | content: `Error editing file: failed to apply changes to file.\n\nPlease try again with correct args or notify the user and request further instructions.`, |
| 554 | hidden: false, |
| 555 | }, |
| 556 | ], |
| 557 | }); |
| 558 | }); |
| 559 | |
| 560 | it("should handle IDE messenger error response", async () => { |
| 561 | const payload: ApplyToFilePayload & { toolCallId: string } = { |
| 562 | toolCallId: "test-tool-call", |
| 563 | streamId: "test-stream", |
| 564 | filepath: "test.txt", |
| 565 | text: "new content", |
| 566 | isSearchAndReplace: true, |
| 567 | }; |
| 568 | |
| 569 | const toolCallState: ToolCallState = { |
| 570 | toolCallId: "test-tool-call", |
| 571 | status: "calling", |
| 572 | ...UNUSED_TOOL_CALL_PARAMS, |
| 573 | }; |
| 574 | const applyState: ApplyState = { |
| 575 | status: "streaming", |
| 576 | streamId: "test-stream", |
| 577 | }; |
| 578 | |
| 579 | vi.mocked(selectToolCallById).mockReturnValue(toolCallState); |
| 580 | vi.mocked(selectApplyStateByToolCallId).mockReturnValue(applyState); |
| 581 | mockGetState.mockReturnValue({}); |
| 582 | |
| 583 | mockExtra.ideMessenger.request.mockResolvedValue({ status: "error" }); |
| 584 | |
| 585 | const thunk = applyForEditTool(payload); |
| 586 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 587 | |
| 588 | expect(errorToolCall).toHaveBeenCalledWith({ |
| 589 | toolCallId: "test-tool-call", |
| 590 | }); |
| 591 | expect(updateToolCallOutput).toHaveBeenCalledWith({ |
| 592 | toolCallId: "test-tool-call", |
| 593 | contextItems: [ |
| 594 | { |
| 595 | icon: "problems", |
| 596 | name: "Apply Error", |
| 597 | description: "Failed to apply changes", |
| 598 | content: `Error editing file: failed to apply changes to file.\n\nPlease try again with correct args or notify the user and request further instructions.`, |
| 599 | hidden: false, |
| 600 | }, |
| 601 | ], |
| 602 | }); |
| 603 | }); |
| 604 | |
| 605 | it("should not error if tool call is not in calling state", async () => { |
| 606 | const payload: ApplyToFilePayload & { toolCallId: string } = { |
| 607 | toolCallId: "test-tool-call", |
| 608 | streamId: "test-stream", |
| 609 | filepath: "test.txt", |
| 610 | text: "new content", |
| 611 | isSearchAndReplace: true, |
| 612 | }; |
| 613 | |
| 614 | const toolCallState: ToolCallState = { |
| 615 | toolCallId: "test-tool-call", |
| 616 | status: "done", |
| 617 | ...UNUSED_TOOL_CALL_PARAMS, |
| 618 | }; |
| 619 | const applyState: ApplyState = { |
| 620 | status: "streaming", |
| 621 | streamId: "test-stream", |
| 622 | }; |
| 623 | |
| 624 | vi.mocked(selectToolCallById).mockReturnValue(toolCallState); |
| 625 | vi.mocked(selectApplyStateByToolCallId).mockReturnValue(applyState); |
| 626 | mockGetState.mockReturnValue({}); |
| 627 | |
| 628 | mockExtra.ideMessenger.request.mockRejectedValue( |
| 629 | new Error("Request failed"), |
| 630 | ); |
| 631 | |
| 632 | const thunk = applyForEditTool(payload); |
| 633 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 634 | |
| 635 | expect(errorToolCall).not.toHaveBeenCalled(); |
| 636 | expect(updateToolCallOutput).not.toHaveBeenCalled(); |
| 637 | }); |
| 638 | |
| 639 | it("should not error if apply state is closed", async () => { |
| 640 | const payload: ApplyToFilePayload & { toolCallId: string } = { |
| 641 | toolCallId: "test-tool-call", |
| 642 | streamId: "test-stream", |
| 643 | filepath: "test.txt", |
| 644 | text: "new content", |
| 645 | isSearchAndReplace: true, |
| 646 | }; |
| 647 | |
| 648 | const toolCallState: ToolCallState = { |
| 649 | toolCallId: "test-tool-call", |
| 650 | status: "calling", |
| 651 | ...UNUSED_TOOL_CALL_PARAMS, |
| 652 | }; |
| 653 | const applyState: ApplyState = { |
| 654 | status: "closed", |
| 655 | streamId: "test-stream", |
| 656 | }; // Already closed |
| 657 | |
| 658 | vi.mocked(selectToolCallById).mockReturnValue(toolCallState); |
| 659 | vi.mocked(selectApplyStateByToolCallId).mockReturnValue(applyState); |
| 660 | mockGetState.mockReturnValue({}); |
| 661 | |
| 662 | mockExtra.ideMessenger.request.mockRejectedValue( |
| 663 | new Error("Request failed"), |
| 664 | ); |
| 665 | |
| 666 | const thunk = applyForEditTool(payload); |
| 667 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 668 | |
| 669 | expect(errorToolCall).not.toHaveBeenCalled(); |
| 670 | expect(updateToolCallOutput).not.toHaveBeenCalled(); |
| 671 | }); |
| 672 | |
| 673 | it("should not error if tool call state is not found", async () => { |
| 674 | const payload: ApplyToFilePayload & { toolCallId: string } = { |
| 675 | toolCallId: "test-tool-call", |
| 676 | streamId: "test-stream", |
| 677 | filepath: "test.txt", |
| 678 | text: "new content", |
| 679 | isSearchAndReplace: true, |
| 680 | }; |
| 681 | |
| 682 | vi.mocked(selectToolCallById).mockReturnValue(undefined); // Tool call not found |
| 683 | vi.mocked(selectApplyStateByToolCallId).mockReturnValue({ |
| 684 | status: "streaming", |
| 685 | streamId: "test-stream", |
| 686 | }); |
| 687 | mockGetState.mockReturnValue({}); |
| 688 | |
| 689 | mockExtra.ideMessenger.request.mockRejectedValue( |
| 690 | new Error("Request failed"), |
| 691 | ); |
| 692 | |
| 693 | const thunk = applyForEditTool(payload); |
| 694 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 695 | |
| 696 | expect(errorToolCall).not.toHaveBeenCalled(); |
| 697 | expect(updateToolCallOutput).not.toHaveBeenCalled(); |
| 698 | }); |
| 699 | |
| 700 | it("should not error if apply state is not found", async () => { |
| 701 | const payload: ApplyToFilePayload & { toolCallId: string } = { |
| 702 | toolCallId: "test-tool-call", |
| 703 | streamId: "test-stream", |
| 704 | filepath: "test.txt", |
| 705 | text: "new content", |
| 706 | isSearchAndReplace: true, |
| 707 | }; |
| 708 | |
| 709 | vi.mocked(selectApplyStateByToolCallId).mockReturnValue(undefined); // Apply state not found |
| 710 | mockGetState.mockReturnValue({}); |
| 711 | |
| 712 | mockExtra.ideMessenger.request.mockRejectedValue( |
| 713 | new Error("Request failed"), |
| 714 | ); |
| 715 | |
| 716 | const thunk = applyForEditTool(payload); |
| 717 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 718 | |
| 719 | expect(errorToolCall).not.toHaveBeenCalled(); |
| 720 | expect(updateToolCallOutput).not.toHaveBeenCalled(); |
| 721 | }); |
| 722 | }); |
| 723 | |
| 724 | describe("edge cases", () => { |
| 725 | it("should handle different payload types", async () => { |
| 726 | const payloads: (ApplyToFilePayload & { toolCallId: string })[] = [ |
| 727 | { |
| 728 | toolCallId: "test-1", |
| 729 | streamId: "stream-1", |
| 730 | filepath: "file1.txt", |
| 731 | text: "content 1", |
| 732 | isSearchAndReplace: false, |
| 733 | }, |
| 734 | { |
| 735 | toolCallId: "test-2", |
| 736 | streamId: "stream-2", |
| 737 | filepath: "file2.js", |
| 738 | text: "content 2", |
| 739 | isSearchAndReplace: true, |
| 740 | }, |
| 741 | ]; |
| 742 | |
| 743 | for (const payload of payloads) { |
| 744 | vi.clearAllMocks(); |
| 745 | mockExtra.ideMessenger.request.mockResolvedValue({ status: "success" }); |
| 746 | |
| 747 | const thunk = applyForEditTool(payload); |
| 748 | await thunk(mockDispatch, mockGetState, mockExtra); |
| 749 | |
| 750 | expect(mockExtra.ideMessenger.request).toHaveBeenCalledWith( |
| 751 | "applyToFile", |
| 752 | payload, |
| 753 | ); |
| 754 | expect(updateApplyState).toHaveBeenCalledWith({ |
| 755 | streamId: payload.streamId, |
| 756 | toolCallId: payload.toolCallId, |
| 757 | status: "not-started", |
| 758 | }); |
| 759 | } |
| 760 | }); |
| 761 | }); |
| 762 | }); |