Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as PlatformError from "effect/PlatformError";
5import * as Redacted from "effect/Redacted";
6import { VcsProcessExitError, VcsProcessSpawnError } from "@t3tools/contracts";
7import * as HostProcess from "@t3tools/shared/HostProcess";
8import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
9import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
10
11import * as GitCafeCredentials from "./GitCafeCredentials.ts";
12
13const processOutput = TestSourceControlHost.processOutput;
14
15function harness(
16 run: SourceControlHost.SourceControlHost["Service"]["process"]["run"],
17 env: NodeJS.ProcessEnv = {},
18) {
19 const calls: Array<SourceControlHost.SourceControlProcessInput> = [];
20 const layer = GitCafeCredentials.layer.pipe(
21 Layer.provide(
22 TestSourceControlHost.layer({
23 process: {
24 run: (input) => {
25 calls.push(input);
26 return run(input);
27 },
28 },
29 }),
30 ),
31 Layer.provide(Layer.succeed(HostProcess.Environment, env)),
32 Layer.provide(Layer.succeed(HostProcess.WorkingDirectory, "/server")),
33 );
34 return { layer, calls };
35}
36
37describe("GitCafeCredentials", () => {
38 it("hands CAFE_TOKEN only to the host cafe itself targets", () => {
39 const token = { CAFE_TOKEN: "env-token" };
40 assert.strictEqual(GitCafeCredentials.environmentToken("git.cafe", token), "env-token");
41 assert.isNull(GitCafeCredentials.environmentToken("staging.git.cafe", token));
42 for (const CAFE_HOST of ["https://staging.git.cafe/api", "staging.git.cafe"]) {
43 const staging = { ...token, CAFE_HOST };
44 assert.strictEqual(
45 GitCafeCredentials.environmentToken("staging.git.cafe", staging),
46 "env-token",
47 );
48 assert.isNull(GitCafeCredentials.environmentToken("git.cafe", staging));
49 }
50 assert.isNull(GitCafeCredentials.environmentToken("git.cafe", { CAFE_TOKEN: " " }));
51 });
52
53 it.effect("prefers CAFE_TOKEN without running cafe", () => {
54 const { layer, calls } = harness(() => Effect.die("cafe must not run"), {
55 CAFE_TOKEN: "env-token",
56 });
57 return Effect.gen(function* () {
58 const credentials = yield* GitCafeCredentials.GitCafeCredentials;
59 const credential = yield* credentials.get("git.cafe");
60 assert.strictEqual(Redacted.value(credential.token), "env-token");
61 assert.strictEqual(credential.source, "env");
62 assert.deepStrictEqual(calls, []);
63 }).pipe(Effect.provide(layer));
64 });
65
66 it.effect("uses a token saved in Settings for git.cafe only, ahead of CAFE_TOKEN", () => {
67 const calls: Array<SourceControlHost.SourceControlProcessInput> = [];
68 const layer = GitCafeCredentials.layer.pipe(
69 Layer.provideMerge(
70 TestSourceControlHost.layer({
71 process: {
72 run: (input) => {
73 calls.push(input);
74 return Effect.succeed(processOutput("password=gct_staging\n"));
75 },
76 },
77 }),
78 ),
79 Layer.provide(Layer.succeed(HostProcess.Environment, { CAFE_TOKEN: "env-token" })),
80 Layer.provide(Layer.succeed(HostProcess.WorkingDirectory, "/server")),
81 );
82 return Effect.gen(function* () {
83 const credentials = yield* GitCafeCredentials.GitCafeCredentials;
84 const settings = yield* TestSourceControlHost.TestSourceControlHostSettings;
85 yield* settings.update({ sourceControlHosts: { gitcafe: { token: "saved-token" } } });
86
87 const saved = yield* credentials.get("git.cafe");
88 assert.strictEqual(Redacted.value(saved.token), "saved-token");
89 assert.strictEqual(saved.source, "settings");
90 assert.deepStrictEqual(yield* credentials.cliEnv("git.cafe"), { CAFE_TOKEN: "saved-token" });
91 assert.deepStrictEqual(yield* credentials.cliEnv("staging.git.cafe"), {});
92 assert.strictEqual(
93 Redacted.value((yield* credentials.get("staging.git.cafe")).token),
94 "gct_staging",
95 );
96
97 yield* settings.update({ sourceControlHosts: { gitcafe: { token: "" } } });
98 assert.strictEqual((yield* credentials.get("git.cafe")).source, "env");
99 }).pipe(Effect.provide(layer));
100 });
101
102 it.effect("asks cafe's Git credential helper over stdin and reuses the token", () => {
103 const { layer, calls } = harness(() =>
104 Effect.succeed(
105 processOutput("protocol=https\nhost=staging.git.cafe\nusername=alice\npassword=gct_1\n"),
106 ),
107 );
108 return Effect.gen(function* () {
109 const credentials = yield* GitCafeCredentials.GitCafeCredentials;
110 const credential = yield* credentials.get("Staging.Git.Cafe");
111 assert.strictEqual(Redacted.value(credential.token), "gct_1");
112 assert.strictEqual(credential.source, "cafe");
113 yield* credentials.get("staging.git.cafe");
114 assert.strictEqual(calls.length, 1);
115 assert.strictEqual(calls[0]?.command, "cafe");
116 assert.deepStrictEqual(calls[0]?.args, [
117 "auth",
118 "http",
119 "credential",
120 "--host",
121 "https://staging.git.cafe/api",
122 "get",
123 ]);
124 assert.strictEqual(calls[0]?.stdin, "protocol=https\nhost=staging.git.cafe\n\n");
125 yield* credentials.invalidate("staging.git.cafe");
126 yield* credentials.get("staging.git.cafe");
127 assert.strictEqual(calls.length, 2);
128 }).pipe(Effect.provide(layer));
129 });
130
131 it.effect.each([
132 [
133 "a missing CLI",
134 Effect.fail(
135 new VcsProcessSpawnError({
136 operation: "GitCafeCredentials.get",
137 command: "cafe",
138 cwd: "/server",
139 cause: PlatformError.systemError({
140 _tag: "NotFound",
141 module: "ChildProcess",
142 method: "spawn",
143 }),
144 }),
145 ),
146 "GitCafeCliMissingError",
147 ],
148 [
149 "a helper with no login",
150 Effect.fail(
151 new VcsProcessExitError({
152 operation: "GitCafeCredentials.get",
153 command: "cafe",
154 cwd: "/server",
155 exitCode: 1,
156 detail: "not signed in",
157 }),
158 ),
159 "GitCafeNotSignedInError",
160 ],
161 [
162 "an answer without a password",
163 Effect.succeed(processOutput("username=alice\n")),
164 "GitCafeNotSignedInError",
165 ],
166 ] as const)("reports %s", ([, result, tag]) => {
167 const { layer } = harness(() => result);
168 return Effect.gen(function* () {
169 const credentials = yield* GitCafeCredentials.GitCafeCredentials;
170 const error = yield* credentials.get("git.cafe").pipe(Effect.flip);
171 assert.strictEqual(error._tag, tag);
172 }).pipe(Effect.provide(layer));
173 });
174});