Repository navigation
Expand file tree
/
Copy pathrewind.e2e.test.ts
More file actions
98 lines (85 loc) · 4.46 KB
/
Copy pathrewind.e2e.test.ts
File metadata and controls
98 lines (85 loc) · 4.46 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { join, resolve } from "node:path";
import { describe, expect, it } from "vitest";
import { approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
const FILE_NAME = "rewind-sdk.txt";
const ORIGINAL_FILE_CONTENT = "Original rewind content";
const PREPARED_FILE_CONTENT = "Prepared rewind content";
const FILE_CONTENT = "SDK rewind content";
function expectSamePath(actual: string, expected: string): void {
const actualPath = resolve(actual);
const expectedPath = resolve(expected);
if (process.platform === "win32") {
expect(actualPath.toLowerCase()).toBe(expectedPath.toLowerCase());
} else {
expect(actualPath).toBe(expectedPath);
}
}
describe("Rewind", async () => {
const { copilotClient: client, workDir } = await createSdkTestContext();
it("should restore tracked file and conversation", async () => {
const filePath = join(workDir, FILE_NAME);
writeFileSync(filePath, ORIGINAL_FILE_CONTENT);
const session = await client.createSession({
model: "claude-sonnet-5",
enableFileChangeTracking: true,
onPermissionRequest: approveAll,
});
try {
const ready = await session.sendAndWait({
prompt: `Use the edit tool to replace the exact contents of ${FILE_NAME} from ${ORIGINAL_FILE_CONTENT} to ${PREPARED_FILE_CONTENT}. After the tool succeeds, reply with exactly SDK_REWIND_READY.`,
});
expect(ready?.data.content).toBe("SDK_REWIND_READY");
expect(readFileSync(filePath, "utf8")).toBe(PREPARED_FILE_CONTENT);
const response = await session.sendAndWait({
prompt: `Use the edit tool to replace the exact contents of ${FILE_NAME} from ${PREPARED_FILE_CONTENT} to ${FILE_CONTENT}. After the tool succeeds, reply with exactly SDK_REWIND_DONE.`,
});
expect(response?.data.content).toBe("SDK_REWIND_DONE");
expect(existsSync(filePath)).toBe(true);
expect(readFileSync(filePath, "utf8")).toBe(FILE_CONTENT);
let rewindPoints = await session.rpc.history.listRewindPoints();
const deadline = Date.now() + 30_000;
while (
Date.now() < deadline &&
(rewindPoints.unavailableReason !== undefined ||
rewindPoints.points.length !== 2 ||
!rewindPoints.points[1]?.turnChangedFiles ||
!rewindPoints.points[1]?.canRestoreFiles)
) {
await new Promise((resolveDelay) => setTimeout(resolveDelay, 100));
rewindPoints = await session.rpc.history.listRewindPoints();
}
expect(rewindPoints.unavailableReason).toBeUndefined();
expect(rewindPoints.fileChangeTrackingEnabled).toBe(true);
expect(rewindPoints.points).toHaveLength(2);
const rewindPoint = rewindPoints.points[1];
expect(rewindPoint.turnChangedFiles).toBe(true);
expect(rewindPoint.canRestoreFiles).toBe(true);
expect(rewindPoint.fileCount).toBe(1);
const preview = await session.rpc.history.previewRewind({
eventId: rewindPoint.eventId,
});
expect(preview.available).toBe(true);
expect(preview.files).toHaveLength(1);
expectSamePath(preview.files[0].path, filePath);
const rewind = await session.rpc.history.rewind({
eventId: rewindPoint.eventId,
mode: "conversation-and-files",
});
expect(rewind.outcome).toBe("success");
expect(rewind.eventsRemoved).toBeGreaterThan(0);
expect(rewind.restoredFiles).toHaveLength(1);
expectSamePath(rewind.restoredFiles[0], filePath);
expect(existsSync(filePath)).toBe(true);
expect(readFileSync(filePath, "utf8")).toBe(PREPARED_FILE_CONTENT);
const events = await session.getEvents();
expect(events.some((event) => event.id === rewindPoint.eventId)).toBe(false);
} finally {
await session.disconnect();
}
});
});