The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | |
| 4 | import * as GitCafePullRequestJson from "./gitCafePullRequestJson.ts"; |
| 5 | |
| 6 | const decodePull = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafePull); |
| 7 | const decodePullDetail = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafePullDetail); |
| 8 | const decodeComments = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeComments); |
| 9 | const decodeReviews = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeReviews); |
| 10 | const decodeCommits = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeCommits); |
| 11 | const decodeReactions = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeReactions); |
| 12 | const decodeStackEnvelope = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeStackEnvelope); |
| 13 | const decodeDiff = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeDiff); |
| 14 | const decodeChecks = Schema.decodeUnknownSync(GitCafePullRequestJson.GitCafeChecks); |
| 15 | |
| 16 | const timestamp = "2026-09-12T12:00:00Z"; |
| 17 | const actor = { |
| 18 | kind: "local", |
| 19 | actorId: "act_one", |
| 20 | handle: "alice", |
| 21 | displayName: null, |
| 22 | avatarUrl: "/avatars/one", |
| 23 | }; |
| 24 | const pull = { |
| 25 | id: "pr_one", |
| 26 | number: 7, |
| 27 | title: "Change", |
| 28 | state: "open", |
| 29 | draft: true, |
| 30 | sourceBranch: "feature", |
| 31 | targetBranch: "main", |
| 32 | headOid: "abcdef", |
| 33 | author: actor, |
| 34 | createdAt: timestamp, |
| 35 | updatedAt: timestamp, |
| 36 | version: 1, |
| 37 | }; |
| 38 | const detail = { |
| 39 | ...pull, |
| 40 | draft: false, |
| 41 | description: "Body", |
| 42 | closedAt: null, |
| 43 | mergedAt: null, |
| 44 | sourceRepo: null, |
| 45 | capabilities: { comment: true, review: true, merge: true, edit: true, moderate: true }, |
| 46 | }; |
| 47 | const lineComment = { |
| 48 | author: actor, |
| 49 | body: "Review", |
| 50 | path: "file.ts", |
| 51 | line: 3, |
| 52 | side: "right", |
| 53 | createdAt: timestamp, |
| 54 | commitOid: "abcdef", |
| 55 | resolvedAt: null, |
| 56 | }; |
| 57 | const capabilities = (overrides: Record<string, boolean> = {}) => ({ |
| 58 | edit: false, |
| 59 | hide: false, |
| 60 | unhide: false, |
| 61 | delete: false, |
| 62 | resolve: false, |
| 63 | unresolve: false, |
| 64 | ...overrides, |
| 65 | }); |
| 66 | const noReviews = decodeReviews({ items: [], next: null }); |
| 67 | const noCommits = decodeCommits({ items: [], headOid: "abcdef", truncated: false, next: null }); |
| 68 | const activity = ( |
| 69 | comments: ReadonlyArray<Record<string, unknown>>, |
| 70 | extra: { readonly reactions?: unknown; readonly host?: string } = {}, |
| 71 | ) => |
| 72 | GitCafePullRequestJson.toActivity({ |
| 73 | comments: decodeComments({ items: comments, next: null }), |
| 74 | reviews: noReviews, |
| 75 | commits: noCommits, |
| 76 | headOid: "abcdef", |
| 77 | host: extra.host ?? "git.cafe", |
| 78 | ...(extra.reactions === undefined ? {} : { reactions: decodeReactions(extra.reactions) }), |
| 79 | }); |
| 80 | |
| 81 | describe("toChangeRequest", () => { |
| 82 | it("reads draft state, the repository host's URLs and absolute avatars", () => { |
| 83 | const row = GitCafePullRequestJson.toChangeRequest(decodePull(pull), { |
| 84 | repository: "owner/repo", |
| 85 | host: "staging.git.cafe", |
| 86 | }); |
| 87 | assert.strictEqual(row.state, "open"); |
| 88 | assert.isTrue(row.isDraft); |
| 89 | assert.strictEqual(row.url, "https://staging.git.cafe/owner/repo/pulls/7"); |
| 90 | assert.deepStrictEqual(row.author, { |
| 91 | login: "alice", |
| 92 | name: null, |
| 93 | avatarUrl: "https://staging.git.cafe/avatars/one", |
| 94 | }); |
| 95 | }); |
| 96 | |
| 97 | it("names linked GitHub reviewers by both logins and drops unavailable ones", () => { |
| 98 | const row = GitCafePullRequestJson.toChangeRequest( |
| 99 | decodePull({ |
| 100 | ...pull, |
| 101 | author: { kind: "github", actorId: "act_remote", login: "remote-user", avatarUrl: null }, |
| 102 | reviewers: [ |
| 103 | { actor }, |
| 104 | { |
| 105 | actor: { |
| 106 | kind: "github", |
| 107 | actorId: "act_github", |
| 108 | login: "upstream", |
| 109 | avatarUrl: null, |
| 110 | linkedProfile: { handle: "linked-local" }, |
| 111 | }, |
| 112 | }, |
| 113 | { actor: { kind: "unavailable", actorId: "act_deleted" } }, |
| 114 | ], |
| 115 | }), |
| 116 | { repository: "owner/repo", host: "git.cafe" }, |
| 117 | ); |
| 118 | assert.strictEqual(row.author?.login, "remote-user"); |
| 119 | assert.deepStrictEqual(row.reviewRequestLogins, ["alice", "upstream", "linked-local"]); |
| 120 | }); |
| 121 | |
| 122 | it("marks a mirrored GitHub bot account as a bot", () => { |
| 123 | const row = (accountType: string) => |
| 124 | GitCafePullRequestJson.toChangeRequest( |
| 125 | decodePull({ |
| 126 | ...pull, |
| 127 | author: { |
| 128 | kind: "github", |
| 129 | actorId: "act_bot", |
| 130 | login: "coderabbitai[bot]", |
| 131 | accountType, |
| 132 | avatarUrl: null, |
| 133 | }, |
| 134 | }), |
| 135 | { repository: "owner/repo", host: "git.cafe" }, |
| 136 | ); |
| 137 | assert.isTrue(row("bot").author?.isBot); |
| 138 | assert.isUndefined(row("user").author?.isBot); |
| 139 | }); |
| 140 | }); |
| 141 | |
| 142 | describe("toViewerPermissions", () => { |
| 143 | const permissions = (overrides: Record<string, unknown>) => |
| 144 | GitCafePullRequestJson.toViewerPermissions(decodePullDetail({ ...detail, ...overrides })); |
| 145 | |
| 146 | it("keeps a locked conversation's composer for moderators only", () => { |
| 147 | const locked = (moderate: boolean) => |
| 148 | permissions({ |
| 149 | lockedAt: timestamp, |
| 150 | capabilities: { comment: true, review: false, merge: false, edit: false, moderate }, |
| 151 | }); |
| 152 | assert.isFalse(locked(false).comment); |
| 153 | assert.isFalse(locked(false).resolve); |
| 154 | assert.isTrue(locked(true).comment); |
| 155 | assert.deepStrictEqual(locked(true).verdicts, []); |
| 156 | }); |
| 157 | }); |
| 158 | |
| 159 | describe("toActivity", () => { |
| 160 | it("groups line comments by their persisted thread id and marks a paged list truncated", () => { |
| 161 | const result = GitCafePullRequestJson.toActivity({ |
| 162 | comments: decodeComments({ |
| 163 | items: [ |
| 164 | { ...lineComment, id: "reply", threadId: "thread-one" }, |
| 165 | { ...lineComment, id: "other", threadId: "thread-two" }, |
| 166 | ], |
| 167 | next: "next", |
| 168 | }), |
| 169 | reviews: noReviews, |
| 170 | commits: noCommits, |
| 171 | headOid: "abcdef", |
| 172 | host: "git.cafe", |
| 173 | }); |
| 174 | assert.deepStrictEqual(result.reviewThreads.map((thread) => thread.id).toSorted(), [ |
| 175 | "thread-one", |
| 176 | "thread-two", |
| 177 | ]); |
| 178 | assert.isTrue(result.commentsTruncated); |
| 179 | }); |
| 180 | |
| 181 | it.each([timestamp, null])("takes the thread's resolution from its root: %s", (resolvedAt) => { |
| 182 | const result = activity([ |
| 183 | { ...lineComment, id: "root", threadId: "root", resolvedAt }, |
| 184 | { ...lineComment, id: "reply", threadId: "root", resolvedAt: null }, |
| 185 | ]); |
| 186 | assert.strictEqual(result.reviewThreads.length, 1); |
| 187 | assert.strictEqual(result.reviewThreads[0]?.isResolved, resolvedAt !== null); |
| 188 | assert.strictEqual(result.reviewThreads[0]?.comments.length, 2); |
| 189 | }); |
| 190 | |
| 191 | it("carries per-remark edit permission onto both copies of a remark", () => { |
| 192 | const result = activity([ |
| 193 | { |
| 194 | ...lineComment, |
| 195 | id: "root", |
| 196 | threadId: "root", |
| 197 | capabilities: capabilities({ edit: false, resolve: true }), |
| 198 | }, |
| 199 | { |
| 200 | ...lineComment, |
| 201 | id: "reply", |
| 202 | threadId: "root", |
| 203 | capabilities: capabilities({ edit: true }), |
| 204 | }, |
| 205 | { ...lineComment, id: "silent", threadId: "silent", path: null, line: null, side: null }, |
| 206 | ]); |
| 207 | const canEdit = (id: string) => result.comments.find((comment) => comment.id === id)?.canEdit; |
| 208 | assert.strictEqual(canEdit("root"), false); |
| 209 | assert.strictEqual(canEdit("reply"), true); |
| 210 | assert.isUndefined(canEdit("silent")); |
| 211 | assert.deepStrictEqual( |
| 212 | result.reviewThreads[0]?.comments.map((comment) => [comment.id, comment.canEdit]), |
| 213 | [ |
| 214 | ["root", false], |
| 215 | ["reply", true], |
| 216 | ], |
| 217 | ); |
| 218 | }); |
| 219 | |
| 220 | it("maps supported reactions to the pull request and to matching comments in both views", () => { |
| 221 | const result = activity( |
| 222 | [ |
| 223 | { ...lineComment, id: "comment-a", threadId: "comment-a" }, |
| 224 | { ...lineComment, id: "comment-b", threadId: "comment-a" }, |
| 225 | ], |
| 226 | { |
| 227 | reactions: { |
| 228 | next: null, |
| 229 | items: [ |
| 230 | { |
| 231 | subject: { kind: "pull_request_comment", id: "comment-b" }, |
| 232 | emoji: { kind: "unicode", value: "👍" }, |
| 233 | count: 3, |
| 234 | viewerReactionId: "reaction-viewer", |
| 235 | reactors: [actor, { kind: "unavailable", actorId: "deleted" }], |
| 236 | }, |
| 237 | { |
| 238 | subject: { kind: "pull_request", id: "pr_one" }, |
| 239 | emoji: { kind: "unicode", value: "🚀" }, |
| 240 | count: 1, |
| 241 | viewerReactionId: null, |
| 242 | reactors: [actor], |
| 243 | }, |
| 244 | { |
| 245 | subject: { kind: "pull_request", id: "pr_one" }, |
| 246 | emoji: { kind: "custom", id: "party-parrot" }, |
| 247 | count: 2, |
| 248 | viewerReactionId: null, |
| 249 | reactors: [actor], |
| 250 | }, |
| 251 | { |
| 252 | subject: { kind: "pull_request", id: "pr_one" }, |
| 253 | emoji: { kind: "unicode", value: "🔥" }, |
| 254 | count: 1, |
| 255 | viewerReactionId: null, |
| 256 | reactors: [actor], |
| 257 | }, |
| 258 | ], |
| 259 | }, |
| 260 | }, |
| 261 | ); |
| 262 | const thumbsUp = { |
| 263 | content: "thumbs-up", |
| 264 | count: 3, |
| 265 | actors: ["alice"], |
| 266 | viewerHasReacted: true, |
| 267 | } as const; |
| 268 | assert.deepStrictEqual(result.reactions, [ |
| 269 | { content: "rocket", count: 1, actors: ["alice"], viewerHasReacted: false }, |
| 270 | ]); |
| 271 | assert.deepStrictEqual( |
| 272 | result.comments.map((comment) => [comment.id, comment.reactions]), |
| 273 | [ |
| 274 | ["comment-a", []], |
| 275 | ["comment-b", [thumbsUp]], |
| 276 | ], |
| 277 | ); |
| 278 | assert.deepStrictEqual( |
| 279 | result.reviewThreads[0]?.comments.map((comment) => [comment.id, comment.reactions]), |
| 280 | [ |
| 281 | ["comment-a", []], |
| 282 | ["comment-b", [thumbsUp]], |
| 283 | ], |
| 284 | ); |
| 285 | }); |
| 286 | }); |
| 287 | |
| 288 | describe("toStack", () => { |
| 289 | it("orders members by position and reports heads only where GitCafe gives them", () => { |
| 290 | const { stack } = decodeStackEnvelope({ |
| 291 | stack: { |
| 292 | id: "stack-one", |
| 293 | number: 3, |
| 294 | revision: 1, |
| 295 | landingBase: "main", |
| 296 | members: [ |
| 297 | { |
| 298 | pullRequestNumber: 7, |
| 299 | title: "Top", |
| 300 | state: "open", |
| 301 | draft: true, |
| 302 | sourceBranch: "feature", |
| 303 | position: 2, |
| 304 | }, |
| 305 | { |
| 306 | pullRequestNumber: 6, |
| 307 | title: "Base", |
| 308 | state: "merged", |
| 309 | draft: false, |
| 310 | sourceBranch: "base", |
| 311 | headOid: "0123456789abcdef0123456789abcdef01234567", |
| 312 | position: 1, |
| 313 | }, |
| 314 | ], |
| 315 | }, |
| 316 | }); |
| 317 | assert.isNotNull(stack); |
| 318 | const result = GitCafePullRequestJson.toStack(stack!, { |
| 319 | repository: "owner/repo", |
| 320 | host: "staging.git.cafe", |
| 321 | }); |
| 322 | assert.strictEqual(result.url, "https://staging.git.cafe/owner/repo/stacks/3"); |
| 323 | assert.strictEqual(result.base, "main"); |
| 324 | assert.deepStrictEqual(result.layers, [ |
| 325 | { |
| 326 | number: 6, |
| 327 | title: "Base", |
| 328 | headBranch: "base", |
| 329 | state: "merged", |
| 330 | isDraft: false, |
| 331 | headSha: "0123456789abcdef0123456789abcdef01234567", |
| 332 | }, |
| 333 | { number: 7, title: "Top", headBranch: "feature", state: "open", isDraft: true }, |
| 334 | ]); |
| 335 | }); |
| 336 | }); |
| 337 | |
| 338 | describe("toDiff", () => { |
| 339 | it.each(["+", "-", " "])("marks unterminated ordinary %j lines", (origin) => { |
| 340 | const { patch } = GitCafePullRequestJson.toDiff( |
| 341 | decodeDiff({ |
| 342 | items: [ |
| 343 | { |
| 344 | path: "file.ts", |
| 345 | status: "modified", |
| 346 | hunks: [ |
| 347 | { |
| 348 | oldStart: 1, |
| 349 | oldLines: origin === "+" ? 0 : 1, |
| 350 | newStart: 1, |
| 351 | newLines: origin === "-" ? 0 : 1, |
| 352 | lines: [{ origin, content: "value" }], |
| 353 | }, |
| 354 | ], |
| 355 | }, |
| 356 | ], |
| 357 | }), |
| 358 | ); |
| 359 | assert.isTrue(patch.endsWith(`${origin}value\n\\ No newline at end of file\n`)); |
| 360 | }); |
| 361 | |
| 362 | it.each([ |
| 363 | ["+", ">"], |
| 364 | ["-", "<"], |
| 365 | [" ", "="], |
| 366 | ])("emits one marker for an explicit %s/%s EOF pair", (origin, marker) => { |
| 367 | const { patch } = GitCafePullRequestJson.toDiff( |
| 368 | decodeDiff({ |
| 369 | items: [ |
| 370 | { |
| 371 | path: "file.ts", |
| 372 | status: "modified", |
| 373 | hunks: [ |
| 374 | { |
| 375 | oldStart: 1, |
| 376 | oldLines: origin === "+" ? 0 : 1, |
| 377 | newStart: 1, |
| 378 | newLines: origin === "-" ? 0 : 1, |
| 379 | lines: [ |
| 380 | { origin, content: "value" }, |
| 381 | { origin: marker, content: "\n" }, |
| 382 | ], |
| 383 | }, |
| 384 | ], |
| 385 | }, |
| 386 | ], |
| 387 | }), |
| 388 | ); |
| 389 | assert.strictEqual(patch.split("\\ No newline at end of file").length - 1, 1); |
| 390 | }); |
| 391 | |
| 392 | it.each([false, true])("preserves rename metadata for changed=%s", (changed) => { |
| 393 | const { patch } = GitCafePullRequestJson.toDiff( |
| 394 | decodeDiff({ |
| 395 | items: [ |
| 396 | { |
| 397 | path: "after.ts", |
| 398 | oldPath: "before.ts", |
| 399 | status: "renamed", |
| 400 | hunks: changed |
| 401 | ? [ |
| 402 | { |
| 403 | oldStart: 1, |
| 404 | oldLines: 1, |
| 405 | newStart: 1, |
| 406 | newLines: 1, |
| 407 | lines: [ |
| 408 | { origin: "-", content: "before\n" }, |
| 409 | { origin: "+", content: "after\n" }, |
| 410 | ], |
| 411 | }, |
| 412 | ] |
| 413 | : [], |
| 414 | }, |
| 415 | ], |
| 416 | }), |
| 417 | ); |
| 418 | assert.include(patch, changed ? "similarity index 0%\n" : "similarity index 100%\n"); |
| 419 | assert.include(patch, "rename from before.ts\nrename to after.ts\n"); |
| 420 | }); |
| 421 | |
| 422 | it("converts numeric hunk coordinates into a unified patch", () => { |
| 423 | const diff = decodeDiff({ |
| 424 | items: [ |
| 425 | { |
| 426 | path: "file.ts", |
| 427 | oldPath: null, |
| 428 | status: "modified", |
| 429 | additions: 1, |
| 430 | deletions: 1, |
| 431 | binary: false, |
| 432 | hunks: [ |
| 433 | { |
| 434 | oldStart: 1, |
| 435 | oldLines: 1, |
| 436 | newStart: 1, |
| 437 | newLines: 1, |
| 438 | lines: [ |
| 439 | { origin: "-", content: "old\n" }, |
| 440 | { origin: "+", content: "new\n" }, |
| 441 | ], |
| 442 | }, |
| 443 | ], |
| 444 | }, |
| 445 | ], |
| 446 | truncated: false, |
| 447 | }); |
| 448 | assert.include(GitCafePullRequestJson.toDiff(diff).patch, "@@ -1,1 +1,1 @@\n-old\n+new\n"); |
| 449 | }); |
| 450 | |
| 451 | it("marks an omitted patch incomplete even when GitCafe's own flag is false", () => { |
| 452 | const diff = decodeDiff({ |
| 453 | items: [{ path: "large.ts", oldPath: null, status: "modified", tooLarge: true, hunks: [] }], |
| 454 | truncated: false, |
| 455 | }); |
| 456 | const result = GitCafePullRequestJson.toDiff(diff); |
| 457 | assert.isTrue(result.truncated); |
| 458 | assert.deepStrictEqual(result.omittedFileStats, [ |
| 459 | { path: "large.ts", additions: 0, deletions: 0 }, |
| 460 | ]); |
| 461 | }); |
| 462 | |
| 463 | it("quotes renamed and binary paths", () => { |
| 464 | const { patch } = GitCafePullRequestJson.toDiff( |
| 465 | decodeDiff({ |
| 466 | items: [ |
| 467 | { |
| 468 | path: "new image.png", |
| 469 | oldPath: "old image.png", |
| 470 | status: "renamed", |
| 471 | binary: true, |
| 472 | hunks: [], |
| 473 | }, |
| 474 | ], |
| 475 | truncated: false, |
| 476 | }), |
| 477 | ); |
| 478 | assert.include(patch, "rename from old image.png"); |
| 479 | assert.include(patch, "Binary files a/old image.png and b/new image.png differ"); |
| 480 | }); |
| 481 | |
| 482 | it("C-escapes a name whose newline or tab would break the patch headers", () => { |
| 483 | const { patch } = GitCafePullRequestJson.toDiff( |
| 484 | decodeDiff({ |
| 485 | items: [{ path: "two\nlines\tname.ts", status: "added", hunks: [] }], |
| 486 | truncated: false, |
| 487 | }), |
| 488 | ); |
| 489 | assert.include(patch, 'diff --git "a/two\\nlines\\tname.ts" "b/two\\nlines\\tname.ts"'); |
| 490 | assert.notInclude(patch, "two\nlines"); |
| 491 | }); |
| 492 | }); |
| 493 | |
| 494 | describe("toChecks", () => { |
| 495 | it("maps GitCafe's status and conclusion onto the shared check states", () => { |
| 496 | const check = ( |
| 497 | status: string, |
| 498 | conclusion: string | null, |
| 499 | name = `${status}-${conclusion}`, |
| 500 | ) => ({ |
| 501 | name, |
| 502 | status, |
| 503 | conclusion, |
| 504 | summary: null, |
| 505 | detailsUrl: null, |
| 506 | }); |
| 507 | const checks = GitCafePullRequestJson.toChecks( |
| 508 | decodeChecks({ |
| 509 | items: [ |
| 510 | check("queued", null), |
| 511 | check("in_progress", null), |
| 512 | check("completed", "success", "Build"), |
| 513 | check("completed", "cancelled"), |
| 514 | check("completed", "action_required"), |
| 515 | check("completed", "timed_out", "Lint"), |
| 516 | ], |
| 517 | next: null, |
| 518 | }), |
| 519 | ); |
| 520 | assert.deepStrictEqual( |
| 521 | checks.map((item) => [item.name, item.status]), |
| 522 | [ |
| 523 | ["completed-cancelled", "cancelled"], |
| 524 | ["Lint", "failure"], |
| 525 | ["queued-null", "pending"], |
| 526 | ["in_progress-null", "pending"], |
| 527 | ["Build", "success"], |
| 528 | ["completed-action_required", "action-required"], |
| 529 | ], |
| 530 | ); |
| 531 | }); |
| 532 | |
| 533 | it("keeps only the newest run of a re-run check", () => { |
| 534 | const run = (id: string, conclusion: string, githubApp = "github-actions") => ({ |
| 535 | id, |
| 536 | provider: "github", |
| 537 | githubApp, |
| 538 | name: "Check", |
| 539 | status: "completed", |
| 540 | conclusion, |
| 541 | summary: null, |
| 542 | detailsUrl: null, |
| 543 | }); |
| 544 | const statusOf = (items: ReadonlyArray<ReturnType<typeof run>>) => |
| 545 | GitCafePullRequestJson.toChecks(decodeChecks({ items, next: null })).map( |
| 546 | (check) => check.status, |
| 547 | ); |
| 548 | assert.deepStrictEqual(statusOf([run("99", "failure"), run("100", "success")]), ["success"]); |
| 549 | assert.deepStrictEqual(statusOf([run("101", "failure"), run("100", "success")]), ["failure"]); |
| 550 | // Another app's run of the same name is a separate check, not a re-run. |
| 551 | assert.deepStrictEqual(statusOf([run("100", "failure", "a"), run("101", "success", "b")]), [ |
| 552 | "failure", |
| 553 | "success", |
| 554 | ]); |
| 555 | // Commit statuses carry no run order, so neither is taken for the other's re-run. |
| 556 | assert.deepStrictEqual(statusOf([run("status:2", "failure"), run("status:1", "success")]), [ |
| 557 | "failure", |
| 558 | "success", |
| 559 | ]); |
| 560 | }); |
| 561 | }); |