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