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

gitLabMergeRequestJson.test.ts

793 lines23.7 KB

1import * as Result from "effect/Result";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 decodeAwardEmojiJson,
6 decodeRepositoryBlobsJson,
7 decodeCommitsJson,
8 decodeMergeRequestDetailJson,
9 decodeMergeRequestDiffsJson,
10 decodeMergeRequestListJson,
11 decodeMergeRequestFileStatsJson,
12 decodeMergeRequestStatsJson,
13 decodeNotesJson,
14 decodeOwnAwardIdJson,
15 decodeReviewDecisionsJson,
16 decodeTriggerJobChecksJson,
17} from "./gitLabMergeRequestJson.ts";
18​
19function listJson(entries: ReadonlyArray<Record<string, unknown>>): string {
20 return JSON.stringify(
21 entries.map((entry) => ({
22 iid: 1,
23 title: "Add the merge requests page",
24 web_url: "https://gitlab.com/acme/web/-/merge_requests/1",
25 source_branch: "feat/page",
26 target_branch: "main",
27 created_at: "2026-07-01T00:00:00Z",
28 updated_at: "2026-07-02T00:00:00Z",
29 ...entry,
30 })),
31 );
33​
34function detailJson(entry: Record<string, unknown>): string {
35 return JSON.stringify({
36 iid: 1,
37 title: "Add the merge requests page",
38 web_url: "https://gitlab.com/acme/web/-/merge_requests/1",
39 source_branch: "feat/page",
40 target_branch: "main",
41 created_at: "2026-07-01T00:00:00Z",
42 updated_at: "2026-07-02T00:00:00Z",
43 ...entry,
44 });
46​
47function expectSuccess<A>(result: Result.Result<A, unknown>): A {
48 expect(Result.isSuccess(result)).toBe(true);
49 if (!Result.isSuccess(result)) throw new Error("expected a successful decode");
50 return result.success;
52​
53describe("decodeMergeRequestListJson", () => {
54 it("reads a merge request as a change request", () => {
55 const batch = expectSuccess(
56 decodeMergeRequestListJson(
57 listJson([
58 {
59 iid: 42,
60 author: { username: "bilal", name: "Bilal" },
61 state: "opened",
62 merge_status: "can_be_merged",
63 draft: false,
64 reviewers: [{ username: "julius" }],
65 labels: ["backend", " "],
66 },
67 ]),
68 ),
69 );
70​
71 expect(batch.items).toHaveLength(1);
72 expect(batch.items[0]).toMatchObject({
73 number: 42,
74 author: { login: "bilal", name: "Bilal" },
75 headBranch: "feat/page",
76 baseBranch: "main",
77 state: "open",
78 isDraft: false,
79 mergeability: "mergeable",
80 reviewRequestLogins: ["julius"],
81 labels: [{ name: "backend", color: null }],
82 });
83 });
84​
85 it("treats a merged timestamp as merged whatever the state says", () => {
86 const batch = expectSuccess(
87 decodeMergeRequestListJson(
88 listJson([{ state: "opened", merged_at: "2026-07-03T00:00:00Z" }]),
89 ),
90 );
91​
92 expect(batch.items[0]?.state).toBe("merged");
93 });
94​
95 it("reads the legacy draft flag", () => {
96 const batch = expectSuccess(decodeMergeRequestListJson(listJson([{ work_in_progress: true }])));
97​
98 expect(batch.items[0]?.isDraft).toBe(true);
99 });
100​
101 it("calls a conflicted merge request conflicting even while the merge check is pending", () => {
102 const batch = expectSuccess(
103 decodeMergeRequestListJson(listJson([{ merge_status: "checking", has_conflicts: true }])),
104 );
105​
106 expect(batch.items[0]?.mergeability).toBe("conflicting");
107 });
108​
109 it("skips a malformed row but still counts it, so paging does not stop early", () => {
110 const batch = expectSuccess(
111 decodeMergeRequestListJson(
112 JSON.stringify([{ iid: "not a number" }, ...JSON.parse(listJson([{}]))]),
113 ),
114 );
115​
116 expect(batch.items).toHaveLength(1);
117 expect(batch.rawIndexes).toEqual([1]);
118 expect(batch.rawCount).toBe(2);
119 });
120});
121​
122describe("decodeMergeRequestDetailJson", () => {
123 it("reads the description, file count and pipeline", () => {
124 const detail = expectSuccess(
125 decodeMergeRequestDetailJson(
126 detailJson({
127 description: "Ships the page.",
128 changes_count: "3",
129 reviewers: [{ username: "julius", name: "Julius" }],
130 head_pipeline: {
131 status: "success",
132 web_url: "https://gitlab.com/acme/web/-/pipelines/9",
133 source: "merge_request_event",
134 },
135 }),
136 ),
137 );
138​
139 expect(detail.body).toBe("Ships the page.");
140 expect(detail.changedFiles).toBe(3);
141 expect(detail.reviewers).toEqual([{ login: "julius", name: "Julius", avatarUrl: null }]);
142 expect(detail.checks).toEqual([
143 {
144 name: "Pipeline",
145 status: "success",
146 description: "merge_request_event",
147 url: "https://gitlab.com/acme/web/-/pipelines/9",
148 },
149 ]);
150 });
151​
152 it.each(["canceled", "canceling"])("maps a %s pipeline to cancelled", (status) => {
153 const detail = expectSuccess(
154 decodeMergeRequestDetailJson(detailJson({ head_pipeline: { status } })),
155 );
156​
157 expect(detail.checks[0]?.status).toBe("cancelled");
158 });
159​
160 it("reads an uncounted change set as its floor", () => {
161 const detail = expectSuccess(
162 decodeMergeRequestDetailJson(detailJson({ changes_count: "1000+" })),
163 );
164​
165 expect(detail.changedFiles).toBe(1000);
166 });
167​
168 it("reads the head commit, and says nothing where GitLab omitted it", () => {
169 const headSha = (entry: Record<string, unknown>) =>
170 expectSuccess(decodeMergeRequestDetailJson(detailJson(entry))).headSha;
171 const sha = "afce4da4d5fe7d2fd0647b5b864526ea0a2827cb";
172​
173 expect(headSha({ sha })).toBe(sha);
174 expect(headSha({ sha, diff_refs: null })).toBe(sha);
175 expect(headSha({ sha, merge_commit_sha: "0".repeat(40) })).toBe(sha);
176 expect(headSha({})).toBeUndefined();
177 expect(headSha({ sha: null })).toBeUndefined();
178 expect(headSha({ sha: " " })).toBeUndefined();
179 });
180​
181 it("reads either auto-merge field, and says nothing where GitLab named neither", () => {
182 const armed = (entry: Record<string, unknown>) =>
183 expectSuccess(decodeMergeRequestDetailJson(detailJson(entry))).autoMergeEnabled;
184​
185 expect(armed({ merge_when_pipeline_succeeds: true })).toBe(true);
186 expect(armed({ auto_merge_enabled: true })).toBe(true);
187 expect(armed({ merge_when_pipeline_succeeds: false })).toBe(false);
188 expect(armed({})).toBeUndefined();
189 });
190​
191 it("keeps the squash choice stored with auto-merge", () => {
192 expect(
193 expectSuccess(
194 decodeMergeRequestDetailJson(
195 detailJson({ auto_merge_enabled: true, squash_on_merge: true }),
196 ),
197 ),
198 ).toMatchObject({ autoMergeEnabled: true, autoMergeMethod: "squash" });
199​
200 expect(
201 expectSuccess(
202 decodeMergeRequestDetailJson(
203 detailJson({
204 auto_merge_enabled: true,
205 squash: true,
206 squash_on_merge: false,
207 }),
208 ),
209 ).autoMergeMethod,
210 ).toBeUndefined();
211 });
212​
213 it("keeps a divergence GitLab did not count apart from a divergence of none", () => {
214 const behind = (entry: Record<string, unknown>) =>
215 expectSuccess(decodeMergeRequestDetailJson(detailJson(entry))).divergedCommits;
216​
217 expect(behind({ diverged_commits_count: 3 })).toBe(3);
218 expect(behind({ diverged_commits_count: 0 })).toBe(0);
219 expect(behind({})).toBeUndefined();
220 expect(behind({ diverged_commits_count: null })).toBeUndefined();
221 });
222​
223 it.each(["scheduled", " SCHEDULED "])("keeps a delayed pipeline pending: %s", (status) => {
224 const detail = expectSuccess(
225 decodeMergeRequestDetailJson(detailJson({ head_pipeline: { status } })),
226 );
227​
228 expect(detail.checks[0]?.status).toBe("pending");
229 });
230​
231 it("maps a pipeline waiting on a person to neutral, not failure", () => {
232 const detail = expectSuccess(
233 decodeMergeRequestDetailJson(detailJson({ head_pipeline: { status: "manual" } })),
234 );
235​
236 expect(detail.checks[0]?.status).toBe("neutral");
237 });
238});
239​
240describe("decodeNotesJson", () => {
241 it("keeps comments and drops GitLab's own activity notes", () => {
242 const notes = expectSuccess(
243 decodeNotesJson(
244 JSON.stringify([
245 {
246 id: 1,
247 body: "assigned to @bilal",
248 system: true,
249 created_at: "2026-07-01T00:00:00Z",
250 },
251 {
252 id: 2,
253 body: "Looks good.",
254 author: { username: "julius" },
255 created_at: "2026-07-02T00:00:00Z",
256 },
257 { id: 3, body: " ", created_at: "2026-07-03T00:00:00Z" },
258 ]),
259 ),
260 );
261​
262 expect(notes.comments).toHaveLength(1);
263 expect(notes.comments[0]).toMatchObject({
264 id: "2",
265 kind: "issue-comment",
266 body: "Looks good.",
267 });
268 expect(notes.rawCount).toBe(3);
269 });
270​
271 it("reads a line note as a review comment on its file", () => {
272 const notes = expectSuccess(
273 decodeNotesJson(
274 JSON.stringify([
275 {
276 id: 7,
277 type: "DiffNote",
278 body: "Rename this.",
279 created_at: "2026-07-02T00:00:00Z",
280 position: { new_path: "src/app.ts", old_path: "src/old.ts" },
281 },
282 ]),
283 ),
284 );
285​
286 expect(notes.comments[0]).toMatchObject({ kind: "review-comment", path: "src/app.ts" });
287 });
288​
289 it("falls back to the old path for a note on a deleted line", () => {
290 const notes = expectSuccess(
291 decodeNotesJson(
292 JSON.stringify([
293 {
294 id: 8,
295 type: "DiffNote",
296 body: "Gone.",
297 created_at: "2026-07-02T00:00:00Z",
298 position: { new_path: null, old_path: "src/old.ts" },
299 },
300 ]),
301 ),
302 );
303​
304 expect(notes.comments[0]?.path).toBe("src/old.ts");
305 });
306});
307​
308describe("decodeCommitsJson", () => {
309 it("falls back to the creation timestamp when there is no commit date", () => {
310 const commits = expectSuccess(
311 decodeCommitsJson(
312 JSON.stringify([
313 {
314 id: "aaa",
315 created_at: "2026-07-01T00:00:00+08:00",
316 author_name: "Ada Lovelace",
317 author_email: "ada@example.com",
318 },
319 ]),
320 ),
321 );
322​
323 expect(commits[0]).toMatchObject({
324 oid: "aaa",
325 committedDate: "2026-07-01T00:00:00+08:00",
326 authors: [{ login: "Ada Lovelace", name: "Ada Lovelace", avatarUrl: null }],
327 });
328 });
329});
330​
331describe("decodeMergeRequestDiffsJson", () => {
332 it("quotes literal backslashes without interpreting them as escapes", () => {
333 const result = expectSuccess(
334 decodeMergeRequestDiffsJson(
335 JSON.stringify([
336 {
337 old_path: String.raw`src\notes.ts`,
338 new_path: String.raw`src\notes.ts`,
339 diff: "@@ -1 +1 @@\n-old\n+new",
340 },
341 ]),
342 ),
343 );
344​
345 expect(result.patch).toBe(
346 [
347 String.raw`diff --git "a/src\\notes.ts" "b/src\\notes.ts"`,
348 String.raw`--- "a/src\\notes.ts"`,
349 String.raw`+++ "b/src\\notes.ts"`,
350 "@@ -1 +1 @@",
351 "-old",
352 "+new",
353 "",
354 ].join("\n"),
355 );
356 });
357​
358 it("preserves spaces and literal backslashes in both rename paths", () => {
359 const result = expectSuccess(
360 decodeMergeRequestDiffsJson(
361 JSON.stringify([
362 {
363 old_path: String.raw` old\name.ts `,
364 new_path: String.raw` new\name.ts `,
365 renamed_file: true,
366 diff: "",
367 },
368 ]),
369 ),
370 );
371​
372 expect(result.patch).toBe(
373 [
374 String.raw`diff --git "a/ old\\name.ts " "b/ new\\name.ts "`,
375 "similarity index 100%",
376 String.raw`rename from " old\\name.ts "`,
377 String.raw`rename to " new\\name.ts "`,
378 String.raw`--- "a/ old\\name.ts "`,
379 String.raw`+++ "b/ new\\name.ts "`,
380 ].join("\n"),
381 );
382 });
383​
384 it("assembles a unified patch GitLab does not return", () => {
385 const result = expectSuccess(
386 decodeMergeRequestDiffsJson(
387 JSON.stringify([
388 {
389 old_path: "src/app.ts",
390 new_path: "src/app.ts",
391 diff: "@@ -1 +1 @@\n-old\n+new\n",
392 },
393 ]),
394 ),
395 );
396​
397 expect(result.patch).toBe(
398 [
399 "diff --git a/src/app.ts b/src/app.ts",
400 "--- a/src/app.ts",
401 "+++ b/src/app.ts",
402 "@@ -1 +1 @@",
403 "-old",
404 "+new",
405 "",
406 ].join("\n"),
407 );
408 expect(result.truncated).toBe(false);
409 });
410​
411 it("points a new file at /dev/null on the left and a deleted file on the right", () => {
412 const result = expectSuccess(
413 decodeMergeRequestDiffsJson(
414 JSON.stringify([
415 {
416 old_path: "src/new.ts",
417 new_path: "src/new.ts",
418 new_file: true,
419 b_mode: "100755",
420 diff: "@@ -0,0 +1 @@\n+hello\n",
421 },
422 {
423 old_path: "src/gone.ts",
424 new_path: "src/gone.ts",
425 deleted_file: true,
426 diff: "@@ -1 +0,0 @@\n-bye\n",
427 },
428 ]),
429 ),
430 );
431​
432 expect(result.patch).toContain("new file mode 100755");
433 expect(result.patch).toContain("--- /dev/null");
434 expect(result.patch).toContain("deleted file mode 100644");
435 expect(result.patch).toContain("+++ /dev/null");
436 });
437​
438 it("reports truncation for a file GitLab refused to inline", () => {
439 const result = expectSuccess(
440 decodeMergeRequestDiffsJson(
441 JSON.stringify([{ old_path: "big.bin", new_path: "big.bin", diff: "", too_large: true }]),
442 ),
443 );
444​
445 expect(result.truncated).toBe(true);
446 expect(result.patch).toContain("diff --git a/big.bin b/big.bin");
447 });
448​
449 it("reports how many files GitLab returned, so the caller can page", () => {
450 const result = expectSuccess(
451 decodeMergeRequestDiffsJson(
452 JSON.stringify(
453 Array.from({ length: 3 }, (_, index) => ({
454 old_path: `src/${index}.ts`,
455 new_path: `src/${index}.ts`,
456 diff: "@@ -1 +1 @@\n-a\n+b\n",
457 })),
458 ),
459 ),
460 );
461​
462 expect(result.rawCount).toBe(3);
463 expect(result.truncated).toBe(false);
464 expect(result.patch).toContain("src/2.ts");
465 });
466});
467​
468describe("decodeTriggerJobChecksJson", () => {
469 it("reads a downstream failure GitLab was told to allow as neutral, not failure", () => {
470 const result = expectSuccess(
471 decodeTriggerJobChecksJson(
472 JSON.stringify([
473 { name: "e2e", allow_failure: true, downstream_pipeline: { status: "failed" } },
474 { name: "deploy", allow_failure: false, downstream_pipeline: { status: "running" } },
475 { name: 7 },
476 ]),
477 ),
478 );
479​
480 expect(result.checks.map((check) => [check.name, check.status])).toEqual([
481 ["e2e", "neutral"],
482 ["deploy", "pending"],
483 ]);
484 expect(result.rawCount).toBe(3);
485 });
486});
487​
488describe("merge request viewer fields", () => {
489 it("leaves merging permitted where GitLab answered without the field", () => {
490 expect(expectSuccess(decodeMergeRequestDetailJson(detailJson({}))).viewerCanMerge).toBe(true);
491 expect(
492 expectSuccess(decodeMergeRequestDetailJson(detailJson({ user: null }))).viewerCanMerge,
493 ).toBe(true);
494 });
495});
496​
497describe("decodeAwardEmojiJson", () => {
498 it("reads MR-level awards, keys per-note awards by the REST id inside their gid, and ignores an award outside the eight", () => {
499 const result = expectSuccess(
500 decodeAwardEmojiJson(
501 JSON.stringify({
502 data: {
503 currentUser: { username: "bilal" },
504 project: {
505 mergeRequest: {
506 awardEmoji: {
507 nodes: [
508 { name: "thumbsup", user: { username: "bilal" } },
509 { name: "thumbsup", user: { username: "julius" } },
510 ],
511 },
512 notes: {
513 pageInfo: { hasNextPage: false, endCursor: null },
514 nodes: [
515 {
516 id: "gid://gitlab/DiffNote/42",
517 awardEmoji: { nodes: [{ name: "heart", user: { username: "julius" } }] },
518 },
519 {
520 id: "gid://gitlab/Note/7",
521 awardEmoji: {
522 nodes: [{ name: "partyparrot", user: { username: "bilal" } }],
523 },
524 },
525 ],
526 },
527 },
528 },
529 },
530 }),
531 ),
532 );
533​
534 expect(result.reactions).toEqual([
535 { content: "thumbs-up", count: 2, actors: ["julius"], viewerHasReacted: true },
536 ]);
537 expect([...result.reactionsByNoteId]).toEqual([
538 ["42", [{ content: "heart", count: 1, actors: ["julius"], viewerHasReacted: false }]],
539 ]);
540 expect(result.nextCursor).toBeNull();
541 });
542​
543 it("matches the viewer's username case-insensitively, since GitLab is not consistent about case", () => {
544 const result = expectSuccess(
545 decodeAwardEmojiJson(
546 JSON.stringify({
547 data: {
548 currentUser: { username: "Bilal" },
549 project: {
550 mergeRequest: {
551 awardEmoji: {
552 nodes: [
553 { name: "heart", user: { username: "bilal" } },
554 { name: "heart", user: { username: "julius" } },
555 ],
556 },
557 notes: { pageInfo: { hasNextPage: false, endCursor: null }, nodes: [] },
558 },
559 },
560 },
561 }),
562 ),
563 );
564​
565 expect(result.reactions).toEqual([
566 { content: "heart", count: 2, actors: ["julius"], viewerHasReacted: true },
567 ]);
568 });
569});
570​
571describe("decodeOwnAwardIdJson", () => {
572 const awards = JSON.stringify([
573 { id: 101, name: "thumbsup", user: { username: "julius" } },
574 { id: 102, name: "thumbsup", user: { username: "bilal" } },
575 ]);
576​
577 it("finds the reader's own award of that name, which is the one a removal deletes", () => {
578 expect(
579 expectSuccess(decodeOwnAwardIdJson(awards, { content: "thumbs-up", viewer: "bilal" })),
580 ).toBe(102);
581 });
582});
583​
584describe("decodeRepositoryBlobsJson", () => {
585 function expectBlobs(result: Result.Result<ReadonlyMap<string, string> | null, unknown>) {
586 const blobs = expectSuccess(result);
587 expect(blobs).not.toBe(null);
588 if (blobs === null) throw new Error("expected an answered blobs query");
589 return blobs;
590 }
591​
592 it("leaves out a node missing either half, which names no version", () => {
593 const blobs = expectBlobs(
594 decodeRepositoryBlobsJson(
595 JSON.stringify({
596 data: {
597 project: {
598 repository: {
599 blobs: {
600 nodes: [
601 { path: "src/a.ts", oid: null },
602 { path: null, oid: "bbb222" },
603 null,
604 { path: "src/c.ts", oid: "ccc333" },
605 ],
606 },
607 },
608 },
609 },
610 }),
611 ),
612 );
613​
614 expect([...blobs]).toEqual([["src/c.ts", "ccc333"]]);
615 });
616​
617 it("keys a blob by the path the host spelled, spaces and all", () => {
618 const blobs = expectBlobs(
619 decodeRepositoryBlobsJson(
620 JSON.stringify({
621 data: {
622 project: {
623 repository: {
624 blobs: {
625 nodes: [
626 { path: " leading.ts", oid: "aaa111" },
627 { path: "trailing.ts ", oid: "bbb222" },
628 { path: " ", oid: "ccc333" },
629 { path: "", oid: "ddd444" },
630 ],
631 },
632 },
633 },
634 },
635 }),
636 ),
637 );
638​
639 expect([...blobs]).toEqual([
640 [" leading.ts", "aaa111"],
641 ["trailing.ts ", "bbb222"],
642 [" ", "ccc333"],
643 ]);
644 });
645​
646 it("tells a project the reader cannot see from a revision with none of the files", () => {
647 expect(
648 expectSuccess(decodeRepositoryBlobsJson(JSON.stringify({ data: { project: null } }))),
649 ).toBe(null);
650 expect(
651 expectSuccess(
652 decodeRepositoryBlobsJson(JSON.stringify({ data: { project: { repository: null } } })),
653 ),
654 ).toBe(null);
655 expect(
656 expectSuccess(
657 decodeRepositoryBlobsJson(
658 JSON.stringify({ data: { project: { repository: { blobs: { nodes: [] } } } } }),
659 ),
660 ),
661 ).toEqual(new Map());
662 });
663});
664​
665describe("decodeReviewDecisionsJson", () => {
666 const decisionsOf = (enterprise: boolean | null, nodes: ReadonlyArray<unknown>) =>
667 expectSuccess(
668 decodeReviewDecisionsJson(
669 JSON.stringify({
670 data: {
671 metadata: enterprise === null ? null : { enterprise },
672 project: { mergeRequests: { nodes } },
673 },
674 }),
675 ),
676 );
677 const approvedBy = (...usernames: ReadonlyArray<string>) => ({
678 nodes: usernames.map((username) => ({ username })),
679 });
680​
681 it.each([
682 {
683 reading: "an approval short of Enterprise approval rules",
684 enterprise: true,
685 approval: { approved: false, approvedBy: approvedBy("GitLabDuo") },
686 decision: "review-required",
687 },
688 {
689 reading: "Enterprise approval rules no one has approved against",
690 enterprise: true,
691 approval: { approved: false, approvedBy: approvedBy() },
692 decision: "review-required",
693 },
694 {
695 reading: "Enterprise approval rules met",
696 enterprise: true,
697 approval: { approved: true, approvedBy: approvedBy("julius") },
698 decision: "approved",
699 },
700 {
701 reading: "Enterprise without approval rules or approvals",
702 enterprise: true,
703 approval: { approved: true, approvedBy: approvedBy() },
704 decision: null,
705 },
706 {
707 reading: "Community Edition with an approval",
708 enterprise: false,
709 approval: { approved: true, approvedBy: approvedBy("julius") },
710 decision: "approved",
711 },
712 {
713 reading: "Community Edition without approvals",
714 enterprise: false,
715 approval: { approved: false, approvedBy: approvedBy() },
716 decision: null,
717 },
718 ])("reads $reading as $decision", ({ enterprise, approval, decision }) => {
719 expect([...decisionsOf(enterprise, [{ iid: "7", ...approval }])]).toEqual([[7, decision]]);
720 });
721​
722 it("leaves out a merge request it cannot key, and a project the reader cannot see", () => {
723 expect([...decisionsOf(true, [null, { iid: "x", approved: true }])]).toEqual([]);
724 expect([
725 ...decisionsOf(null, [{ iid: "7", approved: false, approvedBy: approvedBy() }]),
726 ]).toEqual([]);
727 expect([
728 ...decisionsOf(true, [
729 { iid: "7", approved: null, approvedBy: approvedBy("julius") },
730 { iid: "8", approved: true, approvedBy: null },
731 ]),
732 ]).toEqual([]);
733 expect(
734 expectSuccess(decodeReviewDecisionsJson(JSON.stringify({ data: { project: null } }))),
735 ).toEqual(new Map());
736 });
737});
738​
739describe("decodeMergeRequestStatsJson", () => {
740 it("keys the counts by merge request number", () => {
741 const stats = expectSuccess(
742 decodeMergeRequestStatsJson(
743 JSON.stringify({
744 data: {
745 project: {
746 mergeRequests: {
747 nodes: [
748 { iid: "7", diffStatsSummary: { additions: 12, deletions: 3 } },
749 { iid: "8", diffStatsSummary: null },
750 ],
751 },
752 },
753 },
754 }),
755 ),
756 );
757​
758 // A merge request without a summary is left out rather than shown as zero lines changed.
759 expect([...stats]).toEqual([[7, { additions: 12, deletions: 3 }]]);
760 });
761​
762 it("answers nothing for a project the token cannot see", () => {
763 const stats = expectSuccess(
764 decodeMergeRequestStatsJson(JSON.stringify({ data: { project: null } })),
765 );
766​
767 expect(stats.size).toBe(0);
768 });
769​
770 it("reports a file list shorter than the summary as truncated", () => {
771 const read = (fileCount: number) =>
772 expectSuccess(
773 decodeMergeRequestFileStatsJson(
774 JSON.stringify({
775 data: {
776 project: {
777 mergeRequest: {
778 diffStatsSummary: { fileCount },
779 diffStats: [{ path: "src/a.ts", additions: 2, deletions: 1 }],
780 },
781 },
782 },
783 }),
784 ),
785 );
786​
787 expect(read(1)).toEqual({
788 files: [{ path: "src/a.ts", additions: 2, deletions: 1 }],
789 truncated: false,
790 });
791 expect(read(2).truncated).toBe(true);
792 });
793});