Skip to content

Commit 8977618

Browse files
committed
passing tests
1 parent 93cc7b4 commit 8977618

File tree

2 files changed

+184
-4
lines changed

2 files changed

+184
-4
lines changed

src/core/webview/__tests__/ClineProvider.spec.ts

Lines changed: 182 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ describe("ClineProvider", () => {
11691169

11701170
test('handles "Just this message" deletion correctly', async () => {
11711171
// Mock user selecting "Just this message"
1172-
;(vscode.window.showInformationMessage as any).mockResolvedValue("confirmation.just_this_message")
1172+
;(vscode.window.showInformationMessage as any).mockResolvedValue("confirmation.delete_just_this_message")
11731173

11741174
// Setup mock messages
11751175
const mockMessages = [
@@ -1224,7 +1224,7 @@ describe("ClineProvider", () => {
12241224

12251225
test('handles "This and all subsequent messages" deletion correctly', async () => {
12261226
// Mock user selecting "This and all subsequent messages"
1227-
;(vscode.window.showInformationMessage as any).mockResolvedValue("confirmation.this_and_subsequent")
1227+
;(vscode.window.showInformationMessage as any).mockResolvedValue("confirmation.delete_this_and_subsequent")
12281228

12291229
// Setup mock messages
12301230
const mockMessages = [
@@ -1287,6 +1287,186 @@ describe("ClineProvider", () => {
12871287
})
12881288
})
12891289

1290+
describe("editMessage", () => {
1291+
beforeEach(async () => {
1292+
// Mock window.showInformationMessage
1293+
;(vscode.window.showInformationMessage as any) = vi.fn()
1294+
await provider.resolveWebviewView(mockWebviewView)
1295+
})
1296+
1297+
test('handles "No, just edit this one" edit correctly', async () => {
1298+
// Mock user selecting "No, just edit this one"
1299+
;(vscode.window.showInformationMessage as any).mockResolvedValue("confirmation.edit_just_this_message")
1300+
1301+
// Setup mock messages
1302+
const mockMessages = [
1303+
{ ts: 1000, type: "say", say: "user_feedback" }, // User message 1
1304+
{ ts: 2000, type: "say", say: "tool" }, // Tool message
1305+
{ ts: 3000, type: "say", say: "text", value: 4000 }, // Message to edit
1306+
{ ts: 4000, type: "say", say: "browser_action" }, // Response to edit
1307+
{ ts: 5000, type: "say", say: "user_feedback" }, // Next user message
1308+
{ ts: 6000, type: "say", say: "user_feedback" }, // Final message
1309+
] as ClineMessage[]
1310+
1311+
const mockApiHistory = [
1312+
{ ts: 1000 },
1313+
{ ts: 2000 },
1314+
{ ts: 3000 },
1315+
{ ts: 4000 },
1316+
{ ts: 5000 },
1317+
{ ts: 6000 },
1318+
] as (Anthropic.MessageParam & { ts?: number })[]
1319+
1320+
// Setup Task instance with auto-mock from the top of the file
1321+
const mockCline = new Task(defaultTaskOptions) // Create a new mocked instance
1322+
mockCline.clineMessages = mockMessages // Set test-specific messages
1323+
mockCline.apiConversationHistory = mockApiHistory // Set API history
1324+
1325+
// Explicitly mock the overwrite methods since they're not being called in the tests
1326+
mockCline.overwriteClineMessages = vi.fn()
1327+
mockCline.overwriteApiConversationHistory = vi.fn()
1328+
mockCline.handleWebviewAskResponse = vi.fn()
1329+
1330+
await provider.addClineToStack(mockCline) // Add the mocked instance to the stack
1331+
1332+
// Mock getTaskWithId
1333+
;(provider as any).getTaskWithId = vi.fn().mockResolvedValue({
1334+
historyItem: { id: "test-task-id" },
1335+
})
1336+
1337+
// Trigger message edit
1338+
// Get the message handler function that was registered with the webview
1339+
const messageHandler = (mockWebviewView.webview.onDidReceiveMessage as any).mock.calls[0][0]
1340+
1341+
// Call the message handler with a submitEditedMessage message
1342+
await messageHandler({
1343+
type: "submitEditedMessage",
1344+
value: 4000,
1345+
editedMessageContent: "Edited message content",
1346+
})
1347+
1348+
// Verify correct messages were kept
1349+
expect(mockCline.overwriteClineMessages).toHaveBeenCalledWith([
1350+
mockMessages[0],
1351+
mockMessages[1],
1352+
mockMessages[4],
1353+
mockMessages[5],
1354+
])
1355+
1356+
// Verify correct API messages were kept
1357+
expect(mockCline.overwriteApiConversationHistory).toHaveBeenCalledWith([
1358+
mockApiHistory[0],
1359+
mockApiHistory[1],
1360+
mockApiHistory[4],
1361+
mockApiHistory[5],
1362+
])
1363+
1364+
// Verify handleWebviewAskResponse was called with the edited content
1365+
expect(mockCline.handleWebviewAskResponse).toHaveBeenCalledWith(
1366+
"messageResponse",
1367+
"Edited message content",
1368+
undefined,
1369+
)
1370+
})
1371+
1372+
test('handles "Yes" (edit and delete subsequent) correctly', async () => {
1373+
// Mock user selecting "Yes"
1374+
;(vscode.window.showInformationMessage as any).mockResolvedValue(
1375+
"confirmation.edit_this_and_delete_subsequent",
1376+
)
1377+
1378+
// Setup mock messages
1379+
const mockMessages = [
1380+
{ ts: 1000, type: "say", say: "user_feedback" },
1381+
{ ts: 2000, type: "say", say: "text", value: 3000 }, // Message to edit
1382+
{ ts: 3000, type: "say", say: "user_feedback" },
1383+
{ ts: 4000, type: "say", say: "user_feedback" },
1384+
] as ClineMessage[]
1385+
1386+
const mockApiHistory = [
1387+
{ ts: 1000 },
1388+
{ ts: 2000 },
1389+
{ ts: 3000 },
1390+
{ ts: 4000 },
1391+
] as (Anthropic.MessageParam & {
1392+
ts?: number
1393+
})[]
1394+
1395+
// Setup Cline instance with auto-mock from the top of the file
1396+
const mockCline = new Task(defaultTaskOptions) // Create a new mocked instance
1397+
mockCline.clineMessages = mockMessages
1398+
mockCline.apiConversationHistory = mockApiHistory
1399+
1400+
// Explicitly mock the overwrite methods since they're not being called in the tests
1401+
mockCline.overwriteClineMessages = vi.fn()
1402+
mockCline.overwriteApiConversationHistory = vi.fn()
1403+
mockCline.handleWebviewAskResponse = vi.fn()
1404+
1405+
await provider.addClineToStack(mockCline)
1406+
1407+
// Mock getTaskWithId
1408+
;(provider as any).getTaskWithId = vi.fn().mockResolvedValue({
1409+
historyItem: { id: "test-task-id" },
1410+
})
1411+
1412+
// Trigger message edit
1413+
// Get the message handler function that was registered with the webview
1414+
const messageHandler = (mockWebviewView.webview.onDidReceiveMessage as any).mock.calls[0][0]
1415+
1416+
// Call the message handler with a submitEditedMessage message
1417+
await messageHandler({
1418+
type: "submitEditedMessage",
1419+
value: 3000,
1420+
editedMessageContent: "Edited message content",
1421+
})
1422+
1423+
// Verify only messages before the edited message were kept
1424+
expect(mockCline.overwriteClineMessages).toHaveBeenCalledWith([mockMessages[0]])
1425+
1426+
// Verify only API messages before the edited message were kept
1427+
expect(mockCline.overwriteApiConversationHistory).toHaveBeenCalledWith([mockApiHistory[0]])
1428+
1429+
// Verify handleWebviewAskResponse was called with the edited content
1430+
expect(mockCline.handleWebviewAskResponse).toHaveBeenCalledWith(
1431+
"messageResponse",
1432+
"Edited message content",
1433+
undefined,
1434+
)
1435+
})
1436+
1437+
test("handles Cancel correctly", async () => {
1438+
// Mock user selecting "Cancel"
1439+
;(vscode.window.showInformationMessage as any).mockResolvedValue("Cancel")
1440+
1441+
// Setup Cline instance with auto-mock from the top of the file
1442+
const mockCline = new Task(defaultTaskOptions) // Create a new mocked instance
1443+
mockCline.clineMessages = [{ ts: 1000 }, { ts: 2000 }] as ClineMessage[]
1444+
mockCline.apiConversationHistory = [{ ts: 1000 }, { ts: 2000 }] as (Anthropic.MessageParam & {
1445+
ts?: number
1446+
})[]
1447+
1448+
// Explicitly mock the overwrite methods since they're not being called in the tests
1449+
mockCline.overwriteClineMessages = vi.fn()
1450+
mockCline.overwriteApiConversationHistory = vi.fn()
1451+
mockCline.handleWebviewAskResponse = vi.fn()
1452+
1453+
await provider.addClineToStack(mockCline)
1454+
1455+
// Trigger message edit
1456+
const messageHandler = (mockWebviewView.webview.onDidReceiveMessage as any).mock.calls[0][0]
1457+
await messageHandler({
1458+
type: "submitEditedMessage",
1459+
value: 2000,
1460+
editedMessageContent: "Edited message content",
1461+
})
1462+
1463+
// Verify no messages were edited or deleted
1464+
expect(mockCline.overwriteClineMessages).not.toHaveBeenCalled()
1465+
expect(mockCline.overwriteApiConversationHistory).not.toHaveBeenCalled()
1466+
expect(mockCline.handleWebviewAskResponse).not.toHaveBeenCalled()
1467+
})
1468+
})
1469+
12901470
describe("getSystemPrompt", () => {
12911471
beforeEach(async () => {
12921472
mockPostMessage.mockClear()

src/core/webview/webviewMessageHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const webviewMessageHandler = async (
100100
try {
101101
const { historyItem } = await provider.getTaskWithId(currentCline.taskId)
102102

103-
// Check if user selected the "just this message" option
103+
// Check if user selected the "modify just this message" option
104104
// For delete: options[0], for edit: options[1]
105105
if (
106106
(operation === "delete" && answer === options[0]) ||
@@ -146,7 +146,7 @@ export const webviewMessageHandler = async (
146146
}
147147
}
148148
} else if (
149-
// Check if user selected the "this and subsequent" option
149+
// Check if user selected the "modify this and subsequent" option
150150
// For delete: options[1], for edit: options[0]
151151
(operation === "delete" && answer === options[1]) ||
152152
(operation === "edit" && answer === options[0])

0 commit comments

Comments
 (0)