Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Redacted from "effect/Redacted";
6import { ChildProcessSpawner } from "effect/process";
7
8import * as ServerSettings from "../serverSettings.ts";
9import * as VcsProcess from "../vcs/VcsProcess.ts";
10import * as GitHubCredentials from "./GitHubCredentials.ts";
11
12const TOKEN_VARIABLES = [
13 "GH_TOKEN",
14 "GITHUB_TOKEN",
15 "GH_ENTERPRISE_TOKEN",
16 "GITHUB_ENTERPRISE_TOKEN",
17] as const;
18
19function harness(
20 hosts: Record<string, { readonly account?: string; readonly enabled?: boolean }> = {},
21 signedOut: ReadonlyArray<string> = [],
22 tokens: Record<string, string> = {},
23) {
24 const calls: Array<ReadonlyArray<string>> = [];
25 const process = Layer.mock(VcsProcess.VcsProcess)({
26 run: (input) =>
27 Effect.sync(() => {
28 calls.push(input.args);
29 const user = input.args[input.args.indexOf("--user") + 1];
30 if (input.args.includes("--user") && user !== undefined && signedOut.includes(user)) {
31 return {
32 exitCode: ChildProcessSpawner.ExitCode(0),
33 stdout: "",
34 stderr: "",
35 stdoutTruncated: false,
36 stderrTruncated: false,
37 };
38 }
39 return {
40 exitCode: ChildProcessSpawner.ExitCode(0),
41 stdout: input.args.includes("--user") ? `token-for-${user}\n` : "active-token\n",
42 stderr: "",
43 stdoutTruncated: false,
44 stderrTruncated: false,
45 };
46 }),
47 });
48 const layer = GitHubCredentials.layer.pipe(
49 Layer.provideMerge(
50 ServerSettings.ServerSettingsService.layerTest({ github: { hosts, tokens } }),
51 ),
52 Layer.provide(process),
53 Layer.provide(NodeServices.layer),
54 );
55 return { layer, calls };
56}
57
58describe("GitHubCredentials", () => {
59 beforeEach(() => {
60 for (const name of TOKEN_VARIABLES) vi.stubEnv(name, "");
61 });
62 afterEach(() => {
63 vi.unstubAllEnvs();
64 });
65
66 it.effect("asks gh for the active login when Settings pin nothing", () => {
67 const { layer, calls } = harness();
68 return Effect.gen(function* () {
69 const credentials = yield* GitHubCredentials.GitHubCredentials;
70 const credential = yield* credentials.get("GitHub.com");
71 expect(Redacted.value(credential.token)).toBe("active-token");
72 expect(calls).toEqual([["auth", "token", "--hostname", "github.com"]]);
73 }).pipe(Effect.provide(layer));
74 });
75
76 it.effect("passes --user for an account pinned in Settings", () => {
77 const { layer, calls } = harness({ "github.com": { account: "work" } });
78 return Effect.gen(function* () {
79 const credentials = yield* GitHubCredentials.GitHubCredentials;
80 const credential = yield* credentials.get("github.com");
81 expect(Redacted.value(credential.token)).toBe("token-for-work");
82 expect(calls).toEqual([["auth", "token", "--hostname", "github.com", "--user", "work"]]);
83 }).pipe(Effect.provide(layer));
84 });
85
86 it.effect("fails a host turned off in Settings without asking gh", () => {
87 const { layer, calls } = harness({ "github.com": { enabled: false } });
88 return Effect.gen(function* () {
89 const credentials = yield* GitHubCredentials.GitHubCredentials;
90 const error = yield* Effect.flip(credentials.get("github.com"));
91 expect(error._tag).toBe("GitHubHostDisabledError");
92 expect(error.message).toBe(
93 "GitHub host github.com is turned off in Settings → Source Control.",
94 );
95 expect(calls).toEqual([]);
96 }).pipe(Effect.provide(layer));
97 });
98
99 it.effect("lets an environment token win over a pinned account, as gh does", () => {
100 vi.stubEnv("GH_TOKEN", "from-env");
101 const { layer, calls } = harness({ "github.com": { account: "work" } });
102 return Effect.gen(function* () {
103 const credentials = yield* GitHubCredentials.GitHubCredentials;
104 const credential = yield* credentials.get("github.com");
105 expect(Redacted.value(credential.token)).toBe("from-env");
106 expect(credential.source).toBe("env");
107 expect(calls).toEqual([]);
108 }).pipe(Effect.provide(layer));
109 });
110
111 it.effect("picks up a changed account on the next request without a restart", () => {
112 const { layer, calls } = harness();
113 return Effect.gen(function* () {
114 const credentials = yield* GitHubCredentials.GitHubCredentials;
115 const settings = yield* ServerSettings.ServerSettingsService;
116 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("active-token");
117
118 yield* settings.updateSettings({
119 github: { hosts: { "github.com": { account: "work", enabled: true } } },
120 });
121 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("token-for-work");
122
123 yield* settings.updateSettings({ github: { hosts: { "github.com": { enabled: false } } } });
124 expect((yield* Effect.flip(credentials.get("github.com")))._tag).toBe(
125 "GitHubHostDisabledError",
126 );
127
128 yield* settings.updateSettings({ github: { hosts: {} } });
129 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("active-token");
130 // The unpinned token stayed cached; only the newly pinned account cost a gh call.
131 expect(calls).toHaveLength(2);
132 }).pipe(Effect.provide(layer));
133 });
134
135 it.effect("falls back to the active login when the pinned one is no longer signed in", () => {
136 const { layer, calls } = harness({ "github.com": { account: "gone" } }, ["gone"]);
137 return Effect.gen(function* () {
138 const credentials = yield* GitHubCredentials.GitHubCredentials;
139 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("active-token");
140 expect(calls).toEqual([
141 ["auth", "token", "--hostname", "github.com", "--user", "gone"],
142 ["auth", "token", "--hostname", "github.com"],
143 ]);
144 }).pipe(Effect.provide(layer));
145 });
146
147 it.effect("uses a token saved in Settings before GH_TOKEN and gh", () => {
148 vi.stubEnv("GH_TOKEN", "env-token");
149 const { layer, calls } = harness({}, [], { "github.com": "saved-token" });
150 return Effect.gen(function* () {
151 const credentials = yield* GitHubCredentials.GitHubCredentials;
152 const credential = yield* credentials.get("github.com");
153 expect(Redacted.value(credential.token)).toBe("saved-token");
154 expect(credential.source).toBe("settings");
155 expect(calls).toEqual([]);
156 }).pipe(Effect.provide(layer));
157 });
158
159 it.effect("keeps a host turned off even with a token saved in Settings", () => {
160 const { layer } = harness({ "github.com": { enabled: false } }, [], {
161 "github.com": "saved-token",
162 });
163 return Effect.gen(function* () {
164 const credentials = yield* GitHubCredentials.GitHubCredentials;
165 expect((yield* Effect.flip(credentials.get("github.com")))._tag).toBe(
166 "GitHubHostDisabledError",
167 );
168 }).pipe(Effect.provide(layer));
169 });
170});