151 lines
7.7 KiB
TypeScript
151 lines
7.7 KiB
TypeScript
import { test, expect, describe } from "bun:test";
|
||
import { readFileSync } from "node:fs";
|
||
import path from "node:path";
|
||
import { classifyOutcome, INFRA_FAILURE_PREFIXES, phaseOf } from "@bbbug/shared/batch_monitor";
|
||
import { buildBatchReport, buildBatchList } from "../src/batch_monitor";
|
||
import { AUTH_FAILURE_HINT } from "../../satellite/src/run_outcome";
|
||
import type { Task } from "../src/types";
|
||
|
||
const REPO = path.resolve(import.meta.dir, "../..");
|
||
const satText = ["satellite/src/runner.ts", "satellite/src/evaluator.ts"]
|
||
.map((f) => readFileSync(path.join(REPO, f), "utf8")).join("\n") + "\n" + AUTH_FAILURE_HINT;
|
||
|
||
const H = 3_600_000;
|
||
const NOW = 1_800_000_000_000;
|
||
let seq = 0;
|
||
function task(p: Partial<Task>): Task {
|
||
seq++;
|
||
return {
|
||
id: `t${seq}`, title: `bug ${seq}`, meegoId: String(7000000000 + seq), meegoUrl: null,
|
||
platform: "ios", status: "awaiting_fix", failureReason: null, executorNodeId: "ios-1",
|
||
attempts: 1, tags: ["batch_a"], clusters: ["evaluator"], createdAt: NOW - 10 * H,
|
||
statusChangedAt: NOW - H, unattended: true, mrUrl: null,
|
||
...p,
|
||
} as Task;
|
||
}
|
||
const node = (id: string, platform: "ios" | "android", extra: Record<string, unknown> = {}) => ({
|
||
id, platform, clusters: ["evaluator"], live: true, health: { alerts: [] },
|
||
evalInFlight: 0, evalCapacity: 1, fixInFlight: 1, fixCapacity: 1, ...extra,
|
||
});
|
||
|
||
describe("classifyOutcome", () => {
|
||
test("fixed / unrepro / archived / cancelled", () => {
|
||
expect(classifyOutcome({ status: "manual_review", failureReason: null }).outcome).toBe("fixed");
|
||
expect(classifyOutcome({ status: "fix_succeeded", failureReason: null }).outcome).toBe("fixed");
|
||
expect(classifyOutcome({ status: "unreproducible", failureReason: null }).outcome).toBe("unrepro");
|
||
expect(classifyOutcome({ status: "archived", failureReason: null }).outcome).toBe("task_failed");
|
||
expect(classifyOutcome({ status: "cancelled", failureReason: null }).outcome).toBe("aborted");
|
||
});
|
||
test("a fix whose MR failed stays fixed but says so", () => {
|
||
const v = classifyOutcome({ status: "manual_review", failureReason: "MR 提交失败(as x):create_mr: Invalid token" });
|
||
expect(v.outcome).toBe("fixed");
|
||
expect(v.basis).toContain("MR 提交失败");
|
||
});
|
||
test("fix_failed splits infra / aborted / task verdict", () => {
|
||
expect(classifyOutcome({ status: "fix_failed", failureReason: "API 错误,多次自动重试仍失败(非修复失败,可稍后重排)" }).outcome).toBe("infra");
|
||
expect(classifyOutcome({ status: "fix_failed", failureReason: "scheduler caught: boom" }).outcome).toBe("infra");
|
||
expect(classifyOutcome({ status: "fix_failed", failureReason: "用户中止" }).outcome).toBe("aborted");
|
||
expect(classifyOutcome({ status: "fix_failed", failureReason: "Agent exited with STATUS: FIX_FAILED (or no STATUS line)" }).outcome).toBe("task_failed");
|
||
expect(classifyOutcome({ status: "fix_failed", failureReason: null }).outcome).toBe("task_failed");
|
||
});
|
||
test("info_required: infra vs a real needs-info verdict", () => {
|
||
expect(classifyOutcome({ status: "info_required", failureReason: AUTH_FAILURE_HINT }).outcome).toBe("infra");
|
||
expect(classifyOutcome({ status: "info_required", failureReason: "工单缺少复现路径" }).outcome).toBe("needs_info");
|
||
});
|
||
test("every infra prefix still appears in the satellite's wording", () => {
|
||
for (const p of INFRA_FAILURE_PREFIXES) expect(satText).toContain(p);
|
||
});
|
||
test("phases", () => {
|
||
expect(phaseOf("initializing")).toBe("evaluating");
|
||
expect(phaseOf("pending_confirmation")).toBe("waiting_human");
|
||
expect(phaseOf("awaiting_fix")).toBe("queued");
|
||
expect(phaseOf("fixing")).toBe("running");
|
||
expect(phaseOf("fix_failed")).toBe("settled");
|
||
});
|
||
});
|
||
|
||
describe("buildBatchReport", () => {
|
||
const tasks = [
|
||
task({ status: "manual_review", statusChangedAt: NOW - 2 * H }),
|
||
task({ status: "fix_failed", failureReason: "Agent exited with STATUS: FIX_FAILED (or no STATUS line)" }),
|
||
task({ status: "fix_failed", failureReason: "API 错误,多次自动重试仍失败(非修复失败,可稍后重排)" }),
|
||
task({ status: "fixing", statusChangedAt: NOW - 6 * H }),
|
||
task({ status: "awaiting_fix" }),
|
||
task({ status: "awaiting_fix", platform: "android", executorNodeId: "and-1" }),
|
||
task({ status: "initializing", platform: "android", executorNodeId: null }),
|
||
task({ status: "manual_review", tags: ["other"] }),
|
||
];
|
||
// 本批第 1 条:评估 10 分钟、排队 30 分钟、执行 60 分钟。
|
||
const tr = [
|
||
{ targetId: "t1", toStatus: "initializing", nodeId: "ios-1", ts: NOW - 5 * H },
|
||
{ targetId: "t1", toStatus: "awaiting_fix", nodeId: "ios-1", ts: NOW - 5 * H + 10 * 60_000 },
|
||
{ targetId: "t1", toStatus: "fixing", nodeId: "ios-1", ts: NOW - 5 * H + 40 * 60_000 },
|
||
{ targetId: "t1", toStatus: "manual_review", nodeId: "ios-1", ts: NOW - 5 * H + 100 * 60_000 },
|
||
];
|
||
const r = buildBatchReport("batch_a", tasks, tr, [node("ios-1", "ios"), node("and-1", "android", { live: false })], NOW);
|
||
|
||
test("scopes to the tag and counts outcomes", () => {
|
||
expect(r.total).toBe(7);
|
||
expect(r.outcomes.fixed).toBe(1);
|
||
expect(r.outcomes.task_failed).toBe(1);
|
||
expect(r.outcomes.infra).toBe(1);
|
||
expect(r.settled).toBe(3);
|
||
// 有效成功率不计基础设施失败:1 / (3 − 1)
|
||
expect(r.effectiveSuccessRate).toBeCloseTo(0.5);
|
||
expect(r.infraRate).toBeCloseTo(1 / 3);
|
||
});
|
||
|
||
test("durations from transitions", () => {
|
||
expect(r.durations.eval.p50Min).toBe(10);
|
||
expect(r.durations.queue.p50Min).toBe(30);
|
||
expect(r.durations.exec.p50Min).toBe(60);
|
||
});
|
||
|
||
test("eta = remaining × median exec ÷ live fix slots; no live slot → null", () => {
|
||
const ios = r.platforms.find((p) => p.platform === "ios")!;
|
||
expect(ios.remaining).toBe(2);
|
||
expect(ios.fixSlots).toBe(1);
|
||
expect(ios.etaHours).toBe(2);
|
||
const and = r.platforms.find((p) => p.platform === "android")!;
|
||
expect(and.fixSlots).toBe(0);
|
||
expect(and.etaHours).toBeNull();
|
||
});
|
||
|
||
test("issues: offline node, unroutable, stuck exec, infra failure", () => {
|
||
const kinds = r.issues.map((i) => i.kind);
|
||
expect(kinds).toContain("node_offline");
|
||
expect(kinds).toContain("unroutable"); // android 评估中、无主、唯一的 android 节点离线
|
||
expect(kinds).toContain("stuck_exec"); // 6h > max(4h, 3×60min)
|
||
expect(kinds).toContain("infra_failed");
|
||
expect(r.issues[0].severity).toBe("critical");
|
||
});
|
||
|
||
test("node rows carry batch-local settled / infra counts", () => {
|
||
const ios = r.nodes.find((n) => n.id === "ios-1")!;
|
||
expect(ios.settled).toBe(3);
|
||
expect(ios.infraFailed).toBe(1);
|
||
});
|
||
|
||
test("batch list groups by tag", () => {
|
||
const list = buildBatchList(tasks);
|
||
expect(list.find((b) => b.tag === "batch_a")).toMatchObject({ total: 7, settled: 3, fixed: 1 });
|
||
expect(list.find((b) => b.tag === "other")?.total).toBe(1);
|
||
});
|
||
});
|
||
|
||
describe("batch start boundary", () => {
|
||
test("phases before the batch started don't count; shown dwell starts at the batch; stuck alerts keep the real age", () => {
|
||
const start = NOW - 24 * H;
|
||
const old = task({ status: "fixing", statusChangedAt: NOW - 30 * 24 * H, createdAt: NOW - 60 * 24 * H });
|
||
const tr = [
|
||
{ targetId: old.id, toStatus: "fixing", nodeId: "ios-1", ts: NOW - 40 * 24 * H },
|
||
{ targetId: old.id, toStatus: "fix_failed", nodeId: "ios-1", ts: NOW - 39 * 24 * H },
|
||
{ targetId: old.id, toStatus: "fixing", nodeId: "ios-1", ts: NOW - 30 * 24 * H },
|
||
];
|
||
const r = buildBatchReport("batch_a", [old], tr, [node("ios-1", "ios")], NOW, start);
|
||
expect(r.durations.exec.n).toBe(0);
|
||
expect(r.tasks[0].since).toBe(start);
|
||
expect(r.issues.some((i) => i.kind === "stuck_exec" && i.message.includes("720.0"))).toBe(true);
|
||
});
|
||
});
|