Open History to see the last commit
1import { afterEach, assert, expect, it, vi } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { ChildProcessSpawner } from "effect/unstable/process";
5
6import * as AzureDevOpsCli from "../sourceControl/AzureDevOpsCli.ts";
7import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
8import * as AzureDevOpsPullRequestProvider from "./AzureDevOpsPullRequestProvider.ts";
9
10const mockedExecute = vi.fn<AzureDevOpsCli.AzureDevOpsCli["Service"]["execute"]>();
11
12const layer = it.layer(
13 AzureDevOpsPullRequestCli.layer.pipe(
14 Layer.provide(
15 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({
16 execute: mockedExecute,
17 }),
18 ),
19 ),
20);
21
22function output(stdout: string) {
23 return {
24 exitCode: ChildProcessSpawner.ExitCode(0),
25 stdout,
26 stderr: "",
27 stdoutTruncated: false,
28 stderrTruncated: false,
29 };
30}
31
32/** What VcsProcess allows a read that asked for no ceiling of its own. */
33const VCS_DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
34
35/**
36 * The runner as it really behaves: it cuts stdout at the ceiling its caller asked for, and cuts
37 * it at the process default when the caller asked for none. A read whose response is larger than
38 * its ceiling gets JSON that stops mid-string, which is the whole cost of an unset ceiling.
39 */
40function outputWithin(maxOutputBytes: number | undefined, response: string) {
41 const ceiling = maxOutputBytes ?? VCS_DEFAULT_MAX_OUTPUT_BYTES;
42 return ceiling >= Buffer.byteLength(response)
43 ? output(response)
44 : { ...output(response.slice(0, ceiling)), stdoutTruncated: true };
45}
46
47/** A fixture's own shape, spelled the way `az` would answer with it. */
48const json = (value: Record<string, unknown>) => JSON.stringify(value);
49
50const pullRequestRow = {
51 pullRequestId: 42,
52 title: "Add the page",
53 status: "active",
54 sourceRefName: "refs/heads/feat/page",
55 targetRefName: "refs/heads/main",
56 creationDate: "2026-07-01T00:00:00Z",
57 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
58 repository: { name: "web", project: { name: "platform" } },
59};
60
61const oneIteration = {
62 value: [
63 {
64 id: 1,
65 sourceRefCommit: { commitId: "a".repeat(40) },
66 commonRefCommit: { commitId: "b".repeat(40) },
67 },
68 ],
69};
70
71function pullRequestRows(
72 count: number,
73 firstNumber: number,
74): ReadonlyArray<Record<string, unknown>> {
75 return Array.from({ length: count }, (_, index) => ({
76 pullRequestId: firstNumber + index,
77 title: `Pull request ${firstNumber + index}`,
78 status: "active",
79 sourceRefName: "refs/heads/feat/page",
80 targetRefName: "refs/heads/main",
81 creationDate: "2026-07-01T00:00:00Z",
82 repository: { name: "web", project: { name: "platform" } },
83 url: `https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/${firstNumber + index}`,
84 }));
85}
86
87function pullRequests(count: number, firstNumber: number): string {
88 return JSON.stringify(pullRequestRows(count, firstNumber));
89}
90
91/** The arguments of the nth az invocation. */
92function argsOfCall(index: number): ReadonlyArray<string> {
93 const call = mockedExecute.mock.calls[index];
94 assert.isDefined(call);
95 return call[0].args;
96}
97
98/** The output ceiling the nth az invocation asked for, if it asked for one at all. */
99function maxOutputBytesOfCall(index: number) {
100 const call = mockedExecute.mock.calls[index];
101 assert.isDefined(call);
102 return call[0].maxOutputBytes;
103}
104
105/** A page of change entries the size Azure really answers with, url and object ids and all. */
106function changeEntries(count: number): ReadonlyArray<Record<string, unknown>> {
107 const commit = "c".repeat(40);
108 return Array.from({ length: count }, (_, index) => {
109 const path = `/apps/server/src/generated/module-${index}/persisted-projection-${index}.ts`;
110 return {
111 changeType: "edit",
112 item: {
113 path,
114 objectId: "a".repeat(40),
115 originalObjectId: "b".repeat(40),
116 commitId: commit,
117 gitObjectType: "blob",
118 url: `https://dev.azure.com/acme/platform/_apis/git/repositories/6f9c9b7f-0000-0000-0000-000000000000/items${path}?versionType=Commit&version=${commit}`,
119 },
120 };
121 });
122}
123
124/** An Azure identity, which rides along with every comment and every push Azure answers with. */
125function identity(name: string) {
126 const id = "6f9c9b7f-0000-0000-0000-000000000000";
127 return {
128 displayName: name,
129 id,
130 uniqueName: `${name.toLowerCase().replace(/ /g, ".")}@acme.com`,
131 descriptor: `aad.${"z".repeat(52)}`,
132 imageUrl: `https://dev.azure.com/acme/_api/_common/identityImage?id=${id}`,
133 url: `https://spsprodweu1.vssps.visualstudio.com/A${id}/_apis/Identities/${id}`,
134 _links: {
135 avatar: {
136 href: `https://dev.azure.com/acme/_apis/GraphProfile/MemberAvatars/aad.${"z".repeat(52)}`,
137 },
138 },
139 };
140}
141
142/** A review's threads the shape Azure answers with, system threads and identities and all. */
143function threadRows(count: number): ReadonlyArray<Record<string, unknown>> {
144 return Array.from({ length: count }, (_, index) => ({
145 id: index + 1,
146 publishedDate: "2026-07-02T00:00:00Z",
147 lastUpdatedDate: "2026-07-02T00:00:00Z",
148 status: "active",
149 threadContext: { filePath: `/apps/server/src/generated/module-${index}.ts` },
150 identities: { 1: identity("Reviewer One") },
151 isDeleted: false,
152 comments: [
153 {
154 id: 1,
155 parentCommentId: 0,
156 author: identity("Reviewer One"),
157 content: `Comment ${index}: ${"this needs another look. ".repeat(20)}`,
158 publishedDate: "2026-07-02T00:00:00Z",
159 lastUpdatedDate: "2026-07-02T00:00:00Z",
160 commentType: "text",
161 usersLiked: [],
162 },
163 ],
164 }));
165}
166
167/** A review's iterations the shape Azure answers with, one per push. */
168function iterationRows(count: number): ReadonlyArray<Record<string, unknown>> {
169 return Array.from({ length: count }, (_, index) => ({
170 id: index + 1,
171 description: `Pushed ${index} commits`,
172 author: identity("Author One"),
173 createdDate: "2026-07-02T00:00:00Z",
174 updatedDate: "2026-07-02T00:00:00Z",
175 sourceRefCommit: { commitId: `${index}`.padStart(40, "a") },
176 targetRefCommit: { commitId: "b".repeat(40) },
177 commonRefCommit: { commitId: "c".repeat(40) },
178 hasMultipleCommits: true,
179 reason: "push",
180 push: { pushId: index + 1, date: "2026-07-02T00:00:00Z", pushedBy: identity("Author One") },
181 }));
182}
183
184afterEach(() => {
185 mockedExecute.mockReset();
186});
187
188layer("AzureDevOpsPullRequestCli.layer", (it) => {
189 it.effect("asks for one row more than the page, to probe for a next page", () =>
190 Effect.gen(function* () {
191 mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(3, 1))));
192 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
193
194 const batch = yield* cli.listPullRequests({
195 cwd: "/w",
196 repository: "web",
197 state: "open",
198 involvement: "all",
199 viewer: "bilal@acme.dev",
200 limit: 10,
201 });
202
203 assert.strictEqual(batch.items.length, 3);
204 assert.isFalse(batch.truncated);
205 expect(argsOfCall(0)).toEqual([
206 "repos",
207 "pr",
208 "list",
209 "--detect",
210 "true",
211 "--repository",
212 "web",
213 "--status",
214 "active",
215 "--include-links",
216 "--top",
217 "11",
218 "--only-show-errors",
219 "--output",
220 "json",
221 ]);
222 }),
223 );
224
225 it.effect("reads an Azure pull request page larger than the VCS default output limit", () =>
226 Effect.gen(function* () {
227 const rows = pullRequestRows(100, 1).map((row) => ({
228 ...row,
229 description: "x".repeat(10_000),
230 }));
231 // @effect-diagnostics-next-line preferSchemaOverJson:off
232 const response = JSON.stringify(rows);
233 expect(Buffer.byteLength(response)).toBeGreaterThan(1_000_000);
234
235 mockedExecute.mockImplementationOnce((input) =>
236 Effect.succeed(outputWithin(input.maxOutputBytes, response)),
237 );
238 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
239
240 const batch = yield* cli.listPullRequests({
241 cwd: "/w",
242 repository: "web",
243 state: "merged",
244 involvement: "all",
245 viewer: "bilal@acme.dev",
246 limit: 99,
247 });
248
249 assert.strictEqual(batch.items.length, 99);
250 assert.isTrue(batch.truncated);
251 }),
252 );
253
254 it.effect("reads the page unnarrowed when asked to search, having nothing to search with", () =>
255 Effect.gen(function* () {
256 mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(3, 1))));
257 const provider = yield* AzureDevOpsPullRequestProvider.make;
258
259 const page = yield* provider.listChangeRequests({
260 cwd: "/w",
261 repository: "web",
262 host: "dev.azure.com",
263 state: "open",
264 involvement: "all",
265 viewer: "bilal@acme.dev",
266 limit: 10,
267 query: "page",
268 });
269
270 // `az repos pr list` filters by status, creator, reviewer and branch, and by no text at
271 // all. The rows come back as they would have without a search, for the caller to narrow;
272 // nothing of the search reaches the command, where it could only mean the wrong thing.
273 assert.strictEqual(page.items.length, 3);
274 expect(argsOfCall(0)).toEqual([
275 "repos",
276 "pr",
277 "list",
278 "--detect",
279 "true",
280 "--repository",
281 "web",
282 "--status",
283 "active",
284 "--include-links",
285 "--top",
286 "11",
287 "--only-show-errors",
288 "--output",
289 "json",
290 ]);
291 }),
292 );
293
294 it.effect("steps over what it has already handed over, which is all Azure can be told", () =>
295 Effect.gen(function* () {
296 mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(3, 1))));
297 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
298
299 yield* cli.listPullRequests({
300 cwd: "/w",
301 repository: "web",
302 state: "open",
303 involvement: "all",
304 viewer: "bilal@acme.dev",
305 limit: 10,
306 // The instant is the same cursor every other host reads; Azure has no filter for it and
307 // takes the count instead.
308 cursor: { updatedBefore: "2026-07-02T00:00:00Z", delivered: 20 },
309 });
310
311 const args = argsOfCall(0);
312 expect(args).toContain("--skip");
313 assert.strictEqual(args[args.indexOf("--skip") + 1], "20");
314 expect(args).not.toContain("2026-07-02T00:00:00Z");
315 }),
316 );
317
318 it.effect("reports truncation from the extra row", () =>
319 Effect.gen(function* () {
320 mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(11, 1))));
321 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
322
323 const batch = yield* cli.listPullRequests({
324 cwd: "/w",
325 repository: "web",
326 state: "open",
327 involvement: "all",
328 viewer: "bilal@acme.dev",
329 limit: 10,
330 });
331
332 assert.strictEqual(batch.items.length, 10);
333 assert.isTrue(batch.truncated);
334 assert.strictEqual(batch.cursorAdvance, 10);
335 }),
336 );
337
338 it.effect("advances by malformed raw rows and keeps reading until the page is full", () =>
339 Effect.gen(function* () {
340 mockedExecute
341 .mockReturnValueOnce(
342 Effect.succeed(
343 output(
344 // @effect-diagnostics-next-line preferSchemaOverJson:off
345 JSON.stringify([
346 { pullRequestId: "malformed" },
347 pullRequestRows(1, 1)[0],
348 { pullRequestId: "also malformed" },
349 ]),
350 ),
351 ),
352 )
353 .mockReturnValueOnce(Effect.succeed(output(pullRequests(2, 2))));
354 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
355
356 const batch = yield* cli.listPullRequests({
357 cwd: "/w",
358 repository: "web",
359 state: "open",
360 involvement: "all",
361 viewer: "bilal@acme.dev",
362 limit: 2,
363 });
364
365 expect(batch.items.map((item) => item.number)).toEqual([1, 2]);
366 assert.isTrue(batch.truncated);
367 // Three raw rows from the first request and one from the second produced this page.
368 assert.strictEqual(batch.cursorAdvance, 4);
369 const secondArgs = argsOfCall(1);
370 assert.strictEqual(secondArgs[secondArgs.indexOf("--skip") + 1], "3");
371 assert.strictEqual(secondArgs[secondArgs.indexOf("--top") + 1], "2");
372 }),
373 );
374
375 it.effect("narrows to the author on the authored tab", () =>
376 Effect.gen(function* () {
377 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
378 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
379
380 yield* cli.listPullRequests({
381 cwd: "/w",
382 repository: "web",
383 state: "closed",
384 involvement: "authored",
385 viewer: "bilal@acme.dev",
386 limit: 10,
387 });
388
389 expect(argsOfCall(0)).toContain("--creator");
390 expect(argsOfCall(0)).toContain("bilal@acme.dev");
391 // Azure calls a closed pull request abandoned.
392 expect(argsOfCall(0)).toContain("abandoned");
393 }),
394 );
395
396 it.effect("asks Azure for every status on the All tab", () =>
397 Effect.gen(function* () {
398 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
399 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
400
401 yield* cli.listPullRequests({
402 cwd: "/w",
403 repository: "web",
404 state: "all",
405 involvement: "all",
406 viewer: "bilal@acme.dev",
407 limit: 10,
408 });
409
410 expect(argsOfCall(0)).toContain("--status");
411 expect(argsOfCall(0)).toContain("all");
412 }),
413 );
414
415 it.effect("narrows to the reviewer on the reviewing tab", () =>
416 Effect.gen(function* () {
417 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
418 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
419
420 yield* cli.listPullRequests({
421 cwd: "/w",
422 repository: "web",
423 state: "open",
424 involvement: "reviewing",
425 viewer: "bilal@acme.dev",
426 limit: 10,
427 });
428
429 expect(argsOfCall(0)).toContain("--reviewer");
430 }),
431 );
432
433 it.effect("reads the signed-in account, which az reports as a bare value", () =>
434 Effect.gen(function* () {
435 // `--query user` unwraps the object, so the wrapper has to put it back.
436 mockedExecute.mockReturnValueOnce(
437 // @effect-diagnostics-next-line preferSchemaOverJson:off
438 Effect.succeed(output(JSON.stringify({ name: "bilal@acme.dev", type: "user" }))),
439 );
440 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
441
442 const viewer = yield* cli.getViewer({ cwd: "/w" });
443
444 assert.strictEqual(viewer, "bilal@acme.dev");
445 expect(argsOfCall(0)).toEqual([
446 "account",
447 "show",
448 "--query",
449 "user",
450 "--only-show-errors",
451 "--output",
452 "json",
453 ]);
454 }),
455 );
456
457 it.effect("fails when nobody is signed in", () =>
458 Effect.gen(function* () {
459 mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
460 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
461
462 const error = yield* Effect.flip(cli.getViewer({ cwd: "/w" }));
463
464 assert.strictEqual(error._tag, "AzureDevOpsViewerUnavailableError");
465 }),
466 );
467
468 it.effect("completes a pull request to merge it, squashing only when asked", () =>
469 Effect.gen(function* () {
470 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
471 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
472
473 yield* cli.runPullRequestAction({
474 cwd: "/w",
475 number: 42,
476 action: "merge",
477 mergeMethod: "squash",
478 });
479
480 expect(argsOfCall(0)).toEqual([
481 "repos",
482 "pr",
483 "update",
484 "--detect",
485 "true",
486 "--id",
487 "42",
488 "--status",
489 "completed",
490 "--squash",
491 "true",
492 "--only-show-errors",
493 "--output",
494 "json",
495 ]);
496 }),
497 );
498
499 it.effect("stores the squash choice with an auto-completion, as a merge now does", () =>
500 Effect.gen(function* () {
501 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
502 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
503
504 yield* cli.runPullRequestAction({
505 cwd: "/w",
506 number: 42,
507 action: "enable-auto-merge",
508 mergeMethod: "squash",
509 });
510
511 expect(argsOfCall(0)).toEqual([
512 "repos",
513 "pr",
514 "update",
515 "--detect",
516 "true",
517 "--id",
518 "42",
519 "--auto-complete",
520 "true",
521 "--squash",
522 "true",
523 "--only-show-errors",
524 "--output",
525 "json",
526 ]);
527 }),
528 );
529
530 it.effect.each([
531 { action: "enable-auto-merge", expected: ["--auto-complete", "true"] },
532 { action: "disable-auto-merge", expected: ["--auto-complete", "false"] },
533 { action: "draft", expected: ["--draft", "true"] },
534 { action: "ready", expected: ["--draft", "false"] },
535 { action: "close", expected: ["--status", "abandoned"] },
536 { action: "reopen", expected: ["--status", "active"] },
537 ] as const)("moves a pull request with $action", ({ action, expected }) =>
538 Effect.gen(function* () {
539 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
540 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
541
542 yield* cli.runPullRequestAction({ cwd: "/w", number: 42, action });
543
544 expect(argsOfCall(0)).toEqual([
545 "repos",
546 "pr",
547 "update",
548 "--detect",
549 "true",
550 "--id",
551 "42",
552 ...expected,
553 "--only-show-errors",
554 "--output",
555 "json",
556 ]);
557 }),
558 );
559
560 it.effect.each([
561 { name: "a title", rewrite: { title: "Add the page" }, expected: ["--title=Add the page"] },
562 {
563 name: "a description",
564 rewrite: { body: "Why the page changed" },
565 expected: ["--description=Why the page changed"],
566 },
567 {
568 name: "both",
569 rewrite: { title: "Add the page", body: "Why the page changed" },
570 expected: ["--title=Add the page", "--description=Why the page changed"],
571 },
572 ] as const)("rewrites $name, sending nothing it was not given", ({ rewrite, expected }) =>
573 Effect.gen(function* () {
574 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
575 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
576
577 yield* cli.updatePullRequest({ cwd: "/w", number: 42, ...rewrite });
578
579 expect(argsOfCall(0)).toEqual([
580 "repos",
581 "pr",
582 "update",
583 "--detect",
584 "true",
585 "--id",
586 "42",
587 ...expected,
588 "--only-show-errors",
589 "--output",
590 "json",
591 ]);
592 }),
593 );
594
595 it.effect("sends a description that starts with a dash as one value, not as a flag", () =>
596 Effect.gen(function* () {
597 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
598 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
599
600 yield* cli.updatePullRequest({
601 cwd: "/w",
602 number: 42,
603 body: "- rewrote the page\n- kept the rest",
604 });
605
606 // One argument, so the leading dash of an ordinary bullet list never reaches az as a flag,
607 // and the whole text stays together where `--description` would otherwise take several.
608 expect(argsOfCall(0)).toContain("--description=- rewrote the page\n- kept the rest");
609 }),
610 );
611
612 it.effect("rewrites through the provider, which says it takes one", () =>
613 Effect.gen(function* () {
614 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
615 const provider = yield* AzureDevOpsPullRequestProvider.make;
616
617 // False for a remark because nothing here can post one, so there is none to rewrite.
618 expect(provider.capabilities.edit).toEqual({ changeRequest: true, comment: false });
619 assert.isDefined(provider.updateChangeRequest);
620 yield* provider.updateChangeRequest({
621 cwd: "/w",
622 repository: "web",
623 host: "dev.azure.com",
624 number: 42,
625 title: "Add the page",
626 });
627
628 expect(argsOfCall(0)).toContain("--title=Add the page");
629 expect(argsOfCall(0)).not.toContain("--description");
630 }),
631 );
632
633 it.effect("names the head's blob as what a cleared file was cleared at", () =>
634 Effect.gen(function* () {
635 mockedExecute
636 .mockReturnValueOnce(
637 Effect.succeed(
638 output(
639 // @effect-diagnostics-next-line preferSchemaOverJson:off
640 JSON.stringify({
641 pullRequestId: 42,
642 title: "Add the page",
643 status: "active",
644 sourceRefName: "refs/heads/feat/page",
645 targetRefName: "refs/heads/main",
646 creationDate: "2026-07-01T00:00:00Z",
647 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
648 repository: { name: "web", project: { name: "platform" } },
649 }),
650 ),
651 ),
652 )
653 .mockReturnValueOnce(
654 Effect.succeed(
655 output(
656 // @effect-diagnostics-next-line preferSchemaOverJson:off
657 JSON.stringify({
658 value: [
659 {
660 id: 1,
661 sourceRefCommit: { commitId: "a".repeat(40) },
662 commonRefCommit: { commitId: "b".repeat(40) },
663 },
664 {
665 id: 2,
666 sourceRefCommit: { commitId: "c".repeat(40) },
667 commonRefCommit: { commitId: "b".repeat(40) },
668 },
669 ],
670 }),
671 ),
672 ),
673 )
674 .mockReturnValueOnce(
675 Effect.succeed(
676 output(
677 // @effect-diagnostics-next-line preferSchemaOverJson:off
678 JSON.stringify({
679 changeEntries: [
680 { changeType: "edit", item: { path: "/README.md", objectId: "8f80" } },
681 { changeType: "add", item: { path: "/DEMO.md", objectId: "0ca4" } },
682 ],
683 }),
684 ),
685 ),
686 );
687 const provider = yield* AzureDevOpsPullRequestProvider.make;
688
689 // Kept here rather than on Azure: its own record of what a reader has read is behind an
690 // undocumented endpoint, so the marks belong to this environment and need a revision of
691 // their own to tell a re-push from a file still as it was read.
692 assert.strictEqual(provider.capabilities.viewedFiles, "environment");
693 assert.isDefined(provider.getFileRevisions);
694 const answer = yield* provider.getFileRevisions({
695 cwd: "/w",
696 repository: "web",
697 host: "dev.azure.com",
698 number: 42,
699 paths: ["README.md"],
700 });
701
702 // The latest push, since an iteration's changes are reported against the merge base rather
703 // than against the push before it.
704 expect(argsOfCall(2)).toContain("iterationId=2");
705 // Only what was asked for. DEMO.md changed too, and nobody has marked it.
706 expect([...answer.revisions]).toEqual([["README.md", "8f80"]]);
707 }),
708 );
709
710 it.effect("reads where a pull request lives once, however often it is asked about", () =>
711 Effect.gen(function* () {
712 // A pull request cannot move repositories, and the marks would otherwise pay for a whole
713 // pull request read every time they checked whether a file had been pushed to.
714 const pullRequest = Effect.succeed(
715 output(
716 // @effect-diagnostics-next-line preferSchemaOverJson:off
717 JSON.stringify({
718 pullRequestId: 42,
719 title: "Add the page",
720 status: "active",
721 sourceRefName: "refs/heads/feat/page",
722 targetRefName: "refs/heads/main",
723 creationDate: "2026-07-01T00:00:00Z",
724 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
725 repository: { name: "web", project: { name: "platform" } },
726 }),
727 ),
728 );
729 const iterations = () =>
730 Effect.succeed(
731 output(
732 JSON.stringify({
733 value: [
734 {
735 id: 1,
736 sourceRefCommit: { commitId: "a".repeat(40) },
737 commonRefCommit: { commitId: "b".repeat(40) },
738 },
739 ],
740 }),
741 ),
742 );
743 const changes = () =>
744 Effect.succeed(
745 output(
746 JSON.stringify({
747 changeEntries: [
748 { changeType: "edit", item: { path: "/README.md", objectId: "8f80" } },
749 ],
750 }),
751 ),
752 );
753 mockedExecute
754 .mockReturnValueOnce(pullRequest)
755 .mockReturnValueOnce(iterations())
756 .mockReturnValueOnce(changes())
757 .mockReturnValueOnce(iterations())
758 .mockReturnValueOnce(changes());
759 const provider = yield* AzureDevOpsPullRequestProvider.make;
760 const read = provider.getFileRevisions;
761 assert.isDefined(read);
762 const ask = () =>
763 read({
764 cwd: "/w",
765 repository: "web",
766 host: "dev.azure.com",
767 number: 42,
768 paths: ["README.md"],
769 });
770
771 yield* ask();
772 const again = yield* ask();
773
774 assert.strictEqual(mockedExecute.mock.calls.length, 5);
775 // The second read goes straight to the pushes, and still answers with the head's blob.
776 expect(argsOfCall(3)).toContain("pullRequestIterations");
777 expect([...again.revisions]).toEqual([["README.md", "8f80"]]);
778 }),
779 );
780
781 it.effect(
782 "answers for a marked file the pull request no longer changes as the empty version",
783 () =>
784 Effect.gen(function* () {
785 // Which is what was stored for it when it was ticked with nothing on the head, so a file
786 // the pull request deletes is cleared once and stays cleared.
787 mockedExecute
788 .mockReturnValueOnce(
789 Effect.succeed(
790 output(
791 json({
792 pullRequestId: 42,
793 title: "Add the page",
794 status: "active",
795 sourceRefName: "refs/heads/feat/page",
796 targetRefName: "refs/heads/main",
797 creationDate: "2026-07-01T00:00:00Z",
798 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
799 repository: { name: "web", project: { name: "platform" } },
800 }),
801 ),
802 ),
803 )
804 .mockReturnValueOnce(
805 Effect.succeed(
806 output(
807 json({
808 value: [
809 {
810 id: 1,
811 sourceRefCommit: { commitId: "a".repeat(40) },
812 commonRefCommit: { commitId: "b".repeat(40) },
813 },
814 ],
815 }),
816 ),
817 ),
818 )
819 .mockReturnValueOnce(Effect.succeed(output('{"changeEntries":[]}')));
820 const provider = yield* AzureDevOpsPullRequestProvider.make;
821 assert.isDefined(provider.getFileRevisions);
822
823 const answer = yield* provider.getFileRevisions({
824 cwd: "/w",
825 repository: "web",
826 host: "dev.azure.com",
827 number: 42,
828 paths: ["GONE.md"],
829 });
830
831 expect([...answer.revisions]).toEqual([["GONE.md", ""]]);
832 }),
833 );
834
835 it.effect("says nothing about the files past the end of a change it gave up following", () =>
836 Effect.gen(function* () {
837 // Every page is an `az` process of its own, so a change past the ceiling stops being
838 // followed. A path nobody looked at must not be answered for as deleted.
839 const entries = (from: number, count: number) =>
840 Array.from({ length: count }, (_, index) => ({
841 changeType: "edit",
842 item: { path: `/src/f${from + index}.ts`, objectId: `blob-${from + index}` },
843 }));
844 mockedExecute
845 .mockReturnValueOnce(
846 Effect.succeed(
847 output(
848 json({
849 pullRequestId: 42,
850 title: "Add the page",
851 status: "active",
852 sourceRefName: "refs/heads/feat/page",
853 targetRefName: "refs/heads/main",
854 creationDate: "2026-07-01T00:00:00Z",
855 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
856 repository: { name: "web", project: { name: "platform" } },
857 }),
858 ),
859 ),
860 )
861 .mockReturnValueOnce(
862 Effect.succeed(
863 output(
864 json({
865 value: [
866 {
867 id: 1,
868 sourceRefCommit: { commitId: "a".repeat(40) },
869 commonRefCommit: { commitId: "b".repeat(40) },
870 },
871 ],
872 }),
873 ),
874 ),
875 )
876 .mockReturnValueOnce(
877 Effect.succeed(output(json({ changeEntries: entries(0, 5_000), nextSkip: 5_000 }))),
878 )
879 .mockReturnValueOnce(
880 Effect.succeed(output(json({ changeEntries: entries(5_000, 5_000), nextSkip: 10_000 }))),
881 );
882 const provider = yield* AzureDevOpsPullRequestProvider.make;
883 assert.isDefined(provider.getFileRevisions);
884
885 const answer = yield* provider.getFileRevisions({
886 cwd: "/w",
887 repository: "web",
888 host: "dev.azure.com",
889 number: 42,
890 paths: ["src/f1.ts", "src/f5001.ts", "src/past-the-cut.ts"],
891 });
892
893 // The second page picks up where the first said it ended.
894 expect(argsOfCall(3)).toContain("$skip=5000");
895 expect([...answer.revisions]).toEqual([
896 ["src/f1.ts", "blob-1"],
897 ["src/f5001.ts", "blob-5001"],
898 ]);
899 }),
900 );
901
902 it.effect("stops following pages by what Azure counts, not by what survives decoding", () =>
903 Effect.gen(function* () {
904 mockedExecute
905 .mockReturnValueOnce(
906 Effect.succeed(
907 output(
908 json({
909 pullRequestId: 42,
910 title: "Add the page",
911 status: "active",
912 sourceRefName: "refs/heads/feat/page",
913 targetRefName: "refs/heads/main",
914 creationDate: "2026-07-01T00:00:00Z",
915 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
916 repository: { name: "web", project: { name: "platform" } },
917 }),
918 ),
919 ),
920 )
921 .mockReturnValueOnce(
922 Effect.succeed(
923 output(
924 json({
925 value: [
926 {
927 id: 1,
928 sourceRefCommit: { commitId: "a".repeat(40) },
929 commonRefCommit: { commitId: "b".repeat(40) },
930 },
931 ],
932 }),
933 ),
934 ),
935 )
936 // Nothing a review can show, so every page decodes to nothing at all and a ceiling counted
937 // in files would never be reached however long the walk went on.
938 .mockImplementation((command) => {
939 const skip = command.args.find((arg) => arg.startsWith("$skip="));
940 const from = Number(skip?.slice("$skip=".length) ?? 0);
941 return Effect.succeed(
942 output(
943 json({
944 changeEntries: [{ changeType: "add", item: { path: "/src", isFolder: true } }],
945 nextSkip: from + 2_000,
946 }),
947 ),
948 );
949 });
950 const provider = yield* AzureDevOpsPullRequestProvider.make;
951 assert.isDefined(provider.getFileRevisions);
952
953 const answer = yield* provider.getFileRevisions({
954 cwd: "/w",
955 repository: "web",
956 host: "dev.azure.com",
957 number: 42,
958 paths: ["src/page.ts"],
959 });
960
961 // The pull request, its pushes, and five pages: the walk gives up on Azure's own offset
962 // rather than spending an `az` process a page for as long as Azure keeps paging.
963 assert.strictEqual(mockedExecute.mock.calls.length, 7);
964 // And it read part of a change, so it says nothing about the file it never saw.
965 assert.strictEqual(answer.revisions.size, 0);
966 }),
967 );
968
969 it.effect("leaves one file the host would not hand over listed without its hunks", () =>
970 Effect.gen(function* () {
971 mockedExecute
972 .mockReturnValueOnce(Effect.succeed(output(json(pullRequestRow))))
973 .mockReturnValueOnce(Effect.succeed(output(json(oneIteration))))
974 .mockReturnValueOnce(
975 Effect.succeed(
976 output(
977 json({
978 changeEntries: [
979 { changeType: "add", item: { path: "/huge.bin", objectId: "8f80" } },
980 { changeType: "add", item: { path: "/DEMO.md", objectId: "0ca4" } },
981 ],
982 }),
983 ),
984 ),
985 )
986 .mockReturnValueOnce(
987 Effect.fail(
988 new AzureDevOpsCli.AzureDevOpsCommandFailedError({
989 operation: "execute",
990 command: "az",
991 cwd: "/w",
992 argumentCount: 1,
993 cause: "the blob is past what the route will carry",
994 }),
995 ),
996 )
997 .mockReturnValueOnce(Effect.succeed(output(json({ content: "hello\n" }))));
998 const provider = yield* AzureDevOpsPullRequestProvider.make;
999
1000 const slice = yield* provider.getDiff({
1001 cwd: "/w",
1002 repository: "web",
1003 host: "dev.azure.com",
1004 number: 42,
1005 });
1006
1007 assert.isTrue(slice.truncated);
1008 expect(slice.patch).toContain("diff --git a/huge.bin b/huge.bin");
1009 // And the file behind it still renders, which is the point of giving up on one file.
1010 expect(slice.patch).toContain("+hello");
1011 }),
1012 );
1013
1014 it.effect("fails the whole read when it is the connection that would not answer", () =>
1015 Effect.gen(function* () {
1016 // A rate limit is not this file's problem, and answering with a change full of files listed
1017 // without their hunks would read as a change nobody can see rather than as a host to wait
1018 // for.
1019 mockedExecute
1020 .mockReturnValueOnce(Effect.succeed(output(json(pullRequestRow))))
1021 .mockReturnValueOnce(Effect.succeed(output(json(oneIteration))))
1022 .mockReturnValueOnce(
1023 Effect.succeed(
1024 output(
1025 json({
1026 changeEntries: [
1027 { changeType: "add", item: { path: "/DEMO.md", objectId: "0ca4" } },
1028 ],
1029 }),
1030 ),
1031 ),
1032 )
1033 .mockReturnValueOnce(
1034 Effect.fail(
1035 new AzureDevOpsCli.AzureDevOpsCliRateLimitError({
1036 operation: "execute",
1037 command: "az",
1038 cwd: "/w",
1039 argumentCount: 1,
1040 cause: "429",
1041 }),
1042 ),
1043 );
1044 const provider = yield* AzureDevOpsPullRequestProvider.make;
1045
1046 const error = yield* Effect.flip(
1047 provider.getDiff({ cwd: "/w", repository: "web", host: "dev.azure.com", number: 42 }),
1048 );
1049
1050 assert.strictEqual(error.reason, "rate-limited");
1051 }),
1052 );
1053
1054 it.effect("takes Azure's own word on a file it will not spell out", () =>
1055 Effect.gen(function* () {
1056 mockedExecute
1057 .mockReturnValueOnce(
1058 Effect.succeed(
1059 output(
1060 json({
1061 pullRequestId: 42,
1062 title: "Add the page",
1063 status: "active",
1064 sourceRefName: "refs/heads/feat/page",
1065 targetRefName: "refs/heads/main",
1066 creationDate: "2026-07-01T00:00:00Z",
1067 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
1068 repository: { name: "web", project: { name: "platform" } },
1069 }),
1070 ),
1071 ),
1072 )
1073 .mockReturnValueOnce(
1074 Effect.succeed(
1075 output(
1076 json({
1077 value: [
1078 {
1079 id: 1,
1080 sourceRefCommit: { commitId: "a".repeat(40) },
1081 commonRefCommit: { commitId: "b".repeat(40) },
1082 },
1083 ],
1084 }),
1085 ),
1086 ),
1087 )
1088 .mockReturnValueOnce(
1089 Effect.succeed(
1090 output(
1091 json({
1092 changeEntries: [
1093 { changeType: "add", item: { path: "/logo.png", objectId: "8f80" } },
1094 ],
1095 }),
1096 ),
1097 ),
1098 )
1099 .mockReturnValueOnce(
1100 Effect.succeed(
1101 output(json({ content: "iVBORw0KGgo=", contentMetadata: { isBinary: true } })),
1102 ),
1103 );
1104 const provider = yield* AzureDevOpsPullRequestProvider.make;
1105
1106 const slice = yield* provider.getDiff({
1107 cwd: "/w",
1108 repository: "web",
1109 host: "dev.azure.com",
1110 number: 42,
1111 });
1112
1113 // Azure leaves the metadata out unless it is asked for, and without it every file reads as
1114 // text however it was stored.
1115 expect(argsOfCall(3)).toContain("includeContentMetadata=true");
1116 expect(slice.patch).toContain("Binary files a/logo.png and b/logo.png differ");
1117 assert.isTrue(slice.truncated);
1118 }),
1119 );
1120
1121 it.effect("stops following pages when one of them does not move the cursor on", () =>
1122 Effect.gen(function* () {
1123 mockedExecute
1124 .mockReturnValueOnce(
1125 Effect.succeed(
1126 output(
1127 json({
1128 pullRequestId: 42,
1129 title: "Add the page",
1130 status: "active",
1131 sourceRefName: "refs/heads/feat/page",
1132 targetRefName: "refs/heads/main",
1133 creationDate: "2026-07-01T00:00:00Z",
1134 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
1135 repository: { name: "web", project: { name: "platform" } },
1136 }),
1137 ),
1138 ),
1139 )
1140 .mockReturnValueOnce(
1141 Effect.succeed(
1142 output(
1143 json({
1144 value: [
1145 {
1146 id: 1,
1147 sourceRefCommit: { commitId: "a".repeat(40) },
1148 commonRefCommit: { commitId: "b".repeat(40) },
1149 },
1150 ],
1151 }),
1152 ),
1153 ),
1154 )
1155 .mockReturnValueOnce(
1156 Effect.succeed(
1157 output(
1158 json({
1159 changeEntries: [{ changeType: "edit", item: { path: "/a.ts", objectId: "8f80" } }],
1160 nextSkip: 2_000,
1161 }),
1162 ),
1163 ),
1164 )
1165 .mockReturnValueOnce(
1166 Effect.succeed(
1167 output(
1168 json({
1169 changeEntries: [{ changeType: "edit", item: { path: "/b.ts", objectId: "0ca4" } }],
1170 nextSkip: 2_000,
1171 }),
1172 ),
1173 ),
1174 );
1175 const provider = yield* AzureDevOpsPullRequestProvider.make;
1176 assert.isDefined(provider.getFileRevisions);
1177
1178 const answer = yield* provider.getFileRevisions({
1179 cwd: "/w",
1180 repository: "web",
1181 host: "dev.azure.com",
1182 number: 42,
1183 paths: ["a.ts", "b.ts", "unlisted.ts"],
1184 });
1185
1186 // Four reads and no more: a page pointing at where it already is would be read forever.
1187 assert.strictEqual(mockedExecute.mock.calls.length, 4);
1188 // And what was read is not the whole change, so the file nobody listed is left unanswered
1189 // rather than reported as gone from the change request.
1190 expect([...answer.revisions]).toEqual([
1191 ["a.ts", "8f80"],
1192 ["b.ts", "0ca4"],
1193 ]);
1194 }),
1195 );
1196
1197 it.effect("asks Azure nothing when no file has been ticked off", () =>
1198 Effect.gen(function* () {
1199 mockedExecute.mockReturnValue(Effect.succeed(output('{"changeEntries":[]}')));
1200 const provider = yield* AzureDevOpsPullRequestProvider.make;
1201 assert.isDefined(provider.getFileRevisions);
1202
1203 const answer = yield* provider.getFileRevisions({
1204 cwd: "/w",
1205 repository: "web",
1206 host: "dev.azure.com",
1207 number: 42,
1208 paths: [],
1209 });
1210
1211 expect(answer.revisions.size).toBe(0);
1212 // Nothing was marked, so Azure was not asked at all.
1213 assert.strictEqual(mockedExecute.mock.calls.length, 0);
1214 }),
1215 );
1216
1217 it.effect("reads the conversation through the REST API, pinned to a version", () =>
1218 Effect.gen(function* () {
1219 mockedExecute.mockReturnValueOnce(
1220 Effect.succeed(
1221 output(
1222 // @effect-diagnostics-next-line preferSchemaOverJson:off
1223 JSON.stringify({
1224 value: [
1225 {
1226 id: 1,
1227 comments: [
1228 { id: 1, content: "Looks good.", publishedDate: "2026-07-02T00:00:00Z" },
1229 ],
1230 },
1231 ],
1232 }),
1233 ),
1234 ),
1235 );
1236 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1237
1238 const comments = yield* cli.listThreads({
1239 cwd: "/w",
1240 location: { project: "platform", repository: "web" },
1241 number: 42,
1242 });
1243
1244 assert.strictEqual(comments.length, 1);
1245 // `az devops invoke` rather than `az rest`: it signs in the way the azure-devops extension
1246 // does, and `az rest` mints its own token against whichever tenant `az` defaults to.
1247 expect(argsOfCall(0)).toContain("invoke");
1248 expect(argsOfCall(0)).toContain("pullRequestThreads");
1249 expect(argsOfCall(0)).toContain("project=platform");
1250 expect(argsOfCall(0)).toContain("repositoryId=web");
1251 expect(argsOfCall(0)).toContain("pullRequestId=42");
1252 // A review's threads grow with how long it ran, and this route does not page, so the read
1253 // asks for more than the process default rather than taking whatever it is given.
1254 expect(maxOutputBytesOfCall(0)).toBeGreaterThan(VCS_DEFAULT_MAX_OUTPUT_BYTES);
1255 }),
1256 );
1257
1258 it.effect("reads a long review's threads, which are past the default output limit", () =>
1259 Effect.gen(function* () {
1260 const response = json({ value: threadRows(800) });
1261 // Azure opens a thread per vote and per ref update beside the ones people wrote, and every
1262 // comment carries a full identity, so a review argued over for weeks outgrows the default.
1263 expect(Buffer.byteLength(response)).toBeGreaterThan(VCS_DEFAULT_MAX_OUTPUT_BYTES);
1264 mockedExecute.mockImplementationOnce((input) =>
1265 Effect.succeed(outputWithin(input.maxOutputBytes, response)),
1266 );
1267 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1268
1269 const comments = yield* cli.listThreads({
1270 cwd: "/w",
1271 location: { project: "platform", repository: "web" },
1272 number: 42,
1273 });
1274
1275 assert.strictEqual(comments.length, 800);
1276 }),
1277 );
1278
1279 it.effect("reads a long review's iterations, which are past the default output limit", () =>
1280 Effect.gen(function* () {
1281 const response = json({ value: iterationRows(1_200) });
1282 // This route does not page, so the whole history arrives at once. Cut at the default it is
1283 // JSON stopping mid-string, and every diff and file revision read on this host fails with
1284 // it, since each of them starts by asking which iteration is the latest.
1285 expect(Buffer.byteLength(response)).toBeGreaterThan(VCS_DEFAULT_MAX_OUTPUT_BYTES);
1286 mockedExecute.mockImplementationOnce((input) =>
1287 Effect.succeed(outputWithin(input.maxOutputBytes, response)),
1288 );
1289 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1290
1291 const iterations = yield* cli.listIterations({
1292 cwd: "/w",
1293 location: { project: "platform", repository: "web" },
1294 number: 42,
1295 });
1296
1297 assert.strictEqual(iterations.length, 1_200);
1298 assert.strictEqual(iterations.at(-1)?.id, 1_200);
1299 }),
1300 );
1301
1302 it.effect("reads a full page of change entries, which is past the default output limit", () =>
1303 Effect.gen(function* () {
1304 const response = json({ changeEntries: changeEntries(2_000) });
1305 // Azure's own maximum for this route, and every entry carries a path, a url and three
1306 // object ids, so an ordinary page of a large change already outgrows the default.
1307 expect(Buffer.byteLength(response)).toBeGreaterThan(VCS_DEFAULT_MAX_OUTPUT_BYTES);
1308 mockedExecute.mockImplementationOnce((input) =>
1309 Effect.succeed(outputWithin(input.maxOutputBytes, response)),
1310 );
1311 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1312
1313 const page = yield* cli.listIterationChanges({
1314 cwd: "/w",
1315 location: { project: "platform", repository: "web" },
1316 number: 42,
1317 iterationId: 1,
1318 });
1319
1320 // Cut at the default this would arrive as JSON stopping mid-string, and a perfectly
1321 // ordinary page would be reported as a host answering with nonsense.
1322 assert.strictEqual(page.changes.length, 2_000);
1323 assert.isFalse(page.truncated);
1324 }),
1325 );
1326
1327 it.effect("reads a file whose JSON envelope is past the default output limit", () =>
1328 Effect.gen(function* () {
1329 // Under a megabyte as bytes on the host, so this is a file the other hosts hand over.
1330 const file = "const value = 1;\n".repeat(57_000);
1331 const response = json({ content: file });
1332 expect(Buffer.byteLength(file)).toBeLessThan(VCS_DEFAULT_MAX_OUTPUT_BYTES);
1333 // And past it once Azure wraps it, because there is no route here that serves the bytes.
1334 expect(Buffer.byteLength(response)).toBeGreaterThan(VCS_DEFAULT_MAX_OUTPUT_BYTES);
1335 mockedExecute.mockImplementationOnce((input) =>
1336 Effect.succeed(outputWithin(input.maxOutputBytes, response)),
1337 );
1338 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1339
1340 const item = yield* cli.readItemContent({
1341 cwd: "/w",
1342 location: { project: "platform", repository: "web" },
1343 path: "src/generated/schema.ts",
1344 commit: "a".repeat(40),
1345 });
1346
1347 assert.strictEqual(item.contents, file);
1348 assert.isFalse(item.isBinary);
1349 }),
1350 );
1351
1352 it.effect("asks for a file by Azure's own spelling of its path", () =>
1353 Effect.gen(function* () {
1354 mockedExecute.mockReturnValueOnce(Effect.succeed(output(json({ content: "const a = 1;" }))));
1355 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1356
1357 // The path carried around here has had Azure's leading slash taken off so it matches the
1358 // patch and the viewed mark. The items route is documented in Azure's spelling, so it goes
1359 // back on the way out rather than being sent as the shorter name.
1360 yield* cli.readItemContent({
1361 cwd: "/w",
1362 location: { project: "platform", repository: "web" },
1363 path: "src/app.ts",
1364 commit: "a".repeat(40),
1365 });
1366
1367 expect(argsOfCall(0)).toContain("path=/src/app.ts");
1368 }),
1369 );
1370
1371 it.effect("reports a pull request it cannot place as its own outcome", () =>
1372 Effect.gen(function* () {
1373 mockedExecute.mockReturnValueOnce(
1374 Effect.succeed(
1375 output(
1376 // Well-formed, but with nothing to build a link from: not a decode failure.
1377 // @effect-diagnostics-next-line preferSchemaOverJson:off
1378 JSON.stringify({
1379 pullRequestId: 42,
1380 title: "Add the page",
1381 sourceRefName: "refs/heads/feat/page",
1382 targetRefName: "refs/heads/main",
1383 creationDate: "2026-07-01T00:00:00Z",
1384 }),
1385 ),
1386 ),
1387 );
1388 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1389
1390 const error = yield* Effect.flip(cli.getPullRequest({ cwd: "/w", number: 42 }));
1391
1392 assert.strictEqual(error._tag, "AzureDevOpsPullRequestIncompleteError");
1393 }),
1394 );
1395
1396 it.effect("fails the read when az returns something unreadable", () =>
1397 Effect.gen(function* () {
1398 mockedExecute.mockReturnValueOnce(Effect.succeed(output('{"message":"not found"}')));
1399 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1400
1401 const error = yield* Effect.flip(cli.getPullRequest({ cwd: "/w", number: 42 }));
1402
1403 assert.strictEqual(error._tag, "AzureDevOpsPullRequestReadError");
1404 }),
1405 );
1406
1407 it.effect("adds reviewers with the one command Azure has for it", () =>
1408 Effect.gen(function* () {
1409 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
1410 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1411
1412 yield* cli.setPullRequestReviewers({
1413 cwd: "/w",
1414 number: 42,
1415 reviewers: ["octocat@acme.test", "hubot@acme.test"],
1416 requested: true,
1417 });
1418
1419 expect(argsOfCall(0)).toEqual([
1420 "repos",
1421 "pr",
1422 "reviewer",
1423 "add",
1424 "--detect",
1425 "true",
1426 "--id",
1427 "42",
1428 "--reviewers",
1429 "octocat@acme.test",
1430 "hubot@acme.test",
1431 "--only-show-errors",
1432 "--output",
1433 "json",
1434 ]);
1435 }),
1436 );
1437
1438 it.effect("takes a reviewer off the pull request with the same command's counterpart", () =>
1439 Effect.gen(function* () {
1440 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
1441 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1442
1443 yield* cli.setPullRequestReviewers({
1444 cwd: "/w",
1445 number: 42,
1446 reviewers: ["octocat@acme.test"],
1447 requested: false,
1448 });
1449
1450 expect(argsOfCall(0)).toContain("remove");
1451 }),
1452 );
1453
1454 it.effect("refuses a reviewer az would read as a flag, before running anything", () =>
1455 Effect.gen(function* () {
1456 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
1457
1458 const error = yield* Effect.flip(
1459 cli.setPullRequestReviewers({
1460 cwd: "/w",
1461 number: 42,
1462 reviewers: ["--query"],
1463 requested: true,
1464 }),
1465 );
1466
1467 assert.strictEqual(error._tag, "AzureDevOpsReviewerNameError");
1468 assert.strictEqual(mockedExecute.mock.calls.length, 0);
1469 }),
1470 );
1471});