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 the supported home modules and keeps the local vehicle fallback", () => { const config: PublicSiteConfig = { heroSlides: [ { id: "hero-1", title: "后台首页主视觉", kicker: "后台小标题", image: "https://cdn.example.test/hero.jpg", isActive: true, sortOrder: 0, }, ], vehicleOptions: [ { id: "vehicle-1", title: "舒适型用车", description: "适合家庭出行", image: "https://cdn.example.test/vehicle.jpg", isActive: true, sortOrder: 0, }, ], }; const data = normalizeSiteConfig(config, fallbackData); expect(data.heroSlides).toMatchObject([ { id: "hero-1", title: "后台首页主视觉", image: "https://cdn.example.test/hero.jpg", }, ]); expect(data.vehicleOptions).toMatchObject([ { id: "vehicle-1", title: "舒适型用车", description: "适合家庭出行", }, ]); expect(data.vehicleService.introTitle).toBe("旅游出行专属用车服务"); }); it("keeps the backend hero actionLabel when normalizing site config", () => { const config: PublicSiteConfig = { heroSlides: [ { id: "hero-api", title: "经典人文打卡线路", kicker: "经典人文", actionLabel: "查看线路", image: "https://cdn.example.test/hero.webp", targetType: null, targetValue: null, }, ], }; const data = normalizeSiteConfig(config, fallbackData); expect(data.heroSlides[0]).toMatchObject({ id: "hero-api", title: "经典人文打卡线路", kicker: "经典人文", actionLabel: "查看线路", image: "https://cdn.example.test/hero.webp", }); }); it("replaces root-relative API images with network fallbacks", () => { const config: PublicSiteConfig = { heroSlides: [ { id: "hero-local-path", title: "山野户外", image: "/assets/guizhou/wanfenglin.jpg", }, ], vehicleOptions: [ { id: "vehicle-local-path", title: "舒适型用车", description: "适合家庭出行", image: "/assets/guizhou/jiaxiu-tower.jpg", }, ], }; const data = normalizeSiteConfig(config, fallbackData); expect(data.heroSlides[0]?.image).toBe( "https://wanfenglin-guanwang.oss-cn-chengdu.aliyuncs.com/uploads/20240718/23ea4291a9570b94a1f8df9a012c18ec.jpg", ); expect(data.vehicleOptions[0]?.image).toBe( "https://dimg04.c-ctrip.com/images/1me6m12000rntioi4FFB2.jpg?proc=source%2Ftripcommunity", ); }); it("returns a stable submit error message", () => { expect(getSubmitErrorMessage(new Error("网络异常"))).toBe("网络异常"); expect(getSubmitErrorMessage("unknown")).toBe("提交失败,请稍后再试。"); }); });