Co-authored-by: codex <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: cursor[bot] <206951365+cursor[bot]@users.noreply.github.com>
134 lines
5.1 KiB
TypeScript
134 lines
5.1 KiB
TypeScript
import { mkdtempSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import { NetService } from "@t3tools/shared/Net";
|
|
import { assert, it } from "@effect/vitest";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import * as CliError from "effect/unstable/cli/CliError";
|
|
import * as TestConsole from "effect/testing/TestConsole";
|
|
import { Command } from "effect/unstable/cli";
|
|
|
|
import { cli } from "./cli.ts";
|
|
|
|
const CliRuntimeLayer = Layer.mergeAll(NodeServices.layer, NetService.layer);
|
|
|
|
const runCli = (args: ReadonlyArray<string>) => Command.runWith(cli, { version: "0.0.0" })(args);
|
|
const runCliWithRuntime = (args: ReadonlyArray<string>) =>
|
|
runCli(args).pipe(Effect.provide(CliRuntimeLayer));
|
|
|
|
const captureStdout = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
|
Effect.gen(function* () {
|
|
const result = yield* effect;
|
|
const output =
|
|
(yield* TestConsole.logLines).findLast((line): line is string => typeof line === "string") ??
|
|
"";
|
|
return { result, output };
|
|
}).pipe(Effect.provide(Layer.mergeAll(CliRuntimeLayer, TestConsole.layer)));
|
|
|
|
it.layer(NodeServices.layer)("cli log-level parsing", (it) => {
|
|
it.effect("accepts the built-in lowercase log-level flag values", () =>
|
|
runCliWithRuntime(["--log-level", "debug", "--version"]),
|
|
);
|
|
|
|
it.effect("rejects invalid log-level casing before launching the server", () =>
|
|
Effect.gen(function* () {
|
|
const error = yield* runCliWithRuntime(["--log-level", "Debug"]).pipe(Effect.flip);
|
|
|
|
if (!CliError.isCliError(error)) {
|
|
assert.fail(`Expected CliError, got ${String(error)}`);
|
|
}
|
|
if (error._tag !== "InvalidValue") {
|
|
assert.fail(`Expected InvalidValue, got ${error._tag}`);
|
|
}
|
|
assert.equal(error.option, "log-level");
|
|
assert.equal(error.value, "Debug");
|
|
}),
|
|
);
|
|
|
|
it.effect("executes auth pairing subcommands and redacts secrets from list output", () =>
|
|
Effect.gen(function* () {
|
|
const baseDir = mkdtempSync(join(tmpdir(), "t3-cli-auth-pairing-test-"));
|
|
|
|
const createdOutput = yield* captureStdout(
|
|
runCli(["auth", "pairing", "create", "--base-dir", baseDir, "--json"]),
|
|
);
|
|
const created = JSON.parse(createdOutput.output) as {
|
|
readonly id: string;
|
|
readonly credential: string;
|
|
};
|
|
const listedOutput = yield* captureStdout(
|
|
runCli(["auth", "pairing", "list", "--base-dir", baseDir, "--json"]),
|
|
);
|
|
const listed = JSON.parse(listedOutput.output) as ReadonlyArray<{
|
|
readonly id: string;
|
|
readonly credential?: string;
|
|
}>;
|
|
|
|
assert.equal(typeof created.id, "string");
|
|
assert.equal(typeof created.credential, "string");
|
|
assert.equal(created.credential.length > 0, true);
|
|
assert.equal(listed.length, 1);
|
|
assert.equal(listed[0]?.id, created.id);
|
|
assert.equal("credential" in (listed[0] ?? {}), false);
|
|
}),
|
|
);
|
|
|
|
it.effect("executes auth session subcommands and redacts secrets from list output", () =>
|
|
Effect.gen(function* () {
|
|
const baseDir = mkdtempSync(join(tmpdir(), "t3-cli-auth-session-test-"));
|
|
|
|
const issuedOutput = yield* captureStdout(
|
|
runCli(["auth", "session", "issue", "--base-dir", baseDir, "--json"]),
|
|
);
|
|
const issued = JSON.parse(issuedOutput.output) as {
|
|
readonly sessionId: string;
|
|
readonly token: string;
|
|
readonly role: string;
|
|
};
|
|
const listedOutput = yield* captureStdout(
|
|
runCli(["auth", "session", "list", "--base-dir", baseDir, "--json"]),
|
|
);
|
|
const listed = JSON.parse(listedOutput.output) as ReadonlyArray<{
|
|
readonly sessionId: string;
|
|
readonly token?: string;
|
|
readonly role: string;
|
|
}>;
|
|
|
|
assert.equal(typeof issued.sessionId, "string");
|
|
assert.equal(typeof issued.token, "string");
|
|
assert.equal(issued.role, "owner");
|
|
assert.equal(listed.length, 1);
|
|
assert.equal(listed[0]?.sessionId, issued.sessionId);
|
|
assert.equal(listed[0]?.role, "owner");
|
|
assert.equal("token" in (listed[0] ?? {}), false);
|
|
}),
|
|
);
|
|
|
|
it.effect("rejects invalid ttl values before running auth commands", () =>
|
|
Effect.gen(function* () {
|
|
const error = yield* runCliWithRuntime(["auth", "pairing", "create", "--ttl", "soon"]).pipe(
|
|
Effect.flip,
|
|
);
|
|
|
|
if (!CliError.isCliError(error)) {
|
|
assert.fail(`Expected CliError, got ${String(error)}`);
|
|
}
|
|
if (error._tag !== "ShowHelp") {
|
|
assert.fail(`Expected ShowHelp, got ${error._tag}`);
|
|
}
|
|
assert.deepEqual(error.commandPath, ["t3", "auth", "pairing", "create"]);
|
|
const ttlError = error.errors[0] as CliError.CliError | undefined;
|
|
if (!ttlError || ttlError._tag !== "InvalidValue") {
|
|
assert.fail(`Expected InvalidValue, got ${String(ttlError?._tag)}`);
|
|
}
|
|
assert.equal(ttlError.option, "ttl");
|
|
assert.equal(ttlError.value, "soon");
|
|
assert.isTrue(ttlError.message.includes("Invalid duration"));
|
|
assert.isTrue(ttlError.message.includes("5m, 1h, 30d, or 15 minutes"));
|
|
}),
|
|
);
|
|
});
|