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

SourceControlProviderRegistry.test.ts

219 lines6.5 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";
8​
9import { ServerConfig } from "../config.ts";
10import type * as VcsDriver from "../vcs/VcsDriver.ts";
11import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
12import * as VcsProcess from "../vcs/VcsProcess.ts";
13import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
14import * as BitbucketApi from "./BitbucketApi.ts";
15import * as GitHubCli from "./GitHubCli.ts";
16import * as GitLabCli from "./GitLabCli.ts";
17import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
18​
19const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z");
20​
21const processOutput = (
22 stdout: string,
23 options?: {
24 readonly stderr?: string;
25 readonly exitCode?: ChildProcessSpawner.ExitCode;
26 },
27): VcsProcess.VcsProcessOutput => ({
28 exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
29 stdout,
30 stderr: options?.stderr ?? "",
31 stdoutTruncated: false,
32 stderrTruncated: false,
33});
34​
35function makeRegistry(input: {
36 readonly remotes: ReadonlyArray<{
37 readonly name: string;
38 readonly url: string;
39 }>;
40 readonly process?: Partial<VcsProcess.VcsProcessShape>;
41}) {
42 const driver = {
43 listRemotes: () =>
44 Effect.succeed({
45 remotes: input.remotes.map((remote) => ({
46 ...remote,
47 pushUrl: Option.none(),
48 isPrimary: remote.name === "origin",
49 })),
50 freshness: {
51 source: "live-local" as const,
52 observedAt: TEST_EPOCH,
53 expiresAt: Option.none(),
54 },
55 }),
56 } satisfies Partial<VcsDriver.VcsDriverShape>;
57​
58 const registryLayer = Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
59 get: () => Effect.succeed(driver as unknown as VcsDriver.VcsDriverShape),
60 resolve: () =>
61 Effect.succeed({
62 kind: "git",
63 repository: {
64 kind: "git",
65 rootPath: "/repo",
66 metadataPath: null,
67 freshness: {
68 source: "live-local" as const,
69 observedAt: TEST_EPOCH,
70 expiresAt: Option.none(),
71 },
72 },
73 driver: driver as unknown as VcsDriver.VcsDriverShape,
74 }),
75 });
76​
77 const processLayer = Layer.mock(VcsProcess.VcsProcess)({
78 run: () => Effect.succeed(processOutput("")),
79 ...input.process,
80 });
81​
82 return SourceControlProviderRegistry.make().pipe(
83 Effect.provide(
84 Layer.mergeAll(
85 registryLayer,
86 processLayer,
87 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
88 Layer.mock(BitbucketApi.BitbucketApi)({}),
89 Layer.mock(GitHubCli.GitHubCli)({}),
90 Layer.mock(GitLabCli.GitLabCli)({}),
91 ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-registry-test-" }).pipe(
92 Layer.provide(NodeServices.layer),
93 ),
94 ),
95 ),
96 );
98​
99it.effect("routes GitHub remotes to the GitHub provider", () =>
100 Effect.gen(function* () {
101 const registry = yield* makeRegistry({
102 remotes: [{ name: "origin", url: "git@github.com:pingdotgg/t3code.git" }],
103 });
104​
105 const provider = yield* registry.resolve({ cwd: "/repo" });
106​
107 assert.strictEqual(provider.kind, "github");
108 }),
109);
110​
111it.effect("routes directly by provider kind for remote-first workflows", () =>
112 Effect.gen(function* () {
113 const registry = yield* makeRegistry({
114 remotes: [],
115 });
116​
117 const provider = yield* registry.get("github");
118​
119 assert.strictEqual(provider.kind, "github");
120 }),
121);
122​
123it.effect("routes GitLab remotes to the GitLab provider", () =>
124 Effect.gen(function* () {
125 const registry = yield* makeRegistry({
126 remotes: [{ name: "origin", url: "git@gitlab.com:group/project.git" }],
127 });
128​
129 const provider = yield* registry.resolve({ cwd: "/repo" });
130​
131 assert.strictEqual(provider.kind, "gitlab");
132 }),
133);
134​
135it.effect("routes authenticated self-hosted GitLab remotes without relying on host naming", () =>
136 Effect.gen(function* () {
137 const registry = yield* makeRegistry({
138 remotes: [{ name: "origin", url: "https://self-hosted.example.test/group/project.git" }],
139 process: {
140 run: () =>
141 Effect.succeed(
142 processOutput(
143 `gitlab.com
144 x gitlab.com: API call failed: 401 Unauthorized
145 ! No token found
146self-hosted.example.test
147 ✓ Logged in to self-hosted.example.test as gitlab-user
148 ✓ Token found: ******
149`,
150 { exitCode: ChildProcessSpawner.ExitCode(1) },
151 ),
152 ),
153 },
154 });
155​
156 const provider = yield* registry.resolve({ cwd: "/repo" });
157​
158 assert.strictEqual(provider.kind, "gitlab");
159 }),
160);
161​
162it.effect("routes authenticated self-hosted GitLab remotes on non-standard ports", () =>
163 Effect.gen(function* () {
164 const registry = yield* makeRegistry({
165 remotes: [{ name: "origin", url: "https://self-hosted.example.test:8443/group/project.git" }],
166 process: {
167 run: () =>
168 Effect.succeed(
169 processOutput(
170 `self-hosted.example.test:8443
171 ✓ Logged in to self-hosted.example.test:8443 as gitlab-user
172 ✓ Token found: ******
173`,
174 ),
175 ),
176 },
177 });
178​
179 const provider = yield* registry.resolve({ cwd: "/repo" });
180​
181 assert.strictEqual(provider.kind, "gitlab");
182 }),
183);
184​
185it.effect("routes Bitbucket remotes to the Bitbucket provider", () =>
186 Effect.gen(function* () {
187 const registry = yield* makeRegistry({
188 remotes: [{ name: "origin", url: "git@bitbucket.org:pingdotgg/t3code.git" }],
189 });
190​
191 const provider = yield* registry.resolve({ cwd: "/repo" });
192​
193 assert.strictEqual(provider.kind, "bitbucket");
194 }),
195);
196​
197it.effect("routes Azure DevOps remotes to the Azure DevOps provider", () =>
198 Effect.gen(function* () {
199 const registry = yield* makeRegistry({
200 remotes: [{ name: "origin", url: "https://dev.azure.com/acme/project/_git/repo" }],
201 });
202​
203 const provider = yield* registry.resolve({ cwd: "/repo" });
204​
205 assert.strictEqual(provider.kind, "azure-devops");
206 }),
207);
208​
209it.effect("falls back to a non-origin remote when origin is not configured", () =>
210 Effect.gen(function* () {
211 const registry = yield* makeRegistry({
212 remotes: [{ name: "upstream", url: "https://dev.azure.com/acme/project/_git/repo" }],
213 });
214​
215 const provider = yield* registry.resolve({ cwd: "/repo" });
216​
217 assert.strictEqual(provider.kind, "azure-devops");
218 }),
219);