The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Redacted from "effect/Redacted"; |
| 5 | import * as TestClock from "effect/testing/TestClock"; |
| 6 | import { SourceControlProviderKind, type PullRequestReaction } from "@t3tools/contracts"; |
| 7 | |
| 8 | import * as GitHubRequestPolicy from "./GitHubRequestPolicy.ts"; |
| 9 | import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; |
| 10 | import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts"; |
| 11 | import type { |
| 12 | GitHubPullRequestCore, |
| 13 | GitHubPullRequestSearchItem, |
| 14 | } from "./gitHubPullRequestJson.ts"; |
| 15 | import { gitHubViewerPermissions, loginAvatarUrl, make } from "./GitHubPullRequestProvider.ts"; |
| 16 | import type { GitHubReviewThreadComments } from "./gitHubPullRequestJson.ts"; |
| 17 | |
| 18 | const coreFields = { |
| 19 | maintainerCanModify: true, |
| 20 | canEditMaintainerEdits: false, |
| 21 | reviewRequestTeams: [], |
| 22 | checksTruncated: false, |
| 23 | comparison: { behindBy: 0, viewerCanUpdate: true }, |
| 24 | viewerAccess: { |
| 25 | canWrite: true, |
| 26 | canTriage: true, |
| 27 | canUpdate: true, |
| 28 | didAuthor: false, |
| 29 | mergeCapabilities: { merge: true, squash: true, rebase: true }, |
| 30 | autoMergeAllowed: true, |
| 31 | }, |
| 32 | }; |
| 33 | |
| 34 | it.effect("maps credential verification failures without relabeling operation failures", () => |
| 35 | Effect.gen(function* () { |
| 36 | let verificationFails = true; |
| 37 | let operations = 0; |
| 38 | const provider = yield* make.pipe( |
| 39 | Effect.provide( |
| 40 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 41 | revalidateChecks: (_input, read) => read, |
| 42 | withVerifiedCredential: (_input, use) => |
| 43 | verificationFails |
| 44 | ? Effect.fail( |
| 45 | new GitHubPullRequestApi.GitHubViewerLoginUnavailableError({ |
| 46 | cwd: "/w", |
| 47 | }), |
| 48 | ) |
| 49 | : use({ accountId: "123", viewer: "viewer", credentialFingerprint: "fingerprint" }), |
| 50 | }), |
| 51 | ), |
| 52 | ); |
| 53 | const verify = provider.withVerifiedCredential; |
| 54 | if (verify === undefined) |
| 55 | return yield* Effect.die("credential verification was not implemented"); |
| 56 | const input = { cwd: "/w", host: "github.com" }; |
| 57 | const operation = () => |
| 58 | Effect.sync(() => { |
| 59 | operations++; |
| 60 | }).pipe(Effect.andThen(Effect.fail("operation-failed"))); |
| 61 | expect(yield* verify(input, operation).pipe(Effect.flip)).toMatchObject({ |
| 62 | _tag: "PullRequestProviderError", |
| 63 | operation: "routeIdentity", |
| 64 | }); |
| 65 | expect(operations).toBe(0); |
| 66 | verificationFails = false; |
| 67 | expect(yield* verify(input, operation).pipe(Effect.flip)).toBe("operation-failed"); |
| 68 | expect(operations).toBe(1); |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | it.effect.each([ |
| 73 | { status: 403, githubErrorTypes: [], reason: "forbidden" }, |
| 74 | { status: 503, githubErrorTypes: [], reason: "failed" }, |
| 75 | { status: 200, githubErrorTypes: ["FORBIDDEN"], reason: "forbidden" }, |
| 76 | { status: 200, githubErrorTypes: ["INTERNAL"], reason: "failed" }, |
| 77 | { status: 200, githubErrorTypes: ["FORBIDDEN", "NOT_FOUND"], reason: "forbidden" }, |
| 78 | { status: 200, githubErrorTypes: ["FORBIDDEN", "INTERNAL"], reason: "failed" }, |
| 79 | { status: 200, githubErrorTypes: ["FORBIDDEN", null], reason: "failed" }, |
| 80 | ])("classifies GitHub response $status as $reason", ({ status, githubErrorTypes, reason }) => |
| 81 | Effect.gen(function* () { |
| 82 | const provider = yield* make.pipe( |
| 83 | Effect.provide( |
| 84 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 85 | revalidateChecks: (_input, read) => read, |
| 86 | getPullRequestDetail: () => |
| 87 | Effect.fail( |
| 88 | new GitHubRequestPolicy.GitHubApiResponseError({ |
| 89 | host: "github.com", |
| 90 | operation: "detail", |
| 91 | status, |
| 92 | githubErrorTypes, |
| 93 | }), |
| 94 | ), |
| 95 | }), |
| 96 | ), |
| 97 | ); |
| 98 | const error = yield* provider |
| 99 | .getChangeRequest({ |
| 100 | cwd: "/w", |
| 101 | repository: "acme/web", |
| 102 | host: "github.com", |
| 103 | number: 7, |
| 104 | }) |
| 105 | .pipe(Effect.flip); |
| 106 | expect(error.reason).toBe(reason); |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | it.effect("advertises standalone branch deletion for source repository writers", () => |
| 111 | Effect.gen(function* () { |
| 112 | let action: string | undefined; |
| 113 | const provider = yield* make.pipe( |
| 114 | Effect.provide( |
| 115 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 116 | runPullRequestAction: (input) => |
| 117 | Effect.sync(() => { |
| 118 | action = input.action; |
| 119 | }), |
| 120 | }), |
| 121 | ), |
| 122 | ); |
| 123 | expect(provider.capabilities.deleteBranch).toBe(true); |
| 124 | expect(provider.capabilities.actions).toContain("delete-branch"); |
| 125 | yield* provider.runAction({ |
| 126 | cwd: "/w", |
| 127 | repository: "acme/web", |
| 128 | host: "github.com", |
| 129 | number: 7, |
| 130 | action: "delete-branch", |
| 131 | }); |
| 132 | expect(action).toBe("delete-branch"); |
| 133 | const access = { |
| 134 | canWrite: false, |
| 135 | canTriage: false, |
| 136 | canUpdate: false, |
| 137 | didAuthor: false, |
| 138 | canDeleteBranch: true, |
| 139 | autoMergeAllowed: true, |
| 140 | }; |
| 141 | expect(gitHubViewerPermissions(access).actions).toContain("delete-branch"); |
| 142 | expect( |
| 143 | gitHubViewerPermissions({ |
| 144 | ...access, |
| 145 | canWrite: true, |
| 146 | didAuthor: true, |
| 147 | canDeleteBranch: false, |
| 148 | }).actions, |
| 149 | ).not.toContain("delete-branch"); |
| 150 | }), |
| 151 | ); |
| 152 | |
| 153 | describe("gitHubViewerPermissions", () => { |
| 154 | it("offers everything to a viewer who can write to the repository", () => { |
| 155 | expect( |
| 156 | gitHubViewerPermissions({ |
| 157 | canWrite: true, |
| 158 | canDeleteBranch: true, |
| 159 | canTriage: true, |
| 160 | canUpdate: true, |
| 161 | didAuthor: false, |
| 162 | autoMergeAllowed: true, |
| 163 | }), |
| 164 | ).toEqual({ |
| 165 | actions: [ |
| 166 | "delete-branch", |
| 167 | "merge", |
| 168 | "enable-auto-merge", |
| 169 | "disable-auto-merge", |
| 170 | "revert", |
| 171 | "approve-workflows", |
| 172 | "ready", |
| 173 | "draft", |
| 174 | "close", |
| 175 | "reopen", |
| 176 | ], |
| 177 | comment: true, |
| 178 | resolve: true, |
| 179 | stackRebase: true, |
| 180 | verdicts: ["comment", "approve", "request-changes"], |
| 181 | requestReviewers: true, |
| 182 | labels: true, |
| 183 | }); |
| 184 | }); |
| 185 | |
| 186 | it("leaves a passer-by on a repository they can only read nothing but the review", () => { |
| 187 | expect( |
| 188 | gitHubViewerPermissions({ |
| 189 | canWrite: false, |
| 190 | canTriage: false, |
| 191 | canUpdate: false, |
| 192 | didAuthor: false, |
| 193 | autoMergeAllowed: true, |
| 194 | }), |
| 195 | ).toEqual({ |
| 196 | actions: [], |
| 197 | comment: true, |
| 198 | resolve: false, |
| 199 | verdicts: ["comment", "approve", "request-changes"], |
| 200 | requestReviewers: false, |
| 201 | labels: false, |
| 202 | }); |
| 203 | }); |
| 204 | |
| 205 | it("lets a triager label without letting them merge or ask for a review", () => { |
| 206 | const permissions = gitHubViewerPermissions({ |
| 207 | canWrite: false, |
| 208 | canTriage: true, |
| 209 | canUpdate: false, |
| 210 | didAuthor: false, |
| 211 | autoMergeAllowed: true, |
| 212 | }); |
| 213 | expect(permissions.labels).toBe(true); |
| 214 | expect(permissions.requestReviewers).toBe(false); |
| 215 | expect(permissions.actions).toEqual([]); |
| 216 | }); |
| 217 | |
| 218 | it("keeps an author's own pull request theirs to close, with read access and no more", () => { |
| 219 | expect( |
| 220 | gitHubViewerPermissions({ |
| 221 | canWrite: false, |
| 222 | canTriage: false, |
| 223 | canUpdate: true, |
| 224 | didAuthor: true, |
| 225 | autoMergeAllowed: true, |
| 226 | }), |
| 227 | ).toEqual({ |
| 228 | actions: ["ready", "draft", "close", "reopen"], |
| 229 | comment: true, |
| 230 | resolve: true, |
| 231 | verdicts: ["comment"], |
| 232 | requestReviewers: false, |
| 233 | labels: false, |
| 234 | }); |
| 235 | }); |
| 236 | |
| 237 | it.effect("uses the permissions carried by the core read without a second request", () => |
| 238 | Effect.gen(function* () { |
| 239 | const provider = yield* make; |
| 240 | const detail = yield* provider.getChangeRequest({ |
| 241 | cwd: "/w", |
| 242 | repository: "acme/web", |
| 243 | host: "github.com", |
| 244 | number: 7, |
| 245 | }); |
| 246 | |
| 247 | expect(detail.viewerPermissions).toEqual({ |
| 248 | maintainerEdits: false, |
| 249 | actions: ["ready", "draft", "close", "reopen"], |
| 250 | comment: true, |
| 251 | resolve: false, |
| 252 | verdicts: ["comment", "approve", "request-changes"], |
| 253 | requestReviewers: false, |
| 254 | labels: false, |
| 255 | }); |
| 256 | expect(detail.workflowApprovalsRequired).toBeUndefined(); |
| 257 | expect(detail.checks).toContainEqual({ |
| 258 | name: "Workflow approval status", |
| 259 | status: "action-required", |
| 260 | description: "GitHub could not determine whether workflows are awaiting approval.", |
| 261 | url: null, |
| 262 | placeholder: true, |
| 263 | }); |
| 264 | for (const fingerprint of ["broad", "restricted", "broad"]) { |
| 265 | const scoped = yield* provider |
| 266 | .getChangeRequest({ |
| 267 | cwd: "/w", |
| 268 | repository: "acme/web", |
| 269 | host: "github.com", |
| 270 | number: 7, |
| 271 | }) |
| 272 | .pipe( |
| 273 | Effect.provideService(GitHubRequestPolicy.PinnedGitHubCredential, { |
| 274 | host: "github.com", |
| 275 | token: Redacted.make("credential"), |
| 276 | credentialFingerprint: fingerprint, |
| 277 | }), |
| 278 | ); |
| 279 | expect(scoped.mergeCapabilities.squash).toBe(fingerprint !== "restricted"); |
| 280 | } |
| 281 | }).pipe( |
| 282 | Effect.provide( |
| 283 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 284 | revalidateChecks: (_input, read) => read, |
| 285 | getPullRequestDetail: () => |
| 286 | Effect.succeed({ |
| 287 | ...coreFields, |
| 288 | comparison: null, |
| 289 | viewerAccess: { ...coreFields.viewerAccess, canWrite: false, canTriage: false }, |
| 290 | number: 7, |
| 291 | title: "Pull request 7", |
| 292 | url: "https://github.com/acme/web/pull/7", |
| 293 | author: null, |
| 294 | isCrossRepository: true, |
| 295 | headRepositoryOwner: null, |
| 296 | headBranch: "feat/page", |
| 297 | baseBranch: "main", |
| 298 | state: "open" as const, |
| 299 | isDraft: false, |
| 300 | mergeability: "mergeable" as const, |
| 301 | reviewDecision: null, |
| 302 | additions: 1, |
| 303 | deletions: 1, |
| 304 | createdAt: "2026-07-01T00:00:00Z", |
| 305 | updatedAt: "2026-07-02T00:00:00Z", |
| 306 | reviewRequestLogins: [], |
| 307 | hasTeamReviewRequest: false, |
| 308 | checksState: null, |
| 309 | labels: [], |
| 310 | body: "", |
| 311 | changedFiles: 1, |
| 312 | mergedAt: null, |
| 313 | closedAt: null, |
| 314 | checks: [], |
| 315 | comments: [], |
| 316 | commits: [], |
| 317 | }).pipe( |
| 318 | Effect.flatMap((detail) => |
| 319 | GitHubRequestPolicy.PinnedGitHubCredential.pipe( |
| 320 | Effect.map((credential) => ({ |
| 321 | ...detail, |
| 322 | viewerAccess: { |
| 323 | ...detail.viewerAccess, |
| 324 | mergeCapabilities: { |
| 325 | ...detail.viewerAccess.mergeCapabilities, |
| 326 | squash: credential?.credentialFingerprint !== "restricted", |
| 327 | }, |
| 328 | }, |
| 329 | })), |
| 330 | ), |
| 331 | ), |
| 332 | ), |
| 333 | }), |
| 334 | ), |
| 335 | ), |
| 336 | ); |
| 337 | |
| 338 | it.effect("keeps fork workflows awaiting approval out of the passing state", () => |
| 339 | Effect.gen(function* () { |
| 340 | const provider = yield* make; |
| 341 | const detail = yield* provider.getChangeRequest({ |
| 342 | cwd: "/w", |
| 343 | repository: "acme/web", |
| 344 | host: "github.com", |
| 345 | number: 7, |
| 346 | }); |
| 347 | |
| 348 | const readChecks = provider.getChangeRequestChecks; |
| 349 | if (readChecks === undefined) return yield* Effect.die("checks read missing"); |
| 350 | expect( |
| 351 | yield* readChecks({ cwd: "/w", repository: "acme/web", host: "github.com", number: 7 }), |
| 352 | ).toEqual({ state: detail.state, checks: detail.checks }); |
| 353 | expect(detail.workflowApprovalsRequired).toBe(1); |
| 354 | expect(detail.checks).toEqual([ |
| 355 | { |
| 356 | name: "manual gate", |
| 357 | status: "action-required", |
| 358 | description: null, |
| 359 | url: "https://example.com/manual-gate", |
| 360 | }, |
| 361 | { |
| 362 | name: "build", |
| 363 | status: "success", |
| 364 | description: null, |
| 365 | url: null, |
| 366 | }, |
| 367 | { |
| 368 | name: "contributor tests", |
| 369 | status: "action-required", |
| 370 | description: "A maintainer must approve this workflow before it can run.", |
| 371 | url: "https://github.com/acme/web/actions/runs/123", |
| 372 | }, |
| 373 | ]); |
| 374 | }).pipe( |
| 375 | Effect.provide( |
| 376 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 377 | revalidateChecks: (_input, read) => read, |
| 378 | getPullRequestDetail: () => |
| 379 | Effect.succeed({ |
| 380 | ...coreFields, |
| 381 | number: 7, |
| 382 | title: "Pull request 7", |
| 383 | url: "https://github.com/acme/web/pull/7", |
| 384 | author: null, |
| 385 | isCrossRepository: true, |
| 386 | headRepositoryOwner: "octocat", |
| 387 | headSha: "abc123", |
| 388 | headBranch: "feat/page", |
| 389 | baseBranch: "main", |
| 390 | state: "open", |
| 391 | isDraft: false, |
| 392 | mergeability: "mergeable", |
| 393 | reviewDecision: null, |
| 394 | additions: 1, |
| 395 | deletions: 1, |
| 396 | createdAt: "2026-07-01T00:00:00Z", |
| 397 | updatedAt: "2026-07-02T00:00:00Z", |
| 398 | reviewRequestLogins: [], |
| 399 | hasTeamReviewRequest: false, |
| 400 | checksState: "passing", |
| 401 | labels: [], |
| 402 | body: "", |
| 403 | changedFiles: 1, |
| 404 | mergedAt: null, |
| 405 | closedAt: null, |
| 406 | checks: [ |
| 407 | { |
| 408 | name: "manual gate", |
| 409 | status: "action-required", |
| 410 | description: null, |
| 411 | url: "https://example.com/manual-gate", |
| 412 | }, |
| 413 | { name: "build", status: "success", description: null, url: null }, |
| 414 | ], |
| 415 | comments: [], |
| 416 | commits: [], |
| 417 | }), |
| 418 | listUnfinishedWorkflowRuns: () => Effect.succeed([]), |
| 419 | listWorkflowRunsRequiringApproval: () => |
| 420 | Effect.succeed([ |
| 421 | { |
| 422 | id: 123, |
| 423 | name: "contributor tests", |
| 424 | url: "https://github.com/acme/web/actions/runs/123", |
| 425 | }, |
| 426 | ]), |
| 427 | getViewerAccess: () => |
| 428 | Effect.succeed({ |
| 429 | canWrite: true, |
| 430 | canTriage: true, |
| 431 | canUpdate: true, |
| 432 | didAuthor: false, |
| 433 | mergeCapabilities: { merge: true, squash: true, rebase: true }, |
| 434 | autoMergeAllowed: true, |
| 435 | }), |
| 436 | }), |
| 437 | ), |
| 438 | ), |
| 439 | ); |
| 440 | }); |
| 441 | |
| 442 | const openDetail = { |
| 443 | ...coreFields, |
| 444 | number: 7, |
| 445 | title: "Pull request 7", |
| 446 | url: "https://github.com/acme/web/pull/7", |
| 447 | author: null, |
| 448 | isCrossRepository: true, |
| 449 | headRepositoryOwner: "acme", |
| 450 | headSha: "abc123", |
| 451 | headBranch: "feat/page", |
| 452 | baseBranch: "main", |
| 453 | state: "open" as const, |
| 454 | isDraft: false, |
| 455 | mergeability: "mergeable" as const, |
| 456 | reviewDecision: null, |
| 457 | additions: 1, |
| 458 | deletions: 1, |
| 459 | createdAt: "2026-07-01T00:00:00Z", |
| 460 | updatedAt: "2026-07-02T00:00:00Z", |
| 461 | reviewRequestLogins: [], |
| 462 | hasTeamReviewRequest: false, |
| 463 | checksState: null, |
| 464 | labels: [], |
| 465 | body: "", |
| 466 | changedFiles: 1, |
| 467 | mergedAt: null, |
| 468 | closedAt: null, |
| 469 | checks: [], |
| 470 | comments: [], |
| 471 | commits: [], |
| 472 | }; |
| 473 | |
| 474 | const sameRepositoryChecks = [ |
| 475 | { name: "CodeRabbit", status: "skipped" as const, description: null, url: null }, |
| 476 | { |
| 477 | name: "lint", |
| 478 | status: "pending" as const, |
| 479 | description: null, |
| 480 | url: "https://github.com/acme/web/actions/runs/201/job/1", |
| 481 | }, |
| 482 | ]; |
| 483 | |
| 484 | it.effect("adds unstarted same-repository workflow runs as pending, not as approvals", () => |
| 485 | Effect.gen(function* () { |
| 486 | const provider = yield* make; |
| 487 | const detail = yield* provider.getChangeRequest({ |
| 488 | cwd: "/w", |
| 489 | repository: "acme/web", |
| 490 | host: "github.com", |
| 491 | number: 7, |
| 492 | }); |
| 493 | |
| 494 | expect(detail.workflowApprovalsRequired).toBe(0); |
| 495 | expect(detail.checks).toEqual([ |
| 496 | ...sameRepositoryChecks, |
| 497 | { |
| 498 | name: "CI", |
| 499 | status: "pending", |
| 500 | description: "This workflow run has not finished.", |
| 501 | url: "https://github.com/acme/web/actions/runs/202", |
| 502 | }, |
| 503 | ]); |
| 504 | }).pipe( |
| 505 | Effect.provide( |
| 506 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 507 | revalidateChecks: (_input, read) => read, |
| 508 | getPullRequestDetail: () => |
| 509 | Effect.succeed({ ...openDetail, isCrossRepository: false, checks: sameRepositoryChecks }), |
| 510 | // Run 201 is represented by its running job; run 202 is queued before any job exists. |
| 511 | listUnfinishedWorkflowRuns: () => |
| 512 | Effect.succeed([ |
| 513 | { id: 201, name: "Lint", url: "https://github.com/acme/web/actions/runs/201" }, |
| 514 | { id: 202, name: "CI", url: "https://github.com/acme/web/actions/runs/202" }, |
| 515 | ]), |
| 516 | listWorkflowRunsRequiringApproval: () => |
| 517 | Effect.die("same-repository pull requests must not probe fork workflow approvals"), |
| 518 | getViewerAccess: () => |
| 519 | Effect.succeed({ |
| 520 | canWrite: true, |
| 521 | canTriage: true, |
| 522 | canUpdate: true, |
| 523 | didAuthor: false, |
| 524 | mergeCapabilities: { merge: true, squash: true, rebase: true }, |
| 525 | autoMergeAllowed: true, |
| 526 | }), |
| 527 | }), |
| 528 | ), |
| 529 | ), |
| 530 | ); |
| 531 | |
| 532 | it.effect("keeps an unsafe workflow approval scope visible as unknown", () => |
| 533 | Effect.gen(function* () { |
| 534 | const provider = yield* make; |
| 535 | const detail = yield* provider.getChangeRequest({ |
| 536 | cwd: "/w", |
| 537 | repository: "acme/web", |
| 538 | host: "github.com", |
| 539 | number: 7, |
| 540 | }); |
| 541 | |
| 542 | expect(detail.workflowApprovalsRequired).toBeUndefined(); |
| 543 | expect(detail.checks).toEqual([ |
| 544 | { |
| 545 | name: "Workflow approval status", |
| 546 | status: "action-required", |
| 547 | description: "GitHub could not determine whether workflows are awaiting approval.", |
| 548 | url: null, |
| 549 | placeholder: true, |
| 550 | }, |
| 551 | ]); |
| 552 | }).pipe( |
| 553 | Effect.provide( |
| 554 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 555 | revalidateChecks: (_input, read) => read, |
| 556 | getPullRequestDetail: () => Effect.succeed(openDetail), |
| 557 | listUnfinishedWorkflowRuns: () => Effect.succeed([]), |
| 558 | listWorkflowRunsRequiringApproval: () => |
| 559 | Effect.fail( |
| 560 | new GitHubPullRequestApi.GitHubWorkflowApprovalRefusedError({ |
| 561 | cwd: "/w", |
| 562 | number: 7, |
| 563 | reason: "head-not-unique", |
| 564 | observedCount: 2, |
| 565 | limit: 1_000, |
| 566 | }), |
| 567 | ), |
| 568 | getViewerAccess: () => |
| 569 | Effect.succeed({ |
| 570 | canWrite: true, |
| 571 | canTriage: true, |
| 572 | canUpdate: true, |
| 573 | didAuthor: false, |
| 574 | mergeCapabilities: { merge: true, squash: true, rebase: true }, |
| 575 | autoMergeAllowed: true, |
| 576 | }), |
| 577 | }), |
| 578 | ), |
| 579 | ), |
| 580 | ); |
| 581 | |
| 582 | describe("getViewerPermissions", () => { |
| 583 | const access = { |
| 584 | canWrite: true, |
| 585 | canTriage: true, |
| 586 | canUpdate: true, |
| 587 | didAuthor: false, |
| 588 | mergeCapabilities: { merge: true, squash: true, rebase: true }, |
| 589 | autoMergeAllowed: true, |
| 590 | }; |
| 591 | |
| 592 | const layerWithDetail = ( |
| 593 | detail: Effect.Effect<GitHubPullRequestCore, GitHubPullRequestApi.GitHubPullRequestApiError>, |
| 594 | onDetail: (allowReserve: boolean) => void = () => {}, |
| 595 | ) => |
| 596 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 597 | revalidateChecks: (_input, read) => read, |
| 598 | getPullRequestDetail: () => |
| 599 | SourceControlRateLimit.Interactive.pipe( |
| 600 | Effect.tap((allowReserve) => Effect.sync(() => onDetail(allowReserve))), |
| 601 | Effect.flatMap(() => detail), |
| 602 | ), |
| 603 | getViewerAccess: () => Effect.die("Unexpected access read"), |
| 604 | }); |
| 605 | |
| 606 | it.effect("answers access and update-branch from the one detail read", () => |
| 607 | Effect.gen(function* () { |
| 608 | const provider = yield* make; |
| 609 | const permissions = yield* provider.getViewerPermissions({ |
| 610 | cwd: "/w", |
| 611 | repository: "acme/web", |
| 612 | host: "github.com", |
| 613 | number: 7, |
| 614 | }); |
| 615 | |
| 616 | expect(permissions.actions).toContain("merge"); |
| 617 | expect(permissions.actions).toContain("update-branch"); |
| 618 | expect(permissions.updateMethods).toEqual(["merge", "rebase"]); |
| 619 | }).pipe( |
| 620 | Effect.provide( |
| 621 | layerWithDetail( |
| 622 | Effect.succeed({ ...openDetail, comparison: { behindBy: 3, viewerCanUpdate: true } }), |
| 623 | ), |
| 624 | ), |
| 625 | ), |
| 626 | ); |
| 627 | |
| 628 | it.effect("still answers from the light access read when the detail read fails", () => |
| 629 | Effect.gen(function* () { |
| 630 | const provider = yield* make; |
| 631 | const permissions = yield* provider.getViewerPermissions({ |
| 632 | cwd: "/w", |
| 633 | repository: "acme/web", |
| 634 | host: "github.com", |
| 635 | number: 7, |
| 636 | }); |
| 637 | expect(permissions.actions).toContain("merge"); |
| 638 | expect(permissions.actions).not.toContain("update-branch"); |
| 639 | }).pipe( |
| 640 | Effect.provide( |
| 641 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 642 | revalidateChecks: (_input, read) => read, |
| 643 | getPullRequestDetail: () => |
| 644 | Effect.fail( |
| 645 | new GitHubPullRequestApi.GitHubPullRequestReadError({ |
| 646 | cwd: "/w", |
| 647 | operation: "getPullRequestDetail", |
| 648 | cause: new Error("head changed"), |
| 649 | }), |
| 650 | ), |
| 651 | getViewerAccess: () => Effect.succeed(access), |
| 652 | }), |
| 653 | ), |
| 654 | ), |
| 655 | ); |
| 656 | |
| 657 | it.effect("withholds update-branch where GitHub could not compare the branch", () => |
| 658 | Effect.gen(function* () { |
| 659 | const provider = yield* make; |
| 660 | const permissions = yield* provider.getViewerPermissions({ |
| 661 | cwd: "/w", |
| 662 | repository: "acme/web", |
| 663 | host: "github.com", |
| 664 | number: 7, |
| 665 | }); |
| 666 | |
| 667 | expect(permissions.actions).not.toContain("update-branch"); |
| 668 | expect(permissions.updateMethods).toBeUndefined(); |
| 669 | expect(permissions.actions).toContain("merge"); |
| 670 | }).pipe(Effect.provide(layerWithDetail(Effect.succeed({ ...openDetail, comparison: null })))), |
| 671 | ); |
| 672 | }); |
| 673 | |
| 674 | describe("getChangeRequest commits", () => { |
| 675 | const baseDetail = { |
| 676 | number: 7, |
| 677 | title: "Pull request 7", |
| 678 | url: "https://github.com/acme/web/pull/7", |
| 679 | author: null, |
| 680 | headBranch: "feat/page", |
| 681 | baseBranch: "main", |
| 682 | state: "open" as const, |
| 683 | isDraft: false, |
| 684 | mergeability: "mergeable" as const, |
| 685 | additions: 1, |
| 686 | deletions: 1, |
| 687 | createdAt: "2026-07-01T00:00:00Z", |
| 688 | updatedAt: "2026-07-02T00:00:00Z", |
| 689 | reviewRequestLogins: [], |
| 690 | hasTeamReviewRequest: false, |
| 691 | checksState: null, |
| 692 | labels: [], |
| 693 | body: "", |
| 694 | changedFiles: 1, |
| 695 | mergedAt: null, |
| 696 | closedAt: null, |
| 697 | checks: [], |
| 698 | comments: [], |
| 699 | }; |
| 700 | |
| 701 | const baseThreadComments = { |
| 702 | comments: [], |
| 703 | dismissalsByReviewId: new Map<string, string>(), |
| 704 | reviewThreads: [], |
| 705 | commentCount: 0, |
| 706 | truncated: false, |
| 707 | reviewThreadsTruncated: false, |
| 708 | reactions: [], |
| 709 | reactionsById: new Map<string, ReadonlyArray<PullRequestReaction>>(), |
| 710 | editedAtById: new Map<string, string>(), |
| 711 | reviewers: [], |
| 712 | avatarsByLogin: new Map<string, string>(), |
| 713 | botLogins: new Set<string>(), |
| 714 | viewer: { canUpdate: true, didAuthor: false }, |
| 715 | }; |
| 716 | |
| 717 | const layerWith = (commits: GitHubReviewThreadComments["commits"]) => |
| 718 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 719 | revalidateChecks: (_input, read) => read, |
| 720 | getPullRequestActivity: () => |
| 721 | Effect.succeed({ |
| 722 | author: baseDetail.author, |
| 723 | comments: baseDetail.comments, |
| 724 | commits: [ |
| 725 | { |
| 726 | oid: "graphql-newest", |
| 727 | messageHeadline: "Activity metadata", |
| 728 | committedDate: "2026-07-06T00:00:00Z", |
| 729 | additions: 18, |
| 730 | deletions: 7, |
| 731 | authors: [], |
| 732 | }, |
| 733 | { |
| 734 | oid: "view-oldest", |
| 735 | messageHeadline: "gh pr view's oldest commit", |
| 736 | committedDate: "2026-01-01T00:00:00Z", |
| 737 | authors: [], |
| 738 | }, |
| 739 | ], |
| 740 | truncated: false, |
| 741 | }), |
| 742 | listReviewThreadComments: () => Effect.succeed({ ...baseThreadComments, commits }), |
| 743 | }); |
| 744 | |
| 745 | it.effect("prefers the GraphQL commits, which are the newest, over the gh view list", () => |
| 746 | Effect.gen(function* () { |
| 747 | const provider = yield* make; |
| 748 | const detail = yield* provider.getChangeRequestActivity({ |
| 749 | cwd: "/w", |
| 750 | repository: "acme/web", |
| 751 | host: "github.com", |
| 752 | number: 7, |
| 753 | }); |
| 754 | |
| 755 | expect(detail.commits.map((commit) => commit.oid)).toEqual(["graphql-newest"]); |
| 756 | expect(detail.commits[0]).toMatchObject({ |
| 757 | messageHeadline: "the newest commit gh pr view drops", |
| 758 | additions: 18, |
| 759 | deletions: 7, |
| 760 | }); |
| 761 | }).pipe( |
| 762 | Effect.provide( |
| 763 | layerWith([ |
| 764 | { |
| 765 | oid: "graphql-newest", |
| 766 | messageHeadline: "the newest commit gh pr view drops", |
| 767 | committedDate: "2026-07-06T00:00:00Z", |
| 768 | authors: [], |
| 769 | }, |
| 770 | ]), |
| 771 | ), |
| 772 | ), |
| 773 | ); |
| 774 | |
| 775 | it.effect("falls back to the gh view list when the GraphQL read has no commits", () => |
| 776 | Effect.gen(function* () { |
| 777 | const provider = yield* make; |
| 778 | const detail = yield* provider.getChangeRequestActivity({ |
| 779 | cwd: "/w", |
| 780 | repository: "acme/web", |
| 781 | host: "github.com", |
| 782 | number: 7, |
| 783 | }); |
| 784 | |
| 785 | expect(detail.commits.map((commit) => commit.oid)).toEqual(["graphql-newest", "view-oldest"]); |
| 786 | }).pipe(Effect.provide(layerWith([]))), |
| 787 | ); |
| 788 | }); |
| 789 | |
| 790 | describe("getChangeRequestActivity", () => { |
| 791 | const dismissedReview = (body: string) => ({ |
| 792 | id: "PRR_1", |
| 793 | kind: "review" as const, |
| 794 | author: { login: "macroscopeapp", name: null, avatarUrl: null }, |
| 795 | body, |
| 796 | createdAt: "2026-07-03T00:00:00Z", |
| 797 | url: null, |
| 798 | path: null, |
| 799 | reviewState: "DISMISSED", |
| 800 | }); |
| 801 | const threadComments: GitHubReviewThreadComments = { |
| 802 | comments: [], |
| 803 | dismissalsByReviewId: new Map([["PRR_1", "Dismissing prior approval to re-evaluate 9b66581"]]), |
| 804 | reviewThreads: [], |
| 805 | commentCount: 0, |
| 806 | truncated: false, |
| 807 | reviewThreadsTruncated: false, |
| 808 | reactions: [], |
| 809 | reactionsById: new Map(), |
| 810 | editedAtById: new Map([["PRR_1", "2026-07-04T00:00:00Z"]]), |
| 811 | reviewers: [], |
| 812 | avatarsByLogin: new Map(), |
| 813 | botLogins: new Set(["macroscopeapp"]), |
| 814 | commits: [], |
| 815 | viewer: { canUpdate: true, didAuthor: false }, |
| 816 | }; |
| 817 | const layerFor = (body: string) => |
| 818 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 819 | revalidateChecks: (_input, read) => read, |
| 820 | getPullRequestActivity: () => |
| 821 | Effect.succeed({ |
| 822 | author: null, |
| 823 | comments: [dismissedReview(body)], |
| 824 | commits: [], |
| 825 | truncated: false, |
| 826 | }), |
| 827 | listReviewThreadComments: () => Effect.succeed(threadComments), |
| 828 | }); |
| 829 | const readActivity = Effect.gen(function* () { |
| 830 | const provider = yield* make; |
| 831 | return yield* provider.getChangeRequestActivity({ |
| 832 | cwd: "/w", |
| 833 | repository: "acme/web", |
| 834 | host: "github.com", |
| 835 | number: 7, |
| 836 | }); |
| 837 | }); |
| 838 | |
| 839 | it.effect("fills a marker-only dismissed review with the timeline's reason", () => |
| 840 | readActivity.pipe( |
| 841 | Effect.map((activity) => { |
| 842 | expect(activity.comments[0]?.body).toBe("Dismissing prior approval to re-evaluate 9b66581"); |
| 843 | expect(activity.comments[0]?.author?.isBot).toBe(true); |
| 844 | expect(activity.comments[0]?.editedAt).toBe("2026-07-04T00:00:00Z"); |
| 845 | }), |
| 846 | Effect.provide(layerFor("<!-- Macroscope (Approvability) review body marker -->")), |
| 847 | ), |
| 848 | ); |
| 849 | |
| 850 | const failingThreadRead = (error: GitHubPullRequestApi.GitHubPullRequestApiError) => |
| 851 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 852 | getPullRequestActivity: () => |
| 853 | Effect.succeed({ author: null, comments: [], commits: [], truncated: false }), |
| 854 | listReviewThreadComments: () => Effect.fail(error), |
| 855 | }); |
| 856 | const retryAt = 1_900_000_000_000; |
| 857 | |
| 858 | it.effect.each([ |
| 859 | { |
| 860 | name: "rate limit", |
| 861 | error: new GitHubRequestPolicy.GitHubApiRateLimitError({ |
| 862 | host: "github.com", |
| 863 | operation: "listReviewThreadComments", |
| 864 | retryAt, |
| 865 | }), |
| 866 | }, |
| 867 | { |
| 868 | name: "rate-limit pause", |
| 869 | error: new SourceControlRateLimit.SourceControlRateLimitPausedError({ |
| 870 | provider: SourceControlProviderKind.make("github"), |
| 871 | host: "github.com", |
| 872 | retryAt, |
| 873 | }), |
| 874 | }, |
| 875 | ])("fails rate-limited when the review-thread read hits a $name", ({ error }) => |
| 876 | readActivity.pipe( |
| 877 | Effect.flip, |
| 878 | Effect.map((failure) => expect(failure).toMatchObject({ reason: "rate-limited", retryAt })), |
| 879 | Effect.provide(failingThreadRead(error)), |
| 880 | ), |
| 881 | ); |
| 882 | |
| 883 | it.effect( |
| 884 | "keeps activity with truncated threads when the review-thread read fails otherwise", |
| 885 | () => |
| 886 | readActivity.pipe( |
| 887 | Effect.map((activity) => { |
| 888 | expect(activity.reviewThreads).toEqual([]); |
| 889 | expect(activity.reviewThreadsTruncated).toBe(true); |
| 890 | }), |
| 891 | Effect.provide( |
| 892 | failingThreadRead( |
| 893 | new GitHubRequestPolicy.GitHubApiResponseError({ |
| 894 | host: "github.com", |
| 895 | operation: "listReviewThreadComments", |
| 896 | status: 500, |
| 897 | }), |
| 898 | ), |
| 899 | ), |
| 900 | ), |
| 901 | ); |
| 902 | }); |
| 903 | |
| 904 | describe("loginAvatarUrl", () => { |
| 905 | it("refuses anything that is not a login, rather than building a URL out of it", () => { |
| 906 | for (const login of ["../../etc", "a b", "-leading", "x".repeat(40), ""]) { |
| 907 | expect(loginAvatarUrl(login, "github.com")).toBeNull(); |
| 908 | } |
| 909 | }); |
| 910 | }); |
| 911 | |
| 912 | describe("listChangeRequestsAcross", () => { |
| 913 | const searched = (repository: string): GitHubPullRequestSearchItem => ({ |
| 914 | repository, |
| 915 | number: 1, |
| 916 | title: "Change", |
| 917 | url: `https://github.com/${repository}/pull/1`, |
| 918 | author: null, |
| 919 | headBranch: "feature", |
| 920 | baseBranch: "main", |
| 921 | state: "open" as const, |
| 922 | isDraft: false, |
| 923 | mergeability: "mergeable" as const, |
| 924 | additions: 0, |
| 925 | deletions: 0, |
| 926 | createdAt: "2026-07-01T00:00:00Z", |
| 927 | updatedAt: "2026-07-02T00:00:00Z", |
| 928 | reviewRequestLogins: [], |
| 929 | hasTeamReviewRequest: false, |
| 930 | labels: [], |
| 931 | reviewDecision: null, |
| 932 | checksState: null, |
| 933 | }); |
| 934 | const across = { |
| 935 | cwd: "/w", |
| 936 | host: "github.com", |
| 937 | repositories: ["acme/web", "acme/docs"], |
| 938 | state: "open" as const, |
| 939 | involvement: "all" as const, |
| 940 | viewer: "viewer", |
| 941 | limit: 50, |
| 942 | }; |
| 943 | type Search = Parameters< |
| 944 | GitHubPullRequestApi.GitHubPullRequestApi["Service"]["searchPullRequests"] |
| 945 | >[0]; |
| 946 | // Pages answer in turn; a one-repository check for any pull request answers from `covered`. |
| 947 | const providerAnswering = (input: { |
| 948 | readonly pages: Array<GitHubPullRequestApi.GitHubPullRequestSearchBatch>; |
| 949 | readonly covered?: (repository: string) => boolean; |
| 950 | readonly checks?: Array<string>; |
| 951 | }) => |
| 952 | make.pipe( |
| 953 | Effect.provide( |
| 954 | Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({ |
| 955 | searchPullRequests: (search: Search) => { |
| 956 | if (search.state !== "all") return Effect.succeed(input.pages.shift()!); |
| 957 | const repository = search.repositories[0]!; |
| 958 | input.checks?.push(repository); |
| 959 | return input.covered === undefined |
| 960 | ? Effect.fail( |
| 961 | new GitHubPullRequestApi.GitHubViewerLoginUnavailableError({ cwd: search.cwd }), |
| 962 | ) |
| 963 | : Effect.succeed({ |
| 964 | items: input.covered(repository) ? [searched(repository)] : [], |
| 965 | truncated: false, |
| 966 | }); |
| 967 | }, |
| 968 | }), |
| 969 | ), |
| 970 | Effect.map((provider) => provider.listChangeRequestsAcross!), |
| 971 | ); |
| 972 | |
| 973 | it.effect("believes silence about a repository search covers, and checks it once", () => |
| 974 | Effect.gen(function* () { |
| 975 | const checks: Array<string> = []; |
| 976 | const read = yield* providerAnswering({ |
| 977 | pages: [ |
| 978 | { items: [searched("acme/web")], truncated: false }, |
| 979 | { items: [searched("acme/web")], truncated: false }, |
| 980 | ], |
| 981 | covered: () => true, |
| 982 | checks, |
| 983 | }); |
| 984 | expect((yield* read(across)).unresolved).toEqual([]); |
| 985 | expect((yield* read(across)).unresolved).toEqual([]); |
| 986 | expect(checks).toEqual(["acme/docs"]); |
| 987 | }), |
| 988 | ); |
| 989 | |
| 990 | it.effect("leaves a repository search does not cover to be read on its own", () => |
| 991 | Effect.gen(function* () { |
| 992 | const read = yield* providerAnswering({ |
| 993 | pages: [{ items: [searched("acme/web")], truncated: false }], |
| 994 | covered: (repository) => repository !== "acme/docs", |
| 995 | }); |
| 996 | expect((yield* read(across)).unresolved).toEqual(["acme/docs"]); |
| 997 | }), |
| 998 | ); |
| 999 | |
| 1000 | it.effect("does not check a repository a page had rows for", () => |
| 1001 | Effect.gen(function* () { |
| 1002 | const checks: Array<string> = []; |
| 1003 | const read = yield* providerAnswering({ |
| 1004 | pages: [ |
| 1005 | { items: [searched("acme/web"), searched("acme/docs")], truncated: false }, |
| 1006 | { items: [searched("acme/web")], truncated: false }, |
| 1007 | ], |
| 1008 | covered: () => true, |
| 1009 | checks, |
| 1010 | }); |
| 1011 | yield* read(across); |
| 1012 | expect((yield* read(across)).unresolved).toEqual([]); |
| 1013 | expect(checks).toEqual([]); |
| 1014 | }), |
| 1015 | ); |
| 1016 | |
| 1017 | it.effect("vouches for no silent repository in a truncated first slice", () => |
| 1018 | Effect.gen(function* () { |
| 1019 | const read = yield* providerAnswering({ |
| 1020 | pages: [{ items: [searched("acme/web")], truncated: true }], |
| 1021 | covered: () => true, |
| 1022 | }); |
| 1023 | expect((yield* read(across)).unresolved).toEqual(["acme/docs"]); |
| 1024 | }), |
| 1025 | ); |
| 1026 | |
| 1027 | it.effect("past the first slice, believes a covered repository and not an uncovered one", () => |
| 1028 | Effect.gen(function* () { |
| 1029 | const read = yield* providerAnswering({ |
| 1030 | pages: [ |
| 1031 | { items: [searched("acme/web")], truncated: true }, |
| 1032 | { items: [searched("acme/web")], truncated: true }, |
| 1033 | ], |
| 1034 | covered: (repository) => repository !== "acme/renamed", |
| 1035 | }); |
| 1036 | const later = { ...across, cursor: { updatedBefore: "2026-07-02T00:00:00Z", delivered: 1 } }; |
| 1037 | expect((yield* read(later)).unresolved).toEqual([]); |
| 1038 | expect( |
| 1039 | (yield* read({ ...later, repositories: ["acme/web", "acme/renamed"] })).unresolved, |
| 1040 | ).toEqual(["acme/renamed"]); |
| 1041 | }), |
| 1042 | ); |
| 1043 | |
| 1044 | it.effect("asks again after a failed check, and after coverage has had time to change", () => |
| 1045 | Effect.gen(function* () { |
| 1046 | const checks: Array<string> = []; |
| 1047 | const failing = yield* providerAnswering({ |
| 1048 | pages: [ |
| 1049 | { items: [searched("acme/web")], truncated: false }, |
| 1050 | { items: [searched("acme/web")], truncated: false }, |
| 1051 | ], |
| 1052 | checks, |
| 1053 | }); |
| 1054 | expect((yield* failing(across)).unresolved).toEqual(["acme/docs"]); |
| 1055 | expect((yield* failing(across)).unresolved).toEqual(["acme/docs"]); |
| 1056 | expect(checks).toEqual(["acme/docs", "acme/docs"]); |
| 1057 | |
| 1058 | checks.length = 0; |
| 1059 | const read = yield* providerAnswering({ |
| 1060 | pages: [ |
| 1061 | { items: [searched("acme/web")], truncated: false }, |
| 1062 | { items: [searched("acme/web")], truncated: false }, |
| 1063 | ], |
| 1064 | covered: () => true, |
| 1065 | checks, |
| 1066 | }); |
| 1067 | yield* read(across); |
| 1068 | yield* TestClock.adjust("61 minutes"); |
| 1069 | yield* read(across); |
| 1070 | expect(checks).toEqual(["acme/docs", "acme/docs"]); |
| 1071 | }), |
| 1072 | ); |
| 1073 | }); |