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

129 lines
4.9 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";
const packagesRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const runtimeRoot = path.resolve(packagesRoot, "../apps/miniapp");
function sourceFile(templateRoot, relativePath, extension) {
const local = path.join(templateRoot, "miniapp", `${relativePath}.${extension}`);
const runtime = path.join(runtimeRoot, `${relativePath}.${extension}`);
return fs.existsSync(local) ? local : fs.existsSync(runtime) ? runtime : null;
}
function renderedFields(templateRoot, entrypoint) {
const seen = new Set();
const fields = new Set();
const heroFields = new Set();
const profileFields = new Set();
const trustFields = new Set();
function visit(relativePath) {
if (seen.has(relativePath)) return;
seen.add(relativePath);
for (const extension of ["wxml", "js"]) {
const source = sourceFile(templateRoot, relativePath, extension);
if (!source) continue;
const content = fs.readFileSync(source, "utf8");
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])hero\.([A-Za-z][A-Za-z0-9]*)/g)) {
heroFields.add(match[1]);
}
for (const match of content.matchAll(/brand\.profilePresentation\.([A-Za-z][A-Za-z0-9]*)/g)) {
profileFields.add(match[1]);
}
for (const match of content.matchAll(/trustContent\.([A-Za-z][A-Za-z0-9]*)/g)) {
trustFields.add(match[1]);
}
}
const componentConfig = sourceFile(templateRoot, relativePath, "json");
if (!componentConfig) return;
const config = JSON.parse(fs.readFileSync(componentConfig, "utf8"));
for (const component of Object.values(config.usingComponents ?? {})) {
if (typeof component === "string" && component.startsWith("/components/")) {
visit(component.slice(1));
}
}
}
visit(entrypoint);
return { fields, heroFields, profileFields, trustFields, seen };
}
const sharedOfferingBindings = [
"offering.config.categoryLabel",
"offering.config.detailHero",
"offering.config.area",
"offering.config.series",
"offering.config.spaceType",
"offering.config.keywords",
"offering.config.details.background",
"offering.config.details.designConcept",
"offering.config.details.crafts",
"offering.config.details.materialsList",
];
for (const directory of fs
.readdirSync(packagesRoot)
.filter((name) => name.startsWith("template-"))) {
const templateRoot = path.join(packagesRoot, directory);
const manifestPath = path.join(templateRoot, "template.json");
if (!fs.existsSync(manifestPath)) continue;
test(`${directory} exposes rendered content through Admin bindings`, () => {
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
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,
);
for (const field of fields) {
assert.ok(
bindings.has(`brand.config.visualPresentation.${pageKey}.${field}`),
`${manifest.packageKey}: ${pageKey}.${field} is rendered but has no Admin field`,
);
}
for (const field of heroFields) {
const binding =
field === "image" ? "brand.config.heroImage" : `brand.config.homeHero.${field}`;
assert.ok(
bindings.has(binding),
`${manifest.packageKey}: ${pageKey} hero.${field} has no Admin field`,
);
}
for (const field of profileFields) {
assert.ok(
bindings.has(`brand.config.profilePresentation.${field}`),
`${manifest.packageKey}: profilePresentation.${field} has no Admin field`,
);
}
for (const field of trustFields) {
if (field === "bio") {
assert.ok(
manifest.trustContentTypes?.some((type) => type.type === "credential"),
`${manifest.packageKey}: biography has no editable trust content type`,
);
continue;
}
assert.ok(
manifest.trustContentTypes?.some((type) =>
type.configFields?.some(
(configField) => configField.binding === `trust.config.${field}`,
),
),
`${manifest.packageKey}: trustContent.${field} has no Admin field`,
);
}
if (seen.has("components/modules/offering-detail/offering-detail")) {
for (const binding of sharedOfferingBindings) {
assert.ok(bindings.has(binding), `${manifest.packageKey}: missing ${binding}`);
}
}
}
});
}