Open History to see the last commit
1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { assert, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import { ChildProcessSpawner } from "effect/process";
7
8import * as GitLabCli from "./GitLabCli.ts";
9import { parseGitLabAuthStatusHosts } from "./gitLabAuthStatus.ts";
10import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
11
12function makeProvider(gitlab: Partial<GitLabCli.GitLabCli["Service"]>) {
13 return GitLabSourceControlProvider.make.pipe(
14 Effect.provide(Layer.mock(GitLabCli.GitLabCli)(gitlab)),
15 );
16}
17
18it.effect("maps GitLab MR summaries into provider-neutral change requests", () =>
19 Effect.gen(function* () {
20 const provider = yield* makeProvider({
21 getMergeRequest: () =>
22 Effect.succeed({
23 number: 42,
24 title: "Add GitLab provider",
25 url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/42",
26 baseRefName: "main",
27 headRefName: "feature/source-control",
28 state: "closed",
29 closedAt: "2026-08-23T10:00:00Z",
30 isCrossRepository: true,
31 headRepositoryNameWithOwner: "fork/t3code",
32 headRepositoryOwnerLogin: "fork",
33 }),
34 });
35
36 const changeRequest = yield* provider.getChangeRequest({
37 cwd: "/repo",
38 reference: "42",
39 });
40
41 assert.deepStrictEqual(changeRequest, {
42 provider: SourceControlProviderKind.make("gitlab"),
43 number: 42,
44 title: "Add GitLab provider",
45 url: "https://gitlab.com/pingdotgg/t3code/-/merge_requests/42",
46 baseRefName: "main",
47 headRefName: "feature/source-control",
48 state: "closed",
49 closedAt: "2026-08-23T10:00:00Z",
50 mergedAt: null,
51 updatedAt: Option.none(),
52 isCrossRepository: true,
53 headRepositoryNameWithOwner: "fork/t3code",
54 headRepositoryOwnerLogin: "fork",
55 });
56 }),
57);
58
59it.effect("adds repository context while retaining GitLab CLI causes", () =>
60 Effect.gen(function* () {
61 const cause = new GitLabCli.GitLabCliCommandError({
62 operation: "execute",
63 command: "glab",
64 cwd: "/repo",
65 cause: new Error("raw upstream detail that should remain in the cause"),
66 });
67 const provider = yield* makeProvider({
68 createRepository: () => Effect.fail(cause),
69 });
70
71 const error = yield* provider
72 .createRepository({
73 cwd: "/repo",
74 repository: "owner/repo",
75 visibility: "private",
76 })
77 .pipe(Effect.flip);
78
79 assert.deepStrictEqual(
80 {
81 provider: error.provider,
82 operation: error.operation,
83 command: error.command,
84 cwd: error.cwd,
85 repository: error.repository,
86 detail: error.detail,
87 },
88 {
89 provider: "gitlab",
90 operation: "createRepository",
91 command: "glab",
92 cwd: "/repo",
93 repository: "owner/repo",
94 detail: "GitLab CLI command failed.",
95 },
96 );
97 assert.strictEqual(error.cause, cause);
98 assert.equal(error.message.includes("raw upstream detail"), false);
99 }),
100);
101
102it.effect("lists GitLab MRs through provider-neutral input names", () =>
103 Effect.gen(function* () {
104 let listInput: Parameters<GitLabCli.GitLabCli["Service"]["listMergeRequests"]>[0] | null = null;
105 const provider = yield* makeProvider({
106 listMergeRequests: (input) => {
107 listInput = input;
108 return Effect.succeed([]);
109 },
110 });
111
112 yield* provider.listChangeRequests({
113 cwd: "/repo",
114 headSelector: "feature/provider",
115 state: "all",
116 limit: 10,
117 });
118
119 assert.deepStrictEqual(listInput, {
120 cwd: "/repo",
121 headSelector: "feature/provider",
122 state: "all",
123 limit: 10,
124 });
125 }),
126);
127
128it.effect("creates GitLab MRs through provider-neutral input names", () =>
129 Effect.gen(function* () {
130 let createInput: Parameters<GitLabCli.GitLabCli["Service"]["createMergeRequest"]>[0] | null =
131 null;
132 const provider = yield* makeProvider({
133 createMergeRequest: (input) => {
134 createInput = input;
135 return Effect.void;
136 },
137 });
138
139 yield* provider.createChangeRequest({
140 cwd: "/repo",
141 baseRefName: "main",
142 headSelector: "owner:feature/provider",
143 title: "Provider MR",
144 bodyFile: "/tmp/body.md",
145 });
146
147 assert.deepStrictEqual(createInput, {
148 cwd: "/repo",
149 baseBranch: "main",
150 headSelector: "owner:feature/provider",
151 source: {
152 owner: "owner",
153 refName: "feature/provider",
154 },
155 title: "Provider MR",
156 bodyFile: "/tmp/body.md",
157 });
158 }),
159);
160
161it("accepts authenticated GitLab hosts when another configured host fails", () => {
162 const auth = GitLabSourceControlProvider.discovery.parseAuth({
163 exitCode: ChildProcessSpawner.ExitCode(1),
164 stdout: `gitlab.com
165 x gitlab.com: API call failed: 401 Unauthorized
166 ! No token found
167self-hosted.example.test
168 ✓ Logged in to self-hosted.example.test as gitlab-user
169 ✓ Token found: ******
170`,
171 stderr: "",
172 });
173
174 assert.deepStrictEqual(
175 {
176 status: auth.status,
177 account: auth.account,
178 host: auth.host,
179 },
180 {
181 status: "authenticated",
182 account: Option.some("gitlab-user"),
183 host: Option.some("self-hosted.example.test"),
184 },
185 );
186});
187
188it("refines unknown GitLab remotes with mixed-case provider hosts", () => {
189 const provider = GitLabSourceControlProvider.discovery.refineUnknownRemote?.({
190 cwd: "/repo",
191 context: {
192 provider: {
193 kind: SourceControlProviderKind.make("unknown"),
194 name: "Self-Hosted.Example.Test",
195 baseUrl: "https://Self-Hosted.Example.Test",
196 },
197 remoteName: "origin",
198 remoteUrl: "https://Self-Hosted.Example.Test/group/project.git",
199 },
200 auth: {
201 exitCode: ChildProcessSpawner.ExitCode(0),
202 stdout: `self-hosted.example.test
203 ✓ Logged in to self-hosted.example.test as gitlab-user
204 ✓ Token found: ******
205`,
206 stderr: "",
207 },
208 });
209
210 assert.deepStrictEqual(provider, {
211 kind: "gitlab",
212 name: "GitLab Self-Hosted",
213 baseUrl: "https://Self-Hosted.Example.Test",
214 });
215});
216
217it("tells a host glab has no login for apart from output that cannot say", () => {
218 const refine = (stderr: string) =>
219 GitLabSourceControlProvider.discovery.refineUnknownRemote?.({
220 cwd: "/repo",
221 context: {
222 provider: {
223 kind: SourceControlProviderKind.make("unknown"),
224 name: "git.example.test",
225 baseUrl: "https://git.example.test",
226 },
227 remoteName: "origin",
228 remoteUrl: "https://git.example.test/group/project.git",
229 },
230 auth: { exitCode: ChildProcessSpawner.ExitCode(1), stdout: "", stderr },
231 });
232
233 assert.strictEqual(
234 refine("x git.example.test has not been authenticated with glab. Run `glab auth login`"),
235 null,
236 );
237 assert.strictEqual(
238 refine("No GitLab instances have been authenticated with glab. Run `glab auth login`"),
239 null,
240 );
241 assert.strictEqual(refine("git.example.test\n x API call failed: 503"), undefined);
242});
243
244it("parses authenticated GitLab auth status hosts with ports and single-label names", () => {
245 assert.deepStrictEqual(
246 parseGitLabAuthStatusHosts(`localhost:8080
247 ✓ Logged in to localhost:8080 as local-user
248selfhosted
249 ✓ Logged in to selfhosted as single-label-user
250`),
251 [
252 { host: "localhost:8080", account: "local-user" },
253 { host: "selfhosted", account: "single-label-user" },
254 ],
255 );
256});
257
258it.effect.each(["merge_requests", "issues"])(
259 "resolves %s subjects on the linked host without using the checkout",
260 (kind) =>
261 Effect.gen(function* () {
262 const provider = yield* makeProvider({
263 execute: (input) => {
264 assert.deepStrictEqual(input.args, [
265 "api",
266 "--hostname",
267 "gitlab.com",
268 `projects/group%2Fsubgroup%2Fproject/${kind}/42`,
269 ]);
270 assert.strictEqual(input.maxOutputBytes, 32_000);
271 assert.strictEqual(input.timeoutMs, 3_000);
272 return Effect.succeed({
273 exitCode: ChildProcessSpawner.ExitCode(0),
274 stdout: JSON.stringify({
275 title: "Pairing expiry",
276 description: "Preserve remote access",
277 }),
278 stderr: "",
279 stdoutTruncated: false,
280 stderrTruncated: false,
281 });
282 },
283 });
284 const lookup = provider.resolveLink?.({
285 cwd: "/unrelated",
286 url: new URL(`https://gitlab.com/group/subgroup/project/-/${kind}/42`),
287 });
288 assert.ok(lookup);
289 assert.deepStrictEqual(yield* lookup, {
290 title: "Pairing expiry",
291 body: "Preserve remote access",
292 });
293 assert.strictEqual(
294 provider.resolveLink?.({
295 cwd: "/unrelated",
296 url: new URL("https://gitlab.com/owner/repo"),
297 }),
298 undefined,
299 );
300 }),
301);
302
303it.effect.each(["read", "decode"] as const)(
304 "retains the %s failure without exposing its raw contents",
305 (stage) =>
306 Effect.gen(function* () {
307 const cause = new GitLabCli.GitLabCliCommandError({
308 command: "glab",
309 cwd: "/repo",
310 operation: "execute",
311 cause: new Error("private response text"),
312 });
313 const provider = yield* makeProvider({
314 execute: () =>
315 stage === "read"
316 ? Effect.fail(cause)
317 : Effect.succeed({
318 exitCode: ChildProcessSpawner.ExitCode(0),
319 stdout: "private response text",
320 stderr: "",
321 stdoutTruncated: false,
322 stderrTruncated: false,
323 }),
324 });
325 const lookup = provider.resolveLink?.({
326 cwd: "/repo",
327 url: new URL("https://gitlab.com/owner/repo/-/issues/42"),
328 });
329 assert.ok(lookup);
330 const error = yield* Effect.flip(lookup);
331 assert.strictEqual(error.operation, stage === "read" ? "resolveLink" : "resolveLink.decode");
332 assert.strictEqual(error.detail, "The linked subject could not be read.");
333 assert.notInclude(error.message, "private response text");
334 if (stage === "read") assert.strictEqual(error.cause, cause);
335 else assert.propertyVal(error.cause, "_tag", "SchemaError");
336 }),
337);