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

TanStackSourceCredentials.test.ts

132 lines5.4 KB

1import { assert, describe, it } from "@effect/vitest";
2import { VcsProcessExitError } from "@t3tools/contracts";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
5import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import * as Redacted from "effect/Redacted";
9​
10import * as TanStackSourceCredentials from "./TanStackSourceCredentials.ts";
11​
12const processOutput = TestSourceControlHost.processOutput;
13const HOST = "source.tanstack.com";
14​
15function harness(
16 run: SourceControlHost.SourceControlHost["Service"]["process"]["run"],
17 env: NodeJS.ProcessEnv = {},
18) {
19 const calls: Array<SourceControlHost.SourceControlProcessInput> = [];
20 const layer = TanStackSourceCredentials.layer.pipe(
21 Layer.provideMerge(
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 };
36​
37const notSignedIn = (input: SourceControlHost.SourceControlProcessInput) =>
38 Effect.fail(
39 new VcsProcessExitError({
40 operation: input.operation,
41 command: input.command,
42 cwd: input.cwd,
43 exitCode: 1,
44 detail: "no oauth token found",
45 }),
46 );
47​
48describe("TanStackSourceCredentials", () => {
49 it("hands SRC_TOKEN only to the host src itself sends it to", () => {
50 const token = { SRC_TOKEN: "tsp_env" };
51 assert.strictEqual(TanStackSourceCredentials.environmentToken(HOST, token), "tsp_env");
52 assert.isNull(TanStackSourceCredentials.environmentToken("github.com", token));
53 assert.isNull(
54 TanStackSourceCredentials.environmentToken(HOST, { ...token, SRC_HOST: "localhost:5173" }),
55 );
56 assert.isNull(TanStackSourceCredentials.environmentToken(HOST, { SRC_TOKEN: " " }));
57 });
58​
59 it.effect("puts a saved token before SRC_TOKEN, and SRC_TOKEN before the src login", () => {
60 const { layer, calls } = harness(() => Effect.succeed(processOutput("tsp_cli\n")), {
61 SRC_TOKEN: "tsp_env",
62 });
63 return Effect.gen(function* () {
64 const credentials = yield* TanStackSourceCredentials.TanStackSourceCredentials;
65 const settings = yield* TestSourceControlHost.TestSourceControlHostSettings;
66 yield* settings.update({
67 sourceControlHosts: { "tanstack-source": { tokens: { [HOST]: "tsp_saved" } } },
68 });
69 const saved = yield* credentials.get(HOST);
70 assert.strictEqual(Redacted.value(saved.token), "tsp_saved");
71 assert.strictEqual(saved.source, "settings");
72​
73 yield* settings.update({ sourceControlHosts: { "tanstack-source": { tokens: {} } } });
74 const fromEnv = yield* credentials.get(HOST);
75 assert.strictEqual(Redacted.value(fromEnv.token), "tsp_env");
76 assert.strictEqual(fromEnv.source, "env");
77 assert.deepStrictEqual(calls, []);
78 }).pipe(Effect.provide(layer));
79 });
80​
81 it.effect("asks src for the account picked in Settings, then its active login", () => {
82 const { layer, calls } = harness((input) =>
83 input.args.includes("--user")
84 ? notSignedIn(input)
85 : Effect.succeed(processOutput("tsp_active\n")),
86 );
87 return Effect.gen(function* () {
88 const credentials = yield* TanStackSourceCredentials.TanStackSourceCredentials;
89 const settings = yield* TestSourceControlHost.TestSourceControlHostSettings;
90 yield* settings.update({
91 sourceControlHosts: { "tanstack-source": { hosts: { [HOST]: { account: "gone" } } } },
92 });
93 const credential = yield* credentials.get("Source.TanStack.com");
94 assert.strictEqual(Redacted.value(credential.token), "tsp_active");
95 assert.strictEqual(credential.source, "src");
96 assert.deepStrictEqual(
97 calls.map((call) => [call.command, ...call.args]),
98 [
99 ["src", "auth", "token", "--hostname", HOST, "--user", "gone"],
100 ["src", "auth", "token", "--hostname", HOST],
101 ],
102 );
103 // An inherited SRC_TOKEN must not answer for the login asked for.
104 assert.strictEqual(calls[0]?.env?.SRC_TOKEN, "");
105 yield* credentials.get(HOST);
106 assert.strictEqual(calls.length, 2);
107 }).pipe(Effect.provide(layer));
108 });
109​
110 it.effect("refuses a host turned off in Settings and any host that is not Source's", () => {
111 const { layer, calls } = harness(() => Effect.succeed(processOutput("tsp_cli\n")), {
112 SRC_TOKEN: "tsp_env",
113 });
114 return Effect.gen(function* () {
115 const credentials = yield* TanStackSourceCredentials.TanStackSourceCredentials;
116 const settings = yield* TestSourceControlHost.TestSourceControlHostSettings;
117 const foreign = yield* Effect.flip(credentials.get("source.tanstack.com.evil.test"));
118 assert.strictEqual(foreign._tag, "TanStackSourceNotSignedInError");
119 yield* settings.update({
120 sourceControlHosts: {
121 "tanstack-source": {
122 hosts: { [HOST]: { enabled: false } },
123 tokens: { [HOST]: "tsp_saved" },
124 },
125 },
126 });
127 const disabled = yield* Effect.flip(credentials.get(HOST));
128 assert.strictEqual(disabled._tag, "TanStackSourceHostDisabledError");
129 assert.deepStrictEqual(calls, []);
130 }).pipe(Effect.provide(layer));
131 });
132});