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

AzureDevOpsPullRequestCli.test.ts

507 lines15.4 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 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.each([
346 { action: "draft", expected: ["--draft", "true"] },
347 { action: "ready", expected: ["--draft", "false"] },
348 { action: "close", expected: ["--status", "abandoned"] },
349 { action: "reopen", expected: ["--status", "active"] },
350 ] as const)("moves a pull request with $action", ({ action, expected }) =>
351 Effect.gen(function* () {
352 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
353 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
354​
355 yield* cli.runPullRequestAction({ cwd: "/w", number: 42, action });
356​
357 expect(argsOfCall(0)).toEqual([
358 "repos",
359 "pr",
360 "update",
361 "--detect",
362 "true",
363 "--id",
364 "42",
365 ...expected,
366 "--only-show-errors",
367 "--output",
368 "json",
369 ]);
370 }),
371 );
372​
373 it.effect("reads the conversation through the REST API, pinned to a version", () =>
374 Effect.gen(function* () {
375 mockedExecute.mockReturnValueOnce(
376 Effect.succeed(
377 output(
378 // @effect-diagnostics-next-line preferSchemaOverJson:off
379 JSON.stringify({
380 value: [
381 {
382 id: 1,
383 comments: [
384 { id: 1, content: "Looks good.", publishedDate: "2026-07-02T00:00:00Z" },
385 ],
386 },
387 ],
388 }),
389 ),
390 ),
391 );
392 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
393​
394 const comments = yield* cli.listThreads({
395 cwd: "/w",
396 threadsUrl: "https://dev.azure.com/acme/platform/_apis/git/r/web/pullRequests/42/threads",
397 });
398​
399 assert.strictEqual(comments.length, 1);
400 expect(argsOfCall(0)).toContain("rest");
401 expect(argsOfCall(0)).toContain(
402 "https://dev.azure.com/acme/platform/_apis/git/r/web/pullRequests/42/threads?api-version=7.1",
403 );
404 }),
405 );
406​
407 it.effect("reports a pull request it cannot place as its own outcome", () =>
408 Effect.gen(function* () {
409 mockedExecute.mockReturnValueOnce(
410 Effect.succeed(
411 output(
412 // Well-formed, but with nothing to build a link from: not a decode failure.
413 // @effect-diagnostics-next-line preferSchemaOverJson:off
414 JSON.stringify({
415 pullRequestId: 42,
416 title: "Add the page",
417 sourceRefName: "refs/heads/feat/page",
418 targetRefName: "refs/heads/main",
419 creationDate: "2026-07-01T00:00:00Z",
420 }),
421 ),
422 ),
423 );
424 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
425​
426 const error = yield* Effect.flip(cli.getPullRequest({ cwd: "/w", number: 42 }));
427​
428 assert.strictEqual(error._tag, "AzureDevOpsPullRequestIncompleteError");
429 }),
430 );
431​
432 it.effect("fails the read when az returns something unreadable", () =>
433 Effect.gen(function* () {
434 mockedExecute.mockReturnValueOnce(Effect.succeed(output('{"message":"not found"}')));
435 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
436​
437 const error = yield* Effect.flip(cli.getPullRequest({ cwd: "/w", number: 42 }));
438​
439 assert.strictEqual(error._tag, "AzureDevOpsPullRequestReadError");
440 }),
441 );
442​
443 it.effect("adds reviewers with the one command Azure has for it", () =>
444 Effect.gen(function* () {
445 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
446 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
447​
448 yield* cli.setPullRequestReviewers({
449 cwd: "/w",
450 number: 42,
451 reviewers: ["octocat@acme.test", "hubot@acme.test"],
452 requested: true,
453 });
454​
455 expect(argsOfCall(0)).toEqual([
456 "repos",
457 "pr",
458 "reviewer",
459 "add",
460 "--detect",
461 "true",
462 "--id",
463 "42",
464 "--reviewers",
465 "octocat@acme.test",
466 "hubot@acme.test",
467 "--only-show-errors",
468 "--output",
469 "json",
470 ]);
471 }),
472 );
473​
474 it.effect("takes a reviewer off the pull request with the same command's counterpart", () =>
475 Effect.gen(function* () {
476 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
477 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
478​
479 yield* cli.setPullRequestReviewers({
480 cwd: "/w",
481 number: 42,
482 reviewers: ["octocat@acme.test"],
483 requested: false,
484 });
485​
486 expect(argsOfCall(0)).toContain("remove");
487 }),
488 );
489​
490 it.effect("refuses a reviewer az would read as a flag, before running anything", () =>
491 Effect.gen(function* () {
492 const cli = yield* AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCli;
493​
494 const error = yield* Effect.flip(
495 cli.setPullRequestReviewers({
496 cwd: "/w",
497 number: 42,
498 reviewers: ["--query"],
499 requested: true,
500 }),
501 );
502​
503 assert.strictEqual(error._tag, "AzureDevOpsReviewerNameError");
504 assert.strictEqual(mockedExecute.mock.calls.length, 0);
505 }),
506 );
507});