The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, expect, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as TestClock from "effect/testing/TestClock"; |
| 6 | |
| 7 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 8 | |
| 9 | import * as GitLabCli from "./GitLabCli.ts"; |
| 10 | import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts"; |
| 11 | import { gitLabViewerPermissions, make } from "./GitLabPullRequestProvider.ts"; |
| 12 | |
| 13 | it.effect.each(["merge", "disable-auto-merge"] as const)( |
| 14 | "keeps reads and %s on the selected host when glab prefers another remote", |
| 15 | (action) => |
| 16 | Effect.gen(function* () { |
| 17 | const selectedHost = "gitlab.example.test:8443"; |
| 18 | const otherHost = "other.gitlab.example.test"; |
| 19 | const readHosts: string[] = []; |
| 20 | const mergedHosts: string[] = []; |
| 21 | const native = yield* GitLabCli.make.pipe( |
| 22 | Effect.provide( |
| 23 | TestSourceControlHost.layer({ |
| 24 | process: { |
| 25 | run: (input) => { |
| 26 | // glab prefers a remote named `gitlab` to `origin`. The checkout's |
| 27 | // selected repository must still own every request and mutation. |
| 28 | const host = input.env?.GITLAB_HOST ?? otherHost; |
| 29 | const path = input.args[1]; |
| 30 | const mutation = |
| 31 | input.args[0] === "mr" || (input.args.includes("POST") && path !== "graphql"); |
| 32 | if (mutation) mergedHosts.push(host); |
| 33 | else readHosts.push(host); |
| 34 | const response = mutation |
| 35 | ? {} |
| 36 | : path === "graphql" |
| 37 | ? { |
| 38 | data: { |
| 39 | project: { |
| 40 | mergeRequests: { |
| 41 | nodes: [ |
| 42 | { iid: "7", diffStatsSummary: { additions: 2, deletions: 1 } }, |
| 43 | ], |
| 44 | }, |
| 45 | mergeRequest: { |
| 46 | userPermissions: { adminMergeRequest: true }, |
| 47 | diffStatsSummary: { fileCount: 1 }, |
| 48 | diffStats: [{ path: "src/a.ts", additions: 2, deletions: 1 }], |
| 49 | }, |
| 50 | }, |
| 51 | }, |
| 52 | } |
| 53 | : path?.includes("merge_requests/") |
| 54 | ? { |
| 55 | iid: 7, |
| 56 | title: "Merge request 7", |
| 57 | web_url: `https://${host}/acme/web/-/merge_requests/7`, |
| 58 | source_branch: "feature", |
| 59 | target_branch: "main", |
| 60 | created_at: "2026-07-01T00:00:00Z", |
| 61 | updated_at: "2026-07-02T00:00:00Z", |
| 62 | user: { can_merge: true }, |
| 63 | diff_refs: { |
| 64 | head_sha: "a".repeat(40), |
| 65 | base_sha: "b".repeat(40), |
| 66 | start_sha: "b".repeat(40), |
| 67 | }, |
| 68 | } |
| 69 | : path === "user" |
| 70 | ? { username: "reviewer" } |
| 71 | : path?.includes("/labels?") || path?.includes("/users?") |
| 72 | ? [] |
| 73 | : { merge_method: "merge", squash_option: "default_off" }; |
| 74 | return Effect.succeed( |
| 75 | TestSourceControlHost.processOutput(JSON.stringify(response)), |
| 76 | ); |
| 77 | }, |
| 78 | }, |
| 79 | }).pipe(Layer.merge(FileSystem.layerNoop({}))), |
| 80 | ), |
| 81 | ); |
| 82 | const cli = yield* GitLabPullRequestCli.make.pipe( |
| 83 | Effect.provideService(GitLabCli.GitLabCli, native), |
| 84 | ); |
| 85 | const provider = yield* make.pipe( |
| 86 | Effect.provideService(GitLabPullRequestCli.GitLabPullRequestCli, cli), |
| 87 | ); |
| 88 | const reference = { |
| 89 | cwd: "/repo", |
| 90 | repository: "acme/web", |
| 91 | host: selectedHost, |
| 92 | number: 7, |
| 93 | }; |
| 94 | const detail = yield* provider.getChangeRequest(reference); |
| 95 | expect({ additions: detail.additions, deletions: detail.deletions }).toEqual({ |
| 96 | additions: 2, |
| 97 | deletions: 1, |
| 98 | }); |
| 99 | assert.isDefined(provider.getChangeRequestSummary); |
| 100 | expect((yield* provider.getChangeRequestSummary(reference)).additions).toBe(2); |
| 101 | assert.isDefined(provider.listChangeRequestStats); |
| 102 | expect( |
| 103 | yield* provider.listChangeRequestStats({ ...reference, changeRequests: [reference] }), |
| 104 | ).toEqual([{ repository: reference.repository, number: 7, additions: 2, deletions: 1 }]); |
| 105 | assert.isDefined(provider.getFileStats); |
| 106 | expect(yield* provider.getFileStats(reference)).toEqual({ |
| 107 | files: [{ path: "src/a.ts", additions: 2, deletions: 1 }], |
| 108 | truncated: false, |
| 109 | }); |
| 110 | expect(yield* provider.getViewer(reference)).toBe("reviewer"); |
| 111 | assert.isDefined(provider.listRepositoryLabels); |
| 112 | yield* provider.listRepositoryLabels(reference); |
| 113 | assert.isDefined(provider.searchRepositoryAuthors); |
| 114 | yield* provider.searchRepositoryAuthors(reference); |
| 115 | yield* provider.runAction({ ...reference, action }); |
| 116 | |
| 117 | expect(readHosts).toEqual(Array(action === "merge" ? 14 : 13).fill(selectedHost)); |
| 118 | expect({ url: detail.url, mutatedHosts: mergedHosts }).toEqual({ |
| 119 | url: `https://${selectedHost}/acme/web/-/merge_requests/7`, |
| 120 | mutatedHosts: [selectedHost], |
| 121 | }); |
| 122 | }), |
| 123 | ); |
| 124 | |
| 125 | describe("gitLabViewerPermissions", () => { |
| 126 | it("offers everything to a viewer GitLab says can merge", () => { |
| 127 | expect( |
| 128 | gitLabViewerPermissions({ |
| 129 | viewerCanMerge: true, |
| 130 | viewerCanDeleteBranch: true, |
| 131 | viewerCanEditLabels: true, |
| 132 | }), |
| 133 | ).toEqual({ |
| 134 | actions: [ |
| 135 | "merge", |
| 136 | "ready", |
| 137 | "draft", |
| 138 | "close", |
| 139 | "reopen", |
| 140 | "update-branch", |
| 141 | "enable-auto-merge", |
| 142 | "disable-auto-merge", |
| 143 | "revert", |
| 144 | "delete-branch", |
| 145 | ], |
| 146 | comment: true, |
| 147 | resolve: true, |
| 148 | verdicts: ["comment", "approve", "request-changes"], |
| 149 | requestReviewers: true, |
| 150 | maintainerEdits: false, |
| 151 | labels: true, |
| 152 | updateMethods: ["rebase"], |
| 153 | }); |
| 154 | }); |
| 155 | |
| 156 | it("keeps merge, now and later, from a viewer GitLab says cannot", () => { |
| 157 | expect(gitLabViewerPermissions({ viewerCanMerge: false })).toEqual({ |
| 158 | actions: ["ready", "draft", "close", "reopen"], |
| 159 | comment: true, |
| 160 | resolve: true, |
| 161 | verdicts: ["comment", "approve", "request-changes"], |
| 162 | requestReviewers: true, |
| 163 | maintainerEdits: false, |
| 164 | labels: false, |
| 165 | }); |
| 166 | }); |
| 167 | }); |
| 168 | |
| 169 | describe("getChangeRequest base freshness", () => { |
| 170 | const detail = { |
| 171 | number: 7, |
| 172 | title: "Merge request 7", |
| 173 | url: "https://gitlab.com/acme/web/-/merge_requests/7", |
| 174 | author: null, |
| 175 | headBranch: "feat/page", |
| 176 | baseBranch: "main", |
| 177 | state: "open" as const, |
| 178 | isDraft: false, |
| 179 | mergeability: "mergeable" as const, |
| 180 | additions: 0, |
| 181 | deletions: 0, |
| 182 | createdAt: "2026-07-01T00:00:00Z", |
| 183 | updatedAt: "2026-07-02T00:00:00Z", |
| 184 | reviewRequestLogins: [], |
| 185 | labels: [], |
| 186 | body: "", |
| 187 | changedFiles: 1, |
| 188 | mergedAt: null, |
| 189 | closedAt: null, |
| 190 | reviewers: [], |
| 191 | checks: [], |
| 192 | viewerCanMerge: true, |
| 193 | reviewerIds: [], |
| 194 | reviewerUsers: [], |
| 195 | }; |
| 196 | |
| 197 | it.effect.each(["getChangeRequestPreview", "getChangeRequestSummary"] as const)( |
| 198 | "%s preserves merge request fields without fetching project settings or fork permissions", |
| 199 | (operation) => |
| 200 | Effect.gen(function* () { |
| 201 | const detailInputs: Array<{ readonly maintainerEdits?: boolean }> = []; |
| 202 | const provider = yield* make.pipe( |
| 203 | Effect.provide( |
| 204 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 205 | getMergeRequestDetail: (input) => { |
| 206 | detailInputs.push(input); |
| 207 | return Effect.succeed(detail); |
| 208 | }, |
| 209 | getListStates: () => Effect.succeed(new Map()), |
| 210 | |
| 211 | listMergeRequestStats: () => Effect.succeed([]), |
| 212 | }), |
| 213 | ), |
| 214 | ); |
| 215 | const read = provider[operation]; |
| 216 | assert.isDefined(read); |
| 217 | expect( |
| 218 | yield* read({ cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 }), |
| 219 | ).toEqual(detail); |
| 220 | expect(detailInputs.map((input) => input.maintainerEdits)).toEqual([false]); |
| 221 | }), |
| 222 | ); |
| 223 | |
| 224 | const readWith = ( |
| 225 | divergence: { readonly divergedCommits?: number }, |
| 226 | stats: GitLabPullRequestCli.GitLabPullRequestCli["Service"]["listMergeRequestStats"] = () => |
| 227 | Effect.succeed([]), |
| 228 | ) => |
| 229 | Effect.gen(function* () { |
| 230 | const provider = yield* make; |
| 231 | return yield* provider.getChangeRequest({ |
| 232 | cwd: "/w", |
| 233 | repository: "acme/web", |
| 234 | host: "gitlab.com", |
| 235 | number: 7, |
| 236 | }); |
| 237 | }).pipe( |
| 238 | Effect.provide( |
| 239 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 240 | getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }), |
| 241 | getProjectMergeCapabilities: () => |
| 242 | Effect.succeed({ merge: true, squash: true, rebase: true }), |
| 243 | listMergeRequestStats: stats, |
| 244 | }), |
| 245 | ), |
| 246 | ); |
| 247 | |
| 248 | it.effect("reads a counted divergence as a branch that has fallen behind", () => |
| 249 | Effect.gen(function* () { |
| 250 | const changeRequest = yield* readWith({ divergedCommits: 3 }); |
| 251 | |
| 252 | expect(changeRequest.baseComparison).toBe("behind"); |
| 253 | expect(changeRequest.behindBy).toBe(3); |
| 254 | }), |
| 255 | ); |
| 256 | |
| 257 | it.effect("says nothing at all where GitLab counted nothing", () => |
| 258 | Effect.gen(function* () { |
| 259 | const changeRequest = yield* readWith({}); |
| 260 | |
| 261 | expect(changeRequest.baseComparison).toBe("unknown"); |
| 262 | expect(changeRequest.behindBy).toBeUndefined(); |
| 263 | }), |
| 264 | ); |
| 265 | |
| 266 | it.effect("fills in the line counts the REST detail leaves out", () => |
| 267 | Effect.gen(function* () { |
| 268 | const changeRequest = yield* readWith({}, () => |
| 269 | Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]), |
| 270 | ); |
| 271 | |
| 272 | expect(changeRequest.additions).toBe(40); |
| 273 | expect(changeRequest.deletions).toBe(2); |
| 274 | }), |
| 275 | ); |
| 276 | |
| 277 | it.effect("reports an unauthenticated line count read instead of a detail without counts", () => |
| 278 | Effect.gen(function* () { |
| 279 | const result = yield* Effect.result( |
| 280 | readWith({}, () => |
| 281 | Effect.fail( |
| 282 | new GitLabCli.GitLabCliAuthenticationError({ |
| 283 | operation: "execute", |
| 284 | command: "glab", |
| 285 | cwd: "/w", |
| 286 | cause: null, |
| 287 | }), |
| 288 | ), |
| 289 | ), |
| 290 | ); |
| 291 | |
| 292 | expect(result).toMatchObject({ |
| 293 | _tag: "Failure", |
| 294 | failure: { _tag: "PullRequestProviderError", reason: "unauthenticated" }, |
| 295 | }); |
| 296 | }), |
| 297 | ); |
| 298 | |
| 299 | it.effect("gives linked threads line counts without reading the merge settings", () => |
| 300 | Effect.gen(function* () { |
| 301 | const summary = yield* Effect.gen(function* () { |
| 302 | const provider = yield* make; |
| 303 | const read = provider.getChangeRequestSummary; |
| 304 | if (read === undefined) return yield* Effect.die("summary read missing"); |
| 305 | return yield* read({ cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 }); |
| 306 | }).pipe( |
| 307 | Effect.provide( |
| 308 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 309 | getMergeRequestDetail: () => Effect.succeed(detail), |
| 310 | getProjectMergeCapabilities: () => Effect.die("linked threads skip merge settings"), |
| 311 | getListStates: () => Effect.succeed(new Map()), |
| 312 | listMergeRequestStats: () => |
| 313 | Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]), |
| 314 | }), |
| 315 | ), |
| 316 | ); |
| 317 | |
| 318 | expect(summary.additions).toBe(40); |
| 319 | expect(summary.deletions).toBe(2); |
| 320 | }), |
| 321 | ); |
| 322 | }); |
| 323 | |
| 324 | describe("merge request reads", () => { |
| 325 | const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 }; |
| 326 | const detail = { |
| 327 | number: 7, |
| 328 | title: "Merge request 7", |
| 329 | url: "https://gitlab.com/acme/web/-/merge_requests/7", |
| 330 | author: null, |
| 331 | headBranch: "feat/page", |
| 332 | baseBranch: "main", |
| 333 | state: "open" as const, |
| 334 | isDraft: false, |
| 335 | mergeability: "mergeable" as const, |
| 336 | additions: 0, |
| 337 | deletions: 0, |
| 338 | createdAt: "2026-07-01T00:00:00Z", |
| 339 | updatedAt: "2026-07-02T00:00:00Z", |
| 340 | reviewRequestLogins: [], |
| 341 | labels: [], |
| 342 | body: "", |
| 343 | changedFiles: 1, |
| 344 | mergedAt: null, |
| 345 | closedAt: null, |
| 346 | reviewers: [], |
| 347 | checks: [], |
| 348 | viewerCanMerge: true, |
| 349 | reviewerIds: [], |
| 350 | reviewerUsers: [], |
| 351 | }; |
| 352 | |
| 353 | const countingProvider = Effect.gen(function* () { |
| 354 | const calls = { detail: 0, mergeCapabilities: 0 }; |
| 355 | const host = { viewerCanMerge: true }; |
| 356 | const provider = yield* make.pipe( |
| 357 | Effect.provide( |
| 358 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 359 | listMergeRequestStats: () => Effect.succeed([]), |
| 360 | getListStates: () => Effect.succeed(new Map()), |
| 361 | getMergeRequestDetail: (input) => |
| 362 | Effect.sync(() => { |
| 363 | calls.detail += 1; |
| 364 | return { ...detail, number: input.number, viewerCanMerge: host.viewerCanMerge }; |
| 365 | }), |
| 366 | getProjectMergeCapabilities: () => |
| 367 | Effect.sync(() => { |
| 368 | calls.mergeCapabilities += 1; |
| 369 | return { merge: true, squash: true, rebase: true }; |
| 370 | }), |
| 371 | runMergeRequestAction: () => Effect.void, |
| 372 | setLabels: () => Effect.void, |
| 373 | }), |
| 374 | ), |
| 375 | ); |
| 376 | const checks = provider.getChangeRequestChecks; |
| 377 | const forget = provider.forgetChangeRequestReads; |
| 378 | if (checks === undefined || forget === undefined) { |
| 379 | return yield* Effect.die("GitLab answers checks and forgets its reads"); |
| 380 | } |
| 381 | return { provider, checks, forget, calls, host }; |
| 382 | }); |
| 383 | |
| 384 | it.effect("asks for a project's merge methods once across its merge requests", () => |
| 385 | Effect.gen(function* () { |
| 386 | const { provider, calls } = yield* countingProvider; |
| 387 | |
| 388 | yield* provider.getChangeRequest(reference); |
| 389 | yield* provider.getChangeRequest(reference); |
| 390 | yield* provider.getChangeRequest({ ...reference, number: 8 }); |
| 391 | |
| 392 | expect(calls.mergeCapabilities).toBe(1); |
| 393 | }), |
| 394 | ); |
| 395 | |
| 396 | it.effect("answers checks from the merge request the detail just read", () => |
| 397 | Effect.gen(function* () { |
| 398 | const { provider, checks, calls } = yield* countingProvider; |
| 399 | |
| 400 | yield* provider.getChangeRequest(reference); |
| 401 | yield* checks(reference); |
| 402 | |
| 403 | expect(calls.detail).toBe(1); |
| 404 | |
| 405 | const summary = provider.getChangeRequestSummary; |
| 406 | assert.isDefined(summary); |
| 407 | yield* summary(reference); |
| 408 | expect(calls.detail).toBe(2); |
| 409 | }), |
| 410 | ); |
| 411 | |
| 412 | it.effect.each([ |
| 413 | { |
| 414 | write: "runAction", |
| 415 | run: (provider: Effect.Success<typeof make>) => |
| 416 | provider.runAction({ ...reference, action: "ready" }), |
| 417 | }, |
| 418 | { |
| 419 | write: "setLabels", |
| 420 | run: (provider: Effect.Success<typeof make>) => |
| 421 | provider.setLabels?.({ ...reference, labels: ["bug"], applied: true }) ?? Effect.void, |
| 422 | }, |
| 423 | ])("reads a merge request again after $write", ({ run }) => |
| 424 | Effect.gen(function* () { |
| 425 | const { provider, checks, calls } = yield* countingProvider; |
| 426 | |
| 427 | yield* provider.getChangeRequest(reference); |
| 428 | yield* run(provider); |
| 429 | yield* checks(reference); |
| 430 | |
| 431 | expect(calls.detail).toBe(2); |
| 432 | }), |
| 433 | ); |
| 434 | |
| 435 | it.effect.each([{ cwd: "/other" }, { host: "other.gitlab.test" }])( |
| 436 | "does not share reads with %j", |
| 437 | (other) => |
| 438 | Effect.gen(function* () { |
| 439 | const { provider, calls } = yield* countingProvider; |
| 440 | |
| 441 | yield* provider.getChangeRequest(reference); |
| 442 | yield* provider.getChangeRequest({ ...reference, ...other }); |
| 443 | yield* provider.getChangeRequest(reference); |
| 444 | yield* provider.getChangeRequest({ ...reference, ...other }); |
| 445 | |
| 446 | expect(calls).toEqual({ detail: 2, mergeCapabilities: 2 }); |
| 447 | }), |
| 448 | ); |
| 449 | |
| 450 | it.effect("reads a merge request again once a minute has passed", () => |
| 451 | Effect.gen(function* () { |
| 452 | const { provider, calls } = yield* countingProvider; |
| 453 | |
| 454 | yield* provider.getChangeRequest(reference); |
| 455 | yield* TestClock.adjust("61 seconds"); |
| 456 | yield* provider.getChangeRequest(reference); |
| 457 | |
| 458 | expect(calls.detail).toBe(2); |
| 459 | }), |
| 460 | ); |
| 461 | |
| 462 | it.effect("answers viewer permissions from the host, not the page's read", () => |
| 463 | Effect.gen(function* () { |
| 464 | const { provider, host } = yield* countingProvider; |
| 465 | |
| 466 | host.viewerCanMerge = false; |
| 467 | yield* provider.getChangeRequest(reference); |
| 468 | host.viewerCanMerge = true; |
| 469 | const granted = yield* provider.getViewerPermissions(reference); |
| 470 | host.viewerCanMerge = false; |
| 471 | const revoked = yield* provider.getViewerPermissions(reference); |
| 472 | |
| 473 | expect(granted.actions).toContain("merge"); |
| 474 | expect(revoked.actions).not.toContain("merge"); |
| 475 | }), |
| 476 | ); |
| 477 | |
| 478 | it.effect("reads a merge request again once its reads are forgotten", () => |
| 479 | Effect.gen(function* () { |
| 480 | const { provider, checks, forget, calls } = yield* countingProvider; |
| 481 | const other = { ...reference, repository: "acme/api" }; |
| 482 | |
| 483 | yield* provider.getChangeRequest(reference); |
| 484 | yield* provider.getChangeRequest(other); |
| 485 | yield* forget({ repository: "ACME/web", number: 7 }); |
| 486 | yield* checks(reference); |
| 487 | yield* checks(other); |
| 488 | expect(calls.detail).toBe(3); |
| 489 | |
| 490 | yield* forget({ repository: "acme/web" }); |
| 491 | yield* checks(reference); |
| 492 | yield* forget(); |
| 493 | yield* checks(other); |
| 494 | expect(calls.detail).toBe(5); |
| 495 | }), |
| 496 | ); |
| 497 | }); |
| 498 | |
| 499 | describe("optional activity reads", () => { |
| 500 | const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 }; |
| 501 | const comment = { |
| 502 | id: "42", |
| 503 | kind: "issue-comment" as const, |
| 504 | author: null, |
| 505 | body: "Keep this comment", |
| 506 | createdAt: "2026-07-01T00:00:00Z", |
| 507 | url: null, |
| 508 | path: null, |
| 509 | reviewState: null, |
| 510 | }; |
| 511 | const commit = { |
| 512 | oid: "abc", |
| 513 | messageHeadline: "Keep this commit", |
| 514 | committedDate: "2026-07-01T00:00:00Z", |
| 515 | }; |
| 516 | const webUrl = "https://git.acme.dev/acme/web/-/merge_requests/7"; |
| 517 | const currentRefs = { baseSha: "base2", startSha: "start2", headSha: "head2" }; |
| 518 | const reads = { |
| 519 | getMergeRequestDetail: () => |
| 520 | Effect.succeed({ |
| 521 | number: 7, |
| 522 | title: "Merge request 7", |
| 523 | url: webUrl, |
| 524 | author: null, |
| 525 | headBranch: "feat/page", |
| 526 | baseBranch: "main", |
| 527 | state: "open" as const, |
| 528 | isDraft: false, |
| 529 | mergeability: "mergeable" as const, |
| 530 | additions: 0, |
| 531 | deletions: 0, |
| 532 | createdAt: "2026-07-01T00:00:00Z", |
| 533 | updatedAt: "2026-07-02T00:00:00Z", |
| 534 | reviewRequestLogins: [], |
| 535 | labels: [], |
| 536 | body: "", |
| 537 | changedFiles: 1, |
| 538 | mergedAt: null, |
| 539 | closedAt: null, |
| 540 | reviewers: [], |
| 541 | checks: [], |
| 542 | viewerCanMerge: true, |
| 543 | reviewerIds: [], |
| 544 | reviewerUsers: [], |
| 545 | diffRefs: currentRefs, |
| 546 | }), |
| 547 | listNotes: () => Effect.succeed({ comments: [comment], truncated: false }), |
| 548 | listCommits: () => Effect.succeed([commit]), |
| 549 | listDiscussions: () => |
| 550 | Effect.succeed({ |
| 551 | threads: [], |
| 552 | diffRefsByThreadId: new Map(), |
| 553 | overviewFindingIds: [], |
| 554 | truncated: false, |
| 555 | }), |
| 556 | listReactions: () => |
| 557 | Effect.succeed({ |
| 558 | reactions: [], |
| 559 | reactionsByNoteId: new Map(), |
| 560 | editedAtByNoteId: new Map(), |
| 561 | botLogins: new Set<string>(), |
| 562 | }), |
| 563 | }; |
| 564 | const context = { |
| 565 | operation: "execute" as const, |
| 566 | command: "glab" as const, |
| 567 | cwd: "/w", |
| 568 | cause: null, |
| 569 | }; |
| 570 | |
| 571 | it.effect.each([ |
| 572 | { |
| 573 | operation: "listNotes", |
| 574 | error: new GitLabCli.GitLabCliAuthenticationError(context), |
| 575 | reason: "unauthenticated", |
| 576 | }, |
| 577 | { |
| 578 | operation: "listCommits", |
| 579 | error: new GitLabCli.GitLabCliUnavailableError(context), |
| 580 | reason: "missing-tool", |
| 581 | }, |
| 582 | ] as const)( |
| 583 | "reports $reason instead of successful partial activity", |
| 584 | ({ operation, error, reason }) => |
| 585 | Effect.gen(function* () { |
| 586 | const provider = yield* make.pipe( |
| 587 | Effect.provide( |
| 588 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 589 | ...reads, |
| 590 | [operation]: () => Effect.fail(error), |
| 591 | }), |
| 592 | ), |
| 593 | ); |
| 594 | |
| 595 | expect(yield* Effect.result(provider.getChangeRequestActivity(reference))).toMatchObject({ |
| 596 | _tag: "Failure", |
| 597 | failure: { |
| 598 | _tag: "PullRequestProviderError", |
| 599 | provider: "gitlab", |
| 600 | operation: "getChangeRequestActivity", |
| 601 | reason, |
| 602 | detail: error.detail, |
| 603 | cause: error, |
| 604 | }, |
| 605 | }); |
| 606 | }), |
| 607 | ); |
| 608 | |
| 609 | it.effect( |
| 610 | "turns an open overview discussion into a finding and a thread apart from file threads", |
| 611 | () => |
| 612 | Effect.gen(function* () { |
| 613 | const finding = { ...comment, id: "10", body: "Mention the flag in the README" }; |
| 614 | const reply = { ...comment, id: "11", body: "Will do" }; |
| 615 | const thread = { |
| 616 | side: "right", |
| 617 | isResolved: false, |
| 618 | isOutdated: false, |
| 619 | comments: [], |
| 620 | } as const; |
| 621 | const provider = yield* make.pipe( |
| 622 | Effect.provide( |
| 623 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 624 | ...reads, |
| 625 | listNotes: () => |
| 626 | Effect.succeed({ comments: [comment, finding, reply], truncated: false }), |
| 627 | listDiscussions: () => |
| 628 | Effect.succeed({ |
| 629 | threads: [ |
| 630 | { ...thread, id: "overview", path: null, line: null }, |
| 631 | { ...thread, id: "file", path: "src/a.ts", line: 3 }, |
| 632 | ], |
| 633 | diffRefsByThreadId: new Map(), |
| 634 | overviewFindingIds: ["10"], |
| 635 | truncated: false, |
| 636 | }), |
| 637 | }), |
| 638 | ), |
| 639 | ); |
| 640 | |
| 641 | const { comments, reviewThreads, overviewThreads } = |
| 642 | yield* provider.getChangeRequestActivity(reference); |
| 643 | |
| 644 | // Clients that require a thread path must never receive one without it. |
| 645 | expect(reviewThreads.map(({ id }) => id)).toEqual(["file"]); |
| 646 | expect(overviewThreads?.map(({ id }) => id)).toEqual(["overview"]); |
| 647 | |
| 648 | expect(comments.map((item) => [item.id, item.kind])).toEqual([ |
| 649 | ["42", "issue-comment"], |
| 650 | ["10", "review-comment"], |
| 651 | ["11", "issue-comment"], |
| 652 | ]); |
| 653 | }), |
| 654 | ); |
| 655 | |
| 656 | it.effect("dates edited comments in the conversation and in threads", () => |
| 657 | Effect.gen(function* () { |
| 658 | const editedAt = "2026-07-02T00:00:00Z"; |
| 659 | const provider = yield* make.pipe( |
| 660 | Effect.provide( |
| 661 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 662 | ...reads, |
| 663 | listDiscussions: () => |
| 664 | Effect.succeed({ |
| 665 | threads: [ |
| 666 | { |
| 667 | id: "d1", |
| 668 | path: "src/a.ts", |
| 669 | line: 1, |
| 670 | side: "right" as const, |
| 671 | isResolved: false, |
| 672 | isOutdated: false, |
| 673 | comments: [comment], |
| 674 | }, |
| 675 | ], |
| 676 | diffRefsByThreadId: new Map(), |
| 677 | overviewFindingIds: [], |
| 678 | truncated: false, |
| 679 | }), |
| 680 | listReactions: () => |
| 681 | Effect.succeed({ |
| 682 | reactions: [], |
| 683 | reactionsByNoteId: new Map(), |
| 684 | editedAtByNoteId: new Map([[comment.id, editedAt]]), |
| 685 | botLogins: new Set<string>(), |
| 686 | }), |
| 687 | }), |
| 688 | ), |
| 689 | ); |
| 690 | |
| 691 | const activity = yield* provider.getChangeRequestActivity(reference); |
| 692 | |
| 693 | expect(activity.comments[0]?.editedAt).toBe(editedAt); |
| 694 | expect(activity.reviewThreads[0]?.comments[0]?.editedAt).toBe(editedAt); |
| 695 | }), |
| 696 | ); |
| 697 | |
| 698 | it.effect("links comments and thread comments to their note on the merge request page", () => |
| 699 | Effect.gen(function* () { |
| 700 | const provider = yield* make.pipe( |
| 701 | Effect.provide( |
| 702 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 703 | ...reads, |
| 704 | listDiscussions: () => |
| 705 | Effect.succeed({ |
| 706 | threads: [ |
| 707 | { |
| 708 | id: "d1", |
| 709 | path: "src/a.ts", |
| 710 | line: 1, |
| 711 | side: "right" as const, |
| 712 | isResolved: false, |
| 713 | isOutdated: false, |
| 714 | comments: [comment], |
| 715 | }, |
| 716 | ], |
| 717 | diffRefsByThreadId: new Map(), |
| 718 | overviewFindingIds: [], |
| 719 | truncated: false, |
| 720 | }), |
| 721 | }), |
| 722 | ), |
| 723 | ); |
| 724 | |
| 725 | const activity = yield* provider.getChangeRequestActivity(reference); |
| 726 | |
| 727 | expect(activity.comments[0]?.url).toBe(`${webUrl}#note_42`); |
| 728 | expect(activity.reviewThreads[0]?.comments[0]?.url).toBe(`${webUrl}#note_42`); |
| 729 | }), |
| 730 | ); |
| 731 | |
| 732 | it.effect("marks a thread outdated once the merge request's diff moves past it", () => |
| 733 | Effect.gen(function* () { |
| 734 | let detailReads = 0; |
| 735 | const thread = (id: string) => ({ |
| 736 | id, |
| 737 | path: "src/a.ts", |
| 738 | line: 3, |
| 739 | side: "right" as const, |
| 740 | isResolved: false, |
| 741 | isOutdated: false, |
| 742 | comments: [], |
| 743 | }); |
| 744 | const provider = yield* make.pipe( |
| 745 | Effect.provide( |
| 746 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 747 | ...reads, |
| 748 | getMergeRequestDetail: () => |
| 749 | Effect.map(reads.getMergeRequestDetail(), (detail) => ({ |
| 750 | ...detail, |
| 751 | diffRefs: ++detailReads === 1 ? { ...currentRefs, headSha: "head1" } : currentRefs, |
| 752 | })), |
| 753 | listDiscussions: () => |
| 754 | Effect.succeed({ |
| 755 | threads: [thread("current"), thread("moved"), thread("unplaced")], |
| 756 | diffRefsByThreadId: new Map([ |
| 757 | ["current", currentRefs], |
| 758 | ["moved", { ...currentRefs, headSha: "head1" }], |
| 759 | ]), |
| 760 | overviewFindingIds: [], |
| 761 | truncated: false, |
| 762 | }), |
| 763 | }), |
| 764 | ), |
| 765 | ); |
| 766 | |
| 767 | const checks = provider.getChangeRequestChecks; |
| 768 | if (!checks) throw new Error("missing getChangeRequestChecks"); |
| 769 | yield* checks(reference); |
| 770 | const { reviewThreads } = yield* provider.getChangeRequestActivity(reference); |
| 771 | expect(reviewThreads.map(({ id, isOutdated }) => [id, isOutdated])).toEqual([ |
| 772 | ["current", false], |
| 773 | ["moved", true], |
| 774 | ["unplaced", false], |
| 775 | ]); |
| 776 | }), |
| 777 | ); |
| 778 | |
| 779 | it.effect("keeps comments and commits when optional discussions, reactions and links fail", () => |
| 780 | Effect.gen(function* () { |
| 781 | const provider = yield* make.pipe( |
| 782 | Effect.provide( |
| 783 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 784 | ...reads, |
| 785 | listDiscussions: () => |
| 786 | Effect.fail( |
| 787 | new GitLabPullRequestCli.GitLabMergeRequestReadError({ |
| 788 | command: "glab", |
| 789 | cwd: "/w", |
| 790 | operation: "listDiscussions", |
| 791 | cause: null, |
| 792 | }), |
| 793 | ), |
| 794 | listReactions: () => Effect.fail(new GitLabCli.GitLabCliCommandError(context)), |
| 795 | getMergeRequestDetail: () => Effect.fail(new GitLabCli.GitLabCliCommandError(context)), |
| 796 | }), |
| 797 | ), |
| 798 | ); |
| 799 | |
| 800 | expect(yield* provider.getChangeRequestActivity(reference)).toEqual({ |
| 801 | comments: [{ ...comment, reactions: [] }], |
| 802 | commentCount: 1, |
| 803 | commentsTruncated: true, |
| 804 | reviewThreads: [], |
| 805 | overviewThreads: [], |
| 806 | reactions: [], |
| 807 | commits: [commit], |
| 808 | }); |
| 809 | }), |
| 810 | ); |
| 811 | |
| 812 | it.effect("flags comments by bot authors as bots, in the conversation and in threads", () => |
| 813 | Effect.gen(function* () { |
| 814 | const bot = { login: "project_1_bot_abc", name: "Review bot", avatarUrl: null }; |
| 815 | const human = { login: "julius", name: null, avatarUrl: null }; |
| 816 | const provider = yield* make.pipe( |
| 817 | Effect.provide( |
| 818 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 819 | ...reads, |
| 820 | listNotes: () => |
| 821 | Effect.succeed({ |
| 822 | comments: [ |
| 823 | { ...comment, author: bot }, |
| 824 | { ...comment, id: "43", author: human }, |
| 825 | ], |
| 826 | truncated: false, |
| 827 | }), |
| 828 | listDiscussions: () => |
| 829 | Effect.succeed({ |
| 830 | threads: [ |
| 831 | { |
| 832 | id: "d1", |
| 833 | path: "a.ts", |
| 834 | line: 1, |
| 835 | side: "right" as const, |
| 836 | isResolved: false, |
| 837 | isOutdated: false, |
| 838 | comments: [ |
| 839 | { id: "44", author: bot, body: "x", createdAt: comment.createdAt, url: null }, |
| 840 | ], |
| 841 | }, |
| 842 | ], |
| 843 | diffRefsByThreadId: new Map(), |
| 844 | overviewFindingIds: [], |
| 845 | truncated: false, |
| 846 | }), |
| 847 | listReactions: () => |
| 848 | Effect.succeed({ |
| 849 | reactions: [], |
| 850 | reactionsByNoteId: new Map(), |
| 851 | editedAtByNoteId: new Map(), |
| 852 | botLogins: new Set([bot.login.toUpperCase()]), |
| 853 | }), |
| 854 | }), |
| 855 | ), |
| 856 | ); |
| 857 | |
| 858 | const activity = yield* provider.getChangeRequestActivity(reference); |
| 859 | expect(activity.comments.map((entry) => entry.author?.isBot)).toEqual([true, undefined]); |
| 860 | expect(activity.reviewThreads[0]?.comments[0]?.author?.isBot).toBe(true); |
| 861 | }), |
| 862 | ); |
| 863 | }); |
| 864 | |
| 865 | describe("review decisions", () => { |
| 866 | const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.com", number: 7 }; |
| 867 | const item = { |
| 868 | number: 7, |
| 869 | title: "Merge request 7", |
| 870 | url: "https://gitlab.com/acme/web/-/merge_requests/7", |
| 871 | author: null, |
| 872 | headBranch: "feat/page", |
| 873 | baseBranch: "main", |
| 874 | state: "open" as const, |
| 875 | isDraft: false, |
| 876 | mergeability: "mergeable" as const, |
| 877 | additions: 0, |
| 878 | deletions: 0, |
| 879 | createdAt: "2026-07-01T00:00:00Z", |
| 880 | updatedAt: "2026-07-02T00:00:00Z", |
| 881 | reviewRequestLogins: [], |
| 882 | labels: [], |
| 883 | }; |
| 884 | const detail = { |
| 885 | ...item, |
| 886 | body: "", |
| 887 | changedFiles: 1, |
| 888 | mergedAt: null, |
| 889 | closedAt: null, |
| 890 | reviewers: [], |
| 891 | checks: [], |
| 892 | viewerCanMerge: true, |
| 893 | reviewerIds: [], |
| 894 | reviewerUsers: [], |
| 895 | }; |
| 896 | const providerWith = ( |
| 897 | getListStates: GitLabPullRequestCli.GitLabPullRequestCli["Service"]["getListStates"], |
| 898 | overrides: Partial<GitLabPullRequestCli.GitLabPullRequestCli["Service"]> = {}, |
| 899 | ) => |
| 900 | make.pipe( |
| 901 | Effect.provide( |
| 902 | Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ |
| 903 | supportsGraphqlList: () => Effect.succeed(false), |
| 904 | listMergeRequests: () => |
| 905 | Effect.succeed({ |
| 906 | items: [item, { ...item, number: 8 }], |
| 907 | truncated: false, |
| 908 | cursorAdvance: 2, |
| 909 | }), |
| 910 | ...overrides, |
| 911 | getMergeRequestDetail: () => Effect.succeed(detail), |
| 912 | getListStates, |
| 913 | listMergeRequestStats: () => |
| 914 | Effect.succeed([{ repository: "acme/web", number: 7, additions: 40, deletions: 2 }]), |
| 915 | }), |
| 916 | ), |
| 917 | ); |
| 918 | const listing = { |
| 919 | cwd: "/w", |
| 920 | repository: "acme/web", |
| 921 | host: "gitlab.com", |
| 922 | state: "open" as const, |
| 923 | involvement: "all" as const, |
| 924 | viewer: "julius", |
| 925 | limit: 2, |
| 926 | }; |
| 927 | |
| 928 | it.effect( |
| 929 | "puts GitLab's approval and pipeline state on listed and summarized merge requests", |
| 930 | () => |
| 931 | Effect.gen(function* () { |
| 932 | const provider = yield* providerWith((input) => { |
| 933 | expect(input.host).toBe(reference.host); |
| 934 | return Effect.succeed( |
| 935 | new Map([ |
| 936 | [7, { reviewDecision: "approved" as const, checksState: "failing" as const }], |
| 937 | [8, { reviewDecision: null, checksState: null }], |
| 938 | ]), |
| 939 | ); |
| 940 | }); |
| 941 | const listed = yield* provider.listChangeRequests(listing); |
| 942 | assert.isDefined(provider.getChangeRequestSummary); |
| 943 | const summary = yield* provider.getChangeRequestSummary(reference); |
| 944 | |
| 945 | expect(listed.items.map(({ reviewDecision }) => reviewDecision)).toEqual([ |
| 946 | "approved", |
| 947 | null, |
| 948 | ]); |
| 949 | expect(listed.items.map(({ checksState }) => checksState)).toEqual(["failing", null]); |
| 950 | expect(summary.reviewDecision).toBe("approved"); |
| 951 | expect(summary.checksState).toBe("failing"); |
| 952 | expect(summary.additions).toBe(40); |
| 953 | expect(summary.deletions).toBe(2); |
| 954 | }), |
| 955 | ); |
| 956 | |
| 957 | it.effect( |
| 958 | "lists through GraphQL where the host takes it, using the states that read carried", |
| 959 | () => |
| 960 | Effect.gen(function* () { |
| 961 | const graphqlFlags: Array<boolean | undefined> = []; |
| 962 | let versionReads = 0; |
| 963 | const providerReading = ( |
| 964 | supportsGraphqlList: Effect.Effect< |
| 965 | boolean, |
| 966 | GitLabPullRequestCli.GitLabPullRequestCliError |
| 967 | >, |
| 968 | ) => |
| 969 | providerWith(() => Effect.die("a GraphQL page carries its own states"), { |
| 970 | supportsGraphqlList: () => { |
| 971 | versionReads += 1; |
| 972 | return supportsGraphqlList; |
| 973 | }, |
| 974 | listMergeRequests: (input) => { |
| 975 | graphqlFlags.push(input.graphql); |
| 976 | return Effect.succeed({ |
| 977 | items: [item], |
| 978 | truncated: true, |
| 979 | cursorAdvance: 1, |
| 980 | resumeAfter: "eyJpZCI6IjcifQ", |
| 981 | listStates: new Map([ |
| 982 | [7, { reviewDecision: "changes-requested" as const, checksState: null }], |
| 983 | ]), |
| 984 | }); |
| 985 | }, |
| 986 | }); |
| 987 | const listedWith = ( |
| 988 | supportsGraphqlList: Effect.Effect< |
| 989 | boolean, |
| 990 | GitLabPullRequestCli.GitLabPullRequestCliError |
| 991 | >, |
| 992 | ) => |
| 993 | providerReading(supportsGraphqlList).pipe( |
| 994 | Effect.flatMap((provider) => provider.listChangeRequests(listing)), |
| 995 | ); |
| 996 | |
| 997 | const listed = yield* listedWith(Effect.succeed(true)); |
| 998 | expect(listed).toMatchObject({ resumeAfter: "eyJpZCI6IjcifQ", truncated: true }); |
| 999 | expect(listed).not.toHaveProperty("listStates"); |
| 1000 | expect(listed.items[0]?.reviewDecision).toBe("changes-requested"); |
| 1001 | |
| 1002 | // An unreadable version leaves the list on REST rather than failing it, and stays known. |
| 1003 | const unreadable = yield* providerReading( |
| 1004 | Effect.fail( |
| 1005 | new GitLabPullRequestCli.GitLabMergeRequestReadError({ |
| 1006 | command: "glab", |
| 1007 | cwd: "/w", |
| 1008 | operation: "supportsGraphqlList", |
| 1009 | cause: null, |
| 1010 | }), |
| 1011 | ), |
| 1012 | ); |
| 1013 | versionReads = 0; |
| 1014 | yield* unreadable.listChangeRequests(listing); |
| 1015 | yield* unreadable.listChangeRequests(listing); |
| 1016 | expect(graphqlFlags).toEqual([true, false, false]); |
| 1017 | expect(versionReads).toBe(1); |
| 1018 | }), |
| 1019 | ); |
| 1020 | |
| 1021 | it.effect.each([ |
| 1022 | { |
| 1023 | failure: "a failed read", |
| 1024 | error: new GitLabPullRequestCli.GitLabMergeRequestReadError({ |
| 1025 | command: "glab", |
| 1026 | cwd: "/w", |
| 1027 | operation: "getListStates", |
| 1028 | cause: null, |
| 1029 | }), |
| 1030 | }, |
| 1031 | { |
| 1032 | failure: "a rate limit", |
| 1033 | error: new GitLabCli.GitLabCliRateLimitError({ |
| 1034 | operation: "execute", |
| 1035 | command: "glab", |
| 1036 | cwd: "/w", |
| 1037 | cause: null, |
| 1038 | }), |
| 1039 | }, |
| 1040 | ])("keeps rows and summaries with an unknown decision after $failure", ({ error }) => |
| 1041 | Effect.gen(function* () { |
| 1042 | const provider = yield* providerWith(() => Effect.fail(error)); |
| 1043 | const listed = yield* provider.listChangeRequests(listing); |
| 1044 | assert.isDefined(provider.getChangeRequestSummary); |
| 1045 | const summary = yield* provider.getChangeRequestSummary(reference); |
| 1046 | |
| 1047 | expect(listed.items.map(({ reviewDecision }) => reviewDecision)).toEqual([ |
| 1048 | undefined, |
| 1049 | undefined, |
| 1050 | ]); |
| 1051 | expect(summary.reviewDecision).toBeUndefined(); |
| 1052 | }), |
| 1053 | ); |
| 1054 | }); |