Open History to see the last commit
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") {
63 return Effect.succeed(
64 processOutput(`github.com
65Logged in to github.com account juliusmarminge (keyring)
66- Active account: true
67- Git operations protocol: ssh
68- Token: gho_************************************
69- Token scopes: 'admin:public_key', 'gist', 'read:org', 'repo'
70`),
71 );
72 }
73 return Effect.fail(
74 new VcsProcessSpawnError({
75 operation: input.operation,
76 command: input.command,
77 cwd: input.cwd,
78 cause: new Error(`${input.command} not found`),
79 }),
80 );
81 },
82 } satisfies Partial<VcsProcess.VcsProcessShape>;
83 const testLayer = SourceControlDiscovery.layer.pipe(
84 Layer.provide(
85 ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-discovery-" }),
86 ),
87 Layer.provide(Layer.mock(VcsProcess.VcsProcess)(processMock)),
88 Layer.provide(
89 sourceControlProviderRegistryTestLayer({
90 process: processMock,
91 bitbucket: {
92 probeAuth: Effect.succeed({
93 status: "unauthenticated",
94 account: Option.none(),
95 host: Option.some("bitbucket.org"),
96 detail: Option.some(
97 "Set T3CODE_BITBUCKET_EMAIL and T3CODE_BITBUCKET_API_TOKEN, or T3CODE_BITBUCKET_ACCESS_TOKEN.",
98 ),
99 }),
100 },
101 }),
102 ),
103 Layer.provideMerge(NodeServices.layer),
104 );
105
106 return Effect.gen(function* () {
107 const discovery = yield* SourceControlDiscovery.SourceControlDiscovery;
108 const result = yield* discovery.discover;
109
110 assert.deepStrictEqual(
111 result.versionControlSystems.map((item) => ({
112 kind: item.kind,
113 implemented: item.implemented,
114 status: item.status,
115 })),
116 [
117 { kind: "git", implemented: true, status: "available" },
118 { kind: "jj", implemented: false, status: "missing" },
119 ],
120 );
121 assert.deepStrictEqual(
122 result.sourceControlProviders.map((item) => ({
123 kind: item.kind,
124 status: item.status,
125 auth: item.auth.status,
126 account: item.auth.account,
127 })),
128 [
129 {
130 kind: "github",
131 status: "available",
132 auth: "authenticated",
133 account: Option.some("juliusmarminge"),
134 },
135 {
136 kind: "gitlab",
137 status: "missing",
138 auth: "unknown",
139 account: Option.none(),
140 },
141 {
142 kind: "azure-devops",
143 status: "missing",
144 auth: "unknown",
145 account: Option.none(),
146 },
147 {
148 kind: "bitbucket",
149 status: "available",
150 auth: "unauthenticated",
151 account: Option.none(),
152 },
153 ],
154 );
155 const bitbucket = result.sourceControlProviders.find((item) => item.kind === "bitbucket");
156 assert.ok(bitbucket);
157 assert.strictEqual(bitbucket.executable, undefined);
158 }).pipe(Effect.provide(testLayer));
159});
160
161it.effect("probes provider authentication without exposing token details", () => {
162 const processMock = {
163 run: (input: VcsProcess.VcsProcessInput) => {
164 if (input.args[0] === "--version") {
165 return Effect.succeed(processOutput(`${input.command} version test\n`));
166 }
167 if (input.command === "gh" && input.args.join(" ") === "auth status") {
168 return Effect.succeed(
169 processOutput(`github.com
170Logged in to github.com account octocat (keyring)
171- Token: gho_************************************
172- Token scopes: 'repo'
173`),
174 );
175 }
176 if (input.command === "glab" && input.args.join(" ") === "auth status") {
177 return Effect.succeed(
178 processOutput(`gitlab.com
179Logged in to gitlab.com as gitlab-user
180`),
181 );
182 }
183 if (
184 input.command === "az" &&
185 input.args.join(" ") === "account show --query user.name -o tsv"
186 ) {
187 return Effect.succeed(processOutput("azure-user@example.com\n"));
188 }
189 return Effect.fail(
190 new VcsProcessSpawnError({
191 operation: input.operation,
192 command: input.command,
193 cwd: input.cwd,
194 cause: new Error(`${input.command} not found`),
195 }),
196 );
197 },
198 } satisfies Partial<VcsProcess.VcsProcessShape>;
199 const testLayer = SourceControlDiscovery.layer.pipe(
200 Layer.provide(
201 ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-auth-discovery-" }),
202 ),
203 Layer.provide(Layer.mock(VcsProcess.VcsProcess)(processMock)),
204 Layer.provide(
205 sourceControlProviderRegistryTestLayer({
206 process: processMock,
207 bitbucket: {
208 probeAuth: Effect.succeed({
209 status: "authenticated",
210 account: Option.some("bitbucket-user"),
211 host: Option.some("bitbucket.org"),
212 detail: Option.none(),
213 }),
214 },
215 }),
216 ),
217 Layer.provideMerge(NodeServices.layer),
218 );
219
220 return Effect.gen(function* () {
221 const discovery = yield* SourceControlDiscovery.SourceControlDiscovery;
222 const result = yield* discovery.discover;
223
224 assert.deepStrictEqual(
225 result.sourceControlProviders.map((item) => ({
226 kind: item.kind,
227 auth: item.auth.status,
228 account: item.auth.account,
229 detail: item.auth.detail,
230 })),
231 [
232 {
233 kind: "github",
234 auth: "authenticated",
235 account: Option.some("octocat"),
236 detail: Option.none(),
237 },
238 {
239 kind: "gitlab",
240 auth: "authenticated",
241 account: Option.some("gitlab-user"),
242 detail: Option.none(),
243 },
244 {
245 kind: "azure-devops",
246 auth: "authenticated",
247 account: Option.some("azure-user@example.com"),
248 detail: Option.none(),
249 },
250 {
251 kind: "bitbucket",
252 auth: "authenticated",
253 account: Option.some("bitbucket-user"),
254 detail: Option.none(),
255 },
256 ],
257 );
258 }).pipe(Effect.provide(testLayer));
259});