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