Open History to see the last commit
1import { ServerSettings, SourceControlProviderKind } from "@t3tools/contracts";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import { assert, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as Redacted from "effect/Redacted";
8import * as Schema from "effect/Schema";
9import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
10import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
11import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
12
13import * as GitHubCredentials from "./GitHubCredentials.ts";
14import * as GitHubAppSignIn from "./GitHubAppSignIn.ts";
15import * as GitHubRest from "./GitHubRest.ts";
16import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
17
18const unknownContext = (baseUrl: string) => ({
19 provider: { kind: SourceControlProviderKind.make("unknown"), name: "Unknown", baseUrl },
20 remoteName: "origin",
21 remoteUrl: "git@code.example.test:team/project.git",
22});
23
24// The default host dies on any command, so refinement never reaches `gh`.
25const makeDiscovery = (credential: GitHubRest.GitHubRest["Service"]["credential"]) =>
26 GitHubSourceControlProvider.makeDiscovery.pipe(
27 Effect.provide(
28 Layer.mergeAll(
29 Layer.mock(GitHubRest.GitHubRest)({ credential }),
30 Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }),
31 TestSourceControlHost.layer(),
32 ),
33 ),
34 Effect.provideService(HostProcess.Environment, {}),
35 );
36
37it.effect.each([
38 { name: "a credential", credential: "found", claimed: true },
39 { name: "a host turned off in Settings", credential: "disabled", claimed: true },
40 { name: "no credential", credential: "missing", claimed: false },
41] as const)("claims a custom-host remote with $name", (scenario) =>
42 Effect.gen(function* () {
43 const hosts: Array<string> = [];
44 const discovery = yield* makeDiscovery((host) => {
45 hosts.push(host);
46 return scenario.credential === "found"
47 ? Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fingerprint` })
48 : scenario.credential === "disabled"
49 ? Effect.fail(new GitHubCredentials.GitHubHostDisabledError({ host }))
50 : Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host }));
51 });
52 if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI");
53 const provider = yield* discovery.refineUnknownRemote({
54 cwd: "/repo",
55 context: unknownContext("https://code.example.test"),
56 });
57 assert.deepStrictEqual(
58 provider,
59 scenario.claimed
60 ? {
61 kind: SourceControlProviderKind.make("github"),
62 name: "GitHub Self-Hosted",
63 baseUrl: "https://code.example.test",
64 }
65 : null,
66 );
67 assert.deepStrictEqual(hosts, ["code.example.test"]);
68 }),
69);
70
71it.effect("asks for no credential when the identity resolver left the base URL empty", () =>
72 Effect.gen(function* () {
73 const hosts: Array<string> = [];
74 const discovery = yield* makeDiscovery((host) => {
75 hosts.push(host);
76 return Effect.succeed({ token: Redacted.make("token"), fingerprint: `${host}:fingerprint` });
77 });
78 if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI");
79 assert.strictEqual(
80 yield* discovery.refineUnknownRemote({ cwd: "/repo", context: unknownContext("") }),
81 null,
82 );
83 assert.deepStrictEqual(hosts, []);
84 }),
85);
86
87const decodeSettings = Schema.decodeSync(ServerSettings);
88const referencedTokenSettings = decodeSettings({
89 sourceControlHosts: {
90 github: { tokens: { "github.com": { provider: "vault", reference: "op://v/gh" } } },
91 },
92});
93
94it.effect.each([
95 {
96 name: "a readable",
97 token: "resolved-token",
98 status: "authenticated",
99 detail: "the token saved in Settings",
100 },
101 {
102 name: "no",
103 token: "",
104 status: "authenticated",
105 detail: "GH_TOKEN from the server environment",
106 },
107 // getWithSecrets leaves a reference its secret provider could not read in place.
108 {
109 name: "an unreadable",
110 token: { provider: "vault", reference: "op://v/gh" },
111 status: "unauthenticated",
112 detail: 'secret provider "vault"',
113 },
114])("reports $name saved GitHub token instead of another credential", (scenario) =>
115 Effect.gen(function* () {
116 const paths: Array<string> = [];
117 const discovery = yield* GitHubSourceControlProvider.makeDiscovery.pipe(
118 Effect.provide(
119 Layer.mergeAll(
120 Layer.mock(GitHubRest.GitHubRest)({
121 rest: (input) => {
122 paths.push(input.path);
123 return Effect.succeed({
124 status: 200,
125 headers: {},
126 body: JSON.stringify({ login: "octocat" }),
127 truncated: false,
128 invalidUtf8: false,
129 });
130 },
131 }),
132 Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }),
133 Layer.effect(
134 SourceControlHost.SourceControlHost,
135 Effect.gen(function* () {
136 const host = yield* SourceControlHost.SourceControlHost;
137 return {
138 ...host,
139 settings: {
140 ...host.settings,
141 getWithSecrets: () =>
142 Effect.succeed(
143 decodeSettings({
144 sourceControlHosts: {
145 github: { tokens: { "github.com": scenario.token } },
146 },
147 }),
148 ),
149 },
150 };
151 }),
152 ).pipe(
153 Layer.provide(
154 TestSourceControlHost.layer({
155 settings: referencedTokenSettings,
156 process: {
157 run: () =>
158 Effect.succeed(
159 TestSourceControlHost.processOutput("", {
160 exitCode: ChildProcessSpawner.ExitCode(1),
161 }),
162 ),
163 },
164 }),
165 ),
166 ),
167 ),
168 ),
169 Effect.provideService(HostProcess.Environment, { GH_TOKEN: "environment-token" }),
170 );
171 if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI");
172 const item = yield* discovery.probe("/repo");
173 const authenticated = scenario.status === "authenticated";
174 assert.deepStrictEqual(paths, authenticated ? ["user"] : []);
175 assert.strictEqual(item.auth.status, scenario.status);
176 assert.deepStrictEqual(
177 item.auth.account,
178 authenticated ? Option.some("octocat") : Option.none(),
179 );
180 assert.include(Option.getOrThrow(item.auth.detail), scenario.detail);
181 }),
182);