Open History to see the last commit
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 );
30}
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 });
43}
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;
49}
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 either auto-merge field, and says nothing where GitLab named neither", () => {
185 const armed = (entry: Record<string, unknown>) =>
186 expectSuccess(decodeMergeRequestDetailJson(detailJson(entry))).autoMergeEnabled;
187
188 expect(armed({ merge_when_pipeline_succeeds: true })).toBe(true);
189 // The newer name for the same fact, which older GitLab installs do not send.
190 expect(armed({ auto_merge_enabled: true })).toBe(true);
191 expect(armed({ merge_when_pipeline_succeeds: false })).toBe(false);
192 // Absent is GitLab not saying, which the page must not read as "not armed".
193 expect(armed({})).toBeUndefined();
194 });
195
196 it("keeps the squash choice stored with auto-merge", () => {
197 expect(
198 expectSuccess(
199 decodeMergeRequestDetailJson(
200 detailJson({ auto_merge_enabled: true, squash_on_merge: true }),
201 ),
202 ),
203 ).toMatchObject({ autoMergeEnabled: true, autoMergeMethod: "squash" });
204
205 expect(
206 expectSuccess(
207 decodeMergeRequestDetailJson(
208 detailJson({
209 auto_merge_enabled: true,
210 squash: true,
211 squash_on_merge: false,
212 }),
213 ),
214 ).autoMergeMethod,
215 ).toBeUndefined();
216 });
217
218 it("keeps a divergence GitLab did not count apart from a divergence of none", () => {
219 const behind = (entry: Record<string, unknown>) =>
220 expectSuccess(decodeMergeRequestDetailJson(detailJson(entry))).divergedCommits;
221
222 expect(behind({ diverged_commits_count: 3 })).toBe(3);
223 // Counted and found level, which is the one answer that entitles the page to say so.
224 expect(behind({ diverged_commits_count: 0 })).toBe(0);
225 // An install that does not answer, and a null where the answer would have gone, are both
226 // silence: reading either as zero would tell a stale branch it is current.
227 expect(behind({})).toBeUndefined();
228 expect(behind({ diverged_commits_count: null })).toBeUndefined();
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("decodeViewerJson", () => {
241 it("reads the signed-in username", () => {
242 expect(expectSuccess(decodeViewerJson(JSON.stringify({ username: "bilal" })))).toBe("bilal");
243 });
244
245 it("returns nothing when the account has no username", () => {
246 expect(expectSuccess(decodeViewerJson(JSON.stringify({ username: " " })))).toBeNull();
247 });
248});
249
250describe("decodeNotesJson", () => {
251 it("keeps comments and drops GitLab's own activity notes", () => {
252 const notes = expectSuccess(
253 decodeNotesJson(
254 JSON.stringify([
255 {
256 id: 1,
257 body: "assigned to @bilal",
258 system: true,
259 created_at: "2026-07-01T00:00:00Z",
260 },
261 {
262 id: 2,
263 body: "Looks good.",
264 author: { username: "julius" },
265 created_at: "2026-07-02T00:00:00Z",
266 },
267 { id: 3, body: " ", created_at: "2026-07-03T00:00:00Z" },
268 ]),
269 ),
270 );
271
272 expect(notes.comments).toHaveLength(1);
273 expect(notes.comments[0]).toMatchObject({
274 id: "2",
275 kind: "issue-comment",
276 body: "Looks good.",
277 });
278 // The raw count keeps the dropped notes visible to the caller, which needs them to page.
279 expect(notes.rawCount).toBe(3);
280 });
281
282 it("reads a line note as a review comment on its file", () => {
283 const notes = expectSuccess(
284 decodeNotesJson(
285 JSON.stringify([
286 {
287 id: 7,
288 type: "DiffNote",
289 body: "Rename this.",
290 created_at: "2026-07-02T00:00:00Z",
291 position: { new_path: "src/app.ts", old_path: "src/old.ts" },
292 },
293 ]),
294 ),
295 );
296
297 expect(notes.comments[0]).toMatchObject({ kind: "review-comment", path: "src/app.ts" });
298 });
299
300 it("falls back to the old path for a note on a deleted line", () => {
301 const notes = expectSuccess(
302 decodeNotesJson(
303 JSON.stringify([
304 {
305 id: 8,
306 type: "DiffNote",
307 body: "Gone.",
308 created_at: "2026-07-02T00:00:00Z",
309 position: { new_path: null, old_path: "src/old.ts" },
310 },
311 ]),
312 ),
313 );
314
315 expect(notes.comments[0]?.path).toBe("src/old.ts");
316 });
317});
318
319describe("decodeCommitsJson", () => {
320 it("returns commits oldest first", () => {
321 const commits = expectSuccess(
322 decodeCommitsJson(
323 JSON.stringify([
324 { id: "bbb", title: "second", committed_date: "2026-07-02T00:00:00Z" },
325 { id: "aaa", title: "first", committed_date: "2026-07-01T00:00:00Z" },
326 ]),
327 ),
328 );
329
330 expect(commits.map((commit) => commit.oid)).toEqual(["aaa", "bbb"]);
331 });
332
333 it("skips commits whose id is empty", () => {
334 const commits = expectSuccess(
335 decodeCommitsJson(
336 JSON.stringify([
337 { id: " ", title: "invalid", committed_date: "2026-07-02T00:00:00Z" },
338 { id: "aaa", committed_date: "2026-07-01T00:00:00Z" },
339 ]),
340 ),
341 );
342
343 expect(commits.map((commit) => commit.oid)).toEqual(["aaa"]);
344 });
345
346 it("falls back to the creation timestamp when there is no commit date", () => {
347 const commits = expectSuccess(
348 decodeCommitsJson(
349 JSON.stringify([
350 {
351 id: "aaa",
352 created_at: "2026-07-01T00:00:00+08:00",
353 author_name: "Ada Lovelace",
354 author_email: "ada@example.com",
355 },
356 ]),
357 ),
358 );
359
360 expect(commits[0]).toMatchObject({
361 oid: "aaa",
362 committedDate: "2026-07-01T00:00:00+08:00",
363 authors: [{ login: "Ada Lovelace", name: "Ada Lovelace", avatarUrl: null }],
364 });
365 });
366
367 it("carries commit additions and deletions when GitLab returns stats", () => {
368 const commits = expectSuccess(
369 decodeCommitsJson(
370 JSON.stringify([
371 {
372 id: "aaa",
373 committed_date: "2026-07-01T00:00:00Z",
374 stats: { additions: 21, deletions: 8, total: 29 },
375 },
376 ]),
377 ),
378 );
379
380 expect(commits[0]).toMatchObject({ additions: 21, deletions: 8 });
381 });
382});
383
384describe("decodeMergeRequestDiffsJson", () => {
385 it("quotes literal backslashes without interpreting them as escapes", () => {
386 const result = expectSuccess(
387 decodeMergeRequestDiffsJson(
388 JSON.stringify([
389 {
390 old_path: String.raw`src\notes.ts`,
391 new_path: String.raw`src\notes.ts`,
392 diff: "@@ -1 +1 @@\n-old\n+new",
393 },
394 ]),
395 ),
396 );
397
398 expect(result.patch).toBe(
399 [
400 String.raw`diff --git "a/src\\notes.ts" "b/src\\notes.ts"`,
401 String.raw`--- "a/src\\notes.ts"`,
402 String.raw`+++ "b/src\\notes.ts"`,
403 "@@ -1 +1 @@",
404 "-old",
405 "+new",
406 "",
407 ].join("\n"),
408 );
409 });
410
411 it("preserves spaces and literal backslashes in both rename paths", () => {
412 const result = expectSuccess(
413 decodeMergeRequestDiffsJson(
414 JSON.stringify([
415 {
416 old_path: String.raw` old\name.ts `,
417 new_path: String.raw` new\name.ts `,
418 renamed_file: true,
419 diff: "",
420 },
421 ]),
422 ),
423 );
424
425 expect(result.patch).toBe(
426 [
427 String.raw`diff --git "a/ old\\name.ts " "b/ new\\name.ts "`,
428 String.raw`rename from " old\\name.ts "`,
429 String.raw`rename to " new\\name.ts "`,
430 String.raw`--- "a/ old\\name.ts "`,
431 String.raw`+++ "b/ new\\name.ts "`,
432 ].join("\n"),
433 );
434 });
435
436 it("assembles a unified patch GitLab does not return", () => {
437 const result = expectSuccess(
438 decodeMergeRequestDiffsJson(
439 JSON.stringify([
440 {
441 old_path: "src/app.ts",
442 new_path: "src/app.ts",
443 diff: "@@ -1 +1 @@\n-old\n+new\n",
444 },
445 ]),
446 ),
447 );
448
449 expect(result.patch).toBe(
450 [
451 "diff --git a/src/app.ts b/src/app.ts",
452 "--- a/src/app.ts",
453 "+++ b/src/app.ts",
454 "@@ -1 +1 @@",
455 "-old",
456 "+new",
457 "",
458 ].join("\n"),
459 );
460 expect(result.truncated).toBe(false);
461 });
462
463 it("points a new file at /dev/null on the left and a deleted file on the right", () => {
464 const result = expectSuccess(
465 decodeMergeRequestDiffsJson(
466 JSON.stringify([
467 {
468 old_path: "src/new.ts",
469 new_path: "src/new.ts",
470 new_file: true,
471 b_mode: "100755",
472 diff: "@@ -0,0 +1 @@\n+hello\n",
473 },
474 {
475 old_path: "src/gone.ts",
476 new_path: "src/gone.ts",
477 deleted_file: true,
478 diff: "@@ -1 +0,0 @@\n-bye\n",
479 },
480 ]),
481 ),
482 );
483
484 expect(result.patch).toContain("new file mode 100755");
485 expect(result.patch).toContain("--- /dev/null");
486 expect(result.patch).toContain("deleted file mode 100644");
487 expect(result.patch).toContain("+++ /dev/null");
488 });
489
490 it("records a rename so the patch names both paths", () => {
491 const result = expectSuccess(
492 decodeMergeRequestDiffsJson(
493 JSON.stringify([
494 { old_path: "src/old.ts", new_path: "src/new.ts", renamed_file: true, diff: "" },
495 ]),
496 ),
497 );
498
499 expect(result.patch).toContain("rename from src/old.ts");
500 expect(result.patch).toContain("rename to src/new.ts");
501 });
502
503 it("reports truncation for a file GitLab refused to inline", () => {
504 const result = expectSuccess(
505 decodeMergeRequestDiffsJson(
506 JSON.stringify([{ old_path: "big.bin", new_path: "big.bin", diff: "", too_large: true }]),
507 ),
508 );
509
510 expect(result.truncated).toBe(true);
511 expect(result.patch).toContain("diff --git a/big.bin b/big.bin");
512 });
513
514 it("reports how many files GitLab returned, so the caller can page", () => {
515 const result = expectSuccess(
516 decodeMergeRequestDiffsJson(
517 JSON.stringify(
518 Array.from({ length: 3 }, (_, index) => ({
519 old_path: `src/${index}.ts`,
520 new_path: `src/${index}.ts`,
521 diff: "@@ -1 +1 @@\n-a\n+b\n",
522 })),
523 ),
524 ),
525 );
526
527 expect(result.rawCount).toBe(3);
528 expect(result.truncated).toBe(false);
529 expect(result.patch).toContain("src/2.ts");
530 });
531
532 it("fails when GitLab did not return a list", () => {
533 expect(Result.isFailure(decodeMergeRequestDiffsJson('{"message":"404"}'))).toBe(true);
534 });
535});
536
537describe("merge request viewer fields", () => {
538 it("carries GitLab's own answer for whether this viewer can merge", () => {
539 expect(
540 expectSuccess(decodeMergeRequestDetailJson(detailJson({ user: { can_merge: false } })))
541 .viewerCanMerge,
542 ).toBe(false);
543 expect(
544 expectSuccess(decodeMergeRequestDetailJson(detailJson({ user: { can_merge: true } })))
545 .viewerCanMerge,
546 ).toBe(true);
547 });
548
549 it("leaves merging permitted where GitLab answered without the field", () => {
550 // Only the single-merge-request endpoint carries `user`, and an install that answers without
551 // it has said nothing about the viewer rather than said no.
552 expect(expectSuccess(decodeMergeRequestDetailJson(detailJson({}))).viewerCanMerge).toBe(true);
553 expect(
554 expectSuccess(decodeMergeRequestDetailJson(detailJson({ user: null }))).viewerCanMerge,
555 ).toBe(true);
556 });
557});
558
559describe("decodeAwardEmojiJson", () => {
560 it("reads MR-level awards, keys per-note awards by the REST id inside their gid, and ignores an award outside the eight", () => {
561 const result = expectSuccess(
562 decodeAwardEmojiJson(
563 JSON.stringify({
564 data: {
565 currentUser: { username: "bilal" },
566 project: {
567 mergeRequest: {
568 awardEmoji: {
569 nodes: [
570 { name: "thumbsup", user: { username: "bilal" } },
571 { name: "thumbsup", user: { username: "julius" } },
572 ],
573 },
574 notes: {
575 pageInfo: { hasNextPage: false, endCursor: null },
576 nodes: [
577 {
578 id: "gid://gitlab/DiffNote/42",
579 awardEmoji: { nodes: [{ name: "heart", user: { username: "julius" } }] },
580 },
581 {
582 id: "gid://gitlab/Note/7",
583 // Not one of the eight the contract carries.
584 awardEmoji: {
585 nodes: [{ name: "partyparrot", user: { username: "bilal" } }],
586 },
587 },
588 ],
589 },
590 },
591 },
592 },
593 }),
594 ),
595 );
596
597 // `bilal` is `currentUser`, so the group they are in reads back as reacted, but their own
598 // username is left out of `actors` — the page names them "You" instead — while `count` still
599 // counts them; `julius` alone does not turn a group's own `viewerHasReacted` on.
600 expect(result.reactions).toEqual([
601 { content: "thumbs-up", count: 2, actors: ["julius"], viewerHasReacted: true },
602 ]);
603 // Note 7's only award named nobody the eight recognise, so it carries no reactions and is
604 // left out of the map rather than kept empty.
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 /** Null is the query going unanswered, which these cases are not about. */
688 function expectBlobs(result: Result.Result<ReadonlyMap<string, string> | null, unknown>) {
689 const blobs = expectSuccess(result);
690 expect(blobs).not.toBe(null);
691 if (blobs === null) throw new Error("expected an answered blobs query");
692 return blobs;
693 }
694
695 it("reads a blob id per path", () => {
696 const blobs = expectBlobs(
697 decodeRepositoryBlobsJson(
698 JSON.stringify({
699 data: {
700 project: {
701 repository: {
702 blobs: {
703 nodes: [
704 { path: "src/a.ts", oid: "aaa111" },
705 { path: "src/b.ts", oid: "bbb222" },
706 ],
707 },
708 },
709 },
710 },
711 }),
712 ),
713 );
714
715 expect([...blobs]).toEqual([
716 ["src/a.ts", "aaa111"],
717 ["src/b.ts", "bbb222"],
718 ]);
719 });
720
721 it("leaves out a node missing either half, which names no version", () => {
722 const blobs = expectBlobs(
723 decodeRepositoryBlobsJson(
724 JSON.stringify({
725 data: {
726 project: {
727 repository: {
728 blobs: {
729 nodes: [
730 { path: "src/a.ts", oid: null },
731 { path: null, oid: "bbb222" },
732 null,
733 { path: "src/c.ts", oid: "ccc333" },
734 ],
735 },
736 },
737 },
738 },
739 }),
740 ),
741 );
742
743 expect([...blobs]).toEqual([["src/c.ts", "ccc333"]]);
744 });
745
746 it("keys a blob by the path the host spelled, spaces and all", () => {
747 // A leading or trailing space is a legal part of a file's name. Trimmed here, the id lands
748 // under a key the asked-for path is not spelled with, and the caller fills that path in as
749 // the empty revision: a mark on the file then never compares against the real head blob.
750 const blobs = expectBlobs(
751 decodeRepositoryBlobsJson(
752 JSON.stringify({
753 data: {
754 project: {
755 repository: {
756 blobs: {
757 nodes: [
758 { path: " leading.ts", oid: "aaa111" },
759 { path: "trailing.ts ", oid: "bbb222" },
760 { path: " ", oid: "ccc333" },
761 { path: "", oid: "ddd444" },
762 ],
763 },
764 },
765 },
766 },
767 }),
768 ),
769 );
770
771 expect([...blobs]).toEqual([
772 [" leading.ts", "aaa111"],
773 ["trailing.ts ", "bbb222"],
774 // A name that is only spaces is one Git carries too, so it is a path like any other.
775 [" ", "ccc333"],
776 ]);
777 });
778
779 it("tells a project the reader cannot see from a revision with none of the files", () => {
780 // Null is the query going unanswered. Read as an empty answer it would say the head has none
781 // of the asked-for files, which reports every file a reader has cleared as changed.
782 expect(
783 expectSuccess(decodeRepositoryBlobsJson(JSON.stringify({ data: { project: null } }))),
784 ).toBe(null);
785 expect(
786 expectSuccess(
787 decodeRepositoryBlobsJson(JSON.stringify({ data: { project: { repository: null } } })),
788 ),
789 ).toBe(null);
790 expect(
791 expectSuccess(
792 decodeRepositoryBlobsJson(
793 JSON.stringify({ data: { project: { repository: { blobs: { nodes: [] } } } } }),
794 ),
795 ),
796 ).toEqual(new Map());
797 });
798
799 it("fails on output that is not the query's shape", () => {
800 expect(Result.isSuccess(decodeRepositoryBlobsJson("not json"))).toBe(false);
801 expect(Result.isSuccess(decodeRepositoryBlobsJson(JSON.stringify({ errors: [] })))).toBe(false);
802 });
803});