Add tenant contract doctor probe and automated regression test matrix

This commit is contained in:
gitea committed 2026-09-13 22:02:39 +08:00
1 parent e76af01480
commit 733ede97d9
12 files changed
+1741 -4

No files matched your search

+25
View File
@@ -25,6 +25,31 @@ export async function handleDoctorCommand(
return;
}
// 3. Output tenant-level health contract and quota diagnosis
if (subAction === "tenant" || subAction === "corp" || subAction === "enterprise") {
const { selectCorp } = await import("../helpers.js");
const { createSealClient } = await import("../../core/http/factory.js");
const { stringOption } = await import("../parser.js");
const { diagnoseTenantContract, formatTenantDoctorDashboard } = await import("../../core/doctor/tenant-diagnose.js");
const corpSelector =
stringOption(args.options.corp) ??
(maybeName && maybeName !== subAction ? maybeName : undefined) ??
(args.command[2] && args.command[2] !== subAction ? args.command[2] : undefined);
const corp = selectCorp(corpSelector);
const client = await createSealClient(corp);
const diagnosis = await diagnoseTenantContract(client, corp);
const isQuiet = booleanOption(args.options.quiet) || booleanOption(args.options.silent);
if (!isQuiet) {
console.error(formatTenantDoctorDashboard(diagnosis));
}
printJson(diagnosis);
return;
}
// 3. Default: Full system diagnostic inspection
const diagnosis = diagnoseSystem();
+43
View File
@@ -0,0 +1,43 @@
import { describe, expect, test } from "bun:test";
import { runCli } from "../runner.js";
describe("seal-use test regression CLI", () => {
test("fails with exit code 2 when unknown subaction is requested", async () => {
const result = await runCli(["test", "invalid-action"]);
expect(result.exitCode).toBe(2);
expect(result.stderr).toContain("未知的 test 子命令");
});
test("rejects when production enterprise has no bound test enterprise", async () => {
const result = await runCli(["test", "regression", "--corp", "ID01Nl1kQVYbKL"]);
expect(result.exitCode).toBe(2);
expect(result.stderr).toContain("未绑定测试靶场企业");
expect(result.stderr).toContain("硬性规定:回归测试严禁在生产环境直接运行");
});
test("executes dry-run regression preview on paired test enterprise", async () => {
const result = await runCli(["test", "regression", "--corp", "xiezihao", "--samples", "2", "--dry-run"]);
expect(result.exitCode).toBe(0);
const payload = JSON.parse(result.stdout);
expect(payload.ok).toBe(true);
expect(payload.verdict).toBe("DRY_RUN");
expect(payload.safeToPublish).toBe(true);
expect(payload.metrics.sampleCount).toBe(2);
expect(payload.sourceCorp.id).toBe("LyG3iezucw03fs");
expect(payload.targetCorp.id).toBe("xiezihao");
expect(payload.records.length).toBe(2);
expect(result.stderr).toContain("规则回归测试矩阵");
expect(result.stderr).toContain("DRY-RUN");
});
test("corps regression alias routes to test regression command", async () => {
const result = await runCli(["corps", "regression", "--corp", "xiezihao", "--samples", "2", "--dry-run"]);
expect(result.exitCode).toBe(0);
const payload = JSON.parse(result.stdout);
expect(payload.verdict).toBe("DRY_RUN");
expect(payload.targetCorp.id).toBe("xiezihao");
});
});
+113
View File
@@ -0,0 +1,113 @@
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"
);
}
+43
View File
@@ -143,6 +143,17 @@ export const HELP_REGISTRY: AreaHelp[] = [
name: "add-link",
summary: "Import enterprise configuration from a Hose assistance link",
usage: ["seal-use corps add-link <https://.../web/thirdparty.html?...> [--name <customName>] [--overwrite]"]
},
{
name: "regression",
aliases: ["test-regression"],
summary: "Run automated rule regression test matrix against paired test enterprise",
usage: ["seal-use corps regression [--corp <corpId>] [--samples <N>] [--dry-run] [--fail-on-flips]"],
options: [
{ flag: "--samples <N>", description: "Number of historical production runs to replay (default: 10)" },
{ flag: "--dry-run", description: "Preview sample documents and verify quota without executing replayed audits" },
{ flag: "--fail-on-flips", description: "Exit with non-zero code if any decision flip is detected" }
]
}
]
},
@@ -1009,6 +1020,38 @@ export const HELP_REGISTRY: AreaHelp[] = [
aliases: ["dirs"],
summary: "Inspect core filesystem paths and directory health",
usage: ["seal-use doctor paths"]
},
{
name: "tenant",
aliases: ["corp", "enterprise"],
summary: "Inspect tenant-level health contract, auth permissions, AI approval quota, and rules state",
usage: ["seal-use doctor tenant [--corp <corpId>] [--quiet]"],
options: [
{ flag: "--corp <corpId>", description: "Enterprise ID or selector to diagnose" },
{ flag: "--quiet", description: "Suppress formatted dashboard on stderr" }
]
}
]
},
{
area: "test",
title: "Automated test harnesses and rule regression matrix",
description: "Run automated regression tests and decision comparison matrix across paired production and test environments.",
commands: [
{
name: "regression",
aliases: ["regress", "matrix"],
summary: "Run automated rule regression test matrix against paired test enterprise",
usage: ["seal-use test regression [--corp <corpId>] [--samples <N>] [--dry-run] [--fail-on-flips]"],
options: [
{ flag: "--corp <corpId>", description: "Target test enterprise or production parent (must be paired)" },
{ flag: "--samples <N>", description: "Number of historical production runs to replay (default: 10)" },
{ flag: "--dry-run", description: "Preview sample documents and verify quota without executing replayed audits" },
{ flag: "--fail-on-flips", description: "Exit with non-zero code if any decision flip is detected" },
{ flag: "--prod-corp <id>", description: "Explicit production enterprise source for sample documents" },
{ flag: "--test-corp <id>", description: "Explicit test enterprise target for replay" },
{ flag: "--quiet", description: "Suppress formatted summary dashboard on stderr" }
]
}
]
}
+12 -1
View File
@@ -29,6 +29,7 @@ import { handleServiceCommand } from "./commands/service.js";
import { handleSimulationCommand } from "./commands/simulation.js";
import { handleSkillCommand } from "./commands/skill.js";
import { handleSourceCommand } from "./commands/source.js";
import { handleTestCommand } from "./commands/test.js";
import { handleToolCommand } from "./commands/tool.js";
import { handleToolsListCommand } from "./commands/tools.js";
import { handleUpdateCommand } from "./commands/update.js";
@@ -91,7 +92,8 @@ export const KNOWN_AREAS = new Set([
"openapi",
"probe",
"doctor",
"changelog"
"changelog",
"test"
]);
export type { CliResult, CliRunOptions } from "./types.js";
@@ -228,6 +230,11 @@ async function dispatch(argv: string[], runtime: CliRuntime) {
return;
}
if (area === "test") {
await handleTestCommand(action, maybeName, args);
return;
}
if (area === "config") {
handleConfigCommand(action, maybeName, args);
return;
@@ -283,6 +290,10 @@ async function dispatch(argv: string[], runtime: CliRuntime) {
}
if (area === "corps" || area === "corp") {
if (action === "regression" || action === "test-regression") {
await handleTestCommand("regression", maybeName, args);
return;
}
await handleCorpsCommand(action, maybeName, args);
return;
}