import { describe, expect, it } from "vitest"; import { UnlockPrivateResponseSchema } from "@/data/schemas/chat"; import type { PendingChatPromotion } from "@/data/storage/navigation"; import { appendPromotionMessage, applyPromotionUnlockOutput, createChatPromotionState, } from "@/stores/chat/helper/promotion"; const promotion: PendingChatPromotion = { characterId: "elio", promotionType: "image", lockType: "image_paywall", clientLockId: "promotion-1", createdAt: 1, }; describe("chat promotion", () => { it("creates the requested locked message and appends it at the tail", () => { const state = createChatPromotionState(promotion); const messages = appendPromotionMessage( [ { displayId: "history-1", content: "History", isFromAI: true, date: "2026-07-13", }, ], state, ); expect(messages.map((message) => message.displayId)).toEqual([ "history-1", "promotion:promotion-1", ]); expect(messages[1]).toMatchObject({ locked: true, lockReason: "image_paywall", imagePaywalled: true, }); }); it("replaces a temporary promotion with the real unlocked image", () => { const state = createChatPromotionState(promotion); const next = applyPromotionUnlockOutput(state, { displayMessageId: "promotion:promotion-1", request: { displayMessageId: "promotion:promotion-1", lockType: "image_paywall", clientLockId: "promotion-1", }, response: UnlockPrivateResponseSchema.parse({ unlocked: true, messageId: "backend-1", image: { type: "promotion", url: "https://example.com/unlocked.jpg", }, }), }); expect(next?.message).toMatchObject({ displayId: "promotion:promotion-1", remoteId: "backend-1", imageUrl: "https://example.com/unlocked.jpg", imagePaywalled: false, locked: false, }); }); it("keeps the promotion last and removes matching history duplicates", () => { const state = createChatPromotionState(promotion, "backend-1"); const userMessage = { displayId: "server:backend-1:user", remoteId: "backend-1", content: "User message with the shared remote id", isFromAI: false, date: "2026-07-13", }; const messages = appendPromotionMessage( [ userMessage, { displayId: "server:backend-1:assistant", remoteId: "backend-1", content: "Stale history copy", isFromAI: true, date: "2026-07-13", }, ], state, ); expect(messages).toEqual([userMessage, state.message]); }); });