import { describe, expect, test } from "vitest"; import { FormValidationError, parseForm } from "@/shared/forms/validation"; import { emojiPackFormSchema, giftFormSchema, resourceFormSchema, resourceGrantFormSchema, resourceGroupCreateFormSchema, resourceShopItemsFormSchema, } from "./schema.js"; describe("resource form schema", () => { test("allows profile card resources", () => { const payload = parseForm(resourceFormSchema, { animationUrl: "https://media.haiyihy.com/resource/profile-card.pag", badgeForm: "tile", enabled: true, metadataJson: '{"profile_card_layout":{"source_width":1136,"source_height":1680,"color_content_width":750,"content_top":117,"content_bottom":1666,"content_height":1550,"content_top_ratio":0.069643,"content_height_ratio":0.922619,"detect_version":1}}', name: "Profile Card", previewUrl: "https://media.haiyihy.com/resource/profile-card-cover.png", priceType: "free", resourceCode: "profile_card_gold", resourceType: "profile_card", walletAssetAmount: "", }); expect(payload.resourceType).toBe("profile_card"); expect(payload.metadataJson).toContain("profile_card_layout"); }); test("requires badge form for badge resources", () => { const payload = parseForm(resourceFormSchema, { animationUrl: "https://media.haiyihy.com/resource/badge.pag", badgeForm: "strip", badgeKind: "level", enabled: true, levelTrack: "wealth", name: "VIP Badge", previewUrl: "https://media.haiyihy.com/resource/badge.png", priceType: "free", resourceCode: "badge_vip", resourceType: "badge", walletAssetAmount: "", }); expect(payload.badgeForm).toBe("strip"); expect(payload.badgeKind).toBe("level"); expect(payload.levelTrack).toBe("wealth"); expect(() => parseForm(resourceFormSchema, { animationUrl: "https://media.haiyihy.com/resource/badge.pag", enabled: true, name: "VIP Badge", previewUrl: "https://media.haiyihy.com/resource/badge.png", priceType: "free", resourceCode: "badge_vip", resourceType: "badge", walletAssetAmount: "", }), ).toThrow(FormValidationError); expect(() => parseForm(resourceFormSchema, { animationUrl: "https://media.haiyihy.com/resource/badge.pag", badgeForm: "strip", badgeKind: "level", enabled: true, name: "VIP Badge", previewUrl: "https://media.haiyihy.com/resource/badge.png", priceType: "free", resourceCode: "badge_vip_level", resourceType: "badge", walletAssetAmount: "", }), ).toThrow(FormValidationError); }); test("allows resource group resource items without wallet asset type", () => { const payload = parseForm(resourceGroupCreateFormSchema, { description: "", enabled: true, groupCode: "starter_pack", items: [ { durationDays: "7", itemType: "resource", resourceId: "11", sortOrder: "0", walletAssetAmount: "", walletAssetType: "", }, ], name: "Starter Pack", sortOrder: "0", }); expect(payload.items[0].walletAssetType).toBeUndefined(); }); test("rejects invalid wallet asset type with Chinese message", () => { expect(() => parseForm(resourceGroupCreateFormSchema, { description: "", enabled: true, groupCode: "starter_pack", items: [ { durationDays: "", itemType: "wallet_asset", resourceId: "", sortOrder: "0", walletAssetAmount: "100", walletAssetType: "gold", }, ], name: "Starter Pack", sortOrder: "0", }), ).toThrow(FormValidationError); }); test("validates editable gift form fields", () => { const payload = parseForm(giftFormSchema, { chargeAssetType: "COIN", coinPrice: "10", effectTypes: ["animation"], effectiveFrom: "", effectiveTo: "", enabled: true, giftId: "rose", giftPointAmount: "10", giftTypeCode: "normal", name: "Rose", presentationJson: "{}", priceVersion: "default", regionIds: ["0", "1001"], resourceId: "11", sortOrder: "0", }); expect(payload.regionIds).toEqual(["0", "1001"]); }); test("validates emoji pack category and pricing type", () => { const payload = parseForm(emojiPackFormSchema, { animationUrl: "https://media.haiyihy.com/emoji/wave.svga", category: "热门", coverUrl: "https://media.haiyihy.com/emoji/wave.png", name: "Wave", pricingType: "paid", regionIds: ["0"], sortOrder: "0", }); expect(payload.category).toBe("热门"); expect(payload.pricingType).toBe("paid"); }); test("validates resource grant form fields", () => { const payload = parseForm(resourceGrantFormSchema, { durationDays: "7", quantity: "1", reason: "manual", resourceIds: ["11", "12"], subjectType: "resource", targetUserId: "1001", }); expect(payload.subjectType).toBe("resource"); expect(payload.resourceIds).toEqual(["11", "12"]); }); test("validates resource shop durations", () => { const payload = parseForm(resourceShopItemsFormSchema, { items: [ { durationDays: "3", effectiveFrom: "", effectiveTo: "", resourceId: "11", sortOrder: "0", }, ], }); expect(payload.items[0].durationDays).toBe("3"); expect(() => parseForm(resourceShopItemsFormSchema, { items: [ { durationDays: "2", effectiveFrom: "", effectiveTo: "", resourceId: "11", sortOrder: "0", }, ], }), ).toThrow(FormValidationError); }); });