Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodePath from "node:path";
3
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { it } from "@effect/vitest";
6import { ThreadId, type VcsError } from "@v12/contracts";
7import * as Effect from "effect/Effect";
8import * as FileSystem from "effect/FileSystem";
9import * as Layer from "effect/Layer";
10import * as PlatformError from "effect/PlatformError";
11import * as Scope from "effect/Scope";
12import { describe, expect } from "vite-plus/test";
13
14import { checkpointRefForThreadTurn } from "./Utils.ts";
15import * as CheckpointStore from "./CheckpointStore.ts";
16import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
17import * as VcsProcess from "../vcs/VcsProcess.ts";
18import * as ServerConfig from "../config.ts";
19
20const ServerConfigLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
21 prefix: "v12-checkpoint-store-test-",
22});
23const VcsProcessTestLayer = VcsProcess.layer.pipe(Layer.provide(NodeServices.layer));
24const VcsDriverTestLayer = VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcessTestLayer));
25const CheckpointStoreTestLayer = CheckpointStore.layer.pipe(
26 Layer.provideMerge(VcsDriverTestLayer),
27 Layer.provideMerge(NodeServices.layer),
28);
29const TestLayer = CheckpointStoreTestLayer.pipe(
30 Layer.provideMerge(VcsProcessTestLayer),
31 Layer.provideMerge(VcsDriverTestLayer),
32 Layer.provideMerge(ServerConfigLayer),
33 Layer.provideMerge(NodeServices.layer),
34);
35
36function makeTmpDir(
37 prefix = "checkpoint-store-test-",
38): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> {
39 return Effect.gen(function* () {
40 const fileSystem = yield* FileSystem.FileSystem;
41 return yield* fileSystem.makeTempDirectoryScoped({ prefix });
42 });
43}
44
45function writeTextFile(
46 filePath: string,
47 contents: string,
48): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
49 return Effect.gen(function* () {
50 const fileSystem = yield* FileSystem.FileSystem;
51 yield* fileSystem.writeFileString(filePath, contents);
52 });
53}
54
55function git(
56 cwd: string,
57 args: ReadonlyArray<string>,
58): Effect.Effect<string, VcsError, VcsProcess.VcsProcess> {
59 return Effect.gen(function* () {
60 const process = yield* VcsProcess.VcsProcess;
61 const result = yield* process.run({
62 operation: "CheckpointStore.test.git",
63 command: "git",
64 cwd,
65 args,
66 timeoutMs: 10_000,
67 });
68 return result.stdout.trim();
69 });
70}
71
72function initRepoWithCommit(
73 cwd: string,
74): Effect.Effect<
75 void,
76 VcsError | PlatformError.PlatformError,
77 VcsProcess.VcsProcess | FileSystem.FileSystem
78> {
79 return Effect.gen(function* () {
80 yield* git(cwd, ["init"]);
81 yield* git(cwd, ["config", "user.email", "test@test.com"]);
82 yield* git(cwd, ["config", "user.name", "Test"]);
83 yield* writeTextFile(NodePath.join(cwd, "README.md"), "# test\n");
84 yield* git(cwd, ["add", "."]);
85 yield* git(cwd, ["commit", "-m", "initial commit"]);
86 });
87}
88
89function buildLargeText(lineCount = 5_000): string {
90 return Array.from({ length: lineCount }, (_, index) => `line ${String(index).padStart(5, "0")}`)
91 .join("\n")
92 .concat("\n");
93}
94
95it.layer(TestLayer)("CheckpointStore.layer", (it) => {
96 describe("isGitRepository", () => {
97 it.effect("returns false when no Git repository is detected", () =>
98 Effect.gen(function* () {
99 const tmp = yield* makeTmpDir();
100 const checkpointStore = yield* CheckpointStore.CheckpointStore;
101
102 expect(yield* checkpointStore.isGitRepository(tmp)).toBe(false);
103 }),
104 );
105
106 it.effect("returns true when a Git repository is detected", () =>
107 Effect.gen(function* () {
108 const tmp = yield* makeTmpDir();
109 yield* initRepoWithCommit(tmp);
110 const checkpointStore = yield* CheckpointStore.CheckpointStore;
111
112 expect(yield* checkpointStore.isGitRepository(tmp)).toBe(true);
113 }),
114 );
115 });
116
117 describe("diffCheckpoints", () => {
118 it.effect("returns full oversized checkpoint diffs without truncation", () =>
119 Effect.gen(function* () {
120 const tmp = yield* makeTmpDir();
121 yield* initRepoWithCommit(tmp);
122 const checkpointStore = yield* CheckpointStore.CheckpointStore;
123 const threadId = ThreadId.make("thread-checkpoint-store");
124 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
125 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
126
127 yield* checkpointStore.captureCheckpoint({
128 cwd: tmp,
129 checkpointRef: fromCheckpointRef,
130 });
131 yield* writeTextFile(NodePath.join(tmp, "README.md"), buildLargeText());
132 yield* checkpointStore.captureCheckpoint({
133 cwd: tmp,
134 checkpointRef: toCheckpointRef,
135 });
136
137 const diff = yield* checkpointStore.diffCheckpoints({
138 cwd: tmp,
139 fromCheckpointRef,
140 toCheckpointRef,
141 ignoreWhitespace: true,
142 });
143
144 expect(diff).toContain("diff --git");
145 expect(diff).not.toContain("[truncated]");
146 expect(diff).toContain("+line 04999");
147 }),
148 );
149
150 it.effect("can hide indentation churn when changes wrap existing lines", () =>
151 Effect.gen(function* () {
152 const tmp = yield* makeTmpDir();
153 yield* initRepoWithCommit(tmp);
154 const checkpointStore = yield* CheckpointStore.CheckpointStore;
155 const threadId = ThreadId.make("thread-checkpoint-store-whitespace");
156 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
157 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
158
159 const componentPath = NodePath.join(tmp, "Component.tsx");
160 yield* writeTextFile(
161 componentPath,
162 [
163 "export function View() {",
164 " return (",
165 " <section>",
166 " <h1>Title</h1>",
167 " <p>Body</p>",
168 " </section>",
169 " );",
170 "}",
171 "",
172 ].join("\n"),
173 );
174 yield* checkpointStore.captureCheckpoint({
175 cwd: tmp,
176 checkpointRef: fromCheckpointRef,
177 });
178 yield* writeTextFile(
179 componentPath,
180 [
181 "export function View() {",
182 " return (",
183 " <section>",
184 " {isReady ? (",
185 " <div>",
186 " <h1>Title</h1>",
187 " <p>Body</p>",
188 " </div>",
189 " ) : null}",
190 " </section>",
191 " );",
192 "}",
193 "",
194 ].join("\n"),
195 );
196 yield* checkpointStore.captureCheckpoint({
197 cwd: tmp,
198 checkpointRef: toCheckpointRef,
199 });
200
201 const normalDiff = yield* checkpointStore.diffCheckpoints({
202 cwd: tmp,
203 fromCheckpointRef,
204 toCheckpointRef,
205 ignoreWhitespace: false,
206 });
207 const whitespaceIgnoredDiff = yield* checkpointStore.diffCheckpoints({
208 cwd: tmp,
209 fromCheckpointRef,
210 toCheckpointRef,
211 ignoreWhitespace: true,
212 });
213
214 expect(normalDiff).toContain("diff --git");
215 expect(normalDiff).toContain("- <h1>Title</h1>");
216 expect(normalDiff).toContain("+ <h1>Title</h1>");
217 expect(whitespaceIgnoredDiff).toContain("diff --git");
218 expect(whitespaceIgnoredDiff).toContain("+ {isReady ? (");
219 expect(whitespaceIgnoredDiff).toContain("+ <div>");
220 expect(whitespaceIgnoredDiff).not.toContain("- <h1>Title</h1>");
221 expect(whitespaceIgnoredDiff).not.toContain("+ <h1>Title</h1>");
222 }),
223 );
224 });
225});