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 // A settled "no" is cached; anything else makes the next read ask glab again.
234 assert.strictEqual(
235 refine("x git.example.test has not been authenticated with glab. Run `glab auth login`"),
236 null,
237 );
238 assert.strictEqual(
239 refine("No GitLab instances have been authenticated with glab. Run `glab auth login`"),
240 null,
241 );
242 assert.strictEqual(refine("git.example.test\n x API call failed: 503"), undefined);
243});
244
245it("parses authenticated GitLab auth status hosts with ports and single-label names", () => {
246 assert.deepStrictEqual(
247 parseGitLabAuthStatusHosts(`localhost:8080
248 ✓ Logged in to localhost:8080 as local-user
249selfhosted
250 ✓ Logged in to selfhosted as single-label-user
251`),
252 [
253 { host: "localhost:8080", account: "local-user" },
254 { host: "selfhosted", account: "single-label-user" },
255 ],
256 );
257});
258
259it.effect.each(["merge_requests", "issues"])(
260 "resolves %s subjects on the linked host without using the checkout",
261 (kind) =>
262 Effect.gen(function* () {
263 const provider = yield* makeProvider({
264 execute: (input) => {
265 assert.deepStrictEqual(input.args, [
266 "api",
267 "--hostname",
268 "gitlab.com",
269 `projects/group%2Fsubgroup%2Fproject/${kind}/42`,
270 ]);
271 assert.strictEqual(input.maxOutputBytes, 32_000);
272 assert.strictEqual(input.timeoutMs, 3_000);
273 return Effect.succeed({
274 exitCode: ChildProcessSpawner.ExitCode(0),
275 stdout: JSON.stringify({
276 title: "Pairing expiry",
277 description: "Preserve remote access",
278 }),
279 stderr: "",
280 stdoutTruncated: false,
281 stderrTruncated: false,
282 });
283 },
284 });
285 const lookup = provider.resolveLink?.({
286 cwd: "/unrelated",
287 url: new URL(`https://gitlab.com/group/subgroup/project/-/${kind}/42`),
288 });
289 assert.ok(lookup);
290 assert.deepStrictEqual(yield* lookup, {
291 title: "Pairing expiry",
292 body: "Preserve remote access",
293 });
294 assert.strictEqual(
295 provider.resolveLink?.({
296 cwd: "/unrelated",
297 url: new URL("https://gitlab.com/owner/repo"),
298 }),
299 undefined,
300 );
301 }),
302);
303
304it.effect.each(["read", "decode"] as const)(
305 "retains the %s failure without exposing its raw contents",
306 (stage) =>
307 Effect.gen(function* () {
308 const cause = new GitLabCli.GitLabCliCommandError({
309 command: "glab",
310 cwd: "/repo",
311 operation: "execute",
312 cause: new Error("private response text"),
313 });
314 const provider = yield* makeProvider({
315 execute: () =>
316 stage === "read"
317 ? Effect.fail(cause)
318 : Effect.succeed({
319 exitCode: ChildProcessSpawner.ExitCode(0),
320 stdout: "private response text",
321 stderr: "",
322 stdoutTruncated: false,
323 stderrTruncated: false,
324 }),
325 });
326 const lookup = provider.resolveLink?.({
327 cwd: "/repo",
328 url: new URL("https://gitlab.com/owner/repo/-/issues/42"),
329 });
330 assert.ok(lookup);
331 const error = yield* Effect.flip(lookup);
332 assert.strictEqual(error.operation, stage === "read" ? "resolveLink" : "resolveLink.decode");
333 assert.strictEqual(error.detail, "The linked subject could not be read.");
334 assert.notInclude(error.message, "private response text");
335 if (stage === "read") assert.strictEqual(error.cause, cause);
336 else assert.propertyVal(error.cause, "_tag", "SchemaError");
337 }),
338);