import { describe, expect, test } from "bun:test"; import { commandEnvelopeSchema } from "../host/command-envelope-schema"; import { internalHostCommandEnvelopeWithInstanceIdSchema } from "./host-command-envelope"; const inlineOpenEnvelope = { version: "1", instanceId: "instance_1", requestId: "request_1", idempotencyKey: "idempotency_1", command: { kind: "open", flow: { definition: { id: "inline-flow" }, theme: { css: ":root { color: rebeccapurple; }" }, }, flowHandleId: "handle_1", metadata: { tags: { source: "docs" } }, hideCloseButton: true, }, } as const; describe("internal host command envelope", () => { test("keeps inline commands outside the public envelope contract", () => { expect(commandEnvelopeSchema.safeParse(inlineOpenEnvelope).success).toBe( false, ); }); test("accepts an exact inline open envelope with an instance ID", () => { expect( internalHostCommandEnvelopeWithInstanceIdSchema.parse(inlineOpenEnvelope), ).toEqual(inlineOpenEnvelope); }); test("requires a non-empty instance ID", () => { const { instanceId: _instanceId, ...withoutInstanceId } = inlineOpenEnvelope; expect( internalHostCommandEnvelopeWithInstanceIdSchema.safeParse( withoutInstanceId, ).success, ).toBe(false); expect( internalHostCommandEnvelopeWithInstanceIdSchema.safeParse({ ...inlineOpenEnvelope, instanceId: " ", }).success, ).toBe(false); }); test("accepts handle-free inline open, prefetch, and prerender commands", () => { const { flowHandleId: _flowHandleId, ...commandWithoutHandle } = inlineOpenEnvelope.command; for (const kind of ["open", "prefetch", "prerender"] as const) { expect( internalHostCommandEnvelopeWithInstanceIdSchema.safeParse({ ...inlineOpenEnvelope, command: kind === "open" ? { ...commandWithoutHandle, kind } : { kind, flow: commandWithoutHandle.flow }, }).success, ).toBe(true); } }); test("rejects a blank inline flow handle ID", () => { expect( internalHostCommandEnvelopeWithInstanceIdSchema.safeParse({ ...inlineOpenEnvelope, command: { ...inlineOpenEnvelope.command, flowHandleId: " " }, }).success, ).toBe(false); }); });