import { describe, expect, it } from "vitest"; import { fallbackData, getSubmitErrorMessage, normalizeSiteConfig } from "@/lib/data"; import type { PublicSiteConfig } from "@/lib/types"; describe("WonderQ shared data helpers", () => { it("uses local defaults when the site configuration is empty", () => { expect(normalizeSiteConfig({}, fallbackData)).toEqual(fallbackData); }); it("normalizes active site modules and ignores incomplete records", () => { const config: PublicSiteConfig = { destinationHero: [ { id: "hero-1", title: "后台主视觉标题", kicker: "后台小标题", image: "https://cdn.example.test/hero.jpg", isActive: true, sortOrder: 0, }, ], demandForm: [ { id: "form-1", destinationLabel: "想去哪里", submitLabel: "提交给管家", chips: [" 贵州 ", "", "黄果树"], isActive: true, }, ], }; const data = normalizeSiteConfig(config, fallbackData); expect(data.destinationHero).toMatchObject({ id: "hero-1", title: "后台主视觉标题", image: "https://cdn.example.test/hero.jpg", }); expect(data.demandForm).toMatchObject({ id: "form-1", destinationLabel: "想去哪里", submitLabel: "提交给管家", chips: ["贵州", "黄果树"], }); }); it("returns a stable submit error message", () => { expect(getSubmitErrorMessage(new Error("网络异常"))).toBe("网络异常"); expect(getSubmitErrorMessage("unknown")).toBe("提交失败,请稍后再试。"); }); it("normalizes the vehicle service module and falls back incomplete sections", () => { const config: PublicSiteConfig = { vehicleService: { id: "vehicle-service-1", introTitle: "旅游专属用车", intro: " 提交需求后由管家跟进。 ", serviceSections: [{ title: " 包车服务 ", description: " 灵活安排 " }], advantages: [" 车型齐全 ", ""], processSteps: [{ title: "提交需求", description: " 填写出行信息 " }], isActive: true, }, }; const data = normalizeSiteConfig(config, fallbackData); expect(data.vehicleService).toMatchObject({ id: "vehicle-service-1", introTitle: "旅游专属用车", intro: "提交需求后由管家跟进。", advantages: ["车型齐全"], }); expect(data.vehicleService.serviceSections).toEqual([ { title: "包车服务", description: "灵活安排" }, ]); expect(data.vehicleService.processSteps).toEqual([ { title: "提交需求", description: "填写出行信息" }, ]); }); });