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 { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Context from "effect/Context"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import type * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 10 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 11 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 12 | |
| 13 | import * as GitCafeApi from "./GitCafeApi.ts"; |
| 14 | import * as GitCafeCredentials from "./GitCafeCredentials.ts"; |
| 15 | import * as GitCafePullRequestProvider from "./GitCafePullRequestProvider.ts"; |
| 16 | |
| 17 | const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); |
| 18 | const decodeJson = Schema.decodeUnknownSync(Schema.fromJsonString(Schema.Unknown)); |
| 19 | |
| 20 | interface SentRequest { |
| 21 | readonly method: string; |
| 22 | readonly path: string; |
| 23 | readonly query: URLSearchParams; |
| 24 | readonly host: string; |
| 25 | readonly body: unknown; |
| 26 | readonly redirect: string | undefined; |
| 27 | } |
| 28 | |
| 29 | function fakeGitCafe( |
| 30 | routes: Record<string, unknown | ((request: SentRequest) => unknown)>, |
| 31 | options: { readonly status?: number; readonly env?: NodeJS.ProcessEnv } = {}, |
| 32 | ) { |
| 33 | const sent: Array<SentRequest> = []; |
| 34 | const client = HttpClient.make( |
| 35 | (request: HttpClientRequest.HttpClientRequest, _url, _signal, fiber) => { |
| 36 | const url = new URL(request.url); |
| 37 | assert.isTrue(url.pathname.startsWith("/api/"), url.pathname); |
| 38 | assert.strictEqual(request.headers.authorization, "Bearer env-token"); |
| 39 | const entry: SentRequest = { |
| 40 | method: request.method, |
| 41 | path: url.pathname.slice("/api".length), |
| 42 | query: url.searchParams, |
| 43 | host: url.host, |
| 44 | body: |
| 45 | request.body._tag === "Uint8Array" |
| 46 | ? decodeJson(new TextDecoder().decode(request.body.body)) |
| 47 | : undefined, |
| 48 | redirect: Context.getOrUndefined(fiber.context, FetchHttpClient.RequestInit)?.redirect, |
| 49 | }; |
| 50 | sent.push(entry); |
| 51 | const route = routes[`${entry.method} ${entry.path}`]; |
| 52 | const response = |
| 53 | route === undefined |
| 54 | ? new Response(encodeJson({ type: "https://cafe.sh/errors/not-found" }), { status: 404 }) |
| 55 | : route instanceof Response |
| 56 | ? route |
| 57 | : new Response(encodeJson(typeof route === "function" ? route(entry) : route), { |
| 58 | status: options.status ?? 200, |
| 59 | }); |
| 60 | return Effect.succeed(HttpClientResponse.fromWeb(request, response)); |
| 61 | }, |
| 62 | ); |
| 63 | const layer = GitCafeApi.layer.pipe( |
| 64 | Layer.provide(GitCafeCredentials.layer), |
| 65 | Layer.provide(TestSourceControlHost.layer()), |
| 66 | Layer.provide( |
| 67 | Layer.succeed(HostProcess.Environment, options.env ?? { CAFE_TOKEN: "env-token" }), |
| 68 | ), |
| 69 | Layer.provide(Layer.succeed(HostProcess.WorkingDirectory, "/server")), |
| 70 | Layer.provide(Layer.succeed(HttpClient.HttpClient, client)), |
| 71 | ); |
| 72 | const writes = () => |
| 73 | sent.filter((request) => request.method !== "GET").map((r) => `${r.method} ${r.path}`); |
| 74 | return { layer, sent, writes }; |
| 75 | } |
| 76 | |
| 77 | const target = { cwd: "/repo", host: "git.cafe", repository: "owner/repo", number: 7 }; |
| 78 | const timestamp = "2026-09-12T12:00:00Z"; |
| 79 | const headOid = "0123456789abcdef0123456789abcdef01234567"; |
| 80 | const baseOid = "89abcdef0123456789abcdef0123456789abcdef"; |
| 81 | const actor = { |
| 82 | kind: "local", |
| 83 | actorId: "act_one", |
| 84 | handle: "alice", |
| 85 | displayName: null, |
| 86 | avatarUrl: "/avatars/one", |
| 87 | }; |
| 88 | const pull = { |
| 89 | id: "pr_one", |
| 90 | number: 7, |
| 91 | title: "Change", |
| 92 | state: "open", |
| 93 | draft: false, |
| 94 | sourceBranch: "feature", |
| 95 | targetBranch: "main", |
| 96 | headOid, |
| 97 | observedBaseOid: baseOid, |
| 98 | mergeRoute: "provider", |
| 99 | author: actor, |
| 100 | createdAt: timestamp, |
| 101 | updatedAt: timestamp, |
| 102 | version: 4, |
| 103 | description: "Body", |
| 104 | closedAt: null, |
| 105 | mergedAt: null, |
| 106 | sourceRepo: null, |
| 107 | capabilities: { comment: true, review: true, merge: true, edit: true, moderate: true }, |
| 108 | }; |
| 109 | const labels = [ |
| 110 | { id: "label_bug", name: "bug", color: "#d73a4a", description: null }, |
| 111 | { id: "label_docs", name: "docs", color: "#0075ca", description: "Documentation" }, |
| 112 | ]; |
| 113 | const root = { |
| 114 | id: "root", |
| 115 | threadId: "root", |
| 116 | version: 2, |
| 117 | author: actor, |
| 118 | body: "Review", |
| 119 | path: "file.ts", |
| 120 | line: 3, |
| 121 | side: "right", |
| 122 | createdAt: timestamp, |
| 123 | commitOid: headOid, |
| 124 | resolvedAt: null, |
| 125 | capabilities: { |
| 126 | edit: true, |
| 127 | hide: false, |
| 128 | unhide: false, |
| 129 | delete: false, |
| 130 | resolve: true, |
| 131 | unresolve: false, |
| 132 | }, |
| 133 | }; |
| 134 | const stack = ( |
| 135 | members: ReadonlyArray<{ number: number; headOid: string; state?: string }>, |
| 136 | ): Record<string, unknown> => ({ |
| 137 | "GET /repos/owner/repo/pulls/7/stack": { |
| 138 | stack: { |
| 139 | id: "stack-one", |
| 140 | number: 3, |
| 141 | revision: 5, |
| 142 | landingBase: "main", |
| 143 | members: members.map((member, index) => ({ |
| 144 | pullRequestNumber: member.number, |
| 145 | title: `Layer ${member.number}`, |
| 146 | state: member.state ?? "open", |
| 147 | draft: false, |
| 148 | sourceBranch: `layer-${member.number}`, |
| 149 | position: index + 1, |
| 150 | })), |
| 151 | }, |
| 152 | }, |
| 153 | ...Object.fromEntries( |
| 154 | members.map((member) => [ |
| 155 | `GET /repos/owner/repo/pulls/${member.number}`, |
| 156 | { ...pull, number: member.number, headOid: member.headOid }, |
| 157 | ]), |
| 158 | ), |
| 159 | }); |
| 160 | |
| 161 | const assertRequestId = (body: unknown) => |
| 162 | assert.match( |
| 163 | (body as { readonly requestId?: unknown } | undefined)?.requestId as string, |
| 164 | /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u, |
| 165 | ); |
| 166 | |
| 167 | describe("GitCafePullRequestProvider changed paths", () => { |
| 168 | const snapshot = { version: pull.version, headOid, comparisonBaseOid: baseOid }; |
| 169 | |
| 170 | it.effect("pages changed paths without fetching diff-file hunks", () => { |
| 171 | const server = fakeGitCafe({ |
| 172 | "GET /repos/owner/repo/pulls/7": pull, |
| 173 | "GET /repos/owner/repo/pulls/7/changes": (request: SentRequest) => ({ |
| 174 | ...snapshot, |
| 175 | items: request.query.has("after") |
| 176 | ? [{ path: "gone.ts", status: "deleted" }] |
| 177 | : [{ path: "new name.ts", oldPath: "old name.ts", status: "renamed" }], |
| 178 | next: request.query.has("after") ? null : "next-page", |
| 179 | }), |
| 180 | }); |
| 181 | return Effect.gen(function* () { |
| 182 | const provider = yield* GitCafePullRequestProvider.make; |
| 183 | const first = yield* provider.getDiffFilePaths!(target); |
| 184 | const second = yield* provider.getDiffFilePaths!({ ...target, cursor: first.nextCursor! }); |
| 185 | assert.deepStrictEqual(first.files, [{ oldPath: "old name.ts", newPath: "new name.ts" }]); |
| 186 | assert.deepStrictEqual(second, { |
| 187 | files: [{ oldPath: "gone.ts", newPath: "gone.ts" }], |
| 188 | nextCursor: null, |
| 189 | }); |
| 190 | assert.isFalse(server.sent.some((request) => request.path.includes("/diff-file"))); |
| 191 | }).pipe(Effect.provide(server.layer)); |
| 192 | }); |
| 193 | |
| 194 | it.effect.each(["getDiff", "getDiffFilePaths"] as const)( |
| 195 | "%s rejects a repeated continuation cursor", |
| 196 | (method) => { |
| 197 | const server = fakeGitCafe({ |
| 198 | "GET /repos/owner/repo/pulls/7": pull, |
| 199 | "GET /repos/owner/repo/pulls/7/changes": { ...snapshot, items: [], next: "same-page" }, |
| 200 | }); |
| 201 | return Effect.gen(function* () { |
| 202 | const provider = yield* GitCafePullRequestProvider.make; |
| 203 | const first = yield* provider[method]!(target); |
| 204 | const input = { ...target, cursor: first.nextCursor! }; |
| 205 | const result = |
| 206 | method === "getDiff" |
| 207 | ? yield* provider.getDiff(input).pipe(Effect.result) |
| 208 | : yield* provider.getDiffFilePaths!(input).pipe(Effect.result); |
| 209 | assert.strictEqual(result._tag, "Failure"); |
| 210 | }).pipe(Effect.provide(server.layer)); |
| 211 | }, |
| 212 | ); |
| 213 | |
| 214 | // getDiff cases guard parity with the existing patch path; getDiffFilePaths must refuse the same pages. |
| 215 | it.effect.each( |
| 216 | (["getDiff", "getDiffFilePaths"] as const).flatMap((method) => |
| 217 | [{ version: 5 }, { headOid: baseOid }, { comparisonBaseOid: headOid }].map((changed) => ({ |
| 218 | method, |
| 219 | changed, |
| 220 | })), |
| 221 | ), |
| 222 | )( |
| 223 | "$method refuses a continuation from a different revision ($changed)", |
| 224 | ({ method, changed }) => { |
| 225 | const server = fakeGitCafe({ |
| 226 | "GET /repos/owner/repo/pulls/7": pull, |
| 227 | "GET /repos/owner/repo/pulls/7/changes": (request: SentRequest) => ({ |
| 228 | ...snapshot, |
| 229 | ...(request.query.has("after") ? changed : {}), |
| 230 | items: [], |
| 231 | next: request.query.has("after") ? null : "next-page", |
| 232 | }), |
| 233 | }); |
| 234 | return Effect.gen(function* () { |
| 235 | const provider = yield* GitCafePullRequestProvider.make; |
| 236 | const read = (cursor?: string) => |
| 237 | method === "getDiff" |
| 238 | ? provider.getDiff({ ...target, ...(cursor === undefined ? {} : { cursor }) }) |
| 239 | : provider.getDiffFilePaths!({ |
| 240 | ...target, |
| 241 | ...(cursor === undefined ? {} : { cursor }), |
| 242 | }); |
| 243 | const first = yield* read(); |
| 244 | const input = { ...target, cursor: first.nextCursor! }; |
| 245 | const result = |
| 246 | method === "getDiff" |
| 247 | ? yield* provider.getDiff(input).pipe(Effect.result) |
| 248 | : yield* provider.getDiffFilePaths!(input).pipe(Effect.result); |
| 249 | assert.strictEqual(result._tag, "Failure"); |
| 250 | }).pipe(Effect.provide(server.layer)); |
| 251 | }, |
| 252 | ); |
| 253 | |
| 254 | it.effect.each(["getDiff", "getDiffFilePaths"] as const)( |
| 255 | "%s reads a pull that reports no head yet", |
| 256 | (method) => { |
| 257 | const server = fakeGitCafe({ |
| 258 | "GET /repos/owner/repo/pulls/7": { ...pull, headOid: null }, |
| 259 | "GET /repos/owner/repo/pulls/7/changes": { ...snapshot, items: [], next: null }, |
| 260 | }); |
| 261 | return Effect.gen(function* () { |
| 262 | const provider = yield* GitCafePullRequestProvider.make; |
| 263 | const result = yield* provider[method]!(target); |
| 264 | assert.isNull(result.nextCursor); |
| 265 | }).pipe(Effect.provide(server.layer)); |
| 266 | }, |
| 267 | ); |
| 268 | |
| 269 | it.effect.each([{ version: 5 }, { headOid: baseOid }])( |
| 270 | "refuses initial changed paths from a different pull revision (%j)", |
| 271 | (changed) => { |
| 272 | const server = fakeGitCafe({ |
| 273 | "GET /repos/owner/repo/pulls/7": pull, |
| 274 | "GET /repos/owner/repo/pulls/7/changes": { ...snapshot, ...changed, items: [], next: null }, |
| 275 | }); |
| 276 | return Effect.gen(function* () { |
| 277 | const provider = yield* GitCafePullRequestProvider.make; |
| 278 | const result = yield* provider.getDiffFilePaths!(target).pipe(Effect.result); |
| 279 | assert.strictEqual(result._tag, "Failure"); |
| 280 | }).pipe(Effect.provide(server.layer)); |
| 281 | }, |
| 282 | ); |
| 283 | }); |
| 284 | |
| 285 | describe("GitCafePullRequestProvider source contents", () => { |
| 286 | const comparisonBase = "a".repeat(40); |
| 287 | const snapshot = { version: pull.version, headOid, comparisonBaseOid: comparisonBase }; |
| 288 | const input = { |
| 289 | ...target, |
| 290 | changeType: "rename-changed" as const, |
| 291 | oldPath: "old name.ts", |
| 292 | newPath: "src/new #name.ts", |
| 293 | }; |
| 294 | const blob = (request: SentRequest) => ({ |
| 295 | commit: request.query.get("oid"), |
| 296 | oid: "b".repeat(40), |
| 297 | path: request.query.get("path"), |
| 298 | size: 16, |
| 299 | binary: false, |
| 300 | content: "const value = 1;", |
| 301 | }); |
| 302 | const routes = { |
| 303 | "GET /repos/owner/repo/pulls/7": pull, |
| 304 | "GET /repos/owner/repo/pulls/7/changes": snapshot, |
| 305 | "GET /repos/owner/repo/blob": blob, |
| 306 | }; |
| 307 | |
| 308 | it.effect( |
| 309 | "pins rename source to the comparison base and fork head, without reading branches", |
| 310 | () => { |
| 311 | const server = fakeGitCafe({ |
| 312 | ...routes, |
| 313 | "GET /repos/owner/repo/pulls/7": { |
| 314 | ...pull, |
| 315 | sourceRepo: { owner: "fork", name: "project" }, |
| 316 | }, |
| 317 | "GET /repos/fork/project/blob": blob, |
| 318 | }); |
| 319 | return Effect.gen(function* () { |
| 320 | const provider = yield* GitCafePullRequestProvider.make; |
| 321 | const result = yield* provider.getDiffFileContents!(input); |
| 322 | assert.deepStrictEqual(result, { |
| 323 | oldContents: "const value = 1;", |
| 324 | newContents: "const value = 1;", |
| 325 | }); |
| 326 | const changes = server.sent.find((request) => request.path.endsWith("/changes"))!; |
| 327 | assert.strictEqual(changes.query.get("expectedVersion"), "4"); |
| 328 | assert.strictEqual(changes.query.get("limit"), "1"); |
| 329 | assert.deepStrictEqual( |
| 330 | server.sent |
| 331 | .filter((request) => request.path.endsWith("/blob")) |
| 332 | .map((request) => ({ |
| 333 | path: request.path, |
| 334 | oid: request.query.get("oid"), |
| 335 | file: request.query.get("path"), |
| 336 | ref: request.query.get("ref"), |
| 337 | })), |
| 338 | [ |
| 339 | { path: "/repos/owner/repo/blob", oid: comparisonBase, file: input.oldPath, ref: null }, |
| 340 | { path: "/repos/fork/project/blob", oid: headOid, file: input.newPath, ref: null }, |
| 341 | ], |
| 342 | ); |
| 343 | assert.deepStrictEqual(server.writes(), []); |
| 344 | }).pipe(Effect.provide(server.layer)); |
| 345 | }, |
| 346 | ); |
| 347 | |
| 348 | it.effect.each(["new", "deleted"] as const)( |
| 349 | "skips the absent side of a %s file", |
| 350 | (changeType) => { |
| 351 | const server = fakeGitCafe(routes); |
| 352 | return Effect.gen(function* () { |
| 353 | const provider = yield* GitCafePullRequestProvider.make; |
| 354 | const result = yield* provider.getDiffFileContents!({ ...input, changeType }); |
| 355 | assert.strictEqual(result[changeType === "new" ? "oldContents" : "newContents"], ""); |
| 356 | assert.lengthOf( |
| 357 | server.sent.filter((request) => request.path.endsWith("/blob")), |
| 358 | 1, |
| 359 | ); |
| 360 | }).pipe(Effect.provide(server.layer)); |
| 361 | }, |
| 362 | ); |
| 363 | |
| 364 | it.effect.each([ |
| 365 | { ...snapshot, version: 5 }, |
| 366 | { ...snapshot, headOid: baseOid }, |
| 367 | { ...snapshot, comparisonBaseOid: "main" }, |
| 368 | ])("refuses a changed or nonimmutable comparison snapshot", (response) => { |
| 369 | const server = fakeGitCafe({ ...routes, "GET /repos/owner/repo/pulls/7/changes": response }); |
| 370 | return Effect.gen(function* () { |
| 371 | const provider = yield* GitCafePullRequestProvider.make; |
| 372 | const result = yield* provider.getDiffFileContents!(input).pipe(Effect.result); |
| 373 | assert.strictEqual(result._tag, "Failure"); |
| 374 | assert.isFalse(server.sent.some((request) => request.path.endsWith("/blob"))); |
| 375 | }).pipe(Effect.provide(server.layer)); |
| 376 | }); |
| 377 | |
| 378 | it.effect.each([ |
| 379 | { binary: true, content: null }, |
| 380 | { content: "binary\0data" }, |
| 381 | { size: 1_048_577 }, |
| 382 | { size: 1, content: "x".repeat(1_048_577) }, |
| 383 | { size: 1, content: "é".repeat(524_289) }, |
| 384 | { commit: baseOid }, |
| 385 | { path: "another.ts" }, |
| 386 | { content: null }, |
| 387 | ])("refuses unavailable, oversized, binary, or mismatched blobs", (fields) => { |
| 388 | const server = fakeGitCafe({ |
| 389 | ...routes, |
| 390 | "GET /repos/owner/repo/blob": (request: SentRequest) => ({ ...blob(request), ...fields }), |
| 391 | }); |
| 392 | return Effect.gen(function* () { |
| 393 | const provider = yield* GitCafePullRequestProvider.make; |
| 394 | const result = yield* provider.getDiffFileContents!(input).pipe(Effect.result); |
| 395 | assert.strictEqual(result._tag, "Failure"); |
| 396 | assert.deepStrictEqual(server.writes(), []); |
| 397 | }).pipe(Effect.provide(server.layer)); |
| 398 | }); |
| 399 | |
| 400 | it.effect.each([ |
| 401 | [413, "oversized"], |
| 402 | [501, "unavailable"], |
| 403 | ] as const)("marks HTTP %s blob refusals permanently unavailable", ([status, reason]) => { |
| 404 | const server = fakeGitCafe({ |
| 405 | ...routes, |
| 406 | "GET /repos/owner/repo/blob": new Response("{}", { status }), |
| 407 | }); |
| 408 | return Effect.gen(function* () { |
| 409 | const provider = yield* GitCafePullRequestProvider.make; |
| 410 | const error = yield* provider.getDiffFileContents!({ ...input, changeType: "new" }).pipe( |
| 411 | Effect.flip, |
| 412 | ); |
| 413 | assert.deepInclude(error.cause as object, { |
| 414 | _tag: "GitCafeDiffSourceUnavailableError", |
| 415 | reason, |
| 416 | }); |
| 417 | }).pipe(Effect.provide(server.layer)); |
| 418 | }); |
| 419 | |
| 420 | it.effect("preserves authentication failures from blob access", () => { |
| 421 | const server = fakeGitCafe({ |
| 422 | ...routes, |
| 423 | "GET /repos/owner/repo/blob": new Response("{}", { status: 401 }), |
| 424 | }); |
| 425 | return Effect.gen(function* () { |
| 426 | const provider = yield* GitCafePullRequestProvider.make; |
| 427 | const error = yield* provider.getDiffFileContents!({ ...input, changeType: "new" }).pipe( |
| 428 | Effect.flip, |
| 429 | ); |
| 430 | assert.strictEqual(error.reason, "unauthenticated"); |
| 431 | assert.strictEqual(error.operation, "getDiffFileContents"); |
| 432 | }).pipe(Effect.provide(server.layer)); |
| 433 | }); |
| 434 | |
| 435 | it.effect( |
| 436 | "refuses commit-specific contents consistently with unsupported GitCafe commit diffs", |
| 437 | () => { |
| 438 | const server = fakeGitCafe({}); |
| 439 | return Effect.gen(function* () { |
| 440 | const provider = yield* GitCafePullRequestProvider.make; |
| 441 | const result = yield* provider.getDiffFileContents!({ ...input, commit: headOid }).pipe( |
| 442 | Effect.result, |
| 443 | ); |
| 444 | assert.strictEqual(result._tag, "Failure"); |
| 445 | assert.deepStrictEqual(server.sent, []); |
| 446 | }).pipe(Effect.provide(server.layer)); |
| 447 | }, |
| 448 | ); |
| 449 | }); |
| 450 | |
| 451 | describe("GitCafePullRequestProvider", () => { |
| 452 | it.effect("keeps a staging repository's requests and links on staging", () => { |
| 453 | const server = fakeGitCafe( |
| 454 | { "GET /repos/owner/repo/pulls/7": pull }, |
| 455 | { env: { CAFE_TOKEN: "env-token", CAFE_HOST: "staging.git.cafe" } }, |
| 456 | ); |
| 457 | return Effect.gen(function* () { |
| 458 | const provider = yield* GitCafePullRequestProvider.make; |
| 459 | const input = { |
| 460 | ...target, |
| 461 | host: "staging.git.cafe", |
| 462 | }; |
| 463 | const preview = yield* provider.getChangeRequestPreview!(input); |
| 464 | assert.deepInclude(preview, { |
| 465 | number: 7, |
| 466 | title: "Change", |
| 467 | url: "https://staging.git.cafe/owner/repo/pulls/7", |
| 468 | author: { login: "alice", name: null, avatarUrl: "https://staging.git.cafe/avatars/one" }, |
| 469 | state: "open", |
| 470 | isDraft: false, |
| 471 | createdAt: timestamp, |
| 472 | }); |
| 473 | const summary = yield* provider.getChangeRequestSummary!(input); |
| 474 | assert.strictEqual(summary.url, "https://staging.git.cafe/owner/repo/pulls/7"); |
| 475 | assert.isTrue(server.sent.every((request) => request.host === "staging.git.cafe")); |
| 476 | assert.deepStrictEqual( |
| 477 | server.sent.map((request) => `${request.method} ${request.path}`), |
| 478 | [ |
| 479 | "GET /repos/owner/repo/pulls/7", |
| 480 | "GET /repos/owner/repo/pulls/7", |
| 481 | "GET /repos/owner/repo/pulls/7/changes", |
| 482 | ], |
| 483 | ); |
| 484 | }).pipe(Effect.provide(server.layer)); |
| 485 | }); |
| 486 | |
| 487 | it.effect("refuses a repository outside GitCafe's hosts before sending anything", () => { |
| 488 | const server = fakeGitCafe({}); |
| 489 | return Effect.gen(function* () { |
| 490 | const provider = yield* GitCafePullRequestProvider.make; |
| 491 | const result = yield* provider |
| 492 | .getChangeRequest({ ...target, host: "github.com" }) |
| 493 | .pipe(Effect.result); |
| 494 | assert.strictEqual(result._tag, "Failure"); |
| 495 | assert.deepStrictEqual(server.sent, []); |
| 496 | }).pipe(Effect.provide(server.layer)); |
| 497 | }); |
| 498 | |
| 499 | it.effect("reports a refused token as unauthenticated and a limit as rate-limited", () => |
| 500 | Effect.gen(function* () { |
| 501 | for (const [status, reason] of [ |
| 502 | [401, "unauthenticated"], |
| 503 | [429, "rate-limited"], |
| 504 | ] as const) { |
| 505 | const server = fakeGitCafe( |
| 506 | { "GET /repos/owner/repo/pulls/7/stack": { stack: null } }, |
| 507 | { |
| 508 | status, |
| 509 | }, |
| 510 | ); |
| 511 | const error = yield* GitCafePullRequestProvider.make.pipe( |
| 512 | Effect.flatMap((provider) => provider.getChangeRequestStack!(target)), |
| 513 | Effect.flip, |
| 514 | Effect.provide(server.layer), |
| 515 | ); |
| 516 | assert.strictEqual(error.reason, reason); |
| 517 | } |
| 518 | }), |
| 519 | ); |
| 520 | |
| 521 | it.effect("never lets fetch follow a redirect with the token", () => { |
| 522 | const server = fakeGitCafe({ "GET /repos/owner/repo/pulls/7/stack": { stack: null } }); |
| 523 | return GitCafePullRequestProvider.make.pipe( |
| 524 | Effect.flatMap((provider) => provider.getChangeRequestStack!(target)), |
| 525 | Effect.tap(() => |
| 526 | Effect.sync(() => |
| 527 | assert.deepStrictEqual( |
| 528 | server.sent.map((r) => r.redirect), |
| 529 | ["manual"], |
| 530 | ), |
| 531 | ), |
| 532 | ), |
| 533 | Effect.provide(server.layer), |
| 534 | ); |
| 535 | }); |
| 536 | |
| 537 | it.effect("follows a renumbered pull request's redirect only within the host's API", () => |
| 538 | Effect.gen(function* () { |
| 539 | const moved = (location: string) => |
| 540 | new Response(null, { status: 301, headers: { location } }); |
| 541 | const renumbered = fakeGitCafe({ |
| 542 | "GET /repos/owner/repo/pulls/7/stack": moved("/api/repos/owner/repo/pulls/8/stack"), |
| 543 | "GET /repos/owner/repo/pulls/8/stack": { stack: null }, |
| 544 | }); |
| 545 | yield* GitCafePullRequestProvider.make.pipe( |
| 546 | Effect.flatMap((provider) => provider.getChangeRequestStack!(target)), |
| 547 | Effect.provide(renumbered.layer), |
| 548 | ); |
| 549 | assert.deepStrictEqual( |
| 550 | renumbered.sent.map((r) => `${r.host}${r.path}`), |
| 551 | ["git.cafe/repos/owner/repo/pulls/7/stack", "git.cafe/repos/owner/repo/pulls/8/stack"], |
| 552 | ); |
| 553 | |
| 554 | for (const location of [ |
| 555 | "https://evil.example/api/repos/owner/repo/pulls/8/stack", |
| 556 | "//evil.example/api/repos/owner/repo/pulls/8/stack", |
| 557 | "https://staging.git.cafe/api/repos/owner/repo/pulls/8/stack", |
| 558 | "/repos/owner/repo/pulls/8/stack", |
| 559 | ]) { |
| 560 | const elsewhere = fakeGitCafe({ "GET /repos/owner/repo/pulls/7/stack": moved(location) }); |
| 561 | const error = yield* GitCafePullRequestProvider.make.pipe( |
| 562 | Effect.flatMap((provider) => provider.getChangeRequestStack!(target)), |
| 563 | Effect.flip, |
| 564 | Effect.provide(elsewhere.layer), |
| 565 | ); |
| 566 | assert.strictEqual(error.detail, "GitCafe answered HTTP 301."); |
| 567 | assert.strictEqual(elsewhere.sent.length, 1); |
| 568 | } |
| 569 | }), |
| 570 | ); |
| 571 | |
| 572 | it.effect("reads the stack a pull request belongs to, with each open layer's head", () => { |
| 573 | const { "GET /repos/owner/repo/pulls/6": _merged, ...routes } = stack([ |
| 574 | { number: 6, headOid: baseOid, state: "merged" }, |
| 575 | { number: 7, headOid }, |
| 576 | ]); |
| 577 | const server = fakeGitCafe(routes); |
| 578 | return Effect.gen(function* () { |
| 579 | const provider = yield* GitCafePullRequestProvider.make; |
| 580 | const result = yield* provider.getChangeRequestStack!(target); |
| 581 | assert.strictEqual(result?.number, 3); |
| 582 | assert.deepStrictEqual( |
| 583 | result?.layers.map((layer) => [layer.number, layer.headSha]), |
| 584 | [ |
| 585 | [6, undefined], |
| 586 | [7, headOid], |
| 587 | ], |
| 588 | ); |
| 589 | }).pipe(Effect.provide(server.layer)); |
| 590 | }); |
| 591 | |
| 592 | it.effect("resolves a thread through its root at the root's version", () => { |
| 593 | const server = fakeGitCafe({ |
| 594 | "GET /repos/owner/repo/pulls/7/comments": { items: [root], next: null }, |
| 595 | "POST /repos/owner/repo/pulls/7/comments/root/resolve": { |
| 596 | id: "root", |
| 597 | threadId: "root", |
| 598 | version: 3, |
| 599 | }, |
| 600 | }); |
| 601 | return Effect.gen(function* () { |
| 602 | const provider = yield* GitCafePullRequestProvider.make; |
| 603 | yield* provider.setThreadResolution({ ...target, threadId: "root", resolved: true }); |
| 604 | assert.deepStrictEqual(server.writes(), [ |
| 605 | "POST /repos/owner/repo/pulls/7/comments/root/resolve", |
| 606 | ]); |
| 607 | assert.deepStrictEqual(server.sent.at(-1)?.body, { expectedVersion: 2 }); |
| 608 | }).pipe(Effect.provide(server.layer)); |
| 609 | }); |
| 610 | |
| 611 | it.effect("keeps the timeline when GitCafe no longer has the head's commits", () => { |
| 612 | const comment = { ...root, id: "c1", threadId: "c1", body: "Looks good", path: null }; |
| 613 | const server = fakeGitCafe({ |
| 614 | "GET /repos/owner/repo/pulls/7": { ...pull, state: "closed", closedAt: timestamp }, |
| 615 | "GET /repos/owner/repo/pulls/7/comments": { items: [comment], next: null }, |
| 616 | "GET /repos/owner/repo/pulls/7/reviews": { items: [], next: null }, |
| 617 | }); |
| 618 | return Effect.gen(function* () { |
| 619 | const provider = yield* GitCafePullRequestProvider.make; |
| 620 | const activity = yield* provider.getChangeRequestActivity(target); |
| 621 | assert.deepStrictEqual( |
| 622 | activity.comments.map((entry) => entry.body), |
| 623 | ["Looks good"], |
| 624 | ); |
| 625 | }).pipe(Effect.provide(server.layer)); |
| 626 | }); |
| 627 | |
| 628 | it.effect("submits a review against the pull request's current head", () => { |
| 629 | const server = fakeGitCafe({ |
| 630 | "GET /repos/owner/repo/pulls/7": pull, |
| 631 | "POST /repos/owner/repo/pulls/7/reviews": { id: "review-1" }, |
| 632 | }); |
| 633 | return Effect.gen(function* () { |
| 634 | const provider = yield* GitCafePullRequestProvider.make; |
| 635 | yield* provider.submitReview({ |
| 636 | ...target, |
| 637 | verdict: "request-changes", |
| 638 | body: "Please fix", |
| 639 | comments: [], |
| 640 | }); |
| 641 | assert.deepStrictEqual(server.writes(), ["POST /repos/owner/repo/pulls/7/reviews"]); |
| 642 | assert.deepInclude(server.sent.at(-1)?.body as object, { |
| 643 | verdict: "request_changes", |
| 644 | body: "Please fix", |
| 645 | commitOid: headOid, |
| 646 | }); |
| 647 | assertRequestId(server.sent.at(-1)?.body); |
| 648 | |
| 649 | // GitCafe rejects a null body; an empty review leaves it out. |
| 650 | yield* provider.submitReview({ ...target, verdict: "approve", body: "", comments: [] }); |
| 651 | assert.notProperty(server.sent.at(-1)?.body as object, "body"); |
| 652 | assertRequestId(server.sent.at(-1)?.body); |
| 653 | }).pipe(Effect.provide(server.layer)); |
| 654 | }); |
| 655 | |
| 656 | it.effect.each([ |
| 657 | ["completed", true], |
| 658 | ["failed", false], |
| 659 | ] as const)("merges synchronously and reads a %s outcome", ([state, succeeds]) => { |
| 660 | const server = fakeGitCafe({ |
| 661 | "GET /repos/owner/repo/pulls/7": pull, |
| 662 | "POST /repos/owner/repo/pulls/7/merge": { id: "merge-1", state, reason: "Blocked" }, |
| 663 | }); |
| 664 | return Effect.gen(function* () { |
| 665 | const provider = yield* GitCafePullRequestProvider.make; |
| 666 | const result = yield* provider |
| 667 | .runAction({ ...target, action: "merge", mergeMethod: "squash" }) |
| 668 | .pipe(Effect.result); |
| 669 | assert.strictEqual(result._tag, succeeds ? "Success" : "Failure"); |
| 670 | assert.deepStrictEqual(server.writes(), ["POST /repos/owner/repo/pulls/7/merge"]); |
| 671 | assert.deepInclude(server.sent.at(-1)?.body as object, { |
| 672 | strategy: "squash", |
| 673 | headOid, |
| 674 | expectedVersion: 4, |
| 675 | }); |
| 676 | assertRequestId(server.sent.at(-1)?.body); |
| 677 | }).pipe(Effect.provide(server.layer)); |
| 678 | }); |
| 679 | |
| 680 | it.effect("reads a GitHub-mirrored check's log, and re-runs only GitCafe Actions runs", () => { |
| 681 | const server = fakeGitCafe({ |
| 682 | "GET /repos/owner/repo/ci/github/checks/555/log": { |
| 683 | content: "install\ntest\nFAIL a.test.ts\n", |
| 684 | truncated: true, |
| 685 | }, |
| 686 | "POST /repos/owner/repo/actions/runs/actwfrun_abc/rerun": {}, |
| 687 | "GET /repos/owner/repo/ci/github/checks/401/log": new Response("{}", { status: 401 }), |
| 688 | }); |
| 689 | const mirrored = { name: "Test", url: "https://github.com/owner/repo/runs/555" }; |
| 690 | const missing = { name: "Gone", url: "https://github.com/owner/repo/runs/404" }; |
| 691 | const signedOut = { name: "Lint", url: "https://github.com/owner/repo/runs/401" }; |
| 692 | const native = { name: "Build", url: "https://git.cafe/owner/repo/actions/runs/actwfrun_abc" }; |
| 693 | return Effect.gen(function* () { |
| 694 | const provider = yield* GitCafePullRequestProvider.make; |
| 695 | const logs = yield* provider.getCheckLogs!({ |
| 696 | ...target, |
| 697 | checks: [mirrored, missing], |
| 698 | tailLines: 1, |
| 699 | }); |
| 700 | assert.deepInclude(logs[0], { log: "FAIL a.test.ts", truncated: true, detail: null }); |
| 701 | assert.isNotNull(logs[1]?.detail); |
| 702 | const refused = yield* Effect.flip( |
| 703 | provider.getCheckLogs!({ ...target, checks: [signedOut], tailLines: 1 }), |
| 704 | ); |
| 705 | assert.strictEqual(refused.reason, "unauthenticated"); |
| 706 | const reruns = yield* provider.rerunChecks!({ ...target, checks: [mirrored, native] }); |
| 707 | assert.deepStrictEqual( |
| 708 | reruns.map(({ name, rerun }) => [name, rerun]), |
| 709 | [ |
| 710 | ["Test", false], |
| 711 | ["Build", true], |
| 712 | ], |
| 713 | ); |
| 714 | assert.deepStrictEqual(server.writes(), [ |
| 715 | "POST /repos/owner/repo/actions/runs/actwfrun_abc/rerun", |
| 716 | ]); |
| 717 | }).pipe(Effect.provide(server.layer)); |
| 718 | }); |
| 719 | |
| 720 | it.effect( |
| 721 | "lands a stack through a layer only while its heads are the ones the reader saw", |
| 722 | () => { |
| 723 | const layers = [ |
| 724 | { number: 6, headOid: baseOid }, |
| 725 | { number: 7, headOid }, |
| 726 | ]; |
| 727 | const server = fakeGitCafe({ |
| 728 | ...stack(layers), |
| 729 | "POST /repos/owner/repo/pulls/stacks/3/land-through": { |
| 730 | id: "land-1", |
| 731 | state: "completed", |
| 732 | landedCount: 2, |
| 733 | stepCount: 2, |
| 734 | stopReason: null, |
| 735 | error: null, |
| 736 | steps: [], |
| 737 | }, |
| 738 | }); |
| 739 | return Effect.gen(function* () { |
| 740 | const provider = yield* GitCafePullRequestProvider.make; |
| 741 | const heads = layers.map((layer) => ({ number: layer.number, headSha: layer.headOid })); |
| 742 | const stale = yield* provider |
| 743 | .runAction({ |
| 744 | ...target, |
| 745 | action: "merge", |
| 746 | stackNumber: 3, |
| 747 | expectedStackHeads: [heads[0]!, { number: 7, headSha: baseOid }], |
| 748 | }) |
| 749 | .pipe(Effect.result); |
| 750 | assert.strictEqual(stale._tag, "Failure"); |
| 751 | assert.deepStrictEqual(server.writes(), []); |
| 752 | |
| 753 | yield* provider.runAction({ |
| 754 | ...target, |
| 755 | action: "merge", |
| 756 | stackNumber: 3, |
| 757 | expectedStackHeads: heads, |
| 758 | mergeMethod: "merge", |
| 759 | }); |
| 760 | assert.deepStrictEqual(server.writes(), [ |
| 761 | "POST /repos/owner/repo/pulls/stacks/3/land-through", |
| 762 | ]); |
| 763 | assert.deepInclude(server.sent.at(-1)?.body as object, { |
| 764 | expectedRevision: 5, |
| 765 | throughPullRequestNumber: 7, |
| 766 | strategy: "merge", |
| 767 | }); |
| 768 | assertRequestId(server.sent.at(-1)?.body); |
| 769 | }).pipe(Effect.provide(server.layer)); |
| 770 | }, |
| 771 | ); |
| 772 | |
| 773 | it.effect.each([ |
| 774 | ["authored", "authors"], |
| 775 | ["reviewing", "reviewers"], |
| 776 | ] as const)("filters %s pull requests by the viewer's actor on GitCafe", ([involvement, key]) => { |
| 777 | const server = fakeGitCafe({ |
| 778 | "GET /auth/principal": { actorId: "act_one", handle: "Alice" }, |
| 779 | "GET /repos/owner/repo/pulls": { items: [pull], next: null }, |
| 780 | }); |
| 781 | return Effect.gen(function* () { |
| 782 | const provider = yield* GitCafePullRequestProvider.make; |
| 783 | const page = yield* provider.listChangeRequests({ |
| 784 | ...target, |
| 785 | state: "open", |
| 786 | involvement, |
| 787 | viewer: "alice", |
| 788 | limit: 10, |
| 789 | }); |
| 790 | assert.strictEqual(server.sent.at(-1)?.query.get(key), '["act_one"]'); |
| 791 | assert.strictEqual( |
| 792 | page.items[0]?.reviewRequestLogins.includes("alice"), |
| 793 | involvement === "reviewing", |
| 794 | ); |
| 795 | }).pipe(Effect.provide(server.layer)); |
| 796 | }); |
| 797 | |
| 798 | it.effect("narrows a listing on GitCafe by label ids, draft state and the author's actor", () => { |
| 799 | const server = fakeGitCafe({ |
| 800 | "GET /repos/owner/repo/labels": { items: labels, next: null }, |
| 801 | "GET /users/bob": { actorId: "act_bob", handle: "bob" }, |
| 802 | "GET /repos/owner/repo/pulls": { items: [pull], next: null }, |
| 803 | }); |
| 804 | return Effect.gen(function* () { |
| 805 | const provider = yield* GitCafePullRequestProvider.make; |
| 806 | yield* provider.listChangeRequests({ |
| 807 | ...target, |
| 808 | state: "open", |
| 809 | involvement: "all", |
| 810 | viewer: "alice", |
| 811 | limit: 10, |
| 812 | filters: { |
| 813 | draft: "hide", |
| 814 | labels: [["missing"], ["Bug", "docs"]], |
| 815 | excludedLabels: ["docs", "missing"], |
| 816 | author: "bob", |
| 817 | }, |
| 818 | }); |
| 819 | const listing = server.sent.at(-1)!; |
| 820 | assert.strictEqual(listing.path, "/repos/owner/repo/pulls"); |
| 821 | assert.strictEqual(listing.query.get("labels"), '["label_bug","label_docs"]'); |
| 822 | assert.strictEqual(listing.query.get("labelNots"), '["label_docs"]'); |
| 823 | assert.strictEqual(listing.query.get("stateNots"), '["draft"]'); |
| 824 | assert.strictEqual(listing.query.get("authors"), '["act_bob"]'); |
| 825 | }).pipe(Effect.provide(server.layer)); |
| 826 | }); |
| 827 | |
| 828 | it.effect("finds a GitHub-synced author's actor on their pull requests to narrow by it", () => { |
| 829 | const synced = { |
| 830 | ...pull, |
| 831 | number: 8, |
| 832 | author: { kind: "github", actorId: "act_synced", login: "Synced", avatarUrl: null }, |
| 833 | }; |
| 834 | const server = fakeGitCafe({ |
| 835 | "GET /repos/owner/repo/pulls": (request: SentRequest) => |
| 836 | request.query.get("authors") === '["act_synced"]' |
| 837 | ? { items: [synced], next: null } |
| 838 | : request.query.get("after") === null |
| 839 | ? { items: [pull], next: "o1" } |
| 840 | : { items: [synced], next: null }, |
| 841 | }); |
| 842 | return Effect.gen(function* () { |
| 843 | const provider = yield* GitCafePullRequestProvider.make; |
| 844 | const list = (author: string) => |
| 845 | provider.listChangeRequests({ |
| 846 | ...target, |
| 847 | state: "open", |
| 848 | involvement: "all", |
| 849 | viewer: "alice", |
| 850 | limit: 10, |
| 851 | filters: { author }, |
| 852 | }); |
| 853 | const page = yield* list("synced"); |
| 854 | assert.deepStrictEqual( |
| 855 | page.items.map((item) => item.number), |
| 856 | [8], |
| 857 | ); |
| 858 | assert.strictEqual(server.sent.at(-1)!.query.get("authors"), '["act_synced"]'); |
| 859 | // Once seen, the actor is narrowed by straight away. |
| 860 | const sentBefore = server.sent.length; |
| 861 | yield* list("synced"); |
| 862 | assert.deepStrictEqual( |
| 863 | server.sent.slice(sentBefore).map((request) => request.query.get("authors")), |
| 864 | [null, '["act_synced"]'], |
| 865 | ); |
| 866 | // An author on none of the pages it reads gets nothing rather than unnarrowed rows a |
| 867 | // continuation would count against a different listing. |
| 868 | const missing = yield* list("nobody"); |
| 869 | assert.deepStrictEqual(missing, { items: [], truncated: false, continues: false }); |
| 870 | }).pipe(Effect.provide(server.layer)); |
| 871 | }); |
| 872 | |
| 873 | it.effect("carries a listing on after the last row delivered when rows above it close", () => { |
| 874 | const created = (number: number) => `2026-09-12T12:00:0${9 - number}Z`; |
| 875 | let pulls = [1, 2, 3, 4, 5].map((number) => ({ ...pull, number, createdAt: created(number) })); |
| 876 | const server = fakeGitCafe({ |
| 877 | "GET /repos/owner/repo/pulls": (request: SentRequest) => { |
| 878 | const offset = Number(request.query.get("after")?.slice(1) ?? 0); |
| 879 | const end = offset + Number(request.query.get("limit")); |
| 880 | return { items: pulls.slice(offset, end), next: end < pulls.length ? `o${end}` : null }; |
| 881 | }, |
| 882 | }); |
| 883 | return Effect.gen(function* () { |
| 884 | const provider = yield* GitCafePullRequestProvider.make; |
| 885 | const list = (cursor?: { readonly resumeAfter?: string }) => |
| 886 | provider.listChangeRequests({ |
| 887 | ...target, |
| 888 | state: "open", |
| 889 | involvement: "all", |
| 890 | viewer: "alice", |
| 891 | limit: 2, |
| 892 | ...(cursor === undefined |
| 893 | ? {} |
| 894 | : { cursor: { updatedBefore: timestamp, delivered: 2, ...cursor } }), |
| 895 | }); |
| 896 | const first = yield* list(); |
| 897 | assert.deepStrictEqual( |
| 898 | first.items.map((item) => item.number), |
| 899 | [1, 2], |
| 900 | ); |
| 901 | pulls = pulls.filter((candidate) => candidate.number !== 1); |
| 902 | const next = yield* list({ resumeAfter: first.resumeAfter! }); |
| 903 | assert.deepStrictEqual( |
| 904 | next.items.map((item) => item.number), |
| 905 | [3, 4], |
| 906 | ); |
| 907 | assert.isTrue(next.truncated); |
| 908 | assert.isTrue(next.continues); |
| 909 | }).pipe(Effect.provide(server.layer)); |
| 910 | }); |
| 911 | |
| 912 | it.effect("lists nothing for a viewer the token does not belong to", () => { |
| 913 | const server = fakeGitCafe({ |
| 914 | "GET /auth/principal": { actorId: "act_two", handle: "bob" }, |
| 915 | }); |
| 916 | return Effect.gen(function* () { |
| 917 | const provider = yield* GitCafePullRequestProvider.make; |
| 918 | const page = yield* provider.listChangeRequests({ |
| 919 | ...target, |
| 920 | state: "open", |
| 921 | involvement: "authored", |
| 922 | viewer: "alice", |
| 923 | limit: 10, |
| 924 | }); |
| 925 | assert.deepStrictEqual(page.items, []); |
| 926 | assert.deepStrictEqual( |
| 927 | server.sent.map((request) => request.path), |
| 928 | ["/auth/principal"], |
| 929 | ); |
| 930 | }).pipe(Effect.provide(server.layer)); |
| 931 | }); |
| 932 | |
| 933 | it.effect.each(["authored", "reviewing"] as const)( |
| 934 | "lists %s unnarrowed when GitCafe can't say who the viewer is", |
| 935 | (involvement) => { |
| 936 | const server = fakeGitCafe({ "GET /repos/owner/repo/pulls": { items: [pull], next: null } }); |
| 937 | return Effect.gen(function* () { |
| 938 | const provider = yield* GitCafePullRequestProvider.make; |
| 939 | const page = yield* provider.listChangeRequests({ |
| 940 | ...target, |
| 941 | state: "open", |
| 942 | involvement, |
| 943 | viewer: "alice", |
| 944 | limit: 10, |
| 945 | }); |
| 946 | const listing = server.sent.find((request) => request.path === "/repos/owner/repo/pulls"); |
| 947 | assert.isDefined(listing); |
| 948 | assert.isNull(listing!.query.get(involvement === "reviewing" ? "reviewers" : "authors")); |
| 949 | assert.notInclude(page.items[0]?.reviewRequestLogins ?? [], "alice"); |
| 950 | }).pipe(Effect.provide(server.layer)); |
| 951 | }, |
| 952 | ); |
| 953 | |
| 954 | it.effect("reads line counts, open merge strategies, reviewers and labels", () => { |
| 955 | const snapshot = { version: 4, headOid, comparisonBaseOid: baseOid }; |
| 956 | const files = (from: number, count: number) => |
| 957 | Array.from({ length: count }, (_, index) => ({ path: `f${from + index}.ts` })); |
| 958 | let changePage = 0; |
| 959 | const server = fakeGitCafe({ |
| 960 | "GET /repos/owner/repo/pulls/7": pull, |
| 961 | "GET /repos/owner/repo/pulls/7/reviewers": { items: [{ id: "act_one", actor }], next: null }, |
| 962 | "GET /repos/owner/repo/pulls/7/labels": { items: [labels[0]], next: null }, |
| 963 | "GET /repos/owner/repo/pulls/7/status": { |
| 964 | merge: { |
| 965 | conflicts: "none", |
| 966 | fastForward: true, |
| 967 | strategies: ["merge", "squash", "rebase"], |
| 968 | blockers: [{ blockedStrategies: ["squash"] }, {}], |
| 969 | }, |
| 970 | checks: { pending: 0, failing: 0, total: 0 }, |
| 971 | }, |
| 972 | [`GET /repos/owner/repo/commits/${headOid}/checks`]: (request: SentRequest) => ({ |
| 973 | items: [ |
| 974 | { |
| 975 | name: request.query.has("after") ? "late" : "early", |
| 976 | status: "completed", |
| 977 | conclusion: request.query.has("after") ? "failure" : "success", |
| 978 | summary: null, |
| 979 | detailsUrl: null, |
| 980 | }, |
| 981 | ], |
| 982 | next: request.query.has("after") ? null : "checks-1", |
| 983 | }), |
| 984 | "GET /repos/owner/repo/pulls/7/changes": () => |
| 985 | changePage++ === 0 |
| 986 | ? { ...snapshot, items: files(0, 2), next: "cursor-1" } |
| 987 | : { ...snapshot, items: files(2, 1), next: null }, |
| 988 | "POST /repos/owner/repo/pulls/7/diff-files": (request: SentRequest) => ({ |
| 989 | ...snapshot, |
| 990 | items: (request.body as { paths: ReadonlyArray<string> }).paths.map((path) => ({ |
| 991 | path, |
| 992 | additions: 2, |
| 993 | deletions: 1, |
| 994 | })), |
| 995 | }), |
| 996 | }); |
| 997 | return Effect.gen(function* () { |
| 998 | const provider = yield* GitCafePullRequestProvider.make; |
| 999 | const detail = yield* provider.getChangeRequest(target); |
| 1000 | assert.deepInclude(detail, { |
| 1001 | additions: 6, |
| 1002 | deletions: 3, |
| 1003 | changedFiles: 3, |
| 1004 | mergeability: "mergeable", |
| 1005 | }); |
| 1006 | assert.strictEqual( |
| 1007 | server.sent.filter((request) => request.path.endsWith("/changes"))[1]?.query.get("after"), |
| 1008 | "cursor-1", |
| 1009 | ); |
| 1010 | assert.deepStrictEqual(detail.mergeCapabilities, { |
| 1011 | merge: true, |
| 1012 | squash: false, |
| 1013 | rebase: true, |
| 1014 | }); |
| 1015 | assert.deepStrictEqual( |
| 1016 | detail.checks.map((check) => check.name), |
| 1017 | ["late", "early"], |
| 1018 | ); |
| 1019 | assert.deepStrictEqual( |
| 1020 | detail.reviewers.map((reviewer) => reviewer.login), |
| 1021 | ["alice"], |
| 1022 | ); |
| 1023 | assert.deepStrictEqual(detail.reviewRequestLogins, ["alice"]); |
| 1024 | assert.deepStrictEqual(detail.labels, [{ name: "bug", color: "#d73a4a" }]); |
| 1025 | }).pipe(Effect.provide(server.layer)); |
| 1026 | }); |
| 1027 | |
| 1028 | it.effect("reads the checks state from each check's newest run", () => { |
| 1029 | const run = (id: string, conclusion: string) => ({ |
| 1030 | id, |
| 1031 | name: "Check", |
| 1032 | status: "completed", |
| 1033 | conclusion, |
| 1034 | summary: null, |
| 1035 | detailsUrl: null, |
| 1036 | }); |
| 1037 | const items = [run("120859457782", "success"), run("120857059247", "failure")]; |
| 1038 | const detailWith = (checks: (request: SentRequest) => unknown) => { |
| 1039 | const server = fakeGitCafe({ |
| 1040 | "GET /repos/owner/repo/pulls/7": pull, |
| 1041 | "GET /repos/owner/repo/pulls/7/reviewers": { items: [], next: null }, |
| 1042 | "GET /repos/owner/repo/pulls/7/labels": { items: [], next: null }, |
| 1043 | "GET /repos/owner/repo/pulls/7/status": { |
| 1044 | merge: { conflicts: "none", fastForward: null, strategies: [] }, |
| 1045 | // GitCafe counts the cancelled run's failure beside the re-run that passed. |
| 1046 | checks: { pending: 0, failing: 1, total: 2 }, |
| 1047 | }, |
| 1048 | [`GET /repos/owner/repo/commits/${headOid}/checks`]: checks, |
| 1049 | "GET /repos/owner/repo/pulls/7/changes": { |
| 1050 | version: 4, |
| 1051 | headOid, |
| 1052 | comparisonBaseOid: baseOid, |
| 1053 | items: [], |
| 1054 | next: null, |
| 1055 | }, |
| 1056 | }); |
| 1057 | return Effect.gen(function* () { |
| 1058 | const provider = yield* GitCafePullRequestProvider.make; |
| 1059 | return yield* provider.getChangeRequest(target); |
| 1060 | }).pipe(Effect.provide(server.layer)); |
| 1061 | }; |
| 1062 | return Effect.gen(function* () { |
| 1063 | const detail = yield* detailWith(() => ({ items, next: null })); |
| 1064 | assert.deepStrictEqual( |
| 1065 | detail.checks.map((check) => [check.name, check.status]), |
| 1066 | [["Check", "success"]], |
| 1067 | ); |
| 1068 | assert.strictEqual(detail.checksState, "passing"); |
| 1069 | |
| 1070 | // A listing cut off at the page limit may hide a failure, so GitCafe's counts decide. |
| 1071 | const truncated = yield* detailWith((request) => ({ |
| 1072 | items, |
| 1073 | next: String(Number(request.query.get("after") ?? 0) + 1), |
| 1074 | })); |
| 1075 | assert.strictEqual(truncated.checksState, "failing"); |
| 1076 | }); |
| 1077 | }); |
| 1078 | |
| 1079 | it.effect("still reads a pull request whose reviewers the token may not see", () => { |
| 1080 | const server = fakeGitCafe({ |
| 1081 | "GET /repos/owner/repo/pulls/7": { ...pull, headOid: null }, |
| 1082 | "GET /repos/owner/repo/pulls/7/reviewers": new Response( |
| 1083 | encodeJson({ type: "https://cafe.sh/errors/forbidden" }), |
| 1084 | { status: 403 }, |
| 1085 | ), |
| 1086 | "GET /repos/owner/repo/pulls/7/labels": { items: [labels[0]], next: null }, |
| 1087 | "GET /repos/owner/repo/pulls/7/status": { |
| 1088 | merge: { conflicts: "unknown", fastForward: null, strategies: [] }, |
| 1089 | checks: { pending: 0, failing: 0, total: 0 }, |
| 1090 | }, |
| 1091 | }); |
| 1092 | return Effect.gen(function* () { |
| 1093 | const provider = yield* GitCafePullRequestProvider.make; |
| 1094 | const detail = yield* provider.getChangeRequest(target); |
| 1095 | assert.deepStrictEqual(detail.reviewers, []); |
| 1096 | assert.deepStrictEqual(detail.labels, [{ name: "bug", color: "#d73a4a" }]); |
| 1097 | }).pipe(Effect.provide(server.layer)); |
| 1098 | }); |
| 1099 | |
| 1100 | it.effect("fails the detail rather than hide reviewers GitCafe could not serve", () => { |
| 1101 | const server = fakeGitCafe({ |
| 1102 | "GET /repos/owner/repo/pulls/7": { ...pull, headOid: null }, |
| 1103 | "GET /repos/owner/repo/pulls/7/reviewers": new Response("", { status: 503 }), |
| 1104 | "GET /repos/owner/repo/pulls/7/labels": { items: [], next: null }, |
| 1105 | "GET /repos/owner/repo/pulls/7/status": { |
| 1106 | merge: { conflicts: "unknown", fastForward: null, strategies: [] }, |
| 1107 | checks: { pending: 0, failing: 0, total: 0 }, |
| 1108 | }, |
| 1109 | }); |
| 1110 | return Effect.gen(function* () { |
| 1111 | const provider = yield* GitCafePullRequestProvider.make; |
| 1112 | const result = yield* provider.getChangeRequest(target).pipe(Effect.result); |
| 1113 | assert.strictEqual(result._tag, "Failure"); |
| 1114 | }).pipe(Effect.provide(server.layer)); |
| 1115 | }); |
| 1116 | |
| 1117 | const detailWith = (overrides: Record<string, unknown>, reasons: ReadonlyArray<string> = []) => |
| 1118 | fakeGitCafe({ |
| 1119 | "GET /repos/owner/repo/pulls/7": { ...pull, headOid: null, ...overrides }, |
| 1120 | "GET /repos/owner/repo/pulls/7/reviewers": { items: [], next: null }, |
| 1121 | "GET /repos/owner/repo/pulls/7/labels": { items: [], next: null }, |
| 1122 | "GET /repos/owner/repo/pulls/7/status": { |
| 1123 | merge: { conflicts: "none", fastForward: null, strategies: ["squash"], reasons }, |
| 1124 | checks: { pending: 0, failing: 0, total: 0 }, |
| 1125 | }, |
| 1126 | }); |
| 1127 | |
| 1128 | it.effect("shows a merge GitCafe accepted as armed until it lands", () => { |
| 1129 | const server = detailWith({}, ["provider-merge-pending"]); |
| 1130 | return Effect.gen(function* () { |
| 1131 | const provider = yield* GitCafePullRequestProvider.make; |
| 1132 | const detail = yield* provider.getChangeRequest(target); |
| 1133 | assert.strictEqual(detail.autoMergeEnabled, true); |
| 1134 | assert.notInclude(detail.viewerPermissions?.actions ?? [], "merge"); |
| 1135 | }).pipe(Effect.provide(server.layer)); |
| 1136 | }); |
| 1137 | |
| 1138 | it.effect("reads a pull request GitHub merged as merged before the sync reports it", () => { |
| 1139 | const mergedAt = "2026-09-12T13:00:00Z"; |
| 1140 | const mergeCommitOid = "3b2f958603a51d8c42c4ede02eee7b8cb310b2d9"; |
| 1141 | const server = detailWith({ providerMerge: { mergedAt, mergeCommitOid } }); |
| 1142 | return Effect.gen(function* () { |
| 1143 | const provider = yield* GitCafePullRequestProvider.make; |
| 1144 | const detail = yield* provider.getChangeRequest(target); |
| 1145 | assert.strictEqual(detail.state, "merged"); |
| 1146 | assert.strictEqual(detail.mergedAt, mergedAt); |
| 1147 | const preview = yield* provider.getChangeRequestPreview!(target); |
| 1148 | assert.strictEqual(preview.state, "merged"); |
| 1149 | assert.strictEqual(detail.mergeCommitSha, mergeCommitOid); |
| 1150 | }).pipe(Effect.provide(server.layer)); |
| 1151 | }); |
| 1152 | |
| 1153 | it.effect.each([ |
| 1154 | ["their own pull request", actor], |
| 1155 | [ |
| 1156 | "the pull request GitHub sync opened for them", |
| 1157 | { |
| 1158 | kind: "github", |
| 1159 | actorId: "act_github", |
| 1160 | login: "alice", |
| 1161 | avatarUrl: null, |
| 1162 | linkedProfile: { handle: "alice", actorId: "act_one" }, |
| 1163 | }, |
| 1164 | ], |
| 1165 | ] as const)("leaves the author only a comment on %s", ([, author]) => { |
| 1166 | const server = detailWith({ author, viewerActorId: "act_one" }); |
| 1167 | return Effect.gen(function* () { |
| 1168 | const provider = yield* GitCafePullRequestProvider.make; |
| 1169 | const detail = yield* provider.getChangeRequest(target); |
| 1170 | assert.deepStrictEqual(detail.viewerPermissions?.verdicts, ["comment"]); |
| 1171 | }).pipe(Effect.provide(server.layer)); |
| 1172 | }); |
| 1173 | |
| 1174 | it.effect("requests and withdraws reviewers by rewriting the whole list", () => { |
| 1175 | const bob = { ...actor, actorId: "act_two", handle: "bob" }; |
| 1176 | let requested = [{ id: "act_two", actor: bob }]; |
| 1177 | const server = fakeGitCafe({ |
| 1178 | "GET /repos/owner/repo/pulls/7": pull, |
| 1179 | "GET /repos/owner/repo/pulls/7/reviewer-candidates": { |
| 1180 | items: [ |
| 1181 | { id: "act_one", actor }, |
| 1182 | { id: "act_two", actor: bob }, |
| 1183 | { id: "act_bot", actor: { ...actor, actorId: "act_bot", handle: "lint", bot: true } }, |
| 1184 | ], |
| 1185 | next: null, |
| 1186 | }, |
| 1187 | "GET /repos/owner/repo/pulls/7/reviewers": () => ({ items: requested, next: null }), |
| 1188 | "PUT /repos/owner/repo/pulls/7/reviewers": { version: 5, items: [] }, |
| 1189 | }); |
| 1190 | return Effect.gen(function* () { |
| 1191 | const provider = yield* GitCafePullRequestProvider.make; |
| 1192 | const list = yield* provider.listReviewerCandidates(target); |
| 1193 | // The author, alice, cannot review her own pull request. |
| 1194 | assert.deepStrictEqual( |
| 1195 | list.candidates.map((c) => [c.id, c.login, c.isRequested, c.isBot ?? false]), |
| 1196 | [ |
| 1197 | ["act_two", "bob", true, false], |
| 1198 | ["act_bot", "lint", false, true], |
| 1199 | ], |
| 1200 | ); |
| 1201 | |
| 1202 | const set = (id: string, on: boolean) => |
| 1203 | provider.setReviewerRequest({ |
| 1204 | ...target, |
| 1205 | reviewers: [{ id, kind: "user" }], |
| 1206 | requested: on, |
| 1207 | }); |
| 1208 | yield* set("act_bot", true); |
| 1209 | assert.deepStrictEqual(server.sent.at(-1)?.body, { |
| 1210 | expectedVersion: 4, |
| 1211 | actorIds: ["act_two", "act_bot"], |
| 1212 | }); |
| 1213 | yield* set("act_two", false); |
| 1214 | assert.deepStrictEqual(server.sent.at(-1)?.body, { expectedVersion: 4, actorIds: [] }); |
| 1215 | |
| 1216 | requested = []; |
| 1217 | yield* set("act_two", false); |
| 1218 | assert.strictEqual(server.writes().length, 2); |
| 1219 | }).pipe(Effect.provide(server.layer)); |
| 1220 | }); |
| 1221 | |
| 1222 | it.effect("names the missing scopes when the token may not request reviewers", () => { |
| 1223 | const server = fakeGitCafe({ |
| 1224 | "GET /repos/owner/repo/pulls/7": pull, |
| 1225 | "GET /repos/owner/repo/pulls/7/reviewer-candidates": new Response("", { status: 403 }), |
| 1226 | "GET /repos/owner/repo/pulls/7/reviewers": { items: [], next: null }, |
| 1227 | }); |
| 1228 | return Effect.gen(function* () { |
| 1229 | const provider = yield* GitCafePullRequestProvider.make; |
| 1230 | const error = yield* Effect.flip(provider.listReviewerCandidates(target)); |
| 1231 | assert.match(error.detail, /pr:reviewers:write/u); |
| 1232 | }).pipe(Effect.provide(server.layer)); |
| 1233 | }); |
| 1234 | |
| 1235 | it.effect("applies and removes labels by name through their ids", () => { |
| 1236 | const server = fakeGitCafe({ |
| 1237 | "GET /repos/owner/repo/pulls/7": pull, |
| 1238 | "GET /repos/owner/repo/labels": { items: labels, next: null }, |
| 1239 | "GET /repos/owner/repo/pulls/7/labels": { items: [labels[0]], next: null }, |
| 1240 | "PUT /repos/owner/repo/pulls/7/labels": { version: 5, items: [] }, |
| 1241 | }); |
| 1242 | return Effect.gen(function* () { |
| 1243 | const provider = yield* GitCafePullRequestProvider.make; |
| 1244 | const list = yield* provider.listLabelCandidates!(target); |
| 1245 | assert.deepStrictEqual( |
| 1246 | list.candidates.map((label) => [label.name, label.isApplied]), |
| 1247 | [ |
| 1248 | ["bug", true], |
| 1249 | ["docs", false], |
| 1250 | ], |
| 1251 | ); |
| 1252 | |
| 1253 | yield* provider.setLabels!({ ...target, labels: ["docs"], applied: true }); |
| 1254 | assert.deepStrictEqual(server.sent.at(-1)?.body, { |
| 1255 | expectedVersion: 4, |
| 1256 | labelIds: ["label_bug", "label_docs"], |
| 1257 | }); |
| 1258 | yield* provider.setLabels!({ ...target, labels: ["bug"], applied: false }); |
| 1259 | assert.deepStrictEqual(server.sent.at(-1)?.body, { expectedVersion: 4, labelIds: [] }); |
| 1260 | |
| 1261 | const missing = yield* provider.setLabels!({ |
| 1262 | ...target, |
| 1263 | labels: ["nope"], |
| 1264 | applied: true, |
| 1265 | }).pipe(Effect.result); |
| 1266 | assert.strictEqual(missing._tag, "Failure"); |
| 1267 | assert.strictEqual(server.writes().length, 2); |
| 1268 | }).pipe(Effect.provide(server.layer)); |
| 1269 | }); |
| 1270 | |
| 1271 | it.effect("recounts summary lines only when the compared commits move", () => { |
| 1272 | const landedBase = "fedcba9876543210fedcba9876543210fedcba98"; |
| 1273 | const mainTip = "0123012301230123012301230123012301230123"; |
| 1274 | let comparisonBaseOid = baseOid; |
| 1275 | let tips = { targetBranch: "main", observedBaseOid: baseOid }; |
| 1276 | let pullReads = 0; |
| 1277 | let listingCut = true; |
| 1278 | let hunksDown = false; |
| 1279 | const hunks = (request: SentRequest) => ({ |
| 1280 | version: 4, |
| 1281 | headOid, |
| 1282 | comparisonBaseOid, |
| 1283 | items: (request.body as { paths: ReadonlyArray<string> }).paths.map((path) => ({ |
| 1284 | path, |
| 1285 | additions: comparisonBaseOid === baseOid ? 3 : 1, |
| 1286 | deletions: 1, |
| 1287 | })), |
| 1288 | }); |
| 1289 | const server = fakeGitCafe({ |
| 1290 | // A comment moves the version without moving the compared commits. |
| 1291 | "GET /repos/owner/repo/pulls/7": () => ({ ...pull, ...tips, version: 4 + pullReads++ }), |
| 1292 | "GET /repos/owner/repo/pulls/7/changes": (request: SentRequest) => ({ |
| 1293 | version: 4, |
| 1294 | headOid, |
| 1295 | comparisonBaseOid, |
| 1296 | ...(!listingCut |
| 1297 | ? { items: [{ path: "a.ts" }, { path: "b.ts" }], next: null } |
| 1298 | : request.query.has("after") |
| 1299 | ? { items: [], next: "more" } |
| 1300 | : { items: [{ path: "a.ts" }], next: "more" }), |
| 1301 | }), |
| 1302 | get "POST /repos/owner/repo/pulls/7/diff-files"() { |
| 1303 | return hunksDown |
| 1304 | ? new Response(encodeJson({ type: "https://cafe.sh/errors/conflict" }), { status: 409 }) |
| 1305 | : hunks; |
| 1306 | }, |
| 1307 | }); |
| 1308 | const reads = (suffix: string) => |
| 1309 | server.sent.filter((request) => request.path.endsWith(suffix)).length; |
| 1310 | return Effect.gen(function* () { |
| 1311 | const provider = yield* GitCafePullRequestProvider.make; |
| 1312 | // A listing that stops before its last page holds nothing for its commits. |
| 1313 | yield* provider.getChangeRequestSummary!(target); |
| 1314 | listingCut = false; |
| 1315 | hunksDown = true; |
| 1316 | // Unread hunks leave the lines unknown, and are read again next time. |
| 1317 | const unread = yield* provider.getChangeRequestSummary!(target); |
| 1318 | assert.strictEqual(unread.additions, undefined); |
| 1319 | hunksDown = false; |
| 1320 | const first = yield* provider.getChangeRequestSummary!(target); |
| 1321 | const second = yield* provider.getChangeRequestSummary!(target); |
| 1322 | assert.deepInclude(first, { additions: 6, deletions: 2, changedFiles: 2 }); |
| 1323 | assert.deepInclude(second, { additions: 6, deletions: 2, changedFiles: 2 }); |
| 1324 | // Neither the head nor the target tip moved, so the file list is not read again. |
| 1325 | assert.deepStrictEqual([reads("/changes"), reads("/diff-files")], [4, 3]); |
| 1326 | // The target branch advancing keeps the merge base, so only the file list is read. |
| 1327 | tips = { ...tips, observedBaseOid: mainTip }; |
| 1328 | const third = yield* provider.getChangeRequestSummary!(target); |
| 1329 | assert.deepInclude(third, { additions: 6, deletions: 2, changedFiles: 2 }); |
| 1330 | assert.deepStrictEqual([reads("/changes"), reads("/diff-files")], [5, 3]); |
| 1331 | // A landed parent layer retargets the pull and moves the comparison base under the same head. |
| 1332 | comparisonBaseOid = landedBase; |
| 1333 | tips = { targetBranch: "trunk", observedBaseOid: mainTip }; |
| 1334 | const fourth = yield* provider.getChangeRequestSummary!(target); |
| 1335 | assert.deepInclude(fourth, { additions: 2, deletions: 2, changedFiles: 2 }); |
| 1336 | assert.deepStrictEqual([reads("/changes"), reads("/diff-files")], [6, 4]); |
| 1337 | }).pipe(Effect.provide(server.layer)); |
| 1338 | }); |
| 1339 | |
| 1340 | it.effect("fences a native merge on the target branch's current commit", () => { |
| 1341 | const branchOid = "fedcba9876543210fedcba9876543210fedcba98"; |
| 1342 | const server = fakeGitCafe({ |
| 1343 | "GET /repos/owner/repo/pulls/7": { ...pull, mergeRoute: "native", observedBaseOid: null }, |
| 1344 | "GET /repos/owner/repo/commit": { oid: branchOid }, |
| 1345 | "POST /repos/owner/repo/pulls/7/merge": { id: "merge-1", state: "completed" }, |
| 1346 | }); |
| 1347 | return Effect.gen(function* () { |
| 1348 | const provider = yield* GitCafePullRequestProvider.make; |
| 1349 | yield* provider.runAction({ ...target, action: "merge" }); |
| 1350 | const branchRead = server.sent.find((request) => request.path === "/repos/owner/repo/commit"); |
| 1351 | assert.strictEqual(branchRead?.query.get("ref"), "refs/heads/main"); |
| 1352 | assert.deepInclude(server.sent.at(-1)?.body as object, { baseOid: branchOid, headOid }); |
| 1353 | }).pipe(Effect.provide(server.layer)); |
| 1354 | }); |
| 1355 | |
| 1356 | it.effect("refuses to drop a reaction whose id it cannot address", () => { |
| 1357 | const server = fakeGitCafe({ |
| 1358 | "GET /repos/owner/repo/pulls/7/reactions/": { |
| 1359 | items: [ |
| 1360 | { |
| 1361 | subject: { kind: "pull_request_comment", id: "root" }, |
| 1362 | emoji: { kind: "unicode", value: "🚀" }, |
| 1363 | count: 1, |
| 1364 | viewerReactionId: "../escape", |
| 1365 | reactors: [actor], |
| 1366 | }, |
| 1367 | ], |
| 1368 | next: null, |
| 1369 | }, |
| 1370 | }); |
| 1371 | return Effect.gen(function* () { |
| 1372 | const provider = yield* GitCafePullRequestProvider.make; |
| 1373 | const result = yield* provider |
| 1374 | .setReaction({ ...target, subjectId: "root", content: "rocket", reacted: false }) |
| 1375 | .pipe(Effect.result); |
| 1376 | assert.strictEqual(result._tag, "Failure"); |
| 1377 | assert.deepStrictEqual(server.writes(), []); |
| 1378 | }).pipe(Effect.provide(server.layer)); |
| 1379 | }); |
| 1380 | }); |