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 @@
1import { assert, it } from "@effect/vitest";1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { Effect, Layer } from "effect";3import { Effect, Layer, Option } from "effect";
4import { ChildProcessSpawner } from "effect/unstable/process";4import { ChildProcessSpawner } from "effect/unstable/process";
5import { VcsProcessSpawnError } from "@t3tools/contracts";5import { VcsProcessSpawnError } from "@t3tools/contracts";
6 6
@@ -8,10 +8,16 @@ import { ServerConfig } from "../config.ts"; · 1 unmodified line
8import * as VcsProcess from "../vcs/VcsProcess.ts";8import * as VcsProcess from "../vcs/VcsProcess.ts";
9import { SourceControlDiscovery, layer } from "./SourceControlDiscovery.ts";9import { SourceControlDiscovery, layer } from "./SourceControlDiscovery.ts";
10 10
11const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({11const processOutput = (
12 exitCode: ChildProcessSpawner.ExitCode(0),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),
13 stdout,19 stdout,
14 stderr: "",20 stderr: options?.stderr ?? "",
15 stdoutTruncated: false,21 stdoutTruncated: false,
16 stderrTruncated: false,22 stderrTruncated: false,
17});23});
@@ -27,9 +33,20 @@ it.effect("reports implemented tools separately from locally available CLIs", () · 9 unmodified lines
27 if (input.command === "git") {33 if (input.command === "git") {
28 return Effect.succeed(processOutput("git version 2.51.0\n"));34 return Effect.succeed(processOutput("git version 2.51.0\n"));
29 }35 }
30 if (input.command === "gh") {36 if (input.command === "gh" && input.args[0] === "--version") {
31 return Effect.succeed(processOutput("gh version 2.83.0\n"));37 return Effect.succeed(processOutput("gh version 2.83.0\n"));
32 }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 }
33 return Effect.fail(50 return Effect.fail(
34 new VcsProcessSpawnError({51 new VcsProcessSpawnError({
35 operation: input.operation,52 operation: input.operation,
@@ -65,12 +82,133 @@ it.effect("reports implemented tools separately from locally available CLIs", () · 29 unmodified lines
65 kind: item.kind,82 kind: item.kind,
66 implemented: item.implemented,83 implemented: item.implemented,
67 status: item.status,84 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
122it.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
136Logged 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
145Logged 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
158Logged 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,
68 })),186 })),
69 [187 [
70 { kind: "github", implemented: true, status: "available" },188 {
71 { kind: "gitlab", implemented: false, status: "missing" },189 kind: "github",
72 { kind: "azure-devops", implemented: false, status: "missing" },190 auth: "authenticated",
73 { kind: "bitbucket", implemented: false, status: "missing" },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 },
74 ],212 ],
75 );213 );
76 }).pipe(Effect.provide(testLayer));214 }).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