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

gitLabMergeRequestJson.test.ts

455 lines13.4 KB

1import * as Result from "effect/Result";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 decodeCommitsJson,
6 decodeMergeRequestDetailJson,
7 decodeMergeRequestDiffsJson,
8 decodeMergeRequestListJson,
9 decodeNotesJson,
10 decodeViewerJson,
11} from "./gitLabMergeRequestJson.ts";
12​
13function listJson(entries: ReadonlyArray<Record<string, unknown>>): string {
14 return JSON.stringify(
15 entries.map((entry) => ({
16 iid: 1,
17 title: "Add the merge requests page",
18 web_url: "https://gitlab.com/acme/web/-/merge_requests/1",
19 source_branch: "feat/page",
20 target_branch: "main",
21 created_at: "2026-07-01T00:00:00Z",
22 updated_at: "2026-07-02T00:00:00Z",
23 ...entry,
24 })),
25 );
27​
28function detailJson(entry: Record<string, unknown>): string {
29 return JSON.stringify({
30 iid: 1,
31 title: "Add the merge requests page",
32 web_url: "https://gitlab.com/acme/web/-/merge_requests/1",
33 source_branch: "feat/page",
34 target_branch: "main",
35 created_at: "2026-07-01T00:00:00Z",
36 updated_at: "2026-07-02T00:00:00Z",
37 ...entry,
38 });
40​
41function expectSuccess<A>(result: Result.Result<A, unknown>): A {
42 expect(Result.isSuccess(result)).toBe(true);
43 if (!Result.isSuccess(result)) throw new Error("expected a successful decode");
44 return result.success;
46​
47describe("decodeMergeRequestListJson", () => {
48 it("reads a merge request as a change request", () => {
49 const batch = expectSuccess(
50 decodeMergeRequestListJson(
51 listJson([
52 {
53 iid: 42,
54 author: { username: "bilal", name: "Bilal" },
55 state: "opened",
56 merge_status: "can_be_merged",
57 draft: false,
58 reviewers: [{ username: "julius" }],
59 labels: ["backend", " "],
60 },
61 ]),
62 ),
63 );
64​
65 expect(batch.items).toHaveLength(1);
66 expect(batch.items[0]).toMatchObject({
67 number: 42,
68 author: { login: "bilal", name: "Bilal" },
69 headBranch: "feat/page",
70 baseBranch: "main",
71 state: "open",
72 isDraft: false,
73 mergeability: "mergeable",
74 reviewRequestLogins: ["julius"],
75 labels: [{ name: "backend", color: null }],
76 });
77 });
78​
79 it("reports no line counts, which GitLab does not expose", () => {
80 const batch = expectSuccess(decodeMergeRequestListJson(listJson([{}])));
81​
82 expect(batch.items[0]).toMatchObject({ additions: 0, deletions: 0 });
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("keeps a locked merge request open", () => {
96 const batch = expectSuccess(decodeMergeRequestListJson(listJson([{ state: "locked" }])));
97​
98 expect(batch.items[0]?.state).toBe("open");
99 });
100​
101 it("reads the legacy draft flag", () => {
102 const batch = expectSuccess(decodeMergeRequestListJson(listJson([{ work_in_progress: true }])));
103​
104 expect(batch.items[0]?.isDraft).toBe(true);
105 });
106​
107 it("calls a conflicted merge request conflicting even while the merge check is pending", () => {
108 const batch = expectSuccess(
109 decodeMergeRequestListJson(listJson([{ merge_status: "checking", has_conflicts: true }])),
110 );
111​
112 expect(batch.items[0]?.mergeability).toBe("conflicting");
113 });
114​
115 it("leaves an unfinished merge check unknown", () => {
116 const batch = expectSuccess(
117 decodeMergeRequestListJson(listJson([{ merge_status: "checking" }])),
118 );
119​
120 expect(batch.items[0]?.mergeability).toBe("unknown");
121 });
122​
123 it("skips a malformed row but still counts it, so paging does not stop early", () => {
124 const batch = expectSuccess(
125 decodeMergeRequestListJson(
126 JSON.stringify([{ iid: "not a number" }, ...JSON.parse(listJson([{}]))]),
127 ),
128 );
129​
130 expect(batch.items).toHaveLength(1);
131 expect(batch.rawIndexes).toEqual([1]);
132 expect(batch.rawCount).toBe(2);
133 });
134});
135​
136describe("decodeMergeRequestDetailJson", () => {
137 it("reads the description, file count and pipeline", () => {
138 const detail = expectSuccess(
139 decodeMergeRequestDetailJson(
140 detailJson({
141 description: "Ships the page.",
142 changes_count: "3",
143 reviewers: [{ username: "julius", name: "Julius" }],
144 head_pipeline: {
145 status: "success",
146 web_url: "https://gitlab.com/acme/web/-/pipelines/9",
147 source: "merge_request_event",
148 },
149 }),
150 ),
151 );
152​
153 expect(detail.body).toBe("Ships the page.");
154 expect(detail.changedFiles).toBe(3);
155 expect(detail.reviewers).toEqual([{ login: "julius", name: "Julius", avatarUrl: null }]);
156 expect(detail.checks).toEqual([
157 {
158 name: "Pipeline",
159 status: "success",
160 description: "merge_request_event",
161 url: "https://gitlab.com/acme/web/-/pipelines/9",
162 },
163 ]);
164 });
165​
166 it("reads an uncounted change set as its floor", () => {
167 const detail = expectSuccess(
168 decodeMergeRequestDetailJson(detailJson({ changes_count: "1000+" })),
169 );
170​
171 expect(detail.changedFiles).toBe(1000);
172 });
173​
174 it("falls back to no file count when GitLab omits one", () => {
175 const detail = expectSuccess(decodeMergeRequestDetailJson(detailJson({})));
176​
177 expect(detail.changedFiles).toBe(0);
178 });
179​
180 it("maps a pipeline waiting on a person to neutral, not failure", () => {
181 const detail = expectSuccess(
182 decodeMergeRequestDetailJson(detailJson({ head_pipeline: { status: "manual" } })),
183 );
184​
185 expect(detail.checks[0]?.status).toBe("neutral");
186 });
187});
188​
189describe("decodeViewerJson", () => {
190 it("reads the signed-in username", () => {
191 expect(expectSuccess(decodeViewerJson(JSON.stringify({ username: "bilal" })))).toBe("bilal");
192 });
193​
194 it("returns nothing when the account has no username", () => {
195 expect(expectSuccess(decodeViewerJson(JSON.stringify({ username: " " })))).toBeNull();
196 });
197});
198​
199describe("decodeNotesJson", () => {
200 it("keeps comments and drops GitLab's own activity notes", () => {
201 const notes = expectSuccess(
202 decodeNotesJson(
203 JSON.stringify([
204 {
205 id: 1,
206 body: "assigned to @bilal",
207 system: true,
208 created_at: "2026-07-01T00:00:00Z",
209 },
210 {
211 id: 2,
212 body: "Looks good.",
213 author: { username: "julius" },
214 created_at: "2026-07-02T00:00:00Z",
215 },
216 { id: 3, body: " ", created_at: "2026-07-03T00:00:00Z" },
217 ]),
218 ),
219 );
220​
221 expect(notes.comments).toHaveLength(1);
222 expect(notes.comments[0]).toMatchObject({
223 id: "2",
224 kind: "issue-comment",
225 body: "Looks good.",
226 });
227 // The raw count keeps the dropped notes visible to the caller, which needs them to page.
228 expect(notes.rawCount).toBe(3);
229 });
230​
231 it("reads a line note as a review comment on its file", () => {
232 const notes = expectSuccess(
233 decodeNotesJson(
234 JSON.stringify([
235 {
236 id: 7,
237 type: "DiffNote",
238 body: "Rename this.",
239 created_at: "2026-07-02T00:00:00Z",
240 position: { new_path: "src/app.ts", old_path: "src/old.ts" },
241 },
242 ]),
243 ),
244 );
245​
246 expect(notes.comments[0]).toMatchObject({ kind: "review-comment", path: "src/app.ts" });
247 });
248​
249 it("falls back to the old path for a note on a deleted line", () => {
250 const notes = expectSuccess(
251 decodeNotesJson(
252 JSON.stringify([
253 {
254 id: 8,
255 type: "DiffNote",
256 body: "Gone.",
257 created_at: "2026-07-02T00:00:00Z",
258 position: { new_path: null, old_path: "src/old.ts" },
259 },
260 ]),
261 ),
262 );
263​
264 expect(notes.comments[0]?.path).toBe("src/old.ts");
265 });
266});
267​
268describe("decodeCommitsJson", () => {
269 it("returns commits oldest first", () => {
270 const commits = expectSuccess(
271 decodeCommitsJson(
272 JSON.stringify([
273 { id: "bbb", title: "second", committed_date: "2026-07-02T00:00:00Z" },
274 { id: "aaa", title: "first", committed_date: "2026-07-01T00:00:00Z" },
275 ]),
276 ),
277 );
278​
279 expect(commits.map((commit) => commit.oid)).toEqual(["aaa", "bbb"]);
280 });
281​
282 it("skips commits whose id is empty", () => {
283 const commits = expectSuccess(
284 decodeCommitsJson(
285 JSON.stringify([
286 { id: " ", title: "invalid", committed_date: "2026-07-02T00:00:00Z" },
287 { id: "aaa", committed_date: "2026-07-01T00:00:00Z" },
288 ]),
289 ),
290 );
291​
292 expect(commits.map((commit) => commit.oid)).toEqual(["aaa"]);
293 });
294​
295 it("falls back to the creation timestamp when there is no commit date", () => {
296 const commits = expectSuccess(
297 decodeCommitsJson(
298 JSON.stringify([
299 {
300 id: "aaa",
301 created_at: "2026-07-01T00:00:00+08:00",
302 author_name: "Ada Lovelace",
303 author_email: "ada@example.com",
304 },
305 ]),
306 ),
307 );
308​
309 expect(commits[0]).toMatchObject({
310 oid: "aaa",
311 committedDate: "2026-07-01T00:00:00+08:00",
312 authors: [{ login: "Ada Lovelace", name: "Ada Lovelace", avatarUrl: null }],
313 });
314 });
315​
316 it("carries commit additions and deletions when GitLab returns stats", () => {
317 const commits = expectSuccess(
318 decodeCommitsJson(
319 JSON.stringify([
320 {
321 id: "aaa",
322 committed_date: "2026-07-01T00:00:00Z",
323 stats: { additions: 21, deletions: 8, total: 29 },
324 },
325 ]),
326 ),
327 );
328​
329 expect(commits[0]).toMatchObject({ additions: 21, deletions: 8 });
330 });
331});
332​
333describe("decodeMergeRequestDiffsJson", () => {
334 it("assembles a unified patch GitLab does not return", () => {
335 const result = expectSuccess(
336 decodeMergeRequestDiffsJson(
337 JSON.stringify([
338 {
339 old_path: "src/app.ts",
340 new_path: "src/app.ts",
341 diff: "@@ -1 +1 @@\n-old\n+new\n",
342 },
343 ]),
344 ),
345 );
346​
347 expect(result.patch).toBe(
348 [
349 "diff --git a/src/app.ts b/src/app.ts",
350 "--- a/src/app.ts",
351 "+++ b/src/app.ts",
352 "@@ -1 +1 @@",
353 "-old",
354 "+new",
355 "",
356 ].join("\n"),
357 );
358 expect(result.truncated).toBe(false);
359 });
360​
361 it("points a new file at /dev/null on the left and a deleted file on the right", () => {
362 const result = expectSuccess(
363 decodeMergeRequestDiffsJson(
364 JSON.stringify([
365 {
366 old_path: "src/new.ts",
367 new_path: "src/new.ts",
368 new_file: true,
369 b_mode: "100755",
370 diff: "@@ -0,0 +1 @@\n+hello\n",
371 },
372 {
373 old_path: "src/gone.ts",
374 new_path: "src/gone.ts",
375 deleted_file: true,
376 diff: "@@ -1 +0,0 @@\n-bye\n",
377 },
378 ]),
379 ),
380 );
381​
382 expect(result.patch).toContain("new file mode 100755");
383 expect(result.patch).toContain("--- /dev/null");
384 expect(result.patch).toContain("deleted file mode 100644");
385 expect(result.patch).toContain("+++ /dev/null");
386 });
387​
388 it("records a rename so the patch names both paths", () => {
389 const result = expectSuccess(
390 decodeMergeRequestDiffsJson(
391 JSON.stringify([
392 { old_path: "src/old.ts", new_path: "src/new.ts", renamed_file: true, diff: "" },
393 ]),
394 ),
395 );
396​
397 expect(result.patch).toContain("rename from src/old.ts");
398 expect(result.patch).toContain("rename to src/new.ts");
399 });
400​
401 it("reports truncation for a file GitLab refused to inline", () => {
402 const result = expectSuccess(
403 decodeMergeRequestDiffsJson(
404 JSON.stringify([{ old_path: "big.bin", new_path: "big.bin", diff: "", too_large: true }]),
405 ),
406 );
407​
408 expect(result.truncated).toBe(true);
409 expect(result.patch).toContain("diff --git a/big.bin b/big.bin");
410 });
411​
412 it("reports how many files GitLab returned, so the caller can page", () => {
413 const result = expectSuccess(
414 decodeMergeRequestDiffsJson(
415 JSON.stringify(
416 Array.from({ length: 3 }, (_, index) => ({
417 old_path: `src/${index}.ts`,
418 new_path: `src/${index}.ts`,
419 diff: "@@ -1 +1 @@\n-a\n+b\n",
420 })),
421 ),
422 ),
423 );
424​
425 expect(result.rawCount).toBe(3);
426 expect(result.truncated).toBe(false);
427 expect(result.patch).toContain("src/2.ts");
428 });
429​
430 it("fails when GitLab did not return a list", () => {
431 expect(Result.isFailure(decodeMergeRequestDiffsJson('{"message":"404"}'))).toBe(true);
432 });
433});
434​
435describe("merge request viewer fields", () => {
436 it("carries GitLab's own answer for whether this viewer can merge", () => {
437 expect(
438 expectSuccess(decodeMergeRequestDetailJson(detailJson({ user: { can_merge: false } })))
439 .viewerCanMerge,
440 ).toBe(false);
441 expect(
442 expectSuccess(decodeMergeRequestDetailJson(detailJson({ user: { can_merge: true } })))
443 .viewerCanMerge,
444 ).toBe(true);
445 });
446​
447 it("leaves merging permitted where GitLab answered without the field", () => {
448 // Only the single-merge-request endpoint carries `user`, and an install that answers without
449 // it has said nothing about the viewer rather than said no.
450 expect(expectSuccess(decodeMergeRequestDetailJson(detailJson({}))).viewerCanMerge).toBe(true);
451 expect(
452 expectSuccess(decodeMergeRequestDetailJson(detailJson({ user: null }))).viewerCanMerge,
453 ).toBe(true);
454 });
455});