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

SourceControlDiscovery.test.ts

279 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 "@t3tools/contracts";
8​
9import { 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.BitbucketApiShape>;
21 readonly process: Partial<VcsProcess.VcsProcessShape>;
22}) =>
23 SourceControlProviderRegistry.layer.pipe(
24 Layer.provide(
25 Layer.mergeAll(
26 ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-registry-test-" }).pipe(
27 Layer.provide(NodeServices.layer),
28 ),
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.VcsProcessShape>;
92 const testLayer = SourceControlDiscovery.layer.pipe(
93 Layer.provide(
94 ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-discovery-" }),
95 ),
96 Layer.provide(Layer.mock(VcsProcess.VcsProcess)(processMock)),
97 Layer.provide(
98 sourceControlProviderRegistryTestLayer({
99 process: processMock,
100 bitbucket: {
101 probeAuth: Effect.succeed({
102 status: "unauthenticated",
103 account: Option.none(),
104 host: Option.some("bitbucket.org"),
105 detail: Option.some(
106 "Set T3CODE_BITBUCKET_EMAIL and T3CODE_BITBUCKET_API_TOKEN, or T3CODE_BITBUCKET_ACCESS_TOKEN.",
107 ),
108 }),
109 },
110 }),
111 ),
112 Layer.provideMerge(NodeServices.layer),
113 );
114​
115 return Effect.gen(function* () {
116 const discovery = yield* SourceControlDiscovery.SourceControlDiscovery;
117 const result = yield* discovery.discover;
118​
119 assert.deepStrictEqual(
120 result.versionControlSystems.map((item) => ({
121 kind: item.kind,
122 implemented: item.implemented,
123 status: item.status,
124 })),
125 [
126 { kind: "git", implemented: true, status: "available" },
127 { kind: "jj", implemented: false, status: "missing" },
128 ],
129 );
130 assert.deepStrictEqual(
131 result.sourceControlProviders.map((item) => ({
132 kind: item.kind,
133 status: item.status,
134 auth: item.auth.status,
135 account: item.auth.account,
136 })),
137 [
138 {
139 kind: "github",
140 status: "available",
141 auth: "authenticated",
142 account: Option.some("juliusmarminge"),
143 },
144 {
145 kind: "gitlab",
146 status: "missing",
147 auth: "unknown",
148 account: Option.none(),
149 },
150 {
151 kind: "azure-devops",
152 status: "missing",
153 auth: "unknown",
154 account: Option.none(),
155 },
156 {
157 kind: "bitbucket",
158 status: "available",
159 auth: "unauthenticated",
160 account: Option.none(),
161 },
162 ],
163 );
164 const bitbucket = result.sourceControlProviders.find((item) => item.kind === "bitbucket");
165 assert.ok(bitbucket);
166 assert.strictEqual(bitbucket.executable, undefined);
167 }).pipe(Effect.provide(testLayer));
168});
169​
170it.effect("probes provider authentication without exposing token details", () => {
171 const processMock = {
172 run: (input: VcsProcess.VcsProcessInput) => {
173 if (input.args[0] === "--version") {
174 return Effect.succeed(processOutput(`${input.command} version test\n`));
175 }
176 if (input.command === "gh" && input.args.join(" ") === "auth status --json hosts") {
177 return Effect.succeed(
178 processOutput(
179 JSON.stringify({
180 hosts: {
181 "github.com": [
182 {
183 state: "success",
184 active: true,
185 host: "github.com",
186 login: "octocat",
187 tokenSource: "keyring",
188 gitProtocol: "ssh",
189 },
190 ],
191 },
192 }),
193 ),
194 );
195 }
196 if (input.command === "glab" && input.args.join(" ") === "auth status") {
197 return Effect.succeed(
198 processOutput(`gitlab.com
199Logged in to gitlab.com as gitlab-user
200`),
201 );
202 }
203 if (
204 input.command === "az" &&
205 input.args.join(" ") === "account show --query user.name -o tsv"
206 ) {
207 return Effect.succeed(processOutput("azure-user@example.com\n"));
208 }
209 return Effect.fail(
210 new VcsProcessSpawnError({
211 operation: input.operation,
212 command: input.command,
213 cwd: input.cwd,
214 cause: new Error(`${input.command} not found`),
215 }),
216 );
217 },
218 } satisfies Partial<VcsProcess.VcsProcessShape>;
219 const testLayer = SourceControlDiscovery.layer.pipe(
220 Layer.provide(
221 ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-auth-discovery-" }),
222 ),
223 Layer.provide(Layer.mock(VcsProcess.VcsProcess)(processMock)),
224 Layer.provide(
225 sourceControlProviderRegistryTestLayer({
226 process: processMock,
227 bitbucket: {
228 probeAuth: Effect.succeed({
229 status: "authenticated",
230 account: Option.some("bitbucket-user"),
231 host: Option.some("bitbucket.org"),
232 detail: Option.none(),
233 }),
234 },
235 }),
236 ),
237 Layer.provideMerge(NodeServices.layer),
238 );
239​
240 return Effect.gen(function* () {
241 const discovery = yield* SourceControlDiscovery.SourceControlDiscovery;
242 const result = yield* discovery.discover;
243​
244 assert.deepStrictEqual(
245 result.sourceControlProviders.map((item) => ({
246 kind: item.kind,
247 auth: item.auth.status,
248 account: item.auth.account,
249 detail: item.auth.detail,
250 })),
251 [
252 {
253 kind: "github",
254 auth: "authenticated",
255 account: Option.some("octocat"),
256 detail: Option.none(),
257 },
258 {
259 kind: "gitlab",
260 auth: "authenticated",
261 account: Option.some("gitlab-user"),
262 detail: Option.none(),
263 },
264 {
265 kind: "azure-devops",
266 auth: "authenticated",
267 account: Option.some("azure-user@example.com"),
268 detail: Option.none(),
269 },
270 {
271 kind: "bitbucket",
272 auth: "authenticated",
273 account: Option.some("bitbucket-user"),
274 detail: Option.none(),
275 },
276 ],
277 );
278 }).pipe(Effect.provide(testLayer));
279});