Files
pikasTech-unidesk/scripts/src/cli-input-errors.test.ts
T
2026-07-12 02:17:05 +02:00

58 lines
2.5 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { describe, expect, test } from "bun:test";
import { repoRoot } from "./config";
describe("CLI input error hierarchy", () => {
test("unknown root commands are compact and stack-free by default", () => {
const result = runCli("definitely-not-a-command");
expect(result.status).toBe(1);
expect(result.stderr).toBe("");
expect(result.stdout.trim().split("\n").length).toBeLessThanOrEqual(8);
expect(result.stdout).toContain("ERROR cli-input/unknown-command");
expect(result.stdout).toContain("usage: bun scripts/cli.ts help");
expect(result.stdout).not.toContain("stack");
expect(result.stdout).not.toContain(" at ");
});
test("predictable Gitea option validation is compact and stack-free", () => {
const result = runCli("platform-infra", "gitea", "status", "--target", "NC01", "--json");
expect(result.status).toBe(1);
expect(result.stderr).toBe("");
expect(result.stdout.trim().split("\n").length).toBeLessThanOrEqual(8);
expect(result.stdout).toContain("ERROR cli-input/unsupported-option");
expect(result.stdout).toContain("argument: --json");
expect(result.stdout).not.toContain("stack");
expect(result.stdout).not.toContain("platform-infra-gitea.ts:");
});
test("explicit debug retains stack while internal exceptions remain observable", () => {
const debug = runCliWithEnv({ UNIDESK_CLI_DEBUG: "1" }, "definitely-not-a-command");
expect(debug.status).toBe(1);
const debugPayload = JSON.parse(debug.stdout) as { error: { debug: boolean; stack: string } };
expect(debugPayload.error.debug).toBe(true);
expect(debugPayload.error.stack).toContain("CliInputError: Unknown command");
const internal = spawnSync("bun", ["-e", "import { emitError } from './scripts/src/output.ts'; emitError('internal-probe', new Error('internal-probe-failed'));"], {
cwd: repoRoot,
env: process.env,
encoding: "utf8",
});
expect(internal.status).toBe(0);
const internalPayload = JSON.parse(internal.stdout) as { error: { message: string; stack: string } };
expect(internalPayload.error.message).toBe("internal-probe-failed");
expect(internalPayload.error.stack).toContain("Error: internal-probe-failed");
});
});
function runCli(...args: string[]) {
return runCliWithEnv({}, ...args);
}
function runCliWithEnv(env: NodeJS.ProcessEnv, ...args: string[]) {
return spawnSync("bun", ["scripts/cli.ts", ...args], {
cwd: repoRoot,
env: { ...process.env, ...env },
encoding: "utf8",
});
}