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