import assert from "node:assert/strict"; import test from "node:test"; import { projectOfferingPreviewFields } from "../src/features/services/offering-preview-projection.ts"; const baseItem = { name: "原名称", summary: "原简介", priceText: "原价格", coverUrl: "/media/original-cover.jpg", presentation: { categoryKey: "portfolio", categoryLabel: "品牌视觉", heroImage: "/media/original-hero.jpg", keywords: ["原始"], media: [], details: { background: "原始背景", designConcept: "", crafts: [], materialsList: [], }, }, }; test("filled name and coverUrl override originals", () => { const result = projectOfferingPreviewFields({ item: baseItem, config: {}, formValues: { name: "新名称", coverUrl: "/media/new-cover.jpg", summary: "", priceText: "", }, }); assert.equal(result.name, "新名称"); assert.equal(result.coverUrl, "/media/new-cover.jpg"); // summary/priceText 空字符串应覆盖原值(允许清空)。 assert.equal(result.summary, ""); assert.equal(result.priceText, ""); }); test("empty name and coverUrl preserve originals", () => { const result = projectOfferingPreviewFields({ item: baseItem, config: {}, formValues: { name: " ", coverUrl: "", summary: "新简介", }, }); assert.equal(result.name, "原名称"); assert.equal(result.coverUrl, "/media/original-cover.jpg"); assert.equal(result.summary, "新简介"); }); test("presentation merges base config and nested presentation", () => { const result = projectOfferingPreviewFields({ item: baseItem, config: { area: "杭州", series: "品牌焕新", spaceType: "线上与线下", presentation: { categoryLabel: "新分类" }, keywords: ["新关键词"], }, formValues: {}, }); assert.equal(result.presentation.area, "杭州"); assert.equal(result.presentation.series, "品牌焕新"); assert.equal(result.presentation.spaceType, "线上与线下"); assert.equal(result.presentation.categoryLabel, "新分类"); assert.deepEqual(result.presentation.keywords, ["新关键词"]); // categoryKey 不被覆盖时保留原值。 assert.equal(result.presentation.categoryKey, "portfolio"); }); test("heroImage follows detailHero, then coverUrl, then original", () => { const hero = projectOfferingPreviewFields({ item: baseItem, config: { detailHero: "/media/detail-hero.jpg" }, formValues: { coverUrl: "/media/new-cover.jpg" }, }); assert.equal(hero.presentation.heroImage, "/media/detail-hero.jpg"); const fallbackCover = projectOfferingPreviewFields({ item: baseItem, config: { detailHero: "" }, formValues: { coverUrl: "/media/new-cover.jpg" }, }); assert.equal(fallbackCover.presentation.heroImage, "/media/new-cover.jpg"); const fallbackOriginal = projectOfferingPreviewFields({ item: baseItem, config: { detailHero: "" }, formValues: { coverUrl: "" }, }); assert.equal(fallbackOriginal.presentation.heroImage, "/media/original-cover.jpg"); }); test("details merge preserves originals and applies updates", () => { const result = projectOfferingPreviewFields({ item: baseItem, config: { details: { background: "更新后的背景", designConcept: "新的设计理念", crafts: ["新交付"], materialsList: [{ name: "策略", spec: "定位" }], }, }, formValues: {}, }); assert.equal(result.presentation.details.background, "更新后的背景"); assert.equal(result.presentation.details.designConcept, "新的设计理念"); assert.deepEqual(result.presentation.details.crafts, ["新交付"]); assert.deepEqual(result.presentation.details.materialsList, [{ name: "策略", spec: "定位" }]); }); test("non-object config.presentation and details are ignored safely", () => { const result = projectOfferingPreviewFields({ item: baseItem, config: { presentation: "invalid", details: 42, }, formValues: {}, }); assert.deepEqual(result.presentation.keywords, ["原始"]); assert.equal(result.presentation.details.background, "原始背景"); assert.deepEqual(result.presentation.details.crafts, []); });