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

gitLabMergeRequestJson.test.ts

792 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 String.raw`rename from " old\\name.ts "`,
376 String.raw`rename to " new\\name.ts "`,
377 String.raw`--- "a/ old\\name.ts "`,
378 String.raw`+++ "b/ new\\name.ts "`,
379 ].join("\n"),
380 );
381 });
382​
383 it("assembles a unified patch GitLab does not return", () => {
384 const result = expectSuccess(
385 decodeMergeRequestDiffsJson(
386 JSON.stringify([
387 {
388 old_path: "src/app.ts",
389 new_path: "src/app.ts",
390 diff: "@@ -1 +1 @@\n-old\n+new\n",
391 },
392 ]),
393 ),
394 );
395​
396 expect(result.patch).toBe(
397 [
398 "diff --git a/src/app.ts b/src/app.ts",
399 "--- a/src/app.ts",
400 "+++ b/src/app.ts",
401 "@@ -1 +1 @@",
402 "-old",
403 "+new",
404 "",
405 ].join("\n"),
406 );
407 expect(result.truncated).toBe(false);
408 });
409​
410 it("points a new file at /dev/null on the left and a deleted file on the right", () => {
411 const result = expectSuccess(
412 decodeMergeRequestDiffsJson(
413 JSON.stringify([
414 {
415 old_path: "src/new.ts",
416 new_path: "src/new.ts",
417 new_file: true,
418 b_mode: "100755",
419 diff: "@@ -0,0 +1 @@\n+hello\n",
420 },
421 {
422 old_path: "src/gone.ts",
423 new_path: "src/gone.ts",
424 deleted_file: true,
425 diff: "@@ -1 +0,0 @@\n-bye\n",
426 },
427 ]),
428 ),
429 );
430​
431 expect(result.patch).toContain("new file mode 100755");
432 expect(result.patch).toContain("--- /dev/null");
433 expect(result.patch).toContain("deleted file mode 100644");
434 expect(result.patch).toContain("+++ /dev/null");
435 });
436​
437 it("reports truncation for a file GitLab refused to inline", () => {
438 const result = expectSuccess(
439 decodeMergeRequestDiffsJson(
440 JSON.stringify([{ old_path: "big.bin", new_path: "big.bin", diff: "", too_large: true }]),
441 ),
442 );
443​
444 expect(result.truncated).toBe(true);
445 expect(result.patch).toContain("diff --git a/big.bin b/big.bin");
446 });
447​
448 it("reports how many files GitLab returned, so the caller can page", () => {
449 const result = expectSuccess(
450 decodeMergeRequestDiffsJson(
451 JSON.stringify(
452 Array.from({ length: 3 }, (_, index) => ({
453 old_path: `src/${index}.ts`,
454 new_path: `src/${index}.ts`,
455 diff: "@@ -1 +1 @@\n-a\n+b\n",
456 })),
457 ),
458 ),
459 );
460​
461 expect(result.rawCount).toBe(3);
462 expect(result.truncated).toBe(false);
463 expect(result.patch).toContain("src/2.ts");
464 });
465});
466​
467describe("decodeTriggerJobChecksJson", () => {
468 it("reads a downstream failure GitLab was told to allow as neutral, not failure", () => {
469 const result = expectSuccess(
470 decodeTriggerJobChecksJson(
471 JSON.stringify([
472 { name: "e2e", allow_failure: true, downstream_pipeline: { status: "failed" } },
473 { name: "deploy", allow_failure: false, downstream_pipeline: { status: "running" } },
474 { name: 7 },
475 ]),
476 ),
477 );
478​
479 expect(result.checks.map((check) => [check.name, check.status])).toEqual([
480 ["e2e", "neutral"],
481 ["deploy", "pending"],
482 ]);
483 expect(result.rawCount).toBe(3);
484 });
485});
486​
487describe("merge request viewer fields", () => {
488 it("leaves merging permitted where GitLab answered without the field", () => {
489 expect(expectSuccess(decodeMergeRequestDetailJson(detailJson({}))).viewerCanMerge).toBe(true);
490 expect(
491 expectSuccess(decodeMergeRequestDetailJson(detailJson({ user: null }))).viewerCanMerge,
492 ).toBe(true);
493 });
494});
495​
496describe("decodeAwardEmojiJson", () => {
497 it("reads MR-level awards, keys per-note awards by the REST id inside their gid, and ignores an award outside the eight", () => {
498 const result = expectSuccess(
499 decodeAwardEmojiJson(
500 JSON.stringify({
501 data: {
502 currentUser: { username: "bilal" },
503 project: {
504 mergeRequest: {
505 awardEmoji: {
506 nodes: [
507 { name: "thumbsup", user: { username: "bilal" } },
508 { name: "thumbsup", user: { username: "julius" } },
509 ],
510 },
511 notes: {
512 pageInfo: { hasNextPage: false, endCursor: null },
513 nodes: [
514 {
515 id: "gid://gitlab/DiffNote/42",
516 awardEmoji: { nodes: [{ name: "heart", user: { username: "julius" } }] },
517 },
518 {
519 id: "gid://gitlab/Note/7",
520 awardEmoji: {
521 nodes: [{ name: "partyparrot", user: { username: "bilal" } }],
522 },
523 },
524 ],
525 },
526 },
527 },
528 },
529 }),
530 ),
531 );
532​
533 expect(result.reactions).toEqual([
534 { content: "thumbs-up", count: 2, actors: ["julius"], viewerHasReacted: true },
535 ]);
536 expect([...result.reactionsByNoteId]).toEqual([
537 ["42", [{ content: "heart", count: 1, actors: ["julius"], viewerHasReacted: false }]],
538 ]);
539 expect(result.nextCursor).toBeNull();
540 });
541​
542 it("matches the viewer's username case-insensitively, since GitLab is not consistent about case", () => {
543 const result = expectSuccess(
544 decodeAwardEmojiJson(
545 JSON.stringify({
546 data: {
547 currentUser: { username: "Bilal" },
548 project: {
549 mergeRequest: {
550 awardEmoji: {
551 nodes: [
552 { name: "heart", user: { username: "bilal" } },
553 { name: "heart", user: { username: "julius" } },
554 ],
555 },
556 notes: { pageInfo: { hasNextPage: false, endCursor: null }, nodes: [] },
557 },
558 },
559 },
560 }),
561 ),
562 );
563​
564 expect(result.reactions).toEqual([
565 { content: "heart", count: 2, actors: ["julius"], viewerHasReacted: true },
566 ]);
567 });
568});
569​
570describe("decodeOwnAwardIdJson", () => {
571 const awards = JSON.stringify([
572 { id: 101, name: "thumbsup", user: { username: "julius" } },
573 { id: 102, name: "thumbsup", user: { username: "bilal" } },
574 ]);
575​
576 it("finds the reader's own award of that name, which is the one a removal deletes", () => {
577 expect(
578 expectSuccess(decodeOwnAwardIdJson(awards, { content: "thumbs-up", viewer: "bilal" })),
579 ).toBe(102);
580 });
581});
582​
583describe("decodeRepositoryBlobsJson", () => {
584 function expectBlobs(result: Result.Result<ReadonlyMap<string, string> | null, unknown>) {
585 const blobs = expectSuccess(result);
586 expect(blobs).not.toBe(null);
587 if (blobs === null) throw new Error("expected an answered blobs query");
588 return blobs;
589 }
590​
591 it("leaves out a node missing either half, which names no version", () => {
592 const blobs = expectBlobs(
593 decodeRepositoryBlobsJson(
594 JSON.stringify({
595 data: {
596 project: {
597 repository: {
598 blobs: {
599 nodes: [
600 { path: "src/a.ts", oid: null },
601 { path: null, oid: "bbb222" },
602 null,
603 { path: "src/c.ts", oid: "ccc333" },
604 ],
605 },
606 },
607 },
608 },
609 }),
610 ),
611 );
612​
613 expect([...blobs]).toEqual([["src/c.ts", "ccc333"]]);
614 });
615​
616 it("keys a blob by the path the host spelled, spaces and all", () => {
617 const blobs = expectBlobs(
618 decodeRepositoryBlobsJson(
619 JSON.stringify({
620 data: {
621 project: {
622 repository: {
623 blobs: {
624 nodes: [
625 { path: " leading.ts", oid: "aaa111" },
626 { path: "trailing.ts ", oid: "bbb222" },
627 { path: " ", oid: "ccc333" },
628 { path: "", oid: "ddd444" },
629 ],
630 },
631 },
632 },
633 },
634 }),
635 ),
636 );
637​
638 expect([...blobs]).toEqual([
639 [" leading.ts", "aaa111"],
640 ["trailing.ts ", "bbb222"],
641 [" ", "ccc333"],
642 ]);
643 });
644​
645 it("tells a project the reader cannot see from a revision with none of the files", () => {
646 expect(
647 expectSuccess(decodeRepositoryBlobsJson(JSON.stringify({ data: { project: null } }))),
648 ).toBe(null);
649 expect(
650 expectSuccess(
651 decodeRepositoryBlobsJson(JSON.stringify({ data: { project: { repository: null } } })),
652 ),
653 ).toBe(null);
654 expect(
655 expectSuccess(
656 decodeRepositoryBlobsJson(
657 JSON.stringify({ data: { project: { repository: { blobs: { nodes: [] } } } } }),
658 ),
659 ),
660 ).toEqual(new Map());
661 });
662});
663​
664describe("decodeReviewDecisionsJson", () => {
665 const decisionsOf = (enterprise: boolean | null, nodes: ReadonlyArray<unknown>) =>
666 expectSuccess(
667 decodeReviewDecisionsJson(
668 JSON.stringify({
669 data: {
670 metadata: enterprise === null ? null : { enterprise },
671 project: { mergeRequests: { nodes } },
672 },
673 }),
674 ),
675 );
676 const approvedBy = (...usernames: ReadonlyArray<string>) => ({
677 nodes: usernames.map((username) => ({ username })),
678 });
679​
680 it.each([
681 {
682 reading: "an approval short of Enterprise approval rules",
683 enterprise: true,
684 approval: { approved: false, approvedBy: approvedBy("GitLabDuo") },
685 decision: "review-required",
686 },
687 {
688 reading: "Enterprise approval rules no one has approved against",
689 enterprise: true,
690 approval: { approved: false, approvedBy: approvedBy() },
691 decision: "review-required",
692 },
693 {
694 reading: "Enterprise approval rules met",
695 enterprise: true,
696 approval: { approved: true, approvedBy: approvedBy("julius") },
697 decision: "approved",
698 },
699 {
700 reading: "Enterprise without approval rules or approvals",
701 enterprise: true,
702 approval: { approved: true, approvedBy: approvedBy() },
703 decision: null,
704 },
705 {
706 reading: "Community Edition with an approval",
707 enterprise: false,
708 approval: { approved: true, approvedBy: approvedBy("julius") },
709 decision: "approved",
710 },
711 {
712 reading: "Community Edition without approvals",
713 enterprise: false,
714 approval: { approved: false, approvedBy: approvedBy() },
715 decision: null,
716 },
717 ])("reads $reading as $decision", ({ enterprise, approval, decision }) => {
718 expect([...decisionsOf(enterprise, [{ iid: "7", ...approval }])]).toEqual([[7, decision]]);
719 });
720​
721 it("leaves out a merge request it cannot key, and a project the reader cannot see", () => {
722 expect([...decisionsOf(true, [null, { iid: "x", approved: true }])]).toEqual([]);
723 expect([
724 ...decisionsOf(null, [{ iid: "7", approved: false, approvedBy: approvedBy() }]),
725 ]).toEqual([]);
726 expect([
727 ...decisionsOf(true, [
728 { iid: "7", approved: null, approvedBy: approvedBy("julius") },
729 { iid: "8", approved: true, approvedBy: null },
730 ]),
731 ]).toEqual([]);
732 expect(
733 expectSuccess(decodeReviewDecisionsJson(JSON.stringify({ data: { project: null } }))),
734 ).toEqual(new Map());
735 });
736});
737​
738describe("decodeMergeRequestStatsJson", () => {
739 it("keys the counts by merge request number", () => {
740 const stats = expectSuccess(
741 decodeMergeRequestStatsJson(
742 JSON.stringify({
743 data: {
744 project: {
745 mergeRequests: {
746 nodes: [
747 { iid: "7", diffStatsSummary: { additions: 12, deletions: 3 } },
748 { iid: "8", diffStatsSummary: null },
749 ],
750 },
751 },
752 },
753 }),
754 ),
755 );
756​
757 // A merge request without a summary is left out rather than shown as zero lines changed.
758 expect([...stats]).toEqual([[7, { additions: 12, deletions: 3 }]]);
759 });
760​
761 it("answers nothing for a project the token cannot see", () => {
762 const stats = expectSuccess(
763 decodeMergeRequestStatsJson(JSON.stringify({ data: { project: null } })),
764 );
765​
766 expect(stats.size).toBe(0);
767 });
768​
769 it("reports a file list shorter than the summary as truncated", () => {
770 const read = (fileCount: number) =>
771 expectSuccess(
772 decodeMergeRequestFileStatsJson(
773 JSON.stringify({
774 data: {
775 project: {
776 mergeRequest: {
777 diffStatsSummary: { fileCount },
778 diffStats: [{ path: "src/a.ts", additions: 2, deletions: 1 }],
779 },
780 },
781 },
782 }),
783 ),
784 );
785​
786 expect(read(1)).toEqual({
787 files: [{ path: "src/a.ts", additions: 2, deletions: 1 }],
788 truncated: false,
789 });
790 expect(read(2).truncated).toBe(true);
791 });
792});