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 VcsProcess from "../vcs/VcsProcess.ts";
9import { SourceControlDiscovery, layer } from "./SourceControlDiscovery.ts";
10
11const processOutput = (
12 stdout: string,
13 options?: {
14 readonly stderr?: string;
15 readonly exitCode?: ChildProcessSpawner.ExitCode;
16 },
17): VcsProcess.VcsProcessOutput => ({
18 exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
19 stdout,
20 stderr: options?.stderr ?? "",
21 stdoutTruncated: false,
22 stderrTruncated: false,
23});
24
25it.effect("reports implemented tools separately from locally available CLIs", () => {
26 const testLayer = layer.pipe(
27 Layer.provide(
28 ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-discovery-" }),
29 ),
30 Layer.provide(
31 Layer.mock(VcsProcess.VcsProcess)({
32 run: (input) => {
33 if (input.command === "git") {
34 return Effect.succeed(processOutput("git version 2.51.0\n"));
35 }
36 if (input.command === "gh" && input.args[0] === "--version") {
37 return Effect.succeed(processOutput("gh version 2.83.0\n"));
38 }
39 if (input.command === "gh" && input.args.join(" ") === "auth status") {
40 return Effect.succeed(
41 processOutput(`github.com
42Logged in to github.com account juliusmarminge (keyring)
43- Active account: true
44- Git operations protocol: ssh
45- Token: gho_************************************
46- Token scopes: 'admin:public_key', 'gist', 'read:org', 'repo'
47`),
48 );
49 }
50 return Effect.fail(
51 new VcsProcessSpawnError({
52 operation: input.operation,
53 command: input.command,
54 cwd: input.cwd,
55 cause: new Error(`${input.command} not found`),
56 }),
57 );
58 },
59 }),
60 ),
61 Layer.provideMerge(NodeServices.layer),
62 );
63
64 return Effect.gen(function* () {
65 const discovery = yield* SourceControlDiscovery;
66 const result = yield* discovery.discover;
67
68 assert.deepStrictEqual(
69 result.versionControlSystems.map((item) => ({
70 kind: item.kind,
71 implemented: item.implemented,
72 status: item.status,
73 })),
74 [
75 { kind: "git", implemented: true, status: "available" },
76 { kind: "jj", implemented: false, status: "missing" },
77 ],
78 );
79 assert.deepStrictEqual(
80 result.sourceControlProviders.map((item) => ({
81 kind: item.kind,
82 implemented: item.implemented,
83 status: item.status,
84 auth: item.auth.status,
85 account: item.auth.account,
86 })),
87 [
88 {
89 kind: "github",
90 implemented: true,
91 status: "available",
92 auth: "authenticated",
93 account: Option.some("juliusmarminge"),
94 },
95 {
96 kind: "gitlab",
97 implemented: true,
98 status: "missing",
99 auth: "unknown",
100 account: Option.none(),
101 },
102 {
103 kind: "azure-devops",
104 implemented: false,
105 status: "missing",
106 auth: "unknown",
107 account: Option.none(),
108 },
109 {
110 kind: "bitbucket",
111 implemented: false,
112 status: "missing",
113 auth: "unknown",
114 account: Option.none(),
115 },
116 ],
117 );
118 const bitbucket = result.sourceControlProviders.find((item) => item.kind === "bitbucket");
119 assert.ok(bitbucket);
120 assert.strictEqual("executable" in bitbucket, false);
121 }).pipe(Effect.provide(testLayer));
122});
123
124it.effect("probes provider authentication without exposing token details", () => {
125 const testLayer = layer.pipe(
126 Layer.provide(
127 ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-auth-discovery-" }),
128 ),
129 Layer.provide(
130 Layer.mock(VcsProcess.VcsProcess)({
131 run: (input) => {
132 if (input.args[0] === "--version") {
133 return Effect.succeed(processOutput(`${input.command} version test\n`));
134 }
135 if (input.command === "gh" && input.args.join(" ") === "auth status") {
136 return Effect.succeed(
137 processOutput(`github.com
138Logged in to github.com account octocat (keyring)
139- Token: gho_************************************
140- Token scopes: 'repo'
141`),
142 );
143 }
144 if (input.command === "glab" && input.args.join(" ") === "auth status") {
145 return Effect.succeed(
146 processOutput(`gitlab.com
147Logged in to gitlab.com as gitlab-user
148`),
149 );
150 }
151 if (
152 input.command === "az" &&
153 input.args.join(" ") === "account show --query user.name -o tsv"
154 ) {
155 return Effect.succeed(processOutput("azure-user@example.com\n"));
156 }
157 if (input.command === "bb" && input.args.join(" ") === "auth status") {
158 return Effect.succeed(
159 processOutput(`bitbucket.org
160Logged in as bitbucket-user
161`),
162 );
163 }
164 return Effect.fail(
165 new VcsProcessSpawnError({
166 operation: input.operation,
167 command: input.command,
168 cwd: input.cwd,
169 cause: new Error(`${input.command} not found`),
170 }),
171 );
172 },
173 }),
174 ),
175 Layer.provideMerge(NodeServices.layer),
176 );
177
178 return Effect.gen(function* () {
179 const discovery = yield* SourceControlDiscovery;
180 const result = yield* discovery.discover;
181
182 assert.deepStrictEqual(
183 result.sourceControlProviders.map((item) => ({
184 kind: item.kind,
185 auth: item.auth.status,
186 account: item.auth.account,
187 detail: item.auth.detail,
188 })),
189 [
190 {
191 kind: "github",
192 auth: "authenticated",
193 account: Option.some("octocat"),
194 detail: Option.none(),
195 },
196 {
197 kind: "gitlab",
198 auth: "authenticated",
199 account: Option.some("gitlab-user"),
200 detail: Option.none(),
201 },
202 {
203 kind: "azure-devops",
204 auth: "authenticated",
205 account: Option.some("azure-user@example.com"),
206 detail: Option.none(),
207 },
208 {
209 kind: "bitbucket",
210 auth: "unknown",
211 account: Option.none(),
212 detail: Option.some("Bitbucket provider support is not available yet."),
213 },
214 ],
215 );
216 }).pipe(Effect.provide(testLayer));
217});