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, describe, it } from "@effect/vitest"; |
| 3 | import { GitCommandError } from "@t3tools/contracts"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 11 | import { ChildProcessSpawner } from "effect/process"; |
| 12 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 13 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 14 | |
| 15 | import * as GitCafeApi from "./GitCafeApi.ts"; |
| 16 | import * as GitCafeCredentials from "./GitCafeCredentials.ts"; |
| 17 | import * as GitCafeSourceControlProvider from "./GitCafeSourceControlProvider.ts"; |
| 18 | |
| 19 | const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); |
| 20 | const decodeJson = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown)); |
| 21 | |
| 22 | const authAnswer = (input: { stdout?: unknown; stderr?: unknown; exitCode?: number }) => ({ |
| 23 | stdout: input.stdout === undefined ? "" : encodeJson(input.stdout), |
| 24 | stderr: input.stderr === undefined ? "" : encodeJson(input.stderr), |
| 25 | exitCode: ChildProcessSpawner.ExitCode(input.exitCode ?? 0), |
| 26 | }); |
| 27 | const failure = (code: string, status: number | null) => ({ |
| 28 | schemaVersion: 1, |
| 29 | error: { code, status, message: `Failure ${code}` }, |
| 30 | }); |
| 31 | |
| 32 | /** Most calls touch no file; one that does fails the test. */ |
| 33 | const NoFiles = FileSystem.layerNoop({}); |
| 34 | |
| 35 | /** A provider on git.cafe whose checkout has no remote and which may not run `cafe`. */ |
| 36 | const layerFor = ( |
| 37 | client: HttpClient.HttpClient, |
| 38 | options: { readonly files?: Layer.Layer<FileSystem.FileSystem> } = {}, |
| 39 | ) => |
| 40 | Layer.merge(GitCafeApi.layer, options.files ?? NoFiles).pipe( |
| 41 | Layer.provideMerge(GitCafeCredentials.layer), |
| 42 | Layer.provideMerge( |
| 43 | TestSourceControlHost.layer({ |
| 44 | git: { |
| 45 | resolvePrimaryRemoteName: () => |
| 46 | Effect.fail( |
| 47 | new GitCommandError({ |
| 48 | operation: "resolvePrimaryRemoteName", |
| 49 | command: "git remote", |
| 50 | cwd: "/repo", |
| 51 | detail: "No git remote is configured.", |
| 52 | }), |
| 53 | ), |
| 54 | }, |
| 55 | }), |
| 56 | ), |
| 57 | Layer.provide(Layer.succeed(HostProcess.Environment, { CAFE_TOKEN: "env-token" })), |
| 58 | Layer.provide(Layer.succeed(HostProcess.WorkingDirectory, "/server")), |
| 59 | Layer.provide(Layer.succeed(HttpClient.HttpClient, client)), |
| 60 | ); |
| 61 | |
| 62 | describe("GitCafeSourceControlProvider", () => { |
| 63 | it("reads a signed-in account and keeps transient failures apart from sign-out", () => { |
| 64 | const { parseAuth } = GitCafeSourceControlProvider; |
| 65 | const signedIn = parseAuth( |
| 66 | authAnswer({ stdout: { schemaVersion: 1, data: { host: "git.cafe", username: "alice" } } }), |
| 67 | ); |
| 68 | assert.strictEqual(signedIn.status, "authenticated"); |
| 69 | assert.deepStrictEqual(signedIn.account, Option.some("alice")); |
| 70 | assert.strictEqual( |
| 71 | parseAuth(authAnswer({ stderr: failure("AUTHENTICATION_REQUIRED", 401), exitCode: 1 })) |
| 72 | .status, |
| 73 | "unauthenticated", |
| 74 | ); |
| 75 | for (const [code, status] of [ |
| 76 | ["FORBIDDEN", 403], |
| 77 | ["NETWORK_ERROR", null], |
| 78 | ] as const) { |
| 79 | assert.strictEqual( |
| 80 | parseAuth(authAnswer({ stderr: failure(code, status), exitCode: 1 })).status, |
| 81 | "unknown", |
| 82 | ); |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | it.effect.each([ |
| 87 | ["https://staging.git.cafe/team/project.git", "staging.git.cafe"], |
| 88 | ["ssh@git.cafe:team/project.git", "git.cafe"], |
| 89 | ] as const)("resolves the repository from the remote %s", ([remoteUrl, host]) => { |
| 90 | const urls: Array<string> = []; |
| 91 | const client = HttpClient.make((request) => { |
| 92 | urls.push(request.url); |
| 93 | return Effect.succeed( |
| 94 | HttpClientResponse.fromWeb( |
| 95 | request, |
| 96 | new Response(encodeJson({ name: "project", defaultBranch: "trunk" })), |
| 97 | ), |
| 98 | ); |
| 99 | }); |
| 100 | const context = { |
| 101 | provider: { |
| 102 | kind: SourceControlProviderKind.make("gitcafe"), |
| 103 | name: "GitCafe", |
| 104 | baseUrl: `https://${host}`, |
| 105 | }, |
| 106 | remoteName: "origin", |
| 107 | remoteUrl, |
| 108 | }; |
| 109 | return Effect.gen(function* () { |
| 110 | const provider = yield* GitCafeSourceControlProvider.make; |
| 111 | assert.strictEqual(provider.kind, "gitcafe"); |
| 112 | assert.strictEqual(yield* provider.getDefaultBranch({ cwd: "/repo", context }), "trunk"); |
| 113 | assert.deepStrictEqual( |
| 114 | yield* provider.getRepositoryCloneUrls({ cwd: "/repo", context, repository: remoteUrl }), |
| 115 | { |
| 116 | nameWithOwner: "team/project", |
| 117 | url: `https://${host}/team/project`, |
| 118 | sshUrl: `ssh@${host}:team/project.git`, |
| 119 | }, |
| 120 | ); |
| 121 | assert.deepStrictEqual(urls, [ |
| 122 | `https://${host}/api/repos/team/project`, |
| 123 | `https://${host}/api/repos/team/project`, |
| 124 | ]); |
| 125 | }).pipe( |
| 126 | Effect.provide( |
| 127 | Layer.merge(GitCafeApi.layer, NoFiles).pipe( |
| 128 | Layer.provideMerge(GitCafeCredentials.layer), |
| 129 | Layer.provideMerge(TestSourceControlHost.layer()), |
| 130 | Layer.provide( |
| 131 | Layer.succeed(HostProcess.Environment, { CAFE_TOKEN: "env-token", CAFE_HOST: host }), |
| 132 | ), |
| 133 | Layer.provide(Layer.succeed(HostProcess.WorkingDirectory, "/server")), |
| 134 | Layer.provide(Layer.succeed(HttpClient.HttpClient, client)), |
| 135 | ), |
| 136 | ), |
| 137 | ); |
| 138 | }); |
| 139 | |
| 140 | it.effect( |
| 141 | "reads a bare owner/name on git.cafe without a remote, and no other host's pulls", |
| 142 | () => { |
| 143 | const urls: Array<string> = []; |
| 144 | const client = HttpClient.make((request) => { |
| 145 | urls.push(request.url); |
| 146 | return Effect.succeed( |
| 147 | HttpClientResponse.fromWeb( |
| 148 | request, |
| 149 | new Response(encodeJson({ name: "project", defaultBranch: "trunk" })), |
| 150 | ), |
| 151 | ); |
| 152 | }); |
| 153 | return Effect.gen(function* () { |
| 154 | const provider = yield* GitCafeSourceControlProvider.make; |
| 155 | assert.deepStrictEqual( |
| 156 | yield* provider.getRepositoryCloneUrls({ cwd: "/repo", repository: "team/project" }), |
| 157 | { |
| 158 | nameWithOwner: "team/project", |
| 159 | url: "https://git.cafe/team/project", |
| 160 | sshUrl: "ssh@git.cafe:team/project.git", |
| 161 | }, |
| 162 | ); |
| 163 | const foreign = yield* provider |
| 164 | .getChangeRequest({ |
| 165 | cwd: "/repo", |
| 166 | reference: "https://example.com/team/project/pulls/42", |
| 167 | }) |
| 168 | .pipe(Effect.flip); |
| 169 | assert.strictEqual(foreign._tag, "SourceControlProviderError"); |
| 170 | assert.deepStrictEqual(urls, ["https://git.cafe/api/repos/team/project"]); |
| 171 | }).pipe( |
| 172 | Effect.provide( |
| 173 | Layer.merge(GitCafeApi.layer, NoFiles).pipe( |
| 174 | Layer.provideMerge(GitCafeCredentials.layer), |
| 175 | Layer.provideMerge( |
| 176 | TestSourceControlHost.layer({ |
| 177 | git: { |
| 178 | resolvePrimaryRemoteName: () => |
| 179 | Effect.fail( |
| 180 | new GitCommandError({ |
| 181 | operation: "resolvePrimaryRemoteName", |
| 182 | command: "git remote", |
| 183 | cwd: "/repo", |
| 184 | detail: "No git remote is configured.", |
| 185 | }), |
| 186 | ), |
| 187 | }, |
| 188 | }), |
| 189 | ), |
| 190 | Layer.provide(Layer.succeed(HostProcess.Environment, { CAFE_TOKEN: "env-token" })), |
| 191 | Layer.provide(Layer.succeed(HostProcess.WorkingDirectory, "/server")), |
| 192 | Layer.provide(Layer.succeed(HttpClient.HttpClient, client)), |
| 193 | ), |
| 194 | ), |
| 195 | ); |
| 196 | }, |
| 197 | ); |
| 198 | |
| 199 | it.effect.each([ |
| 200 | ["no context", undefined, "https://git.cafe/fork/project.git"], |
| 201 | ["an ssh:// remote", "ssh://ssh@git.cafe/team/project.git", "ssh@git.cafe:fork/project.git"], |
| 202 | [ |
| 203 | "an HTTPS remote with a user", |
| 204 | "https://alice@git.cafe/team/project.git", |
| 205 | "https://git.cafe/fork/project.git", |
| 206 | ], |
| 207 | ] as const)("checks a fork out over the transport of %s", ([, remoteUrl, expected]) => { |
| 208 | const remotes: Array<string> = []; |
| 209 | const client = HttpClient.make((request) => |
| 210 | Effect.succeed( |
| 211 | HttpClientResponse.fromWeb( |
| 212 | request, |
| 213 | new Response( |
| 214 | encodeJson({ |
| 215 | number: 7, |
| 216 | title: "Change", |
| 217 | state: "open", |
| 218 | draft: false, |
| 219 | sourceBranch: "feature", |
| 220 | targetBranch: "main", |
| 221 | headOid: null, |
| 222 | updatedAt: null, |
| 223 | sourceRepo: { owner: "fork", name: "project" }, |
| 224 | closedAt: null, |
| 225 | mergedAt: null, |
| 226 | }), |
| 227 | ), |
| 228 | ), |
| 229 | ), |
| 230 | ); |
| 231 | const context = |
| 232 | remoteUrl === undefined |
| 233 | ? undefined |
| 234 | : { |
| 235 | provider: { |
| 236 | kind: SourceControlProviderKind.make("gitcafe"), |
| 237 | name: "GitCafe", |
| 238 | baseUrl: "https://git.cafe", |
| 239 | }, |
| 240 | remoteName: "origin", |
| 241 | remoteUrl, |
| 242 | }; |
| 243 | return Effect.gen(function* () { |
| 244 | const provider = yield* GitCafeSourceControlProvider.make; |
| 245 | for (const route of ["pull", "pulls"]) { |
| 246 | yield* provider.checkoutChangeRequest({ |
| 247 | cwd: "/repo", |
| 248 | reference: `https://git.cafe/team/project/${route}/7`, |
| 249 | ...(context === undefined ? {} : { context }), |
| 250 | }); |
| 251 | } |
| 252 | assert.deepStrictEqual(remotes, [expected, expected]); |
| 253 | }).pipe( |
| 254 | Effect.provide( |
| 255 | Layer.merge(GitCafeApi.layer, NoFiles).pipe( |
| 256 | Layer.provideMerge(GitCafeCredentials.layer), |
| 257 | Layer.provideMerge( |
| 258 | TestSourceControlHost.layer({ |
| 259 | git: { |
| 260 | ensureRemote: (input) => { |
| 261 | remotes.push(input.url); |
| 262 | return Effect.succeed("gitcafe"); |
| 263 | }, |
| 264 | listLocalBranchNames: () => Effect.succeed([]), |
| 265 | fetchRemoteBranch: () => Effect.void, |
| 266 | setBranchUpstream: () => Effect.void, |
| 267 | switchRef: () => Effect.succeed({ refName: null }), |
| 268 | }, |
| 269 | }), |
| 270 | ), |
| 271 | Layer.provide(Layer.succeed(HostProcess.Environment, { CAFE_TOKEN: "env-token" })), |
| 272 | Layer.provide(Layer.succeed(HostProcess.WorkingDirectory, "/server")), |
| 273 | Layer.provide(Layer.succeed(HttpClient.HttpClient, client)), |
| 274 | ), |
| 275 | ), |
| 276 | ); |
| 277 | }); |
| 278 | |
| 279 | it.effect("opens a pull request from a fork over REST with the body file's text", () => { |
| 280 | const sent: Array<{ method: string; url: string; body: unknown }> = []; |
| 281 | const client = HttpClient.make((request) => { |
| 282 | const body = |
| 283 | request.body._tag === "Uint8Array" |
| 284 | ? decodeJson(new TextDecoder().decode(request.body.body)) |
| 285 | : undefined; |
| 286 | sent.push({ method: request.method, url: request.url, body }); |
| 287 | return Effect.succeed( |
| 288 | HttpClientResponse.fromWeb( |
| 289 | request, |
| 290 | new Response( |
| 291 | encodeJson( |
| 292 | request.method === "POST" |
| 293 | ? { number: 3 } |
| 294 | : { id: "repo_fork", name: "project", defaultBranch: "main" }, |
| 295 | ), |
| 296 | ), |
| 297 | ), |
| 298 | ); |
| 299 | }); |
| 300 | return Effect.gen(function* () { |
| 301 | const provider = yield* GitCafeSourceControlProvider.make; |
| 302 | yield* provider.createChangeRequest({ |
| 303 | cwd: "/repo", |
| 304 | target: { repository: "team/project", refName: "main" }, |
| 305 | baseRefName: "main", |
| 306 | headSelector: "fork:feature", |
| 307 | title: "Add a thing", |
| 308 | bodyFile: "/tmp/body.md", |
| 309 | }); |
| 310 | assert.deepStrictEqual( |
| 311 | sent.map((request) => `${request.method} ${request.url}`), |
| 312 | [ |
| 313 | "GET https://git.cafe/api/repos/fork/project", |
| 314 | "POST https://git.cafe/api/repos/team/project/pulls", |
| 315 | ], |
| 316 | ); |
| 317 | assert.deepStrictEqual(sent[1]?.body, { |
| 318 | repo: "repo_fork", |
| 319 | source: "feature", |
| 320 | target: "main", |
| 321 | title: "Add a thing", |
| 322 | description: "Body", |
| 323 | }); |
| 324 | }).pipe( |
| 325 | Effect.provide( |
| 326 | layerFor(client, { |
| 327 | files: FileSystem.layerNoop({ |
| 328 | readFileString: (path) => |
| 329 | path === "/tmp/body.md" ? Effect.succeed("Body") : Effect.die(path), |
| 330 | }), |
| 331 | }), |
| 332 | ), |
| 333 | ); |
| 334 | }); |
| 335 | }); |