2026-05-25 19:36:44 +08:00

151 lines
5.0 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { FormValidationError, parseForm } from "@/shared/forms/validation";
import {
emojiPackFormSchema,
giftFormSchema,
resourceFormSchema,
resourceGrantFormSchema,
resourceGroupCreateFormSchema,
} 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,
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");
});
test("requires badge form for badge resources", () => {
const payload = parseForm(resourceFormSchema, {
animationUrl: "https://media.haiyihy.com/resource/badge.pag",
badgeForm: "strip",
enabled: true,
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(() =>
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);
});
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",
heatValue: "2",
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",
resourceId: "11",
subjectType: "resource",
targetUserId: "1001",
});
expect(payload.subjectType).toBe("resource");
});
});