fix(cli): 收敛逐级披露与输入错误
This commit is contained in:
@@ -3,23 +3,7 @@ import { describe, expect, test } from "bun:test";
|
||||
import { repoRoot } from "./config";
|
||||
import { sshHelp, sshHelpScope } from "./help";
|
||||
|
||||
type RenderedHelp = {
|
||||
ok: boolean;
|
||||
command: string;
|
||||
data: {
|
||||
command?: string;
|
||||
scope?: string;
|
||||
runtime?: { repoRoot: string };
|
||||
routeSyntax?: Record<string, string> | string;
|
||||
operationGroups?: Record<string, string>;
|
||||
scopedHelp?: { availableOperationCount: number };
|
||||
usage?: string[];
|
||||
outputTruncated?: boolean;
|
||||
dump?: unknown;
|
||||
};
|
||||
};
|
||||
|
||||
function runTransHelp(...args: string[]): { stdout: string; rendered: RenderedHelp } {
|
||||
function runTransHelp(...args: string[]): string {
|
||||
const result = spawnSync("bun", ["scripts/ssh-cli.ts", ...args], {
|
||||
cwd: repoRoot,
|
||||
env: {
|
||||
@@ -31,7 +15,7 @@ function runTransHelp(...args: string[]): { stdout: string; rendered: RenderedHe
|
||||
});
|
||||
expect(result.status).toBe(0);
|
||||
expect(result.stderr).toBe("");
|
||||
return { stdout: result.stdout, rendered: JSON.parse(result.stdout) as RenderedHelp };
|
||||
return result.stdout;
|
||||
}
|
||||
|
||||
describe("ssh bounded progressive help", () => {
|
||||
@@ -101,28 +85,41 @@ describe("ssh bounded progressive help", () => {
|
||||
|
||||
test("renders compact top-level and scoped help without dump fallback", () => {
|
||||
const top = runTransHelp("--help");
|
||||
expect(Buffer.byteLength(top.stdout, "utf8")).toBeLessThan(6_144);
|
||||
expect(top.rendered.ok).toBe(true);
|
||||
expect(top.rendered.command).toBe("trans --help");
|
||||
expect(top.rendered.data.outputTruncated).toBeUndefined();
|
||||
expect(top.rendered.data.dump).toBeUndefined();
|
||||
expect(top.rendered.data.operationGroups?.transfer).toContain("download");
|
||||
expect(Buffer.byteLength(top, "utf8")).toBeLessThan(2_048);
|
||||
expect(top.trim().split("\n")).toHaveLength(10);
|
||||
expect(top).toContain("usage: trans <route> <operation>");
|
||||
expect(top).toContain("transfer: upload | download");
|
||||
expect(top).toContain("scoped help: trans --help <operation>");
|
||||
expect(top).not.toContain("outputTruncated");
|
||||
expect(top).not.toContain("dump");
|
||||
|
||||
const routeTop = runTransHelp("NC01:k3s", "--help");
|
||||
expect(routeTop.rendered.command).toBe("trans NC01:k3s --help");
|
||||
expect(routeTop.rendered.data.outputTruncated).toBeUndefined();
|
||||
expect(routeTop.rendered.data.operationGroups).toEqual(top.rendered.data.operationGroups);
|
||||
expect(routeTop).toBe(top);
|
||||
|
||||
const applyPatch = runTransHelp("--help", "apply-patch");
|
||||
expect(applyPatch.rendered.command).toBe("trans --help apply-patch");
|
||||
expect(applyPatch.rendered.data.scope).toBe("apply-patch");
|
||||
expect(applyPatch.rendered.data.usage?.[0]).toContain("apply-patch");
|
||||
expect(applyPatch.rendered.data.outputTruncated).toBeUndefined();
|
||||
expect(applyPatch.trim().split("\n").length).toBeLessThanOrEqual(8);
|
||||
expect(applyPatch).toContain("TRANS HELP apply-patch");
|
||||
expect(applyPatch).toContain("usage: trans <route> apply-patch");
|
||||
expect(applyPatch).not.toContain("outputTruncated");
|
||||
|
||||
const download = runTransHelp("--help", "download");
|
||||
expect(download.rendered.command).toBe("trans --help download");
|
||||
expect(download.rendered.data.scope).toBe("download");
|
||||
expect(download.rendered.data.runtime?.repoRoot).toBe(repoRoot);
|
||||
expect(download.rendered.data.outputTruncated).toBeUndefined();
|
||||
expect(download.trim().split("\n").length).toBeLessThanOrEqual(8);
|
||||
expect(download).toContain("TRANS HELP download");
|
||||
expect(download).toContain("download <remote-file> <local-file>");
|
||||
expect(download).not.toContain("outputTruncated");
|
||||
});
|
||||
|
||||
test("returns compact no-stack input errors for unknown help scopes", () => {
|
||||
const result = spawnSync("bun", ["scripts/ssh-cli.ts", "--help", "unknown-operation"], {
|
||||
cwd: repoRoot,
|
||||
env: { ...process.env, UNIDESK_SSH_ENTRYPOINT: "trans", UNIDESK_TRANS_REPO_ROOT: repoRoot },
|
||||
encoding: "utf8",
|
||||
});
|
||||
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-help-scope");
|
||||
expect(result.stdout).not.toContain("stack");
|
||||
expect(result.stdout).not.toContain(" at ");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user