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(/(? 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}`); } } } }); }