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

SourceControlProviderRegistry.test.ts

295 lines9.0 KB

1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as DateTime from "effect/DateTime";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import { ChildProcessSpawner } from "effect/unstable/process";
8import { VcsRepositoryDetectionError } from "@t3tools/contracts";
9​
10import * as ServerConfig from "../config.ts";
11import type * as VcsDriver from "../vcs/VcsDriver.ts";
12import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
13import * as VcsProcess from "../vcs/VcsProcess.ts";
14import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
15import * as BitbucketApi from "./BitbucketApi.ts";
16import * as GitHubCli from "./GitHubCli.ts";
17import * as GitLabCli from "./GitLabCli.ts";
18import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
19​
20const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z");
21​
22const processOutput = (
23 stdout: string,
24 options?: {
25 readonly stderr?: string;
26 readonly exitCode?: ChildProcessSpawner.ExitCode;
27 },
28): VcsProcess.VcsProcessOutput => ({
29 exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
30 stdout,
31 stderr: options?.stderr ?? "",
32 stdoutTruncated: false,
33 stderrTruncated: false,
34});
35​
36function makeRegistry(input: {
37 readonly remotes: ReadonlyArray<{
38 readonly name: string;
39 readonly url: string;
40 }>;
41 readonly process?: Partial<VcsProcess.VcsProcess["Service"]>;
42 readonly resolve?: VcsDriverRegistry.VcsDriverRegistry["Service"]["resolve"];
43}) {
44 const driver = {
45 listRemotes: () =>
46 Effect.succeed({
47 remotes: input.remotes.map((remote) => ({
48 ...remote,
49 pushUrl: Option.none(),
50 isPrimary: remote.name === "origin",
51 })),
52 freshness: {
53 source: "live-local" as const,
54 observedAt: TEST_EPOCH,
55 expiresAt: Option.none(),
56 },
57 }),
58 } satisfies Partial<VcsDriver.VcsDriver["Service"]>;
59​
60 const registryLayer = Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
61 get: () => Effect.succeed(driver as unknown as VcsDriver.VcsDriver["Service"]),
62 resolve:
63 input.resolve ??
64 (() =>
65 Effect.succeed({
66 kind: "git",
67 repository: {
68 kind: "git",
69 rootPath: "/repo",
70 metadataPath: null,
71 freshness: {
72 source: "live-local" as const,
73 observedAt: TEST_EPOCH,
74 expiresAt: Option.none(),
75 },
76 },
77 driver: driver as unknown as VcsDriver.VcsDriver["Service"],
78 })),
79 });
80​
81 const processLayer = Layer.mock(VcsProcess.VcsProcess)({
82 run: () => Effect.succeed(processOutput("")),
83 ...input.process,
84 });
85​
86 return SourceControlProviderRegistry.make.pipe(
87 Effect.provide(
88 Layer.mergeAll(
89 registryLayer,
90 processLayer,
91 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
92 Layer.mock(BitbucketApi.BitbucketApi)({}),
93 Layer.mock(GitHubCli.GitHubCli)({}),
94 Layer.mock(GitLabCli.GitLabCli)({}),
95 ServerConfig.layerTest(process.cwd(), {
96 prefix: "t3-source-control-registry-test-",
97 }).pipe(Layer.provide(NodeServices.layer)),
98 ),
99 ),
100 );
102​
103it.effect("routes GitHub remotes to the GitHub provider", () =>
104 Effect.gen(function* () {
105 const registry = yield* makeRegistry({
106 remotes: [{ name: "origin", url: "git@github.com:pingdotgg/t3code.git" }],
107 });
108​
109 const provider = yield* registry.resolve({ cwd: "/repo" });
110​
111 assert.strictEqual(provider.kind, "github");
112 }),
113);
114​
115it.effect("routes directly by provider kind for remote-first workflows", () =>
116 Effect.gen(function* () {
117 const registry = yield* makeRegistry({
118 remotes: [],
119 });
120​
121 const provider = yield* registry.get("github");
122​
123 assert.strictEqual(provider.kind, "github");
124 }),
125);
126​
127it.effect("includes the request cwd when an unregistered provider is used", () =>
128 Effect.gen(function* () {
129 const registry = yield* makeRegistry({ remotes: [] });
130 const provider = yield* registry.get("unknown");
131​
132 const error = yield* provider
133 .getChangeRequest({ cwd: "/repo", reference: "#42" })
134 .pipe(Effect.flip);
135​
136 assert.strictEqual(error.provider, "unknown");
137 assert.strictEqual(error.operation, "getChangeRequest");
138 assert.strictEqual(error.cwd, "/repo");
139 assert.strictEqual(error.reference, "#42");
140 }),
141);
142​
143it.effect("retains VCS detection failures with structured cwd context", () =>
144 Effect.gen(function* () {
145 const cause = new VcsRepositoryDetectionError({
146 operation: "resolve",
147 cwd: "/repo",
148 detail: "raw VCS detection failure",
149 cause: new Error("raw nested failure"),
150 });
151 const registry = yield* makeRegistry({
152 remotes: [],
153 resolve: () => Effect.fail(cause),
154 });
155​
156 const error = yield* registry.resolve({ cwd: "/repo" }).pipe(Effect.flip);
157​
158 assert.strictEqual(error.provider, "unknown");
159 assert.strictEqual(error.operation, "detectProvider");
160 assert.strictEqual(error.cwd, "/repo");
161 assert.strictEqual(error.detail, "Failed to detect source control provider.");
162 assert.strictEqual(error.cause, cause);
163 assert.equal(error.message.includes(cause.message), false);
164 }),
165);
166​
167it.effect("routes GitLab remotes to the GitLab provider", () =>
168 Effect.gen(function* () {
169 const registry = yield* makeRegistry({
170 remotes: [{ name: "origin", url: "git@gitlab.com:group/project.git" }],
171 });
172​
173 const provider = yield* registry.resolve({ cwd: "/repo" });
174​
175 assert.strictEqual(provider.kind, "gitlab");
176 }),
177);
178​
179it.effect("routes authenticated self-hosted GitLab remotes without relying on host naming", () =>
180 Effect.gen(function* () {
181 const registry = yield* makeRegistry({
182 remotes: [{ name: "origin", url: "https://self-hosted.example.test/group/project.git" }],
183 process: {
184 run: () =>
185 Effect.succeed(
186 processOutput(
187 `gitlab.com
188 x gitlab.com: API call failed: 401 Unauthorized
189 ! No token found
190self-hosted.example.test
191 ✓ Logged in to self-hosted.example.test as gitlab-user
192 ✓ Token found: ******
193`,
194 { exitCode: ChildProcessSpawner.ExitCode(1) },
195 ),
196 ),
197 },
198 });
199​
200 const provider = yield* registry.resolve({ cwd: "/repo" });
201​
202 assert.strictEqual(provider.kind, "gitlab");
203 }),
204);
205​
206it.effect("refines the caller-selected remote instead of choosing another configured remote", () =>
207 Effect.gen(function* () {
208 const registry = yield* makeRegistry({
209 remotes: [{ name: "origin", url: "git@github.com:fork/project.git" }],
210 process: {
211 run: () =>
212 Effect.succeed(
213 processOutput(`self-hosted.example.test
214 ✓ Logged in to self-hosted.example.test as gitlab-user
215`),
216 ),
217 },
218 });
219​
220 const handle = yield* registry.resolveHandle({
221 cwd: "/repo",
222 context: {
223 provider: {
224 kind: "unknown",
225 name: "self-hosted.example.test",
226 baseUrl: "https://self-hosted.example.test",
227 },
228 remoteName: "upstream",
229 remoteUrl: "https://self-hosted.example.test/group/project.git",
230 },
231 });
232​
233 assert.strictEqual(handle.context?.provider.kind, "gitlab");
234 assert.strictEqual(handle.context?.remoteName, "upstream");
235 }),
236);
237​
238it.effect("routes authenticated self-hosted GitLab remotes on non-standard ports", () =>
239 Effect.gen(function* () {
240 const registry = yield* makeRegistry({
241 remotes: [{ name: "origin", url: "https://self-hosted.example.test:8443/group/project.git" }],
242 process: {
243 run: () =>
244 Effect.succeed(
245 processOutput(
246 `self-hosted.example.test:8443
247 ✓ Logged in to self-hosted.example.test:8443 as gitlab-user
248 ✓ Token found: ******
249`,
250 ),
251 ),
252 },
253 });
254​
255 const provider = yield* registry.resolve({ cwd: "/repo" });
256​
257 assert.strictEqual(provider.kind, "gitlab");
258 }),
259);
260​
261it.effect("routes Bitbucket remotes to the Bitbucket provider", () =>
262 Effect.gen(function* () {
263 const registry = yield* makeRegistry({
264 remotes: [{ name: "origin", url: "git@bitbucket.org:pingdotgg/t3code.git" }],
265 });
266​
267 const provider = yield* registry.resolve({ cwd: "/repo" });
268​
269 assert.strictEqual(provider.kind, "bitbucket");
270 }),
271);
272​
273it.effect("routes Azure DevOps remotes to the Azure DevOps provider", () =>
274 Effect.gen(function* () {
275 const registry = yield* makeRegistry({
276 remotes: [{ name: "origin", url: "https://dev.azure.com/acme/project/_git/repo" }],
277 });
278​
279 const provider = yield* registry.resolve({ cwd: "/repo" });
280​
281 assert.strictEqual(provider.kind, "azure-devops");
282 }),
283);
284​
285it.effect("falls back to a non-origin remote when origin is not configured", () =>
286 Effect.gen(function* () {
287 const registry = yield* makeRegistry({
288 remotes: [{ name: "upstream", url: "https://dev.azure.com/acme/project/_git/repo" }],
289 });
290​
291 const provider = yield* registry.resolve({ cwd: "/repo" });
292​
293 assert.strictEqual(provider.kind, "azure-devops");
294 }),
295);