import { describe, expect, it } from "bun:test"; import { MAX_DISPLAY_ATTENTION_COOLDOWN_MS } from "./display-attention-policy"; import { claimPendingFlowAssignmentsRequestSchema, flowAssignmentRecipientRequestSchema, flowAssignmentRecordSchema, markFlowAssignmentDismissedRequestSchema, markFlowAssignmentDismissedResponseSchema, updateFlowAssignmentLifecycleRequestSchema, } from "./flow-assignments"; describe("flow assignment request contracts", () => { it("parses dismissal commands and acknowledgements", () => { expect( markFlowAssignmentDismissedRequestSchema.parse({ flowAssignmentId: "flasn_1", }), ).toEqual({ flowAssignmentId: "flasn_1", identities: [] }); expect( markFlowAssignmentDismissedResponseSchema.parse({ dismissed: true }), ).toEqual({ dismissed: true }); }); it("accepts authored attention policy on assignment records", () => { const result = flowAssignmentRecordSchema.parse({ flowAssignmentId: "flasn_1", flowId: "sur_1", latestFlowVersionId: "flv_1", status: "held", assignedAt: "2026-08-03T00:00:00.000Z", attentionPolicy: { priority: "high", attentionMode: "concurrent", cooldown: { key: "category:onboarding", afterDisplayMs: MAX_DISPLAY_ATTENTION_COOLDOWN_MS, }, }, }); expect(result.attentionPolicy).toEqual({ priority: "high", attentionMode: "concurrent", cooldown: { key: "category:onboarding", afterDisplayMs: MAX_DISPLAY_ATTENTION_COOLDOWN_MS, }, }); }); it("rejects attention cooldowns beyond the supported duration", () => { expect(() => flowAssignmentRecordSchema.parse({ flowAssignmentId: "flasn_1", flowId: "sur_1", latestFlowVersionId: "flv_1", status: "held", assignedAt: "2026-08-03T00:00:00.000Z", attentionPolicy: { cooldown: { key: "flow:sur_1", afterDisplayMs: MAX_DISPLAY_ATTENTION_COOLDOWN_MS + 1, }, }, }), ).toThrow(); }); it("accepts runtime targeting context for listPending", () => { const result = flowAssignmentRecipientRequestSchema.parse({ identities: [{ type: "userId", value: "user_1" }], context: { flags: [{ key: "new_checkout", value: true }], capabilities: [{ key: "checkout.drawer", source: "host-app" }], page: { path: "/checkout", search: "?step=payment" }, }, }); expect(result.context?.flags?.[0]).toEqual({ key: "new_checkout", value: true, }); expect(result.context?.capabilities?.[0]).toEqual({ key: "checkout.drawer", source: "host-app", }); expect(result.context?.page).toEqual({ path: "/checkout", search: "?step=payment", }); }); it("accepts runtime targeting context for claimPending", () => { const result = claimPendingFlowAssignmentsRequestSchema.parse({ holdOwnerKey: "inst_1", context: { flags: [{ key: "pricing_test", value: "variant_a" }], capabilities: [{ key: "billing.portal" }], page: { path: "/billing" }, }, }); expect(result.identities).toEqual([]); expect(result.context?.flags?.[0]?.key).toBe("pricing_test"); expect(result.context?.capabilities?.[0]?.key).toBe("billing.portal"); expect(result.context?.page?.path).toBe("/billing"); }); it("accepts external ID identities for recipient continuity", () => { const result = claimPendingFlowAssignmentsRequestSchema.parse({ holdOwnerKey: "inst_1", identities: [ { type: "externalId.users.none.shopify_customer_id", value: "gid://shopify/Customer/123", }, ], }); expect(result.identities).toEqual([ { type: "externalId.users.none.shopify_customer_id", value: "gid://shopify/Customer/123", }, ]); }); it("rejects unsupported external ID identity namespaces", () => { expect(() => claimPendingFlowAssignmentsRequestSchema.parse({ holdOwnerKey: "inst_1", identities: [ { type: "externalId.accounts.none.shopify_customer_id", value: "gid://shopify/Customer/123", }, ], }), ).toThrow("externalId.users.none."); expect(() => claimPendingFlowAssignmentsRequestSchema.parse({ holdOwnerKey: "inst_1", identities: [ { type: "externalId.users.custom.shopify_customer_id", value: "gid://shopify/Customer/123", }, ], }), ).toThrow("externalId.users.none."); expect(() => claimPendingFlowAssignmentsRequestSchema.parse({ holdOwnerKey: "inst_1", identities: [ { type: "externalId.shopify_customer_id", value: "gid://shopify/Customer/123", }, ], }), ).toThrow("externalId.users.none."); }); it("accepts runtime targeting context for lifecycle updates", () => { const result = updateFlowAssignmentLifecycleRequestSchema.parse({ identities: [{ type: "userId", value: "user_1" }], flowAssignmentId: "flasn_1", flowVersionId: "flv_1", holdGroupToken: "fahold_1", context: { flags: [{ key: "new_checkout", value: true }], capabilities: [{ key: "checkout.drawer", source: "host-app" }], page: { path: "/checkout" }, }, }); expect(result.context?.flags?.[0]).toEqual({ key: "new_checkout", value: true, }); expect(result.context?.capabilities?.[0]).toEqual({ key: "checkout.drawer", source: "host-app", }); expect(result.context?.page?.path).toBe("/checkout"); }); });