| 1 | import { JSONContent } from "@tiptap/core"; |
| 2 | import { InputModifiers } from "core"; |
| 3 | import { describe, expect, it, vi } from "vitest"; |
| 4 | import { createMockStore } from "../../util/test/mockStore"; |
| 5 | import { streamResponseThunk } from "./streamResponse"; |
| 6 | |
| 7 | // Mock external dependencies only - let selectors run naturally |
| 8 | // Removed: modelSupportsNativeTools - let it run naturally |
| 9 | |
| 10 | // Removed: addSystemMessageToolsToSystemMessage - let it run naturally |
| 11 | |
| 12 | // Mock system message construction to keep test readable |
| 13 | vi.mock("../util/getBaseSystemMessage", () => ({ |
| 14 | getBaseSystemMessage: vi.fn(), |
| 15 | })); |
| 16 | |
| 17 | import { getBaseSystemMessage } from "../util/getBaseSystemMessage"; |
| 18 | |
| 19 | // Removed: shouldAutoEnableSystemMessageTools - let it run naturally |
| 20 | |
| 21 | // Additional mocks for streamResponseThunk |
| 22 | vi.mock("posthog-js", () => ({ |
| 23 | default: { |
| 24 | capture: vi.fn(), |
| 25 | }, |
| 26 | })); |
| 27 | |
| 28 | vi.mock("uuid", () => ({ |
| 29 | v4: vi.fn(() => "mock-uuid-123"), |
| 30 | })); |
| 31 | |
| 32 | vi.mock( |
| 33 | "../../components/mainInput/TipTapEditor/utils/resolveEditorContent", |
| 34 | () => ({ |
| 35 | resolveEditorContent: vi.fn(), |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | import { ModelDescription } from "core"; |
| 40 | import posthog from "posthog-js"; |
| 41 | import { resolveEditorContent } from "../../components/mainInput/TipTapEditor/utils/resolveEditorContent"; |
| 42 | |
| 43 | const mockGetBaseSystemMessage = vi.mocked(getBaseSystemMessage); |
| 44 | |
| 45 | const mockPosthog = vi.mocked(posthog); |
| 46 | const mockResolveEditorContent = vi.mocked(resolveEditorContent); |
| 47 | |
| 48 | const mockClaudeModel: ModelDescription = { |
| 49 | title: "Claude 3.5 Sonnet", |
| 50 | model: "claude-3-5-sonnet-20241022", |
| 51 | provider: "anthropic", |
| 52 | underlyingProviderName: "anthropic", |
| 53 | completionOptions: { reasoningBudgetTokens: 2048 }, |
| 54 | }; |
| 55 | |
| 56 | // Mock editor state (what user types in the input) |
| 57 | const mockEditorState: JSONContent = { |
| 58 | type: "doc", |
| 59 | content: [ |
| 60 | { |
| 61 | type: "paragraph", |
| 62 | content: [{ type: "text", text: "Hello, please help me with this code" }], |
| 63 | }, |
| 64 | ], |
| 65 | }; |
| 66 | |
| 67 | // Mock input modifiers (codebase context, etc.) |
| 68 | const mockModifiers: InputModifiers = { |
| 69 | useCodebase: true, |
| 70 | noContext: false, |
| 71 | }; |
| 72 | |
| 73 | function setupTest() { |
| 74 | vi.clearAllMocks(); |
| 75 | |
| 76 | // Default mock implementations for external functions |
| 77 | |
| 78 | // Default mock for resolveEditorContent (can be overridden in individual tests) |
| 79 | mockResolveEditorContent.mockResolvedValue({ |
| 80 | selectedContextItems: [], |
| 81 | selectedCode: [], |
| 82 | content: "Hello, please help me with this code", |
| 83 | legacyCommandWithInput: undefined, |
| 84 | }); |
| 85 | |
| 86 | // Mock getBaseSystemMessage to return simple system message for readable tests |
| 87 | mockGetBaseSystemMessage.mockReturnValue("You are a helpful assistant."); |
| 88 | |
| 89 | // Create store with realistic state that selectors can work with |
| 90 | const mockStore = createMockStore({ |
| 91 | session: { |
| 92 | history: [ |
| 93 | { |
| 94 | message: { id: "1", role: "user", content: "Hello" }, |
| 95 | contextItems: [], |
| 96 | }, |
| 97 | ], |
| 98 | hasReasoningEnabled: false, |
| 99 | isStreaming: true, |
| 100 | id: "session-123", |
| 101 | mode: "chat", |
| 102 | streamAborter: new AbortController(), |
| 103 | contextPercentage: 0, |
| 104 | isPruned: false, |
| 105 | isInEdit: false, |
| 106 | title: "", |
| 107 | lastSessionId: undefined, |
| 108 | isSessionMetadataLoading: false, |
| 109 | allSessionMetadata: [], |
| 110 | symbols: {}, |
| 111 | codeBlockApplyStates: { |
| 112 | states: [], |
| 113 | curIndex: 0, |
| 114 | }, |
| 115 | newestToolbarPreviewForInput: {}, |
| 116 | compactionLoading: {}, |
| 117 | inlineErrorMessage: undefined, |
| 118 | }, |
| 119 | config: { |
| 120 | config: { |
| 121 | tools: [], |
| 122 | rules: [], |
| 123 | tabAutocompleteModel: undefined, |
| 124 | selectedModelByRole: { |
| 125 | chat: mockClaudeModel, |
| 126 | apply: null, |
| 127 | edit: null, |
| 128 | summarize: null, |
| 129 | autocomplete: null, |
| 130 | rerank: null, |
| 131 | embed: null, |
| 132 | }, |
| 133 | experimental: { |
| 134 | onlyUseSystemMessageTools: false, |
| 135 | }, |
| 136 | } as any, |
| 137 | lastSelectedModelByRole: { |
| 138 | chat: mockClaudeModel.title, |
| 139 | }, |
| 140 | } as any, |
| 141 | ui: { |
| 142 | toolSettings: {}, |
| 143 | ruleSettings: {}, |
| 144 | showDialog: false, |
| 145 | dialogMessage: undefined, |
| 146 | } as any, |
| 147 | }); |
| 148 | |
| 149 | const mockIdeMessenger = mockStore.mockIdeMessenger; |
| 150 | |
| 151 | return { mockStore, mockIdeMessenger }; |
| 152 | } |
| 153 | |
| 154 | describe("streamResponseThunk", () => { |
| 155 | it("should throw error when no chat model is selected", async () => { |
| 156 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 157 | |
| 158 | // Create store with no selected chat model |
| 159 | const mockStoreNoModel = createMockStore({ |
| 160 | session: { |
| 161 | history: [ |
| 162 | { |
| 163 | message: { id: "1", role: "user", content: "Hello" }, |
| 164 | contextItems: [], |
| 165 | }, |
| 166 | ], |
| 167 | hasReasoningEnabled: false, |
| 168 | isStreaming: true, |
| 169 | id: "session-123", |
| 170 | mode: "chat", |
| 171 | streamAborter: new AbortController(), |
| 172 | contextPercentage: 0, |
| 173 | isPruned: false, |
| 174 | isInEdit: false, |
| 175 | title: "", |
| 176 | lastSessionId: undefined, |
| 177 | isSessionMetadataLoading: false, |
| 178 | allSessionMetadata: [], |
| 179 | symbols: {}, |
| 180 | codeBlockApplyStates: { |
| 181 | states: [], |
| 182 | curIndex: 0, |
| 183 | }, |
| 184 | newestToolbarPreviewForInput: {}, |
| 185 | compactionLoading: {}, |
| 186 | inlineErrorMessage: undefined, |
| 187 | }, |
| 188 | config: { |
| 189 | config: { |
| 190 | tools: [], |
| 191 | rules: [], |
| 192 | tabAutocompleteModel: undefined, |
| 193 | selectedModelByRole: { |
| 194 | chat: null, // No model selected |
| 195 | apply: null, |
| 196 | edit: null, |
| 197 | summarize: null, |
| 198 | autocomplete: null, |
| 199 | rerank: null, |
| 200 | embed: null, |
| 201 | }, |
| 202 | experimental: { |
| 203 | onlyUseSystemMessageTools: false, |
| 204 | }, |
| 205 | } as any, |
| 206 | lastSelectedModelByRole: {}, |
| 207 | } as any, |
| 208 | ui: { |
| 209 | toolSettings: {}, |
| 210 | ruleSettings: {}, |
| 211 | showDialog: false, |
| 212 | dialogMessage: undefined, |
| 213 | } as any, |
| 214 | }); |
| 215 | |
| 216 | // Execute thunk and expect it to be rejected |
| 217 | const result = await mockStoreNoModel.dispatch( |
| 218 | streamResponseThunk({ |
| 219 | editorState: mockEditorState, |
| 220 | modifiers: mockModifiers, |
| 221 | }) as any, |
| 222 | ); |
| 223 | |
| 224 | // Verify the thunk completed successfully but shows error dialog |
| 225 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 226 | |
| 227 | // Verify the complete error handling flow |
| 228 | const dispatchedActions = (mockStoreNoModel as any).getActions(); |
| 229 | expect(dispatchedActions).toEqual([ |
| 230 | { |
| 231 | type: "chat/streamResponse/pending", |
| 232 | meta: { |
| 233 | arg: { |
| 234 | editorState: mockEditorState, |
| 235 | modifiers: mockModifiers, |
| 236 | }, |
| 237 | requestId: expect.any(String), |
| 238 | requestStatus: "pending", |
| 239 | }, |
| 240 | payload: undefined, |
| 241 | }, |
| 242 | { |
| 243 | type: "chat/streamWrapper/pending", |
| 244 | meta: { |
| 245 | arg: expect.any(Function), |
| 246 | requestId: expect.any(String), |
| 247 | requestStatus: "pending", |
| 248 | }, |
| 249 | payload: undefined, |
| 250 | }, |
| 251 | { |
| 252 | type: "chat/cancelStream/pending", |
| 253 | meta: { |
| 254 | arg: undefined, |
| 255 | requestId: expect.any(String), |
| 256 | requestStatus: "pending", |
| 257 | }, |
| 258 | payload: undefined, |
| 259 | }, |
| 260 | { |
| 261 | type: "session/setInactive", |
| 262 | payload: undefined, |
| 263 | }, |
| 264 | { |
| 265 | type: "session/abortStream", |
| 266 | payload: undefined, |
| 267 | }, |
| 268 | { |
| 269 | type: "session/clearDanglingMessages", |
| 270 | payload: undefined, |
| 271 | }, |
| 272 | { |
| 273 | type: "chat/cancelStream/fulfilled", |
| 274 | meta: { |
| 275 | arg: undefined, |
| 276 | requestId: expect.any(String), |
| 277 | requestStatus: "fulfilled", |
| 278 | }, |
| 279 | payload: undefined, |
| 280 | }, |
| 281 | { |
| 282 | type: "ui/setDialogMessage", |
| 283 | payload: expect.objectContaining({ |
| 284 | props: expect.objectContaining({ |
| 285 | error: expect.objectContaining({ |
| 286 | message: "No chat model selected", |
| 287 | }), |
| 288 | }), |
| 289 | }), |
| 290 | }, |
| 291 | { |
| 292 | type: "ui/setShowDialog", |
| 293 | payload: true, |
| 294 | }, |
| 295 | { |
| 296 | type: "chat/streamWrapper/fulfilled", |
| 297 | meta: { |
| 298 | arg: expect.any(Function), |
| 299 | requestId: expect.any(String), |
| 300 | requestStatus: "fulfilled", |
| 301 | }, |
| 302 | payload: undefined, |
| 303 | }, |
| 304 | { |
| 305 | type: "chat/streamResponse/fulfilled", |
| 306 | meta: { |
| 307 | arg: { |
| 308 | editorState: mockEditorState, |
| 309 | modifiers: mockModifiers, |
| 310 | }, |
| 311 | requestId: expect.any(String), |
| 312 | requestStatus: "fulfilled", |
| 313 | }, |
| 314 | payload: undefined, |
| 315 | }, |
| 316 | ]); |
| 317 | |
| 318 | // Verify no IDE messenger calls were made |
| 319 | expect(mockStoreNoModel.mockIdeMessenger.request).not.toHaveBeenCalled(); |
| 320 | expect( |
| 321 | mockStoreNoModel.mockIdeMessenger.llmStreamChat, |
| 322 | ).not.toHaveBeenCalled(); |
| 323 | |
| 324 | // Verify final state shows error dialog and inactive streaming |
| 325 | const finalState = mockStoreNoModel.getState(); |
| 326 | expect(finalState).toEqual({ |
| 327 | session: { |
| 328 | history: [ |
| 329 | { |
| 330 | contextItems: [], |
| 331 | isGatheringContext: false, |
| 332 | message: { id: "1", role: "user", content: "Hello" }, |
| 333 | }, |
| 334 | ], |
| 335 | hasReasoningEnabled: false, |
| 336 | isStreaming: false, // Set to inactive after error |
| 337 | id: "session-123", |
| 338 | mode: "chat", |
| 339 | streamAborter: expect.any(AbortController), |
| 340 | contextPercentage: 0, |
| 341 | isPruned: false, |
| 342 | isInEdit: false, |
| 343 | title: "", |
| 344 | lastSessionId: undefined, |
| 345 | isSessionMetadataLoading: false, |
| 346 | allSessionMetadata: [], |
| 347 | symbols: {}, |
| 348 | codeBlockApplyStates: { |
| 349 | states: [], |
| 350 | curIndex: 0, |
| 351 | }, |
| 352 | newestToolbarPreviewForInput: {}, |
| 353 | compactionLoading: {}, |
| 354 | inlineErrorMessage: undefined, |
| 355 | }, |
| 356 | config: { |
| 357 | config: { |
| 358 | tools: [], |
| 359 | rules: [], |
| 360 | tabAutocompleteModel: undefined, |
| 361 | selectedModelByRole: { |
| 362 | chat: null, // No model selected |
| 363 | apply: null, |
| 364 | edit: null, |
| 365 | summarize: null, |
| 366 | autocomplete: null, |
| 367 | rerank: null, |
| 368 | embed: null, |
| 369 | }, |
| 370 | experimental: { |
| 371 | onlyUseSystemMessageTools: false, |
| 372 | }, |
| 373 | }, |
| 374 | lastSelectedModelByRole: {}, |
| 375 | loading: false, |
| 376 | configError: undefined, |
| 377 | }, |
| 378 | ui: { |
| 379 | toolSettings: {}, |
| 380 | ruleSettings: {}, |
| 381 | showDialog: true, // Error dialog is shown |
| 382 | dialogMessage: expect.objectContaining({ |
| 383 | props: expect.objectContaining({ |
| 384 | error: expect.objectContaining({ |
| 385 | message: "No chat model selected", |
| 386 | }), |
| 387 | }), |
| 388 | }), |
| 389 | }, |
| 390 | editModeState: { |
| 391 | isInEdit: false, |
| 392 | returnToMode: "chat", |
| 393 | }, |
| 394 | indexing: { |
| 395 | indexing: { |
| 396 | statuses: {}, |
| 397 | hiddenChatPeekTypes: { |
| 398 | docs: false, |
| 399 | }, |
| 400 | }, |
| 401 | }, |
| 402 | tabs: { |
| 403 | tabsItems: [], |
| 404 | }, |
| 405 | profiles: { |
| 406 | organizations: [], |
| 407 | selectedProfileId: null, |
| 408 | selectedOrganizationId: null, |
| 409 | preferencesByProfileId: {}, |
| 410 | }, |
| 411 | }); |
| 412 | }); |
| 413 | |
| 414 | it("should handle compilation error - not enough context", async () => { |
| 415 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 416 | |
| 417 | // Mock compilation failure with "Not enough context" error |
| 418 | mockIdeMessenger.request.mockResolvedValue({ |
| 419 | status: "error", |
| 420 | error: "Not enough context available for this request", |
| 421 | }); |
| 422 | |
| 423 | // Execute thunk |
| 424 | const result = await mockStore.dispatch( |
| 425 | streamResponseThunk({ |
| 426 | editorState: mockEditorState, |
| 427 | modifiers: mockModifiers, |
| 428 | }) as any, |
| 429 | ); |
| 430 | |
| 431 | // Verify thunk completed successfully (doesn't throw, handles error gracefully) |
| 432 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 433 | |
| 434 | // Verify exact action sequence for this error path |
| 435 | const dispatchedActions = (mockStore as any).getActions(); |
| 436 | expect(dispatchedActions).toEqual([ |
| 437 | { |
| 438 | type: "chat/streamResponse/pending", |
| 439 | meta: { |
| 440 | arg: { |
| 441 | editorState: mockEditorState, |
| 442 | modifiers: mockModifiers, |
| 443 | }, |
| 444 | requestId: expect.any(String), |
| 445 | requestStatus: "pending", |
| 446 | }, |
| 447 | payload: undefined, |
| 448 | }, |
| 449 | { |
| 450 | type: "chat/streamWrapper/pending", |
| 451 | meta: { |
| 452 | arg: expect.any(Function), |
| 453 | requestId: expect.any(String), |
| 454 | requestStatus: "pending", |
| 455 | }, |
| 456 | payload: undefined, |
| 457 | }, |
| 458 | { |
| 459 | type: "session/submitEditorAndInitAtIndex", |
| 460 | payload: { |
| 461 | editorState: mockEditorState, |
| 462 | index: 1, |
| 463 | }, |
| 464 | }, |
| 465 | { |
| 466 | type: "session/resetNextCodeBlockToApplyIndex", |
| 467 | payload: undefined, |
| 468 | }, |
| 469 | { |
| 470 | type: "symbols/updateFromContextItems/pending", |
| 471 | meta: { |
| 472 | arg: [], |
| 473 | requestId: expect.any(String), |
| 474 | requestStatus: "pending", |
| 475 | }, |
| 476 | payload: undefined, |
| 477 | }, |
| 478 | { |
| 479 | type: "session/updateHistoryItemAtIndex", |
| 480 | payload: { |
| 481 | index: 1, |
| 482 | updates: { |
| 483 | contextItems: [], |
| 484 | message: { |
| 485 | content: "Hello, please help me with this code", |
| 486 | id: "mock-uuid-123", |
| 487 | role: "user", |
| 488 | }, |
| 489 | }, |
| 490 | }, |
| 491 | }, |
| 492 | { |
| 493 | type: "chat/streamNormalInput/pending", |
| 494 | meta: { |
| 495 | arg: { |
| 496 | legacySlashCommandData: undefined, |
| 497 | }, |
| 498 | requestId: expect.any(String), |
| 499 | requestStatus: "pending", |
| 500 | }, |
| 501 | payload: undefined, |
| 502 | }, |
| 503 | { |
| 504 | type: "session/setAppliedRulesAtIndex", |
| 505 | payload: { |
| 506 | appliedRules: [], |
| 507 | index: 1, |
| 508 | }, |
| 509 | }, |
| 510 | { |
| 511 | type: "session/setActive", |
| 512 | payload: undefined, |
| 513 | }, |
| 514 | { |
| 515 | type: "session/setInlineErrorMessage", |
| 516 | payload: undefined, |
| 517 | }, |
| 518 | { |
| 519 | type: "session/setInlineErrorMessage", |
| 520 | payload: "out-of-context", |
| 521 | }, |
| 522 | { |
| 523 | type: "session/setInactive", |
| 524 | payload: undefined, |
| 525 | }, |
| 526 | { |
| 527 | type: "symbols/updateFromContextItems/fulfilled", |
| 528 | meta: { |
| 529 | arg: [], |
| 530 | requestId: expect.any(String), |
| 531 | requestStatus: "fulfilled", |
| 532 | }, |
| 533 | payload: undefined, |
| 534 | }, |
| 535 | { |
| 536 | type: "chat/streamNormalInput/fulfilled", |
| 537 | meta: { |
| 538 | arg: { |
| 539 | legacySlashCommandData: undefined, |
| 540 | }, |
| 541 | requestId: expect.any(String), |
| 542 | requestStatus: "fulfilled", |
| 543 | }, |
| 544 | payload: undefined, |
| 545 | }, |
| 546 | { |
| 547 | type: "session/saveCurrent/pending", |
| 548 | meta: { |
| 549 | arg: { |
| 550 | generateTitle: true, |
| 551 | openNewSession: false, |
| 552 | }, |
| 553 | requestId: expect.any(String), |
| 554 | requestStatus: "pending", |
| 555 | }, |
| 556 | payload: undefined, |
| 557 | }, |
| 558 | { |
| 559 | type: "session/update/pending", |
| 560 | meta: { |
| 561 | arg: expect.objectContaining({ |
| 562 | history: expect.any(Array), |
| 563 | sessionId: "session-123", |
| 564 | title: "New Session", |
| 565 | workspaceDirectory: "", |
| 566 | }), |
| 567 | requestId: expect.any(String), |
| 568 | requestStatus: "pending", |
| 569 | }, |
| 570 | payload: undefined, |
| 571 | }, |
| 572 | { |
| 573 | type: "session/updateSessionMetadata", |
| 574 | payload: { |
| 575 | sessionId: "session-123", |
| 576 | title: "New Session", |
| 577 | }, |
| 578 | }, |
| 579 | { |
| 580 | type: "session/refreshMetadata/pending", |
| 581 | meta: { |
| 582 | arg: {}, |
| 583 | requestId: expect.any(String), |
| 584 | requestStatus: "pending", |
| 585 | }, |
| 586 | payload: undefined, |
| 587 | }, |
| 588 | { |
| 589 | type: "session/refreshMetadata/rejected", |
| 590 | meta: { |
| 591 | aborted: false, |
| 592 | arg: {}, |
| 593 | condition: false, |
| 594 | rejectedWithValue: false, |
| 595 | requestId: expect.any(String), |
| 596 | requestStatus: "rejected", |
| 597 | }, |
| 598 | payload: undefined, |
| 599 | error: { |
| 600 | message: "Not enough context available for this request", |
| 601 | name: "Error", |
| 602 | stack: expect.any(String), |
| 603 | }, |
| 604 | }, |
| 605 | { |
| 606 | type: "session/update/fulfilled", |
| 607 | meta: { |
| 608 | arg: expect.objectContaining({ |
| 609 | history: expect.any(Array), |
| 610 | sessionId: "session-123", |
| 611 | title: "New Session", |
| 612 | workspaceDirectory: "", |
| 613 | }), |
| 614 | requestId: expect.any(String), |
| 615 | requestStatus: "fulfilled", |
| 616 | }, |
| 617 | payload: undefined, |
| 618 | }, |
| 619 | { |
| 620 | type: "session/saveCurrent/fulfilled", |
| 621 | meta: { |
| 622 | arg: { |
| 623 | generateTitle: true, |
| 624 | openNewSession: false, |
| 625 | }, |
| 626 | requestId: expect.any(String), |
| 627 | requestStatus: "fulfilled", |
| 628 | }, |
| 629 | payload: undefined, |
| 630 | }, |
| 631 | { |
| 632 | type: "chat/streamWrapper/fulfilled", |
| 633 | meta: { |
| 634 | arg: expect.any(Function), |
| 635 | requestId: expect.any(String), |
| 636 | requestStatus: "fulfilled", |
| 637 | }, |
| 638 | payload: undefined, |
| 639 | }, |
| 640 | { |
| 641 | type: "chat/streamResponse/fulfilled", |
| 642 | meta: { |
| 643 | arg: { |
| 644 | editorState: mockEditorState, |
| 645 | modifiers: mockModifiers, |
| 646 | }, |
| 647 | requestId: expect.any(String), |
| 648 | requestStatus: "fulfilled", |
| 649 | }, |
| 650 | payload: undefined, |
| 651 | }, |
| 652 | ]); |
| 653 | |
| 654 | // Verify IDE messenger was called for compilation but not streaming |
| 655 | expect(mockIdeMessenger.request).toHaveBeenCalledWith("llm/compileChat", { |
| 656 | messages: [ |
| 657 | { |
| 658 | role: "system", |
| 659 | content: "You are a helpful assistant.", |
| 660 | }, |
| 661 | { |
| 662 | role: "user", |
| 663 | content: [ |
| 664 | { |
| 665 | text: "Hello", |
| 666 | type: "text", |
| 667 | }, |
| 668 | ], |
| 669 | }, |
| 670 | { |
| 671 | role: "user", |
| 672 | content: [ |
| 673 | { |
| 674 | text: "Hello, please help me with this code", |
| 675 | type: "text", |
| 676 | }, |
| 677 | ], |
| 678 | }, |
| 679 | ], |
| 680 | options: { |
| 681 | reasoning: false, |
| 682 | }, |
| 683 | }); |
| 684 | expect(mockIdeMessenger.llmStreamChat).not.toHaveBeenCalled(); |
| 685 | |
| 686 | // Verify final state shows error condition |
| 687 | const finalState = mockStore.getState(); |
| 688 | expect(finalState).toEqual({ |
| 689 | session: { |
| 690 | history: [ |
| 691 | { |
| 692 | contextItems: [], |
| 693 | message: { |
| 694 | content: "Hello", |
| 695 | id: "1", |
| 696 | role: "user", |
| 697 | }, |
| 698 | }, |
| 699 | { |
| 700 | appliedRules: [], |
| 701 | contextItems: [], |
| 702 | editorState: { |
| 703 | type: "doc", |
| 704 | content: [ |
| 705 | { |
| 706 | type: "paragraph", |
| 707 | content: [ |
| 708 | { |
| 709 | type: "text", |
| 710 | text: "Hello, please help me with this code", |
| 711 | }, |
| 712 | ], |
| 713 | }, |
| 714 | ], |
| 715 | }, |
| 716 | message: { |
| 717 | content: "Hello, please help me with this code", |
| 718 | id: "mock-uuid-123", |
| 719 | role: "user", |
| 720 | }, |
| 721 | }, |
| 722 | { |
| 723 | contextItems: [], |
| 724 | isGatheringContext: false, |
| 725 | message: { |
| 726 | content: "", |
| 727 | id: "mock-uuid-123", |
| 728 | role: "assistant", |
| 729 | }, |
| 730 | }, |
| 731 | ], |
| 732 | hasReasoningEnabled: false, |
| 733 | isStreaming: false, // Should be inactive due to error |
| 734 | id: "session-123", |
| 735 | mode: "chat", |
| 736 | streamAborter: expect.any(AbortController), |
| 737 | contextPercentage: 0, // Unchanged - no successful compilation |
| 738 | isPruned: false, // Unchanged - no successful compilation |
| 739 | isInEdit: false, |
| 740 | title: "New Session", |
| 741 | lastSessionId: undefined, |
| 742 | isSessionMetadataLoading: false, |
| 743 | allSessionMetadata: [], |
| 744 | symbols: {}, |
| 745 | codeBlockApplyStates: { |
| 746 | states: [], |
| 747 | curIndex: 0, |
| 748 | }, |
| 749 | newestToolbarPreviewForInput: {}, |
| 750 | compactionLoading: {}, |
| 751 | inlineErrorMessage: "out-of-context", // Error message set |
| 752 | }, |
| 753 | config: { |
| 754 | config: { |
| 755 | tools: [], |
| 756 | rules: [], |
| 757 | tabAutocompleteModel: undefined, |
| 758 | selectedModelByRole: { |
| 759 | chat: mockClaudeModel, |
| 760 | apply: null, |
| 761 | edit: null, |
| 762 | summarize: null, |
| 763 | autocomplete: null, |
| 764 | rerank: null, |
| 765 | embed: null, |
| 766 | }, |
| 767 | experimental: { |
| 768 | onlyUseSystemMessageTools: false, |
| 769 | }, |
| 770 | }, |
| 771 | lastSelectedModelByRole: { |
| 772 | chat: mockClaudeModel.title, |
| 773 | }, |
| 774 | loading: false, |
| 775 | configError: undefined, |
| 776 | }, |
| 777 | ui: { |
| 778 | toolSettings: {}, |
| 779 | ruleSettings: {}, |
| 780 | showDialog: false, |
| 781 | dialogMessage: undefined, |
| 782 | }, |
| 783 | editModeState: { |
| 784 | isInEdit: false, |
| 785 | returnToMode: "chat", |
| 786 | }, |
| 787 | indexing: { |
| 788 | indexing: { |
| 789 | statuses: {}, |
| 790 | hiddenChatPeekTypes: { |
| 791 | docs: false, |
| 792 | }, |
| 793 | }, |
| 794 | }, |
| 795 | tabs: { |
| 796 | tabsItems: [], |
| 797 | }, |
| 798 | profiles: { |
| 799 | organizations: [], |
| 800 | selectedProfileId: null, |
| 801 | selectedOrganizationId: null, |
| 802 | preferencesByProfileId: {}, |
| 803 | }, |
| 804 | }); |
| 805 | }); |
| 806 | |
| 807 | it("should throw error for other compilation errors", async () => { |
| 808 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 809 | |
| 810 | // Mock compilation failure with generic error |
| 811 | mockIdeMessenger.request.mockResolvedValue({ |
| 812 | status: "error", |
| 813 | error: "Model configuration is invalid", |
| 814 | }); |
| 815 | |
| 816 | // Execute thunk and expect it to be rejected |
| 817 | const result = await mockStore.dispatch( |
| 818 | streamResponseThunk({ |
| 819 | editorState: mockEditorState, |
| 820 | modifiers: mockModifiers, |
| 821 | }) as any, |
| 822 | ); |
| 823 | |
| 824 | // Verify thunk completed successfully but shows error dialog |
| 825 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 826 | |
| 827 | // Verify exact action sequence for compilation error with dialog |
| 828 | const dispatchedActions = (mockStore as any).getActions(); |
| 829 | expect(dispatchedActions).toEqual([ |
| 830 | { |
| 831 | type: "chat/streamResponse/pending", |
| 832 | meta: { |
| 833 | arg: { |
| 834 | editorState: mockEditorState, |
| 835 | modifiers: mockModifiers, |
| 836 | }, |
| 837 | requestId: expect.any(String), |
| 838 | requestStatus: "pending", |
| 839 | }, |
| 840 | payload: undefined, |
| 841 | }, |
| 842 | { |
| 843 | type: "chat/streamWrapper/pending", |
| 844 | meta: { |
| 845 | arg: expect.any(Function), |
| 846 | requestId: expect.any(String), |
| 847 | requestStatus: "pending", |
| 848 | }, |
| 849 | payload: undefined, |
| 850 | }, |
| 851 | { |
| 852 | type: "session/submitEditorAndInitAtIndex", |
| 853 | payload: { |
| 854 | editorState: mockEditorState, |
| 855 | index: 1, |
| 856 | }, |
| 857 | }, |
| 858 | { |
| 859 | type: "session/resetNextCodeBlockToApplyIndex", |
| 860 | payload: undefined, |
| 861 | }, |
| 862 | { |
| 863 | type: "symbols/updateFromContextItems/pending", |
| 864 | meta: { |
| 865 | arg: [], |
| 866 | requestId: expect.any(String), |
| 867 | requestStatus: "pending", |
| 868 | }, |
| 869 | payload: undefined, |
| 870 | }, |
| 871 | { |
| 872 | type: "session/updateHistoryItemAtIndex", |
| 873 | payload: { |
| 874 | index: 1, |
| 875 | updates: { |
| 876 | contextItems: [], |
| 877 | message: { |
| 878 | content: "Hello, please help me with this code", |
| 879 | id: "mock-uuid-123", |
| 880 | role: "user", |
| 881 | }, |
| 882 | }, |
| 883 | }, |
| 884 | }, |
| 885 | { |
| 886 | type: "chat/streamNormalInput/pending", |
| 887 | meta: { |
| 888 | arg: { |
| 889 | legacySlashCommandData: undefined, |
| 890 | }, |
| 891 | requestId: expect.any(String), |
| 892 | requestStatus: "pending", |
| 893 | }, |
| 894 | payload: undefined, |
| 895 | }, |
| 896 | { |
| 897 | type: "session/setAppliedRulesAtIndex", |
| 898 | payload: { |
| 899 | appliedRules: [], |
| 900 | index: 1, |
| 901 | }, |
| 902 | }, |
| 903 | { |
| 904 | type: "session/setActive", |
| 905 | payload: undefined, |
| 906 | }, |
| 907 | { |
| 908 | type: "session/setInlineErrorMessage", |
| 909 | payload: undefined, |
| 910 | }, |
| 911 | { |
| 912 | type: "symbols/updateFromContextItems/fulfilled", |
| 913 | meta: { |
| 914 | arg: [], |
| 915 | requestId: expect.any(String), |
| 916 | requestStatus: "fulfilled", |
| 917 | }, |
| 918 | payload: undefined, |
| 919 | }, |
| 920 | { |
| 921 | type: "chat/streamNormalInput/rejected", |
| 922 | meta: { |
| 923 | aborted: false, |
| 924 | arg: { |
| 925 | legacySlashCommandData: undefined, |
| 926 | }, |
| 927 | condition: false, |
| 928 | rejectedWithValue: false, |
| 929 | requestId: expect.any(String), |
| 930 | requestStatus: "rejected", |
| 931 | }, |
| 932 | payload: undefined, |
| 933 | error: { |
| 934 | message: "Model configuration is invalid", |
| 935 | name: "Error", |
| 936 | stack: expect.any(String), |
| 937 | }, |
| 938 | }, |
| 939 | { |
| 940 | type: "chat/cancelStream/pending", |
| 941 | meta: { |
| 942 | arg: undefined, |
| 943 | requestId: expect.any(String), |
| 944 | requestStatus: "pending", |
| 945 | }, |
| 946 | payload: undefined, |
| 947 | }, |
| 948 | { |
| 949 | type: "session/setInactive", |
| 950 | payload: undefined, |
| 951 | }, |
| 952 | { |
| 953 | type: "session/abortStream", |
| 954 | payload: undefined, |
| 955 | }, |
| 956 | { |
| 957 | type: "session/clearDanglingMessages", |
| 958 | payload: undefined, |
| 959 | }, |
| 960 | { |
| 961 | type: "chat/cancelStream/fulfilled", |
| 962 | meta: { |
| 963 | arg: undefined, |
| 964 | requestId: expect.any(String), |
| 965 | requestStatus: "fulfilled", |
| 966 | }, |
| 967 | payload: undefined, |
| 968 | }, |
| 969 | { |
| 970 | type: "ui/setDialogMessage", |
| 971 | payload: expect.objectContaining({ |
| 972 | props: expect.objectContaining({ |
| 973 | error: expect.objectContaining({ |
| 974 | message: "Model configuration is invalid", |
| 975 | }), |
| 976 | }), |
| 977 | }), |
| 978 | }, |
| 979 | { |
| 980 | type: "ui/setShowDialog", |
| 981 | payload: true, |
| 982 | }, |
| 983 | { |
| 984 | type: "chat/streamWrapper/fulfilled", |
| 985 | meta: { |
| 986 | arg: expect.any(Function), |
| 987 | requestId: expect.any(String), |
| 988 | requestStatus: "fulfilled", |
| 989 | }, |
| 990 | payload: undefined, |
| 991 | }, |
| 992 | { |
| 993 | type: "chat/streamResponse/fulfilled", |
| 994 | meta: { |
| 995 | arg: { |
| 996 | editorState: mockEditorState, |
| 997 | modifiers: mockModifiers, |
| 998 | }, |
| 999 | requestId: expect.any(String), |
| 1000 | requestStatus: "fulfilled", |
| 1001 | }, |
| 1002 | payload: undefined, |
| 1003 | }, |
| 1004 | ]); |
| 1005 | |
| 1006 | // Verify IDE messenger was called for compilation but not streaming |
| 1007 | expect(mockIdeMessenger.request).toHaveBeenCalledWith("llm/compileChat", { |
| 1008 | messages: [ |
| 1009 | { |
| 1010 | role: "system", |
| 1011 | content: "You are a helpful assistant.", |
| 1012 | }, |
| 1013 | { |
| 1014 | role: "user", |
| 1015 | content: [ |
| 1016 | { |
| 1017 | type: "text", |
| 1018 | text: "Hello", |
| 1019 | }, |
| 1020 | ], |
| 1021 | }, |
| 1022 | { |
| 1023 | role: "user", |
| 1024 | content: [ |
| 1025 | { |
| 1026 | type: "text", |
| 1027 | text: "Hello, please help me with this code", |
| 1028 | }, |
| 1029 | ], |
| 1030 | }, |
| 1031 | ], |
| 1032 | options: { |
| 1033 | reasoning: false, |
| 1034 | }, |
| 1035 | }); |
| 1036 | expect(mockIdeMessenger.llmStreamChat).not.toHaveBeenCalled(); |
| 1037 | |
| 1038 | // Verify final state shows error dialog and inactive streaming |
| 1039 | const finalState = mockStore.getState(); |
| 1040 | expect(finalState).toEqual({ |
| 1041 | session: { |
| 1042 | history: [ |
| 1043 | { |
| 1044 | contextItems: [], |
| 1045 | message: { id: "1", role: "user", content: "Hello" }, |
| 1046 | }, |
| 1047 | ], |
| 1048 | hasReasoningEnabled: false, |
| 1049 | isStreaming: false, // Set to inactive after error |
| 1050 | id: "session-123", |
| 1051 | mode: "chat", |
| 1052 | streamAborter: expect.any(AbortController), |
| 1053 | contextPercentage: 0, |
| 1054 | isPruned: false, |
| 1055 | isInEdit: false, |
| 1056 | title: "", |
| 1057 | lastSessionId: undefined, |
| 1058 | isSessionMetadataLoading: false, |
| 1059 | allSessionMetadata: [], |
| 1060 | symbols: {}, |
| 1061 | codeBlockApplyStates: { |
| 1062 | states: [], |
| 1063 | curIndex: 0, |
| 1064 | }, |
| 1065 | newestToolbarPreviewForInput: {}, |
| 1066 | compactionLoading: {}, |
| 1067 | inlineErrorMessage: undefined, |
| 1068 | mainEditorContentTrigger: mockEditorState, // Editor content that triggered the request |
| 1069 | }, |
| 1070 | config: { |
| 1071 | config: { |
| 1072 | tools: [], |
| 1073 | rules: [], |
| 1074 | tabAutocompleteModel: undefined, |
| 1075 | selectedModelByRole: { |
| 1076 | chat: mockClaudeModel, |
| 1077 | apply: null, |
| 1078 | edit: null, |
| 1079 | summarize: null, |
| 1080 | autocomplete: null, |
| 1081 | rerank: null, |
| 1082 | embed: null, |
| 1083 | }, |
| 1084 | experimental: { |
| 1085 | onlyUseSystemMessageTools: false, |
| 1086 | }, |
| 1087 | }, |
| 1088 | lastSelectedModelByRole: { |
| 1089 | chat: mockClaudeModel.title, |
| 1090 | }, |
| 1091 | loading: false, |
| 1092 | configError: undefined, |
| 1093 | }, |
| 1094 | ui: { |
| 1095 | toolSettings: {}, |
| 1096 | ruleSettings: {}, |
| 1097 | showDialog: true, // Error dialog is shown |
| 1098 | dialogMessage: expect.objectContaining({ |
| 1099 | props: expect.objectContaining({ |
| 1100 | error: expect.objectContaining({ |
| 1101 | message: "Model configuration is invalid", |
| 1102 | }), |
| 1103 | }), |
| 1104 | }), |
| 1105 | }, |
| 1106 | editModeState: { |
| 1107 | isInEdit: false, |
| 1108 | returnToMode: "chat", |
| 1109 | }, |
| 1110 | indexing: { |
| 1111 | indexing: { |
| 1112 | statuses: {}, |
| 1113 | hiddenChatPeekTypes: { |
| 1114 | docs: false, |
| 1115 | }, |
| 1116 | }, |
| 1117 | }, |
| 1118 | tabs: { |
| 1119 | tabsItems: [], |
| 1120 | }, |
| 1121 | profiles: { |
| 1122 | organizations: [], |
| 1123 | selectedProfileId: null, |
| 1124 | selectedOrganizationId: null, |
| 1125 | preferencesByProfileId: {}, |
| 1126 | }, |
| 1127 | }); |
| 1128 | }); |
| 1129 | }); |