import { describe, expect, test } from "bun:test"; import { toCoreCommandPayload } from "./public-core-command"; describe("toCoreCommandPayload", () => { test("strips the host-only open container field", () => { expect( toCoreCommandPayload({ kind: "open", flowId: "flow_1", flowHandleId: "handle_1", container: null, hideCloseButton: true, metadata: { tags: { journey: "checkout" } }, }), ).toEqual({ kind: "open", flowId: "flow_1", flowHandleId: "handle_1", hideCloseButton: true, metadata: { tags: { journey: "checkout" } }, }); }); test("maps host context and signal commands to core kinds", () => { expect( toCoreCommandPayload({ kind: "updateHostContext", context: { url: "app://checkout?step=payment", env: { locale: "en-AU" }, storage: { campaign: "winter" }, page: { path: "/checkout", referrer: "app://cart", search: "?step=payment", }, }, }), ).toEqual({ kind: "hostContextUpdated", context: { url: "app://checkout?step=payment", env: { locale: "en-AU" }, storage: { campaign: "winter" }, page: { path: "/checkout", referrer: "app://cart", search: "?step=payment", }, }, }); expect( toCoreCommandPayload({ kind: "emitHostSignal", name: "screenFocused", data: { path: "/checkout" }, }), ).toEqual({ kind: "hostSignal", name: "screenFocused", data: { path: "/checkout" }, }); }); test("returns null for host-owned container commands", () => { expect( toCoreCommandPayload({ kind: "setContainer", flowHandleId: "handle_1", container: null, }), ).toBeNull(); expect( toCoreCommandPayload({ kind: "setDefaultContainerPolicy", policy: { kind: "floating" }, }), ).toBeNull(); }); test("preserves core-compatible commands", () => { const commands = [ { kind: "prefetch", flowId: "flow_1", flowHandleId: "handle_1" }, { kind: "prerender", flowId: "flow_1", flowHandleId: "handle_1", hideCloseButton: true, }, { kind: "identify", traits: { plan: "pro" } }, { kind: "track", origin: "customer", event: "Checkout Started" }, { kind: "track", origin: "customer", type: "screen", event: "Settings Opened", }, { kind: "configure", opts: { colorScheme: "dark" } }, { kind: "init", opts: { apiKey: "pk_test" } }, { kind: "close", flowHandleId: "handle_1" }, { kind: "reset" }, { kind: "instanceFeedback.capture", payload: { category: "helpfulness", target: { collection: "docs-page", id: "page_1" }, value: true, }, }, { kind: "instanceFeedback.listMine", payload: { selectors: [{ category: "helpfulness" }] }, }, ] as const; for (const command of commands) { expect(toCoreCommandPayload(command)).toEqual(command); } }); });