import { test, expect, describe } from "bun:test"; import { readFileSync } from "node:fs"; import path from "node:path"; import { INFRA_EVAL_FAILURE_PREFIXES, isInfraEvalFailure, unattendedAction, unattendedFallbackPlan, executeGate, } from "../src/task_execution"; import { AUTH_FAILURE_HINT } from "../../satellite/src/run_outcome"; const REPO = path.resolve(import.meta.dir, "../.."); const evaluator = readFileSync(path.join(REPO, "satellite/src/evaluator.ts"), "utf8"); describe("infra failure prefixes stay in sync with the satellite", () => { test("every prefix still appears in the satellite's wording", () => { const satText = evaluator + "\n" + AUTH_FAILURE_HINT; for (const p of INFRA_EVAL_FAILURE_PREFIXES) expect(satText).toContain(p); }); test("auth hint, API retries, crash, hard timeout → infra", () => { expect(isInfraEvalFailure(AUTH_FAILURE_HINT)).toBe(true); expect(isInfraEvalFailure("API 错误,多次自动重试仍失败,请稍后重试或人工补充信息")).toBe(true); expect(isInfraEvalFailure("评估异常: spawn ENOENT")).toBe(true); expect(isInfraEvalFailure("评估超时,已自动终止(可补充信息后重试)")).toBe(true); }); test("real verdicts, including a salvaged timeout verdict → not infra", () => { expect(isInfraEvalFailure("工单缺少复现路径,无法定位入口")).toBe(false); expect(isInfraEvalFailure("按步骤操作未复现(评估超时前已给出结论)")).toBe(false); expect(isInfraEvalFailure(null)).toBe(false); }); }); describe("unattendedAction", () => { const base = { unattended: true, failureReason: null }; test("off → never acts", () => { expect(unattendedAction({ ...base, unattended: false, status: "pending_confirmation" })).toBeNull(); expect(unattendedAction({ ...base, unattended: undefined, status: "info_required" })).toBeNull(); }); test("plan drafted → execute it", () => { expect(unattendedAction({ ...base, status: "pending_confirmation" })).toBe("execute_plan"); }); test("needs info (real verdict) → fallback plan; infra failure → wait for a human", () => { expect(unattendedAction({ ...base, status: "info_required", failureReason: "信息不足" })).toBe("execute_fallback"); expect(unattendedAction({ ...base, status: "info_required", failureReason: AUTH_FAILURE_HINT })).toBeNull(); }); test("other statuses → nothing", () => { for (const s of ["initializing", "awaiting_fix", "fixing", "manual_review", "unreproducible"] as const) { expect(unattendedAction({ ...base, status: s })).toBeNull(); } }); }); describe("fallback plan", () => { test("carries the reason, tells the agent how to end, keeps ticket steps, no NEEDS_INPUT holes", () => { const plan = unattendedFallbackPlan({ lang: "zh", reproSteps: "1. 打开关注页\n2. 下拉刷新", failureReason: "工单缺少账号信息" }); expect(plan).toContain("工单缺少账号信息"); expect(plan).toContain("STATUS: UNREPRODUCIBLE"); expect(plan).toContain("1. 打开关注页"); expect(plan).not.toContain("NEEDS_INPUT"); }); test("english", () => { expect(unattendedFallbackPlan({ lang: "en", reproSteps: "", failureReason: "x" })).toContain("Unattended task"); }); }); describe("executeGate from-status", () => { const t = { status: "info_required" as const, executionPlan: null, createdViaKeyId: null }; test("info_required is only accepted when the caller says so", () => { expect(executeGate(t, { plan: "p" }).ok).toBe(false); expect(executeGate(t, { plan: "p", from: "info_required" }).ok).toBe(true); }); });