fix: 收敛模板编辑发布契约并还原餐饮页面视觉

This commit is contained in:
que01 committed 2026-10-08 13:36:59 +08:00
1 parent 20bcb43534
commit f4061a24bd
102 files changed
+4510 -870

No files matched your search

@@ -6,6 +6,17 @@ import test from "node:test";
const packagesRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const runtimeRoot = path.resolve(packagesRoot, "../apps/miniapp");
const adminTrustEditor = fs.readFileSync(
path.resolve(packagesRoot, "../apps/admin/src/features/trust-content/TrustContentPage.tsx"),
"utf8",
);
for (const field of ["title", "summary", "content"]) {
assert.ok(
adminTrustEditor.includes(`selectedTypeMeta?.adminFields.${field}`),
`Admin trust editor must render the manifest label for ${field}`,
);
}
function sourceFile(templateRoot, relativePath, extension) {
const local = path.join(templateRoot, "miniapp", `${relativePath}.${extension}`);
@@ -19,6 +30,9 @@ function renderedFields(templateRoot, entrypoint) {
const heroFields = new Set();
const profileFields = new Set();
const trustFields = new Set();
const trustItemTypes = new Set();
const brandFields = new Set();
let rendersAllTrustItems = false;
function visit(relativePath) {
if (seen.has(relativePath)) return;
seen.add(relativePath);
@@ -29,6 +43,9 @@ function renderedFields(templateRoot, entrypoint) {
for (const match of content.matchAll(/(?<![.\w])presentation\.([A-Za-z][A-Za-z0-9]*)/g)) {
fields.add(match[1]);
}
for (const match of content.matchAll(/(?<![.\w])brand\.([A-Za-z][A-Za-z0-9]*)/g)) {
brandFields.add(match[1]);
}
for (const match of content.matchAll(/(?<![.\w])hero\.([A-Za-z][A-Za-z0-9]*)/g)) {
heroFields.add(match[1]);
}
@@ -38,6 +55,21 @@ function renderedFields(templateRoot, entrypoint) {
for (const match of content.matchAll(/trustContent\.([A-Za-z][A-Za-z0-9]*)/g)) {
trustFields.add(match[1]);
}
for (const match of content.matchAll(
/selectSlot\(items,\s*["']([A-Za-z][A-Za-z0-9_-]*)["']\)/g,
)) {
trustItemTypes.add(match[1]);
}
if (/trustItems/.test(content)) {
for (const match of content.matchAll(
/item\.type\s*===\s*["']([A-Za-z][A-Za-z0-9_-]*)["']/g,
)) {
trustItemTypes.add(match[1]);
}
if (/wx:for="{{trustItems}}"/.test(content) && !/item\.type\s*===/.test(content)) {
rendersAllTrustItems = true;
}
}
}
const componentConfig = sourceFile(templateRoot, relativePath, "json");
if (!componentConfig) return;
@@ -49,7 +81,16 @@ function renderedFields(templateRoot, entrypoint) {
}
}
visit(entrypoint);
return { fields, heroFields, profileFields, trustFields, seen };
return {
fields,
heroFields,
profileFields,
trustFields,
trustItemTypes,
brandFields,
rendersAllTrustItems,
seen,
};
}
const sharedOfferingBindings = [
@@ -64,6 +105,18 @@ const sharedOfferingBindings = [
"offering.config.details.crafts",
"offering.config.details.materialsList",
];
const offeringRuntimeBindings = {
"offering.config.categoryLabel": "item.presentation.categoryLabel",
"offering.config.detailHero": "item.detailHero",
"offering.config.area": "item.area",
"offering.config.series": "item.series",
"offering.config.spaceType": "item.spaceType",
"offering.config.keywords": "item.keywords",
"offering.config.details.background": "item.details.background",
"offering.config.details.designConcept": "item.details.designConcept",
"offering.config.details.crafts": "item.details.crafts",
"offering.config.details.materialsList": "item.details.materialsList",
};
for (const directory of fs
.readdirSync(packagesRoot)
@@ -73,31 +126,99 @@ for (const directory of fs
if (!fs.existsSync(manifestPath)) continue;
test(`${directory} exposes rendered content through Admin bindings`, () => {
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
for (const declaration of manifest.trustContentTypes ?? []) {
for (const field of ["title", "summary", "content"]) {
assert.ok(
declaration.adminFields?.[field]?.trim(),
`${manifest.packageKey}: trust type ${declaration.type} has no Admin label for ${field}`,
);
}
}
for (const sample of Object.values(manifest.sampleContent ?? {})) {
for (const item of sample.trustContent ?? []) {
assert.ok(
manifest.trustContentTypes?.some((type) => type.type === item.type),
`${manifest.packageKey}: sample trust type ${item.type} has no declared editor or renderer`,
);
}
}
for (const section of manifest.admin.sections) {
assert.ok(
manifest.entrypoints[section.page],
`${manifest.packageKey}: ${section.key} points to an unknown Mini Program page`,
);
}
const bindings = new Set(
manifest.admin.sections.flatMap((section) => section.fields.map((field) => field.binding)),
);
for (const [pageKey, entrypoint] of Object.entries(manifest.entrypoints)) {
const { fields, heroFields, profileFields, trustFields, seen } = renderedFields(
templateRoot,
entrypoint,
const {
fields,
heroFields,
profileFields,
trustFields,
trustItemTypes,
brandFields,
rendersAllTrustItems,
seen,
} = renderedFields(templateRoot, entrypoint);
const pageBindings = new Set(
manifest.admin.sections
.filter((section) => section.page === pageKey)
.flatMap((section) => section.fields.map((field) => field.binding)),
);
for (const field of fields) {
const binding = `brand.config.visualPresentation.${pageKey}.${field}`;
assert.ok(
bindings.has(`brand.config.visualPresentation.${pageKey}.${field}`),
`${manifest.packageKey}: ${pageKey}.${field} is rendered but has no Admin field`,
bindings.has(binding),
`${manifest.packageKey}: ${pageKey}.${field} has no Admin field`,
);
assert.ok(
pageBindings.has(binding),
`${manifest.packageKey}: ${pageKey}.${field} Admin field is assigned to another page`,
);
}
for (const field of heroFields) {
const binding =
field === "image" ? "brand.config.heroImage" : `brand.config.homeHero.${field}`;
assert.ok(
bindings.has(binding),
pageBindings.has(binding),
`${manifest.packageKey}: ${pageKey} hero.${field} has no Admin field`,
);
}
for (const declaration of manifest.admin.sections.filter((item) => item.page === pageKey)) {
for (const field of declaration.fields) {
const binding = field.binding;
if (binding === "brand.avatarUrl")
assert.ok(
brandFields.has("avatarUrl"),
`${manifest.packageKey}: avatar has no renderer`,
);
if (binding === "brand.description")
assert.ok(
brandFields.has("slogan") || brandFields.has("description"),
`${manifest.packageKey}: description has no renderer`,
);
if (binding === "brand.contactPhone")
assert.ok(
brandFields.has("phone") || brandFields.has("contactPhone"),
`${manifest.packageKey}: phone has no renderer`,
);
if (binding === "brand.contactWechat")
assert.ok(
brandFields.has("wechat") || brandFields.has("contactWechat"),
`${manifest.packageKey}: WeChat has no renderer`,
);
if (binding === "brand.address")
assert.ok(
brandFields.has("studioAddress") || brandFields.has("address"),
`${manifest.packageKey}: address has no renderer`,
);
}
}
for (const field of profileFields) {
assert.ok(
bindings.has(`brand.config.profilePresentation.${field}`),
pageBindings.has(`brand.config.profilePresentation.${field}`),
`${manifest.packageKey}: profilePresentation.${field} has no Admin field`,
);
}
@@ -110,17 +231,107 @@ for (const directory of fs
continue;
}
assert.ok(
manifest.trustContentTypes?.some((type) =>
type.configFields?.some(
(configField) => configField.binding === `trust.config.${field}`,
),
manifest.trustContentTypes?.some(
(type) =>
type.pages?.includes(pageKey) &&
type.configFields?.some(
(configField) => configField.binding === `trust.config.${field}`,
),
),
`${manifest.packageKey}: trustContent.${field} has no Admin field`,
);
}
for (const type of trustItemTypes) {
const declaration = manifest.trustContentTypes?.find((item) => item.type === type);
assert.ok(
declaration,
`${manifest.packageKey}: renderer uses undeclared trust type ${type}`,
);
assert.ok(
declaration.pages?.includes(pageKey),
`${manifest.packageKey}: renderer uses trust type ${type} on ${pageKey} but the page is not declared`,
);
}
for (const declaration of manifest.trustContentTypes ?? []) {
if (!declaration.pages?.includes(pageKey)) continue;
assert.ok(
rendersAllTrustItems || trustItemTypes.has(declaration.type),
`${manifest.packageKey}: trust type ${declaration.type} claims ${pageKey}, but that renderer does not display it`,
);
}
const pageSource = [...seen]
.flatMap((relativePath) =>
["js", "wxml"]
.map((extension) => sourceFile(templateRoot, relativePath, extension))
.filter(Boolean)
.map((file) => fs.readFileSync(file, "utf8")),
)
.join("\n");
for (const declaration of manifest.trustContentTypes ?? []) {
if (!declaration.pages?.includes(pageKey)) continue;
for (const field of declaration.configFields ?? []) {
assert.ok(
pageSource.includes(`.${field.key}`) ||
declaration.pages.some((destination) => {
const destinationFiles = renderedFields(
templateRoot,
manifest.entrypoints[destination],
).seen;
return [...destinationFiles].some((file) =>
["js", "wxml"].some((extension) => {
const source = sourceFile(templateRoot, file, extension);
return source && fs.readFileSync(source, "utf8").includes(`.${field.key}`);
}),
);
}),
`${manifest.packageKey}: ${declaration.type}.${field.key} is editable but is not consumed on ${pageKey}`,
);
}
}
if (pageKey === Object.keys(manifest.entrypoints)[0]) {
for (const declaration of manifest.trustContentTypes ?? []) {
assert.ok(
declaration.pages?.length,
`${manifest.packageKey}: trust type ${declaration.type} must declare its Mini Program destination`,
);
}
}
if (pageKey === "home") {
for (const field of manifest.templateFields ?? []) {
const source = `templateData.${field.key}`;
const entryFiles = [...seen].flatMap((relativePath) =>
["js", "wxml"]
.map((extension) => sourceFile(templateRoot, relativePath, extension))
.filter(Boolean),
);
assert.ok(
entryFiles.some((file) =>
new RegExp(`templateData(?:\\?\\.)?${field.key}`).test(fs.readFileSync(file, "utf8")),
),
`${manifest.packageKey}: template field ${field.key} is declared but never rendered`,
);
}
}
if (seen.has("components/modules/offering-detail/offering-detail")) {
for (const binding of sharedOfferingBindings) {
assert.ok(bindings.has(binding), `${manifest.packageKey}: missing ${binding}`);
assert.ok(
pageBindings.has(binding),
`${manifest.packageKey}: missing ${binding} on detail page`,
);
}
const detailSources = [...seen].flatMap((relativePath) =>
["wxml", "js"]
.map((extension) => sourceFile(templateRoot, relativePath, extension))
.filter(Boolean)
.map((file) => fs.readFileSync(file, "utf8")),
);
const detailSource = detailSources.join("\n");
for (const [binding, runtimePath] of Object.entries(offeringRuntimeBindings)) {
if (!pageBindings.has(binding)) continue;
assert.ok(
detailSource.includes(runtimePath),
`${manifest.packageKey}: ${binding} is editable but ${runtimePath} is not rendered on ${pageKey}`,
);
}
}
}
@@ -4,6 +4,7 @@ import path from "node:path";
import { fileURLToPath } from "node:url";
import test from "node:test";
import { validateTemplatePackageManifest } from "../src/index.ts";
import { runtimeContractForBinding } from "../../types/src/runtime-content-contract.ts";
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
@@ -26,6 +27,61 @@ test("template contract rejects technical or incomplete admin declarations", ()
]);
});
test("template admin sections must point to a declared Mini Program page", () => {
const manifestPath = path.join(packageRoot, "template-acheng-campus-food/template.json");
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.admin.sections[0].page = "missing-page";
assert.deepEqual(validateTemplatePackageManifest(manifest), [
"admin section identity must target a declared entrypoint",
]);
});
test("template trust content pages must point to declared Mini Program pages", () => {
const manifestPath = path.join(packageRoot, "template-acheng-campus-food/template.json");
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.trustContentTypes[0].pages = ["missing-page"];
assert.deepEqual(validateTemplatePackageManifest(manifest), [
"trustContentTypes.0.pages contains an undeclared entrypoint",
]);
});
test("template trust editor fields map to persisted TrustContent columns", () => {
const manifestPath = path.join(packageRoot, "template-acheng-campus-food/template.json");
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.trustContentTypes[0].adminFields.summary = "";
assert.ok(
validateTemplatePackageManifest(manifest).includes(
"trustContentTypes.0.adminFields.summary is required",
),
);
});
test("template samples cannot import trust types without an Admin declaration", () => {
const manifestPath = path.join(packageRoot, "template-acheng-campus-food/template.json");
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.sampleContent.food.trustContent.push({
type: "not-declared",
title: "未知",
content: "未知",
});
assert.ok(
validateTemplatePackageManifest(manifest).includes(
`sampleContent.food.trustContent.${manifest.sampleContent.food.trustContent.length - 1} uses an undeclared trust content type`,
),
);
});
test("template sample inquiry fields use Runtime-supported input types", () => {
const manifestPath = path.join(packageRoot, "template-acheng-campus-food/template.json");
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.sampleContent.food.fields[0].type = "image";
assert.ok(
validateTemplatePackageManifest(manifest).includes(
"sampleContent.food.fields.0 has an unsupported Runtime type",
),
);
});
test("template manifests declare editable material content", () => {
const linjing = JSON.parse(
fs.readFileSync(path.join(packageRoot, "template-linjing-editorial/template.json"), "utf8"),
@@ -47,7 +103,6 @@ test("template manifests declare editable material content", () => {
])
assert.ok(bindings(linjing).has(binding), binding);
for (const binding of [
"brand.config.visualPresentation.profile.coverageContent",
"brand.config.visualPresentation.offerings.menuTip",
"brand.config.visualPresentation.inquiry.promise",
])
@@ -59,3 +114,135 @@ test("template manifests declare editable material content", () => {
["stats", "pillars", "serviceFlow"],
);
});
test("template fields use the shallow templateData namespace", () => {
for (const key of ["acheng-campus-food", "linjing-editorial"]) {
const manifest = JSON.parse(
fs.readFileSync(path.join(packageRoot, `template-${key}/template.json`), "utf8"),
);
for (const field of manifest.templateFields ?? []) {
assert.equal(typeof field.key, "string");
assert.ok(!field.key.includes("."), `${key}: template field keys stay shallow`);
assert.ok(
[
"text",
"textarea",
"number",
"boolean",
"image",
"select",
"multi-select",
"list",
].includes(field.input),
);
}
}
});
test("every declared Admin binding has one Runtime contract", () => {
for (const key of ["acheng-campus-food", "linjing-editorial"]) {
const manifest = JSON.parse(
fs.readFileSync(path.join(packageRoot, `template-${key}/template.json`), "utf8"),
);
for (const field of manifest.admin.sections.flatMap((section) => section.fields)) {
assert.ok(runtimeContractForBinding(field.binding), `${key}: ${field.binding}`);
}
}
});
test("shared brand facts have one editor and template-specific offering detail binds to Runtime", () => {
const sharedBrandBindings = new Set([
"brand.name",
"brand.tagline",
"brand.description",
"brand.avatarUrl",
"brand.coverUrl",
"brand.hours",
"brand.address",
"brand.contactPhone",
"brand.contactWechat",
"brand.deliveryNote",
]);
for (const key of ["acheng-campus-food", "linjing-editorial"]) {
const root = path.join(packageRoot, `template-${key}`);
const manifest = JSON.parse(fs.readFileSync(path.join(root, "template.json"), "utf8"));
const bindings = manifest.admin.sections.flatMap((section) =>
section.fields.map((field) => field.binding),
);
assert.ok(!bindings.some((binding) => sharedBrandBindings.has(binding)), key);
}
const foodRoot = path.join(packageRoot, "template-acheng-campus-food");
const food = JSON.parse(fs.readFileSync(path.join(foodRoot, "template.json"), "utf8"));
const background = food.admin.sections
.flatMap((section) => section.fields)
.find((field) => field.binding === "offering.config.details.background");
assert.equal(background.label, "餐品介绍");
const runtimeRenderer = fs.readFileSync(
path.resolve(
packageRoot,
"../apps/miniapp/components/modules/offering-detail/offering-detail.wxml",
),
"utf8",
);
assert.match(runtimeRenderer, /item\.details\.background\s*\|\|\s*item\.desc/);
const runtimeProjection = fs.readFileSync(
path.resolve(packageRoot, "../apps/api/src/modules/public/public-workspace.controller.ts"),
"utf8",
);
assert.match(runtimeProjection, /projectOfferingDetails\(config\)/);
});
test("food service slots declare the structured fields consumed by their visual layout", () => {
const manifest = JSON.parse(
fs.readFileSync(path.join(packageRoot, "template-acheng-campus-food/template.json"), "utf8"),
);
const schedule = manifest.trustContentTypes.find((item) => item.type === "schedule");
assert.deepEqual(schedule.pages, ["service"]);
assert.deepEqual(schedule.hideStandardFields, ["summary", "content"]);
for (const key of ["weekdayLabel", "weekdayDetail", "weekendLabel", "weekendDetail"]) {
assert.ok(schedule.configFields.some((field) => field.key === key && field.required));
}
const page = fs.readFileSync(
path.join(packageRoot, "template-acheng-campus-food/miniapp/pages/stall-service/index.wxml"),
"utf8",
);
assert.match(page, /wx:for="\{\{schedule\.rows\}\}"/);
assert.doesNotMatch(page, /wx:for="\{\{trustItems\}\}"|item\.summary|item\.content/);
});
test("food home passes projected trust items to the renderer", () => {
const page = fs.readFileSync(
path.join(packageRoot, "template-acheng-campus-food/miniapp/pages/index/index.wxml"),
"utf8",
);
assert.match(page, /<acheng-home[\s\S]*trust-items="\{\{trustItems\}\}"/);
assert.doesNotMatch(page, /trust-content=/);
});
test("food profile renders only profile trust types once", () => {
const manifest = JSON.parse(
fs.readFileSync(path.join(packageRoot, "template-acheng-campus-food/template.json"), "utf8"),
);
const page = fs.readFileSync(
path.join(packageRoot, "template-acheng-campus-food/miniapp/pages/bookings/index.wxml"),
"utf8",
);
assert.equal(
manifest.trustContentTypes.find((item) => item.type === "credential").pages.includes("profile"),
true,
);
for (const type of ["coverage", "process"]) {
assert.equal(
manifest.trustContentTypes.find((item) => item.type === type).pages.includes("profile"),
false,
`${type} must be rendered on its dedicated page`,
);
}
assert.equal((page.match(/credentials="{{credentials}}"/g) ?? []).length, 1);
assert.doesNotMatch(page, /item\.type === '(coverage|process)'/);
for (const type of ["case", "promise"]) {
assert.ok(
manifest.trustContentTypes.find((item) => item.type === type).pages.includes("profile"),
);
}
});