114 lines
3.7 KiB
TypeScript
114 lines
3.7 KiB
TypeScript
import { CliError } from "../errors.js";
|
|
import { selectCorp } from "../helpers.js";
|
|
import { booleanOption, numberOption, stringOption } from "../parser.js";
|
|
import { printJson } from "../printer.js";
|
|
import type { ParsedArgs } from "../types.js";
|
|
import { createSealClient } from "../../core/http/factory.js";
|
|
import {
|
|
formatRegressionDashboard,
|
|
runRegressionTest
|
|
} from "../../domains/seal/regression/service.js";
|
|
|
|
export async function handleTestCommand(
|
|
action: string | undefined,
|
|
maybeName: string | undefined,
|
|
args: ParsedArgs
|
|
) {
|
|
const subAction = (action ?? maybeName ?? "regression").toLowerCase();
|
|
|
|
if (subAction === "regression" || subAction === "regress" || subAction === "matrix") {
|
|
const corpSelector =
|
|
stringOption(args.options.corp) ??
|
|
(maybeName && maybeName !== subAction ? maybeName : undefined);
|
|
|
|
const baseCorp = selectCorp(corpSelector);
|
|
|
|
let sourceCorp = baseCorp;
|
|
let targetCorp = baseCorp;
|
|
|
|
if (baseCorp.environment === "test") {
|
|
targetCorp = baseCorp;
|
|
const sourceCorpId =
|
|
stringOption(args.options["prod-corp"]) ??
|
|
stringOption(args.options["prod"]) ??
|
|
baseCorp.prodCorpId;
|
|
|
|
if (!sourceCorpId) {
|
|
throw new CliError(
|
|
`测试企业「${baseCorp.name}」(${baseCorp.id}) 未绑定生产母企业,无法拉取历史样本。\n` +
|
|
`请建立绑定或通过 --prod-corp 参数指定生产企业来源。`,
|
|
2,
|
|
`seal-use corps bind-test <prodCorpId> ${baseCorp.id}`
|
|
);
|
|
}
|
|
sourceCorp = selectCorp(sourceCorpId);
|
|
} else {
|
|
sourceCorp = baseCorp;
|
|
const targetCorpId =
|
|
stringOption(args.options["test-corp"]) ??
|
|
stringOption(args.options["test"]) ??
|
|
baseCorp.testCorpId;
|
|
|
|
if (!targetCorpId) {
|
|
throw new CliError(
|
|
`生产企业「${baseCorp.name}」(${baseCorp.id}) 未绑定测试靶场企业。\n` +
|
|
`硬性规定:回归测试严禁在生产环境直接运行。必须绑定并指定测试环境(testCorp)。`,
|
|
2,
|
|
`seal-use corps bind-test ${baseCorp.id} <testCorpId>`
|
|
);
|
|
}
|
|
targetCorp = selectCorp(targetCorpId);
|
|
}
|
|
|
|
const sourceClient = await createSealClient(sourceCorp);
|
|
const targetClient = await createSealClient(targetCorp);
|
|
|
|
const samples =
|
|
numberOption(args.options.samples) ??
|
|
numberOption(args.options.limit) ??
|
|
numberOption(args.options.n) ??
|
|
10;
|
|
|
|
const dryRun = booleanOption(args.options["dry-run"]);
|
|
const failOnFlips = booleanOption(args.options["fail-on-flips"]);
|
|
const pollIntervalMs = numberOption(args.options["poll-interval"]) ?? 2000;
|
|
const timeoutMs = numberOption(args.options.timeout) ?? 45000;
|
|
const snPrefix = stringOption(args.options["sn-prefix"]) ?? "REG_";
|
|
|
|
const result = await runRegressionTest({
|
|
sourceCorp,
|
|
targetCorp,
|
|
sourceClient,
|
|
targetClient,
|
|
samples,
|
|
dryRun,
|
|
pollIntervalMs,
|
|
timeoutMs,
|
|
snPrefix,
|
|
failOnFlips
|
|
});
|
|
|
|
const isQuiet = booleanOption(args.options.quiet) || booleanOption(args.options.silent);
|
|
if (!isQuiet) {
|
|
console.error(formatRegressionDashboard(result));
|
|
}
|
|
|
|
// Machine channel (stdout): clean, structured JSON
|
|
printJson(result);
|
|
|
|
if (failOnFlips && !result.ok) {
|
|
throw new CliError(
|
|
`回归测试发现 ${result.metrics.flipCount} 笔单据决策翻转,已按 --fail-on-flips 策略非零终止。`,
|
|
1
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
|
|
throw new CliError(
|
|
`未知的 test 子命令: "${subAction}"。\n可用子命令: seal-use test regression [--corp <id>] [--samples <N>] [--dry-run]`,
|
|
2,
|
|
"seal-use test regression --help"
|
|
);
|
|
}
|