The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { assert, expect, 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 Redacted from "effect/Redacted"; |
| 7 | |
| 8 | import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 9 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 10 | |
| 11 | import * as GitHubAppAccess from "./GitHubAppAccess.ts"; |
| 12 | import { cascadeRebaseStack } from "./githubStackRebase.ts"; |
| 13 | |
| 14 | const layer = Layer.mergeAll( |
| 15 | Layer.mock(GitHubAppAccess.GitHubAppAccess)({ |
| 16 | credentialFor: () => Effect.succeed({ token: Redacted.make("token"), fingerprint: "fp" }), |
| 17 | }), |
| 18 | TestSourceControlHost.layerWithRealGit(), |
| 19 | ).pipe(Layer.provideMerge(NodeServices.layer)); |
| 20 | |
| 21 | const setup = Effect.gen(function* () { |
| 22 | const fs = yield* FileSystem.FileSystem; |
| 23 | const { process } = yield* SourceControlHost.SourceControlHost; |
| 24 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-test-" }); |
| 25 | const remote = `${root}/remote.git`; |
| 26 | const work = `${root}/work`; |
| 27 | const git = (cwd: string, ...args: string[]) => |
| 28 | process |
| 29 | .run({ |
| 30 | operation: "test", |
| 31 | command: "git", |
| 32 | args, |
| 33 | cwd, |
| 34 | env: { |
| 35 | GIT_AUTHOR_NAME: "t", |
| 36 | GIT_AUTHOR_EMAIL: "t@t", |
| 37 | GIT_COMMITTER_NAME: "t", |
| 38 | GIT_COMMITTER_EMAIL: "t@t", |
| 39 | }, |
| 40 | }) |
| 41 | .pipe(Effect.map((out) => out.stdout.trim())); |
| 42 | const write = (path: string, text: string) => fs.writeFileString(`${work}/${path}`, text); |
| 43 | yield* git(root, "init", "--quiet", "--bare", "-b", "main", remote); |
| 44 | yield* git(root, "clone", "--quiet", remote, work); |
| 45 | yield* write("index.ts", "export const answer = 41;\n"); |
| 46 | yield* git(work, "add", "-A"); |
| 47 | yield* git(work, "commit", "--quiet", "-m", "Initial"); |
| 48 | yield* git(work, "switch", "--quiet", "-c", "feat/answer-42"); |
| 49 | yield* write("index.ts", "export const answer = 42;\n"); |
| 50 | yield* git(work, "commit", "--quiet", "-am", "A: set the answer to 42"); |
| 51 | yield* git(work, "switch", "--quiet", "-c", "feat/answer-doc"); |
| 52 | yield* write("index.ts", "/** The answer. */\nexport const answer = 42;\n"); |
| 53 | yield* git(work, "commit", "--quiet", "-am", "B: document the answer"); |
| 54 | yield* git(work, "switch", "--quiet", "main"); |
| 55 | yield* write("README.md", "main moved ahead\n"); |
| 56 | yield* git(work, "add", "-A"); |
| 57 | yield* git(work, "commit", "--quiet", "-m", "Docs"); |
| 58 | yield* git(work, "push", "--quiet", "origin", "main", "feat/answer-42", "feat/answer-doc"); |
| 59 | const head = (branch: string) => git(remote, "--git-dir=.", "rev-parse", `refs/heads/${branch}`); |
| 60 | return { |
| 61 | remote, |
| 62 | head, |
| 63 | git: (...args: string[]) => git(remote, "--git-dir=.", ...args), |
| 64 | layers: [ |
| 65 | { number: 1, headBranch: "feat/answer-42", headSha: yield* head("feat/answer-42") }, |
| 66 | { number: 2, headBranch: "feat/answer-doc", headSha: yield* head("feat/answer-doc") }, |
| 67 | ], |
| 68 | }; |
| 69 | }); |
| 70 | |
| 71 | it.layer(layer)("cascadeRebaseStack", (it) => { |
| 72 | it.effect("moves each layer's own commits onto the rebased layer below it", () => |
| 73 | Effect.gen(function* () { |
| 74 | const repo = yield* setup; |
| 75 | const completed = yield* cascadeRebaseStack({ |
| 76 | host: "github.com", |
| 77 | repository: "acme/web", |
| 78 | base: "main", |
| 79 | layers: repo.layers, |
| 80 | remote: repo.remote, |
| 81 | }); |
| 82 | assert.strictEqual(completed, 2); |
| 83 | |
| 84 | const main = yield* repo.head("main"); |
| 85 | const bottom = yield* repo.head("feat/answer-42"); |
| 86 | const top = yield* repo.head("feat/answer-doc"); |
| 87 | expect(yield* repo.git("rev-parse", `${bottom}~1`)).toBe(main); |
| 88 | expect(yield* repo.git("rev-parse", `${top}~1`)).toBe(bottom); |
| 89 | expect(yield* repo.git("log", "--format=%s", `${bottom}..${top}`)).toBe( |
| 90 | "B: document the answer", |
| 91 | ); |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | it.effect("drops a squash-merged lower layer's commits from the layer above it", () => |
| 96 | Effect.gen(function* () { |
| 97 | const repo = yield* setup; |
| 98 | const fs = yield* FileSystem.FileSystem; |
| 99 | const { process } = yield* SourceControlHost.SourceControlHost; |
| 100 | const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-squash-" }); |
| 101 | const run = (...args: string[]) => |
| 102 | process |
| 103 | .run({ |
| 104 | operation: "test", |
| 105 | command: "git", |
| 106 | args, |
| 107 | cwd: scratch, |
| 108 | env: { |
| 109 | GIT_AUTHOR_NAME: "t", |
| 110 | GIT_AUTHOR_EMAIL: "t@t", |
| 111 | GIT_COMMITTER_NAME: "t", |
| 112 | GIT_COMMITTER_EMAIL: "t@t", |
| 113 | }, |
| 114 | }) |
| 115 | .pipe(Effect.map((out) => out.stdout.trim())); |
| 116 | const write = (text: string) => fs.writeFileString(`${scratch}/index.ts`, text); |
| 117 | yield* run("clone", "--quiet", repo.remote, "."); |
| 118 | yield* run("switch", "--quiet", "-c", "feat/two-steps"); |
| 119 | yield* write("export const answer = 40;\n"); |
| 120 | yield* run("commit", "--quiet", "-am", "A1"); |
| 121 | yield* write("export const answer = 42;\n"); |
| 122 | yield* run("commit", "--quiet", "-am", "A2"); |
| 123 | const mergedHeadSha = yield* run("rev-parse", "HEAD"); |
| 124 | yield* run("switch", "--quiet", "-c", "feat/on-top"); |
| 125 | yield* write("/** The answer. */\nexport const answer = 42;\n"); |
| 126 | yield* run("commit", "--quiet", "-am", "B"); |
| 127 | const headSha = yield* run("rev-parse", "HEAD"); |
| 128 | yield* run("switch", "--quiet", "main"); |
| 129 | yield* run("merge", "--quiet", "--squash", "feat/two-steps"); |
| 130 | yield* run("commit", "--quiet", "-m", "A (#1)"); |
| 131 | yield* run("push", "--quiet", "origin", "main", "feat/on-top"); |
| 132 | |
| 133 | const completed = yield* cascadeRebaseStack({ |
| 134 | host: "github.com", |
| 135 | repository: "acme/web", |
| 136 | base: "main", |
| 137 | layers: [{ number: 2, headBranch: "feat/on-top", headSha }], |
| 138 | mergedHeadSha, |
| 139 | remote: repo.remote, |
| 140 | }); |
| 141 | assert.strictEqual(completed, 1); |
| 142 | const top = yield* repo.head("feat/on-top"); |
| 143 | expect(yield* repo.git("rev-parse", `${top}~1`)).toBe(yield* repo.head("main")); |
| 144 | expect(yield* repo.git("log", "--format=%s", `main..${top}`)).toBe("B"); |
| 145 | }), |
| 146 | ); |
| 147 | |
| 148 | it.effect("refuses to overwrite a layer pushed after it was reviewed", () => |
| 149 | Effect.gen(function* () { |
| 150 | const repo = yield* setup; |
| 151 | const stale = [repo.layers[0]!, { ...repo.layers[1]!, headSha: repo.layers[0]!.headSha }]; |
| 152 | const error = yield* Effect.flip( |
| 153 | cascadeRebaseStack({ |
| 154 | host: "github.com", |
| 155 | repository: "acme/web", |
| 156 | base: "main", |
| 157 | layers: stale, |
| 158 | remote: repo.remote, |
| 159 | }), |
| 160 | ); |
| 161 | expect(error).toMatchObject({ _tag: "GitHubStackRebaseGitError", number: 2, completed: 1 }); |
| 162 | expect(yield* repo.head("feat/answer-doc")).toBe(repo.layers[1]!.headSha); |
| 163 | }), |
| 164 | ); |
| 165 | |
| 166 | it.effect("stops at a conflicting layer and leaves it untouched", () => |
| 167 | Effect.gen(function* () { |
| 168 | const repo = yield* setup; |
| 169 | const fs = yield* FileSystem.FileSystem; |
| 170 | const { process } = yield* SourceControlHost.SourceControlHost; |
| 171 | const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-conflict-" }); |
| 172 | const run = (...args: string[]) => |
| 173 | process.run({ |
| 174 | operation: "test", |
| 175 | command: "git", |
| 176 | args, |
| 177 | cwd: scratch, |
| 178 | env: { |
| 179 | GIT_AUTHOR_NAME: "t", |
| 180 | GIT_AUTHOR_EMAIL: "t@t", |
| 181 | GIT_COMMITTER_NAME: "t", |
| 182 | GIT_COMMITTER_EMAIL: "t@t", |
| 183 | }, |
| 184 | }); |
| 185 | yield* run("clone", "--quiet", repo.remote, "."); |
| 186 | yield* fs.writeFileString(`${scratch}/index.ts`, "export const answer = 43;\n"); |
| 187 | yield* run("commit", "--quiet", "-am", "Conflict"); |
| 188 | yield* run("push", "--quiet", "origin", "main"); |
| 189 | |
| 190 | const error = yield* Effect.flip( |
| 191 | cascadeRebaseStack({ |
| 192 | host: "github.com", |
| 193 | repository: "acme/web", |
| 194 | base: "main", |
| 195 | layers: repo.layers, |
| 196 | remote: repo.remote, |
| 197 | }), |
| 198 | ); |
| 199 | expect(error).toMatchObject({ |
| 200 | _tag: "GitHubStackRebaseConflictError", |
| 201 | number: 1, |
| 202 | completed: 0, |
| 203 | }); |
| 204 | expect(yield* repo.head("feat/answer-42")).toBe(repo.layers[0]!.headSha); |
| 205 | }), |
| 206 | ); |
| 207 | }); |