History

Add source control discovery state

Julius Marminge committed 6 months ago

Commit b133b0d1 parent e36a15513 files · +943 −124Browse files

Showing the changes to apps/server/src/sourceControl/SourceControlDiscovery.test.ts (1 of 13 files). Show all

Unified · Split

apps/server/src/sourceControl/SourceControlDiscovery.test.ts+147−9
@@ -1,6 +1,6 @@
11 import { assert, it } from "@effect/vitest";
22 import * as NodeServices from "@effect/platform-node/NodeServices";
3−import { Effect, Layer } from "effect";
3+import { Effect, Layer, Option } from "effect";
44 import { ChildProcessSpawner } from "effect/unstable/process";
55 import { VcsProcessSpawnError } from "@t3tools/contracts";
66
@@ -8,10 +8,16 @@ import { ServerConfig } from "../config.ts"; · 1 unmodified line
88 import * as VcsProcess from "../vcs/VcsProcess.ts";
99 import { SourceControlDiscovery, layer } from "./SourceControlDiscovery.ts";
1010
11−const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
12− exitCode: ChildProcessSpawner.ExitCode(0),
11+const 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),
1319 stdout,
14− stderr: "",
20+ stderr: options?.stderr ?? "",
1521 stdoutTruncated: false,
1622 stderrTruncated: false,
1723 });
@@ -27,9 +33,20 @@ it.effect("reports implemented tools separately from locally available CLIs", () · 9 unmodified lines
2733 if (input.command === "git") {
2834 return Effect.succeed(processOutput("git version 2.51.0\n"));
2935 }
30− if (input.command === "gh") {
36+ if (input.command === "gh" && input.args[0] === "--version") {
3137 return Effect.succeed(processOutput("gh version 2.83.0\n"));
3238 }
39+ if (input.command === "gh" && input.args.join(" ") === "auth status") {
40+ return Effect.succeed(
41+ processOutput(`github.com
42+Logged 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+ }
3350 return Effect.fail(
3451 new VcsProcessSpawnError({
3552 operation: input.operation,
@@ -65,12 +82,133 @@ it.effect("reports implemented tools separately from locally available CLIs", () · 29 unmodified lines
6582 kind: item.kind,
6683 implemented: item.implemented,
6784 status: item.status,
85+ auth: item.auth.status,
86+ account: item.auth.account,
87+ })),
88+ [
89+ {
90+ kind: "github",
91+ implemented: true,
92+ status: "available",
93+ auth: "authenticated",
94+ account: Option.some("juliusmarminge"),
95+ },
96+ {
97+ kind: "gitlab",
98+ implemented: false,
99+ status: "missing",
100+ auth: "unknown",
101+ account: Option.none(),
102+ },
103+ {
104+ kind: "azure-devops",
105+ implemented: false,
106+ status: "missing",
107+ auth: "unknown",
108+ account: Option.none(),
109+ },
110+ {
111+ kind: "bitbucket",
112+ implemented: false,
113+ status: "missing",
114+ auth: "unknown",
115+ account: Option.none(),
116+ },
117+ ],
118+ );
119+ }).pipe(Effect.provide(testLayer));
120+});
121+
122+it.effect("probes provider authentication without exposing token details", () => {
123+ const testLayer = layer.pipe(
124+ Layer.provide(
125+ ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-auth-discovery-" }),
126+ ),
127+ Layer.provide(
128+ Layer.mock(VcsProcess.VcsProcess)({
129+ run: (input) => {
130+ if (input.args[0] === "--version") {
131+ return Effect.succeed(processOutput(`${input.command} version test\n`));
132+ }
133+ if (input.command === "gh" && input.args.join(" ") === "auth status") {
134+ return Effect.succeed(
135+ processOutput(`github.com
136+Logged in to github.com account octocat (keyring)
137+- Token: gho_************************************
138+- Token scopes: 'repo'
139+`),
140+ );
141+ }
142+ if (input.command === "glab" && input.args.join(" ") === "auth status") {
143+ return Effect.succeed(
144+ processOutput(`gitlab.com
145+Logged in to gitlab.com as gitlab-user
146+`),
147+ );
148+ }
149+ if (
150+ input.command === "az" &&
151+ input.args.join(" ") === "account show --query user.name -o tsv"
152+ ) {
153+ return Effect.succeed(processOutput("azure-user@example.com\n"));
154+ }
155+ if (input.command === "bb" && input.args.join(" ") === "auth status") {
156+ return Effect.succeed(
157+ processOutput(`bitbucket.org
158+Logged in as bitbucket-user
159+`),
160+ );
161+ }
162+ return Effect.fail(
163+ new VcsProcessSpawnError({
164+ operation: input.operation,
165+ command: input.command,
166+ cwd: input.cwd,
167+ cause: new Error(`${input.command} not found`),
168+ }),
169+ );
170+ },
171+ }),
172+ ),
173+ Layer.provideMerge(NodeServices.layer),
174+ );
175+
176+ return Effect.gen(function* () {
177+ const discovery = yield* SourceControlDiscovery;
178+ const result = yield* discovery.discover;
179+
180+ assert.deepStrictEqual(
181+ result.sourceControlProviders.map((item) => ({
182+ kind: item.kind,
183+ auth: item.auth.status,
184+ account: item.auth.account,
185+ detail: item.auth.detail,
68186 })),
69187 [
70− { kind: "github", implemented: true, status: "available" },
71− { kind: "gitlab", implemented: false, status: "missing" },
72− { kind: "azure-devops", implemented: false, status: "missing" },
73− { kind: "bitbucket", implemented: false, status: "missing" },
188+ {
189+ kind: "github",
190+ auth: "authenticated",
191+ account: Option.some("octocat"),
192+ detail: Option.none(),
193+ },
194+ {
195+ kind: "gitlab",
196+ auth: "authenticated",
197+ account: Option.some("gitlab-user"),
198+ detail: Option.none(),
199+ },
200+ {
201+ kind: "azure-devops",
202+ auth: "authenticated",
203+ account: Option.some("azure-user@example.com"),
204+ detail: Option.none(),
205+ },
206+ {
207+ kind: "bitbucket",
208+ auth: "authenticated",
209+ account: Option.some("bitbucket-user"),
210+ detail: Option.none(),
211+ },
74212 ],
75213 );
76214 }).pipe(Effect.provide(testLayer));

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t