fix(cli): 补齐显式机器输出边界

This commit is contained in:
Codex
2026-07-12 02:17:05 +02:00
parent 80edc6c08b
commit bcc7807c3d
5 changed files with 181 additions and 38 deletions
+45 -2
View File
@@ -14,17 +14,60 @@ describe("CLI input error hierarchy", () => {
expect(result.stdout).not.toContain(" at ");
});
test("explicit machine output returns a JSON input-error envelope without a stack", () => {
for (const args of [["definitely-not-a-command", "--json"], ["definitely-not-a-command", "-o", "json"]]) {
const result = runCli(...args);
expect(result.status).toBe(1);
expect(result.stderr).toBe("");
const payload = JSON.parse(result.stdout) as {
ok: boolean;
error: { kind: string; code: string; debug: boolean; stack?: string };
};
expect(payload.ok).toBe(false);
expect(payload.error.kind).toBe("cli-input");
expect(payload.error.code).toBe("unknown-command");
expect(payload.error.debug).toBe(false);
expect(payload.error.stack).toBeUndefined();
expect(result.stdout).not.toContain("stackOmitted");
}
});
test("predictable Gitea option validation is compact and stack-free", () => {
const result = runCli("platform-infra", "gitea", "status", "--target", "NC01", "--json");
const result = runCli("platform-infra", "gitea", "status", "--target", "NC01", "--definitely-unsupported");
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).toContain("argument: --definitely-unsupported");
expect(result.stdout).not.toContain("stack");
expect(result.stdout).not.toContain("platform-infra-gitea.ts:");
});
test("only an explicitly unknown command target is converted to a compact input error", () => {
const unknownTarget = runCli("platform-infra", "gitea", "status", "--target", "definitely-nope");
expect(unknownTarget.status).toBe(1);
expect(unknownTarget.stderr).toBe("");
expect(unknownTarget.stdout.trim().split("\n").length).toBeLessThanOrEqual(8);
expect(unknownTarget.stdout).toContain("ERROR cli-input/unknown-target");
expect(unknownTarget.stdout).toContain("argument: definitely-nope");
expect(unknownTarget.stdout).not.toContain("stack");
const internalConfig = spawnSync("bun", ["-e", [
"import { emitError } from './scripts/src/output.ts';",
"import { resolveTarget } from './scripts/src/platform-infra-gitea-config.ts';",
"try { resolveTarget({ defaults: { targetId: 'NC01' }, targets: [] } as any, 'NC01'); }",
"catch (error) { emitError('internal-gitea-config-probe', error); }",
].join(" ")], {
cwd: repoRoot,
env: process.env,
encoding: "utf8",
});
expect(internalConfig.status).toBe(0);
const internalPayload = JSON.parse(internalConfig.stdout) as { error: { message: string; stack: string } };
expect(internalPayload.error.message).toContain("unknown gitea target NC01");
expect(internalPayload.error.stack).toContain("platform-infra-gitea-config.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);