-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathcheckpoint.test.ts
More file actions
432 lines (363 loc) · 13.2 KB
/
checkpoint.test.ts
File metadata and controls
432 lines (363 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"
import { Task } from "../../task/Task"
import { ClineProvider } from "../../webview/ClineProvider"
import { checkpointSave, checkpointRestore, checkpointDiff, getCheckpointService } from "../index"
import * as vscode from "vscode"
// Mock vscode
vi.mock("vscode", () => ({
window: {
showErrorMessage: vi.fn(),
createTextEditorDecorationType: vi.fn(() => ({})),
showInformationMessage: vi.fn(),
},
Uri: {
file: vi.fn((path: string) => ({ fsPath: path })),
parse: vi.fn((uri: string) => ({ with: vi.fn(() => ({})) })),
},
commands: {
executeCommand: vi.fn(),
},
}))
// Mock other dependencies
vi.mock("@roo-code/telemetry", () => ({
TelemetryService: {
instance: {
captureCheckpointCreated: vi.fn(),
captureCheckpointRestored: vi.fn(),
captureCheckpointDiffed: vi.fn(),
},
},
}))
vi.mock("../../../utils/path", () => ({
getWorkspacePath: vi.fn(() => "/test/workspace"),
}))
vi.mock("../../../services/checkpoints")
describe("Checkpoint functionality", () => {
let mockProvider: any
let mockTask: any
let mockCheckpointService: any
beforeEach(async () => {
// Create mock checkpoint service
mockCheckpointService = {
isInitialized: true,
saveCheckpoint: vi.fn().mockResolvedValue({ commit: "test-commit-hash" }),
restoreCheckpoint: vi.fn().mockResolvedValue(undefined),
getDiff: vi.fn().mockResolvedValue([]),
on: vi.fn(),
initShadowGit: vi.fn().mockResolvedValue(undefined),
}
// Create mock provider
mockProvider = {
context: {
globalStorageUri: { fsPath: "/test/storage" },
},
log: vi.fn(),
postMessageToWebview: vi.fn(),
postStateToWebview: vi.fn(),
cancelTask: vi.fn(),
}
// Create mock task
mockTask = {
taskId: "test-task-id",
enableCheckpoints: true,
checkpointService: mockCheckpointService,
checkpointServiceInitializing: false,
providerRef: {
deref: () => mockProvider,
},
clineMessages: [],
apiConversationHistory: [],
pendingUserMessageCheckpoint: undefined,
say: vi.fn().mockResolvedValue(undefined),
overwriteClineMessages: vi.fn(),
overwriteApiConversationHistory: vi.fn(),
combineMessages: vi.fn().mockReturnValue([]),
}
// Update the mock to return our mockCheckpointService
const checkpointsModule = await import("../../../services/checkpoints")
vi.mocked(checkpointsModule.RepoPerTaskCheckpointService.create).mockReturnValue(mockCheckpointService)
})
afterEach(() => {
vi.clearAllMocks()
})
describe("checkpointSave", () => {
it("should wait for checkpoint service initialization before saving", async () => {
// Set up task with uninitialized service
mockCheckpointService.isInitialized = false
mockTask.checkpointService = mockCheckpointService
// Simulate service initialization after a delay
setTimeout(() => {
mockCheckpointService.isInitialized = true
}, 100)
// Call checkpointSave
const savePromise = checkpointSave(mockTask, true)
// Wait for the save to complete
const result = await savePromise
// saveCheckpoint should have been called
expect(mockCheckpointService.saveCheckpoint).toHaveBeenCalledWith(
expect.stringContaining("Task: test-task-id"),
{ allowEmpty: true, suppressMessage: false },
)
// Result should contain the commit hash
expect(result).toEqual({ commit: "test-commit-hash" })
// Task should still have checkpoints enabled
expect(mockTask.enableCheckpoints).toBe(true)
})
it("should handle timeout when service doesn't initialize", async () => {
// Service never initializes
mockCheckpointService.isInitialized = false
// Call checkpointSave with a task that has no checkpoint service
const taskWithNoService = {
...mockTask,
checkpointService: undefined,
enableCheckpoints: false,
}
const result = await checkpointSave(taskWithNoService, true)
// Result should be undefined
expect(result).toBeUndefined()
// saveCheckpoint should not have been called
expect(mockCheckpointService.saveCheckpoint).not.toHaveBeenCalled()
})
it("should preserve checkpoint data through message deletion flow", async () => {
// Initialize service
mockCheckpointService.isInitialized = true
mockTask.checkpointService = mockCheckpointService
// Simulate saving checkpoint before user message
const checkpointResult = await checkpointSave(mockTask, true)
expect(checkpointResult).toEqual({ commit: "test-commit-hash" })
// Simulate setting pendingUserMessageCheckpoint
if (checkpointResult && "commit" in checkpointResult) {
mockTask.pendingUserMessageCheckpoint = {
hash: checkpointResult.commit,
timestamp: Date.now(),
type: "user_message",
}
}
// Verify checkpoint data is preserved
expect(mockTask.pendingUserMessageCheckpoint).toBeDefined()
expect(mockTask.pendingUserMessageCheckpoint.hash).toBe("test-commit-hash")
// Simulate message deletion and reinitialization
mockTask.clineMessages = []
mockTask.checkpointService = mockCheckpointService // Keep service available
mockTask.checkpointServiceInitializing = false
// Save checkpoint again after deletion
const newCheckpointResult = await checkpointSave(mockTask, true)
// Should still work after reinitialization
expect(newCheckpointResult).toEqual({ commit: "test-commit-hash" })
expect(mockTask.enableCheckpoints).toBe(true)
})
it("should handle errors gracefully and disable checkpoints", async () => {
mockCheckpointService.saveCheckpoint.mockRejectedValue(new Error("Save failed"))
const result = await checkpointSave(mockTask)
expect(result).toBeUndefined()
expect(mockTask.enableCheckpoints).toBe(false)
})
})
describe("checkpointRestore", () => {
beforeEach(() => {
mockTask.clineMessages = [
{ ts: 1, say: "user", text: "Message 1" },
{ ts: 2, say: "assistant", text: "Message 2" },
{ ts: 3, say: "user", text: "Message 3" },
]
mockTask.apiConversationHistory = [
{ ts: 1, role: "user", content: [{ type: "text", text: "Message 1" }] },
{ ts: 2, role: "assistant", content: [{ type: "text", text: "Message 2" }] },
{ ts: 3, role: "user", content: [{ type: "text", text: "Message 3" }] },
]
})
it("should restore checkpoint for delete operation", async () => {
await checkpointRestore(mockTask, {
ts: 2,
commitHash: "abc123",
mode: "restore",
operation: "delete",
})
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith("abc123")
expect(mockTask.overwriteApiConversationHistory).toHaveBeenCalledWith([
{ ts: 1, role: "user", content: [{ type: "text", text: "Message 1" }] },
])
expect(mockTask.overwriteClineMessages).toHaveBeenCalledWith([{ ts: 1, say: "user", text: "Message 1" }])
expect(mockProvider.cancelTask).toHaveBeenCalled()
})
it("should restore checkpoint for edit operation", async () => {
await checkpointRestore(mockTask, {
ts: 2,
commitHash: "abc123",
mode: "restore",
operation: "edit",
})
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith("abc123")
expect(mockTask.overwriteApiConversationHistory).toHaveBeenCalledWith([
{ ts: 1, role: "user", content: [{ type: "text", text: "Message 1" }] },
])
// For edit operation, should include the message being edited
expect(mockTask.overwriteClineMessages).toHaveBeenCalledWith([
{ ts: 1, say: "user", text: "Message 1" },
{ ts: 2, say: "assistant", text: "Message 2" },
])
expect(mockProvider.cancelTask).toHaveBeenCalled()
})
it("should handle preview mode without modifying messages", async () => {
await checkpointRestore(mockTask, {
ts: 2,
commitHash: "abc123",
mode: "preview",
})
expect(mockCheckpointService.restoreCheckpoint).toHaveBeenCalledWith("abc123")
expect(mockTask.overwriteApiConversationHistory).not.toHaveBeenCalled()
expect(mockTask.overwriteClineMessages).not.toHaveBeenCalled()
expect(mockProvider.cancelTask).toHaveBeenCalled()
})
it("should handle missing message gracefully", async () => {
await checkpointRestore(mockTask, {
ts: 999, // Non-existent timestamp
commitHash: "abc123",
mode: "restore",
})
expect(mockCheckpointService.restoreCheckpoint).not.toHaveBeenCalled()
})
it("should disable checkpoints on error", async () => {
mockCheckpointService.restoreCheckpoint.mockRejectedValue(new Error("Restore failed"))
await checkpointRestore(mockTask, {
ts: 2,
commitHash: "abc123",
mode: "restore",
})
expect(mockTask.enableCheckpoints).toBe(false)
expect(mockProvider.log).toHaveBeenCalledWith("[checkpointRestore] disabling checkpoints for this task")
})
})
describe("checkpointDiff", () => {
beforeEach(() => {
mockTask.clineMessages = [
{ ts: 1, say: "user", text: "Message 1" },
{ ts: 2, say: "checkpoint_saved", text: "commit1" },
{ ts: 3, say: "user", text: "Message 2" },
{ ts: 4, say: "checkpoint_saved", text: "commit2" },
]
})
it("should show diff for full mode", async () => {
const mockChanges = [
{
paths: { absolute: "/test/file.ts", relative: "file.ts" },
content: { before: "old content", after: "new content" },
},
]
mockCheckpointService.getDiff.mockResolvedValue(mockChanges)
await checkpointDiff(mockTask, {
ts: 4,
commitHash: "commit2",
mode: "full",
})
expect(mockCheckpointService.getDiff).toHaveBeenCalledWith({
from: "commit2",
to: undefined,
})
expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
"vscode.changes",
"Changes since task started",
expect.any(Array),
)
})
it("should show diff for checkpoint mode with next commit", async () => {
const mockChanges = [
{
paths: { absolute: "/test/file.ts", relative: "file.ts" },
content: { before: "old content", after: "new content" },
},
]
mockCheckpointService.getDiff.mockResolvedValue(mockChanges)
await checkpointDiff(mockTask, {
ts: 4,
commitHash: "commit1",
mode: "checkpoint",
})
expect(mockCheckpointService.getDiff).toHaveBeenCalledWith({
from: "commit1",
to: "commit2",
})
expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
"vscode.changes",
"Changes compare with next checkpoint",
expect.any(Array),
)
})
it("should find next checkpoint automatically in checkpoint mode", async () => {
const mockChanges = [
{
paths: { absolute: "/test/file.ts", relative: "file.ts" },
content: { before: "old content", after: "new content" },
},
]
mockCheckpointService.getDiff.mockResolvedValue(mockChanges)
await checkpointDiff(mockTask, {
ts: 4,
commitHash: "commit1",
mode: "checkpoint",
})
expect(mockCheckpointService.getDiff).toHaveBeenCalledWith({
from: "commit1", // Should find the next checkpoint
to: "commit2",
})
})
it("should show information message when no changes found", async () => {
mockCheckpointService.getDiff.mockResolvedValue([])
await checkpointDiff(mockTask, {
ts: 4,
commitHash: "commit2",
mode: "full",
})
expect(vscode.window.showInformationMessage).toHaveBeenCalledWith("No changes found.")
expect(vscode.commands.executeCommand).not.toHaveBeenCalled()
})
it("should disable checkpoints on error", async () => {
mockCheckpointService.getDiff.mockRejectedValue(new Error("Diff failed"))
await checkpointDiff(mockTask, {
ts: 4,
commitHash: "commit2",
mode: "full",
})
expect(mockTask.enableCheckpoints).toBe(false)
expect(mockProvider.log).toHaveBeenCalledWith("[checkpointDiff] disabling checkpoints for this task")
})
})
describe("getCheckpointService", () => {
it("should return existing service if available", async () => {
const service = await getCheckpointService(mockTask)
expect(service).toBe(mockCheckpointService)
})
it("should return undefined if checkpoints are disabled", async () => {
mockTask.enableCheckpoints = false
const service = await getCheckpointService(mockTask)
expect(service).toBeUndefined()
})
it("should return undefined if service is still initializing", async () => {
mockTask.checkpointService = undefined
mockTask.checkpointServiceInitializing = true
const service = await getCheckpointService(mockTask)
expect(service).toBeUndefined()
})
it("should create new service if none exists", async () => {
mockTask.checkpointService = undefined
mockTask.checkpointServiceInitializing = false
const service = getCheckpointService(mockTask)
const checkpointsModule = await import("../../../services/checkpoints")
expect(vi.mocked(checkpointsModule.RepoPerTaskCheckpointService.create)).toHaveBeenCalledWith({
taskId: "test-task-id",
workspaceDir: "/test/workspace",
shadowDir: "/test/storage",
log: expect.any(Function),
})
})
it("should disable checkpoints if workspace path is not found", async () => {
const pathModule = await import("../../../utils/path")
vi.mocked(pathModule.getWorkspacePath).mockReturnValue(null as any)
mockTask.checkpointService = undefined
mockTask.checkpointServiceInitializing = false
const service = await getCheckpointService(mockTask)
expect(service).toBeUndefined()
expect(mockTask.enableCheckpoints).toBe(false)
})
})
})