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 "@t3tools/contracts";
7import * as HostProcess from "@t3tools/shared/HostProcess";
8import * as Effect from "effect/Effect";
9import * as FileSystem from "effect/FileSystem";
10import * as Layer from "effect/Layer";
11import * as PlatformError from "effect/PlatformError";
12import * as Scope from "effect/Scope";
13import { describe, expect } from "vite-plus/test";
14
15import { checkpointRefForThreadTurn } from "./Utils.ts";
16import { parseTurnDiffFilesFromNumstat } from "./Diffs.ts";
17import * as CheckpointStore from "./CheckpointStore.ts";
18import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
19import * as VcsProcess from "../vcs/VcsProcess.ts";
20import * as ServerConfig from "../config.ts";
21
22const layerServerConfig = ServerConfig.ServerConfig.layerTest(process.cwd(), {
23 prefix: "t3-checkpoint-store-test-",
24});
25const layerVcsProcessTest = VcsProcess.layer.pipe(Layer.provide(NodeServices.layer));
26const layerVcsDriverTest = VcsDriverRegistry.layer.pipe(Layer.provide(layerVcsProcessTest));
27const layerCheckpointStoreTest = CheckpointStore.layer.pipe(
28 Layer.provideMerge(layerVcsDriverTest),
29 Layer.provideMerge(NodeServices.layer),
30);
31const layerTest = layerCheckpointStoreTest.pipe(
32 Layer.provideMerge(layerVcsProcessTest),
33 Layer.provideMerge(layerVcsDriverTest),
34 Layer.provideMerge(layerServerConfig),
35 Layer.provideMerge(NodeServices.layer),
36);
37
38function makeTmpDir(
39 prefix = "checkpoint-store-test-",
40): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> {
41 return Effect.gen(function* () {
42 const fileSystem = yield* FileSystem.FileSystem;
43 return yield* fileSystem.makeTempDirectoryScoped({ prefix });
44 });
45}
46
47function writeTextFile(
48 filePath: string,
49 contents: string,
50): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
51 return Effect.gen(function* () {
52 const fileSystem = yield* FileSystem.FileSystem;
53 yield* fileSystem.writeFileString(filePath, contents);
54 });
55}
56
57function git(
58 cwd: string,
59 args: ReadonlyArray<string>,
60 options: { readonly committedAt?: string; readonly allowNonZeroExit?: boolean } = {},
61): Effect.Effect<string, VcsError, VcsProcess.VcsProcess> {
62 return Effect.gen(function* () {
63 const vcsProcess = yield* VcsProcess.VcsProcess;
64 const result = yield* vcsProcess.run({
65 operation: "CheckpointStore.test.git",
66 command: "git",
67 cwd,
68 args,
69 timeoutMs: 10_000,
70 ...(options.committedAt === undefined
71 ? {}
72 : { env: { ...process.env, GIT_COMMITTER_DATE: options.committedAt } }),
73 ...(options.allowNonZeroExit === undefined
74 ? {}
75 : { allowNonZeroExit: options.allowNonZeroExit }),
76 });
77 return result.stdout.trim();
78 });
79}
80
81function initRepoWithCommit(
82 cwd: string,
83): Effect.Effect<
84 void,
85 VcsError | PlatformError.PlatformError,
86 VcsProcess.VcsProcess | FileSystem.FileSystem
87> {
88 return Effect.gen(function* () {
89 yield* git(cwd, ["init"]);
90 yield* git(cwd, ["config", "user.email", "test@test.com"]);
91 yield* git(cwd, ["config", "user.name", "Test"]);
92 yield* writeTextFile(NodePath.join(cwd, "README.md"), "# test\n");
93 yield* git(cwd, ["add", "."]);
94 yield* git(cwd, ["commit", "-m", "initial commit"]);
95 });
96}
97
98function buildLargeText(lineCount = 5_000): string {
99 return Array.from({ length: lineCount }, (_, index) => `line ${String(index).padStart(5, "0")}`)
100 .join("\n")
101 .concat("\n");
102}
103
104it.layer(layerTest)("CheckpointStore.layer", (it) => {
105 it.effect("detects a nested workspace without its own .git entry", () =>
106 Effect.gen(function* () {
107 const tmp = yield* makeTmpDir();
108 yield* initRepoWithCommit(tmp);
109 const fileSystem = yield* FileSystem.FileSystem;
110 const nested = NodePath.join(tmp, "packages", "nested");
111 yield* fileSystem.makeDirectory(nested, { recursive: true });
112 const checkpointStore = yield* CheckpointStore.CheckpointStore;
113 expect(yield* checkpointStore.isCheckpointable(nested)).toBe(true);
114 }),
115 );
116 describe("multi-repo workspaces", () => {
117 const makeWorkspace = Effect.gen(function* () {
118 const fileSystem = yield* FileSystem.FileSystem;
119 const workspace = yield* makeTmpDir("checkpoint-store-workspace-");
120 for (const name of ["api", "web"]) {
121 const repo = NodePath.join(workspace, name);
122 yield* fileSystem.makeDirectory(repo);
123 yield* initRepoWithCommit(repo);
124 }
125 yield* writeTextFile(NodePath.join(workspace, "NOTES.md"), "not in any repository\n");
126 return workspace;
127 });
128
129 it.effect("captures, diffs and restores every repository as one workspace", () =>
130 Effect.gen(function* () {
131 const workspace = yield* makeWorkspace;
132 const fileSystem = yield* FileSystem.FileSystem;
133 const checkpointStore = yield* CheckpointStore.CheckpointStore;
134 const threadId = ThreadId.make("thread-multi-repo");
135 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
136 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
137
138 expect(yield* checkpointStore.isCheckpointable(workspace)).toBe(true);
139 yield* checkpointStore.captureCheckpoint({
140 cwd: workspace,
141 checkpointRef: fromCheckpointRef,
142 });
143 yield* writeTextFile(NodePath.join(workspace, "api", "README.md"), "# api\n");
144 yield* writeTextFile(NodePath.join(workspace, "web", "app.ts"), "export {};\n");
145 yield* checkpointStore.captureCheckpoint({
146 cwd: workspace,
147 checkpointRef: toCheckpointRef,
148 });
149
150 const numstat = yield* checkpointStore.diffCheckpoints({
151 cwd: workspace,
152 fromCheckpointRef,
153 toCheckpointRef,
154 ignoreWhitespace: false,
155 format: "numstat",
156 });
157 expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([
158 { path: "api/README.md", additions: 1, deletions: 1 },
159 { path: "web/app.ts", additions: 1, deletions: 0 },
160 ]);
161 const patch = yield* checkpointStore.diffCheckpoints({
162 cwd: workspace,
163 fromCheckpointRef,
164 toCheckpointRef,
165 ignoreWhitespace: false,
166 });
167 expect(patch).toContain("diff --git a/api/README.md b/api/README.md");
168 expect(patch).toContain("+++ b/web/app.ts");
169
170 expect(
171 yield* checkpointStore.restoreCheckpoint({
172 cwd: workspace,
173 checkpointRef: fromCheckpointRef,
174 }),
175 ).toBe(true);
176 expect(yield* fileSystem.readFileString(NodePath.join(workspace, "api", "README.md"))).toBe(
177 "# test\n",
178 );
179 expect(yield* fileSystem.exists(NodePath.join(workspace, "web", "app.ts"))).toBe(false);
180 expect(yield* fileSystem.exists(NodePath.join(workspace, "NOTES.md"))).toBe(true);
181 }),
182 );
183
184 it.effect("restores nothing when a repository is missing the checkpoint", () =>
185 Effect.gen(function* () {
186 const workspace = yield* makeWorkspace;
187 const fileSystem = yield* FileSystem.FileSystem;
188 const checkpointStore = yield* CheckpointStore.CheckpointStore;
189 const threadId = ThreadId.make("thread-multi-repo-late-repo");
190 const checkpointRef = checkpointRefForThreadTurn(threadId, 0);
191
192 yield* checkpointStore.captureCheckpoint({ cwd: workspace, checkpointRef });
193 const late = NodePath.join(workspace, "late");
194 yield* fileSystem.makeDirectory(late);
195 yield* initRepoWithCommit(late);
196 yield* writeTextFile(NodePath.join(workspace, "api", "README.md"), "# edited\n");
197
198 expect(yield* checkpointStore.hasCheckpointRef({ cwd: workspace, checkpointRef })).toBe(
199 false,
200 );
201 expect(yield* checkpointStore.restoreCheckpoint({ cwd: workspace, checkpointRef })).toBe(
202 false,
203 );
204 expect(yield* fileSystem.readFileString(NodePath.join(workspace, "api", "README.md"))).toBe(
205 "# edited\n",
206 );
207 }),
208 );
209 it.effect("fills in a new repository without overwriting existing checkpoints", () =>
210 Effect.gen(function* () {
211 const workspace = yield* makeWorkspace;
212 const fileSystem = yield* FileSystem.FileSystem;
213 const checkpointStore = yield* CheckpointStore.CheckpointStore;
214 const threadId = ThreadId.make("thread-multi-repo-fill-in");
215 const checkpointRef = checkpointRefForThreadTurn(threadId, 0);
216
217 yield* checkpointStore.captureCheckpoint({ cwd: workspace, checkpointRef });
218 const late = NodePath.join(workspace, "late");
219 yield* fileSystem.makeDirectory(late);
220 yield* initRepoWithCommit(late);
221 yield* writeTextFile(NodePath.join(workspace, "api", "README.md"), "# edited\n");
222
223 yield* checkpointStore.captureCheckpoint({
224 cwd: workspace,
225 checkpointRef,
226 missingOnly: true,
227 });
228
229 expect(yield* checkpointStore.hasCheckpointRef({ cwd: workspace, checkpointRef })).toBe(
230 true,
231 );
232 expect(yield* checkpointStore.restoreCheckpoint({ cwd: workspace, checkpointRef })).toBe(
233 true,
234 );
235 expect(yield* fileSystem.readFileString(NodePath.join(workspace, "api", "README.md"))).toBe(
236 "# test\n",
237 );
238 }),
239 );
240
241 it.effect("leaves out what a pull brought into one repository", () =>
242 Effect.gen(function* () {
243 const workspace = yield* makeWorkspace;
244 const checkpointStore = yield* CheckpointStore.CheckpointStore;
245 const threadId = ThreadId.make("thread-multi-repo-authored");
246 const refs = {
247 cwd: workspace,
248 fromCheckpointRef: checkpointRefForThreadTurn(threadId, 0),
249 toCheckpointRef: checkpointRefForThreadTurn(threadId, 1),
250 };
251 const api = NodePath.join(workspace, "api");
252 yield* git(api, ["checkout", "-b", "upstream"]);
253 yield* writeTextFile(NodePath.join(api, "upstream.txt"), "upstream\n");
254 yield* git(api, ["add", "."]);
255 yield* git(api, ["commit", "-m", "upstream"], { committedAt: "2020-01-01T00:00:00Z" });
256 yield* git(api, ["checkout", "-"]);
257 yield* checkpointStore.captureCheckpoint({
258 cwd: workspace,
259 checkpointRef: refs.fromCheckpointRef,
260 });
261
262 // The turn pulls upstream into api and edits web.
263 yield* git(api, ["merge", "--ff-only", "upstream"]);
264 yield* writeTextFile(NodePath.join(workspace, "web", "app.ts"), "export {};\n");
265 yield* checkpointStore.captureCheckpoint({
266 cwd: workspace,
267 checkpointRef: refs.toCheckpointRef,
268 });
269
270 expect(yield* checkpointStore.listAuthoredPaths(refs)).toEqual(new Set(["web/app.ts"]));
271 const numstat = yield* checkpointStore.diffCheckpoints({
272 ...refs,
273 ignoreWhitespace: false,
274 format: "numstat",
275 filePaths: ["web/app.ts"],
276 });
277 expect(parseTurnDiffFilesFromNumstat(numstat).map((file) => file.path)).toEqual([
278 "web/app.ts",
279 ]);
280 }),
281 );
282 });
283
284 describe("diffCheckpoints", () => {
285 it.effect("returns full oversized checkpoint diffs without truncation", () =>
286 Effect.gen(function* () {
287 const tmp = yield* makeTmpDir();
288 yield* initRepoWithCommit(tmp);
289 const checkpointStore = yield* CheckpointStore.CheckpointStore;
290 const threadId = ThreadId.make("thread-checkpoint-store");
291 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
292 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
293
294 yield* checkpointStore.captureCheckpoint({
295 cwd: tmp,
296 checkpointRef: fromCheckpointRef,
297 });
298 yield* writeTextFile(NodePath.join(tmp, "README.md"), buildLargeText());
299 yield* checkpointStore.captureCheckpoint({
300 cwd: tmp,
301 checkpointRef: toCheckpointRef,
302 });
303
304 const diff = yield* checkpointStore.diffCheckpoints({
305 cwd: tmp,
306 fromCheckpointRef,
307 toCheckpointRef,
308 ignoreWhitespace: true,
309 });
310
311 expect(diff).toContain("diff --git");
312 expect(diff).not.toContain("[truncated]");
313 expect(diff).toContain("+line 04999");
314 }),
315 );
316
317 it.effect("keeps a/ and b/ patch prefixes when the repository disables them", () =>
318 Effect.gen(function* () {
319 const tmp = yield* makeTmpDir();
320 yield* initRepoWithCommit(tmp);
321 yield* git(tmp, ["config", "diff.noprefix", "true"]);
322 const checkpointStore = yield* CheckpointStore.CheckpointStore;
323 const threadId = ThreadId.make("thread-checkpoint-store-noprefix");
324 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
325 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
326
327 yield* checkpointStore.captureCheckpoint({
328 cwd: tmp,
329 checkpointRef: fromCheckpointRef,
330 });
331 yield* writeTextFile(NodePath.join(tmp, "README.md"), "# changed\n");
332 yield* checkpointStore.captureCheckpoint({
333 cwd: tmp,
334 checkpointRef: toCheckpointRef,
335 });
336
337 const diff = yield* checkpointStore.diffCheckpoints({
338 cwd: tmp,
339 fromCheckpointRef,
340 toCheckpointRef,
341 ignoreWhitespace: false,
342 });
343
344 expect(diff).toContain("diff --git a/README.md b/README.md");
345 }),
346 );
347
348 it.effect("can hide indentation churn when changes wrap existing lines", () =>
349 Effect.gen(function* () {
350 const tmp = yield* makeTmpDir();
351 yield* initRepoWithCommit(tmp);
352 const checkpointStore = yield* CheckpointStore.CheckpointStore;
353 const threadId = ThreadId.make("thread-checkpoint-store-whitespace");
354 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
355 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
356
357 const componentPath = NodePath.join(tmp, "Component.tsx");
358 yield* writeTextFile(
359 componentPath,
360 [
361 "export function View() {",
362 " return (",
363 " <section>",
364 " <h1>Title</h1>",
365 " <p>Body</p>",
366 " </section>",
367 " );",
368 "}",
369 "",
370 ].join("\n"),
371 );
372 yield* checkpointStore.captureCheckpoint({
373 cwd: tmp,
374 checkpointRef: fromCheckpointRef,
375 });
376 yield* writeTextFile(
377 componentPath,
378 [
379 "export function View() {",
380 " return (",
381 " <section>",
382 " {isReady ? (",
383 " <div>",
384 " <h1>Title</h1>",
385 " <p>Body</p>",
386 " </div>",
387 " ) : null}",
388 " </section>",
389 " );",
390 "}",
391 "",
392 ].join("\n"),
393 );
394 yield* checkpointStore.captureCheckpoint({
395 cwd: tmp,
396 checkpointRef: toCheckpointRef,
397 });
398
399 const normalDiff = yield* checkpointStore.diffCheckpoints({
400 cwd: tmp,
401 fromCheckpointRef,
402 toCheckpointRef,
403 ignoreWhitespace: false,
404 });
405 const whitespaceIgnoredDiff = yield* checkpointStore.diffCheckpoints({
406 cwd: tmp,
407 fromCheckpointRef,
408 toCheckpointRef,
409 ignoreWhitespace: true,
410 });
411
412 expect(normalDiff).toContain("diff --git");
413 expect(normalDiff).toContain("- <h1>Title</h1>");
414 expect(normalDiff).toContain("+ <h1>Title</h1>");
415 expect(whitespaceIgnoredDiff).toContain("diff --git");
416 expect(whitespaceIgnoredDiff).toContain("+ {isReady ? (");
417 expect(whitespaceIgnoredDiff).toContain("+ <div>");
418 expect(whitespaceIgnoredDiff).not.toContain("- <h1>Title</h1>");
419 expect(whitespaceIgnoredDiff).not.toContain("+ <h1>Title</h1>");
420
421 for (const ignoreWhitespace of [false, true]) {
422 const numstat = yield* checkpointStore.diffCheckpoints({
423 cwd: tmp,
424 fromCheckpointRef,
425 toCheckpointRef,
426 ignoreWhitespace,
427 format: "numstat",
428 });
429 expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([
430 {
431 path: "Component.tsx",
432 additions: ignoreWhitespace ? 4 : 6,
433 deletions: ignoreWhitespace ? 0 : 2,
434 },
435 ]);
436 }
437 }),
438 );
439 });
440
441 describe("checkpoint file summaries", () => {
442 it.effect("counts changes whose full patch exceeds the output limit", () =>
443 Effect.gen(function* () {
444 const tmp = yield* makeTmpDir();
445 yield* initRepoWithCommit(tmp);
446 const checkpointStore = yield* CheckpointStore.CheckpointStore;
447 const threadId = ThreadId.make("large-checkpoint-summary");
448 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
449 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
450 const filePath = NodePath.join(tmp, "README.md");
451 const lineCount = 20_000;
452 yield* writeTextFile(filePath, `${"before".repeat(50)}\n`.repeat(lineCount));
453 yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: fromCheckpointRef });
454 yield* writeTextFile(filePath, `${"after".repeat(60)}\n`.repeat(lineCount));
455 yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: toCheckpointRef });
456
457 const numstat = yield* checkpointStore.diffCheckpoints({
458 cwd: tmp,
459 fromCheckpointRef,
460 toCheckpointRef,
461 ignoreWhitespace: false,
462 format: "numstat",
463 });
464
465 expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([
466 { path: "README.md", additions: lineCount, deletions: lineCount },
467 ]);
468 expect(numstat.length).toBeLessThan(100);
469 }),
470 );
471
472 it.effect("preserves file paths and turn ranges without changing the user index", () =>
473 Effect.gen(function* () {
474 const tmp = yield* makeTmpDir();
475 yield* initRepoWithCommit(tmp);
476 yield* git(tmp, ["config", "diff.renames", "copies"]);
477 const fileSystem = yield* FileSystem.FileSystem;
478 const checkpointStore = yield* CheckpointStore.CheckpointStore;
479 const threadId = ThreadId.make("checkpoint-summary-paths");
480 const baseline = checkpointRefForThreadTurn(threadId, 0);
481 const firstTurn = checkpointRefForThreadTurn(threadId, 1);
482 const secondTurn = checkpointRefForThreadTurn(threadId, 2);
483 const copiedText = Array.from({ length: 20 }, (_, index) => `copy line ${index}\n`).join(
484 "",
485 );
486 const platform = yield* HostProcess.Platform;
487 const renamedPath = platform === "win32" ? "renamed café.txt" : "renamed\tcafé\nname.txt";
488 const addedPath = platform === "win32" ? "new café.txt" : "new\tfile\n名.txt";
489 for (const [path, contents] of Object.entries({
490 "copy-source.txt": copiedText,
491 "deleted.txt": "delete me\n",
492 "rename-old.txt": "before\nkeep one\nkeep two\nkeep three\n",
493 "binary.bin": "\0before",
494 })) {
495 yield* writeTextFile(NodePath.join(tmp, path), contents);
496 }
497 yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: baseline });
498
499 yield* fileSystem.rename(
500 NodePath.join(tmp, "rename-old.txt"),
501 NodePath.join(tmp, renamedPath),
502 );
503 yield* fileSystem.remove(NodePath.join(tmp, "deleted.txt"));
504 for (const [path, contents] of Object.entries({
505 "copy-source.txt": `${copiedText}one more\n`,
506 "copied.txt": copiedText,
507 [renamedPath]: "after\nkeep one\nkeep two\nkeep three\n",
508 "binary.bin": "\0after",
509 "empty.txt": "",
510 [addedPath]: "first\nsecond\n",
511 })) {
512 yield* writeTextFile(NodePath.join(tmp, path), contents);
513 }
514 yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: firstTurn });
515 const userIndex = yield* fileSystem.readFile(NodePath.join(tmp, ".git/index"));
516 const input = {
517 cwd: tmp,
518 fromCheckpointRef: baseline,
519 toCheckpointRef: firstTurn,
520 ignoreWhitespace: false,
521 format: "numstat" as const,
522 };
523 const firstSummary = parseTurnDiffFilesFromNumstat(
524 yield* checkpointStore.diffCheckpoints(input),
525 );
526 const expectedFiles = [
527 { path: "binary.bin", additions: 0, deletions: 0 },
528 { path: "copied.txt", previousPath: "copy-source.txt", additions: 0, deletions: 0 },
529 { path: "copy-source.txt", additions: 1, deletions: 0 },
530 { path: "deleted.txt", additions: 0, deletions: 1 },
531 { path: "empty.txt", additions: 0, deletions: 0 },
532 { path: addedPath, additions: 2, deletions: 0 },
533 { path: renamedPath, previousPath: "rename-old.txt", additions: 1, deletions: 1 },
534 ].toSorted((left, right) => left.path.localeCompare(right.path));
535 expect(firstSummary).toEqual(expectedFiles);
536
537 yield* fileSystem.remove(NodePath.join(tmp, "empty.txt"));
538 yield* writeTextFile(NodePath.join(tmp, "copy-source.txt"), "replacement\n");
539 yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: secondTurn });
540 const secondSummary = parseTurnDiffFilesFromNumstat(
541 yield* checkpointStore.diffCheckpoints({
542 ...input,
543 fromCheckpointRef: firstTurn,
544 toCheckpointRef: secondTurn,
545 }),
546 );
547 expect(secondSummary).toEqual([
548 { path: "copy-source.txt", additions: 1, deletions: 21 },
549 { path: "empty.txt", additions: 0, deletions: 0 },
550 ]);
551
552 const inclusiveSummary = parseTurnDiffFilesFromNumstat(
553 yield* checkpointStore.diffCheckpoints({ ...input, toCheckpointRef: secondTurn }),
554 );
555 expect(inclusiveSummary).toEqual(
556 expectedFiles
557 .filter((file) => file.path !== "empty.txt")
558 .map((file) =>
559 file.path === "copy-source.txt" ? { ...file, additions: 1, deletions: 20 } : file,
560 ),
561 );
562 expect(
563 yield* checkpointStore.diffCheckpoints({ ...input, toCheckpointRef: baseline }),
564 ).toBe("");
565 expect(yield* fileSystem.readFile(NodePath.join(tmp, ".git/index"))).toEqual(userIndex);
566 }),
567 );
568
569 it.effect("uses HEAD for a missing baseline only when requested", () =>
570 Effect.gen(function* () {
571 const tmp = yield* makeTmpDir();
572 yield* initRepoWithCommit(tmp);
573 const checkpointStore = yield* CheckpointStore.CheckpointStore;
574 const threadId = ThreadId.make("checkpoint-summary-fallback");
575 const fromCheckpointRef = checkpointRefForThreadTurn(threadId, 0);
576 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
577 yield* writeTextFile(NodePath.join(tmp, "README.md"), "changed\n");
578 yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: toCheckpointRef });
579 const input = {
580 cwd: tmp,
581 fromCheckpointRef,
582 toCheckpointRef,
583 ignoreWhitespace: false,
584 format: "numstat" as const,
585 };
586
587 const error = yield* Effect.flip(checkpointStore.diffCheckpoints(input));
588 expect(error._tag).toBe("VcsProcessExitError");
589 const numstat = yield* checkpointStore.diffCheckpoints({
590 ...input,
591 fallbackFromToHead: true,
592 });
593 expect(parseTurnDiffFilesFromNumstat(numstat)).toEqual([
594 { path: "README.md", additions: 1, deletions: 1 },
595 ]);
596 }),
597 );
598 it.effect("limits a diff to repository paths from a subdirectory workspace", () =>
599 Effect.gen(function* () {
600 const tmp = yield* makeTmpDir();
601 yield* initRepoWithCommit(tmp);
602 const fileSystem = yield* FileSystem.FileSystem;
603 const cwd = NodePath.join(tmp, "app");
604 yield* fileSystem.makeDirectory(cwd);
605 const checkpointStore = yield* CheckpointStore.CheckpointStore;
606 const threadId = ThreadId.make("checkpoint-file-paths-subdirectory");
607 const refs = {
608 cwd,
609 fromCheckpointRef: checkpointRefForThreadTurn(threadId, 0),
610 toCheckpointRef: checkpointRefForThreadTurn(threadId, 1),
611 };
612 yield* checkpointStore.captureCheckpoint({ cwd, checkpointRef: refs.fromCheckpointRef });
613 yield* writeTextFile(NodePath.join(cwd, "kept.txt"), "kept\n");
614 yield* writeTextFile(NodePath.join(tmp, "README.md"), "# hidden\n");
615 yield* checkpointStore.captureCheckpoint({ cwd, checkpointRef: refs.toCheckpointRef });
616
617 const patch = yield* checkpointStore.diffCheckpoints({
618 ...refs,
619 ignoreWhitespace: false,
620 filePaths: ["app/kept.txt"],
621 });
622 expect(patch).toContain("+kept");
623 expect(patch).not.toContain("hidden");
624 }),
625 );
626 });
627
628 describe("listAuthoredPaths", () => {
629 it.effect("leaves out paths that only a merged upstream changed", () =>
630 Effect.gen(function* () {
631 const tmp = yield* makeTmpDir();
632 yield* initRepoWithCommit(tmp);
633 const checkpointStore = yield* CheckpointStore.CheckpointStore;
634 const threadId = ThreadId.make("checkpoint-authored-paths");
635 const refs = {
636 cwd: tmp,
637 fromCheckpointRef: checkpointRefForThreadTurn(threadId, 0),
638 toCheckpointRef: checkpointRefForThreadTurn(threadId, 1),
639 };
640 const commitBeforeTurn = (file: string, contents: string) =>
641 Effect.gen(function* () {
642 yield* writeTextFile(NodePath.join(tmp, file), contents);
643 yield* git(tmp, ["add", "."]);
644 yield* git(tmp, ["commit", "-m", file], { committedAt: "2020-01-01T00:00:00Z" });
645 });
646
647 yield* git(tmp, ["checkout", "-b", "upstream"]);
648 yield* commitBeforeTurn("upstream.txt", "upstream\n");
649 yield* commitBeforeTurn("shared.txt", "upstream\n");
650 yield* git(tmp, ["checkout", "-"]);
651 yield* commitBeforeTurn("shared.txt", "ours\n");
652 yield* checkpointStore.captureCheckpoint({
653 cwd: tmp,
654 checkpointRef: refs.fromCheckpointRef,
655 });
656
657 yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: refs.toCheckpointRef });
658 expect(yield* checkpointStore.listAuthoredPaths(refs)).toBeNull();
659
660 yield* git(tmp, ["merge", "upstream", "--no-edit"], { allowNonZeroExit: true });
661 yield* writeTextFile(NodePath.join(tmp, "shared.txt"), "upstream\n");
662 yield* git(tmp, ["commit", "-am", "merge upstream"]);
663 yield* writeTextFile(NodePath.join(tmp, "README.md"), "# edited\n");
664 yield* checkpointStore.captureCheckpoint({ cwd: tmp, checkpointRef: refs.toCheckpointRef });
665
666 const numstat = yield* checkpointStore.diffCheckpoints({
667 ...refs,
668 ignoreWhitespace: false,
669 format: "numstat",
670 });
671 expect(parseTurnDiffFilesFromNumstat(numstat).map((file) => file.path)).toEqual([
672 "README.md",
673 "shared.txt",
674 "upstream.txt",
675 ]);
676 expect(yield* checkpointStore.listAuthoredPaths(refs)).toEqual(
677 new Set(["README.md", "shared.txt"]),
678 );
679 }),
680 );
681 });
682});