@@ -1,238 +0,0 @@
1-import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2-import { mkdirSync, rmSync, writeFileSync } from "node:fs";
3-import { tmpdir } from "node:os";
4-import { join } from "node:path";
5-import {
6- deleteSnapshot,
7- loadSnapshot,
8- saveSnapshot,
9-} from "../session/db/index.ts";
10-import { restoreSnapshot, snapshotBeforeEdit } from "./snapshot.ts";
11-
12-// ─── Helpers ──────────────────────────────────────────────────────────────────
13-
14-function spawnSync(args: string[], cwd: string): void {
15- const proc = Bun.spawnSync(args, { cwd, stdout: "pipe", stderr: "pipe" });
16- if (proc.exitCode !== 0) {
17- throw new Error(
18- `Command failed: ${args.join(" ")}\n${proc.stderr.toString()}`,
19- );
20- }
21-}
22-
23-/** Create a minimal git repo in a temp directory and return its path. */
24-function makeRepo(): string {
25- const dir = join(
26- tmpdir(),
27- `mc-snap-test-${Date.now()}-${Math.random().toString(36).slice(2)}`,
28- );
29- mkdirSync(dir, { recursive: true });
30- spawnSync(["git", "init"], dir);
31- spawnSync(["git", "config", "user.email", "test@example.com"], dir);
32- spawnSync(["git", "config", "user.name", "Test"], dir);
33- // Initial commit so HEAD exists
34- writeFileSync(join(dir, "README.md"), "hello\n");
35- spawnSync(["git", "add", "README.md"], dir);
36- spawnSync(["git", "commit", "-m", "init"], dir);
37- return dir;
38-}
39-
40-function cleanRepo(dir: string): void {
41- rmSync(dir, { recursive: true, force: true });
42-}
43-
44-function useTestRepo() {
45- const state = { repoDir: "", sessionId: crypto.randomUUID() };
46- beforeEach(() => {
47- state.repoDir = makeRepo();
48- });
49- afterEach(() => {
50- cleanRepo(state.repoDir);
51- });
52- return state;
53-}
54-
55-// ─── db helpers ───────────────────────────────────────────────────────────────
56-
57-describe("saveSnapshot / loadSnapshot / deleteSnapshot", () => {
58- test("round-trips file entries", () => {
59- const sessionId = crypto.randomUUID();
60- const turnIndex = 42;
61- const content = new TextEncoder().encode("hello world");
62-
63- saveSnapshot(sessionId, turnIndex, [
64- { path: "src/foo.ts", content, existed: true },
65- { path: "src/new.ts", content: null, existed: false },
66- ]);
67-
68- const rows = loadSnapshot(sessionId, turnIndex);
69- expect(rows).toHaveLength(2);
70-
71- const foo = rows.find((r) => r.path === "src/foo.ts");
72- expect(foo?.existed).toBe(true);
73- expect(foo?.content).toEqual(content);
74-
75- const newFile = rows.find((r) => r.path === "src/new.ts");
76- expect(newFile?.existed).toBe(false);
77- expect(newFile?.content).toBeNull();
78-
79- // Cleanup
80- deleteSnapshot(sessionId, turnIndex);
81- expect(loadSnapshot(sessionId, turnIndex)).toHaveLength(0);
82- });
83-});
84-
85-// ─── snapshotBeforeEdit ───────────────────────────────────────────────────────
86-
87-describe("snapshotBeforeEdit", () => {
88- const state = useTestRepo();
89-
90- test("snapshots a modified tracked file before edit", async () => {
91- const filePath = join(state.repoDir, "README.md");
92- const snappedPaths = new Set<string>();
93-
94- await snapshotBeforeEdit(
95- state.repoDir,
96- state.sessionId,
97- 3,
98- filePath,
99- snappedPaths,
100- );
101-
102- const rows = loadSnapshot(state.sessionId, 3);
103- const readme = rows.find((r) => r.path === "README.md");
104- expect(readme).toBeDefined();
105- expect(readme?.existed).toBe(true);
106- expect(new TextDecoder().decode(readme?.content ?? undefined)).toBe(
107- "hello\n",
108- );
109- expect(snappedPaths.has("README.md")).toBe(true);
110-
111- deleteSnapshot(state.sessionId, 3);
112- });
113-
114- test("snapshots an untracked new file with existed=false", async () => {
115- const filePath = join(state.repoDir, "untracked.txt");
116- const snappedPaths = new Set<string>();
117-
118- await snapshotBeforeEdit(
119- state.repoDir,
120- state.sessionId,
121- 4,
122- filePath,
123- snappedPaths,
124- );
125-
126- const rows = loadSnapshot(state.sessionId, 4);
127- const f = rows.find((r) => r.path === "untracked.txt");
128- expect(f).toBeDefined();
129- // File didn't exist before edit
130- expect(f?.existed).toBe(false);
131- expect(f?.content).toBeNull();
132-
133- deleteSnapshot(state.sessionId, 4);
134- });
135-
136- test("does not snapshot the same file twice in one turn", async () => {
137- const filePath = join(state.repoDir, "README.md");
138- const snappedPaths = new Set<string>();
139-
140- await snapshotBeforeEdit(
141- state.repoDir,
142- state.sessionId,
143- 5,
144- filePath,
145- snappedPaths,
146- );
147-
148- // Change the file
149- writeFileSync(filePath, "changed\n");
150-
151- // Second call should be a no-op
152- await snapshotBeforeEdit(
153- state.repoDir,
154- state.sessionId,
155- 5,
156- filePath,
157- snappedPaths,
158- );
159-
160- const rows = loadSnapshot(state.sessionId, 5);
161- const readme = rows.find((r) => r.path === "README.md");
162- expect(new TextDecoder().decode(readme?.content ?? undefined)).toBe(
163- "hello\n",
164- );
165-
166- deleteSnapshot(state.sessionId, 5);
167- });
168-});
169-
170-// ─── restoreSnapshot ──────────────────────────────────────────────────────────
171-
172-describe("restoreSnapshot", () => {
173- const state = useTestRepo();
174-
175- test("returns not-found when no snapshot exists", async () => {
176- const result = await restoreSnapshot(state.repoDir, state.sessionId, 99);
177- expect(result.restored).toBe(false);
178- if (!result.restored) expect(result.reason).toBe("not-found");
179- });
180-
181- test("restores a modified file to its pre-turn content", async () => {
182- const filePath = join(state.repoDir, "README.md");
183- const originalContent = "hello\n";
184- saveSnapshot(state.sessionId, 10, [
185- {
186- path: "README.md",
187- content: new TextEncoder().encode(originalContent),
188- existed: true,
189- },
190- ]);
191-
192- writeFileSync(filePath, "agent changed this\n");
193-
194- const result = await restoreSnapshot(state.repoDir, state.sessionId, 10);
195- expect(result.restored).toBe(true);
196-
197- const restoredContent = await Bun.file(filePath).text();
198- expect(restoredContent).toBe(originalContent);
199-
200- expect(loadSnapshot(state.sessionId, 10)).toHaveLength(0);
201- });
202-
203- test("deletes a file that was created by the agent (existed=false)", async () => {
204- const newFile = join(state.repoDir, "agent-created.ts");
205- writeFileSync(newFile, "export const x = 1;\n");
206-
207- saveSnapshot(state.sessionId, 11, [
208- { path: "agent-created.ts", content: null, existed: false },
209- ]);
210-
211- const result = await restoreSnapshot(state.repoDir, state.sessionId, 11);
212- expect(result.restored).toBe(true);
213-
214- const exists = await Bun.file(newFile).exists();
215- expect(exists).toBe(false);
216-
217- expect(loadSnapshot(state.sessionId, 11)).toHaveLength(0);
218- });
219-
220- test("end-to-end: snapshotBeforeEdit then agent creates file, restoreSnapshot deletes it", async () => {
221- const agentFile = join(state.repoDir, "generated.ts");
222- const snappedPaths = new Set<string>();
223-
224- await snapshotBeforeEdit(
225- state.repoDir,
226- state.sessionId,
227- 13,
228- agentFile,
229- snappedPaths,
230- );
231-
232- writeFileSync(agentFile, "export const y = 2;\n");
233-
234- const result = await restoreSnapshot(state.repoDir, state.sessionId, 13);
235- expect(result.restored).toBe(true);
236- expect(await Bun.file(agentFile).exists()).toBe(false);
237- });
238-});