import { describe, expect, test } from "bun:test"; import { eventCommandSettledSchema } from "./core-host-command-settlement.js"; import { createGeneration } from "./v3-generation.js"; const messageBase = { t: "getuserfeedback:event:commandSettled", gen: createGeneration(3), requestId: "request-a", instanceId: "instance-a", }; describe("core host command settlement", () => { test("accepts a generic success message", () => { expect( eventCommandSettledSchema.safeParse({ ...messageBase, kind: "close", ok: true, result: { closed: true }, }).success, ).toBe(true); }); test("accepts an open success with a flow run result", () => { expect( eventCommandSettledSchema.safeParse({ ...messageBase, kind: "open", ok: true, flowKey: "persisted:core-owned", result: { flowHandleId: "handle-a", flowRunId: "run-a", }, }).success, ).toBe(true); }); test("accepts a prefetch success with a flow handle result", () => { expect( eventCommandSettledSchema.safeParse({ ...messageBase, kind: "prefetch", ok: true, flowKey: "persisted:core-owned", result: { flowHandleId: "handle-a" }, }).success, ).toBe(true); }); test("accepts Core-owned flow-key affinity on allocation results", () => { expect( eventCommandSettledSchema.safeParse({ ...messageBase, kind: "open", ok: true, flowKey: "inline:core-owned", result: { flowHandleId: "handle-a", flowRunId: "run-a", }, }).success, ).toBe(true); }); test("accepts an allocation success without top-level flow-key affinity", () => { expect( eventCommandSettledSchema.safeParse({ ...messageBase, kind: "open", ok: true, result: { flowHandleId: "handle-a", flowRunId: "run-a", }, }).success, ).toBe(true); }); test("accepts a failure message", () => { expect( eventCommandSettledSchema.safeParse({ ...messageBase, kind: "open", ok: false, error: { message: "open failed", code: "OPEN_FAILED" }, }).success, ).toBe(true); }); test("rejects incomplete flow results", () => { expect( eventCommandSettledSchema.safeParse({ ...messageBase, kind: "open", ok: true, result: { flowHandleId: "handle-a" }, }).success, ).toBe(false); expect( eventCommandSettledSchema.safeParse({ ...messageBase, kind: "prefetch", ok: true, result: {}, }).success, ).toBe(false); }); test("rejects invalid generations", () => { expect( eventCommandSettledSchema.safeParse({ ...messageBase, gen: -1, kind: "close", ok: true, }).success, ).toBe(false); expect( eventCommandSettledSchema.safeParse({ ...messageBase, gen: 1.5, kind: "close", ok: true, }).success, ).toBe(false); }); });