import { test, expect, describe } from "bun:test"; import { parseReadableCallback } from "../src/readable_jobs"; /** * The callback descriptor is the version negotiation for readable-report * generation (see ReadableCallback in shared/protocol.ts), so what this parser * returns decides which contract the node honours: a descriptor means "answer * 202 and call back", null means "block and answer with the html, as we always * did". * * That makes null the safe answer, and these cases are mostly about insisting on * it. The dangerous direction is a node that goes async on a caller who is not * listening — an older control plane, or a malformed field from a newer one that * we half-understood. It would answer 202 to something that only reads `html`, * and the report would be generated, delivered nowhere, and reported as a * failure the retry could never fix. */ describe("parseReadableCallback", () => { test("a complete descriptor is accepted", () => { expect(parseReadableCallback({ runId: "r1", kind: "feature", targetId: "f1" })) .toEqual({ runId: "r1", kind: "feature", targetId: "f1" }); expect(parseReadableCallback({ runId: "r2", kind: "task", targetId: "t1" })) .toEqual({ runId: "r2", kind: "task", targetId: "t1" }); // prototype 是第四种 track,可读报告的媒体优先框架要求它也能走异步回调。 expect(parseReadableCallback({ runId: "r3", kind: "prototype", targetId: "p1" })) .toEqual({ runId: "r3", kind: "prototype", targetId: "p1" }); }); test("an absent callback means the caller wants the report inline", () => { // This is the old control plane: it sends the same body it always sent, with // no callback key at all, and must keep getting a synchronous report. expect(parseReadableCallback(undefined)).toBeNull(); expect(parseReadableCallback(null)).toBeNull(); }); test("a half-formed descriptor is treated as no descriptor", () => { // Answering slowly is always recoverable. Accepting work whose result has // nowhere to go is not. expect(parseReadableCallback({ kind: "feature", targetId: "f1" })).toBeNull(); expect(parseReadableCallback({ runId: "r1", targetId: "f1" })).toBeNull(); expect(parseReadableCallback({ runId: "r1", kind: "feature" })).toBeNull(); expect(parseReadableCallback({ runId: " ", kind: "feature", targetId: "f1" })).toBeNull(); expect(parseReadableCallback({ runId: "r1", kind: "security", targetId: "s1" })).toBeNull(); expect(parseReadableCallback("callback")).toBeNull(); expect(parseReadableCallback(42)).toBeNull(); }); test("extra fields from a newer control plane do not invalidate it", () => { // The reverse compatibility direction: a future control plane that adds a // field to the descriptor must not make this node fall back to blocking. expect(parseReadableCallback({ runId: "r1", kind: "task", targetId: "t1", deadlineMs: 900000 })) .toEqual({ runId: "r1", kind: "task", targetId: "t1" }); }); });