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 { normalizeGitRemoteUrl } from "@t3tools/shared/git"; |
| 3 | import { refineUnknownRemoteProvider } from "@t3tools/source-control-core/server/discovery"; |
| 4 | import { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 5 | import { assert, it } from "@effect/vitest"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | import { ChildProcessSpawner } from "effect/process"; |
| 11 | |
| 12 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 13 | import * as GitLabCli from "./GitLabCli.ts"; |
| 14 | import { parseGitLabAuthStatusHosts } from "./gitLabAuthStatus.ts"; |
| 15 | import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts"; |
| 16 | import { definition } from "../shared/definition.ts"; |
| 17 | |
| 18 | function makeProvider(gitlab: Partial<GitLabCli.GitLabCli["Service"]>) { |
| 19 | return GitLabSourceControlProvider.make.pipe( |
| 20 | Effect.provide( |
| 21 | Layer.mergeAll(Layer.mock(GitLabCli.GitLabCli)(gitlab), TestSourceControlHost.layer()), |
| 22 | ), |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | it("accepts authenticated GitLab hosts when another configured host fails", () => { |
| 27 | const auth = GitLabSourceControlProvider.discovery.parseAuth({ |
| 28 | exitCode: ChildProcessSpawner.ExitCode(1), |
| 29 | stdout: `gitlab.com |
| 30 | x gitlab.com: API call failed: 401 Unauthorized |
| 31 | ! No token found |
| 32 | self-hosted.example.test |
| 33 | ✓ Logged in to self-hosted.example.test as gitlab-user |
| 34 | ✓ Token found: ****** |
| 35 | `, |
| 36 | stderr: "", |
| 37 | }); |
| 38 | |
| 39 | assert.deepStrictEqual( |
| 40 | { |
| 41 | status: auth.status, |
| 42 | account: auth.account, |
| 43 | host: auth.host, |
| 44 | }, |
| 45 | { |
| 46 | status: "authenticated", |
| 47 | account: Option.some("gitlab-user"), |
| 48 | host: Option.some("self-hosted.example.test"), |
| 49 | }, |
| 50 | ); |
| 51 | }); |
| 52 | |
| 53 | it("refines unknown GitLab remotes with mixed-case provider hosts", () => { |
| 54 | const provider = GitLabSourceControlProvider.discovery.refineUnknownRemote?.({ |
| 55 | cwd: "/repo", |
| 56 | context: { |
| 57 | provider: { |
| 58 | kind: SourceControlProviderKind.make("unknown"), |
| 59 | name: "Self-Hosted.Example.Test", |
| 60 | baseUrl: "https://Self-Hosted.Example.Test", |
| 61 | }, |
| 62 | remoteName: "origin", |
| 63 | remoteUrl: "https://Self-Hosted.Example.Test/group/project.git", |
| 64 | }, |
| 65 | auth: { |
| 66 | exitCode: ChildProcessSpawner.ExitCode(0), |
| 67 | stdout: `self-hosted.example.test |
| 68 | ✓ Logged in to self-hosted.example.test as gitlab-user |
| 69 | ✓ Token found: ****** |
| 70 | `, |
| 71 | stderr: "", |
| 72 | }, |
| 73 | }); |
| 74 | |
| 75 | assert.deepStrictEqual(provider, { |
| 76 | kind: "gitlab", |
| 77 | name: "GitLab Self-Hosted", |
| 78 | baseUrl: "https://Self-Hosted.Example.Test", |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | it("tells a host glab has no login for apart from output that cannot say", () => { |
| 83 | const refine = (stderr: string) => |
| 84 | GitLabSourceControlProvider.discovery.refineUnknownRemote?.({ |
| 85 | cwd: "/repo", |
| 86 | context: { |
| 87 | provider: { |
| 88 | kind: SourceControlProviderKind.make("unknown"), |
| 89 | name: "git.example.test", |
| 90 | baseUrl: "https://git.example.test", |
| 91 | }, |
| 92 | remoteName: "origin", |
| 93 | remoteUrl: "https://git.example.test/group/project.git", |
| 94 | }, |
| 95 | auth: { exitCode: ChildProcessSpawner.ExitCode(1), stdout: "", stderr }, |
| 96 | }); |
| 97 | |
| 98 | assert.strictEqual( |
| 99 | refine("x git.example.test has not been authenticated with glab. Run `glab auth login`"), |
| 100 | null, |
| 101 | ); |
| 102 | assert.strictEqual( |
| 103 | refine("No GitLab instances have been authenticated with glab. Run `glab auth login`"), |
| 104 | null, |
| 105 | ); |
| 106 | assert.strictEqual(refine("git.example.test\n x API call failed: 503"), undefined); |
| 107 | }); |
| 108 | |
| 109 | // regression: #15395 self-hosted GitLab remotes resolve bare MR numbers once glab is signed in |
| 110 | it.effect.each([ |
| 111 | [ |
| 112 | "https://code.example.test:8443@Code.Example.Test:8443/Team/Repo.git", |
| 113 | true, |
| 114 | "gitlab", |
| 115 | "Code.Example.Test:8443", |
| 116 | ], |
| 117 | ["git@code.example.test:team/repo.git", true, "gitlab"], |
| 118 | ["https://code.example.test/team/repo.git", false, "unknown"], |
| 119 | ["https://github.com/team/repo.git", true, "github"], |
| 120 | ] as const)( |
| 121 | "resolves MR numbers for %s with authenticated=%s as %s", |
| 122 | ([remoteUrl, authenticated, expectedProvider, expectedAuthHost = "code.example.test"]) => |
| 123 | Effect.gen(function* () { |
| 124 | const provider = yield* makeProvider({}); |
| 125 | const identity = { |
| 126 | canonicalKey: normalizeGitRemoteUrl(remoteUrl), |
| 127 | locator: { source: "git-remote" as const, remoteName: "upstream", remoteUrl }, |
| 128 | rootPath: "/repo", |
| 129 | // Only a remote no host recognised is left for glab to refine. |
| 130 | provider: SourceControlProviderKind.make( |
| 131 | expectedProvider === "github" ? "github" : "unknown", |
| 132 | ), |
| 133 | }; |
| 134 | let authCalls = 0; |
| 135 | const refined = yield* ( |
| 136 | provider.refineRepositoryIdentity?.({ |
| 137 | identity, |
| 138 | resolveContext: (input) => |
| 139 | refineUnknownRemoteProvider({ |
| 140 | ...input, |
| 141 | specs: [GitLabSourceControlProvider.discovery], |
| 142 | process: { |
| 143 | run: ({ command, args, cwd }) => { |
| 144 | authCalls++; |
| 145 | assert.deepStrictEqual( |
| 146 | { command, args, cwd }, |
| 147 | { |
| 148 | command: "glab", |
| 149 | args: ["auth", "status", "--hostname", expectedAuthHost], |
| 150 | cwd: "/repo", |
| 151 | }, |
| 152 | ); |
| 153 | return Effect.succeed( |
| 154 | processOutput( |
| 155 | authenticated |
| 156 | ? `${expectedAuthHost}\n ✓ Logged in to ${expectedAuthHost} as gitlab-user\n` |
| 157 | : "x code.example.test has not been authenticated with glab.", |
| 158 | { exitCode: ChildProcessSpawner.ExitCode(authenticated ? 0 : 1) }, |
| 159 | ), |
| 160 | ); |
| 161 | }, |
| 162 | }, |
| 163 | }).pipe( |
| 164 | Effect.map(({ provider }) => |
| 165 | provider ? { ...input.context, provider } : input.context, |
| 166 | ), |
| 167 | ), |
| 168 | }) ?? Effect.succeed(identity) |
| 169 | ); |
| 170 | if (expectedProvider === "gitlab") { |
| 171 | const [host, ...repository] = refined.canonicalKey.split("/"); |
| 172 | assert.strictEqual( |
| 173 | definition.changeRequestUrl({ |
| 174 | host: host!, |
| 175 | repository: repository.join("/"), |
| 176 | number: 42, |
| 177 | }), |
| 178 | "https://code.example.test/team/repo/-/merge_requests/42", |
| 179 | ); |
| 180 | } |
| 181 | assert.deepStrictEqual(refined, { |
| 182 | ...identity, |
| 183 | provider: SourceControlProviderKind.make(expectedProvider), |
| 184 | }); |
| 185 | if (expectedProvider === "github") assert.strictEqual(authCalls, 0); |
| 186 | }), |
| 187 | ); |
| 188 | |
| 189 | it("parses authenticated GitLab auth status hosts with ports and single-label names", () => { |
| 190 | assert.deepStrictEqual( |
| 191 | parseGitLabAuthStatusHosts(`localhost:8080 |
| 192 | ✓ Logged in to localhost:8080 as local-user |
| 193 | selfhosted |
| 194 | ✓ Logged in to selfhosted as single-label-user |
| 195 | `), |
| 196 | [ |
| 197 | { host: "localhost:8080", account: "local-user" }, |
| 198 | { host: "selfhosted", account: "single-label-user" }, |
| 199 | ], |
| 200 | ); |
| 201 | }); |
| 202 | |
| 203 | it.effect( |
| 204 | "looks up a branch's merge requests a full page at a time, past forks' same-named branches", |
| 205 | () => |
| 206 | Effect.gen(function* () { |
| 207 | const provider = yield* makeProvider({}); |
| 208 | assert.deepStrictEqual( |
| 209 | provider.headBranchProbe?.({ |
| 210 | headSelectors: ["octocat:feature", "feature"], |
| 211 | state: "open", |
| 212 | }), |
| 213 | { headSelectors: ["feature"], limit: 100 }, |
| 214 | ); |
| 215 | }), |
| 216 | ); |
| 217 | |
| 218 | it.effect.each(["merge_requests", "issues"])( |
| 219 | "resolves %s subjects on the linked host without using the checkout", |
| 220 | (kind) => |
| 221 | Effect.gen(function* () { |
| 222 | const provider = yield* makeProvider({ |
| 223 | execute: (input) => { |
| 224 | assert.deepStrictEqual(input.args, [ |
| 225 | "api", |
| 226 | "--hostname", |
| 227 | "gitlab.com", |
| 228 | `projects/group%2Fsubgroup%2Fproject/${kind}/42`, |
| 229 | ]); |
| 230 | assert.strictEqual(input.maxOutputBytes, 32_000); |
| 231 | assert.strictEqual(input.timeoutMs, 3_000); |
| 232 | return Effect.succeed({ |
| 233 | exitCode: ChildProcessSpawner.ExitCode(0), |
| 234 | stdout: JSON.stringify({ |
| 235 | title: "Pairing expiry", |
| 236 | description: "Preserve remote access", |
| 237 | }), |
| 238 | stderr: "", |
| 239 | stdoutTruncated: false, |
| 240 | stderrTruncated: false, |
| 241 | }); |
| 242 | }, |
| 243 | }); |
| 244 | const lookup = provider.resolveLink?.({ |
| 245 | cwd: "/unrelated", |
| 246 | url: new URL(`https://gitlab.com/group/subgroup/project/-/${kind}/42`), |
| 247 | }); |
| 248 | assert.ok(lookup); |
| 249 | assert.deepStrictEqual(yield* lookup, { |
| 250 | title: "Pairing expiry", |
| 251 | body: "Preserve remote access", |
| 252 | }); |
| 253 | for (const url of [ |
| 254 | "https://gitlab.com/owner/repo", |
| 255 | "https://gitlab.attacker.test/team/project/-/issues/1", |
| 256 | ]) { |
| 257 | assert.strictEqual( |
| 258 | provider.resolveLink?.({ cwd: "/unrelated", url: new URL(url) }), |
| 259 | undefined, |
| 260 | ); |
| 261 | } |
| 262 | }), |
| 263 | ); |
| 264 | |
| 265 | it.effect.each(["read", "decode"] as const)( |
| 266 | "retains the %s failure without exposing its raw contents", |
| 267 | (stage) => |
| 268 | Effect.gen(function* () { |
| 269 | const cause = new GitLabCli.GitLabCliCommandError({ |
| 270 | command: "glab", |
| 271 | cwd: "/repo", |
| 272 | operation: "execute", |
| 273 | cause: new Error("private response text"), |
| 274 | }); |
| 275 | const provider = yield* makeProvider({ |
| 276 | execute: () => |
| 277 | stage === "read" |
| 278 | ? Effect.fail(cause) |
| 279 | : Effect.succeed({ |
| 280 | exitCode: ChildProcessSpawner.ExitCode(0), |
| 281 | stdout: "private response text", |
| 282 | stderr: "", |
| 283 | stdoutTruncated: false, |
| 284 | stderrTruncated: false, |
| 285 | }), |
| 286 | }); |
| 287 | const lookup = provider.resolveLink?.({ |
| 288 | cwd: "/repo", |
| 289 | url: new URL("https://gitlab.com/owner/repo/-/issues/42"), |
| 290 | }); |
| 291 | assert.ok(lookup); |
| 292 | const error = yield* Effect.flip(lookup); |
| 293 | assert.strictEqual(error.operation, stage === "read" ? "resolveLink" : "resolveLink.decode"); |
| 294 | assert.strictEqual(error.detail, "The linked subject could not be read."); |
| 295 | assert.notInclude(error.message, "private response text"); |
| 296 | if (stage === "read") assert.strictEqual(error.cause, cause); |
| 297 | else assert.propertyVal(error.cause, "_tag", "SchemaError"); |
| 298 | }), |
| 299 | ); |
| 300 | |
| 301 | it.effect.each([ |
| 302 | { headSelector: "octocat:feature/fix", source: undefined, sourceProject: "octocat/t3code" }, |
| 303 | { |
| 304 | headSelector: "feature/fix", |
| 305 | source: { owner: "team/subgroup", refName: "feature/fix" }, |
| 306 | sourceProject: "team/subgroup/t3code", |
| 307 | }, |
| 308 | { |
| 309 | headSelector: "feature/fix", |
| 310 | source: { repository: "octocat/renamed-fork", refName: "feature/fix" }, |
| 311 | sourceProject: "octocat/renamed-fork", |
| 312 | }, |
| 313 | ])("creates a fork MR from $sourceProject into the current project", (testCase) => |
| 314 | Effect.gen(function* () { |
| 315 | const requests: ReadonlyArray<string>[] = []; |
| 316 | const host = TestSourceControlHost.layer({ |
| 317 | process: { |
| 318 | run: (input) => { |
| 319 | requests.push(input.args); |
| 320 | if (input.args[1] === "projects/:fullpath") { |
| 321 | return Effect.succeed(processOutput(JSON.stringify({ id: 100, path: "t3code" }))); |
| 322 | } |
| 323 | assert.deepStrictEqual(input.args, [ |
| 324 | "api", |
| 325 | "--method", |
| 326 | "POST", |
| 327 | `projects/${encodeURIComponent(testCase.sourceProject)}/merge_requests`, |
| 328 | "--raw-field", |
| 329 | "source_branch=feature/fix", |
| 330 | "--raw-field", |
| 331 | "target_branch=main", |
| 332 | "--raw-field", |
| 333 | "target_project_id=100", |
| 334 | "--raw-field", |
| 335 | "title=Fix remote flow", |
| 336 | "--field", |
| 337 | "description=@-", |
| 338 | ]); |
| 339 | assert.strictEqual(input.stdin, "Body"); |
| 340 | return Effect.succeed(processOutput("{}")); |
| 341 | }, |
| 342 | }, |
| 343 | }); |
| 344 | const provider = yield* GitLabSourceControlProvider.make.pipe( |
| 345 | Effect.provide( |
| 346 | Layer.merge( |
| 347 | host, |
| 348 | GitLabCli.layer.pipe( |
| 349 | Layer.provide(host), |
| 350 | Layer.provide(FileSystem.layerNoop({ readFileString: () => Effect.succeed("Body") })), |
| 351 | ), |
| 352 | ), |
| 353 | ), |
| 354 | ); |
| 355 | yield* provider.createChangeRequest({ |
| 356 | cwd: "/repo", |
| 357 | baseRefName: "main", |
| 358 | headSelector: testCase.headSelector, |
| 359 | ...(testCase.source ? { source: testCase.source } : {}), |
| 360 | title: "Fix remote flow", |
| 361 | bodyFile: "/tmp/body.md", |
| 362 | }); |
| 363 | assert.strictEqual(requests.length, 2); |
| 364 | assert.deepStrictEqual(requests[0], ["api", "projects/:fullpath"]); |
| 365 | }), |
| 366 | ); |