|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest" |
| 2 | +import { webviewMessageHandler } from "../webviewMessageHandler" |
| 3 | +import * as vscode from "vscode" |
| 4 | +import { ClineProvider } from "../ClineProvider" |
| 5 | + |
| 6 | +// Mock the saveTaskMessages function |
| 7 | +vi.mock("../../task-persistence", () => ({ |
| 8 | + saveTaskMessages: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +// Mock the i18n module |
| 12 | +vi.mock("../../../i18n", () => ({ |
| 13 | + t: vi.fn((key: string) => key), |
| 14 | + changeLanguage: vi.fn(), |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock("vscode", () => ({ |
| 18 | + window: { |
| 19 | + showErrorMessage: vi.fn(), |
| 20 | + showWarningMessage: vi.fn(), |
| 21 | + showInformationMessage: vi.fn(), |
| 22 | + }, |
| 23 | + workspace: { |
| 24 | + workspaceFolders: undefined, |
| 25 | + getConfiguration: vi.fn(() => ({ |
| 26 | + get: vi.fn(), |
| 27 | + update: vi.fn(), |
| 28 | + })), |
| 29 | + }, |
| 30 | + ConfigurationTarget: { |
| 31 | + Global: 1, |
| 32 | + Workspace: 2, |
| 33 | + WorkspaceFolder: 3, |
| 34 | + }, |
| 35 | + Uri: { |
| 36 | + parse: vi.fn((str) => ({ toString: () => str })), |
| 37 | + file: vi.fn((path) => ({ fsPath: path })), |
| 38 | + }, |
| 39 | + env: { |
| 40 | + openExternal: vi.fn(), |
| 41 | + clipboard: { |
| 42 | + writeText: vi.fn(), |
| 43 | + }, |
| 44 | + }, |
| 45 | + commands: { |
| 46 | + executeCommand: vi.fn(), |
| 47 | + }, |
| 48 | +})) |
| 49 | + |
| 50 | +describe("webviewMessageHandler delete functionality", () => { |
| 51 | + let provider: any |
| 52 | + let getCurrentTaskMock: any |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + // Reset all mocks |
| 56 | + vi.clearAllMocks() |
| 57 | + |
| 58 | + // Create mock task |
| 59 | + getCurrentTaskMock = { |
| 60 | + clineMessages: [], |
| 61 | + apiConversationHistory: [], |
| 62 | + overwriteClineMessages: vi.fn(async () => {}), |
| 63 | + overwriteApiConversationHistory: vi.fn(async () => {}), |
| 64 | + taskId: "test-task-id", |
| 65 | + } |
| 66 | + |
| 67 | + // Create mock provider |
| 68 | + provider = { |
| 69 | + getCurrentTask: vi.fn(() => getCurrentTaskMock), |
| 70 | + postMessageToWebview: vi.fn(), |
| 71 | + contextProxy: { |
| 72 | + getValue: vi.fn(), |
| 73 | + setValue: vi.fn(async () => {}), |
| 74 | + globalStorageUri: { fsPath: "/test/path" }, |
| 75 | + }, |
| 76 | + log: vi.fn(), |
| 77 | + cwd: "/test/cwd", |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + describe("handleDeleteMessageConfirm", () => { |
| 82 | + it("should handle deletion when apiConversationHistoryIndex is -1 (message not in API history)", async () => { |
| 83 | + // Setup test data with a user message and assistant response |
| 84 | + const userMessageTs = 1000 |
| 85 | + const assistantMessageTs = 1001 |
| 86 | + |
| 87 | + getCurrentTaskMock.clineMessages = [ |
| 88 | + { ts: userMessageTs, say: "user", text: "Hello" }, |
| 89 | + { ts: assistantMessageTs, say: "assistant", text: "Hi there" }, |
| 90 | + ] |
| 91 | + |
| 92 | + // API history has the assistant message but not the user message |
| 93 | + // This simulates the case where the user message wasn't in API history |
| 94 | + getCurrentTaskMock.apiConversationHistory = [ |
| 95 | + { ts: assistantMessageTs, role: "assistant", content: { type: "text", text: "Hi there" } }, |
| 96 | + { |
| 97 | + ts: 1002, |
| 98 | + role: "assistant", |
| 99 | + content: { type: "text", text: "attempt_completion" }, |
| 100 | + name: "attempt_completion", |
| 101 | + }, |
| 102 | + ] |
| 103 | + |
| 104 | + // Call delete for the user message |
| 105 | + await webviewMessageHandler(provider, { |
| 106 | + type: "deleteMessageConfirm", |
| 107 | + messageTs: userMessageTs, |
| 108 | + }) |
| 109 | + |
| 110 | + // Verify that clineMessages was truncated at the correct index |
| 111 | + expect(getCurrentTaskMock.overwriteClineMessages).toHaveBeenCalledWith([]) |
| 112 | + |
| 113 | + // When message is not found in API history (index is -1), |
| 114 | + // API history should be truncated from the first API message at/after the deleted timestamp (fallback) |
| 115 | + expect(getCurrentTaskMock.overwriteApiConversationHistory).toHaveBeenCalledWith([]) |
| 116 | + }) |
| 117 | + |
| 118 | + it("should handle deletion when exact apiConversationHistoryIndex is found", async () => { |
| 119 | + // Setup test data where message exists in both arrays |
| 120 | + const messageTs = 1000 |
| 121 | + |
| 122 | + getCurrentTaskMock.clineMessages = [ |
| 123 | + { ts: 900, say: "user", text: "Previous message" }, |
| 124 | + { ts: messageTs, say: "user", text: "Delete this" }, |
| 125 | + { ts: 1100, say: "assistant", text: "Response" }, |
| 126 | + ] |
| 127 | + |
| 128 | + getCurrentTaskMock.apiConversationHistory = [ |
| 129 | + { ts: 900, role: "user", content: { type: "text", text: "Previous message" } }, |
| 130 | + { ts: messageTs, role: "user", content: { type: "text", text: "Delete this" } }, |
| 131 | + { ts: 1100, role: "assistant", content: { type: "text", text: "Response" } }, |
| 132 | + ] |
| 133 | + |
| 134 | + // Call delete |
| 135 | + await webviewMessageHandler(provider, { |
| 136 | + type: "deleteMessageConfirm", |
| 137 | + messageTs: messageTs, |
| 138 | + }) |
| 139 | + |
| 140 | + // Verify truncation at correct indices |
| 141 | + expect(getCurrentTaskMock.overwriteClineMessages).toHaveBeenCalledWith([ |
| 142 | + { ts: 900, say: "user", text: "Previous message" }, |
| 143 | + ]) |
| 144 | + |
| 145 | + expect(getCurrentTaskMock.overwriteApiConversationHistory).toHaveBeenCalledWith([ |
| 146 | + { ts: 900, role: "user", content: { type: "text", text: "Previous message" } }, |
| 147 | + ]) |
| 148 | + }) |
| 149 | + |
| 150 | + it("should handle deletion when message not found in clineMessages", async () => { |
| 151 | + getCurrentTaskMock.clineMessages = [{ ts: 1000, say: "user", text: "Some message" }] |
| 152 | + |
| 153 | + getCurrentTaskMock.apiConversationHistory = [] |
| 154 | + |
| 155 | + // Call delete with non-existent timestamp |
| 156 | + await webviewMessageHandler(provider, { |
| 157 | + type: "deleteMessageConfirm", |
| 158 | + messageTs: 9999, |
| 159 | + }) |
| 160 | + |
| 161 | + // Verify error message was shown (expecting translation key since t() is mocked to return the key) |
| 162 | + expect(vscode.window.showErrorMessage).toHaveBeenCalledWith("common:errors.message.message_not_found") |
| 163 | + |
| 164 | + // Verify no truncation occurred |
| 165 | + expect(getCurrentTaskMock.overwriteClineMessages).not.toHaveBeenCalled() |
| 166 | + expect(getCurrentTaskMock.overwriteApiConversationHistory).not.toHaveBeenCalled() |
| 167 | + }) |
| 168 | + |
| 169 | + it("should handle deletion with attempt_completion in API history", async () => { |
| 170 | + // Setup test data with attempt_completion |
| 171 | + const userMessageTs = 1000 |
| 172 | + const attemptCompletionTs = 1001 |
| 173 | + |
| 174 | + getCurrentTaskMock.clineMessages = [ |
| 175 | + { ts: userMessageTs, say: "user", text: "Fix the bug" }, |
| 176 | + { ts: attemptCompletionTs, say: "assistant", text: "I've fixed the bug" }, |
| 177 | + ] |
| 178 | + |
| 179 | + // API history has attempt_completion but user message is missing |
| 180 | + getCurrentTaskMock.apiConversationHistory = [ |
| 181 | + { |
| 182 | + ts: attemptCompletionTs, |
| 183 | + role: "assistant", |
| 184 | + content: { |
| 185 | + type: "text", |
| 186 | + text: "I've fixed the bug in the code", |
| 187 | + }, |
| 188 | + name: "attempt_completion", |
| 189 | + }, |
| 190 | + { |
| 191 | + ts: 1002, |
| 192 | + role: "user", |
| 193 | + content: { type: "text", text: "Looks good, but..." }, |
| 194 | + }, |
| 195 | + ] |
| 196 | + |
| 197 | + // Call delete for the user message |
| 198 | + await webviewMessageHandler(provider, { |
| 199 | + type: "deleteMessageConfirm", |
| 200 | + messageTs: userMessageTs, |
| 201 | + }) |
| 202 | + |
| 203 | + // Verify that clineMessages was truncated |
| 204 | + expect(getCurrentTaskMock.overwriteClineMessages).toHaveBeenCalledWith([]) |
| 205 | + |
| 206 | + // API history should be truncated from first message at/after deleted timestamp (fallback) |
| 207 | + expect(getCurrentTaskMock.overwriteApiConversationHistory).toHaveBeenCalledWith([]) |
| 208 | + }) |
| 209 | + |
| 210 | + it("should preserve messages before the deleted one", async () => { |
| 211 | + const messageTs = 2000 |
| 212 | + |
| 213 | + getCurrentTaskMock.clineMessages = [ |
| 214 | + { ts: 1000, say: "user", text: "First message" }, |
| 215 | + { ts: 1500, say: "assistant", text: "First response" }, |
| 216 | + { ts: messageTs, say: "user", text: "Delete this" }, |
| 217 | + { ts: 2500, say: "assistant", text: "Response to delete" }, |
| 218 | + ] |
| 219 | + |
| 220 | + getCurrentTaskMock.apiConversationHistory = [ |
| 221 | + { ts: 1000, role: "user", content: { type: "text", text: "First message" } }, |
| 222 | + { ts: 1500, role: "assistant", content: { type: "text", text: "First response" } }, |
| 223 | + { ts: messageTs, role: "user", content: { type: "text", text: "Delete this" } }, |
| 224 | + { ts: 2500, role: "assistant", content: { type: "text", text: "Response to delete" } }, |
| 225 | + ] |
| 226 | + |
| 227 | + await webviewMessageHandler(provider, { |
| 228 | + type: "deleteMessageConfirm", |
| 229 | + messageTs: messageTs, |
| 230 | + }) |
| 231 | + |
| 232 | + // Should preserve messages before the deleted one |
| 233 | + expect(getCurrentTaskMock.overwriteClineMessages).toHaveBeenCalledWith([ |
| 234 | + { ts: 1000, say: "user", text: "First message" }, |
| 235 | + { ts: 1500, say: "assistant", text: "First response" }, |
| 236 | + ]) |
| 237 | + |
| 238 | + // API history should be truncated at the exact index |
| 239 | + expect(getCurrentTaskMock.overwriteApiConversationHistory).toHaveBeenCalledWith([ |
| 240 | + { ts: 1000, role: "user", content: { type: "text", text: "First message" } }, |
| 241 | + { ts: 1500, role: "assistant", content: { type: "text", text: "First response" } }, |
| 242 | + ]) |
| 243 | + }) |
| 244 | + }) |
| 245 | +}) |
0 commit comments