import { describe, expect, test } from "bun:test"; import { coreCommandPayloadSchema } from "../core-commands"; import { publicCommandPayloadSchema } from "../widget-commands"; import { internalHostCommandPayloadSchema, internalHostInlineFlowCommandPayloadSchema, internalHostSuppliedOnlyLocalInitCommandPayloadSchema, toCoreInlineFlowCommandPayload, } from "./host-command-payload"; const inlineFlow = { definition: { id: "inline-flow", }, theme: { css: ":root { color: rebeccapurple; }", }, }; describe("internal host command payload", () => { test("accepts credential-free local init only through the internal host contract", () => { const command = { kind: "init", opts: { registration: { kind: "supplied-only-local", profile: "bundled-docs", }, colorScheme: "dark", }, runtimeEndpoints: { coreUrl: "https://cdn.example.com/widget/core/v3/core.html", }, } as const; expect( internalHostSuppliedOnlyLocalInitCommandPayloadSchema.parse(command), ).toEqual(command); expect(internalHostCommandPayloadSchema.parse(command)).toEqual(command); expect(publicCommandPayloadSchema.safeParse(command).success).toBe(false); expect(coreCommandPayloadSchema.safeParse(command).success).toBe(false); }); test("rejects authority and extra transport fields on local init", () => { expect( internalHostCommandPayloadSchema.safeParse({ kind: "init", opts: { apiKey: "pk_customer", registration: { kind: "supplied-only-local", profile: "bundled-docs", }, }, runtimeEndpoints: { coreUrl: "https://cdn.example.com/widget/core/v3/core.html", }, }).success, ).toBe(false); expect( internalHostCommandPayloadSchema.safeParse({ kind: "init", opts: { registration: { kind: "supplied-only-local", profile: "bundled-docs", }, }, runtimeEndpoints: { apiUrl: "https://api.example.com/v1", coreUrl: "https://cdn.example.com/widget/core/v3/core.html", }, }).success, ).toBe(false); }); test("accepts exact inline open, prefetch, and prerender variants", () => { const commands = [ { kind: "open", flow: inlineFlow, flowHandleId: "handle_open", metadata: { tags: { source: "docs" } }, hideCloseButton: true, }, { kind: "prefetch", flow: inlineFlow, flowHandleId: "handle_prefetch", }, { kind: "prerender", flow: inlineFlow, flowHandleId: "handle_prerender", hideCloseButton: false, }, ] as const; for (const command of commands) { expect(internalHostInlineFlowCommandPayloadSchema.parse(command)).toEqual( command, ); expect(internalHostCommandPayloadSchema.parse(command)).toEqual(command); expect(coreCommandPayloadSchema.parse(command)).toEqual(command); } }); test("keeps public host commands accepted by the internal parser", () => { const command = { kind: "open", flowId: "flow_1", flowHandleId: "handle_1", }; expect(internalHostCommandPayloadSchema.parse(command)).toEqual(command); }); test("keeps supplied open containers host-only", () => { const command = { kind: "open", flow: inlineFlow, container: null, } as const; expect(internalHostCommandPayloadSchema.parse(command)).toEqual(command); expect(coreCommandPayloadSchema.safeParse(command).success).toBe(false); expect( coreCommandPayloadSchema.parse(toCoreInlineFlowCommandPayload(command)), ).toEqual({ kind: "open", flow: inlineFlow }); expect( internalHostCommandPayloadSchema.safeParse({ ...command, flowHandleId: "handle_1", }).success, ).toBe(false); expect( internalHostCommandPayloadSchema.safeParse({ kind: "open", flow: inlineFlow, flowHandleId: "handle_1", }).success, ).toBe(true); }); test("keeps inline flows outside the public command contract", () => { expect( publicCommandPayloadSchema.safeParse({ kind: "open", flow: inlineFlow, flowHandleId: "handle_1", }).success, ).toBe(false); }); test("allows the first inline allocation to omit its flow handle id", () => { for (const kind of ["open", "prefetch", "prerender"] as const) { expect( internalHostCommandPayloadSchema.safeParse({ kind, flow: inlineFlow, }).success, ).toBe(true); expect( internalHostCommandPayloadSchema.safeParse({ kind, flow: inlineFlow, flowHandleId: " ", }).success, ).toBe(false); } }); test("rejects commands containing both flowId and flow", () => { expect( internalHostCommandPayloadSchema.safeParse({ kind: "open", flowId: "flow_1", flow: inlineFlow, flowHandleId: "handle_1", }).success, ).toBe(false); }); test("rejects unknown fields on inline variants", () => { expect( internalHostCommandPayloadSchema.safeParse({ kind: "prerender", flow: inlineFlow, flowHandleId: "handle_1", container: null, }).success, ).toBe(false); }); });