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