Open History to see the last commit
1import { afterEach, assert, expect, it, vi } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import { ChildProcessSpawner } from "effect/process";
5import { VcsProcessExitError } from "@t3tools/contracts";
6
7import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
8import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
9import * as GitLabCli from "./GitLabCli.ts";
10import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
11
12const run = vi.fn<SourceControlHost.SourceControlHost["Service"]["process"]["run"]>();
13const layer = it.layer(
14 GitLabPullRequestCli.layer.pipe(
15 Layer.provide(GitLabCli.layer),
16 Layer.provide(TestSourceControlHost.layer({ process: { run } })),
17 ),
18);
19const input = { cwd: "/repo", repository: "acme/nested/web", number: 7 };
20const file = {
21 old_path: "src/old.ts",
22 new_path: "src/new.ts",
23 renamed_file: true,
24 new_file: false,
25 deleted_file: false,
26 a_mode: "100644",
27 b_mode: "100644",
28 diff: "@@ -1 +1 @@\n-before\n+after\n",
29};
30/** Supplies JSON at the process boundary while retaining the real CLI adapter and decoder. */
31function output(body: unknown, stdoutTruncated = false) {
32 return Effect.succeed({
33 exitCode: ChildProcessSpawner.ExitCode(0),
34 stdout: JSON.stringify(body),
35 stderr: "",
36 stdoutTruncated,
37 stderrTruncated: false,
38 });
39}
40/** Supplies the failure classification produced by VcsProcess for an unsuccessful glab call. */
41function failure(failureKind: "not-found" | "authentication" | "rate-limited" | "command-failed") {
42 return Effect.fail(
43 new VcsProcessExitError({
44 operation: "GitLabCli.execute",
45 command: "glab",
46 cwd: input.cwd,
47 exitCode: 1,
48 detail: "GitLab HTTP failure",
49 failureKind,
50 }),
51 );
52}
53afterEach(() => run.mockReset());
54
55layer("legacy GitLab diff requests through GitLabCli", (it) => {
56 it.effect.each([0, 1, 100, 247])("returns %s legacy changes as one terminal slice", (count) =>
57 Effect.gen(function* () {
58 run.mockReturnValueOnce(failure("not-found"));
59 run.mockReturnValueOnce(
60 output({
61 changes: Array.from({ length: count }, () => file),
62 overflow: false,
63 changes_count: String(count),
64 }),
65 );
66 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
67 const result = yield* cli.getMergeRequestDiff(input);
68 expect(result.nextCursor).toBeNull();
69 expect(result.truncated).toBe(false);
70 expect(result.patch.match(/^diff --git /gm)?.length ?? 0).toBe(count);
71 if (count > 0) {
72 expect(result.patch).toContain("rename from src/old.ts\nrename to src/new.ts");
73 expect(result.patch).toContain("@@ -1 +1 @@\n-before\n+after\n");
74 expect(result.patch.endsWith("\n")).toBe(true);
75 }
76 expect(run.mock.calls.map(([call]) => call.args)).toEqual([
77 ["api", "projects/acme%2Fnested%2Fweb/merge_requests/7/diffs?per_page=100&page=1"],
78 ["api", "projects/acme%2Fnested%2Fweb/merge_requests/7/changes?access_raw_diffs=true"],
79 ]);
80 expect(run.mock.calls[1]?.[0]).toMatchObject({
81 cwd: "/repo",
82 maxOutputBytes: 8 * 1024 * 1024,
83 timeoutMs: 60_000,
84 });
85 }),
86 );
87 it.effect.each([false, true])("accepts an unavailable legacy count (overflow: %s)", (overflow) =>
88 Effect.gen(function* () {
89 run.mockReturnValueOnce(failure("not-found"));
90 run.mockReturnValueOnce(output({ changes: [file], changes_count: null, overflow }));
91 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
92 const result = yield* cli.getMergeRequestDiff(input);
93 expect(result.truncated).toBe(overflow);
94 expect(result.nextCursor).toBeNull();
95 expect(result.patch).toContain("@@ -1 +1 @@\n-before\n+after\n");
96 }),
97 );
98 it.effect.each([{ overflow: true }, { changes_count: "247+" }])(
99 "marks legacy limits %j truncated",
100 (metadata) =>
101 Effect.gen(function* () {
102 run.mockReturnValueOnce(failure("not-found"));
103 run.mockReturnValueOnce(output({ changes: [file], ...metadata }));
104 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
105 expect(yield* cli.getMergeRequestDiff(input)).toMatchObject({
106 truncated: true,
107 nextCursor: null,
108 });
109 }),
110 );
111 it.effect.each([{}, { changes: null }, { changes: {} }, [file]])(
112 "rejects malformed legacy wrapper %j",
113 (body) =>
114 Effect.gen(function* () {
115 run.mockReturnValueOnce(failure("not-found"));
116 run.mockReturnValueOnce(output(body));
117 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
118 const error = yield* cli.getMergeRequestDiff(input).pipe(Effect.flip);
119 assert.equal(error._tag, "GitLabMergeRequestReadError");
120 }),
121 );
122 it.effect.each([false, true])("marks skipped file entries truncated (legacy: %s)", (legacy) =>
123 Effect.gen(function* () {
124 const entries = [file, { new_path: "missing-old-path.ts" }];
125 if (legacy) run.mockReturnValueOnce(failure("not-found"));
126 run.mockReturnValueOnce(output(legacy ? { changes: entries } : entries));
127 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
128 const result = yield* cli.getMergeRequestDiff(input);
129 expect(result.truncated).toBe(true);
130 expect(result.nextCursor).toBeNull();
131 expect(result.patch.match(/^diff --git /gm)).toHaveLength(1);
132 expect(result.patch).toContain("@@ -1 +1 @@\n-before\n+after\n");
133 }),
134 );
135 it.effect("rejects a byte-truncated legacy response", () =>
136 Effect.gen(function* () {
137 run.mockReturnValueOnce(failure("not-found"));
138 run.mockReturnValueOnce(output({ changes: [file] }, true));
139 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
140 expect((yield* cli.getMergeRequestDiff(input).pipe(Effect.flip))._tag).toBe(
141 "GitLabMergeRequestReadError",
142 );
143 }),
144 );
145 it.effect.each(["authentication", "rate-limited", "command-failed"] as const)(
146 "does not fall back on %s",
147 (kind) =>
148 Effect.gen(function* () {
149 run.mockReturnValueOnce(failure(kind));
150 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
151 yield* cli.getMergeRequestDiff(input).pipe(Effect.flip);
152 expect(run).toHaveBeenCalledTimes(1);
153 }),
154 );
155 it.effect("does not fall back for a missing commit or later MR page", () =>
156 Effect.gen(function* () {
157 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
158 for (const target of [
159 { ...input, commit: "abc1234" },
160 { ...input, cursor: "2" },
161 ]) {
162 run.mockReturnValueOnce(failure("not-found"));
163 yield* cli.getMergeRequestDiff(target).pipe(Effect.flip);
164 }
165 expect(run).toHaveBeenCalledTimes(2);
166 }),
167 );
168 it.effect("propagates failure of the legacy endpoint", () =>
169 Effect.gen(function* () {
170 run.mockReturnValueOnce(failure("not-found"));
171 run.mockReturnValueOnce(failure("not-found"));
172 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
173 expect((yield* cli.getMergeRequestDiff(input).pipe(Effect.flip))._tag).toBe(
174 "GitLabCliCommandError",
175 );
176 expect(run).toHaveBeenCalledTimes(2);
177 }),
178 );
179 it.effect("does not fall back on an unreadable successful modern response", () =>
180 Effect.gen(function* () {
181 run.mockReturnValueOnce(output({ changes: [file] }));
182 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
183 expect((yield* cli.getMergeRequestDiff(input).pipe(Effect.flip))._tag).toBe(
184 "GitLabMergeRequestReadError",
185 );
186 expect(run).toHaveBeenCalledTimes(1);
187 }),
188 );
189 it.effect("keeps modern pagination without requesting legacy changes", () =>
190 Effect.gen(function* () {
191 run.mockReturnValueOnce(output(Array.from({ length: 100 }, () => file)));
192 run.mockReturnValueOnce(output([file]));
193 const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
194 const first = yield* cli.getMergeRequestDiff(input);
195 expect(first.nextCursor).toBe("2");
196 expect(
197 (yield* cli.getMergeRequestDiff({ ...input, cursor: first.nextCursor! })).nextCursor,
198 ).toBeNull();
199 expect(run).toHaveBeenCalledTimes(2);
200 expect(run.mock.calls[1]?.[0].args).toContain(
201 "projects/acme%2Fnested%2Fweb/merge_requests/7/diffs?per_page=100&page=2",
202 );
203 }),
204 );
205});