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