// MUST be first: sets DATABASE_URL before db_client is evaluated. // task_execution.ts imports ./db transitively, and db_client throws at import // time on a missing DATABASE_URL — an assignment at the top of THIS file would // run too late, because ES imports hoist. import "./setup_env"; import { describe, expect, it } from "bun:test"; import { executeGate } from "../src/task_execution"; import type { Task, TaskStatus } from "../src/types"; type GateInput = Pick; const PLAN = "## 验证步骤\n1. 打开 App"; function task(over: Partial = {}): GateInput { return { status: "pending_confirmation", executionPlan: PLAN, createdViaKeyId: null, ...over }; } /** Everything except the one status that may be promoted. */ const NON_PROMOTABLE: TaskStatus[] = [ "initializing", "info_required", "awaiting_fix", "fixing", "manual_review", "fix_succeeded", "fix_failed", "unreproducible", "archived", "cancelled", "evaluate_finish", ]; describe("executeGate", () => { it("rejects a missing task", () => { expect(executeGate(null, {})).toEqual({ ok: false, reason: "not_found" }); }); it("promotes a pending_confirmation task using the stored plan", () => { expect(executeGate(task(), {})).toEqual({ ok: true, finalPlan: PLAN }); }); it("trims the stored plan", () => { expect(executeGate(task({ executionPlan: ` ${PLAN} ` }), {})).toEqual({ ok: true, finalPlan: PLAN }); }); describe("status gate", () => { for (const status of NON_PROMOTABLE) { it(`rejects ${status} and reports it back`, () => { expect(executeGate(task({ status }), {})).toEqual({ ok: false, reason: "wrong_status", status }); }); } }); describe("ownership gate", () => { it("passes when the key created the task", () => { const t = task({ createdViaKeyId: "opk_aaa" }); expect(executeGate(t, { requireCreatedViaKeyId: "opk_aaa" })).toEqual({ ok: true, finalPlan: PLAN }); }); it("rejects a task created by a different key", () => { const t = task({ createdViaKeyId: "opk_aaa" }); expect(executeGate(t, { requireCreatedViaKeyId: "opk_bbb" })).toEqual({ ok: false, reason: "not_owner" }); }); it("rejects a task with no open-platform provenance at all", () => { // A console-created task is nobody's to drive via the API. expect(executeGate(task(), { requireCreatedViaKeyId: "opk_aaa" })).toEqual({ ok: false, reason: "not_owner" }); }); // The leak guard. If a later refactor reorders these checks, a key that does // not own the task learns its lifecycle state from the wrong-status branch. it("reports not_owner — NOT wrong_status — for a foreign task in a bad state", () => { const t = task({ status: "fixing", createdViaKeyId: "opk_aaa" }); expect(executeGate(t, { requireCreatedViaKeyId: "opk_bbb" })).toEqual({ ok: false, reason: "not_owner" }); }); it("skips the ownership check entirely for the console path", () => { const t = task({ createdViaKeyId: "opk_aaa" }); expect(executeGate(t, {})).toEqual({ ok: true, finalPlan: PLAN }); }); }); describe("plan gate", () => { for (const [name, executionPlan] of [["null", null], ["empty", ""], ["blank", " "]] as const) { it(`rejects a ${name} stored plan`, () => { expect(executeGate(task({ executionPlan }), {})).toEqual({ ok: false, reason: "empty_execution_plan" }); }); } it("prefers an explicitly passed plan over the stored one", () => { expect(executeGate(task(), { plan: "edited" })).toEqual({ ok: true, finalPlan: "edited" }); }); // `??` not `||`: an explicit blank is the caller saying "clear it", which is a // rejection — not a reason to silently fall back to the stored plan. it("rejects an explicitly blank plan instead of falling back", () => { expect(executeGate(task(), { plan: " " })).toEqual({ ok: false, reason: "empty_execution_plan" }); }); }); });