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

gitLabDiscovery.test.ts

259 lines9.3 KB

1import {
2 parseGitRemoteHost,
3 unknownRemoteProvider,
4} from "@t3tools/source-control-core/shared/definition";
5import {
6 probeSourceControlProvider,
7 refineUnknownRemoteProvider,
8} from "@t3tools/source-control-core/server/discovery";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10import { SpawnExecutableResolution } from "@t3tools/shared/shell";
11import { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost";
12import { VcsProcessSpawnError } from "@t3tools/contracts";
13import { assert, it } from "@effect/vitest";
14import * as Effect from "effect/Effect";
15import * as Option from "effect/Option";
16import * as PlatformError from "effect/PlatformError";
17import { ChildProcessSpawner } from "effect/process";
18​
19import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
20​
21const onPath = (command: string) => `/usr/bin/${command}`;
22​
23it("reads the account from glab output without status marks", () => {
24 const auth = GitLabSourceControlProvider.discovery.parseAuth(
25 processOutput(`gitlab.com
26Logged in to gitlab.com as gitlab-user
27`),
28 );
29 assert.strictEqual(auth.status, "authenticated");
30 assert.deepStrictEqual(auth.account, Option.some("gitlab-user"));
31});
32​
33// Without `--all`, glab 1.79+ checks only the current host unless that is gitlab.com.
34it.effect("reports a signed-in host when glab's default host is signed out", () =>
35 Effect.gen(function* () {
36 const defaultHostOnly = `gitlab.gnome.org
37 x gitlab.gnome.org: API call failed: GET https://gitlab.gnome.org/api/v4/user: 401 {message: 401 Unauthorized}
38`;
39 const allHosts = `gitlab.com
40 ✓ Logged in to gitlab.com as gitlab-user (/home/user/.config/glab-cli/config.yml)
41${defaultHostOnly}`;
42 const item = yield* probeSourceControlProvider({
43 spec: GitLabSourceControlProvider.discovery,
44 cwd: "/repo",
45 process: {
46 run: ({ args }) =>
47 Effect.succeed(
48 args[0] === "--version"
49 ? processOutput("glab 1.124.0\n")
50 : processOutput(args.includes("--all") ? allHosts : defaultHostOnly, {
51 exitCode: ChildProcessSpawner.ExitCode(1),
52 }),
53 ),
54 },
55 });
56 assert.strictEqual(item.auth.status, "authenticated");
57 assert.deepStrictEqual(item.auth.account, Option.some("gitlab-user"));
58 assert.deepStrictEqual(item.auth.host, Option.some("gitlab.com"));
59 }).pipe(Effect.provideService(SpawnExecutableResolution, onPath)),
60);
61​
62// Discovery sweeps every integration on a timer; a CLI that is not installed should cost one spawn.
63it.effect("does not run the auth command when glab is not installed", () =>
64 Effect.gen(function* () {
65 const spawned: Array<string> = [];
66 const item = yield* probeSourceControlProvider({
67 spec: GitLabSourceControlProvider.discovery,
68 cwd: "/repo",
69 process: {
70 run: ({ operation, command, args, cwd }) => {
71 spawned.push([command, ...args].join(" "));
72 return Effect.fail(
73 new VcsProcessSpawnError({
74 operation,
75 command,
76 cwd,
77 cause: PlatformError.systemError({
78 _tag: "NotFound",
79 module: "ChildProcess",
80 method: "spawn",
81 pathOrDescriptor: command,
82 }),
83 }),
84 );
85 },
86 },
87 });
88 assert.strictEqual(item.status, "missing");
89 assert.deepStrictEqual(spawned, ["glab --version"]);
90 }).pipe(Effect.provideService(SpawnExecutableResolution, onPath)),
91);
92​
93it.effect("spawns nothing for a glab that is not on PATH", () =>
94 Effect.gen(function* () {
95 const spawned: Array<string> = [];
96 const item = yield* probeSourceControlProvider({
97 spec: GitLabSourceControlProvider.discovery,
98 cwd: "/repo",
99 process: {
100 run: ({ command, args }) => {
101 spawned.push([command, ...args].join(" "));
102 return Effect.succeed(processOutput("glab 1.124.0\n"));
103 },
104 },
105 });
106 assert.strictEqual(item.status, "missing");
107 assert.strictEqual(item.auth.status, "unknown");
108 assert.deepStrictEqual(spawned, []);
109 }).pipe(Effect.provideService(SpawnExecutableResolution, () => undefined)),
110);
111​
112it.effect("skips --all when the current glab host is signed in", () =>
113 Effect.gen(function* () {
114 const asked: Array<ReadonlyArray<string>> = [];
115 const item = yield* probeSourceControlProvider({
116 spec: GitLabSourceControlProvider.discovery,
117 cwd: "/repo",
118 process: {
119 run: ({ args }) => {
120 asked.push(args);
121 return Effect.succeed(
122 args[0] === "--version"
123 ? processOutput("glab 1.124.0\n")
124 : processOutput(`gitlab.example.test
125 ✓ Logged in to gitlab.example.test as gitlab-user
126`),
127 );
128 },
129 },
130 });
131 assert.strictEqual(item.auth.status, "authenticated");
132 assert.isFalse(asked.some((args) => args.includes("--all")));
133 }).pipe(Effect.provideService(SpawnExecutableResolution, onPath)),
134);
135​
136it.effect("keeps glab's own sign-out detail when glab predates --all", () =>
137 Effect.gen(function* () {
138 const item = yield* probeSourceControlProvider({
139 spec: GitLabSourceControlProvider.discovery,
140 cwd: "/repo",
141 process: {
142 run: ({ args }) =>
143 Effect.succeed(
144 args[0] === "--version"
145 ? processOutput("glab 1.53.0\n")
146 : args.includes("--all")
147 ? processOutput("", {
148 stderr: "unknown flag: --all\n",
149 exitCode: ChildProcessSpawner.ExitCode(1),
150 })
151 : processOutput("", {
152 stderr: "No GitLab instances have been authenticated with glab.\n",
153 exitCode: ChildProcessSpawner.ExitCode(1),
154 }),
155 ),
156 },
157 });
158 assert.strictEqual(item.auth.status, "unauthenticated");
159 assert.deepStrictEqual(
160 item.auth.detail,
161 Option.some("No GitLab instances have been authenticated with glab."),
162 );
163 }).pipe(Effect.provideService(SpawnExecutableResolution, onPath)),
164);
165​
166it.effect("sends the environment token only to glab's default host while probing", () =>
167 Effect.gen(function* () {
168 const withheld = { GITLAB_TOKEN: "", GITLAB_ACCESS_TOKEN: "", OAUTH_TOKEN: "" };
169 const probes: Array<readonly [string, unknown]> = [];
170 const process: Parameters<typeof probeSourceControlProvider>[0]["process"] = {
171 run: ({ args, env }) => {
172 if (args[0] !== "--version") probes.push([args.join(" "), env]);
173 return Effect.succeed(
174 processOutput("", {
175 stderr: "No GitLab instances have been authenticated with glab.\n",
176 exitCode: ChildProcessSpawner.ExitCode(1),
177 }),
178 );
179 },
180 };
181 const remoteUrl = "https://self-hosted.example.test/group/project.git";
182 const remote = parseGitRemoteHost(remoteUrl);
183 assert.ok(remote);
184 yield* probeSourceControlProvider({
185 spec: GitLabSourceControlProvider.discovery,
186 cwd: "/repo",
187 process,
188 });
189 yield* refineUnknownRemoteProvider({
190 specs: [GitLabSourceControlProvider.discovery],
191 cwd: "/repo",
192 context: { provider: unknownRemoteProvider(remote), remoteName: "origin", remoteUrl },
193 process,
194 });
195 assert.deepStrictEqual(probes, [
196 ["auth status", withheld],
197 ["auth status --all", withheld],
198 ["auth status --hostname gitlab.com", {}],
199 ["auth status --hostname self-hosted.example.test", withheld],
200 ]);
201 }).pipe(
202 Effect.provideService(HostProcess.Environment, { GITLAB_TOKEN: "default-host-token" }),
203 Effect.provideService(SpawnExecutableResolution, onPath),
204 ),
205);
206​
207it.effect.each([
208 {
209 name: "without relying on host naming",
210 remoteUrl: "https://self-hosted.example.test/group/project.git",
211 host: "self-hosted.example.test",
212 stdout: `gitlab.com
213 x gitlab.com: API call failed: 401 Unauthorized
214 ! No token found
215self-hosted.example.test
216 ✓ Logged in to self-hosted.example.test as gitlab-user
217 ✓ Token found: ******
218`,
219 exitCode: 1,
220 },
221 {
222 name: "on a non-standard port",
223 remoteUrl: "https://self-hosted.example.test:8443/group/project.git",
224 host: "self-hosted.example.test:8443",
225 stdout: `self-hosted.example.test:8443
226 ✓ Logged in to self-hosted.example.test:8443 as gitlab-user
227 ✓ Token found: ******
228`,
229 exitCode: 0,
230 },
231] as const)("claims a self-hosted remote glab is signed in to $name", (scenario) =>
232 Effect.gen(function* () {
233 const remote = parseGitRemoteHost(scenario.remoteUrl);
234 assert.ok(remote);
235 const asked: Array<ReadonlyArray<string>> = [];
236 const verdict = yield* refineUnknownRemoteProvider({
237 specs: [GitLabSourceControlProvider.discovery],
238 cwd: "/repo",
239 context: {
240 provider: unknownRemoteProvider(remote),
241 remoteName: "origin",
242 remoteUrl: scenario.remoteUrl,
243 },
244 process: {
245 run: ({ command, args }) => {
246 asked.push([command, ...args]);
247 return Effect.succeed(
248 processOutput(scenario.stdout, {
249 exitCode: ChildProcessSpawner.ExitCode(scenario.exitCode),
250 }),
251 );
252 },
253 },
254 });
255 assert.deepStrictEqual(asked, [["glab", "auth", "status", "--hostname", scenario.host]]);
256 assert.strictEqual(verdict.provider?.kind, "gitlab");
257 assert.strictEqual(verdict.settled, true);
258 }),
259);