Files
persionalBrand/packages/template-contract/test/manifest.test.mjs
T

254 lines
10 KiB
JavaScript

import assert from "node:assert/strict";
import fs from "node:fs";
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)), "../..");
for (const key of ["acheng-campus-food", "linjing-editorial"]) {
test(`${key} declares valid admin content and asset requirements`, () => {
const manifestPath = path.join(packageRoot, `template-${key}`, "template.json");
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
assert.deepEqual(validateTemplatePackageManifest(manifest), []);
});
}
test("template contract rejects technical or incomplete admin declarations", () => {
const manifestPath = path.join(packageRoot, "template-acheng-campus-food/template.json");
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
manifest.admin.sections[0].fields[0].label = "";
manifest.assetSlots[0].help = "";
assert.deepEqual(validateTemplatePackageManifest(manifest), [
"every asset slot requires key, binding, label and help",
"every admin field requires key, binding and label",
]);
});
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"),
);
const acheng = JSON.parse(
fs.readFileSync(path.join(packageRoot, "template-acheng-campus-food/template.json"), "utf8"),
);
const bindings = (manifest) =>
new Set(
manifest.admin.sections.flatMap((section) => section.fields.map((field) => field.binding)),
);
for (const binding of [
"brand.config.homeHero.title",
"brand.config.profilePresentation.coverage",
"brand.config.profilePresentation.warranty",
"offering.config.area",
"offering.config.details.designConcept",
"brand.config.visualPresentation.offerings.closingText",
])
assert.ok(bindings(linjing).has(binding), binding);
for (const binding of [
"brand.config.visualPresentation.offerings.menuTip",
"brand.config.visualPresentation.inquiry.promise",
])
assert.ok(bindings(acheng).has(binding), binding);
assert.deepEqual(
linjing.trustContentTypes
.find((item) => item.type === "credential")
.configFields.map((field) => field.key),
["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),
);
for (const field of manifest.admin.sections.flatMap((section) => section.fields)) {
if (sharedBrandBindings.has(field.binding)) {
assert.ok(field.previewTarget, key + ": shared facts need visual targets");
assert.ok(!field.binding.includes(".config."), "Shared facts must use canonical columns");
}
}
}
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.runtime-mapper.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"),
);
}
});