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

cli.test.ts

133 lines5.1 KB

1import { mkdtempSync } from "node:fs";
2import { tmpdir } from "node:os";
3import { join } from "node:path";
4​
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import { NetService } from "@t3tools/shared/Net";
7import { assert, it } from "@effect/vitest";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as CliError from "effect/unstable/cli/CliError";
11import * as TestConsole from "effect/testing/TestConsole";
12import { Command } from "effect/unstable/cli";
13​
14import { cli } from "./cli.ts";
15​
16const CliRuntimeLayer = Layer.mergeAll(NodeServices.layer, NetService.layer);
17​
18const runCli = (args: ReadonlyArray<string>) => Command.runWith(cli, { version: "0.0.0" })(args);
19const runCliWithRuntime = (args: ReadonlyArray<string>) =>
20 runCli(args).pipe(Effect.provide(CliRuntimeLayer));
21​
22const captureStdout = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
23 Effect.gen(function* () {
24 const result = yield* effect;
25 const output =
26 (yield* TestConsole.logLines).findLast((line): line is string => typeof line === "string") ??
27 "";
28 return { result, output };
29 }).pipe(Effect.provide(Layer.mergeAll(CliRuntimeLayer, TestConsole.layer)));
30​
31it.layer(NodeServices.layer)("cli log-level parsing", (it) => {
32 it.effect("accepts the built-in lowercase log-level flag values", () =>
33 runCliWithRuntime(["--log-level", "debug", "--version"]),
34 );
35​
36 it.effect("rejects invalid log-level casing before launching the server", () =>
37 Effect.gen(function* () {
38 const error = yield* runCliWithRuntime(["--log-level", "Debug"]).pipe(Effect.flip);
39​
40 if (!CliError.isCliError(error)) {
41 assert.fail(`Expected CliError, got ${String(error)}`);
42 }
43 if (error._tag !== "InvalidValue") {
44 assert.fail(`Expected InvalidValue, got ${error._tag}`);
45 }
46 assert.equal(error.option, "log-level");
47 assert.equal(error.value, "Debug");
48 }),
49 );
50​
51 it.effect("executes auth pairing subcommands and redacts secrets from list output", () =>
52 Effect.gen(function* () {
53 const baseDir = mkdtempSync(join(tmpdir(), "t3-cli-auth-pairing-test-"));
54​
55 const createdOutput = yield* captureStdout(
56 runCli(["auth", "pairing", "create", "--base-dir", baseDir, "--json"]),
57 );
58 const created = JSON.parse(createdOutput.output) as {
59 readonly id: string;
60 readonly credential: string;
61 };
62 const listedOutput = yield* captureStdout(
63 runCli(["auth", "pairing", "list", "--base-dir", baseDir, "--json"]),
64 );
65 const listed = JSON.parse(listedOutput.output) as ReadonlyArray<{
66 readonly id: string;
67 readonly credential?: string;
68 }>;
69​
70 assert.equal(typeof created.id, "string");
71 assert.equal(typeof created.credential, "string");
72 assert.equal(created.credential.length > 0, true);
73 assert.equal(listed.length, 1);
74 assert.equal(listed[0]?.id, created.id);
75 assert.equal("credential" in (listed[0] ?? {}), false);
76 }),
77 );
78​
79 it.effect("executes auth session subcommands and redacts secrets from list output", () =>
80 Effect.gen(function* () {
81 const baseDir = mkdtempSync(join(tmpdir(), "t3-cli-auth-session-test-"));
82​
83 const issuedOutput = yield* captureStdout(
84 runCli(["auth", "session", "issue", "--base-dir", baseDir, "--json"]),
85 );
86 const issued = JSON.parse(issuedOutput.output) as {
87 readonly sessionId: string;
88 readonly token: string;
89 readonly role: string;
90 };
91 const listedOutput = yield* captureStdout(
92 runCli(["auth", "session", "list", "--base-dir", baseDir, "--json"]),
93 );
94 const listed = JSON.parse(listedOutput.output) as ReadonlyArray<{
95 readonly sessionId: string;
96 readonly token?: string;
97 readonly role: string;
98 }>;
99​
100 assert.equal(typeof issued.sessionId, "string");
101 assert.equal(typeof issued.token, "string");
102 assert.equal(issued.role, "owner");
103 assert.equal(listed.length, 1);
104 assert.equal(listed[0]?.sessionId, issued.sessionId);
105 assert.equal(listed[0]?.role, "owner");
106 assert.equal("token" in (listed[0] ?? {}), false);
107 }),
108 );
109​
110 it.effect("rejects invalid ttl values before running auth commands", () =>
111 Effect.gen(function* () {
112 const error = yield* runCliWithRuntime(["auth", "pairing", "create", "--ttl", "soon"]).pipe(
113 Effect.flip,
114 );
115​
116 if (!CliError.isCliError(error)) {
117 assert.fail(`Expected CliError, got ${String(error)}`);
118 }
119 if (error._tag !== "ShowHelp") {
120 assert.fail(`Expected ShowHelp, got ${error._tag}`);
121 }
122 assert.deepEqual(error.commandPath, ["t3", "auth", "pairing", "create"]);
123 const ttlError = error.errors[0] as CliError.CliError | undefined;
124 if (!ttlError || ttlError._tag !== "InvalidValue") {
125 assert.fail(`Expected InvalidValue, got ${String(ttlError?._tag)}`);
126 }
127 assert.equal(ttlError.option, "ttl");
128 assert.equal(ttlError.value, "soon");
129 assert.isTrue(ttlError.message.includes("Invalid duration"));
130 assert.isTrue(ttlError.message.includes("5m, 1h, 30d, or 15 minutes"));
131 }),
132 );
133});