The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 5 | |
| 6 | import * as GitLabCli from "./GitLabCli.ts"; |
| 7 | import * as GitLabIssueTracker from "./GitLabIssueTracker.ts"; |
| 8 | |
| 9 | const encode = (value: unknown) => JSON.stringify(value); |
| 10 | |
| 11 | const ISSUE = { |
| 12 | iid: 42, |
| 13 | title: "Composer loses focus", |
| 14 | web_url: "https://gitlab.com/acme/web/-/issues/42", |
| 15 | description: "Typing in the composer drops focus after a paste.", |
| 16 | author: { id: 1, username: "octocat", name: "Octo Cat", avatar_url: "https://avatars/octocat" }, |
| 17 | state: "closed", |
| 18 | created_at: "2026-07-01T00:00:00Z", |
| 19 | updated_at: "2026-07-03T00:00:00Z", |
| 20 | closed_at: "2026-07-03T00:00:00Z", |
| 21 | assignees: [{ id: 2, username: "theo", name: null }], |
| 22 | labels: ["bug", " "], |
| 23 | user_notes_count: 3, |
| 24 | }; |
| 25 | |
| 26 | /** Every endpoint the detail read touches, answered by the path `glab api` is given. */ |
| 27 | function answer(path: string): string { |
| 28 | if (path === "user") return encode({ username: "julius" }); |
| 29 | if (path === "projects/acme%2Fweb/issues?iids[]=42&with_labels_details=true&per_page=1") { |
| 30 | return encode([ |
| 31 | { |
| 32 | ...ISSUE, |
| 33 | labels: [ |
| 34 | { name: "bug", color: "#E24329", description: "Something is broken" }, |
| 35 | { name: " ", color: "#000000" }, |
| 36 | ], |
| 37 | }, |
| 38 | ]); |
| 39 | } |
| 40 | if (path.startsWith("projects/acme%2Fweb/issues/42/notes")) { |
| 41 | return encode([ |
| 42 | { |
| 43 | id: 7, |
| 44 | body: "Repro attached.", |
| 45 | author: { username: "julius" }, |
| 46 | created_at: "2026-07-02T00:00:00Z", |
| 47 | }, |
| 48 | { |
| 49 | id: 8, |
| 50 | body: "closed", |
| 51 | author: { username: "theo" }, |
| 52 | created_at: "2026-07-03T00:00:00Z", |
| 53 | system: true, |
| 54 | }, |
| 55 | { |
| 56 | id: 9, |
| 57 | body: "added ~7 label", |
| 58 | author: { username: "theo" }, |
| 59 | created_at: "2026-07-03T00:00:00Z", |
| 60 | system: true, |
| 61 | }, |
| 62 | ]); |
| 63 | } |
| 64 | if (path.startsWith("projects/acme%2Fweb/issues/42/resource_label_events")) { |
| 65 | return encode([ |
| 66 | { |
| 67 | id: 3, |
| 68 | action: "add", |
| 69 | created_at: "2026-07-01T01:00:00Z", |
| 70 | user: { username: "theo" }, |
| 71 | label: { name: "bug" }, |
| 72 | }, |
| 73 | ]); |
| 74 | } |
| 75 | if (path.startsWith("projects/acme%2Fweb/issues/42/closed_by")) { |
| 76 | return encode([ |
| 77 | { |
| 78 | iid: 50, |
| 79 | title: "fix(web): keep composer focus", |
| 80 | web_url: "https://gitlab.com/acme/web/-/merge_requests/50", |
| 81 | state: "merged", |
| 82 | references: { full: "acme/web!50" }, |
| 83 | }, |
| 84 | ]); |
| 85 | } |
| 86 | if (path.startsWith("projects/acme%2Fweb/issues/42/related_merge_requests")) { |
| 87 | return encode([ |
| 88 | { |
| 89 | iid: 50, |
| 90 | title: "fix(web): keep composer focus", |
| 91 | web_url: "https://gitlab.com/acme/web/-/merge_requests/50", |
| 92 | state: "merged", |
| 93 | references: { full: "acme/web!50" }, |
| 94 | }, |
| 95 | { |
| 96 | iid: 51, |
| 97 | title: "chore: mention", |
| 98 | web_url: "https://gitlab.com/acme/web/-/merge_requests/51", |
| 99 | state: "opened", |
| 100 | draft: true, |
| 101 | references: { full: "acme/web!51" }, |
| 102 | }, |
| 103 | ]); |
| 104 | } |
| 105 | if (path.startsWith("projects/acme%2Fweb/issues/42/award_emoji")) { |
| 106 | return encode([{ id: 11, name: "eyes", user: { username: "theo" } }]); |
| 107 | } |
| 108 | if (path === "graphql") { |
| 109 | return encode({ |
| 110 | data: { |
| 111 | currentUser: { username: "julius" }, |
| 112 | project: { |
| 113 | mergeRequest: { |
| 114 | notes: { |
| 115 | pageInfo: { hasNextPage: false }, |
| 116 | nodes: [ |
| 117 | { |
| 118 | id: "gid://gitlab/Note/7", |
| 119 | awardEmoji: { |
| 120 | nodes: [ |
| 121 | { name: "thumbsup", user: { username: "julius" } }, |
| 122 | { name: "thumbsup", user: { username: "theo" } }, |
| 123 | ], |
| 124 | }, |
| 125 | }, |
| 126 | ], |
| 127 | }, |
| 128 | }, |
| 129 | }, |
| 130 | }, |
| 131 | }); |
| 132 | } |
| 133 | throw new Error(`unexpected path ${path}`); |
| 134 | } |
| 135 | |
| 136 | function tracker( |
| 137 | onExecute: ( |
| 138 | args: ReadonlyArray<string>, |
| 139 | input: { readonly host?: string | undefined; readonly stdin?: string }, |
| 140 | ) => string, |
| 141 | ) { |
| 142 | return GitLabIssueTracker.make.pipe( |
| 143 | Effect.provide( |
| 144 | Layer.mock(GitLabCli.GitLabCli)({ |
| 145 | execute: (input) => |
| 146 | Effect.succeed(TestSourceControlHost.processOutput(onExecute(input.args, input))), |
| 147 | }), |
| 148 | ), |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | describe("GitLabIssueTracker", () => { |
| 153 | it.effect("joins the detail's endpoints into the neutral issue shape", () => |
| 154 | Effect.gen(function* () { |
| 155 | const hosts = new Set<string | undefined>(); |
| 156 | const awardCursors: unknown[] = []; |
| 157 | const api = yield* tracker((args, input) => { |
| 158 | hosts.add(input.host); |
| 159 | if (args[1] === "graphql") { |
| 160 | const request = JSON.parse(input.stdin!); |
| 161 | // gitlab.com's schema has no `Issue.awardEmoji`; glab exits 1 on such a query. |
| 162 | const issueFields = /issue\(iid: \$iid\) \{([\s\S]*?)notes\(/.exec(request.query)?.[1]; |
| 163 | if (issueFields?.includes("awardEmoji")) { |
| 164 | throw new Error("Field 'awardEmoji' doesn't exist on type 'Issue'"); |
| 165 | } |
| 166 | awardCursors.push(request.variables.cursor); |
| 167 | } |
| 168 | return answer(args[1]!); |
| 169 | }); |
| 170 | const target = { cwd: "/w", host: "gitlab.example.com", scope: "acme/web", key: "42" }; |
| 171 | const issue = yield* api.getIssue(target); |
| 172 | // A later page of comments carries its own awards, found by the cursor's second half. |
| 173 | const later = yield* api.getComments({ ...target, cursor: "2:after-100" }); |
| 174 | expect(later.comments.map((comment) => comment.reactions?.[0]?.count)).toEqual([2]); |
| 175 | expect(awardCursors).toEqual([null, "after-100"]); |
| 176 | // Every request names the tracker's own host rather than leaving glab to pick one. |
| 177 | expect([...hosts]).toEqual(["gitlab.example.com"]); |
| 178 | expect(issue).toMatchObject({ |
| 179 | key: "42", |
| 180 | displayKey: "#42", |
| 181 | state: { category: "closed", name: "Closed" }, |
| 182 | states: [ |
| 183 | { category: "open", name: "Open" }, |
| 184 | { category: "closed", name: "Closed" }, |
| 185 | ], |
| 186 | author: { login: "octocat", name: "Octo Cat" }, |
| 187 | assignees: [{ login: "theo" }], |
| 188 | labels: [{ name: "bug", color: "#E24329", description: "Something is broken" }], |
| 189 | commentCount: 3, |
| 190 | viewer: "julius", |
| 191 | viewerPermissions: { setState: true, edit: true, labels: true, assignees: true }, |
| 192 | commentsCursor: null, |
| 193 | reactions: [{ content: "eyes", count: 1, actors: ["theo"], viewerHasReacted: false }], |
| 194 | }); |
| 195 | expect(issue.comments).toEqual([ |
| 196 | { |
| 197 | id: "7", |
| 198 | author: { login: "julius", name: null, avatarUrl: null }, |
| 199 | body: "Repro attached.", |
| 200 | createdAt: "2026-07-02T00:00:00Z", |
| 201 | url: null, |
| 202 | reactions: [{ content: "thumbs-up", count: 2, actors: ["theo"], viewerHasReacted: true }], |
| 203 | }, |
| 204 | ]); |
| 205 | // System notes become events; label notes are dropped in favour of the label events. |
| 206 | expect(issue.events.map((event) => [event.kind, event.detail])).toEqual([ |
| 207 | ["labeled", "bug"], |
| 208 | ["closed", null], |
| 209 | ]); |
| 210 | // A closing merge request is not repeated as a related one. |
| 211 | expect( |
| 212 | issue.linkedPullRequests.map((link) => [link.number, link.closesIssue, link.isDraft]), |
| 213 | ).toEqual([ |
| 214 | [50, true, false], |
| 215 | [51, false, true], |
| 216 | ]); |
| 217 | }), |
| 218 | ); |
| 219 | |
| 220 | it.effect("loads the detail without comment reactions where GitLab has no Note.awardEmoji", () => |
| 221 | Effect.gen(function* () { |
| 222 | // GitLab before 16.1 has no `Note.awardEmoji`; glab exits 1 on any query naming it. |
| 223 | const api = yield* GitLabIssueTracker.make.pipe( |
| 224 | Effect.provide( |
| 225 | Layer.mock(GitLabCli.GitLabCli)({ |
| 226 | execute: (input) => |
| 227 | input.args[1] === "graphql" |
| 228 | ? Effect.fail( |
| 229 | new GitLabCli.GitLabCliCommandError({ |
| 230 | operation: "execute", |
| 231 | command: "glab", |
| 232 | cwd: input.cwd, |
| 233 | cause: new Error("glab: Field 'awardEmoji' doesn't exist on type 'Note'"), |
| 234 | }), |
| 235 | ) |
| 236 | : Effect.succeed(TestSourceControlHost.processOutput(answer(input.args[1]!))), |
| 237 | }), |
| 238 | ), |
| 239 | ); |
| 240 | const target = { cwd: "/w", host: "gitlab.example.com", scope: "acme/web", key: "42" }; |
| 241 | const issue = yield* api.getIssue(target); |
| 242 | expect(issue.comments.map((comment) => [comment.id, comment.reactions])).toEqual([["7", []]]); |
| 243 | expect(issue.reactions).toEqual([ |
| 244 | { content: "eyes", count: 1, actors: ["theo"], viewerHasReacted: false }, |
| 245 | ]); |
| 246 | const later = yield* api.getComments({ ...target, cursor: "2:after-100" }); |
| 247 | expect(later.comments.map((comment) => comment.reactions)).toEqual([[]]); |
| 248 | }), |
| 249 | ); |
| 250 | |
| 251 | it.effect("keeps system events after the first hundred notes in the issue timeline", () => |
| 252 | Effect.gen(function* () { |
| 253 | const firstPage = Array.from({ length: 100 }, (_, index) => ({ |
| 254 | id: index + 1000, |
| 255 | body: "Discussion", |
| 256 | author: { username: "julius" }, |
| 257 | created_at: "2026-07-02T00:00:00Z", |
| 258 | })); |
| 259 | const api = yield* tracker((args) => { |
| 260 | const path = args[1]!; |
| 261 | if (path.includes("/notes?")) { |
| 262 | const page = new URLSearchParams(path.split("?")[1]).get("page"); |
| 263 | if (page === "1") return encode(firstPage); |
| 264 | if (page === "2") { |
| 265 | return encode([ |
| 266 | { |
| 267 | id: 1100, |
| 268 | body: "closed", |
| 269 | system: true, |
| 270 | author: { username: "theo" }, |
| 271 | created_at: "2026-07-03T00:00:00Z", |
| 272 | }, |
| 273 | ]); |
| 274 | } |
| 275 | return encode([]); |
| 276 | } |
| 277 | return answer(path); |
| 278 | }); |
| 279 | const target = { cwd: "/w", host: "gitlab.com", scope: "acme/web", key: "42" }; |
| 280 | const issue = yield* api.getIssue(target); |
| 281 | let cursor = issue.commentsCursor; |
| 282 | while (cursor !== null) { |
| 283 | cursor = (yield* api.getComments({ ...target, cursor })).nextCursor; |
| 284 | } |
| 285 | expect(issue.events).toContainEqual( |
| 286 | expect.objectContaining({ id: "note-1100", kind: "closed" }), |
| 287 | ); |
| 288 | }), |
| 289 | ); |
| 290 | |
| 291 | it.effect("follows GitLab's next page past a short page of notes", () => |
| 292 | Effect.gen(function* () { |
| 293 | const notes = (next: string, rows: ReadonlyArray<unknown>) => |
| 294 | `HTTP/2.0 200 OK\r\nContent-Type: application/json\r\nX-Next-Page: ${next}\r\nX-Page: 1\r\n\r\n${encode(rows)}`; |
| 295 | const api = yield* tracker((args) => { |
| 296 | const path = args[1]!; |
| 297 | if (path.includes("/notes?")) { |
| 298 | const params = new URLSearchParams(path.split("?")[1]); |
| 299 | if (params.get("activity_filter") === "only_activity") { |
| 300 | return params.get("page") === "1" |
| 301 | ? notes("2", []) |
| 302 | : notes("", [ |
| 303 | { |
| 304 | id: 300, |
| 305 | body: "closed", |
| 306 | system: true, |
| 307 | author: { username: "theo" }, |
| 308 | created_at: "2026-07-03T00:00:00Z", |
| 309 | }, |
| 310 | ]); |
| 311 | } |
| 312 | return notes("2", [ |
| 313 | { |
| 314 | id: 1, |
| 315 | body: "Hi", |
| 316 | author: { username: "julius" }, |
| 317 | created_at: "2026-07-02T00:00:00Z", |
| 318 | }, |
| 319 | ]); |
| 320 | } |
| 321 | return answer(path); |
| 322 | }); |
| 323 | const issue = yield* api.getIssue({ |
| 324 | cwd: "/w", |
| 325 | host: "gitlab.com", |
| 326 | scope: "acme/web", |
| 327 | key: "42", |
| 328 | }); |
| 329 | expect(issue.commentsCursor).toBe("2"); |
| 330 | expect(issue.events.map((event) => event.id)).toEqual(["label-3", "note-300"]); |
| 331 | }), |
| 332 | ); |
| 333 | |
| 334 | it.effect("pages a listing by GitLab's page number and stops on a short page", () => |
| 335 | Effect.gen(function* () { |
| 336 | const paths: string[] = []; |
| 337 | const api = yield* tracker((args) => { |
| 338 | paths.push(args[1]!); |
| 339 | return encode([ISSUE]); |
| 340 | }); |
| 341 | const page = yield* api.listIssues({ |
| 342 | cwd: "/w", |
| 343 | host: "gitlab.com", |
| 344 | scope: "acme/web", |
| 345 | state: "open", |
| 346 | involvement: "assigned", |
| 347 | viewer: "julius", |
| 348 | sort: "updated", |
| 349 | limit: 1, |
| 350 | cursor: "3", |
| 351 | }); |
| 352 | expect(paths[0]).toBe( |
| 353 | "projects/acme%2Fweb/issues?state=opened&assignee_username=julius&order_by=updated_at&sort=desc&with_labels_details=true&per_page=1&page=3", |
| 354 | ); |
| 355 | // A named assignee takes the one parameter GitLab reads, so "assigned to me" drops rows here. |
| 356 | const both = yield* api.listIssues({ |
| 357 | cwd: "/w", |
| 358 | host: "gitlab.com", |
| 359 | scope: "acme/web", |
| 360 | state: "open", |
| 361 | involvement: "assigned", |
| 362 | viewer: "julius", |
| 363 | labels: ["bug", "needs triage"], |
| 364 | assignee: "theo", |
| 365 | sort: "created", |
| 366 | limit: 1, |
| 367 | }); |
| 368 | expect(paths[1]).toBe( |
| 369 | "projects/acme%2Fweb/issues?state=opened&labels=bug%2Cneeds%20triage&assignee_username=theo&order_by=created_at&sort=desc&with_labels_details=true&per_page=1&page=1", |
| 370 | ); |
| 371 | expect(both.items).toEqual([]); |
| 372 | expect(page.items[0]?.key).toBe("42"); |
| 373 | // A host that ignores with_labels_details still sends names, which keep their chips. |
| 374 | expect(page.items[0]?.labels).toEqual([{ name: "bug", color: null }]); |
| 375 | expect(page.nextCursor).toBe("4"); |
| 376 | }), |
| 377 | ); |
| 378 | |
| 379 | it.effect("removes the viewer's own award by id, paging until it is found", () => |
| 380 | Effect.gen(function* () { |
| 381 | const calls: string[] = []; |
| 382 | const api = yield* tracker((args) => { |
| 383 | const path = args[1]!; |
| 384 | calls.push(args.includes("DELETE") ? `DELETE ${path}` : path); |
| 385 | if (path === "user") return encode({ username: "julius" }); |
| 386 | if (path.endsWith("page=1")) { |
| 387 | return encode([{ id: 1, name: "thumbsup", user: { username: "theo" } }]); |
| 388 | } |
| 389 | if (path.endsWith("page=2") && path.includes("/notes/7/")) { |
| 390 | return encode([{ id: 9, name: "thumbsup", user: { username: "julius" } }]); |
| 391 | } |
| 392 | return "[]"; |
| 393 | }); |
| 394 | const target = { cwd: "/w", host: "gitlab.com", scope: "acme/web", key: "42" }; |
| 395 | yield* api.setReaction!({ ...target, subjectId: "7", content: "thumbs-up", reacted: false }); |
| 396 | expect(calls.at(-1)).toBe("DELETE projects/acme%2Fweb/issues/42/notes/7/award_emoji/9"); |
| 397 | // Nothing to delete is success, with no DELETE sent. |
| 398 | calls.length = 0; |
| 399 | yield* api.setReaction!({ ...target, content: "thumbs-up", reacted: false }); |
| 400 | expect(calls).toEqual([ |
| 401 | "user", |
| 402 | "projects/acme%2Fweb/issues/42/award_emoji?per_page=100&page=1", |
| 403 | "projects/acme%2Fweb/issues/42/award_emoji?per_page=100&page=2", |
| 404 | ]); |
| 405 | }), |
| 406 | ); |
| 407 | |
| 408 | it.effect("writes a kept assignee by the id its username resolves to", () => |
| 409 | Effect.gen(function* () { |
| 410 | const calls: string[] = []; |
| 411 | const bodies: string[] = []; |
| 412 | const api = yield* tracker((args, input) => { |
| 413 | const path = args[1]!; |
| 414 | calls.push(path); |
| 415 | if (input.stdin !== undefined) bodies.push(input.stdin); |
| 416 | if (path === "users?username=theo") return encode([{ id: 2, username: "theo" }]); |
| 417 | if (path.startsWith("users?username=")) return "[]"; |
| 418 | return "{}"; |
| 419 | }); |
| 420 | const target = { cwd: "/w", host: "gitlab.com", scope: "acme/web", key: "42" }; |
| 421 | // The issue's own assignee comes back by login; a newly picked one by id. |
| 422 | yield* api.update!({ ...target, assignees: ["theo", "7"] }); |
| 423 | expect(calls).toEqual(["users?username=theo", "projects/acme%2Fweb/issues/42"]); |
| 424 | expect(JSON.parse(bodies.at(-1)!)).toEqual({ assignee_ids: [2, 7] }); |
| 425 | const error = yield* Effect.flip(api.update!({ ...target, assignees: ["nobody"] })); |
| 426 | expect(error.reason).toBe("failed"); |
| 427 | }), |
| 428 | ); |
| 429 | |
| 430 | it.effect("moves an issue to a state by its name and refuses any other name", () => |
| 431 | Effect.gen(function* () { |
| 432 | const writes: unknown[] = []; |
| 433 | const api = yield* tracker((args, input) => { |
| 434 | writes.push([args[1], JSON.parse(input.stdin ?? "null")]); |
| 435 | return "{}"; |
| 436 | }); |
| 437 | const target = { cwd: "/w", host: "gitlab.com", scope: "acme/web", key: "42" }; |
| 438 | yield* api.setState!({ ...target, state: "Closed" }); |
| 439 | yield* api.setState!({ ...target, state: "Open" }); |
| 440 | // The category is not a state name: nothing is sent for it. |
| 441 | const error = yield* Effect.flip(api.setState!({ ...target, state: "closed" })); |
| 442 | expect(error).toMatchObject({ operation: "setState", reason: "failed" }); |
| 443 | expect(writes).toEqual([ |
| 444 | ["projects/acme%2Fweb/issues/42", { state_event: "close" }], |
| 445 | ["projects/acme%2Fweb/issues/42", { state_event: "reopen" }], |
| 446 | ]); |
| 447 | }), |
| 448 | ); |
| 449 | |
| 450 | it.effect("refuses a key that names no GitLab issue without a request", () => |
| 451 | Effect.gen(function* () { |
| 452 | let requests = 0; |
| 453 | const api = yield* tracker(() => { |
| 454 | requests += 1; |
| 455 | return "{}"; |
| 456 | }); |
| 457 | const error = yield* Effect.flip( |
| 458 | api.getIssue({ cwd: "/w", host: "gitlab.com", scope: "acme/web", key: "ENG-42" }), |
| 459 | ); |
| 460 | expect(error.reason).toBe("not-found"); |
| 461 | expect(requests).toBe(0); |
| 462 | }), |
| 463 | ); |
| 464 | }); |