Open History to see the last commit
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
68it.effect("restores empty checkpoints without changing paths outside the workspace", () =>
69 Effect.gen(function* () {
70 const fileSystem = yield* FileSystem.FileSystem;
71 const path = yield* Path.Path;
72 const driver = yield* GitVcsDriver.makeVcsDriverShape();
73 for (const nested of [false, true]) {
74 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-empty-checkpoint-" });
75 yield* runGit(root, ["init"]);
76 yield* runGit(root, ["config", "user.email", "test@test.com"]);
77 yield* runGit(root, ["config", "user.name", "Test"]);
78 if (nested) {
79 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "original\n");
80 yield* runGit(root, ["add", "."]);
81 }
82 yield* runGit(root, ["commit", "--allow-empty", "-m", "initial"]);
83 const cwd = nested ? path.join(root, "nested") : root;
84 yield* fileSystem.makeDirectory(cwd, { recursive: true });
85 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/empty");
86 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
87 if (nested) {
88 yield* fileSystem.writeFileString(path.join(root, "outside.txt"), "changed\n");
89 yield* runGit(root, ["add", "outside.txt"]);
90 }
91 for (const staged of [false, true]) {
92 const addedPath = path.join(cwd, "added.txt");
93 yield* fileSystem.writeFileString(addedPath, "new\n");
94 if (staged) yield* runGit(cwd, ["add", "added.txt"]);
95 assert.isTrue(
96 yield* driver.checkpoints.restoreCheckpoint({
97 cwd,
98 checkpointRef,
99 fallbackToHead: false,
100 }),
101 );
102 // Restores never delete untracked files; staged additions are index
103 // state and return to the checkpoint's tracked set.
104 assert.strictEqual(yield* fileSystem.exists(addedPath), !staged);
105 }
106 yield* fileSystem.writeFileString(
107 path.join(root, ".git", "info", "exclude"),
108 "ignored.txt\n",
109 );
110 yield* fileSystem.writeFileString(path.join(cwd, "ignored.txt"), "keep\n");
111 yield* fileSystem.makeDirectory(path.join(cwd, "untracked"));
112 yield* fileSystem.writeFileString(path.join(cwd, "untracked", "file.txt"), "remove\n");
113 assert.isTrue(
114 yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
115 );
116 assert.strictEqual(yield* fileSystem.readFileString(path.join(cwd, "ignored.txt")), "keep\n");
117 assert.isTrue(yield* fileSystem.exists(path.join(cwd, "untracked")));
118 if (nested) {
119 assert.strictEqual(
120 yield* fileSystem.readFileString(path.join(root, "outside.txt")),
121 "changed\n",
122 );
123 const staged = yield* driver.execute({
124 operation: "test",
125 cwd: root,
126 args: ["diff", "--cached", "--name-only"],
127 });
128 assert.strictEqual(staged.stdout.trim(), "outside.txt");
129 }
130 }
131 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
132);
133
134it.effect("recreates a nested workspace directory emptied by restore before resetting", () =>
135 Effect.gen(function* () {
136 const fileSystem = yield* FileSystem.FileSystem;
137 const path = yield* Path.Path;
138 const driver = yield* GitVcsDriver.makeVcsDriverShape();
139 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-nested-restore-" });
140 yield* runGit(root, ["init"]);
141 yield* runGit(root, ["config", "user.email", "test@test.com"]);
142 yield* runGit(root, ["config", "user.name", "Test"]);
143 yield* fileSystem.writeFileString(path.join(root, "root.txt"), "root\n");
144 yield* runGit(root, ["add", "."]);
145 yield* runGit(root, ["commit", "-m", "initial"]);
146
147 const cwd = path.join(root, "nested");
148 yield* fileSystem.makeDirectory(cwd, { recursive: true });
149 const checkpointRef = CheckpointRef.make("refs/t3/checkpoints/nested-empty");
150 yield* driver.checkpoints.captureCheckpoint({ cwd, checkpointRef });
151
152 // Tracking the nested workspace's only file and restoring to a checkpoint
153 // without it removes the directory itself; the driver must recreate it
154 // before the follow-up reset runs.
155 yield* fileSystem.writeFileString(path.join(cwd, "only.txt"), "new\n");
156 yield* runGit(root, ["add", "nested/only.txt"]);
157
158 assert.isTrue(
159 yield* driver.checkpoints.restoreCheckpoint({ cwd, checkpointRef, fallbackToHead: false }),
160 );
161 assert.isTrue(yield* fileSystem.exists(cwd));
162 assert.isFalse(yield* fileSystem.exists(path.join(cwd, "only.txt")));
163 }).pipe(Effect.scoped, Effect.provide(GitContractLayer)),
164);
165
166it.effect("GitVcsDriver forwards execute env to the VCS process", () => {
167 let observedEnv: NodeJS.ProcessEnv | undefined;
168 let observedAppendTruncationMarker: boolean | undefined;
169 let observedOutputMode: VcsProcess.VcsProcessInput["outputMode"];
170
171 return Effect.gen(function* () {
172 const driver = yield* GitVcsDriver.makeVcsDriverShape();
173
174 yield* driver.execute({
175 operation: "GitVcsDriver.test.env",
176 cwd: "/repo",
177 args: ["status"],
178 env: {
179 GIT_INDEX_FILE: "/tmp/t3-index",
180 },
181 appendTruncationMarker: true,
182 outputMode: "error",
183 });
184
185 assert.deepStrictEqual(observedEnv, {
186 GIT_INDEX_FILE: "/tmp/t3-index",
187 });
188 assert.strictEqual(observedAppendTruncationMarker, true);
189 assert.strictEqual(observedOutputMode, "error");
190 }).pipe(
191 Effect.provide(
192 Layer.mergeAll(
193 NodeServices.layer,
194 Layer.mock(VcsProcess.VcsProcess)({
195 run: (input) =>
196 Effect.sync(() => {
197 observedEnv = input.env;
198 observedAppendTruncationMarker = input.appendTruncationMarker;
199 observedOutputMode = input.outputMode;
200 return {
201 exitCode: ChildProcessSpawner.ExitCode(0),
202 stdout: "",
203 stderr: "",
204 stdoutTruncated: false,
205 stderrTruncated: false,
206 };
207 }),
208 }),
209 ),
210 ),
211 );
212});