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/process";
5
6import * as AzureDevOpsCli from "./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
32const VCS_DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
33
34function outputWithin(maxOutputBytes: number | undefined, response: string) {
35 const ceiling = maxOutputBytes ?? VCS_DEFAULT_MAX_OUTPUT_BYTES;
36 return ceiling >= Buffer.byteLength(response)
37 ? output(response)
38 : { ...output(response.slice(0, ceiling)), stdoutTruncated: true };
39}
40
41const json = (value: Record<string, unknown>) => JSON.stringify(value);
42
43const pullRequestRow = {
44 pullRequestId: 42,
45 title: "Add the page",
46 status: "active",
47 sourceRefName: "refs/heads/feat/page",
48 targetRefName: "refs/heads/main",
49 creationDate: "2026-07-01T00:00:00Z",
50 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
51 repository: { name: "web", project: { name: "platform" } },
52};
53
54const oneIteration = {
55 value: [
56 {
57 id: 1,
58 sourceRefCommit: { commitId: "a".repeat(40) },
59 commonRefCommit: { commitId: "b".repeat(40) },
60 },
61 ],
62};
63
64function pullRequestRows(
65 count: number,
66 firstNumber: number,
67): ReadonlyArray<Record<string, unknown>> {
68 return Array.from({ length: count }, (_, index) => ({
69 pullRequestId: firstNumber + index,
70 title: `Pull request ${firstNumber + index}`,
71 status: "active",
72 sourceRefName: "refs/heads/feat/page",
73 targetRefName: "refs/heads/main",
74 creationDate: "2026-07-01T00:00:00Z",
75 repository: { name: "web", project: { name: "platform" } },
76 url: `https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/${firstNumber + index}`,
77 }));
78}
79
80function pullRequests(count: number, firstNumber: number): string {
81 return JSON.stringify(pullRequestRows(count, firstNumber));
82}
83
84function argsOfCall(index: number): ReadonlyArray<string> {
85 const call = mockedExecute.mock.calls[index];
86 assert.isDefined(call);
87 return call[0].args;
88}
89
90function changeEntries(count: number): ReadonlyArray<Record<string, unknown>> {
91 const commit = "c".repeat(40);
92 return Array.from({ length: count }, (_, index) => {
93 const path = `/apps/server/src/generated/module-${index}/persisted-projection-${index}.ts`;
94 return {
95 changeType: "edit",
96 item: {
97 path,
98 objectId: "a".repeat(40),
99 originalObjectId: "b".repeat(40),
100 commitId: commit,
101 gitObjectType: "blob",
102 url: `https://dev.azure.com/acme/platform/_apis/git/repositories/6f9c9b7f-0000-0000-0000-000000000000/items${path}?versionType=Commit&version=${commit}`,
103 },
104 };
105 });
106}
107
108afterEach(() => {
109 mockedExecute.mockReset();
110});
111
112layer("AzureDevOpsPullRequestCli.layer", (it) => {
113 it.effect("reports the latest pushed source commit even while the trial merge is stale", () =>
114 Effect.gen(function* () {
115 const previousHead = "a".repeat(40);
116 const latestHead = "c".repeat(40);
117 mockedExecute
118 .mockReturnValueOnce(
119 Effect.succeed(
120 output(json({ ...pullRequestRow, lastMergeSourceCommit: { commitId: previousHead } })),
121 ),
122 )
123 .mockReturnValueOnce(
124 Effect.succeed(
125 output(
126 json({
127 value: [
128 {
129 id: 2,
130 sourceRefCommit: { commitId: latestHead },
131 commonRefCommit: { commitId: "b".repeat(40) },
132 },
133 ...oneIteration.value,
134 ],
135 }),
136 ),
137 ),
138 )
139 .mockReturnValueOnce(Effect.succeed(output(json({ changeEntries: [] }))));
140 const provider = yield* AzureDevOpsPullRequestProvider.make;
141 const detail = yield* provider.getChangeRequest({
142 cwd: "/w",
143 repository: "platform/web",
144 host: "dev.azure.com",
145 number: 42,
146 });
147
148 expect(detail.headSha).toBe(latestHead);
149 expect(argsOfCall(2)).toContain("iterationId=2");
150 expect(mockedExecute).toHaveBeenCalledTimes(3);
151 }),
152 );
153
154 it.effect("reads an Azure pull request page larger than the VCS default output limit", () =>
155 Effect.gen(function* () {
156 const rows = pullRequestRows(100, 1).map((row) => ({
157 ...row,
158 description: "x".repeat(10_000),
159 }));
160 const response = JSON.stringify(rows);
161 expect(Buffer.byteLength(response)).toBeGreaterThan(1_000_000);
162
163 mockedExecute.mockImplementationOnce((input) =>
164 Effect.succeed(outputWithin(input.maxOutputBytes, response)),
165 );
166 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
167
168 const batch = yield* cli.listPullRequests({
169 cwd: "/w",
170 repository: "web",
171 state: "merged",
172 involvement: "all",
173 viewer: "bilal@acme.dev",
174 limit: 99,
175 });
176
177 assert.strictEqual(batch.items.length, 99);
178 assert.isTrue(batch.truncated);
179 }),
180 );
181
182 it.effect("steps over what it has already handed over, which is all Azure can be told", () =>
183 Effect.gen(function* () {
184 mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(3, 1))));
185 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
186
187 yield* cli.listPullRequests({
188 cwd: "/w",
189 repository: "web",
190 state: "open",
191 involvement: "all",
192 viewer: "bilal@acme.dev",
193 limit: 10,
194 cursor: { updatedBefore: "2026-07-02T00:00:00Z", delivered: 20 },
195 });
196
197 const args = argsOfCall(0);
198 expect(args).toContain("--skip");
199 assert.strictEqual(args[args.indexOf("--skip") + 1], "20");
200 expect(args).not.toContain("2026-07-02T00:00:00Z");
201 }),
202 );
203
204 it.effect("advances by malformed raw rows and keeps reading until the page is full", () =>
205 Effect.gen(function* () {
206 mockedExecute
207 .mockReturnValueOnce(
208 Effect.succeed(
209 output(
210 JSON.stringify([
211 { pullRequestId: "malformed" },
212 pullRequestRows(1, 1)[0],
213 { pullRequestId: "also malformed" },
214 ]),
215 ),
216 ),
217 )
218 .mockReturnValueOnce(Effect.succeed(output(pullRequests(2, 2))));
219 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
220
221 const batch = yield* cli.listPullRequests({
222 cwd: "/w",
223 repository: "web",
224 state: "open",
225 involvement: "all",
226 viewer: "bilal@acme.dev",
227 limit: 2,
228 });
229
230 expect(batch.items.map((item) => item.number)).toEqual([1, 2]);
231 assert.isTrue(batch.truncated);
232 assert.strictEqual(batch.cursorAdvance, 4);
233 const secondArgs = argsOfCall(1);
234 assert.strictEqual(secondArgs[secondArgs.indexOf("--skip") + 1], "3");
235 assert.strictEqual(secondArgs[secondArgs.indexOf("--top") + 1], "2");
236 }),
237 );
238
239 it.effect("reads the signed-in account, which az reports as a bare value", () =>
240 Effect.gen(function* () {
241 mockedExecute.mockReturnValueOnce(
242 Effect.succeed(output(JSON.stringify({ name: "bilal@acme.dev", type: "user" }))),
243 );
244 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
245
246 const viewer = yield* cli.getViewer({ cwd: "/w" });
247
248 assert.strictEqual(viewer, "bilal@acme.dev");
249 expect(argsOfCall(0)).toEqual([
250 "account",
251 "show",
252 "--query",
253 "user",
254 "--only-show-errors",
255 "--output",
256 "json",
257 ]);
258 }),
259 );
260
261 it.effect("names a token sign-in by the viewer's own pull request", () =>
262 Effect.gen(function* () {
263 const notLoggedIn = new AzureDevOpsCli.AzureDevOpsCliAuthenticationError({
264 operation: "execute",
265 command: "az",
266 cwd: "/w",
267 argumentCount: 7,
268 cause: new Error("Please run 'az login' to setup account."),
269 });
270 mockedExecute
271 .mockReturnValueOnce(Effect.fail(notLoggedIn))
272 .mockReturnValueOnce(
273 Effect.succeed(
274 output(
275 JSON.stringify([{ ...pullRequestRow, createdBy: { uniqueName: "bilal@acme.dev" } }]),
276 ),
277 ),
278 )
279 .mockReturnValueOnce(Effect.succeed(output("")))
280 .mockReturnValueOnce(Effect.succeed(output("[]")));
281 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
282
283 assert.strictEqual(yield* cli.getViewer({ cwd: "/w" }), "bilal@acme.dev");
284 expect(argsOfCall(1)).toEqual(expect.arrayContaining(["--creator", "me", "--top", "1"]));
285 const error = yield* Effect.flip(cli.getViewer({ cwd: "/w" }));
286 assert.strictEqual(error._tag, "AzureDevOpsViewerUnavailableError");
287 }),
288 );
289
290 it.effect("fails when no sign-in can read pull requests", () =>
291 Effect.gen(function* () {
292 const signedOut = new AzureDevOpsCli.AzureDevOpsCliAuthenticationError({
293 operation: "execute",
294 command: "az",
295 cwd: "/w",
296 argumentCount: 7,
297 cause: new Error("signed out"),
298 });
299 mockedExecute.mockReturnValue(Effect.fail(signedOut));
300 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
301
302 const error = yield* Effect.flip(cli.getViewer({ cwd: "/w" }));
303
304 assert.strictEqual(error._tag, "AzureDevOpsCliAuthenticationError");
305 }),
306 );
307
308 it.effect("sends a description that starts with a dash as one value, not as a flag", () =>
309 Effect.gen(function* () {
310 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
311 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
312
313 yield* cli.updatePullRequest({
314 cwd: "/w",
315 number: 42,
316 body: "- rewrote the page\n- kept the rest",
317 });
318
319 expect(argsOfCall(0)).toContain("--description=- rewrote the page\n- kept the rest");
320 }),
321 );
322
323 it.effect("names the head's blob as what a cleared file was cleared at", () =>
324 Effect.gen(function* () {
325 mockedExecute
326 .mockReturnValueOnce(
327 Effect.succeed(
328 output(
329 JSON.stringify({
330 pullRequestId: 42,
331 title: "Add the page",
332 status: "active",
333 sourceRefName: "refs/heads/feat/page",
334 targetRefName: "refs/heads/main",
335 creationDate: "2026-07-01T00:00:00Z",
336 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
337 repository: { name: "web", project: { name: "platform" } },
338 }),
339 ),
340 ),
341 )
342 .mockReturnValueOnce(
343 Effect.succeed(
344 output(
345 JSON.stringify({
346 value: [
347 {
348 id: 1,
349 sourceRefCommit: { commitId: "a".repeat(40) },
350 commonRefCommit: { commitId: "b".repeat(40) },
351 },
352 {
353 id: 2,
354 sourceRefCommit: { commitId: "c".repeat(40) },
355 commonRefCommit: { commitId: "b".repeat(40) },
356 },
357 ],
358 }),
359 ),
360 ),
361 )
362 .mockReturnValueOnce(
363 Effect.succeed(
364 output(
365 JSON.stringify({
366 changeEntries: [
367 { changeType: "edit", item: { path: "/README.md", objectId: "8f80" } },
368 { changeType: "add", item: { path: "/DEMO.md", objectId: "0ca4" } },
369 ],
370 }),
371 ),
372 ),
373 );
374 const provider = yield* AzureDevOpsPullRequestProvider.make;
375
376 assert.strictEqual(provider.capabilities.viewedFiles, "environment");
377 assert.isDefined(provider.getFileRevisions);
378 const answer = yield* provider.getFileRevisions({
379 cwd: "/w",
380 repository: "web",
381 host: "dev.azure.com",
382 number: 42,
383 paths: ["README.md"],
384 });
385
386 expect(argsOfCall(2)).toContain("iterationId=2");
387 expect([...answer.revisions]).toEqual([["README.md", "8f80"]]);
388 }),
389 );
390
391 it.effect(
392 "answers for a marked file the pull request no longer changes as the empty version",
393 () =>
394 Effect.gen(function* () {
395 mockedExecute
396 .mockReturnValueOnce(
397 Effect.succeed(
398 output(
399 json({
400 pullRequestId: 42,
401 title: "Add the page",
402 status: "active",
403 sourceRefName: "refs/heads/feat/page",
404 targetRefName: "refs/heads/main",
405 creationDate: "2026-07-01T00:00:00Z",
406 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
407 repository: { name: "web", project: { name: "platform" } },
408 }),
409 ),
410 ),
411 )
412 .mockReturnValueOnce(
413 Effect.succeed(
414 output(
415 json({
416 value: [
417 {
418 id: 1,
419 sourceRefCommit: { commitId: "a".repeat(40) },
420 commonRefCommit: { commitId: "b".repeat(40) },
421 },
422 ],
423 }),
424 ),
425 ),
426 )
427 .mockReturnValueOnce(Effect.succeed(output('{"changeEntries":[]}')));
428 const provider = yield* AzureDevOpsPullRequestProvider.make;
429 assert.isDefined(provider.getFileRevisions);
430
431 const answer = yield* provider.getFileRevisions({
432 cwd: "/w",
433 repository: "web",
434 host: "dev.azure.com",
435 number: 42,
436 paths: ["GONE.md"],
437 });
438
439 expect([...answer.revisions]).toEqual([["GONE.md", ""]]);
440 }),
441 );
442
443 it.effect("says nothing about the files past the end of a change it gave up following", () =>
444 Effect.gen(function* () {
445 const entries = (from: number, count: number) =>
446 Array.from({ length: count }, (_, index) => ({
447 changeType: "edit",
448 item: { path: `/src/f${from + index}.ts`, objectId: `blob-${from + index}` },
449 }));
450 mockedExecute
451 .mockReturnValueOnce(
452 Effect.succeed(
453 output(
454 json({
455 pullRequestId: 42,
456 title: "Add the page",
457 status: "active",
458 sourceRefName: "refs/heads/feat/page",
459 targetRefName: "refs/heads/main",
460 creationDate: "2026-07-01T00:00:00Z",
461 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
462 repository: { name: "web", project: { name: "platform" } },
463 }),
464 ),
465 ),
466 )
467 .mockReturnValueOnce(
468 Effect.succeed(
469 output(
470 json({
471 value: [
472 {
473 id: 1,
474 sourceRefCommit: { commitId: "a".repeat(40) },
475 commonRefCommit: { commitId: "b".repeat(40) },
476 },
477 ],
478 }),
479 ),
480 ),
481 )
482 .mockReturnValueOnce(
483 Effect.succeed(output(json({ changeEntries: entries(0, 5_000), nextSkip: 5_000 }))),
484 )
485 .mockReturnValueOnce(
486 Effect.succeed(output(json({ changeEntries: entries(5_000, 5_000), nextSkip: 10_000 }))),
487 );
488 const provider = yield* AzureDevOpsPullRequestProvider.make;
489 assert.isDefined(provider.getFileRevisions);
490
491 const answer = yield* provider.getFileRevisions({
492 cwd: "/w",
493 repository: "web",
494 host: "dev.azure.com",
495 number: 42,
496 paths: ["src/f1.ts", "src/f5001.ts", "src/past-the-cut.ts"],
497 });
498
499 expect(argsOfCall(3)).toContain("$skip=5000");
500 expect([...answer.revisions]).toEqual([
501 ["src/f1.ts", "blob-1"],
502 ["src/f5001.ts", "blob-5001"],
503 ]);
504 }),
505 );
506
507 it.effect("stops following pages by what Azure counts, not by what survives decoding", () =>
508 Effect.gen(function* () {
509 mockedExecute
510 .mockReturnValueOnce(
511 Effect.succeed(
512 output(
513 json({
514 pullRequestId: 42,
515 title: "Add the page",
516 status: "active",
517 sourceRefName: "refs/heads/feat/page",
518 targetRefName: "refs/heads/main",
519 creationDate: "2026-07-01T00:00:00Z",
520 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
521 repository: { name: "web", project: { name: "platform" } },
522 }),
523 ),
524 ),
525 )
526 .mockReturnValueOnce(
527 Effect.succeed(
528 output(
529 json({
530 value: [
531 {
532 id: 1,
533 sourceRefCommit: { commitId: "a".repeat(40) },
534 commonRefCommit: { commitId: "b".repeat(40) },
535 },
536 ],
537 }),
538 ),
539 ),
540 )
541 .mockImplementation((command) => {
542 const skip = command.args.find((arg) => arg.startsWith("$skip="));
543 const from = Number(skip?.slice("$skip=".length) ?? 0);
544 return Effect.succeed(
545 output(
546 json({
547 changeEntries: [{ changeType: "add", item: { path: "/src", isFolder: true } }],
548 nextSkip: from + 2_000,
549 }),
550 ),
551 );
552 });
553 const provider = yield* AzureDevOpsPullRequestProvider.make;
554 assert.isDefined(provider.getFileRevisions);
555
556 const answer = yield* provider.getFileRevisions({
557 cwd: "/w",
558 repository: "web",
559 host: "dev.azure.com",
560 number: 42,
561 paths: ["src/page.ts"],
562 });
563
564 assert.strictEqual(mockedExecute.mock.calls.length, 7);
565 assert.strictEqual(answer.revisions.size, 0);
566 }),
567 );
568
569 it.effect("leaves one file the host would not hand over listed without its hunks", () =>
570 Effect.gen(function* () {
571 mockedExecute
572 .mockReturnValueOnce(Effect.succeed(output(json(pullRequestRow))))
573 .mockReturnValueOnce(Effect.succeed(output(json(oneIteration))))
574 .mockReturnValueOnce(
575 Effect.succeed(
576 output(
577 json({
578 changeEntries: [
579 { changeType: "add", item: { path: "/huge.bin", objectId: "8f80" } },
580 { changeType: "add", item: { path: "/DEMO.md", objectId: "0ca4" } },
581 ],
582 }),
583 ),
584 ),
585 )
586 .mockReturnValueOnce(
587 Effect.fail(
588 new AzureDevOpsCli.AzureDevOpsCommandFailedError({
589 operation: "execute",
590 command: "az",
591 cwd: "/w",
592 argumentCount: 1,
593 cause: "the blob is past what the route will carry",
594 }),
595 ),
596 )
597 .mockReturnValueOnce(Effect.succeed(output(json({ content: "hello\n" }))));
598 const provider = yield* AzureDevOpsPullRequestProvider.make;
599
600 const slice = yield* provider.getDiff({
601 cwd: "/w",
602 repository: "web",
603 host: "dev.azure.com",
604 number: 42,
605 });
606
607 assert.isTrue(slice.truncated);
608 expect(slice.patch).toContain("diff --git a/huge.bin b/huge.bin");
609 expect(slice.patch).toContain("+hello");
610 }),
611 );
612
613 it.effect("fails the whole read when it is the connection that would not answer", () =>
614 Effect.gen(function* () {
615 mockedExecute
616 .mockReturnValueOnce(Effect.succeed(output(json(pullRequestRow))))
617 .mockReturnValueOnce(Effect.succeed(output(json(oneIteration))))
618 .mockReturnValueOnce(
619 Effect.succeed(
620 output(
621 json({
622 changeEntries: [
623 { changeType: "add", item: { path: "/DEMO.md", objectId: "0ca4" } },
624 ],
625 }),
626 ),
627 ),
628 )
629 .mockReturnValueOnce(
630 Effect.fail(
631 new AzureDevOpsCli.AzureDevOpsCliRateLimitError({
632 operation: "execute",
633 command: "az",
634 cwd: "/w",
635 argumentCount: 1,
636 cause: "429",
637 }),
638 ),
639 );
640 const provider = yield* AzureDevOpsPullRequestProvider.make;
641
642 const error = yield* Effect.flip(
643 provider.getDiff({ cwd: "/w", repository: "web", host: "dev.azure.com", number: 42 }),
644 );
645
646 assert.strictEqual(error.reason, "rate-limited");
647 }),
648 );
649
650 it.effect("takes Azure's own word on a file it will not spell out", () =>
651 Effect.gen(function* () {
652 mockedExecute
653 .mockReturnValueOnce(
654 Effect.succeed(
655 output(
656 json({
657 pullRequestId: 42,
658 title: "Add the page",
659 status: "active",
660 sourceRefName: "refs/heads/feat/page",
661 targetRefName: "refs/heads/main",
662 creationDate: "2026-07-01T00:00:00Z",
663 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
664 repository: { name: "web", project: { name: "platform" } },
665 }),
666 ),
667 ),
668 )
669 .mockReturnValueOnce(
670 Effect.succeed(
671 output(
672 json({
673 value: [
674 {
675 id: 1,
676 sourceRefCommit: { commitId: "a".repeat(40) },
677 commonRefCommit: { commitId: "b".repeat(40) },
678 },
679 ],
680 }),
681 ),
682 ),
683 )
684 .mockReturnValueOnce(
685 Effect.succeed(
686 output(
687 json({
688 changeEntries: [
689 { changeType: "add", item: { path: "/logo.png", objectId: "8f80" } },
690 ],
691 }),
692 ),
693 ),
694 )
695 .mockReturnValueOnce(
696 Effect.succeed(
697 output(json({ content: "iVBORw0KGgo=", contentMetadata: { isBinary: true } })),
698 ),
699 );
700 const provider = yield* AzureDevOpsPullRequestProvider.make;
701
702 const slice = yield* provider.getDiff({
703 cwd: "/w",
704 repository: "web",
705 host: "dev.azure.com",
706 number: 42,
707 });
708
709 expect(argsOfCall(3)).toContain("includeContentMetadata=true");
710 expect(slice.patch).toContain("Binary files a/logo.png and b/logo.png differ");
711 assert.isTrue(slice.truncated);
712 }),
713 );
714
715 it.effect("stops following pages when one of them does not move the cursor on", () =>
716 Effect.gen(function* () {
717 mockedExecute
718 .mockReturnValueOnce(
719 Effect.succeed(
720 output(
721 json({
722 pullRequestId: 42,
723 title: "Add the page",
724 status: "active",
725 sourceRefName: "refs/heads/feat/page",
726 targetRefName: "refs/heads/main",
727 creationDate: "2026-07-01T00:00:00Z",
728 url: "https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/42",
729 repository: { name: "web", project: { name: "platform" } },
730 }),
731 ),
732 ),
733 )
734 .mockReturnValueOnce(
735 Effect.succeed(
736 output(
737 json({
738 value: [
739 {
740 id: 1,
741 sourceRefCommit: { commitId: "a".repeat(40) },
742 commonRefCommit: { commitId: "b".repeat(40) },
743 },
744 ],
745 }),
746 ),
747 ),
748 )
749 .mockReturnValueOnce(
750 Effect.succeed(
751 output(
752 json({
753 changeEntries: [{ changeType: "edit", item: { path: "/a.ts", objectId: "8f80" } }],
754 nextSkip: 2_000,
755 }),
756 ),
757 ),
758 )
759 .mockReturnValueOnce(
760 Effect.succeed(
761 output(
762 json({
763 changeEntries: [{ changeType: "edit", item: { path: "/b.ts", objectId: "0ca4" } }],
764 nextSkip: 2_000,
765 }),
766 ),
767 ),
768 );
769 const provider = yield* AzureDevOpsPullRequestProvider.make;
770 assert.isDefined(provider.getFileRevisions);
771
772 const answer = yield* provider.getFileRevisions({
773 cwd: "/w",
774 repository: "web",
775 host: "dev.azure.com",
776 number: 42,
777 paths: ["a.ts", "b.ts", "unlisted.ts"],
778 });
779
780 assert.strictEqual(mockedExecute.mock.calls.length, 4);
781 expect([...answer.revisions]).toEqual([
782 ["a.ts", "8f80"],
783 ["b.ts", "0ca4"],
784 ]);
785 }),
786 );
787
788 it.effect("reads a full page of change entries, which is past the default output limit", () =>
789 Effect.gen(function* () {
790 const response = json({ changeEntries: changeEntries(2_000) });
791 expect(Buffer.byteLength(response)).toBeGreaterThan(VCS_DEFAULT_MAX_OUTPUT_BYTES);
792 mockedExecute.mockImplementationOnce((input) =>
793 Effect.succeed(outputWithin(input.maxOutputBytes, response)),
794 );
795 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
796
797 const page = yield* cli.listIterationChanges({
798 cwd: "/w",
799 location: { project: "platform", repository: "web" },
800 number: 42,
801 iterationId: 1,
802 });
803
804 assert.strictEqual(page.changes.length, 2_000);
805 assert.isFalse(page.truncated);
806 }),
807 );
808
809 it.effect("reads a file whose JSON envelope is past the default output limit", () =>
810 Effect.gen(function* () {
811 const file = "const value = 1;\n".repeat(57_000);
812 const response = json({ content: file });
813 expect(Buffer.byteLength(file)).toBeLessThan(VCS_DEFAULT_MAX_OUTPUT_BYTES);
814 expect(Buffer.byteLength(response)).toBeGreaterThan(VCS_DEFAULT_MAX_OUTPUT_BYTES);
815 mockedExecute.mockImplementationOnce((input) =>
816 Effect.succeed(outputWithin(input.maxOutputBytes, response)),
817 );
818 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
819
820 const item = yield* cli.readItemContent({
821 cwd: "/w",
822 location: { project: "platform", repository: "web" },
823 path: "src/generated/schema.ts",
824 commit: "a".repeat(40),
825 });
826
827 assert.strictEqual(item.contents, file);
828 assert.isFalse(item.isBinary);
829 }),
830 );
831
832 it.effect("reports a pull request it cannot place as its own outcome", () =>
833 Effect.gen(function* () {
834 mockedExecute.mockReturnValueOnce(
835 Effect.succeed(
836 output(
837 JSON.stringify({
838 pullRequestId: 42,
839 title: "Add the page",
840 sourceRefName: "refs/heads/feat/page",
841 targetRefName: "refs/heads/main",
842 creationDate: "2026-07-01T00:00:00Z",
843 }),
844 ),
845 ),
846 );
847 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
848
849 const error = yield* Effect.flip(cli.getPullRequest({ cwd: "/w", number: 42 }));
850
851 assert.strictEqual(error._tag, "AzureDevOpsPullRequestIncompleteError");
852 }),
853 );
854
855 it.effect("refuses a reviewer az would read as a flag, before running anything", () =>
856 Effect.gen(function* () {
857 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
858
859 const error = yield* Effect.flip(
860 cli.setPullRequestReviewers({
861 cwd: "/w",
862 number: 42,
863 reviewers: ["--query"],
864 requested: true,
865 }),
866 );
867
868 assert.strictEqual(error._tag, "AzureDevOpsReviewerNameError");
869 assert.strictEqual(mockedExecute.mock.calls.length, 0);
870 }),
871 );
872});