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

gitLabMergeRequestJson.test.ts

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