The best way to control Claude Code, Codex and more from any computer or phone

main
ts 94 lines 4.25 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, it } from "@effect/vitest";
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as Layer from "effect/Layer";
6 import * as Path from "effect/Path";
7
8 import * as ProcessRunner from "../processRunner.ts";
9 import { makeIsWorktreeContainer } from "./worktreeContainer.ts";
10
11 const git = Effect.fn("worktreeContainerTest.git")(function* (
12 cwd: string,
13 ...args: ReadonlyArray<string>
14 ) {
15 const processRunner = yield* ProcessRunner.ProcessRunner;
16 return yield* processRunner.run({
17 command: "git",
18 args: ["-C", cwd, "-c", "user.email=t@t", "-c", "user.name=t", ...args],
19 });
20 });
21
22 it.layer(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))(
23 "isWorktreeContainer",
24 (it) => {
25 it.effect("is true only for a folder of worktrees around metadata that is no checkout", () =>
26 Effect.gen(function* () {
27 const fileSystem = yield* FileSystem.FileSystem;
28 const path = yield* Path.Path;
29 const isWorktreeContainer = yield* makeIsWorktreeContainer;
30 const fixture = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-container-test-" });
31
32 const source = path.join(fixture, "source");
33 yield* fileSystem.makeDirectory(source);
34 yield* git(source, "init", "-b", "develop");
35 yield* fileSystem.writeFileString(path.join(source, "README.md"), "readme\n");
36 yield* git(source, "add", "README.md");
37 yield* git(source, "commit", "-m", "init");
38
39 const makeContainer = Effect.fn(function* (name: string, coreBare: boolean) {
40 const root = path.join(fixture, name);
41 yield* fileSystem.makeDirectory(root);
42 yield* git(fixture, "clone", "--bare", source, path.join(root, ".bare"));
43 yield* fileSystem.writeFileString(path.join(root, ".git"), "gitdir: ./.bare\n");
44 yield* git(root, "worktree", "add", "develop", "develop");
45 yield* git(root, "config", "core.bare", String(coreBare));
46 return root;
47 });
48 const makeSeparateGitDirCheckout = Effect.fn(function* (name: string) {
49 const root = path.join(fixture, name);
50 yield* fileSystem.makeDirectory(root);
51 yield* git(root, "init", "-b", "develop", "--separate-git-dir", ".metadata", ".");
52 return root;
53 });
54
55 const container = yield* makeContainer("container", false);
56 assert.isTrue(yield* isWorktreeContainer(container), "container");
57 assert.isFalse(yield* isWorktreeContainer(path.join(container, "develop")), "its worktree");
58
59 const linked = path.join(fixture, "link");
60 yield* fileSystem.symlink(container, linked);
61 assert.isTrue(yield* isWorktreeContainer(linked), "container through a symlink");
62
63 // The issue's steps leave core.bare set; Initialize Git would clear it later.
64 const bare = yield* makeContainer("bare", true);
65 assert.isTrue(yield* isWorktreeContainer(bare), "core.bare container");
66
67 // Git allows an orphan worktree before the first commit: no index, yet a valid checkout.
68 const fresh = yield* makeSeparateGitDirCheckout("fresh");
69 yield* git(fresh, "worktree", "add", "--orphan", "-b", "feature", "../fresh-feature");
70 assert.isFalse(yield* isWorktreeContainer(fresh), "fresh checkout with an orphan worktree");
71
72 const checkout = yield* makeSeparateGitDirCheckout("checkout");
73 yield* fileSystem.writeFileString(path.join(checkout, "README.md"), "readme\n");
74 yield* git(checkout, "add", "README.md");
75 yield* git(checkout, "commit", "-m", "init");
76 yield* git(checkout, "worktree", "add", "worktrees/feature", "-b", "feature");
77 assert.isFalse(yield* isWorktreeContainer(checkout), "checkout with a nested worktree");
78
79 const outside = path.join(fixture, "outside");
80 yield* git(
81 fixture,
82 "clone",
83 "--no-checkout",
84 "--separate-git-dir",
85 path.join(outside, ".metadata"),
86 source,
87 outside,
88 );
89 yield* git(outside, "worktree", "add", "../outside-feature", "-b", "feature");
90 assert.isFalse(yield* isWorktreeContainer(outside), "worktrees outside the folder");
91 }),
92 );
93 },
94 );