Files
persionalBrand/apps/admin/test/presentation-bindings.test.mjs

225 lines
7.1 KiB
JavaScript

import assert from "node:assert/strict";
import {
applyPresentationValues,
applyBrandPreviewValues,
applyStandardEntityValues,
filterSectionsForPreviewTarget,
presentationFieldName,
readPresentationValue,
} from "../src/features/brand/presentation-bindings.ts";
import {
resolvePreviewSelection,
previewTrustRecordId,
} from "../src/features/brand/preview-selection.ts";
const config = {
founder: "阿成",
visualPresentation: {
home: { worksTitle: "原标题", preserveMe: "保留" },
inquiry: { subtitle: "原说明" },
},
unrelatedSetting: true,
};
const sections = [
{
key: "home",
fields: [
{ key: "founder", binding: "brand.config.founder" },
{ key: "worksTitle", binding: "brand.config.visualPresentation.home.worksTitle" },
],
},
];
const next = applyPresentationValues(config, sections, {
[presentationFieldName("home", "founder")]: "阿成主理人",
[presentationFieldName("home", "worksTitle")]: "今日套餐",
});
assert.equal(readPresentationValue(next, "brand.config.founder"), "阿成主理人");
assert.equal(
readPresentationValue(next, "brand.config.visualPresentation.home.worksTitle"),
"今日套餐",
);
assert.equal(next.visualPresentation.home.preserveMe, "保留");
assert.equal(next.visualPresentation.inquiry.subtitle, "原说明");
assert.equal(next.unrelatedSetting, true);
assert.equal(config.founder, "阿成");
const previewSections = [
{
key: "home",
fields: [
{ key: "location", previewTarget: "home.location" },
{ key: "hero", previewTarget: "home.hero" },
],
},
{ key: "profile", fields: [{ key: "bio", previewTarget: "profile.bio" }] },
];
assert.deepEqual(filterSectionsForPreviewTarget(previewSections, "home.hero"), [
{ key: "home", fields: [{ key: "hero", previewTarget: "home.hero" }] },
]);
assert.equal(filterSectionsForPreviewTarget(previewSections).length, 2);
const unsafe = applyPresentationValues(
{ founder: "原主理人" },
[
{
key: "unsafe",
fields: [
{ key: "tenant", binding: "brand.id" },
{ key: "prototype", binding: "brand.config.__proto__.polluted" },
],
},
],
{
[presentationFieldName("unsafe", "tenant")]: "other-brand",
[presentationFieldName("unsafe", "prototype")]: "yes",
},
);
assert.deepEqual(unsafe, { founder: "原主理人" });
assert.equal({}.polluted, undefined);
const listFields = [
{
key: "details",
fields: [
{ key: "coverage", binding: "brand.config.profilePresentation.coverage", input: "list" },
{
key: "warranty",
binding: "brand.config.profilePresentation.warranty",
input: "list",
itemValueKey: "value",
},
],
},
];
const existingLists = {
profilePresentation: {
coverage: [{ title: "原区域", content: "原说明" }],
warranty: ["原承诺"],
other: "保留",
},
};
assert.deepEqual(
readPresentationValue(
existingLists,
"brand.config.profilePresentation.warranty",
listFields[0].fields[1],
),
[{ value: "原承诺" }],
);
const updatedLists = applyPresentationValues(existingLists, listFields, {
[presentationFieldName("details", "coverage")]: [{ title: "新区域", content: "新说明" }],
[presentationFieldName("details", "warranty")]: [{ value: "新承诺" }, { value: "" }],
});
assert.deepEqual(updatedLists.profilePresentation.coverage, [
{ title: "新区域", content: "新说明" },
]);
assert.deepEqual(updatedLists.profilePresentation.warranty, ["新承诺"]);
assert.equal(updatedLists.profilePresentation.other, "保留");
assert.deepEqual(existingLists.profilePresentation.warranty, ["原承诺"]);
const offering = applyPresentationValues(
{ details: { background: "旧背景", preserve: "保留" } },
[
{
key: "offering",
fields: [{ key: "background", binding: "offering.config.details.background" }],
},
],
{ [presentationFieldName("offering", "background")]: "新背景" },
);
assert.deepEqual(offering.details, { background: "新背景", preserve: "保留" });
const brandDraft = applyBrandPreviewValues(
{ description: "旧简介", address: "旧地址", config: { visualPresentation: {} } },
{ visualPresentation: {} },
[
{
key: "identity",
fields: [
{ key: "description", binding: "brand.description" },
{ key: "address", binding: "brand.address" },
{ key: "headline", binding: "brand.config.visualPresentation.home.title" },
],
},
],
{
[presentationFieldName("identity", "description")]: "新简介",
[presentationFieldName("identity", "address")]: "新地址",
[presentationFieldName("identity", "headline")]: "新标题",
},
);
assert.equal(brandDraft.description, "新简介");
assert.equal(brandDraft.address, "新地址");
assert.equal(brandDraft.config.visualPresentation.home.title, "新标题");
const entitySection = [
{
key: "card",
fields: [
{ key: "name", binding: "offering.name" },
{ key: "summary", binding: "offering.summary" },
{ key: "category", binding: "offering.config.categoryLabel" },
],
},
];
const updatedEntity = applyStandardEntityValues(
{ id: "one", name: "旧名称", summary: "旧简介", config: { categoryLabel: "分类" } },
"offering",
entitySection,
{ "card:name": "新名称", "card:summary": "新简介", "card:category": "新分类" },
);
assert.equal(updatedEntity.name, "新名称");
assert.equal(updatedEntity.summary, "新简介");
assert.equal(updatedEntity.id, "one");
assert.deepEqual(updatedEntity.config, { categoryLabel: "分类" });
assert.equal(readPresentationValue({}, "offering.name", undefined, updatedEntity), "新名称");
const targets = [
{ key: "brand.hero", label: "主视觉", source: "brand", page: "home" },
{ key: "offering.card", label: "餐品", source: "offering", page: "offerings" },
];
assert.deepEqual(resolvePreviewSelection("brand.hero", undefined, targets, "home"), {
targetKey: "brand.hero",
});
assert.deepEqual(resolvePreviewSelection("offering.card", "one", targets, "offerings"), {
targetKey: "offering.card",
recordId: "one",
});
assert.deepEqual(resolvePreviewSelection("offering.card", undefined, targets, "offerings"), {
targetKey: "offering.card",
});
assert.equal(resolvePreviewSelection("brand.hero", "one", targets, "home"), undefined);
assert.equal(resolvePreviewSelection("brand.hero", undefined, targets, "offerings"), undefined);
// 未挂载的同源编辑入口不能清空已有字段。
assert.deepEqual(
applyPresentationValues(
{ founder: "保留姓名", title: "保留职位" },
[
{
key: "identity",
fields: [
{ key: "founder", binding: "brand.config.founder" },
{ key: "title", binding: "brand.config.title" },
],
},
],
{ "identity:founder": "修改姓名" },
),
{ founder: "修改姓名", title: "保留职位" },
);
assert.equal(
previewTrustRecordId(
[
{ id: "plain", type: "credential", config: {} },
{ id: "structured", type: "credential", config: { stats: [] } },
],
"credential",
),
"structured",
);
assert.equal(previewTrustRecordId([], "credential"), undefined);