import { describe, expect, it } from "bun:test"; import { coreCommandPayloadSchema } from "./core-commands"; import { parseInitOptions } from "./widget-commands"; describe("core inline flow commands", () => { const flow = { definition: { blocks: [] }, theme: { css: ":root {}", themeRuntimeArtifactId: "artifact_123", themeVersionId: "theme_123", }, }; it.each([ { kind: "open", flow, flowHandleId: "handle_123", metadata: { tags: { source: "protocol-test" } }, hideCloseButton: true, }, { kind: "prefetch", flow, flowHandleId: "handle_123" }, { kind: "prerender", flow, flowHandleId: "handle_123", hideCloseButton: true, }, ] as const)("parses $kind commands with inline flows", (command) => { expect(coreCommandPayloadSchema.parse(command)).toEqual(command); }); it("rejects undefined inline definitions", () => { expect( coreCommandPayloadSchema.safeParse({ kind: "open", flow: { definition: undefined }, }).success, ).toBe(false); }); it("rejects unknown inline flow fields", () => { expect( coreCommandPayloadSchema.safeParse({ kind: "open", flow: { definition: { blocks: [] }, unknown: true, }, }).success, ).toBe(false); }); }); describe("Core local registration init", () => { const localInit = { kind: "init" as const, opts: { registration: { kind: "supplied-only-local" as const, profile: "bundled-docs" as const, }, colorScheme: "dark" as const, }, }; it("accepts the exact local shape only in the Core command schema", () => { expect(coreCommandPayloadSchema.safeParse(localInit).success).toBe(true); expect(() => parseInitOptions(localInit.opts)).toThrow(); }); it("normalizes an omitted local color scheme to auto-detection", () => { const parsed = coreCommandPayloadSchema.parse({ kind: "init", opts: { registration: localInit.opts.registration }, }); expect(parsed).toMatchObject({ kind: "init", opts: { colorScheme: { autoDetectColorScheme: ["class", "data-theme"], }, }, }); }); it.each([ { apiKey: "app_should-not-be-here" }, { auth: { jwt: { token: "jwt_should-not-be-here" } } }, { runtimeEndpoints: { apiUrl: "https://api.example.test/v1" } }, { registration: { kind: "supplied-only-local", profile: "bundled-docs", }, consent: "granted", }, { registration: { kind: "supplied-only-local", profile: "bundled-docs", }, unknown: true, }, ] as const)("rejects local authority or unknown fields", (extra) => { expect( coreCommandPayloadSchema.safeParse({ ...localInit, opts: { ...localInit.opts, ...extra }, }).success, ).toBe(false); }); }); describe("Core customer occurrence commands", () => { it("preserves explicit page and screen types while keeping omission optional", () => { const legacyTrack = coreCommandPayloadSchema.parse({ kind: "track", origin: "customer", event: "Checkout Started", }); expect(legacyTrack).not.toHaveProperty("type"); for (const type of ["page", "screen"] as const) { const parsed = coreCommandPayloadSchema.parse({ kind: "track", origin: "customer", type, event: "Product Viewed", properties: { plan: "starter" }, }); expect(parsed).toMatchObject({ kind: "track", origin: "customer", type }); } expect( coreCommandPayloadSchema.safeParse({ kind: "track", origin: "system", type: "page", event: "Flow Viewed", references: { surveyId: "sur_1" }, }).success, ).toBe(false); }); }); describe("Core persisted runtime init", () => { it("accepts runtime configuration only in the Core command schema", () => { const opts = { apiKey: "app_123", runtimeConfiguration: { auth: { jwt: { token: "jwt_123" } }, capabilities: ["feature-a"], consent: "granted" as const, }, }; expect( coreCommandPayloadSchema.safeParse({ kind: "init", opts }).success, ).toBe(true); expect(() => parseInitOptions(opts)).toThrow(); }); });