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 Deferred from "effect/Deferred"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Fiber from "effect/Fiber"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import type { PullRequestCheck, PullRequestCheckStatus } from "@t3tools/contracts"; |
| 7 | |
| 8 | import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts"; |
| 9 | import { |
| 10 | BLOB_BATCH_FILES, |
| 11 | LOCATION_CACHE_CAPACITY, |
| 12 | make, |
| 13 | MAX_DIFF_SPAWNS, |
| 14 | } from "./AzureDevOpsPullRequestProvider.ts"; |
| 15 | import { |
| 16 | byteLength, |
| 17 | MAX_DIFF_SLICE_BYTES, |
| 18 | MAX_DIFF_SLICE_FILES, |
| 19 | MAX_FILE_DIFF_EDITS, |
| 20 | parseAzureDevOpsDiffCursor, |
| 21 | } from "./azureDevOpsDiff.ts"; |
| 22 | import type { AzureDevOpsChangeEntry } from "./azureDevOpsPullRequestJson.ts"; |
| 23 | |
| 24 | const ITERATION = { id: 3, headCommit: "head", mergeBaseCommit: "base" }; |
| 25 | |
| 26 | const PULL_REQUEST = { |
| 27 | number: 7, |
| 28 | title: "Pull request 7", |
| 29 | url: "https://dev.azure.com/acme/web/_git/web/pullrequest/7", |
| 30 | author: null, |
| 31 | headBranch: "feat/page", |
| 32 | baseBranch: "main", |
| 33 | state: "open" as const, |
| 34 | isDraft: false, |
| 35 | mergeability: "mergeable" as const, |
| 36 | createdAt: "2026-07-01T00:00:00Z", |
| 37 | updatedAt: "2026-07-02T00:00:00Z", |
| 38 | closedAt: null, |
| 39 | body: "", |
| 40 | reviewRequestLogins: [], |
| 41 | reviewers: [], |
| 42 | labels: [], |
| 43 | location: { project: "acme", repository: "web" }, |
| 44 | autoMergeEnabled: false, |
| 45 | }; |
| 46 | |
| 47 | function change( |
| 48 | path: string, |
| 49 | changeKind: AzureDevOpsChangeEntry["changeKind"] = "change", |
| 50 | ): AzureDevOpsChangeEntry { |
| 51 | return { path, oldPath: path, changeKind, objectId: "8f80", originalObjectId: "0ca4" }; |
| 52 | } |
| 53 | |
| 54 | /** A batch that returns nothing, so every file takes the per-file read. */ |
| 55 | const NO_BLOBS = { readBlobs: () => Effect.succeed(new Map()) }; |
| 56 | |
| 57 | function side(prefix: string, lines: number, width: number): string { |
| 58 | const pad = "z".repeat(width); |
| 59 | return `${Array.from({ length: lines }, (_, line) => `${prefix} ${line} ${pad}`).join("\n")}\n`; |
| 60 | } |
| 61 | |
| 62 | const readSlice = (input: { |
| 63 | readonly paths: ReadonlyArray<string>; |
| 64 | readonly lines: number; |
| 65 | readonly width: number; |
| 66 | readonly refused?: ReadonlyArray<string>; |
| 67 | readonly created?: ReadonlyArray<string>; |
| 68 | readonly cursor?: string; |
| 69 | }) => |
| 70 | Effect.gen(function* () { |
| 71 | const reads: string[] = []; |
| 72 | const refused = new Set(input.refused ?? []); |
| 73 | const created = new Set(input.created ?? []); |
| 74 | let inFlight = 0; |
| 75 | let peakInFlight = 0; |
| 76 | |
| 77 | const provider = yield* make.pipe( |
| 78 | Effect.provide( |
| 79 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 80 | ...NO_BLOBS, |
| 81 | getPullRequest: () => Effect.succeed(PULL_REQUEST), |
| 82 | listIterations: () => Effect.succeed([ITERATION]), |
| 83 | listIterationChanges: () => |
| 84 | Effect.succeed({ |
| 85 | changes: input.paths.map((path) => |
| 86 | change(path, created.has(path) ? "new" : "change"), |
| 87 | ), |
| 88 | truncated: false, |
| 89 | }), |
| 90 | readItemContent: (item) => |
| 91 | Effect.gen(function* () { |
| 92 | reads.push(item.path); |
| 93 | inFlight += 1; |
| 94 | peakInFlight = Math.max(peakInFlight, inFlight); |
| 95 | // Yield like a subprocess to exercise concurrency. |
| 96 | const answersAfter = input.paths.length - input.paths.indexOf(item.path); |
| 97 | for (let turn = 0; turn < answersAfter; turn += 1) yield* Effect.yieldNow; |
| 98 | inFlight -= 1; |
| 99 | if (refused.has(item.path)) { |
| 100 | return yield* new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({ |
| 101 | command: "az", |
| 102 | cwd: "/w", |
| 103 | operation: "readItemContent", |
| 104 | cause: "refused", |
| 105 | }); |
| 106 | } |
| 107 | const isOldSide = item.commit !== ITERATION.headCommit; |
| 108 | if (isOldSide && created.has(item.path)) return { contents: "", isBinary: false }; |
| 109 | return { |
| 110 | contents: side(isOldSide ? "old" : "new", input.lines, input.width), |
| 111 | isBinary: false, |
| 112 | }; |
| 113 | }), |
| 114 | }), |
| 115 | ), |
| 116 | ); |
| 117 | |
| 118 | const slice = yield* provider.getDiff({ |
| 119 | cwd: "/w", |
| 120 | repository: "acme/web", |
| 121 | host: "dev.azure.com", |
| 122 | number: 7, |
| 123 | ...(input.cursor === undefined ? {} : { cursor: input.cursor }), |
| 124 | }); |
| 125 | return { slice, reads, peakInFlight }; |
| 126 | }); |
| 127 | |
| 128 | function patchedPaths(patch: string): ReadonlyArray<string> { |
| 129 | return [...patch.matchAll(/^diff --git a\/(?<path>\S+) b\//gmu)].map( |
| 130 | (match) => match.groups?.path ?? "", |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | describe("getChangeRequest", () => { |
| 135 | it.effect("preserves the newest head when file counting fails", () => |
| 136 | Effect.gen(function* () { |
| 137 | const provider = yield* make.pipe( |
| 138 | Effect.provide( |
| 139 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 140 | getPullRequest: () => Effect.succeed(PULL_REQUEST), |
| 141 | listPolicyChecks: () => Effect.succeed([]), |
| 142 | listLabels: () => Effect.succeed([]), |
| 143 | listIterations: () => |
| 144 | Effect.succeed([ITERATION, { ...ITERATION, id: 4, headCommit: "new-head" }]), |
| 145 | listIterationChanges: () => |
| 146 | Effect.fail( |
| 147 | new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({ |
| 148 | command: "az", |
| 149 | cwd: "/w", |
| 150 | operation: "listIterationChanges", |
| 151 | cause: "unavailable", |
| 152 | }), |
| 153 | ), |
| 154 | }), |
| 155 | ), |
| 156 | ); |
| 157 | |
| 158 | const detail = yield* provider.getChangeRequest({ |
| 159 | cwd: "/w", |
| 160 | repository: "acme/web", |
| 161 | host: "dev.azure.com", |
| 162 | number: 7, |
| 163 | }); |
| 164 | expect(detail.headSha).toBe("new-head"); |
| 165 | expect(detail.changedFiles).toBe(0); |
| 166 | }), |
| 167 | ); |
| 168 | |
| 169 | it.effect.each(["missing", "failed"] as const)( |
| 170 | "leaves the head unavailable when the iteration read is %s", |
| 171 | (state) => |
| 172 | Effect.gen(function* () { |
| 173 | const provider = yield* make.pipe( |
| 174 | Effect.provide( |
| 175 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 176 | getPullRequest: () => Effect.succeed(PULL_REQUEST), |
| 177 | listPolicyChecks: () => Effect.succeed([]), |
| 178 | listLabels: () => Effect.succeed([]), |
| 179 | listIterations: () => |
| 180 | state === "missing" |
| 181 | ? Effect.succeed([]) |
| 182 | : Effect.fail( |
| 183 | new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({ |
| 184 | command: "az", |
| 185 | cwd: "/w", |
| 186 | operation: "listIterations", |
| 187 | cause: "unavailable", |
| 188 | }), |
| 189 | ), |
| 190 | }), |
| 191 | ), |
| 192 | ); |
| 193 | const detail = yield* provider.getChangeRequest({ |
| 194 | cwd: "/w", |
| 195 | repository: "acme/web", |
| 196 | host: "dev.azure.com", |
| 197 | number: 7, |
| 198 | }); |
| 199 | expect(detail.headSha).toBeUndefined(); |
| 200 | expect(detail.changedFiles).toBe(0); |
| 201 | }), |
| 202 | ); |
| 203 | |
| 204 | it.effect( |
| 205 | "reads the pull request once for overlapping detail and conversation, then its diff", |
| 206 | () => |
| 207 | Effect.gen(function* () { |
| 208 | let reads = 0; |
| 209 | const release = yield* Deferred.make<void>(); |
| 210 | const provider = yield* make.pipe( |
| 211 | Effect.provide( |
| 212 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 213 | getPullRequest: () => |
| 214 | Effect.sync(() => { |
| 215 | reads += 1; |
| 216 | }).pipe(Effect.andThen(Deferred.await(release)), Effect.as(PULL_REQUEST)), |
| 217 | listIterations: () => Effect.succeed([ITERATION]), |
| 218 | listIterationChanges: () => |
| 219 | Effect.succeed({ changes: [change("a.ts")], truncated: false }), |
| 220 | readItemContent: () => |
| 221 | Effect.succeed({ contents: side("new", 2, 4), isBinary: false }), |
| 222 | ...NO_BLOBS, |
| 223 | listPolicyChecks: () => Effect.succeed([]), |
| 224 | listLabels: () => Effect.succeed([]), |
| 225 | listThreads: () => Effect.succeed([]), |
| 226 | }), |
| 227 | ), |
| 228 | ); |
| 229 | const input = { cwd: "/w", repository: "acme/web", host: "dev.azure.com", number: 7 }; |
| 230 | |
| 231 | // Clients request detail and conversation together; both start before the read returns. |
| 232 | const detail = yield* Effect.forkChild(provider.getChangeRequest(input), { |
| 233 | startImmediately: true, |
| 234 | }); |
| 235 | const activity = yield* Effect.forkChild(provider.getChangeRequestActivity(input), { |
| 236 | startImmediately: true, |
| 237 | }); |
| 238 | yield* Deferred.succeed(release, undefined); |
| 239 | yield* Fiber.join(detail); |
| 240 | yield* Fiber.join(activity); |
| 241 | yield* provider.getDiff(input); |
| 242 | |
| 243 | expect(reads).toBe(1); |
| 244 | }), |
| 245 | ); |
| 246 | }); |
| 247 | |
| 248 | describe("policy checks", () => { |
| 249 | const REPOSITORY = { |
| 250 | cwd: "/w", |
| 251 | repository: "acme/web", |
| 252 | host: "dev.azure.com", |
| 253 | number: 7, |
| 254 | }; |
| 255 | const check = (status: PullRequestCheckStatus): PullRequestCheck => ({ |
| 256 | name: "PR validation", |
| 257 | status, |
| 258 | description: null, |
| 259 | url: null, |
| 260 | required: true, |
| 261 | }); |
| 262 | const readFailure = new AzureDevOpsPullRequestCli.AzureDevOpsPullRequestReadError({ |
| 263 | command: "az", |
| 264 | cwd: "/w", |
| 265 | operation: "listPolicyChecks", |
| 266 | cause: "unavailable", |
| 267 | }); |
| 268 | |
| 269 | const provide = ( |
| 270 | listPolicyChecks: AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli["Service"]["listPolicyChecks"], |
| 271 | pullRequest = PULL_REQUEST, |
| 272 | ) => |
| 273 | make.pipe( |
| 274 | Effect.provide( |
| 275 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 276 | getPullRequest: () => Effect.succeed(pullRequest), |
| 277 | listIterations: () => Effect.succeed([ITERATION]), |
| 278 | listIterationChanges: () => Effect.succeed({ changes: [], truncated: false }), |
| 279 | listPolicyChecks, |
| 280 | listLabels: () => Effect.succeed([]), |
| 281 | }), |
| 282 | ), |
| 283 | ); |
| 284 | |
| 285 | it.effect("keeps the detail readable when the policies are not", () => |
| 286 | Effect.gen(function* () { |
| 287 | const provider = yield* provide(() => Effect.fail(readFailure)); |
| 288 | const detail = yield* provider.getChangeRequest(REPOSITORY); |
| 289 | expect(detail.checks).toEqual([]); |
| 290 | }), |
| 291 | ); |
| 292 | |
| 293 | it.effect.each([ |
| 294 | { statuses: ["pending", "failure"], expected: "failing" }, |
| 295 | { statuses: [], expected: null }, |
| 296 | ] as const)("summarizes $statuses as $expected", ({ statuses, expected }) => |
| 297 | Effect.gen(function* () { |
| 298 | const provider = yield* provide(() => Effect.succeed(statuses.map(check))); |
| 299 | const summary = yield* provider.getChangeRequestSummary!(REPOSITORY); |
| 300 | expect(summary.checksState).toBe(expected); |
| 301 | }), |
| 302 | ); |
| 303 | |
| 304 | it.effect("leaves the last known state alone when the policies cannot be read", () => |
| 305 | Effect.gen(function* () { |
| 306 | const provider = yield* provide(() => Effect.fail(readFailure)); |
| 307 | const summary = yield* provider.getChangeRequestSummary!(REPOSITORY); |
| 308 | expect(summary.checksState).toBeUndefined(); |
| 309 | }), |
| 310 | ); |
| 311 | }); |
| 312 | |
| 313 | describe("getDiff reads", () => { |
| 314 | it.effect("holds every reader together to one request's worth of processes", () => |
| 315 | Effect.gen(function* () { |
| 316 | const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"]; |
| 317 | let inFlight = 0; |
| 318 | let peakInFlight = 0; |
| 319 | |
| 320 | const provider = yield* make.pipe( |
| 321 | Effect.provide( |
| 322 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 323 | ...NO_BLOBS, |
| 324 | getPullRequest: () => Effect.succeed(PULL_REQUEST), |
| 325 | listIterations: () => Effect.succeed([ITERATION]), |
| 326 | listIterationChanges: () => |
| 327 | Effect.succeed({ changes: paths.map((path) => change(path)), truncated: false }), |
| 328 | readItemContent: () => |
| 329 | Effect.gen(function* () { |
| 330 | inFlight += 1; |
| 331 | peakInFlight = Math.max(peakInFlight, inFlight); |
| 332 | // Yield like a subprocess to exercise concurrency. |
| 333 | yield* Effect.yieldNow; |
| 334 | yield* Effect.yieldNow; |
| 335 | inFlight -= 1; |
| 336 | return { contents: side("new", 2, 4), isBinary: false }; |
| 337 | }), |
| 338 | }), |
| 339 | ), |
| 340 | ); |
| 341 | |
| 342 | const readDiff = (number: number) => |
| 343 | provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number }); |
| 344 | |
| 345 | yield* Effect.all([readDiff(7), readDiff(8)], { concurrency: 2 }); |
| 346 | |
| 347 | expect(peakInFlight).toBeLessThanOrEqual(MAX_DIFF_SPAWNS); |
| 348 | }), |
| 349 | ); |
| 350 | |
| 351 | it.effect("reads a slice's files in one blob batch, and per file only what it missed", () => |
| 352 | Effect.gen(function* () { |
| 353 | const paths = ["a.ts", "b.ts", "c.ts", "gone.ts", "added.ts"]; |
| 354 | const changes = [ |
| 355 | ...["a.ts", "b.ts", "c.ts"].map((path) => ({ |
| 356 | ...change(path), |
| 357 | objectId: `${path.toUpperCase()}-NEW`, |
| 358 | originalObjectId: `${path.toUpperCase()}-OLD`, |
| 359 | })), |
| 360 | { ...change("gone.ts", "deleted"), objectId: null, originalObjectId: "GONE-OLD" }, |
| 361 | { ...change("added.ts", "new"), objectId: "ADDED-NEW", originalObjectId: null }, |
| 362 | ]; |
| 363 | const batches: ReadonlyArray<string>[] = []; |
| 364 | const reads: string[] = []; |
| 365 | |
| 366 | const provider = yield* make.pipe( |
| 367 | Effect.provide( |
| 368 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 369 | getPullRequest: () => Effect.succeed(PULL_REQUEST), |
| 370 | listIterations: () => Effect.succeed([ITERATION]), |
| 371 | listIterationChanges: () => Effect.succeed({ changes, truncated: false }), |
| 372 | readBlobs: (input) => |
| 373 | Effect.sync(() => { |
| 374 | batches.push(input.objectIds); |
| 375 | // Azure left one side of b.ts out, as it does for a file only it can decode. |
| 376 | return new Map( |
| 377 | input.objectIds |
| 378 | .filter((id) => id !== "B.TS-OLD") |
| 379 | .map((id) => [ |
| 380 | id.toLowerCase(), |
| 381 | { |
| 382 | contents: side(id.endsWith("NEW") ? "new" : "old", 2, 4), |
| 383 | isBinary: false, |
| 384 | }, |
| 385 | ]), |
| 386 | ); |
| 387 | }), |
| 388 | readItemContent: (item) => |
| 389 | Effect.sync(() => { |
| 390 | reads.push(`${item.commit}:${item.path}`); |
| 391 | return { |
| 392 | contents: side(item.commit === ITERATION.headCommit ? "new" : "old", 2, 4), |
| 393 | isBinary: false, |
| 394 | }; |
| 395 | }), |
| 396 | }), |
| 397 | ), |
| 398 | ); |
| 399 | |
| 400 | const slice = yield* provider.getDiff({ |
| 401 | cwd: "/w", |
| 402 | repository: "acme/web", |
| 403 | host: "dev.azure.com", |
| 404 | number: 7, |
| 405 | }); |
| 406 | |
| 407 | expect(patchedPaths(slice.patch)).toEqual(paths); |
| 408 | expect(slice.patch.match(/^@@ /gmu)).toHaveLength(paths.length); |
| 409 | expect(batches).toEqual([ |
| 410 | [ |
| 411 | "A.TS-OLD", |
| 412 | "A.TS-NEW", |
| 413 | "B.TS-OLD", |
| 414 | "B.TS-NEW", |
| 415 | "C.TS-OLD", |
| 416 | "C.TS-NEW", |
| 417 | "GONE-OLD", |
| 418 | "ADDED-NEW", |
| 419 | ], |
| 420 | ]); |
| 421 | expect(reads.toSorted()).toEqual(["base:b.ts", "head:b.ts"]); |
| 422 | }), |
| 423 | ); |
| 424 | |
| 425 | it.effect("starts a new blob batch where the last one's files run out", () => |
| 426 | Effect.gen(function* () { |
| 427 | const paths = Array.from({ length: BLOB_BATCH_FILES + 3 }, (_, at) => `f${at}.ts`); |
| 428 | // Added lines that bring the slice near its edit budget by file 96, so the read batches |
| 429 | // there narrow to 3 and the one from file 99 would reach past the blob batch. |
| 430 | const added = (at: number) => (at < 60 ? 41 : at < 96 ? 40 : at < 99 ? 0 : 10); |
| 431 | const batchSizes: number[] = []; |
| 432 | let fileReads = 0; |
| 433 | |
| 434 | const provider = yield* make.pipe( |
| 435 | Effect.provide( |
| 436 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 437 | getPullRequest: () => Effect.succeed(PULL_REQUEST), |
| 438 | listIterations: () => Effect.succeed([ITERATION]), |
| 439 | listIterationChanges: () => |
| 440 | Effect.succeed({ |
| 441 | changes: paths.map((path) => ({ |
| 442 | ...change(path), |
| 443 | objectId: `${path}-new`, |
| 444 | originalObjectId: `${path}-old`, |
| 445 | })), |
| 446 | truncated: false, |
| 447 | }), |
| 448 | readBlobs: (input) => |
| 449 | Effect.sync(() => { |
| 450 | batchSizes.push(input.objectIds.length); |
| 451 | return new Map( |
| 452 | input.objectIds.map((id) => { |
| 453 | const at = Number(id.slice(1, id.indexOf("."))); |
| 454 | const lines = id.endsWith("-new") ? added(at) : 0; |
| 455 | return [ |
| 456 | id, |
| 457 | { contents: lines === 0 ? "" : side("+", lines, 1), isBinary: false }, |
| 458 | ]; |
| 459 | }), |
| 460 | ); |
| 461 | }), |
| 462 | readItemContent: () => |
| 463 | Effect.sync(() => ((fileReads += 1), { contents: "", isBinary: false })), |
| 464 | }), |
| 465 | ), |
| 466 | ); |
| 467 | |
| 468 | const slice = yield* provider.getDiff({ |
| 469 | cwd: "/w", |
| 470 | repository: "acme/web", |
| 471 | host: "dev.azure.com", |
| 472 | number: 7, |
| 473 | }); |
| 474 | |
| 475 | expect(patchedPaths(slice.patch)).toHaveLength(paths.length); |
| 476 | expect(batchSizes).toEqual([2 * BLOB_BATCH_FILES, 6]); |
| 477 | expect(fileReads).toBe(0); |
| 478 | }), |
| 479 | ); |
| 480 | |
| 481 | it.effect("serves a later slice from the iteration and change list the first one read", () => |
| 482 | Effect.gen(function* () { |
| 483 | const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"]; |
| 484 | let iterationReads = 0; |
| 485 | let changeReads = 0; |
| 486 | |
| 487 | const provider = yield* make.pipe( |
| 488 | Effect.provide( |
| 489 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 490 | ...NO_BLOBS, |
| 491 | getPullRequest: () => Effect.succeed(PULL_REQUEST), |
| 492 | listIterations: () => Effect.sync(() => ((iterationReads += 1), [ITERATION])), |
| 493 | listIterationChanges: () => |
| 494 | Effect.sync(() => { |
| 495 | changeReads += 1; |
| 496 | return { changes: paths.map((path) => change(path)), truncated: false }; |
| 497 | }), |
| 498 | readItemContent: (item) => |
| 499 | Effect.succeed({ |
| 500 | contents: side(item.commit === ITERATION.headCommit ? "new" : "old", 100, 900), |
| 501 | isBinary: false, |
| 502 | }), |
| 503 | }), |
| 504 | ), |
| 505 | ); |
| 506 | const readDiff = (cursor?: string) => |
| 507 | provider.getDiff({ |
| 508 | cwd: "/w", |
| 509 | repository: "acme/web", |
| 510 | host: "dev.azure.com", |
| 511 | number: 7, |
| 512 | ...(cursor === undefined ? {} : { cursor }), |
| 513 | }); |
| 514 | |
| 515 | const first = yield* readDiff(); |
| 516 | const second = yield* readDiff(first.nextCursor ?? undefined); |
| 517 | |
| 518 | expect(patchedPaths(second.patch)).toEqual(["c.ts", "d.ts"]); |
| 519 | expect(iterationReads).toBe(1); |
| 520 | expect(changeReads).toBe(1); |
| 521 | }), |
| 522 | ); |
| 523 | |
| 524 | it.effect("leaves a run of files it could not diff at all for the next slice", () => |
| 525 | Effect.gen(function* () { |
| 526 | const paths = Array.from({ length: MAX_DIFF_SLICE_FILES + 20 }, (_, at) => `gen/a${at}.bin`); |
| 527 | const read = yield* readSlice({ paths, lines: 2, width: 4, refused: paths }); |
| 528 | |
| 529 | expect(patchedPaths(read.slice.patch)).toHaveLength(MAX_DIFF_SLICE_FILES); |
| 530 | expect(byteLength(read.slice.patch)).toBeLessThan(MAX_DIFF_SLICE_BYTES); |
| 531 | expect(parseAzureDevOpsDiffCursor(read.slice.nextCursor)?.fileIndex).toBe( |
| 532 | MAX_DIFF_SLICE_FILES, |
| 533 | ); |
| 534 | }), |
| 535 | ); |
| 536 | |
| 537 | it.effect("keeps the patch in the order the change was listed, whoever answered first", () => |
| 538 | Effect.gen(function* () { |
| 539 | const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts"]; |
| 540 | const read = yield* readSlice({ paths, lines: 2, width: 4 }); |
| 541 | |
| 542 | expect(patchedPaths(read.slice.patch)).toEqual(paths); |
| 543 | expect(read.slice.nextCursor).toBeNull(); |
| 544 | }), |
| 545 | ); |
| 546 | |
| 547 | it.effect("leaves a file the host refused listed without its hunks, in its place", () => |
| 548 | Effect.gen(function* () { |
| 549 | const paths = ["a.ts", "b.ts", "c.ts"]; |
| 550 | const read = yield* readSlice({ paths, lines: 2, width: 4, refused: ["b.ts"] }); |
| 551 | |
| 552 | expect(patchedPaths(read.slice.patch)).toEqual(paths); |
| 553 | expect(read.slice.truncated).toBe(true); |
| 554 | expect(read.slice.patch).toContain("+++ b/b.ts\ndiff --git a/c.ts"); |
| 555 | expect(read.slice.patch.match(/^@@ /gmu)).toHaveLength(2); |
| 556 | }), |
| 557 | ); |
| 558 | }); |
| 559 | |
| 560 | describe("what one diff slice spends", () => { |
| 561 | it.effect("stops on the byte ceiling without carrying what it read past it", () => |
| 562 | Effect.gen(function* () { |
| 563 | const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"]; |
| 564 | const read = yield* readSlice({ paths, lines: 100, width: 900 }); |
| 565 | |
| 566 | expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES); |
| 567 | expect(patchedPaths(read.slice.patch)).toEqual(["a.ts", "b.ts"]); |
| 568 | expect(read.slice.nextCursor).toBe(`${ITERATION.id}:2`); |
| 569 | expect(new Set(read.reads)).toEqual(new Set(["a.ts", "b.ts", "c.ts", "d.ts"])); |
| 570 | }), |
| 571 | ); |
| 572 | |
| 573 | it.effect("stops once the diff work one request may do is spent", () => |
| 574 | Effect.gen(function* () { |
| 575 | const paths = Array.from({ length: 12 }, (_, file) => `file-${file}.ts`); |
| 576 | const read = yield* readSlice({ paths, lines: MAX_FILE_DIFF_EDITS / 4, width: 1 }); |
| 577 | |
| 578 | expect(read.slice.patch.length).toBeLessThan(MAX_DIFF_SLICE_BYTES); |
| 579 | expect(patchedPaths(read.slice.patch)).toEqual(paths.slice(0, 5)); |
| 580 | expect(read.slice.nextCursor).toBe(`${ITERATION.id}:5`); |
| 581 | }), |
| 582 | ); |
| 583 | |
| 584 | it.effect("carries a whole new file and stops the slice on what it weighed", () => |
| 585 | Effect.gen(function* () { |
| 586 | const paths = ["new.ts", "b.ts", "c.ts", "d.ts"]; |
| 587 | const read = yield* readSlice({ paths, lines: 8_000, width: 30, created: ["new.ts"] }); |
| 588 | |
| 589 | expect(patchedPaths(read.slice.patch)).toEqual(["new.ts"]); |
| 590 | expect(read.slice.patch).toContain("--- /dev/null"); |
| 591 | expect(read.slice.patch).toContain("@@ -0,0 +1,8000 @@"); |
| 592 | expect(read.slice.patch.length).toBeGreaterThan(MAX_DIFF_SLICE_BYTES); |
| 593 | expect(read.slice.nextCursor).toBe(`${ITERATION.id}:1`); |
| 594 | }), |
| 595 | ); |
| 596 | |
| 597 | it.effect("carries on from where the last slice stopped", () => |
| 598 | Effect.gen(function* () { |
| 599 | const paths = ["a.ts", "b.ts", "c.ts", "d.ts", "e.ts", "f.ts"]; |
| 600 | const read = yield* readSlice({ |
| 601 | paths, |
| 602 | lines: 100, |
| 603 | width: 900, |
| 604 | cursor: `${ITERATION.id}:2`, |
| 605 | }); |
| 606 | |
| 607 | expect(patchedPaths(read.slice.patch)).toEqual(["c.ts", "d.ts"]); |
| 608 | expect(read.slice.nextCursor).toBe(`${ITERATION.id}:4`); |
| 609 | }), |
| 610 | ); |
| 611 | it.effect("keeps the pull request being read, not the one looked up first", () => |
| 612 | Effect.gen(function* () { |
| 613 | const HOT = 7; |
| 614 | const readsOf = new Map<number, number>(); |
| 615 | |
| 616 | const provider = yield* make.pipe( |
| 617 | Effect.provide( |
| 618 | Layer.mock(AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli)({ |
| 619 | ...NO_BLOBS, |
| 620 | getPullRequest: (input) => |
| 621 | Effect.sync(() => { |
| 622 | readsOf.set(input.number, (readsOf.get(input.number) ?? 0) + 1); |
| 623 | return { ...PULL_REQUEST, number: input.number }; |
| 624 | }), |
| 625 | listIterations: () => Effect.succeed([ITERATION]), |
| 626 | listIterationChanges: () => |
| 627 | Effect.succeed({ changes: [change("a.ts")], truncated: false }), |
| 628 | readItemContent: () => Effect.succeed({ contents: side("new", 2, 4), isBinary: false }), |
| 629 | }), |
| 630 | ), |
| 631 | ); |
| 632 | |
| 633 | const readDiff = (number: number) => |
| 634 | provider.getDiff({ cwd: "/w", repository: "acme/web", host: "dev.azure.com", number }); |
| 635 | |
| 636 | yield* readDiff(HOT); |
| 637 | for (let filled = 0; filled < LOCATION_CACHE_CAPACITY; filled += 1) { |
| 638 | yield* readDiff(HOT + 1 + filled); |
| 639 | yield* readDiff(HOT); |
| 640 | } |
| 641 | |
| 642 | expect(readsOf.get(HOT)).toBe(1); |
| 643 | }), |
| 644 | ); |
| 645 | }); |