Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

gitHubDiscovery.test.ts

161 lines6.3 KB

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 { name: "readable", token: "resolved-token", source: "the token saved in Settings" },
96 { name: "unreadable", token: "", source: "GH_TOKEN from the server environment" },
97])("checks the $name GitHub token reference against the API", (scenario) =>
98 Effect.gen(function* () {
99 const paths: Array<string> = [];
100 const discovery = yield* GitHubSourceControlProvider.makeDiscovery.pipe(
101 Effect.provide(
102 Layer.mergeAll(
103 Layer.mock(GitHubRest.GitHubRest)({
104 rest: (input) => {
105 paths.push(input.path);
106 return Effect.succeed({
107 status: 200,
108 headers: {},
109 body: JSON.stringify({ login: "octocat" }),
110 truncated: false,
111 invalidUtf8: false,
112 });
113 },
114 }),
115 Layer.mock(GitHubAppSignIn.GitHubAppSignIn)({ enabled: false }),
116 Layer.effect(
117 SourceControlHost.SourceControlHost,
118 Effect.gen(function* () {
119 const host = yield* SourceControlHost.SourceControlHost;
120 return {
121 ...host,
122 settings: {
123 ...host.settings,
124 getWithSecrets: () =>
125 Effect.succeed(
126 decodeSettings({
127 sourceControlHosts: {
128 github: { tokens: { "github.com": scenario.token } },
129 },
130 }),
131 ),
132 },
133 };
134 }),
135 ).pipe(
136 Layer.provide(
137 TestSourceControlHost.layer({
138 settings: referencedTokenSettings,
139 process: {
140 run: () =>
141 Effect.succeed(
142 TestSourceControlHost.processOutput("", {
143 exitCode: ChildProcessSpawner.ExitCode(1),
144 }),
145 ),
146 },
147 }),
148 ),
149 ),
150 ),
151 ),
152 Effect.provideService(HostProcess.Environment, { GH_TOKEN: "environment-token" }),
153 );
154 if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI");
155 const item = yield* discovery.probe("/repo");
156 assert.deepStrictEqual(paths, ["user"]);
157 assert.strictEqual(item.auth.status, "authenticated");
158 assert.deepStrictEqual(item.auth.account, Option.some("octocat"));
159 assert.include(Option.getOrThrow(item.auth.detail), scenario.source);
160 }),
161);