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

gitLabDiscovery.test.ts

201 lines7.1 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 { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost";
11import { assert, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13import * as Option from "effect/Option";
14import { ChildProcessSpawner } from "effect/process";
15​
16import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
17​
18it("reads the account from glab output without status marks", () => {
19 const auth = GitLabSourceControlProvider.discovery.parseAuth(
20 processOutput(`gitlab.com
21Logged in to gitlab.com as gitlab-user
22`),
23 );
24 assert.strictEqual(auth.status, "authenticated");
25 assert.deepStrictEqual(auth.account, Option.some("gitlab-user"));
26});
27​
28// Without `--all`, glab 1.79+ checks only the current host unless that is gitlab.com.
29it.effect("reports a signed-in host when glab's default host is signed out", () =>
30 Effect.gen(function* () {
31 const defaultHostOnly = `gitlab.gnome.org
32 x gitlab.gnome.org: API call failed: GET https://gitlab.gnome.org/api/v4/user: 401 {message: 401 Unauthorized}
33`;
34 const allHosts = `gitlab.com
35 ✓ Logged in to gitlab.com as gitlab-user (/home/user/.config/glab-cli/config.yml)
36${defaultHostOnly}`;
37 const item = yield* probeSourceControlProvider({
38 spec: GitLabSourceControlProvider.discovery,
39 cwd: "/repo",
40 process: {
41 run: ({ args }) =>
42 Effect.succeed(
43 args[0] === "--version"
44 ? processOutput("glab 1.124.0\n")
45 : processOutput(args.includes("--all") ? allHosts : defaultHostOnly, {
46 exitCode: ChildProcessSpawner.ExitCode(1),
47 }),
48 ),
49 },
50 });
51 assert.strictEqual(item.auth.status, "authenticated");
52 assert.deepStrictEqual(item.auth.account, Option.some("gitlab-user"));
53 assert.deepStrictEqual(item.auth.host, Option.some("gitlab.com"));
54 }),
55);
56​
57it.effect("skips --all when the current glab host is signed in", () =>
58 Effect.gen(function* () {
59 const asked: Array<ReadonlyArray<string>> = [];
60 const item = yield* probeSourceControlProvider({
61 spec: GitLabSourceControlProvider.discovery,
62 cwd: "/repo",
63 process: {
64 run: ({ args }) => {
65 asked.push(args);
66 return Effect.succeed(
67 args[0] === "--version"
68 ? processOutput("glab 1.124.0\n")
69 : processOutput(`gitlab.example.test
70 ✓ Logged in to gitlab.example.test as gitlab-user
71`),
72 );
73 },
74 },
75 });
76 assert.strictEqual(item.auth.status, "authenticated");
77 assert.isFalse(asked.some((args) => args.includes("--all")));
78 }),
79);
80​
81it.effect("keeps glab's own sign-out detail when glab predates --all", () =>
82 Effect.gen(function* () {
83 const item = yield* probeSourceControlProvider({
84 spec: GitLabSourceControlProvider.discovery,
85 cwd: "/repo",
86 process: {
87 run: ({ args }) =>
88 Effect.succeed(
89 args[0] === "--version"
90 ? processOutput("glab 1.53.0\n")
91 : args.includes("--all")
92 ? processOutput("", {
93 stderr: "unknown flag: --all\n",
94 exitCode: ChildProcessSpawner.ExitCode(1),
95 })
96 : processOutput("", {
97 stderr: "No GitLab instances have been authenticated with glab.\n",
98 exitCode: ChildProcessSpawner.ExitCode(1),
99 }),
100 ),
101 },
102 });
103 assert.strictEqual(item.auth.status, "unauthenticated");
104 assert.deepStrictEqual(
105 item.auth.detail,
106 Option.some("No GitLab instances have been authenticated with glab."),
107 );
108 }),
109);
110​
111it.effect("sends the environment token only to glab's default host while probing", () =>
112 Effect.gen(function* () {
113 const withheld = { GITLAB_TOKEN: "", GITLAB_ACCESS_TOKEN: "", OAUTH_TOKEN: "" };
114 const probes: Array<readonly [string, unknown]> = [];
115 const process: Parameters<typeof probeSourceControlProvider>[0]["process"] = {
116 run: ({ args, env }) => {
117 if (args[0] !== "--version") probes.push([args.join(" "), env]);
118 return Effect.succeed(
119 processOutput("", {
120 stderr: "No GitLab instances have been authenticated with glab.\n",
121 exitCode: ChildProcessSpawner.ExitCode(1),
122 }),
123 );
124 },
125 };
126 const remoteUrl = "https://self-hosted.example.test/group/project.git";
127 const remote = parseGitRemoteHost(remoteUrl);
128 assert.ok(remote);
129 yield* probeSourceControlProvider({
130 spec: GitLabSourceControlProvider.discovery,
131 cwd: "/repo",
132 process,
133 });
134 yield* refineUnknownRemoteProvider({
135 specs: [GitLabSourceControlProvider.discovery],
136 cwd: "/repo",
137 context: { provider: unknownRemoteProvider(remote), remoteName: "origin", remoteUrl },
138 process,
139 });
140 assert.deepStrictEqual(probes, [
141 ["auth status", withheld],
142 ["auth status --all", withheld],
143 ["auth status --hostname gitlab.com", {}],
144 ["auth status --hostname self-hosted.example.test", withheld],
145 ]);
146 }).pipe(Effect.provideService(HostProcess.Environment, { GITLAB_TOKEN: "default-host-token" })),
147);
148​
149it.effect.each([
150 {
151 name: "without relying on host naming",
152 remoteUrl: "https://self-hosted.example.test/group/project.git",
153 host: "self-hosted.example.test",
154 stdout: `gitlab.com
155 x gitlab.com: API call failed: 401 Unauthorized
156 ! No token found
157self-hosted.example.test
158 ✓ Logged in to self-hosted.example.test as gitlab-user
159 ✓ Token found: ******
160`,
161 exitCode: 1,
162 },
163 {
164 name: "on a non-standard port",
165 remoteUrl: "https://self-hosted.example.test:8443/group/project.git",
166 host: "self-hosted.example.test:8443",
167 stdout: `self-hosted.example.test:8443
168 ✓ Logged in to self-hosted.example.test:8443 as gitlab-user
169 ✓ Token found: ******
170`,
171 exitCode: 0,
172 },
173] as const)("claims a self-hosted remote glab is signed in to $name", (scenario) =>
174 Effect.gen(function* () {
175 const remote = parseGitRemoteHost(scenario.remoteUrl);
176 assert.ok(remote);
177 const asked: Array<ReadonlyArray<string>> = [];
178 const verdict = yield* refineUnknownRemoteProvider({
179 specs: [GitLabSourceControlProvider.discovery],
180 cwd: "/repo",
181 context: {
182 provider: unknownRemoteProvider(remote),
183 remoteName: "origin",
184 remoteUrl: scenario.remoteUrl,
185 },
186 process: {
187 run: ({ command, args }) => {
188 asked.push([command, ...args]);
189 return Effect.succeed(
190 processOutput(scenario.stdout, {
191 exitCode: ChildProcessSpawner.ExitCode(scenario.exitCode),
192 }),
193 );
194 },
195 },
196 });
197 assert.deepStrictEqual(asked, [["glab", "auth", "status", "--hostname", scenario.host]]);
198 assert.strictEqual(verdict.provider?.kind, "gitlab");
199 assert.strictEqual(verdict.settled, true);
200 }),
201);