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, 04:45 UTC
| 1 | import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import { assert, it } from "@effect/vitest"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 10 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 11 | import { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 12 | |
| 13 | import * as ForgejoCli from "./ForgejoCli.ts"; |
| 14 | import * as ForgejoSourceControlProvider from "./ForgejoSourceControlProvider.ts"; |
| 15 | |
| 16 | const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); |
| 17 | |
| 18 | it.effect( |
| 19 | "checks out fj pull refs and preserves existing branches and dirty files until forced", |
| 20 | () => |
| 21 | Effect.gen(function* () { |
| 22 | const fs = yield* FileSystem.FileSystem; |
| 23 | const git = (yield* SourceControlHost.SourceControlHost).process; |
| 24 | const path = yield* Path.Path; |
| 25 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-fj-checkout-" }); |
| 26 | const source = path.join(root, "source"); |
| 27 | const cwd = path.join(root, "checkout"); |
| 28 | yield* fs.makeDirectory(source); |
| 29 | for (const args of [ |
| 30 | ["init", "-b", "main"], |
| 31 | ["config", "user.name", "Test"], |
| 32 | ["config", "user.email", "test@example.com"], |
| 33 | ]) |
| 34 | yield* git.run({ operation: "test.setup", command: "git", cwd: source, args }); |
| 35 | yield* fs.writeFileString(path.join(source, "base.txt"), "base\n"); |
| 36 | for (const args of [ |
| 37 | ["add", "base.txt"], |
| 38 | ["commit", "-m", "base"], |
| 39 | ]) |
| 40 | yield* git.run({ operation: "test.setup", command: "git", cwd: source, args }); |
| 41 | const base = (yield* git.run({ |
| 42 | operation: "test.setup", |
| 43 | command: "git", |
| 44 | cwd: source, |
| 45 | args: ["rev-parse", "HEAD"], |
| 46 | })).stdout.trim(); |
| 47 | yield* git.run({ |
| 48 | operation: "test.setup", |
| 49 | command: "git", |
| 50 | cwd: root, |
| 51 | args: ["clone", source, cwd], |
| 52 | }); |
| 53 | yield* fs.writeFileString(path.join(source, "feature.txt"), "pull request change\n"); |
| 54 | for (const args of [ |
| 55 | ["add", "feature.txt"], |
| 56 | ["commit", "-m", "feature"], |
| 57 | ["update-ref", "refs/pull/42/head", "HEAD"], |
| 58 | ]) |
| 59 | yield* git.run({ operation: "test.setup", command: "git", cwd: source, args }); |
| 60 | const head = (yield* git.run({ |
| 61 | operation: "test.setup", |
| 62 | command: "git", |
| 63 | cwd: source, |
| 64 | args: ["rev-parse", "HEAD"], |
| 65 | })).stdout.trim(); |
| 66 | const fetched: string[] = []; |
| 67 | const provider = yield* ForgejoSourceControlProvider.make.pipe( |
| 68 | Effect.provide( |
| 69 | Layer.merge( |
| 70 | TestSourceControlHost.layerWithRealGit({ |
| 71 | process: { |
| 72 | run: (input) => { |
| 73 | if (input.args[0] !== "fetch") return git.run(input); |
| 74 | const url = input.args[2]; |
| 75 | assert.isDefined(url); |
| 76 | fetched.push(url!); |
| 77 | return git.run({ |
| 78 | ...input, |
| 79 | args: input.args.map((arg) => |
| 80 | arg === "git@forgejo.test:reviewer/project.git" ? source : arg, |
| 81 | ), |
| 82 | }); |
| 83 | }, |
| 84 | }, |
| 85 | }), |
| 86 | Layer.mock(ForgejoCli.ForgejoCli)({ |
| 87 | resolveRepository: () => |
| 88 | Effect.succeed({ |
| 89 | command: "fj", |
| 90 | login: "work", |
| 91 | repository: "reviewer/project", |
| 92 | baseUrl: "https://forgejo.test", |
| 93 | }), |
| 94 | api: (input) => { |
| 95 | assert.include( |
| 96 | ["repos/reviewer/project", "repos/reviewer/project/pulls/42"], |
| 97 | input.path, |
| 98 | ); |
| 99 | return Effect.succeed( |
| 100 | processOutput( |
| 101 | encodeJson( |
| 102 | input.path.endsWith("/pulls/42") |
| 103 | ? { |
| 104 | number: 42, |
| 105 | title: "Checkout", |
| 106 | html_url: "https://forgejo.test/reviewer/project/pulls/42", |
| 107 | state: "open", |
| 108 | merged: false, |
| 109 | base: { ref: "main", sha: base, repo: null }, |
| 110 | head: { ref: "feature", sha: head, repo: null }, |
| 111 | } |
| 112 | : { |
| 113 | full_name: "reviewer/project", |
| 114 | clone_url: source, |
| 115 | ssh_url: "git@forgejo.test:reviewer/project.git", |
| 116 | default_branch: "main", |
| 117 | }, |
| 118 | ), |
| 119 | ), |
| 120 | ); |
| 121 | }, |
| 122 | }), |
| 123 | ), |
| 124 | ), |
| 125 | ); |
| 126 | yield* provider.checkoutChangeRequest({ |
| 127 | cwd, |
| 128 | reference: "https://forgejo.test/reviewer/project/pulls/42", |
| 129 | }); |
| 130 | assert.strictEqual( |
| 131 | (yield* git.run({ |
| 132 | operation: "test.verify", |
| 133 | command: "git", |
| 134 | cwd, |
| 135 | args: ["branch", "--show-current"], |
| 136 | })).stdout.trim(), |
| 137 | "pulls/42", |
| 138 | ); |
| 139 | assert.strictEqual( |
| 140 | (yield* git.run({ |
| 141 | operation: "test.verify", |
| 142 | command: "git", |
| 143 | cwd, |
| 144 | args: ["rev-parse", "HEAD"], |
| 145 | })).stdout.trim(), |
| 146 | head, |
| 147 | ); |
| 148 | assert.strictEqual( |
| 149 | yield* fs.readFileString(path.join(cwd, "feature.txt")), |
| 150 | "pull request change\n", |
| 151 | ); |
| 152 | for (const args of [ |
| 153 | ["checkout", "main"], |
| 154 | ["branch", "-f", "pulls/42", "main"], |
| 155 | ]) |
| 156 | yield* git.run({ operation: "test.setup", command: "git", cwd, args }); |
| 157 | yield* fs.writeFileString(path.join(cwd, "base.txt"), "uncommitted work\n"); |
| 158 | yield* provider.checkoutChangeRequest({ cwd, reference: "42" }); |
| 159 | assert.strictEqual( |
| 160 | (yield* git.run({ |
| 161 | operation: "test.verify", |
| 162 | command: "git", |
| 163 | cwd, |
| 164 | args: ["rev-parse", "HEAD"], |
| 165 | })).stdout.trim(), |
| 166 | base, |
| 167 | ); |
| 168 | assert.strictEqual(yield* fs.exists(path.join(cwd, "feature.txt")), false); |
| 169 | yield* provider.checkoutChangeRequest({ |
| 170 | cwd, |
| 171 | reference: "42", |
| 172 | force: true, |
| 173 | context: { |
| 174 | provider: { |
| 175 | kind: SourceControlProviderKind.make("forgejo"), |
| 176 | name: "Forgejo", |
| 177 | baseUrl: "https://forgejo.test", |
| 178 | }, |
| 179 | remoteName: "origin", |
| 180 | remoteUrl: "git@forgejo.test:maria/project.git", |
| 181 | }, |
| 182 | }); |
| 183 | assert.strictEqual( |
| 184 | (yield* git.run({ |
| 185 | operation: "test.verify", |
| 186 | command: "git", |
| 187 | cwd, |
| 188 | args: ["rev-parse", "HEAD"], |
| 189 | })).stdout.trim(), |
| 190 | head, |
| 191 | ); |
| 192 | assert.strictEqual( |
| 193 | yield* fs.readFileString(path.join(cwd, "base.txt")), |
| 194 | "uncommitted work\n", |
| 195 | ); |
| 196 | assert.strictEqual( |
| 197 | yield* fs.readFileString(path.join(cwd, "feature.txt")), |
| 198 | "pull request change\n", |
| 199 | ); |
| 200 | assert.deepStrictEqual(fetched, [source, source, "git@forgejo.test:reviewer/project.git"]); |
| 201 | }).pipe( |
| 202 | Effect.scoped, |
| 203 | Effect.provide(Layer.merge(TestSourceControlHost.layerWithRealGit(), NodeServices.layer)), |
| 204 | ), |
| 205 | ); |