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 GitLabCli from "./GitLabCli.ts";
7import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
8
9const mockedExecute = vi.fn<GitLabCli.GitLabCli["Service"]["execute"]>();
10
11const layer = it.layer(
12 GitLabPullRequestCli.layer.pipe(
13 Layer.provide(
14 Layer.mock(GitLabCli.GitLabCli)({
15 execute: mockedExecute,
16 }),
17 ),
18 ),
19);
20
21function output(stdout: string, stdoutTruncated = false, stdoutInvalidUtf8 = false) {
22 return {
23 exitCode: ChildProcessSpawner.ExitCode(0),
24 stdout,
25 stderr: "",
26 stdoutTruncated,
27 stderrTruncated: false,
28 stdoutInvalidUtf8,
29 };
30}
31
32function mergeRequests(count: number, firstNumber: number): string {
33 return JSON.stringify(
34 Array.from({ length: count }, (_, index) => ({
35 iid: firstNumber + index,
36 title: `Merge request ${firstNumber + index}`,
37 web_url: `https://gitlab.com/acme/web/-/merge_requests/${firstNumber + index}`,
38 source_branch: "feat/page",
39 target_branch: "main",
40 created_at: "2026-07-01T00:00:00Z",
41 updated_at: "2026-07-02T00:00:00Z",
42 })),
43 );
44}
45
46function diffPage(firstIndex: number, count = 100): string {
47 return JSON.stringify(
48 Array.from({ length: count }, (_, index) => ({
49 old_path: `src/${firstIndex + index}.ts`,
50 new_path: `src/${firstIndex + index}.ts`,
51 diff: "@@ -1 +1 @@\n-a\n+b\n",
52 })),
53 );
54}
55
56function notes(count: number, firstId: number): string {
57 return JSON.stringify(
58 Array.from({ length: count }, (_, index) => ({
59 id: firstId + index,
60 body: `note ${firstId + index}`,
61 author: { username: "bilal" },
62 created_at: "2026-07-01T00:00:00Z",
63 })),
64 );
65}
66
67const author = { id: 1, username: "bilal" };
68const reviewer = { id: 5, username: "octocat" };
69
70function mergeRequestJson(overrides: Record<string, unknown>): string {
71 return JSON.stringify({
72 iid: 7,
73 title: "Merge request 7",
74 web_url: "https://gitlab.com/acme/web/-/merge_requests/7",
75 source_branch: "feat/page",
76 target_branch: "main",
77 created_at: "2026-07-01T00:00:00Z",
78 updated_at: "2026-07-02T00:00:00Z",
79 author,
80 ...overrides,
81 });
82}
83
84function argsOfCall(index: number): ReadonlyArray<string> {
85 return callAt(index).args;
86}
87
88function callAt(index: number) {
89 const call = mockedExecute.mock.calls[index];
90 assert.isDefined(call);
91 return call[0];
92}
93
94afterEach(() => {
95 mockedExecute.mockReset();
96});
97
98layer("GitLabPullRequestCli.layer", (it) => {
99 it.effect("asks GitLab for one row more than the page, to probe for a next page", () =>
100 Effect.gen(function* () {
101 mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(3, 1))));
102 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
103
104 const batch = yield* cli.listMergeRequests({
105 cwd: "/w",
106 repository: "acme/web",
107 state: "open",
108 involvement: "all",
109 viewer: "bilal",
110 limit: 10,
111 });
112
113 assert.strictEqual(batch.items.length, 3);
114 assert.isFalse(batch.truncated);
115 assert.strictEqual(batch.cursorAdvance, 3);
116 const path = argsOfCall(0)[1] ?? "";
117 expect(path).toContain("projects/acme%2Fweb/merge_requests");
118 expect(path).toContain("per_page=11");
119 expect(path).toContain("state=opened");
120 }),
121 );
122
123 it.effect("walks pages at a fixed size, because GitLab pages by offset", () =>
124 Effect.gen(function* () {
125 mockedExecute
126 .mockReturnValueOnce(Effect.succeed(output(mergeRequests(100, 1))))
127 .mockReturnValueOnce(Effect.succeed(output(mergeRequests(100, 101))));
128 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
129
130 const batch = yield* cli.listMergeRequests({
131 cwd: "/w",
132 repository: "acme/web",
133 state: "open",
134 involvement: "all",
135 viewer: "bilal",
136 limit: 150,
137 });
138
139 assert.strictEqual(batch.items.length, 150);
140 assert.isTrue(batch.truncated);
141 for (const index of [0, 1]) {
142 expect(argsOfCall(index)[1]).toContain("per_page=100");
143 }
144 expect(argsOfCall(0)[1]).toContain("page=1");
145 expect(argsOfCall(1)[1]).toContain("page=2");
146 }),
147 );
148
149 it.effect("hands a search to GitLab's own search parameter", () =>
150 Effect.gen(function* () {
151 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
152 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
153
154 yield* cli.listMergeRequests({
155 cwd: "/w",
156 repository: "acme/web",
157 state: "open",
158 involvement: "all",
159 viewer: "bilal",
160 limit: 10,
161 query: "page",
162 });
163
164 expect(argsOfCall(0)[1]).toContain("search=page");
165 }),
166 );
167
168 it.effect("carries on from the number of rows already delivered", () =>
169 Effect.gen(function* () {
170 mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(3, 1))));
171 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
172
173 yield* cli.listMergeRequests({
174 cwd: "/w",
175 repository: "acme/web",
176 state: "open",
177 involvement: "all",
178 viewer: "bilal",
179 limit: 10,
180 cursor: { updatedBefore: "2026-07-02T00:00:00Z", delivered: 10 },
181 });
182
183 // GitLab's timestamp filter has no tie-breaker, so an offset is what advances through a
184 // boundary shared by more rows than one page can hold.
185 const path = argsOfCall(0)[1] ?? "";
186 expect(path).not.toContain("updated_before=");
187 expect(path).toContain("order_by=updated_at");
188 expect(path).toContain("per_page=11");
189 expect(path).toContain("page=1");
190 }),
191 );
192
193 it.effect("advances beyond several pages sharing the cursor timestamp", () =>
194 Effect.gen(function* () {
195 mockedExecute
196 .mockReturnValueOnce(Effect.succeed(output(mergeRequests(11, 144))))
197 .mockReturnValueOnce(Effect.succeed(output(mergeRequests(11, 155))));
198 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
199
200 const batch = yield* cli.listMergeRequests({
201 cwd: "/w",
202 repository: "acme/web",
203 state: "open",
204 involvement: "all",
205 viewer: "bilal",
206 limit: 10,
207 cursor: { updatedBefore: "2026-07-02T00:00:00Z", delivered: 150 },
208 });
209
210 expect(argsOfCall(0)[1]).toContain("per_page=11");
211 expect(argsOfCall(0)[1]).toContain("page=14");
212 expect(argsOfCall(1)[1]).toContain("page=15");
213 expect(batch.items.map((item) => item.number)).toEqual([
214 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
215 ]);
216 assert.isTrue(batch.truncated);
217 }),
218 );
219
220 it.effect("advances the cursor through malformed raw rows", () =>
221 Effect.gen(function* () {
222 const rows = JSON.parse(mergeRequests(2, 1)) as ReadonlyArray<unknown>;
223 mockedExecute.mockReturnValueOnce(
224 Effect.succeed(output(JSON.stringify([{ iid: "malformed" }, ...rows]))),
225 );
226 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
227
228 const batch = yield* cli.listMergeRequests({
229 cwd: "/w",
230 repository: "acme/web",
231 state: "open",
232 involvement: "all",
233 viewer: "bilal",
234 limit: 2,
235 });
236
237 expect(batch.items.map((item) => item.number)).toEqual([1, 2]);
238 assert.strictEqual(batch.cursorAdvance, 3);
239 assert.isTrue(batch.truncated);
240 }),
241 );
242
243 it.effect("URL-encodes a search, so it cannot add a parameter of its own", () =>
244 Effect.gen(function* () {
245 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
246 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
247
248 yield* cli.listMergeRequests({
249 cwd: "/w",
250 repository: "acme/web",
251 state: "open",
252 involvement: "all",
253 viewer: "bilal",
254 limit: 10,
255 query: '-a&per_page=1 "b"',
256 });
257
258 const path = argsOfCall(0)[1] ?? "";
259 expect(path).toContain("search=-a%26per_page%3D1%20%22b%22");
260 assert.strictEqual(path.match(/per_page=/g)?.length, 1);
261 }),
262 );
263
264 it.effect("asks for no search at all when the reader typed only spaces", () =>
265 Effect.gen(function* () {
266 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
267 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
268
269 yield* cli.listMergeRequests({
270 cwd: "/w",
271 repository: "acme/web",
272 state: "open",
273 involvement: "all",
274 viewer: "bilal",
275 limit: 10,
276 query: " ",
277 });
278
279 expect(argsOfCall(0)[1]).not.toContain("search=");
280 }),
281 );
282
283 it.effect("stops walking on a short page", () =>
284 Effect.gen(function* () {
285 mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(40, 1))));
286 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
287
288 const batch = yield* cli.listMergeRequests({
289 cwd: "/w",
290 repository: "acme/web",
291 state: "open",
292 involvement: "all",
293 viewer: "bilal",
294 limit: 150,
295 });
296
297 assert.strictEqual(batch.items.length, 40);
298 assert.isFalse(batch.truncated);
299 assert.strictEqual(mockedExecute.mock.calls.length, 1);
300 }),
301 );
302
303 it.effect("stops walking when every row on a page fails to decode", () =>
304 Effect.gen(function* () {
305 const unusable = JSON.stringify(Array.from({ length: 100 }, () => ({ iid: "nope" })));
306 mockedExecute.mockReturnValue(Effect.succeed(output(unusable)));
307 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
308
309 const batch = yield* cli.listMergeRequests({
310 cwd: "/w",
311 repository: "acme/web",
312 state: "open",
313 involvement: "all",
314 viewer: "bilal",
315 limit: 150,
316 });
317
318 assert.strictEqual(batch.items.length, 0);
319 assert.strictEqual(mockedExecute.mock.calls.length, 2);
320 }),
321 );
322
323 it.effect("asks GitLab for every state on the All tab", () =>
324 Effect.gen(function* () {
325 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
326 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
327
328 yield* cli.listMergeRequests({
329 cwd: "/w",
330 repository: "acme/web",
331 state: "all",
332 involvement: "all",
333 viewer: "bilal",
334 limit: 10,
335 });
336
337 expect(argsOfCall(0)[1]).toContain("state=all");
338 }),
339 );
340
341 it.effect("filters by the reviewer when the viewer is reviewing", () =>
342 Effect.gen(function* () {
343 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
344 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
345
346 yield* cli.listMergeRequests({
347 cwd: "/w",
348 repository: "acme/web",
349 state: "open",
350 involvement: "reviewing",
351 viewer: "bilal",
352 limit: 10,
353 });
354
355 expect(argsOfCall(0)[1]).toContain("reviewer_username=bilal");
356 }),
357 );
358
359 it.effect("addresses a nested group project by its encoded full path", () =>
360 Effect.gen(function* () {
361 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
362 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
363
364 yield* cli.listMergeRequests({
365 cwd: "/w",
366 repository: "acme/platform/web",
367 state: "open",
368 involvement: "all",
369 viewer: "bilal",
370 limit: 10,
371 });
372
373 expect(argsOfCall(0)[1]).toContain("projects/acme%2Fplatform%2Fweb/merge_requests");
374 }),
375 );
376
377 it.effect("merges immediately rather than leaving auto-merge armed", () =>
378 Effect.gen(function* () {
379 mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
380 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
381
382 yield* cli.runMergeRequestAction({
383 cwd: "/w",
384 repository: "acme/web",
385 number: 7,
386 action: "merge",
387 mergeMethod: "squash",
388 });
389
390 expect(argsOfCall(0)).toEqual([
391 "mr",
392 "merge",
393 "7",
394 "--repo",
395 "acme/web",
396 "--auto-merge=false",
397 "--yes",
398 "--squash",
399 ]);
400 }),
401 );
402
403 it.effect("arms auto-merge with the same strategy a merge would have used", () =>
404 Effect.gen(function* () {
405 mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
406 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
407
408 yield* cli.runMergeRequestAction({
409 cwd: "/w",
410 repository: "acme/web",
411 number: 7,
412 action: "enable-auto-merge",
413 mergeMethod: "squash",
414 });
415
416 expect(argsOfCall(0)).toEqual([
417 "mr",
418 "merge",
419 "7",
420 "--repo",
421 "acme/web",
422 "--auto-merge=true",
423 "--yes",
424 "--squash",
425 ]);
426 }),
427 );
428
429 it.effect("cancels an armed auto-merge through the API glab has no flag for", () =>
430 Effect.gen(function* () {
431 mockedExecute.mockReturnValueOnce(Effect.succeed(output("{}")));
432 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
433
434 yield* cli.runMergeRequestAction({
435 cwd: "/w",
436 repository: "acme/platform/web",
437 number: 7,
438 action: "disable-auto-merge",
439 });
440
441 expect(argsOfCall(0)).toEqual([
442 "api",
443 "projects/acme%2Fplatform%2Fweb/merge_requests/7/cancel_merge_when_pipeline_succeeds",
444 "--method",
445 "POST",
446 ]);
447 }),
448 );
449
450 it.effect("brings a stale branch up to date by rebasing it, the only way GitLab has", () =>
451 Effect.gen(function* () {
452 mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
453 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
454
455 yield* cli.runMergeRequestAction({
456 cwd: "/w",
457 repository: "acme/web",
458 number: 7,
459 action: "update-branch",
460 });
461
462 expect(argsOfCall(0)).toEqual(["mr", "rebase", "7", "--repo", "acme/web"]);
463 }),
464 );
465
466 it.effect("moves a merge request back to draft through glab", () =>
467 Effect.gen(function* () {
468 mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
469 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
470
471 yield* cli.runMergeRequestAction({
472 cwd: "/w",
473 repository: "acme/web",
474 number: 7,
475 action: "draft",
476 });
477
478 expect(argsOfCall(0)).toEqual(["mr", "update", "7", "--repo", "acme/web", "--draft"]);
479 }),
480 );
481
482 it.effect("sends a comment body over stdin, never in argv", () =>
483 Effect.gen(function* () {
484 mockedExecute.mockReturnValueOnce(Effect.succeed(output("")));
485 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
486
487 yield* cli.commentOnMergeRequest({
488 cwd: "/w",
489 repository: "acme/web",
490 number: 7,
491 body: "true",
492 });
493
494 const call = mockedExecute.mock.calls[0];
495 assert.isDefined(call);
496 expect(call[0].args).toEqual([
497 "api",
498 "projects/acme%2Fweb/merge_requests/7/notes",
499 "--method",
500 "POST",
501 "--input",
502 "-",
503 "--header",
504 "Content-Type: application/json",
505 ]);
506 expect(call[0].stdin).toBe('{"body":"true"}');
507 }),
508 );
509
510 it.effect("reads one diff page and hands back the cursor for the next", () =>
511 Effect.gen(function* () {
512 mockedExecute.mockReturnValueOnce(Effect.succeed(output(diffPage(0))));
513 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
514
515 const diff = yield* cli.getMergeRequestDiff({
516 cwd: "/w",
517 repository: "acme/web",
518 number: 7,
519 });
520
521 assert.strictEqual(mockedExecute.mock.calls.length, 1);
522 assert.isNotNull(diff.nextCursor);
523 assert.isFalse(diff.truncated);
524 expect(argsOfCall(0)[1]).toContain("merge_requests/7/diffs?per_page=100&page=1");
525 }),
526 );
527
528 it.effect("carries on from a cursor at the page it names", () =>
529 Effect.gen(function* () {
530 mockedExecute
531 .mockReturnValueOnce(Effect.succeed(output(diffPage(0))))
532 .mockReturnValueOnce(Effect.succeed(output(diffPage(100, 3))));
533 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
534 const target = { cwd: "/w", repository: "acme/web", number: 7 };
535
536 const first = yield* cli.getMergeRequestDiff(target);
537 assert.isNotNull(first.nextCursor);
538 const second = yield* cli.getMergeRequestDiff({ ...target, cursor: first.nextCursor });
539
540 expect(argsOfCall(1)[1]).toContain("page=2");
541 assert.isNull(second.nextCursor);
542 expect(second.patch).toContain("diff --git a/src/100.ts b/src/100.ts");
543 }),
544 );
545
546 it.effect("refuses a cursor it never handed out rather than reading it into a query", () =>
547 Effect.gen(function* () {
548 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
549
550 const error = yield* Effect.flip(
551 cli.getMergeRequestDiff({
552 cwd: "/w",
553 repository: "acme/web",
554 number: 7,
555 cursor: "1&per_page=1",
556 }),
557 );
558
559 assert.strictEqual(error._tag, "GitLabDiffCursorError");
560 assert.strictEqual(mockedExecute.mock.calls.length, 0);
561 }),
562 );
563
564 it.effect("reads a named commit from its own diff, and pages inside it", () =>
565 Effect.gen(function* () {
566 mockedExecute
567 .mockReturnValueOnce(Effect.succeed(output(diffPage(0))))
568 .mockReturnValueOnce(Effect.succeed(output(diffPage(100, 3))));
569 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
570 const target = {
571 cwd: "/w",
572 repository: "acme/web",
573 number: 7,
574 commit: "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0",
575 };
576
577 const first = yield* cli.getMergeRequestDiff(target);
578 assert.isNotNull(first.nextCursor);
579 const second = yield* cli.getMergeRequestDiff({ ...target, cursor: first.nextCursor });
580
581 const commitPath =
582 "projects/acme%2Fweb/repository/commits/a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0/diff";
583 expect(argsOfCall(0)[1]).toBe(`${commitPath}?per_page=100&page=1`);
584 expect(argsOfCall(1)[1]).toBe(`${commitPath}?per_page=100&page=2`);
585 assert.isNull(second.nextCursor);
586 }),
587 );
588
589 it.effect("refuses a commit that is not a sha rather than reading it into a path", () =>
590 Effect.gen(function* () {
591 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
592
593 const error = yield* Effect.flip(
594 cli.getMergeRequestDiff({
595 cwd: "/w",
596 repository: "acme/web",
597 number: 7,
598 commit: "../../merge_requests/8/diffs",
599 }),
600 );
601
602 assert.strictEqual(error._tag, "GitLabDiffCommitError");
603 assert.strictEqual(mockedExecute.mock.calls.length, 0);
604 }),
605 );
606
607 it.effect("reports a commit with no parent as a structured error", () =>
608 Effect.gen(function* () {
609 mockedExecute.mockReturnValueOnce(
610 Effect.succeed(output(JSON.stringify({ id: "a1b2c3d", parent_ids: [] }))),
611 );
612 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
613
614 const error = yield* Effect.flip(
615 cli.getMergeRequestDiffFileContents({
616 cwd: "/w",
617 repository: "acme/web",
618 number: 7,
619 commit: "a1b2c3d",
620 changeType: "change",
621 oldPath: "src/a.ts",
622 newPath: "src/a.ts",
623 }),
624 );
625
626 assert.strictEqual(error._tag, "GitLabDiffCommitParentUnavailableError");
627 if (error._tag === "GitLabDiffCommitParentUnavailableError") {
628 assert.strictEqual(error.commit, "a1b2c3d");
629 }
630 }),
631 );
632
633 it.effect("expands a new file from a root commit without requiring a parent", () =>
634 Effect.gen(function* () {
635 mockedExecute.mockReturnValueOnce(
636 Effect.succeed(output(JSON.stringify({ id: "a1b2c3d", parent_ids: [] }))),
637 );
638 mockedExecute.mockReturnValueOnce(Effect.succeed(output("first contents\n")));
639 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
640
641 const contents = yield* cli.getMergeRequestDiffFileContents({
642 cwd: "/w",
643 repository: "acme/web",
644 number: 7,
645 commit: "a1b2c3d",
646 changeType: "new",
647 oldPath: "src/first.ts",
648 newPath: "src/first.ts",
649 });
650
651 expect(contents).toEqual({ oldContents: "", newContents: "first contents\n" });
652 expect(argsOfCall(1)[1]).toContain("raw?ref=a1b2c3d");
653 }),
654 );
655
656 it.effect("reports an oversized diff file with its path and reason", () =>
657 Effect.gen(function* () {
658 mockedExecute.mockReturnValueOnce(
659 Effect.succeed(
660 output(
661 JSON.stringify({
662 diff_refs: {
663 base_sha: "a1b2c3d",
664 head_sha: "b1c2d3e",
665 start_sha: "a1b2c3d",
666 },
667 }),
668 ),
669 ),
670 );
671 mockedExecute.mockReturnValueOnce(Effect.succeed(output("partial", true)));
672 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
673
674 const error = yield* Effect.flip(
675 cli.getMergeRequestDiffFileContents({
676 cwd: "/w",
677 repository: "acme/web",
678 number: 7,
679 changeType: "deleted",
680 oldPath: "src/large.ts",
681 newPath: "src/large.ts",
682 }),
683 );
684
685 assert.strictEqual(error._tag, "GitLabDiffFileContentsUnavailableError");
686 if (error._tag === "GitLabDiffFileContentsUnavailableError") {
687 assert.strictEqual(error.path, "src/large.ts");
688 assert.strictEqual(error.reason, "oversized");
689 }
690 }),
691 );
692
693 it.effect("reports undecodable diff file contents as binary", () =>
694 Effect.gen(function* () {
695 mockedExecute.mockReturnValueOnce(
696 Effect.succeed(
697 output(
698 JSON.stringify({
699 diff_refs: {
700 base_sha: "a1b2c3d",
701 head_sha: "b1c2d3e",
702 start_sha: "a1b2c3d",
703 },
704 }),
705 ),
706 ),
707 );
708 mockedExecute.mockReturnValueOnce(
709 Effect.succeed(output("binary\uFFFDcontents", false, true)),
710 );
711 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
712
713 const error = yield* Effect.flip(
714 cli.getMergeRequestDiffFileContents({
715 cwd: "/w",
716 repository: "acme/web",
717 number: 7,
718 changeType: "deleted",
719 oldPath: "assets/logo.png",
720 newPath: "assets/logo.png",
721 }),
722 );
723
724 assert.strictEqual(error._tag, "GitLabDiffFileContentsUnavailableError");
725 if (error._tag === "GitLabDiffFileContentsUnavailableError") {
726 assert.strictEqual(error.path, "assets/logo.png");
727 assert.strictEqual(error.reason, "binary");
728 }
729 }),
730 );
731
732 it.effect("returns valid text containing a literal replacement character", () =>
733 Effect.gen(function* () {
734 mockedExecute.mockReturnValueOnce(
735 Effect.succeed(
736 output(
737 JSON.stringify({
738 diff_refs: {
739 base_sha: "a1b2c3d",
740 head_sha: "b1c2d3e",
741 start_sha: "a1b2c3d",
742 },
743 }),
744 ),
745 ),
746 );
747 mockedExecute.mockReturnValueOnce(Effect.succeed(output("before\uFFFDafter")));
748 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
749
750 const contents = yield* cli.getMergeRequestDiffFileContents({
751 cwd: "/w",
752 repository: "acme/web",
753 number: 7,
754 changeType: "deleted",
755 oldPath: "docs/encoding.md",
756 newPath: "docs/encoding.md",
757 });
758
759 assert.strictEqual(contents.oldContents, "before\uFFFDafter");
760 }),
761 );
762
763 it.effect("ends the diff on a page with no files rather than asking for it again", () =>
764 Effect.gen(function* () {
765 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
766 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
767
768 const diff = yield* cli.getMergeRequestDiff({
769 cwd: "/w",
770 repository: "acme/web",
771 number: 7,
772 cursor: "4",
773 });
774
775 assert.strictEqual(diff.patch, "");
776 assert.isNull(diff.nextCursor);
777 }),
778 );
779
780 it.effect("fails a diff page cut off mid-JSON rather than calling the diff whole", () =>
781 Effect.gen(function* () {
782 mockedExecute.mockReturnValueOnce(
783 Effect.succeed({ ...output('[{"old_path":"src/x.ts","new_p'), stdoutTruncated: true }),
784 );
785 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
786
787 const error = yield* Effect.flip(
788 cli.getMergeRequestDiff({ cwd: "/w", repository: "acme/web", number: 7 }),
789 );
790
791 assert.strictEqual(error._tag, "GitLabMergeRequestReadError");
792 }),
793 );
794
795 it.effect("offers no squash when the project does not say it allows one", () =>
796 Effect.gen(function* () {
797 mockedExecute.mockReturnValueOnce(
798 Effect.succeed(output(JSON.stringify({ merge_method: "merge" }))),
799 );
800 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
801
802 const capabilities = yield* cli.getProjectMergeCapabilities({
803 cwd: "/w",
804 repository: "acme/web",
805 });
806
807 assert.deepStrictEqual(capabilities, { merge: true, squash: false, rebase: false });
808 }),
809 );
810
811 it.effect("reads the project's merge settings as its merge capabilities", () =>
812 Effect.gen(function* () {
813 mockedExecute.mockReturnValueOnce(
814 Effect.succeed(output(JSON.stringify({ merge_method: "ff", squash_option: "never" }))),
815 );
816 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
817
818 const capabilities = yield* cli.getProjectMergeCapabilities({
819 cwd: "/w",
820 repository: "acme/web",
821 });
822
823 assert.deepStrictEqual(capabilities, { merge: false, squash: false, rebase: true });
824 }),
825 );
826
827 it.effect("asks the detail read for the divergence GitLab withholds by default", () =>
828 Effect.gen(function* () {
829 mockedExecute.mockReturnValueOnce(Effect.succeed(output('{"message":"404 Not Found"}')));
830 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
831
832 yield* Effect.ignore(
833 cli.getMergeRequestDetail({ cwd: "/w", repository: "acme/web", number: 7 }),
834 );
835
836 expect(argsOfCall(0)[1]).toBe(
837 "projects/acme%2Fweb/merge_requests/7?include_diverged_commits_count=true",
838 );
839 }),
840 );
841
842 it.effect("fails the read when GitLab returns something unreadable", () =>
843 Effect.gen(function* () {
844 mockedExecute.mockReturnValueOnce(Effect.succeed(output('{"message":"404 Not Found"}')));
845 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
846
847 const error = yield* Effect.flip(
848 cli.getMergeRequestDetail({ cwd: "/w", repository: "acme/web", number: 7 }),
849 );
850
851 assert.strictEqual(error._tag, "GitLabMergeRequestReadError");
852 }),
853 );
854
855 it.effect("fails when the authenticated account has no username", () =>
856 Effect.gen(function* () {
857 mockedExecute.mockReturnValueOnce(Effect.succeed(output(JSON.stringify({ username: "" }))));
858 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
859
860 const error = yield* Effect.flip(cli.getViewerUsername({ cwd: "/w" }));
861
862 assert.strictEqual(error._tag, "GitLabViewerUnavailableError");
863 }),
864 );
865
866 it.effect("walks the notes until GitLab answers with a short page", () =>
867 Effect.gen(function* () {
868 mockedExecute.mockReturnValueOnce(Effect.succeed(output(notes(100, 1))));
869 mockedExecute.mockReturnValueOnce(Effect.succeed(output(notes(2, 101))));
870 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
871
872 const { comments, truncated } = yield* cli.listNotes({
873 cwd: "/w",
874 repository: "acme/web",
875 number: 7,
876 });
877
878 expect(argsOfCall(0).join(" ")).toContain("page=1");
879 expect(argsOfCall(1).join(" ")).toContain("page=2");
880 assert.strictEqual(comments.length, 102);
881 assert.isFalse(truncated);
882 }),
883 );
884
885 it.effect("stops the note walk at its bound and says the conversation was cut short", () =>
886 Effect.gen(function* () {
887 mockedExecute.mockReturnValue(Effect.succeed(output(notes(100, 1))));
888 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
889
890 const { truncated } = yield* cli.listNotes({
891 cwd: "/w",
892 repository: "acme/web",
893 number: 7,
894 });
895
896 assert.strictEqual(mockedExecute.mock.calls.length, 10);
897 assert.isTrue(truncated);
898 }),
899 );
900
901 it.effect("reads a positioned discussion as a thread anchored to its line", () =>
902 Effect.gen(function* () {
903 mockedExecute.mockReturnValueOnce(
904 Effect.succeed(
905 output(
906 JSON.stringify([
907 {
908 id: "abc123",
909 notes: [
910 {
911 id: 1,
912 body: "rename this",
913 author: { username: "bilal", avatar_url: "https://avatars/b.png" },
914 created_at: "2026-07-01T00:00:00Z",
915 resolvable: true,
916 resolved: true,
917 position: {
918 position_type: "text",
919 new_path: "src/a.ts",
920 old_path: "src/a.ts",
921 new_line: 12,
922 old_line: null,
923 },
924 },
925 {
926 id: 2,
927 body: "done",
928 author: { username: "julius" },
929 created_at: "2026-07-01T01:00:00Z",
930 },
931 ],
932 },
933 { id: "def456", notes: [{ id: 3, body: "ship it", created_at: "2026-07-01Z" }] },
934 ]),
935 ),
936 ),
937 );
938 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
939
940 const { threads } = yield* cli.listDiscussions({
941 cwd: "/w",
942 repository: "acme/web",
943 number: 7,
944 });
945
946 assert.strictEqual(threads.length, 1);
947 expect(threads[0]).toMatchObject({
948 id: "abc123",
949 path: "src/a.ts",
950 line: 12,
951 side: "right",
952 isResolved: true,
953 });
954 assert.strictEqual(threads[0]?.comments.length, 2);
955 }),
956 );
957
958 it.effect("sends a review as its comments, then its summary, then the verdict", () =>
959 Effect.gen(function* () {
960 mockedExecute.mockReturnValueOnce(
961 Effect.succeed(
962 output(
963 JSON.stringify({
964 iid: 7,
965 title: "t",
966 web_url: "https://gitlab.com/acme/web/-/merge_requests/7",
967 source_branch: "feat",
968 target_branch: "main",
969 created_at: "2026-07-01T00:00:00Z",
970 updated_at: "2026-07-01T00:00:00Z",
971 diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" },
972 }),
973 ),
974 ),
975 );
976 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
977 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
978
979 yield* cli.submitReview({
980 cwd: "/w",
981 repository: "acme/web",
982 number: 7,
983 verdict: "approve",
984 body: "Looks right.",
985 comments: [
986 {
987 path: "src/b.ts",
988 oldPath: "src/a.ts",
989 position: { kind: "deleted", oldLine: 4 },
990 body: "why remove?",
991 },
992 ],
993 });
994
995 expect(argsOfCall(0)[1]).toContain("merge_requests/7");
996 expect(argsOfCall(1)[1]).toContain("/discussions");
997 expect(JSON.parse(callAt(1).stdin ?? "")).toEqual({
998 body: "why remove?",
999 position: {
1000 base_sha: "base",
1001 head_sha: "head",
1002 start_sha: "start",
1003 position_type: "text",
1004 // A renamed file is the only case the two differ, and GitLab cannot place a
1005 // position that names the same path on both sides of the rename.
1006 old_path: "src/a.ts",
1007 new_path: "src/b.ts",
1008 old_line: 4,
1009 },
1010 });
1011 expect(argsOfCall(2)[1]).toContain("/notes");
1012 expect(argsOfCall(3)[1]).toContain("/approve");
1013 }),
1014 );
1015
1016 it.effect("does not ask for diff revisions when a review carries no line comments", () =>
1017 Effect.gen(function* () {
1018 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
1019 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1020
1021 yield* cli.submitReview({
1022 cwd: "/w",
1023 repository: "acme/web",
1024 number: 7,
1025 verdict: "comment",
1026 body: "One thought.",
1027 comments: [],
1028 });
1029
1030 assert.strictEqual(mockedExecute.mock.calls.length, 1);
1031 expect(argsOfCall(0)[1]).toContain("/notes");
1032 }),
1033 );
1034
1035 it.effect("resolves a discussion in place rather than posting to it", () =>
1036 Effect.gen(function* () {
1037 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
1038 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1039
1040 yield* cli.setDiscussionResolution({
1041 cwd: "/w",
1042 repository: "acme/web",
1043 number: 7,
1044 discussionId: "abc123",
1045 resolved: true,
1046 });
1047
1048 expect(argsOfCall(0)).toContain("--method");
1049 expect(argsOfCall(0)).toContain("PUT");
1050 expect(argsOfCall(0)[1]).toContain("/discussions/abc123");
1051 expect(JSON.parse(callAt(0).stdin ?? "")).toEqual({ resolved: true });
1052 }),
1053 );
1054
1055 it.effect("awards an emoji through a POST naming it, not a body", () =>
1056 Effect.gen(function* () {
1057 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
1058 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1059
1060 yield* cli.setReaction({
1061 cwd: "/w",
1062 repository: "acme/web",
1063 number: 7,
1064 content: "thumbs-up",
1065 reacted: true,
1066 });
1067
1068 assert.strictEqual(mockedExecute.mock.calls.length, 1);
1069 expect(argsOfCall(0)).toEqual([
1070 "api",
1071 "projects/acme%2Fweb/merge_requests/7/award_emoji?name=thumbsup",
1072 "--method",
1073 "POST",
1074 ]);
1075 }),
1076 );
1077
1078 it.effect("removes an award by listing them and deleting the reader's own id", () =>
1079 Effect.gen(function* () {
1080 mockedExecute.mockReturnValueOnce(
1081 Effect.succeed(output(JSON.stringify({ username: "bilal" }))),
1082 );
1083 mockedExecute.mockReturnValueOnce(
1084 Effect.succeed(
1085 output(
1086 JSON.stringify([
1087 { id: 5, name: "thumbsup", user: { username: "bilal" } },
1088 { id: 6, name: "thumbsup", user: { username: "julius" } },
1089 ]),
1090 ),
1091 ),
1092 );
1093 mockedExecute.mockReturnValueOnce(Effect.succeed(output("{}")));
1094 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1095
1096 yield* cli.setReaction({
1097 cwd: "/w",
1098 repository: "acme/web",
1099 number: 7,
1100 content: "thumbs-up",
1101 reacted: false,
1102 });
1103
1104 assert.strictEqual(mockedExecute.mock.calls.length, 3);
1105 expect(argsOfCall(2)).toEqual([
1106 "api",
1107 "projects/acme%2Fweb/merge_requests/7/award_emoji/5",
1108 "--method",
1109 "DELETE",
1110 ]);
1111 }),
1112 );
1113
1114 it.effect("does nothing when the reader has no award of that name to take back", () =>
1115 Effect.gen(function* () {
1116 mockedExecute.mockReturnValueOnce(
1117 Effect.succeed(output(JSON.stringify({ username: "bilal" }))),
1118 );
1119 mockedExecute.mockReturnValueOnce(Effect.succeed(output("[]")));
1120 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1121
1122 yield* cli.setReaction({
1123 cwd: "/w",
1124 repository: "acme/web",
1125 number: 7,
1126 content: "thumbs-up",
1127 reacted: false,
1128 });
1129
1130 assert.strictEqual(mockedExecute.mock.calls.length, 2);
1131 }),
1132 );
1133
1134 it.effect("names a merge request with no diff revisions rather than calling it unreadable", () =>
1135 Effect.gen(function* () {
1136 mockedExecute.mockReturnValueOnce(
1137 Effect.succeed(
1138 output(
1139 JSON.stringify({
1140 iid: 7,
1141 title: "t",
1142 web_url: "https://gitlab.com/acme/web/-/merge_requests/7",
1143 source_branch: "feat",
1144 target_branch: "main",
1145 created_at: "2026-07-01T00:00:00Z",
1146 updated_at: "2026-07-01T00:00:00Z",
1147 diff_refs: null,
1148 }),
1149 ),
1150 ),
1151 );
1152 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1153
1154 const error = yield* Effect.flip(
1155 cli.submitReview({
1156 cwd: "/w",
1157 repository: "acme/web",
1158 number: 7,
1159 verdict: "comment",
1160 body: "",
1161 comments: [{ path: "src/a.ts", position: { kind: "added", newLine: 4 }, body: "nit" }],
1162 }),
1163 );
1164
1165 assert.strictEqual(error._tag, "GitLabDiffRefsUnavailableError");
1166 }),
1167 );
1168
1169 it.effect("reads who has access to the project and who is already on the merge request", () =>
1170 Effect.gen(function* () {
1171 mockedExecute
1172 .mockReturnValueOnce(Effect.succeed(output(mergeRequestJson({ reviewers: [reviewer] }))))
1173 .mockReturnValueOnce(
1174 Effect.succeed(output(JSON.stringify([author, reviewer, { id: 9, username: "hubot" }]))),
1175 );
1176 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1177
1178 const list = yield* cli.listReviewerCandidates({
1179 cwd: "/w",
1180 repository: "acme/web",
1181 number: 7,
1182 });
1183
1184 expect(argsOfCall(1)[1]).toBe("projects/acme%2Fweb/users?per_page=100");
1185 expect(list.candidates.map((candidate) => [candidate.id, candidate.isRequested])).toEqual([
1186 ["5", true],
1187 ["9", false],
1188 ]);
1189 assert.isFalse(list.truncated);
1190 }),
1191 );
1192
1193 it.effect("writes the reviewer set back with the one being asked added to it", () =>
1194 Effect.gen(function* () {
1195 mockedExecute
1196 .mockReturnValueOnce(Effect.succeed(output(mergeRequestJson({ reviewers: [reviewer] }))))
1197 .mockReturnValueOnce(Effect.succeed(output("{}")));
1198 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1199
1200 yield* cli.setReviewerRequest({
1201 cwd: "/w",
1202 repository: "acme/web",
1203 number: 7,
1204 reviewers: [{ id: "9" }],
1205 requested: true,
1206 });
1207
1208 // GitLab replaces the whole set, so the reviewer already on the merge request has to be
1209 // sent back with the new one or the request would take them off it.
1210 expect(argsOfCall(1)).toContain("PUT");
1211 expect(JSON.parse(callAt(1).stdin ?? "")).toEqual({ reviewer_ids: [5, 9] });
1212 }),
1213 );
1214
1215 it.effect("takes a reviewer out of the set rather than clearing it", () =>
1216 Effect.gen(function* () {
1217 mockedExecute
1218 .mockReturnValueOnce(
1219 Effect.succeed(
1220 output(mergeRequestJson({ reviewers: [reviewer, { id: 9, username: "hubot" }] })),
1221 ),
1222 )
1223 .mockReturnValueOnce(Effect.succeed(output("{}")));
1224 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1225
1226 yield* cli.setReviewerRequest({
1227 cwd: "/w",
1228 repository: "acme/web",
1229 number: 7,
1230 reviewers: [{ id: "9" }],
1231 requested: false,
1232 });
1233
1234 expect(JSON.parse(callAt(1).stdin ?? "")).toEqual({ reviewer_ids: [5] });
1235 }),
1236 );
1237
1238 it.effect("ignores an id GitLab could not have handed out, which names nobody", () =>
1239 Effect.gen(function* () {
1240 mockedExecute
1241 .mockReturnValueOnce(Effect.succeed(output(mergeRequestJson({ reviewers: [reviewer] }))))
1242 .mockReturnValueOnce(Effect.succeed(output("{}")));
1243 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1244
1245 yield* cli.setReviewerRequest({
1246 cwd: "/w",
1247 repository: "acme/web",
1248 number: 7,
1249 reviewers: [{ id: "octocat" }],
1250 requested: true,
1251 });
1252
1253 expect(JSON.parse(callAt(1).stdin ?? "")).toEqual({ reviewer_ids: [5] });
1254 }),
1255 );
1256
1257 it.effect("rewrites a title without touching the description", () =>
1258 Effect.gen(function* () {
1259 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
1260 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1261
1262 yield* cli.updateMergeRequest({
1263 cwd: "/w",
1264 repository: "acme/web",
1265 number: 7,
1266 title: "A better title",
1267 });
1268
1269 expect(argsOfCall(0)).toEqual([
1270 "api",
1271 "projects/acme%2Fweb/merge_requests/7",
1272 "--method",
1273 "PUT",
1274 "--input",
1275 "-",
1276 "--header",
1277 "Content-Type: application/json",
1278 ]);
1279 expect(JSON.parse(callAt(0).stdin ?? "")).toEqual({ title: "A better title" });
1280 }),
1281 );
1282
1283 it.effect("sends a rewritten body as GitLab's description, and nothing else", () =>
1284 Effect.gen(function* () {
1285 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
1286 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1287
1288 yield* cli.updateMergeRequest({
1289 cwd: "/w",
1290 repository: "acme/web",
1291 number: 7,
1292 description: "What this changes.",
1293 });
1294
1295 expect(JSON.parse(callAt(0).stdin ?? "")).toEqual({ description: "What this changes." });
1296 }),
1297 );
1298
1299 it.effect("rewrites title and description together in one request", () =>
1300 Effect.gen(function* () {
1301 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
1302 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1303
1304 yield* cli.updateMergeRequest({
1305 cwd: "/w",
1306 repository: "acme/web",
1307 number: 7,
1308 title: "A better title",
1309 description: "What this changes.",
1310 });
1311
1312 assert.strictEqual(mockedExecute.mock.calls.length, 1);
1313 expect(JSON.parse(callAt(0).stdin ?? "")).toEqual({
1314 title: "A better title",
1315 description: "What this changes.",
1316 });
1317 }),
1318 );
1319
1320 it.effect("rewrites a note in place through the note it names", () =>
1321 Effect.gen(function* () {
1322 mockedExecute.mockReturnValue(Effect.succeed(output("{}")));
1323 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1324
1325 yield* cli.updateNote({
1326 cwd: "/w",
1327 repository: "acme/web",
1328 number: 7,
1329 noteId: "42",
1330 body: "true",
1331 });
1332
1333 expect(argsOfCall(0)).toEqual([
1334 "api",
1335 "projects/acme%2Fweb/merge_requests/7/notes/42",
1336 "--method",
1337 "PUT",
1338 "--input",
1339 "-",
1340 "--header",
1341 "Content-Type: application/json",
1342 ]);
1343 expect(callAt(0).stdin).toBe('{"body":"true"}');
1344 }),
1345 );
1346 it.effect("reads blob ids for the marked paths at the merge request's head", () =>
1347 Effect.gen(function* () {
1348 mockedExecute.mockReturnValueOnce(
1349 Effect.succeed(
1350 output(
1351 JSON.stringify({
1352 iid: 7,
1353 title: "t",
1354 web_url: "https://gitlab.com/acme/web/-/merge_requests/7",
1355 source_branch: "feat",
1356 target_branch: "main",
1357 created_at: "2026-07-01T00:00:00Z",
1358 updated_at: "2026-07-01T00:00:00Z",
1359 diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" },
1360 }),
1361 ),
1362 ),
1363 );
1364 mockedExecute.mockReturnValueOnce(
1365 Effect.succeed(
1366 output(
1367 JSON.stringify({
1368 data: {
1369 project: { repository: { blobs: { nodes: [{ path: "src/a.ts", oid: "aaa" }] } } },
1370 },
1371 }),
1372 ),
1373 ),
1374 );
1375 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1376
1377 const revisions = yield* cli.getFileRevisions({
1378 cwd: "/w",
1379 repository: "acme/web",
1380 number: 7,
1381 paths: ["src/a.ts", "src/gone.ts"],
1382 });
1383
1384 expect([...revisions]).toEqual([
1385 ["src/a.ts", "aaa"],
1386 ["src/gone.ts", ""],
1387 ]);
1388 const body: unknown = JSON.parse(callAt(1).stdin ?? "{}");
1389 expect(body).toMatchObject({
1390 variables: { fullPath: "acme/web", ref: "head", paths: ["src/a.ts", "src/gone.ts"] },
1391 });
1392 }),
1393 );
1394
1395 it.effect("leaves the paths out when GitLab did not answer the blobs query", () =>
1396 Effect.gen(function* () {
1397 mockedExecute.mockReturnValueOnce(
1398 Effect.succeed(
1399 output(
1400 JSON.stringify({
1401 iid: 7,
1402 title: "t",
1403 web_url: "https://gitlab.com/acme/web/-/merge_requests/7",
1404 source_branch: "feat",
1405 target_branch: "main",
1406 created_at: "2026-07-01T00:00:00Z",
1407 updated_at: "2026-07-01T00:00:00Z",
1408 diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" },
1409 }),
1410 ),
1411 ),
1412 );
1413 mockedExecute.mockReturnValueOnce(
1414 Effect.succeed(output(JSON.stringify({ data: { project: null } }))),
1415 );
1416 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1417
1418 const revisions = yield* cli.getFileRevisions({
1419 cwd: "/w",
1420 repository: "acme/web",
1421 number: 7,
1422 paths: ["src/a.ts", "src/b.ts"],
1423 });
1424
1425 expect([...revisions]).toEqual([]);
1426 }),
1427 );
1428
1429 it.effect("asks GitLab nothing when no file is marked", () =>
1430 Effect.gen(function* () {
1431 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1432
1433 const revisions = yield* cli.getFileRevisions({
1434 cwd: "/w",
1435 repository: "acme/web",
1436 number: 7,
1437 paths: [],
1438 });
1439
1440 expect([...revisions]).toEqual([]);
1441 expect(mockedExecute).not.toHaveBeenCalled();
1442 }),
1443 );
1444
1445 it.effect("splits the paths across requests, because GitLab charges the query by how many", () =>
1446 Effect.gen(function* () {
1447 const paths = Array.from({ length: 150 }, (_, index) => `src/${index}.ts`);
1448 mockedExecute.mockReturnValueOnce(
1449 Effect.succeed(
1450 output(
1451 JSON.stringify({
1452 iid: 7,
1453 title: "t",
1454 web_url: "https://gitlab.com/acme/web/-/merge_requests/7",
1455 source_branch: "feat",
1456 target_branch: "main",
1457 created_at: "2026-07-01T00:00:00Z",
1458 updated_at: "2026-07-01T00:00:00Z",
1459 diff_refs: { base_sha: "base", head_sha: "head", start_sha: "start" },
1460 }),
1461 ),
1462 ),
1463 );
1464 mockedExecute.mockImplementation((request) => {
1465 const body = JSON.parse(request.stdin ?? "{}") as {
1466 readonly variables: { readonly paths: ReadonlyArray<string> };
1467 };
1468 return Effect.succeed(
1469 output(
1470 JSON.stringify({
1471 data: {
1472 project: {
1473 repository: {
1474 blobs: {
1475 nodes: body.variables.paths.map((path) => ({ path, oid: `oid-${path}` })),
1476 },
1477 },
1478 },
1479 },
1480 }),
1481 ),
1482 );
1483 });
1484 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1485
1486 const revisions = yield* cli.getFileRevisions({
1487 cwd: "/w",
1488 repository: "acme/web",
1489 number: 7,
1490 paths,
1491 });
1492
1493 assert.strictEqual(revisions.size, 150);
1494 assert.strictEqual(revisions.get("src/149.ts"), "oid-src/149.ts");
1495 assert.strictEqual(mockedExecute.mock.calls.length, 3);
1496 }),
1497 );
1498});