// MUST be first: sets DATABASE_URL before db_client is evaluated. import "./setup_env"; import { test, expect, describe } from "bun:test"; import { readableCallbackVerdict } from "../src/readable_report"; /** * Readable-report generation is dispatched to a node and answered later (see * ReadableCallback in shared/protocol.ts). That opens a window in which the * control plane can stop waiting — the reaper times the run out, the retry sweep * starts a new one — while the original node is still working and will * eventually call back anyway. * * This verdict is what stands between that window and a corrupted report. The * cases below are the ones that actually happen in a mixed, restarting fleet, * not exhaustive input validation. */ describe("readableCallbackVerdict", () => { const running = { targetType: "feature", targetId: "f1", status: "running" }; test("the run we are still waiting on is accepted", () => { expect(readableCallbackVerdict(running, { kind: "feature", targetId: "f1" })) .toEqual({ accept: true }); }); test("a callback for a run the reaper already gave up on is dropped", () => { // The node restarted, the reaper marked the run failed, a retry re-dispatched // it — and only then did the original node finish and call back. Its html is // older than whatever the retry produced, so it must not land. const reaped = { ...running, status: "error" }; expect(readableCallbackVerdict(reaped, { kind: "feature", targetId: "f1" })) .toEqual({ accept: false, ignored: "run already error" }); }); test("a duplicate callback for an already-finished run is dropped", () => { const done = { ...running, status: "done" }; expect(readableCallbackVerdict(done, { kind: "feature", targetId: "f1" })) .toEqual({ accept: false, ignored: "run already done" }); }); test("a run id that no longer exists is dropped, not persisted blind", () => { // Anything else would let a node name a target and have the report written // there on nothing but its say-so. expect(readableCallbackVerdict(null, { kind: "feature", targetId: "f1" })) .toEqual({ accept: false, ignored: "unknown run" }); }); test("a run id belonging to a different target is dropped", () => { expect(readableCallbackVerdict(running, { kind: "feature", targetId: "f2" })) .toEqual({ accept: false, ignored: "run does not belong to this target" }); // Same id, wrong table: task and feature ids are separate namespaces, so a // collision is possible and must not write a feature report onto a bug. expect(readableCallbackVerdict(running, { kind: "task", targetId: "f1" })) .toEqual({ accept: false, ignored: "run does not belong to this target" }); }); // Prototype 是第四条走可读性报告的线(media-first,见 docs/prototype.md)。它的 // 回调 kind 是 "prototype",必须和自己那一批 run 对齐、也不能被 task/feature 的 // run 冒领 —— 否则 media-first 正文会被写到别的目标上。 test("a prototype callback matching its own running run is accepted", () => { const run = { targetType: "prototype", targetId: "p1", status: "running" }; expect(readableCallbackVerdict(run, { kind: "prototype", targetId: "p1" })) .toEqual({ accept: true }); }); test("a prototype callback is not accepted against a feature run of the same id", () => { expect(readableCallbackVerdict(running, { kind: "prototype", targetId: "f1" })) .toEqual({ accept: false, ignored: "run does not belong to this target" }); }); });