Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AzureDevOpsPullRequestCli.test.ts

653 lines20.0 KB

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 };
31​
32function pullRequestRows(
33 count: number,
34 firstNumber: number,
35): ReadonlyArray<Record<string, unknown>> {
36 return Array.from({ length: count }, (_, index) => ({
37 pullRequestId: firstNumber + index,
38 title: `Pull request ${firstNumber + index}`,
39 status: "active",
40 sourceRefName: "refs/heads/feat/page",
41 targetRefName: "refs/heads/main",
42 creationDate: "2026-07-01T00:00:00Z",
43 repository: { name: "web", project: { name: "platform" } },
44 url: `https://dev.azure.com/acme/_apis/git/repositories/web/pullRequests/${firstNumber + index}`,
45 }));
47​
48function pullRequests(count: number, firstNumber: number): string {
49 return JSON.stringify(pullRequestRows(count, firstNumber));
51​
52/** The arguments of the nth az invocation. */
53function argsOfCall(index: number): ReadonlyArray<string> {
54 const call = mockedExecute.mock.calls[index];
55 assert.isDefined(call);
56 return call[0].args;
58​
59afterEach(() => {
60 mockedExecute.mockReset();
61});
62​
63layer("AzureDevOpsPullRequestCli.layer", (it) => {
64 it.effect("asks for one row more than the page, to probe for a next page", () =>
65 Effect.gen(function* () {
66 mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(3, 1))));
67 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
68​
69 const batch = yield* cli.listPullRequests({
70 cwd: "/w",
71 repository: "web",
72 state: "open",
73 involvement: "all",
74 viewer: "bilal@acme.dev",
75 limit: 10,
76 });
77​
78 assert.strictEqual(batch.items.length, 3);
79 assert.isFalse(batch.truncated);
80 expect(argsOfCall(0)).toEqual([
81 "repos",
82 "pr",
83 "list",
84 "--detect",
85 "true",
86 "--repository",
87 "web",
88 "--status",
89 "active",
90 "--include-links",
91 "--top",
92 "11",
93 "--only-show-errors",
94 "--output",
95 "json",
96 ]);
97 }),
98 );
99​
100 it.effect("reads an Azure pull request page larger than the VCS default output limit", () =>
101 Effect.gen(function* () {
102 const rows = pullRequestRows(100, 1).map((row) => ({
103 ...row,
104 description: "x".repeat(10_000),
105 }));
106 // @effect-diagnostics-next-line preferSchemaOverJson:off
107 const response = JSON.stringify(rows);
108 expect(Buffer.byteLength(response)).toBeGreaterThan(1_000_000);
109​
110 mockedExecute.mockImplementationOnce((input) => {
111 const maxOutputBytes =
112 "maxOutputBytes" in input && typeof input.maxOutputBytes === "number"
113 ? input.maxOutputBytes
114 : 1_000_000;
115 return Effect.succeed(
116 maxOutputBytes >= Buffer.byteLength(response)
117 ? output(response)
118 : {
119 ...output(response.slice(0, maxOutputBytes)),
120 stdoutTruncated: true,
121 },
122 );
123 });
124 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
125​
126 const batch = yield* cli.listPullRequests({
127 cwd: "/w",
128 repository: "web",
129 state: "merged",
130 involvement: "all",
131 viewer: "bilal@acme.dev",
132 limit: 99,
133 });
134​
135 assert.strictEqual(batch.items.length, 99);
136 assert.isTrue(batch.truncated);
137 }),
138 );
139​
140 it.effect("reads the page unnarrowed when asked to search, having nothing to search with", () =>
141 Effect.gen(function* () {
142 mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(3, 1))));
143 const provider = yield* AzureDevOpsPullRequestProvider.make;
144​
145 const page = yield* provider.listChangeRequests({
146 cwd: "/w",
147 repository: "web",
148 host: "dev.azure.com",
149 state: "open",
150 involvement: "all",
151 viewer: "bilal@acme.dev",
152 limit: 10,
153 query: "page",
154 });
155​
156 // `az repos pr list` filters by status, creator, reviewer and branch, and by no text at
157 // all. The rows come back as they would have without a search, for the caller to narrow;
158 // nothing of the search reaches the command, where it could only mean the wrong thing.
159 assert.strictEqual(page.items.length, 3);
160 expect(argsOfCall(0)).toEqual([
161 "repos",
162 "pr",
163 "list",
164 "--detect",
165 "true",
166 "--repository",
167 "web",
168 "--status",
169 "active",
170 "--include-links",
171 "--top",
172 "11",
173 "--only-show-errors",
174 "--output",
175 "json",
176 ]);
177 }),
178 );
179​
180 it.effect("steps over what it has already handed over, which is all Azure can be told", () =>
181 Effect.gen(function* () {
182 mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(3, 1))));
183 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
184​
185 yield* cli.listPullRequests({
186 cwd: "/w",
187 repository: "web",
188 state: "open",
189 involvement: "all",
190 viewer: "bilal@acme.dev",
191 limit: 10,
192 // The instant is the same cursor every other host reads; Azure has no filter for it and
193 // takes the count instead.
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("reports truncation from the extra row", () =>
205 Effect.gen(function* () {
206 mockedExecute.mockReturnValueOnce(Effect.succeed(output(pullRequests(11, 1))));
207 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
208​
209 const batch = yield* cli.listPullRequests({
210 cwd: "/w",
211 repository: "web",
212 state: "open",
213 involvement: "all",
214 viewer: "bilal@acme.dev",
215 limit: 10,
216 });
217​
218 assert.strictEqual(batch.items.length, 10);
219 assert.isTrue(batch.truncated);
220 assert.strictEqual(batch.cursorAdvance, 10);
221 }),
222 );
223​
224 it.effect("advances by malformed raw rows and keeps reading until the page is full", () =>
225 Effect.gen(function* () {
226 mockedExecute
227 .mockReturnValueOnce(
228 Effect.succeed(
229 output(
230 // @effect-diagnostics-next-line preferSchemaOverJson:off
231 JSON.stringify([
232 { pullRequestId: "malformed" },
233 pullRequestRows(1, 1)[0],
234 { pullRequestId: "also malformed" },
235 ]),
236 ),
237 ),
238 )
239 .mockReturnValueOnce(Effect.succeed(output(pullRequests(2, 2))));
240 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
241​
242 const batch = yield* cli.listPullRequests({
243 cwd: "/w",
244 repository: "web",
245 state: "open",
246 involvement: "all",
247 viewer: "bilal@acme.dev",
248 limit: 2,
249 });
250​
251 expect(batch.items.map((item) => item.number)).toEqual([1, 2]);
252 assert.isTrue(batch.truncated);
253 // Three raw rows from the first request and one from the second produced this page.
254 assert.strictEqual(batch.cursorAdvance, 4);
255 const secondArgs = argsOfCall(1);
256 assert.strictEqual(secondArgs[secondArgs.indexOf("--skip") + 1], "3");
257 assert.strictEqual(secondArgs[secondArgs.indexOf("--top") + 1], "2");
258 }),
259 );
260​
261 it.effect("narrows to the author on the authored tab", () =>
262 Effect.gen(function* () {
263 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
264 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
265​
266 yield* cli.listPullRequests({
267 cwd: "/w",
268 repository: "web",
269 state: "closed",
270 involvement: "authored",
271 viewer: "bilal@acme.dev",
272 limit: 10,
273 });
274​
275 expect(argsOfCall(0)).toContain("--creator");
276 expect(argsOfCall(0)).toContain("bilal@acme.dev");
277 // Azure calls a closed pull request abandoned.
278 expect(argsOfCall(0)).toContain("abandoned");
279 }),
280 );
281​
282 it.effect("asks Azure for every status on the All tab", () =>
283 Effect.gen(function* () {
284 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
285 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
286​
287 yield* cli.listPullRequests({
288 cwd: "/w",
289 repository: "web",
290 state: "all",
291 involvement: "all",
292 viewer: "bilal@acme.dev",
293 limit: 10,
294 });
295​
296 expect(argsOfCall(0)).toContain("--status");
297 expect(argsOfCall(0)).toContain("all");
298 }),
299 );
300​
301 it.effect("narrows to the reviewer on the reviewing tab", () =>
302 Effect.gen(function* () {
303 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
304 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
305​
306 yield* cli.listPullRequests({
307 cwd: "/w",
308 repository: "web",
309 state: "open",
310 involvement: "reviewing",
311 viewer: "bilal@acme.dev",
312 limit: 10,
313 });
314​
315 expect(argsOfCall(0)).toContain("--reviewer");
316 }),
317 );
318​
319 it.effect("reads the signed-in account, which az reports as a bare value", () =>
320 Effect.gen(function* () {
321 // `--query user` unwraps the object, so the wrapper has to put it back.
322 mockedExecute.mockReturnValueOnce(
323 // @effect-diagnostics-next-line preferSchemaOverJson:off
324 Effect.succeed(output(JSON.stringify({ name: "bilal@acme.dev", type: "user" }))),
325 );
326 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
327​
328 const viewer = yield* cli.getViewer({ cwd: "/w" });
329​
330 assert.strictEqual(viewer, "bilal@acme.dev");
331 expect(argsOfCall(0)).toEqual([
332 "account",
333 "show",
334 "--query",
335 "user",
336 "--only-show-errors",
337 "--output",
338 "json",
339 ]);
340 }),
341 );
342​
343 it.effect("fails when nobody is signed in", () =>
344 Effect.gen(function* () {
345 mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
346 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
347​
348 const error = yield* Effect.flip(cli.getViewer({ cwd: "/w" }));
349​
350 assert.strictEqual(error._tag, "AzureDevOpsViewerUnavailableError");
351 }),
352 );
353​
354 it.effect("completes a pull request to merge it, squashing only when asked", () =>
355 Effect.gen(function* () {
356 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
357 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
358​
359 yield* cli.runPullRequestAction({
360 cwd: "/w",
361 number: 42,
362 action: "merge",
363 mergeMethod: "squash",
364 });
365​
366 expect(argsOfCall(0)).toEqual([
367 "repos",
368 "pr",
369 "update",
370 "--detect",
371 "true",
372 "--id",
373 "42",
374 "--status",
375 "completed",
376 "--squash",
377 "true",
378 "--only-show-errors",
379 "--output",
380 "json",
381 ]);
382 }),
383 );
384​
385 it.effect("stores the squash choice with an auto-completion, as a merge now does", () =>
386 Effect.gen(function* () {
387 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
388 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
389​
390 yield* cli.runPullRequestAction({
391 cwd: "/w",
392 number: 42,
393 action: "enable-auto-merge",
394 mergeMethod: "squash",
395 });
396​
397 expect(argsOfCall(0)).toEqual([
398 "repos",
399 "pr",
400 "update",
401 "--detect",
402 "true",
403 "--id",
404 "42",
405 "--auto-complete",
406 "true",
407 "--squash",
408 "true",
409 "--only-show-errors",
410 "--output",
411 "json",
412 ]);
413 }),
414 );
415​
416 it.effect.each([
417 { action: "enable-auto-merge", expected: ["--auto-complete", "true"] },
418 { action: "disable-auto-merge", expected: ["--auto-complete", "false"] },
419 { action: "draft", expected: ["--draft", "true"] },
420 { action: "ready", expected: ["--draft", "false"] },
421 { action: "close", expected: ["--status", "abandoned"] },
422 { action: "reopen", expected: ["--status", "active"] },
423 ] as const)("moves a pull request with $action", ({ action, expected }) =>
424 Effect.gen(function* () {
425 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
426 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
427​
428 yield* cli.runPullRequestAction({ cwd: "/w", number: 42, action });
429​
430 expect(argsOfCall(0)).toEqual([
431 "repos",
432 "pr",
433 "update",
434 "--detect",
435 "true",
436 "--id",
437 "42",
438 ...expected,
439 "--only-show-errors",
440 "--output",
441 "json",
442 ]);
443 }),
444 );
445​
446 it.effect.each([
447 { name: "a title", rewrite: { title: "Add the page" }, expected: ["--title=Add the page"] },
448 {
449 name: "a description",
450 rewrite: { body: "Why the page changed" },
451 expected: ["--description=Why the page changed"],
452 },
453 {
454 name: "both",
455 rewrite: { title: "Add the page", body: "Why the page changed" },
456 expected: ["--title=Add the page", "--description=Why the page changed"],
457 },
458 ] as const)("rewrites $name, sending nothing it was not given", ({ rewrite, expected }) =>
459 Effect.gen(function* () {
460 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
461 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
462​
463 yield* cli.updatePullRequest({ cwd: "/w", number: 42, ...rewrite });
464​
465 expect(argsOfCall(0)).toEqual([
466 "repos",
467 "pr",
468 "update",
469 "--detect",
470 "true",
471 "--id",
472 "42",
473 ...expected,
474 "--only-show-errors",
475 "--output",
476 "json",
477 ]);
478 }),
479 );
480​
481 it.effect("sends a description that starts with a dash as one value, not as a flag", () =>
482 Effect.gen(function* () {
483 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
484 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
485​
486 yield* cli.updatePullRequest({
487 cwd: "/w",
488 number: 42,
489 body: "- rewrote the page\n- kept the rest",
490 });
491​
492 // One argument, so the leading dash of an ordinary bullet list never reaches az as a flag,
493 // and the whole text stays together where `--description` would otherwise take several.
494 expect(argsOfCall(0)).toContain("--description=- rewrote the page\n- kept the rest");
495 }),
496 );
497​
498 it.effect("rewrites through the provider, which says it takes one", () =>
499 Effect.gen(function* () {
500 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
501 const provider = yield* AzureDevOpsPullRequestProvider.make;
502​
503 // False for a remark because nothing here can post one, so there is none to rewrite.
504 expect(provider.capabilities.edit).toEqual({ changeRequest: true, comment: false });
505 assert.isDefined(provider.updateChangeRequest);
506 yield* provider.updateChangeRequest({
507 cwd: "/w",
508 repository: "web",
509 host: "dev.azure.com",
510 number: 42,
511 title: "Add the page",
512 });
513​
514 expect(argsOfCall(0)).toContain("--title=Add the page");
515 expect(argsOfCall(0)).not.toContain("--description");
516 }),
517 );
518​
519 it.effect("reads the conversation through the REST API, pinned to a version", () =>
520 Effect.gen(function* () {
521 mockedExecute.mockReturnValueOnce(
522 Effect.succeed(
523 output(
524 // @effect-diagnostics-next-line preferSchemaOverJson:off
525 JSON.stringify({
526 value: [
527 {
528 id: 1,
529 comments: [
530 { id: 1, content: "Looks good.", publishedDate: "2026-07-02T00:00:00Z" },
531 ],
532 },
533 ],
534 }),
535 ),
536 ),
537 );
538 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
539​
540 const comments = yield* cli.listThreads({
541 cwd: "/w",
542 threadsUrl: "https://dev.azure.com/acme/platform/_apis/git/r/web/pullRequests/42/threads",
543 });
544​
545 assert.strictEqual(comments.length, 1);
546 expect(argsOfCall(0)).toContain("rest");
547 expect(argsOfCall(0)).toContain(
548 "https://dev.azure.com/acme/platform/_apis/git/r/web/pullRequests/42/threads?api-version=7.1",
549 );
550 }),
551 );
552​
553 it.effect("reports a pull request it cannot place as its own outcome", () =>
554 Effect.gen(function* () {
555 mockedExecute.mockReturnValueOnce(
556 Effect.succeed(
557 output(
558 // Well-formed, but with nothing to build a link from: not a decode failure.
559 // @effect-diagnostics-next-line preferSchemaOverJson:off
560 JSON.stringify({
561 pullRequestId: 42,
562 title: "Add the page",
563 sourceRefName: "refs/heads/feat/page",
564 targetRefName: "refs/heads/main",
565 creationDate: "2026-07-01T00:00:00Z",
566 }),
567 ),
568 ),
569 );
570 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
571​
572 const error = yield* Effect.flip(cli.getPullRequest({ cwd: "/w", number: 42 }));
573​
574 assert.strictEqual(error._tag, "AzureDevOpsPullRequestIncompleteError");
575 }),
576 );
577​
578 it.effect("fails the read when az returns something unreadable", () =>
579 Effect.gen(function* () {
580 mockedExecute.mockReturnValueOnce(Effect.succeed(output('{"message":"not found"}')));
581 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
582​
583 const error = yield* Effect.flip(cli.getPullRequest({ cwd: "/w", number: 42 }));
584​
585 assert.strictEqual(error._tag, "AzureDevOpsPullRequestReadError");
586 }),
587 );
588​
589 it.effect("adds reviewers with the one command Azure has for it", () =>
590 Effect.gen(function* () {
591 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
592 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
593​
594 yield* cli.setPullRequestReviewers({
595 cwd: "/w",
596 number: 42,
597 reviewers: ["octocat@acme.test", "hubot@acme.test"],
598 requested: true,
599 });
600​
601 expect(argsOfCall(0)).toEqual([
602 "repos",
603 "pr",
604 "reviewer",
605 "add",
606 "--detect",
607 "true",
608 "--id",
609 "42",
610 "--reviewers",
611 "octocat@acme.test",
612 "hubot@acme.test",
613 "--only-show-errors",
614 "--output",
615 "json",
616 ]);
617 }),
618 );
619​
620 it.effect("takes a reviewer off the pull request with the same command's counterpart", () =>
621 Effect.gen(function* () {
622 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
623 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
624​
625 yield* cli.setPullRequestReviewers({
626 cwd: "/w",
627 number: 42,
628 reviewers: ["octocat@acme.test"],
629 requested: false,
630 });
631​
632 expect(argsOfCall(0)).toContain("remove");
633 }),
634 );
635​
636 it.effect("refuses a reviewer az would read as a flag, before running anything", () =>
637 Effect.gen(function* () {
638 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
639​
640 const error = yield* Effect.flip(
641 cli.setPullRequestReviewers({
642 cwd: "/w",
643 number: 42,
644 reviewers: ["--query"],
645 requested: true,
646 }),
647 );
648​
649 assert.strictEqual(error._tag, "AzureDevOpsReviewerNameError");
650 assert.strictEqual(mockedExecute.mock.calls.length, 0);
651 }),
652 );
653});