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)), "../.."); test("independent preview fixtures validate safety and industry without changing bootstrap content", () => { const manifest = JSON.parse( fs.readFileSync(path.join(packageRoot, "template-atelier-solopreneur/template.json"), "utf8"), ); assert.deepEqual(validateTemplatePackageManifest(manifest), []); assert.notEqual( manifest.previewFixture.personal.content.brandConfig.founder, manifest.sampleContent.personal.brandConfig.founder, ); for (const mutate of [ (value) => { value.previewFixture.personal.safety.submissions = "enabled"; }, (value) => { value.previewFixture.personal.brand.contactPhone = "13800000000"; }, (value) => { value.previewFixture.personal.content.fields = [null]; }, (value) => { value.previewFixture.personal.content.templateKey = "food"; }, (value) => { value.previewFixture = []; }, (value) => { value.previewFixture.personal = null; }, ]) { const invalid = structuredClone(manifest); mutate(invalid); assert.ok( validateTemplatePackageManifest(invalid).some((message) => message.startsWith("previewFixture"), ), ); } }); 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")); const previewFixture = structuredClone(manifest.previewFixture); delete manifest.previewFixture; assert.ok( validateTemplatePackageManifest(manifest).some((message) => message.startsWith("previewFixture"), ), ); manifest.previewFixture = previewFixture; 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 package manifests reject mismatched visual keys and empty visual presentation", () => { const manifestPath = path.join(packageRoot, "template-acheng-campus-food/template.json"); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); manifest.visualTemplateKey = "different-visual"; assert.ok( validateTemplatePackageManifest(manifest).includes("visualTemplateKey must match packageKey"), ); manifest.visualTemplateKey = manifest.packageKey; manifest.visualPresentation = { description: "无效视觉", variants: {}, theme: { colors: {}, radius: {} }, layout: { home: [], offerings: [], detail: [], profile: [] }, }; assert.deepEqual(validateTemplatePackageManifest(manifest), [ "visualPresentation.variants requires hero, offeringList and detail", "visualPresentation.theme.colors requires background, foreground, accent and dark", "visualPresentation.theme.radius requires card", "visualPresentation.layout must contain non-empty page module arrays", ]); }); test("template package manifests must declare package-owned visual presentation", () => { const manifestPath = path.join(packageRoot, "template-acheng-campus-food/template.json"); const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8")); delete manifest.visualPresentation; assert.deepEqual(validateTemplatePackageManifest(manifest), [ "visualPresentation requires description, variants, theme.colors and theme.radius", ]); manifest.visualPresentation = { description: "有效视觉", variants: { hero: "hero", offeringList: "service-list", detail: "case" }, theme: { colors: { background: "#fff", foreground: "#111", accent: "#333", dark: "#000" }, radius: { card: "16rpx" }, }, layout: { home: ["hero"], offerings: ["service-list"], detail: ["case"], profile: ["contact"], }, }; assert.deepEqual(validateTemplatePackageManifest(manifest), []); }); 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, / { 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"), ); } });