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

SourceControlDiscovery.test.ts

283 lines8.8 KB

1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import { ChildProcessSpawner } from "effect/unstable/process";
7import { VcsProcessSpawnError } from "@v12/contracts";
8​
9import * as ServerConfig from "../config.ts";
10import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
11import * as VcsProcess from "../vcs/VcsProcess.ts";
12import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
13import * as BitbucketApi from "./BitbucketApi.ts";
14import * as GitHubCli from "./GitHubCli.ts";
15import * as GitLabCli from "./GitLabCli.ts";
16import * as SourceControlDiscovery from "./SourceControlDiscovery.ts";
17import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
18​
19const sourceControlProviderRegistryTestLayer = (input: {
20 readonly bitbucket: Partial<BitbucketApi.BitbucketApi["Service"]>;
21 readonly process: Partial<VcsProcess.VcsProcess["Service"]>;
22}) =>
23 SourceControlProviderRegistry.layer.pipe(
24 Layer.provide(
25 Layer.mergeAll(
26 ServerConfig.layerTest(process.cwd(), {
27 prefix: "v12-source-control-registry-test-",
28 }).pipe(Layer.provide(NodeServices.layer)),
29 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
30 Layer.mock(BitbucketApi.BitbucketApi)(input.bitbucket),
31 Layer.mock(GitHubCli.GitHubCli)({}),
32 Layer.mock(GitLabCli.GitLabCli)({}),
33 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({}),
34 Layer.mock(VcsProcess.VcsProcess)(input.process),
35 ),
36 ),
37 );
38​
39const processOutput = (
40 stdout: string,
41 options?: {
42 readonly stderr?: string;
43 readonly exitCode?: ChildProcessSpawner.ExitCode;
44 },
45): VcsProcess.VcsProcessOutput => ({
46 exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
47 stdout,
48 stderr: options?.stderr ?? "",
49 stdoutTruncated: false,
50 stderrTruncated: false,
51});
52​
53it.effect("reports implemented tools separately from locally available executables", () => {
54 const processMock = {
55 run: (input: VcsProcess.VcsProcessInput) => {
56 if (input.command === "git") {
57 return Effect.succeed(processOutput("git version 2.51.0\n"));
58 }
59 if (input.command === "gh" && input.args[0] === "--version") {
60 return Effect.succeed(processOutput("gh version 2.83.0\n"));
61 }
62 if (input.command === "gh" && input.args.join(" ") === "auth status --json hosts") {
63 return Effect.succeed(
64 processOutput(
65 JSON.stringify({
66 hosts: {
67 "github.com": [
68 {
69 state: "success",
70 active: true,
71 host: "github.com",
72 login: "juliusmarminge",
73 tokenSource: "keyring",
74 gitProtocol: "ssh",
75 },
76 ],
77 },
78 }),
79 ),
80 );
81 }
82 return Effect.fail(
83 new VcsProcessSpawnError({
84 operation: input.operation,
85 command: input.command,
86 cwd: input.cwd,
87 cause: new Error(`${input.command} not found`),
88 }),
89 );
90 },
91 } satisfies Partial<VcsProcess.VcsProcess["Service"]>;
92 const testLayer = SourceControlDiscovery.layer.pipe(
93 Layer.provide(
94 ServerConfig.layerTest(process.cwd(), {
95 prefix: "v12-source-control-discovery-",
96 }),
97 ),
98 Layer.provide(Layer.mock(VcsProcess.VcsProcess)(processMock)),
99 Layer.provide(
100 sourceControlProviderRegistryTestLayer({
101 process: processMock,
102 bitbucket: {
103 probeAuth: Effect.succeed({
104 status: "unauthenticated",
105 account: Option.none(),
106 host: Option.some("bitbucket.org"),
107 detail: Option.some(
108 "Set V12_BITBUCKET_EMAIL and V12_BITBUCKET_API_TOKEN, or V12_BITBUCKET_ACCESS_TOKEN.",
109 ),
110 }),
111 },
112 }),
113 ),
114 Layer.provideMerge(NodeServices.layer),
115 );
116​
117 return Effect.gen(function* () {
118 const discovery = yield* SourceControlDiscovery.SourceControlDiscovery;
119 const result = yield* discovery.discover;
120​
121 assert.deepStrictEqual(
122 result.versionControlSystems.map((item) => ({
123 kind: item.kind,
124 implemented: item.implemented,
125 status: item.status,
126 })),
127 [
128 { kind: "git", implemented: true, status: "available" },
129 { kind: "jj", implemented: false, status: "missing" },
130 ],
131 );
132 assert.deepStrictEqual(
133 result.sourceControlProviders.map((item) => ({
134 kind: item.kind,
135 status: item.status,
136 auth: item.auth.status,
137 account: item.auth.account,
138 })),
139 [
140 {
141 kind: "github",
142 status: "available",
143 auth: "authenticated",
144 account: Option.some("juliusmarminge"),
145 },
146 {
147 kind: "gitlab",
148 status: "missing",
149 auth: "unknown",
150 account: Option.none(),
151 },
152 {
153 kind: "azure-devops",
154 status: "missing",
155 auth: "unknown",
156 account: Option.none(),
157 },
158 {
159 kind: "bitbucket",
160 status: "available",
161 auth: "unauthenticated",
162 account: Option.none(),
163 },
164 ],
165 );
166 const bitbucket = result.sourceControlProviders.find((item) => item.kind === "bitbucket");
167 assert.ok(bitbucket);
168 assert.strictEqual(bitbucket.executable, undefined);
169 }).pipe(Effect.provide(testLayer));
170});
171​
172it.effect("probes provider authentication without exposing token details", () => {
173 const processMock = {
174 run: (input: VcsProcess.VcsProcessInput) => {
175 if (input.args[0] === "--version") {
176 return Effect.succeed(processOutput(`${input.command} version test\n`));
177 }
178 if (input.command === "gh" && input.args.join(" ") === "auth status --json hosts") {
179 return Effect.succeed(
180 processOutput(
181 JSON.stringify({
182 hosts: {
183 "github.com": [
184 {
185 state: "success",
186 active: true,
187 host: "github.com",
188 login: "octocat",
189 tokenSource: "keyring",
190 gitProtocol: "ssh",
191 },
192 ],
193 },
194 }),
195 ),
196 );
197 }
198 if (input.command === "glab" && input.args.join(" ") === "auth status") {
199 return Effect.succeed(
200 processOutput(`gitlab.com
201Logged in to gitlab.com as gitlab-user
202`),
203 );
204 }
205 if (
206 input.command === "az" &&
207 input.args.join(" ") === "account show --query user.name -o tsv"
208 ) {
209 return Effect.succeed(processOutput("azure-user@example.com\n"));
210 }
211 return Effect.fail(
212 new VcsProcessSpawnError({
213 operation: input.operation,
214 command: input.command,
215 cwd: input.cwd,
216 cause: new Error(`${input.command} not found`),
217 }),
218 );
219 },
220 } satisfies Partial<VcsProcess.VcsProcess["Service"]>;
221 const testLayer = SourceControlDiscovery.layer.pipe(
222 Layer.provide(
223 ServerConfig.layerTest(process.cwd(), {
224 prefix: "v12-source-control-auth-discovery-",
225 }),
226 ),
227 Layer.provide(Layer.mock(VcsProcess.VcsProcess)(processMock)),
228 Layer.provide(
229 sourceControlProviderRegistryTestLayer({
230 process: processMock,
231 bitbucket: {
232 probeAuth: Effect.succeed({
233 status: "authenticated",
234 account: Option.some("bitbucket-user"),
235 host: Option.some("bitbucket.org"),
236 detail: Option.none(),
237 }),
238 },
239 }),
240 ),
241 Layer.provideMerge(NodeServices.layer),
242 );
243​
244 return Effect.gen(function* () {
245 const discovery = yield* SourceControlDiscovery.SourceControlDiscovery;
246 const result = yield* discovery.discover;
247​
248 assert.deepStrictEqual(
249 result.sourceControlProviders.map((item) => ({
250 kind: item.kind,
251 auth: item.auth.status,
252 account: item.auth.account,
253 detail: item.auth.detail,
254 })),
255 [
256 {
257 kind: "github",
258 auth: "authenticated",
259 account: Option.some("octocat"),
260 detail: Option.none(),
261 },
262 {
263 kind: "gitlab",
264 auth: "authenticated",
265 account: Option.some("gitlab-user"),
266 detail: Option.none(),
267 },
268 {
269 kind: "azure-devops",
270 auth: "authenticated",
271 account: Option.some("azure-user@example.com"),
272 detail: Option.none(),
273 },
274 {
275 kind: "bitbucket",
276 auth: "authenticated",
277 account: Option.some("bitbucket-user"),
278 detail: Option.none(),
279 },
280 ],
281 );
282 }).pipe(Effect.provide(testLayer));
283});