|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | +import { ProviderSettings } from "@roo-code/types" |
| 3 | + |
| 4 | +import { Task } from "../Task" |
| 5 | +import { ClineProvider } from "../../webview/ClineProvider" |
| 6 | + |
| 7 | +// Mocks similar to Task.dispose.test.ts |
| 8 | +vi.mock("../../webview/ClineProvider") |
| 9 | +vi.mock("../../../integrations/terminal/TerminalRegistry", () => ({ |
| 10 | + TerminalRegistry: { |
| 11 | + releaseTerminalsForTask: vi.fn(), |
| 12 | + }, |
| 13 | +})) |
| 14 | +vi.mock("../../ignore/RooIgnoreController") |
| 15 | +vi.mock("../../protect/RooProtectedController") |
| 16 | +vi.mock("../../context-tracking/FileContextTracker") |
| 17 | +vi.mock("../../../services/browser/UrlContentFetcher") |
| 18 | +vi.mock("../../../services/browser/BrowserSession") |
| 19 | +vi.mock("../../../integrations/editor/DiffViewProvider") |
| 20 | +vi.mock("../../tools/ToolRepetitionDetector") |
| 21 | +vi.mock("../../../api", () => ({ |
| 22 | + buildApiHandler: vi.fn(() => ({ |
| 23 | + getModel: () => ({ info: {}, id: "test-model" }), |
| 24 | + })), |
| 25 | +})) |
| 26 | +vi.mock("../AutoApprovalHandler") |
| 27 | + |
| 28 | +// Mock TelemetryService |
| 29 | +vi.mock("@roo-code/telemetry", () => ({ |
| 30 | + TelemetryService: { |
| 31 | + instance: { |
| 32 | + captureTaskCreated: vi.fn(), |
| 33 | + captureTaskRestarted: vi.fn(), |
| 34 | + captureConversationMessage: vi.fn(), |
| 35 | + }, |
| 36 | + }, |
| 37 | +})) |
| 38 | + |
| 39 | +describe("Task.presentResumableAsk abort reset", () => { |
| 40 | + let mockProvider: any |
| 41 | + let mockApiConfiguration: ProviderSettings |
| 42 | + let task: Task |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + vi.clearAllMocks() |
| 46 | + |
| 47 | + mockProvider = { |
| 48 | + context: { |
| 49 | + globalStorageUri: { fsPath: "/test/path" }, |
| 50 | + }, |
| 51 | + getState: vi.fn().mockResolvedValue({ mode: "code" }), |
| 52 | + postStateToWebview: vi.fn().mockResolvedValue(undefined), |
| 53 | + postMessageToWebview: vi.fn().mockResolvedValue(undefined), |
| 54 | + updateTaskHistory: vi.fn().mockResolvedValue(undefined), |
| 55 | + log: vi.fn(), |
| 56 | + } |
| 57 | + |
| 58 | + mockApiConfiguration = { |
| 59 | + apiProvider: "anthropic", |
| 60 | + apiKey: "test-key", |
| 61 | + } as ProviderSettings |
| 62 | + |
| 63 | + task = new Task({ |
| 64 | + provider: mockProvider as ClineProvider, |
| 65 | + apiConfiguration: mockApiConfiguration, |
| 66 | + startTask: false, |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + // Ensure we don't leave event listeners dangling |
| 72 | + task.dispose() |
| 73 | + }) |
| 74 | + |
| 75 | + it("resets abort flags and continues the loop on yesButtonClicked", async () => { |
| 76 | + // Arrange aborted state |
| 77 | + task.abort = true |
| 78 | + task.abortReason = "user_cancelled" |
| 79 | + task.didFinishAbortingStream = true |
| 80 | + task.isStreaming = true |
| 81 | + |
| 82 | + // minimal message history |
| 83 | + task.clineMessages = [{ ts: Date.now() - 1000, type: "say", say: "text", text: "prev" } as any] |
| 84 | + |
| 85 | + // Spy and stub ask + loop |
| 86 | + const askSpy = vi.spyOn(task as any, "ask").mockResolvedValue({ response: "yesButtonClicked" }) |
| 87 | + const loopSpy = vi.spyOn(task as any, "initiateTaskLoop").mockResolvedValue(undefined) |
| 88 | + |
| 89 | + // Act |
| 90 | + await task.presentResumableAsk() |
| 91 | + |
| 92 | + // Assert ask was presented |
| 93 | + expect(askSpy).toHaveBeenCalled() |
| 94 | + |
| 95 | + // Abort flags cleared |
| 96 | + expect(task.abort).toBe(false) |
| 97 | + expect(task.abandoned).toBe(false) |
| 98 | + expect(task.abortReason).toBeUndefined() |
| 99 | + expect(task.didFinishAbortingStream).toBe(false) |
| 100 | + expect(task.isStreaming).toBe(false) |
| 101 | + |
| 102 | + // Streaming-local state cleared |
| 103 | + expect(task.currentStreamingContentIndex).toBe(0) |
| 104 | + expect(task.assistantMessageContent).toEqual([]) |
| 105 | + expect(task.userMessageContentReady).toBe(false) |
| 106 | + expect(task.didRejectTool).toBe(false) |
| 107 | + expect(task.presentAssistantMessageLocked).toBe(false) |
| 108 | + |
| 109 | + // Loop resumed |
| 110 | + expect(loopSpy).toHaveBeenCalledTimes(1) |
| 111 | + }) |
| 112 | + |
| 113 | + it("includes user feedback when resuming with messageResponse", async () => { |
| 114 | + task.abort = true |
| 115 | + task.clineMessages = [{ ts: Date.now() - 1000, type: "say", say: "text", text: "prev" } as any] |
| 116 | + |
| 117 | + const askSpy = vi |
| 118 | + .spyOn(task as any, "ask") |
| 119 | + .mockResolvedValue({ response: "messageResponse", text: "Continue with this", images: undefined }) |
| 120 | + const saySpy = vi.spyOn(task, "say").mockResolvedValue(undefined as any) |
| 121 | + const loopSpy = vi.spyOn(task as any, "initiateTaskLoop").mockResolvedValue(undefined) |
| 122 | + |
| 123 | + await task.presentResumableAsk() |
| 124 | + |
| 125 | + expect(askSpy).toHaveBeenCalled() |
| 126 | + expect(saySpy).toHaveBeenCalledWith("user_feedback", "Continue with this", undefined) |
| 127 | + expect(loopSpy).toHaveBeenCalledTimes(1) |
| 128 | + }) |
| 129 | + |
| 130 | + it("does nothing when user clicks Terminate (noButtonClicked)", async () => { |
| 131 | + task.abort = true |
| 132 | + task.abortReason = "user_cancelled" |
| 133 | + task.clineMessages = [{ ts: Date.now() - 1000, type: "say", say: "text", text: "prev" } as any] |
| 134 | + |
| 135 | + vi.spyOn(task as any, "ask").mockResolvedValue({ response: "noButtonClicked" }) |
| 136 | + const loopSpy = vi.spyOn(task as any, "initiateTaskLoop").mockResolvedValue(undefined) |
| 137 | + |
| 138 | + await task.presentResumableAsk() |
| 139 | + |
| 140 | + // Still aborted |
| 141 | + expect(task.abort).toBe(true) |
| 142 | + expect(task.abortReason).toBe("user_cancelled") |
| 143 | + // No loop resume |
| 144 | + expect(loopSpy).not.toHaveBeenCalled() |
| 145 | + }) |
| 146 | +}) |
0 commit comments