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 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 NodeServices.layer,
92 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
93 Layer.mock(BitbucketApi.BitbucketApi)({}),
94 Layer.mock(GitHubCli.GitHubCli)({}),
95 Layer.mock(GitLabCli.GitLabCli)({}),
96 ServerConfig.layerTest(process.cwd(), {
97 prefix: "t3-source-control-registry-test-",
98 }).pipe(Layer.provide(NodeServices.layer)),
99 ),
100 ),
101 );
102}
103
104it.effect("routes GitHub remotes to the GitHub provider", () =>
105 Effect.gen(function* () {
106 const registry = yield* makeRegistry({
107 remotes: [{ name: "origin", url: "git@github.com:pingdotgg/t3code.git" }],
108 });
109
110 const provider = yield* registry.resolve({ cwd: "/repo" });
111
112 assert.strictEqual(provider.kind, "github");
113 }),
114);
115
116it.effect("routes directly by provider kind for remote-first workflows", () =>
117 Effect.gen(function* () {
118 const registry = yield* makeRegistry({
119 remotes: [],
120 });
121
122 const provider = yield* registry.get("github");
123
124 assert.strictEqual(provider.kind, "github");
125 }),
126);
127
128it.effect("includes the request cwd when an unregistered provider is used", () =>
129 Effect.gen(function* () {
130 const registry = yield* makeRegistry({ remotes: [] });
131 const provider = yield* registry.get("unknown");
132
133 const error = yield* provider
134 .getChangeRequest({ cwd: "/repo", reference: "#42" })
135 .pipe(Effect.flip);
136
137 assert.strictEqual(error.provider, "unknown");
138 assert.strictEqual(error.operation, "getChangeRequest");
139 assert.strictEqual(error.cwd, "/repo");
140 assert.strictEqual(error.reference, "#42");
141 }),
142);
143
144it.effect("retains VCS detection failures with structured cwd context", () =>
145 Effect.gen(function* () {
146 const cause = new VcsRepositoryDetectionError({
147 operation: "resolve",
148 cwd: "/repo",
149 detail: "raw VCS detection failure",
150 cause: new Error("raw nested failure"),
151 });
152 const registry = yield* makeRegistry({
153 remotes: [],
154 resolve: () => Effect.fail(cause),
155 });
156
157 const error = yield* registry.resolve({ cwd: "/repo" }).pipe(Effect.flip);
158
159 assert.strictEqual(error.provider, "unknown");
160 assert.strictEqual(error.operation, "detectProvider");
161 assert.strictEqual(error.cwd, "/repo");
162 assert.strictEqual(error.detail, "Failed to detect source control provider.");
163 assert.strictEqual(error.cause, cause);
164 assert.equal(error.message.includes(cause.message), false);
165 }),
166);
167
168it.effect("routes GitLab remotes to the GitLab provider", () =>
169 Effect.gen(function* () {
170 const registry = yield* makeRegistry({
171 remotes: [{ name: "origin", url: "git@gitlab.com:group/project.git" }],
172 });
173
174 const provider = yield* registry.resolve({ cwd: "/repo" });
175
176 assert.strictEqual(provider.kind, "gitlab");
177 }),
178);
179
180it.effect("routes authenticated self-hosted GitLab remotes without relying on host naming", () =>
181 Effect.gen(function* () {
182 const registry = yield* makeRegistry({
183 remotes: [{ name: "origin", url: "https://self-hosted.example.test/group/project.git" }],
184 process: {
185 run: () =>
186 Effect.succeed(
187 processOutput(
188 `gitlab.com
189 x gitlab.com: API call failed: 401 Unauthorized
190 ! No token found
191self-hosted.example.test
192 ✓ Logged in to self-hosted.example.test as gitlab-user
193 ✓ Token found: ******
194`,
195 { exitCode: ChildProcessSpawner.ExitCode(1) },
196 ),
197 ),
198 },
199 });
200
201 const provider = yield* registry.resolve({ cwd: "/repo" });
202
203 assert.strictEqual(provider.kind, "gitlab");
204 }),
205);
206
207it.effect("refines the caller-selected remote instead of choosing another configured remote", () =>
208 Effect.gen(function* () {
209 const registry = yield* makeRegistry({
210 remotes: [{ name: "origin", url: "git@github.com:fork/project.git" }],
211 process: {
212 run: () =>
213 Effect.succeed(
214 processOutput(`self-hosted.example.test
215 ✓ Logged in to self-hosted.example.test as gitlab-user
216`),
217 ),
218 },
219 });
220
221 const handle = yield* registry.resolveHandle({
222 cwd: "/repo",
223 context: {
224 provider: {
225 kind: "unknown",
226 name: "self-hosted.example.test",
227 baseUrl: "https://self-hosted.example.test",
228 },
229 remoteName: "upstream",
230 remoteUrl: "https://self-hosted.example.test/group/project.git",
231 },
232 });
233
234 assert.strictEqual(handle.context?.provider.kind, "gitlab");
235 assert.strictEqual(handle.context?.remoteName, "upstream");
236 }),
237);
238
239it.effect("routes authenticated self-hosted GitLab remotes on non-standard ports", () =>
240 Effect.gen(function* () {
241 const registry = yield* makeRegistry({
242 remotes: [{ name: "origin", url: "https://self-hosted.example.test:8443/group/project.git" }],
243 process: {
244 run: () =>
245 Effect.succeed(
246 processOutput(
247 `self-hosted.example.test:8443
248 ✓ Logged in to self-hosted.example.test:8443 as gitlab-user
249 ✓ Token found: ******
250`,
251 ),
252 ),
253 },
254 });
255
256 const provider = yield* registry.resolve({ cwd: "/repo" });
257
258 assert.strictEqual(provider.kind, "gitlab");
259 }),
260);
261
262it.effect("routes Bitbucket remotes to the Bitbucket provider", () =>
263 Effect.gen(function* () {
264 const registry = yield* makeRegistry({
265 remotes: [{ name: "origin", url: "git@bitbucket.org:pingdotgg/t3code.git" }],
266 });
267
268 const provider = yield* registry.resolve({ cwd: "/repo" });
269
270 assert.strictEqual(provider.kind, "bitbucket");
271 }),
272);
273
274it.effect("routes Azure DevOps remotes to the Azure DevOps provider", () =>
275 Effect.gen(function* () {
276 const registry = yield* makeRegistry({
277 remotes: [{ name: "origin", url: "https://dev.azure.com/acme/project/_git/repo" }],
278 });
279
280 const provider = yield* registry.resolve({ cwd: "/repo" });
281
282 assert.strictEqual(provider.kind, "azure-devops");
283 }),
284);
285
286it.effect("falls back to a non-origin remote when origin is not configured", () =>
287 Effect.gen(function* () {
288 const registry = yield* makeRegistry({
289 remotes: [{ name: "upstream", url: "https://dev.azure.com/acme/project/_git/repo" }],
290 });
291
292 const provider = yield* registry.resolve({ cwd: "/repo" });
293
294 assert.strictEqual(provider.kind, "azure-devops");
295 }),
296);