import { describe, expect, it } from "bun:test"; import { toHostActionExecuteLinkParams, toHostActionExecuteParams, toHostActionSettlementAvailabilityResult, } from "./host-action-rpc"; const flowActionSucceededSeed = { flowId: "flow-1", flowVersionId: "flow-version-1", flowVersionNumber: 3, flowRunId: "flow-run-1", actionPlacementId: "action-placement-1", actionKind: "runtime-action" as const, }; const params = { viewId: "view-1", generation: 3, instanceId: "instance-1", invocationId: "invocation-1", definition: { key: "enable-feature", version: 1 }, flowActionSucceededSeed, }; describe("host action RPC", () => { it("parses an execution and copies trusted success provenance", () => { const parsed = toHostActionExecuteParams(params); expect(parsed).toEqual(params); expect(parsed?.flowActionSucceededSeed).not.toBe(flowActionSucceededSeed); expect(Object.isFrozen(parsed?.flowActionSucceededSeed)).toBe(true); }); it("drops provenance when invocation identity is absent", () => { const { invocationId: _invocationId, ...legacyParams } = params; expect(toHostActionExecuteParams(legacyParams)).toEqual({ viewId: legacyParams.viewId, generation: legacyParams.generation, instanceId: legacyParams.instanceId, definition: legacyParams.definition, }); }); it("rejects malformed action identity", () => { for (const candidate of [ null, { ...params, viewId: " " }, { ...params, generation: 1.5 }, { ...params, invocationId: "" }, { ...params, definition: { key: "", version: 1 } }, { ...params, definition: { key: "enable-feature", version: 0 } }, ]) { expect(toHostActionExecuteParams(candidate)).toBeNull(); } }); it("parses only HTTP links with a positive settlement deadline", () => { const linkParams = { ...params, settlementDeadline: 20_000, url: "https://example.com/settings", }; expect(toHostActionExecuteLinkParams(linkParams)).toEqual(linkParams); expect( toHostActionExecuteLinkParams({ ...linkParams, url: "javascript:alert(1)", }), ).toBeNull(); expect( toHostActionExecuteLinkParams({ ...linkParams, settlementDeadline: 0, }), ).toBeNull(); }); it("parses the exact host settlement availability proof", () => { expect( toHostActionSettlementAvailabilityResult({ remain: true, close: true, link: false, }), ).toEqual({ remain: true, close: true, link: false }); expect( toHostActionSettlementAvailabilityResult({ remain: false, close: true, link: false, }), ).toBeNull(); }); });