@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 83 lines 3.5 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 Path from "effect/Path";
6 import * as ChildProcess from "effect/process/ChildProcess";
7 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
8 import { vi } from "vite-plus/test";
9
10 import { checkpointWorkspace } from "./replayWorkspace.ts";
11
12 const git = (cwd: string, args: ReadonlyArray<string>) =>
13 Effect.gen(function* () {
14 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
15 return yield* spawner.string(ChildProcess.make("git", args, { cwd }));
16 });
17
18 const repositoryState = (repo: string, worktree: string, worktreeIndex: string) =>
19 Effect.gen(function* () {
20 return {
21 config: yield* git(repo, ["config", "--local", "--list"]),
22 refs: yield* git(repo, ["for-each-ref"]),
23 worktrees: yield* git(repo, ["worktree", "list", "--porcelain"]),
24 worktreeHead: yield* git(worktree, ["rev-parse", "HEAD"]),
25 worktreeIndex: yield* git(repo, ["hash-object", "--no-filters", worktreeIndex]),
26 };
27 });
28
29 it.layer(NodeServices.layer)("checkpointWorkspace", (it) => {
30 // `git bisect run` and `git rebase --exec` export GIT_DIR to what they run, and hooks add
31 // GIT_INDEX_FILE. In a linked worktree both are absolute paths into the shared repository.
32 it.effect.each([
33 { runner: "git bisect run", selectors: ["GIT_DIR"] },
34 { runner: "a git hook", selectors: ["GIT_DIR", "GIT_INDEX_FILE"] },
35 ])("keeps to its own repository when run under $runner", ({ selectors }) =>
36 Effect.gen(function* () {
37 yield* Effect.addFinalizer(() => Effect.sync(() => vi.unstubAllEnvs()));
38 // These commits use the contributor's global config, which must not require a signing key.
39 vi.stubEnv("GIT_CONFIG_COUNT", "1");
40 vi.stubEnv("GIT_CONFIG_KEY_0", "commit.gpgsign");
41 vi.stubEnv("GIT_CONFIG_VALUE_0", "false");
42 const fs = yield* FileSystem.FileSystem;
43 const path = yield* Path.Path;
44 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-replay-workspace-decoy-" });
45 const repo = path.join(root, "repo");
46 const worktree = path.join(root, "worktree");
47 yield* fs.makeDirectory(repo);
48 yield* git(repo, ["init"]);
49 yield* git(repo, [
50 "-c",
51 "user.name=Decoy",
52 "-c",
53 "user.email=decoy@example.com",
54 "commit",
55 "--allow-empty",
56 "-m",
57 "decoy",
58 ]);
59 yield* git(repo, ["worktree", "add", "--detach", worktree]);
60 const worktreeGitDir = (yield* git(worktree, ["rev-parse", "--absolute-git-dir"])).trim();
61 const worktreeIndex = path.join(worktreeGitDir, "index");
62 const before = yield* repositoryState(repo, worktree, worktreeIndex);
63
64 const inherited: Record<string, string> = {
65 GIT_DIR: worktreeGitDir,
66 GIT_INDEX_FILE: worktreeIndex,
67 };
68 for (const name of selectors) vi.stubEnv(name, inherited[name]);
69 const workspace = yield* checkpointWorkspace("decoy-guard", { "src/a.txt": "a\n" });
70 vi.unstubAllEnvs();
71
72 assert.deepStrictEqual(yield* repositoryState(repo, worktree, worktreeIndex), before);
73 assert.strictEqual(
74 yield* git(workspace, ["log", "--format=%s%x00%an", "-1"]),
75 "initial\u0000T3 Code Test\n",
76 );
77 assert.strictEqual(
78 (yield* git(workspace, ["ls-files"])).trim().split("\n").join(","),
79 "README.md,src/a.txt",
80 );
81 }),
82 );
83 });