Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitVcsDriver.test.ts

415 lines18.4 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Path from "effect/Path";
6import * as PlatformError from "effect/PlatformError";
7import { ChildProcessSpawner } from "effect/unstable/process";
8import { assert, it } from "@effect/vitest";
9​
10import { CheckpointRef, GitCommandError } from "@t3tools/contracts";
11import * as ServerConfig from "../config.ts";
12import * as GitVcsDriver from "./GitVcsDriver.ts";
13import * as VcsProcess from "./VcsProcess.ts";
14import { runVcsDriverContractSuite } from "./testing/VcsDriverContractHarness.ts";
15​
16const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
17 prefix: "t3-git-vcs-contract-",
18});
19const GitContractLayer = Layer.mergeAll(GitVcsDriver.vcsLayer, GitVcsDriver.layer).pipe(
20 Layer.provide(ServerConfigLayer),
21 Layer.provideMerge(VcsProcess.layer),
22 Layer.provideMerge(NodeServices.layer),
23);
24​
25const runGit = (cwd: string, args: ReadonlyArray<string>) =>
26 Effect.gen(function* () {
27 const driver = yield* GitVcsDriver.GitVcsDriver;
28 yield* driver.execute({
29 operation: "GitVcsDriver.contract.git",
30 cwd,
31 args,
32 timeoutMs: 10_000,
33 });
34 });
35​
36type GitContractError = GitCommandError | PlatformError.PlatformError;
37​
38runVcsDriverContractSuite<GitVcsDriver.GitVcsDriver, GitContractError>({
39 name: "Git",
40 kind: "git",
41 layer: GitContractLayer,
42 fixture: {
43 createRepo: (cwd) =>
44 Effect.gen(function* () {
45 yield* runGit(cwd, ["init"]);
46 yield* runGit(cwd, ["config", "user.email", "test@test.com"]);
47 yield* runGit(cwd, ["config", "user.name", "Test"]);
48 }),
49 writeFile: (cwd, relativePath, contents) =>
50 Effect.gen(function* () {
51 const fileSystem = yield* FileSystem.FileSystem;
52 const path = yield* Path.Path;
53 const absolutePath = path.join(cwd, relativePath);
54 yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
55 yield* fileSystem.writeFileString(absolutePath, contents);
56 }),
57 trackFile: (cwd, relativePath) => runGit(cwd, ["add", relativePath]),
58 commit: (cwd, message) => runGit(cwd, ["commit", "-m", message]),
59 ignorePath: (cwd, pattern) =>
60 Effect.gen(function* () {
61 const fileSystem = yield* FileSystem.FileSystem;
62 const path = yield* Path.Path;
63 yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`);
64 }),
65 },
66});
67​
68const makeCheckpointFixture = Effect.fn("makeCheckpointFixture")(function* (
69 driver: Effect.Success<ReturnType<typeof GitVcsDriver.makeVcsDriverShape>>,
70 cwd: string,
71) {
72 const fileSystem = yield* FileSystem.FileSystem;
73 const path = yield* Path.Path;
74 const git = (args: ReadonlyArray<string>) =>
75 driver.execute({ operation: "checkpoint-test", cwd, args });
76 yield* git(["init"]);
77 yield* git(["config", "user.name", "Test"]);
78 yield* git(["config", "user.email", "test@test.com"]);
79 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "initial\n");
80 yield* git(["add", "."]);
81 yield* git(["commit", "-m", "initial"]);
82 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/test");
83 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "staged\n");
84 yield* git(["add", "."]);
85 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "unstaged\n");
86 return { git, checkpointRef };
87});
88​
89it.effect("checkpoint capture does not rerun clean filters for unchanged indexed files", () =>
90 Effect.gen(function* () {
91 const fileSystem = yield* FileSystem.FileSystem;
92 const path = yield* Path.Path;
93 const driver = yield* GitVcsDriver.makeVcsDriverShape();
94 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-cache-" });
95 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
96 yield* fileSystem.writeFileString(
97 path.join(cwd, ".gitattributes"),
98 "stable.txt filter=probe\n",
99 );
100 yield* fileSystem.writeFileString(path.join(cwd, "stable.txt"), "unchanged\n");
101 yield* fileSystem.writeFileString(
102 path.join(cwd, ".git", "filter.cjs"),
103 'require("node:fs").appendFileSync(".git/filter-runs", "read\\n"); process.stdin.pipe(process.stdout);',
104 );
105 yield* git(["config", "filter.probe.clean", "node .git/filter.cjs"]);
106 yield* fileSystem.utimes(path.join(cwd, "stable.txt"), 1_700_000_000, 1_700_000_000);
107 yield* git(["add", "."]);
108 yield* git(["commit", "-m", "record stable file"]);
109 yield* fileSystem.writeFileString(path.join(cwd, ".git", "filter-runs"), "");
110 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "changed\n");
111 const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
112​
113 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
114​
115 assert.strictEqual(yield* fileSystem.readFileString(path.join(cwd, ".git", "filter-runs")), "");
116 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "changed\n");
117 assert.strictEqual((yield* git(["show", `${checkpointRef}:stable.txt`])).stdout, "unchanged\n");
118 assert.deepEqual(yield* fileSystem.readFile(path.join(cwd, ".git", "index")), originalIndex);
119 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
120);
121​
122for (const timestamp of [1_700_000_000, 1_700_000_000.9999]) {
123 it.effect(
124 `checkpoint capture preserves same-size edits with racy index timestamps (${timestamp})`,
125 () =>
126 Effect.gen(function* () {
127 const fileSystem = yield* FileSystem.FileSystem;
128 const path = yield* Path.Path;
129 const driver = yield* GitVcsDriver.makeVcsDriverShape();
130 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-racy-" });
131 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
132 const filePath = path.join(cwd, "file.txt");
133 const indexPath = path.join(cwd, ".git", "index");
134 yield* git(["config", "core.trustctime", "false"]);
135 yield* fileSystem.writeFileString(filePath, "before\n");
136 yield* fileSystem.utimes(filePath, timestamp, timestamp);
137 yield* git(["add", "file.txt"]);
138 yield* git(["commit", "-m", "record racy file"]);
139 yield* fileSystem.utimes(indexPath, timestamp, timestamp);
140 const originalIndex = yield* fileSystem.readFile(indexPath);
141 const originalIndexMtime = (yield* fileSystem.stat(indexPath)).mtime;
142 yield* fileSystem.writeFileString(filePath, "after!\n");
143 yield* fileSystem.utimes(filePath, timestamp, timestamp);
144​
145 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
146​
147 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "after!\n");
148 assert.deepEqual(yield* fileSystem.readFile(indexPath), originalIndex);
149 assert.deepEqual((yield* fileSystem.stat(indexPath)).mtime, originalIndexMtime);
150 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
151 );
153​
154it.effect("checkpoint capture preserves racy edits made after resetting the index", () =>
155 Effect.gen(function* () {
156 const fileSystem = yield* FileSystem.FileSystem;
157 const path = yield* Path.Path;
158 const liveProcess = yield* VcsProcess.VcsProcess;
159 const driver = yield* GitVcsDriver.makeVcsDriverShape();
160 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-racy-reset-" });
161 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
162 const racyPath = path.join(cwd, "racy.txt");
163 const indexPath = path.join(cwd, ".git", "index");
164 const timestamp = 1_700_000_000;
165 yield* git(["config", "core.trustctime", "false"]);
166 yield* fileSystem.writeFileString(racyPath, "before\n");
167 yield* fileSystem.utimes(racyPath, timestamp, timestamp);
168 yield* git(["add", "."]);
169 yield* git(["commit", "-m", "record racy file"]);
170 yield* fileSystem.writeFileString(path.join(cwd, "file.txt"), "staged\n");
171 yield* git(["add", "file.txt"]);
172 yield* fileSystem.utimes(indexPath, timestamp, timestamp);
173 const originalIndex = yield* fileSystem.readFile(indexPath);
174 const originalIndexMtime = (yield* fileSystem.stat(indexPath)).mtime;
175 const captureDriver = yield* GitVcsDriver.makeVcsDriverShape().pipe(
176 Effect.provideService(VcsProcess.VcsProcess, {
177 run: Effect.fn(function* (input: VcsProcess.VcsProcessInput) {
178 const result = yield* liveProcess.run(input);
179 if (input.args.includes("read-tree") && input.args.includes("--reset")) {
180 yield* fileSystem.writeFileString(racyPath, "after!\n").pipe(Effect.orDie);
181 yield* fileSystem.utimes(racyPath, timestamp, timestamp).pipe(Effect.orDie);
182 }
183 return result;
184 }),
185 }),
186 );
187​
188 yield* captureDriver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
189​
190 assert.strictEqual((yield* git(["show", `${checkpointRef}:racy.txt`])).stdout, "after!\n");
191 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "staged\n");
192 assert.deepEqual(yield* fileSystem.readFile(indexPath), originalIndex);
193 assert.deepEqual((yield* fileSystem.stat(indexPath)).mtime, originalIndexMtime);
194 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
195);
196​
197for (const nested of [false, true]) {
198 for (const indexMode of ["normal", "flags", "split"] as const) {
199 it.effect(
200 `checkpoint index reuse preserves two turns (nested=${nested}, index=${indexMode})`,
201 () =>
202 Effect.gen(function* () {
203 const fileSystem = yield* FileSystem.FileSystem;
204 const path = yield* Path.Path;
205 const driver = yield* GitVcsDriver.makeVcsDriverShape();
206 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-turns-" });
207 const { git } = yield* makeCheckpointFixture(driver, cwd);
208 const write = (name: string, contents: string) =>
209 fileSystem.writeFileString(path.join(cwd, name), contents);
210 yield* fileSystem.makeDirectory(path.join(cwd, "scope"));
211 for (const name of [
212 "scope/staged",
213 "scope/deleted",
214 "scope/assumed",
215 "scope/skipped",
216 "outside",
217 ]) {
218 yield* write(name, "original\n");
219 }
220 yield* git(["add", "."]);
221 yield* git(["commit", "-m", "initial scoped files"]);
222 yield* write("scope/staged", "staged\n");
223 yield* write("scope/new-deleted", "staged then deleted\n");
224 yield* write("outside", "staged outside\n");
225 yield* git(["add", "."]);
226 if (indexMode === "flags") {
227 yield* git(["update-index", "--assume-unchanged", "scope/assumed"]);
228 yield* git(["update-index", "--skip-worktree", "scope/skipped"]);
229 }
230 if (indexMode === "split") {
231 yield* git(["update-index", "--split-index"]);
232 }
233 const originalIndex = yield* fileSystem.readFile(path.join(cwd, ".git", "index"));
234 for (const name of ["scope/staged", "scope/assumed", "scope/skipped", "outside"]) {
235 yield* write(name, "working\n");
236 }
237 yield* write("scope/new", "first\n");
238 yield* fileSystem.remove(path.join(cwd, "scope/deleted"));
239 yield* fileSystem.remove(path.join(cwd, "scope/new-deleted"));
240 const captureCwd = nested ? path.join(cwd, "scope") : cwd;
241 const first = CheckpointRef.make("refs/t3/checkpoints/turns/1");
242 const second = CheckpointRef.make("refs/t3/checkpoints/turns/2");
243 yield* driver.checkpoints.captureCheckpoint({ cwd: captureCwd, checkpointRef: first });
244 for (const name of ["scope/staged", "scope/assumed", "scope/skipped"]) {
245 assert.strictEqual((yield* git(["show", `${first}:${name}`])).stdout, "working\n");
246 }
247 assert.strictEqual(
248 (yield* git(["show", `${first}:outside`])).stdout,
249 nested ? "original\n" : "working\n",
250 );
251 const files = (yield* git(["ls-tree", "-r", "--name-only", first])).stdout.split("\n");
252 assert.notInclude(files, "scope/deleted");
253 assert.notInclude(files, "scope/new-deleted");
254 assert.include(files, "scope/new");
255​
256 yield* write("scope/staged", "second\n");
257 yield* fileSystem.remove(path.join(cwd, "scope/new"));
258 yield* write("scope/second", "added in second turn\n");
259 yield* driver.checkpoints.captureCheckpoint({ cwd: captureCwd, checkpointRef: second });
260 assert.strictEqual(
261 (yield* git(["diff", "--name-only", first, second])).stdout,
262 "scope/new\nscope/second\nscope/staged\n",
263 );
264 assert.strictEqual((yield* git(["show", `${second}:scope/staged`])).stdout, "second\n");
265 assert.strictEqual(
266 (yield* git(["show", `${second}:scope/second`])).stdout,
267 "added in second turn\n",
268 );
269 assert.deepEqual(
270 yield* fileSystem.readFile(path.join(cwd, ".git", "index")),
271 originalIndex,
272 );
273 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
274 );
275 }
277​
278for (const indexState of ["missing", "invalid"] as const) {
279 it.effect(`checkpoint capture falls back when the user index is ${indexState}`, () =>
280 Effect.gen(function* () {
281 const fileSystem = yield* FileSystem.FileSystem;
282 const path = yield* Path.Path;
283 const driver = yield* GitVcsDriver.makeVcsDriverShape();
284 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-checkpoint-index-" });
285 const { git, checkpointRef } = yield* makeCheckpointFixture(driver, cwd);
286 const indexPath = path.join(cwd, ".git", "index");
287 if (indexState === "missing") {
288 yield* fileSystem.remove(indexPath);
289 } else {
290 yield* fileSystem.writeFileString(indexPath, "invalid index");
291 }
292​
293 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
294​
295 assert.strictEqual((yield* git(["show", `${checkpointRef}:file.txt`])).stdout, "unstaged\n");
296 if (indexState === "missing") {
297 assert.isFalse(yield* fileSystem.exists(indexPath));
298 } else {
299 assert.strictEqual(yield* fileSystem.readFileString(indexPath), "invalid index");
300 }
301 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
302 );
304​
305it.effect("restores empty checkpoints without changing paths outside the workspace", () =>
306 Effect.gen(function* () {
307 const fileSystem = yield* FileSystem.FileSystem;
308 const path = yield* Path.Path;
309 const driver = yield* GitVcsDriver.makeVcsDriverShape();
310 for (const nested of [false, true]) {
311 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-empty-checkpoint-" });
312 yield* runGit(root, ["init"]);
313 yield* runGit(root, ["config", "user.email", "test@test.com"]);
314 yield* runGit(root, ["config", "user.name", "Test"]);
315 if (nested) {
316 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "original\n");
317 yield* runGit(root, ["add", "."]);
318 }
319 yield* runGit(root, ["commit", "--allow-empty", "-m", "initial"]);
320 const cwd = nested ? path.join(root, "nested") : root;
321 yield* fileSystem.makeDirectory(cwd, { recursive: true });
322 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/empty");
323 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
324 if (nested) {
325 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "changed\n");
326 yield* runGit(root, ["add", "outside.txt"]);
327 }
328 for (const staged of [false, true]) {
329 const addedPath = path.join(cwd, "added.txt");
330 yield* fileSystem.writeFileString(addedPath, "new\n");
331 if (staged) yield* runGit(cwd, ["add", "added.txt"]);
332 assert.isTrue(
333 yield* driver.checkpoints.restoreCheckpoint({
334 cwd,
335 checkpointRef,
336 fallbackToHead: false,
337 }),
338 );
339 assert.isFalse(yield* fileSystem.exists(addedPath));
340 }
341 yield* fileSystem.writeFileString(
342 path.join(root, ".git", "info", "exclude"),
343 "ignored.txt\n",
344 );
345 yield* fileSystem.writeFileString(path.join(cwd, "ignored.txt"), "keep\n");
346 yield* fileSystem.makeDirectory(path.join(cwd, "untracked"));
347 yield* fileSystem.writeFileString(path.join(cwd, "untracked", "file.txt"), "remove\n");
348 assert.isTrue(
349 yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
350 );
351 assert.strictEqual(yield* fileSystem.readFileString(path.join(cwd, "ignored.txt")), "keep\n");
352 assert.isFalse(yield* fileSystem.exists(path.join(cwd, "untracked")));
353 if (nested) {
354 assert.strictEqual(
355 yield* fileSystem.readFileString(path.join(root, "outside.txt")),
356 "changed\n",
357 );
358 const staged = yield* driver.execute({
359 operation: "test",
360 cwd: root,
361 args: ["diff", "--cached", "--name-only"],
362 });
363 assert.strictEqual(staged.stdout.trim(), "outside.txt");
364 }
365 }
366 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
367);
368​
369it.effect("GitVcsDriver forwards execute env to the VCS process", () => {
370 let observedEnv: NodeJS.ProcessEnv | undefined;
371 let observedAppendTruncationMarker: boolean | undefined;
372 let observedOutputMode: VcsProcess.VcsProcessInput["outputMode"];
373​
374 return Effect.gen(function* () {
375 const driver = yield* GitVcsDriver.makeVcsDriverShape();
376​
377 yield* driver.execute({
378 operation: "GitVcsDriver.test.env",
379 cwd: "/repo",
380 args: ["status"],
381 env: {
382 GIT_INDEX_FILE: "/tmp/t3-index",
383 },
384 appendTruncationMarker: true,
385 outputMode: "error",
386 });
387​
388 assert.deepStrictEqual(observedEnv, {
389 GIT_INDEX_FILE: "/tmp/t3-index",
390 });
391 assert.strictEqual(observedAppendTruncationMarker, true);
392 assert.strictEqual(observedOutputMode, "error");
393 }).pipe(
394 Effect.provide(
395 Layer.mergeAll(
396 NodeServices.layer,
397 Layer.mock(VcsProcess.VcsProcess)({
398 run: (input) =>
399 Effect.sync(() => {
400 observedEnv = input.env;
401 observedAppendTruncationMarker = input.appendTruncationMarker;
402 observedOutputMode = input.outputMode;
403 return {
404 exitCode: ChildProcessSpawner.ExitCode(0),
405 stdout: "",
406 stderr: "",
407 stdoutTruncated: false,
408 stderrTruncated: false,
409 };
410 }),
411 }),
412 ),
413 ),
414 );
415});