110 lines
6.7 KiB
TypeScript
110 lines
6.7 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { readdirSync, readFileSync } from "node:fs";
|
|
import { join, relative } from "node:path";
|
|
import ts from "typescript";
|
|
import { commandFlags, COMMAND_METADATA } from "./command-metadata.js";
|
|
import { parseArgs } from "./parser.js";
|
|
import { runCli } from "./runner.js";
|
|
import { CLI_COMMAND_REGISTRY, findCommandByArea } from "./registry.js";
|
|
import { runAiProCli } from "../aipro-cli.js";
|
|
|
|
describe("strict command options", () => {
|
|
test("rejects misspellings and missing values instead of silently dropping filters", async () => {
|
|
const typo = await runCli(["bill", "list", "--limitt", "5", "--json"]);
|
|
expect(typo.exitCode).toBe(2);
|
|
expect(JSON.parse(typo.stdout).message).toContain("--limit");
|
|
expect(typo.stderr).toBe("");
|
|
for (const args of [["bill", "list", "--limit"], ["bill", "get", "-o"], ["--corp"], ["workflow", "get", "--file="], ["bill", "list", "--limit", "--json"]]) {
|
|
expect((await runCli(args)).exitCode).toBe(2);
|
|
}
|
|
expect((await runCli(["mcp", "serve", "--unknwon"])).exitCode).toBe(2);
|
|
});
|
|
|
|
test("uses command-specific boolean and value options without swallowing IDs", () => {
|
|
expect(parseArgs(["bill", "list", "--fields", "name,code"]).options.fields).toBe("name,code");
|
|
expect(parseArgs(["feetype", "get", "--fields", "FT001"]).args).toEqual(["FT001"]);
|
|
expect(parseArgs(["wf", "job", "--verbose", "JOB_123"]).args).toEqual(["JOB_123"]);
|
|
expect(parseArgs(["workflow", "create", "-f", "template.json"]).options.f).toBe("template.json");
|
|
expect(parseArgs(["workflow", "tail", "-f", "PIPE_123"]).args).toEqual(["PIPE_123"]);
|
|
expect(parseArgs(["auth", "import", "--raw", "curl https://example.test"]).options.raw).toBe("curl https://example.test");
|
|
expect(parseArgs(["update", "--version", "0.9.0"]).options.version).toBe("0.9.0");
|
|
expect(parseArgs(["http", "-h", "127.0.0.1"]).options.h).toBe("127.0.0.1");
|
|
expect(parseArgs(["-c", "corp-test", "bill", "list"]).options.corp).toBe("corp-test");
|
|
expect(parseArgs(["workflow", "get", "ID", "-c", "corp-test"]).options.corp).toBe("corp-test");
|
|
expect(parseArgs(["status", "-c", "corp-test"], "aipro").options.corp).toBe("corp-test");
|
|
expect(parseArgs(["platform", "records", "App", "Entity", "-c", "corp-test"]).options.corp).toBe("corp-test");
|
|
expect(parseArgs(["bind", "R1", "-c", "[1]"], "aipro").options.corp).toBeUndefined();
|
|
expect(parseArgs(["bill", "list", "-c", "BILL001"]).options.corp).toBeUndefined();
|
|
expect(parseArgs(["-h", "http"]).options.help).toBe(true);
|
|
expect(parseArgs(["feetype", "get", "--fields", "1"]).args).toEqual(["1"]);
|
|
});
|
|
|
|
test("preserves explicit booleans, aliases, negative values, and -- boundaries", () => {
|
|
const parsed = parseArgs(["--json", "--corp", "corp-test", "bill", "list", "--limit=-5", "--mine=false", "--", "--literal"]);
|
|
expect(parsed.options).toMatchObject({ json: true, corp: "corp-test", limit: "-5", mine: false });
|
|
expect(parsed.args).toEqual(["--literal"]);
|
|
expect(parseArgs(["bill", "list", "--limit", "-5"]).options.limit).toBe("-5");
|
|
expect(parseArgs(["platform", "record", "toggle", "--active=0", "REC_1"]).args).toEqual(["toggle", "REC_1"]);
|
|
expect(parseArgs(["bill", "get", "ID", "-o", "/tmp/output.json"]).options.outputFile).toBe("/tmp/output.json");
|
|
expect(() => parseArgs(["bill", "get", "ID", "--json=garbage"])).toThrow("布尔值");
|
|
});
|
|
|
|
test("AI PRO returns structured parse failures before opening its repository", async () => {
|
|
const result = await runAiProCli(["review", "--findngs", "[]", "--json"]);
|
|
expect(result.exitCode).toBe(2);
|
|
expect(JSON.parse(result.stdout).message).toContain("--findings");
|
|
expect(parseArgs(["review", "--run", "R1"], "aipro").options.run).toBe("R1");
|
|
expect(parseArgs(["decide", "approve", "R1", "--run", "--yes"], "aipro").options.run).toBe(true);
|
|
const help = await runAiProCli(["review", "--help", "--json"]);
|
|
expect(help.exitCode).toBe(0);
|
|
expect(JSON.parse(help.stdout).help).toContain("核验要点");
|
|
});
|
|
|
|
test("all command domains, aliases and declared flags have usable help", async () => {
|
|
expect(CLI_COMMAND_REGISTRY.map(command => command.area)).toEqual(Object.keys(COMMAND_METADATA));
|
|
const names = CLI_COMMAND_REGISTRY.flatMap(command => [command.area, ...command.aliases]);
|
|
expect(new Set(names).size).toBe(names.length);
|
|
for (const [area, metadata] of Object.entries(COMMAND_METADATA)) {
|
|
for (const name of [area, ...metadata.aliases]) {
|
|
expect(findCommandByArea(name)?.area).toBe(area);
|
|
const result = await runCli(["help", name]);
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain(`${area} -`);
|
|
if (name === area) {
|
|
const documentedFlags = result.stdout.split("支持选项:\n")[1].split("\n\n提示:")[0].trim().split("\n").map(line => line.trim().match(/^--([^=[\s]+)/)?.[1]);
|
|
expect(documentedFlags).toEqual(Object.keys(commandFlags(area)));
|
|
}
|
|
}
|
|
}
|
|
const workflow = await runCli(["wf", "--help"]);
|
|
for (const action of ["enum", "lint-script", "eval-script"]) expect(workflow.stdout).toContain(action);
|
|
expect((await runCli(["help", "workflow", "not-real"])).exitCode).toBe(2);
|
|
expect((await runCli(["help", "workflow", "constructor"])).exitCode).toBe(2);
|
|
expect((await runCli(["help", "not-real"])).exitCode).toBe(2);
|
|
expect((await runCli(["workflow", "help", "enum"])).stdout).toContain("枚举");
|
|
});
|
|
|
|
test("handler option declarations cannot drift outside the shared flag vocabulary", () => {
|
|
const root = join(import.meta.dir, "commands");
|
|
function files(dir: string): string[] {
|
|
return readdirSync(dir, { withFileTypes: true }).flatMap(e => e.isDirectory() ? files(join(dir, e.name)) : e.name.endsWith(".ts") && !e.name.endsWith(".test.ts") ? [join(dir, e.name)] : []);
|
|
}
|
|
const missing: string[] = [];
|
|
for (const path of files(root)) {
|
|
const area = relative(root, path).split("/")[0].replace(/\.ts$/, "");
|
|
const source = ts.createSourceFile(path, readFileSync(path, "utf8"), ts.ScriptTarget.Latest, true);
|
|
function visit(node: ts.Node) {
|
|
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && /^(booleanOption|stringOption|numberOption|listOption)$/.test(node.expression.text)) {
|
|
const keys = node.arguments[node.expression.text === "listOption" ? 0 : 1];
|
|
if (keys && ts.isArrayLiteralExpression(keys)) {
|
|
for (const key of keys.elements) if (ts.isStringLiteral(key) && !commandFlags(area)[key.text]) missing.push(`${relative(root, path)}: --${key.text}`);
|
|
}
|
|
}
|
|
ts.forEachChild(node, visit);
|
|
}
|
|
visit(source);
|
|
}
|
|
expect(missing).toEqual([]);
|
|
});
|
|
});
|