fix: 完善模板可视化编辑闭环与通用验收
This commit is contained in:
1 parent
fa6b492bce
commit
22a82dbfba
39 files changed
+7773
-443
No files matched your search
@@ -0,0 +1,374 @@
|
||||
import ts from "typescript";
|
||||
|
||||
// 静态检查不推断运行时成功;未知表达式保留为 review。
|
||||
export function diagnoseTemplate(manifest, source, file = "taro/index.tsx") {
|
||||
const issues = [];
|
||||
const regions = [];
|
||||
const targets = [];
|
||||
const fields = [];
|
||||
const add = (code, context, message, severity = "blocker") =>
|
||||
issues.push({ code, severity, file, ...context, message });
|
||||
for (const section of manifest.admin?.sections ?? []) {
|
||||
for (const target of section.previewTargets ?? []) {
|
||||
const owned = (section.fields ?? []).filter((field) => field.previewTarget === target.key);
|
||||
// 与 PresentationEditor.previewTargetSource 的优先级一致。
|
||||
const source =
|
||||
["offering", "trust", "brand"].find((kind) =>
|
||||
owned.some((field) => field.binding?.startsWith(kind + ".")),
|
||||
) ?? section.target;
|
||||
targets.push({ ...target, page: section.page, source });
|
||||
}
|
||||
for (const field of section.fields ?? []) fields.push({ ...field, page: section.page });
|
||||
}
|
||||
for (const field of manifest.templateFields ?? []) fields.push(field);
|
||||
for (const declaration of manifest.trustContentTypes ?? []) {
|
||||
for (const page of declaration.pages ?? []) {
|
||||
const key = "trust:" + declaration.type;
|
||||
targets.push({ key, page, source: "trust", implicit: true });
|
||||
for (const name of ["title", "summary", "content"])
|
||||
if (!declaration.hideStandardFields?.includes(name))
|
||||
fields.push({ binding: "trust." + name, previewTarget: key, page });
|
||||
for (const field of (declaration.configFields ?? []).filter((field) => {
|
||||
const target = declaration.previewTargets?.find(
|
||||
(target) => target.key === field.previewTarget,
|
||||
);
|
||||
return !target?.page || target.page === page;
|
||||
}))
|
||||
fields.push({ ...field, previewTarget: field.previewTarget ?? key, page });
|
||||
}
|
||||
for (const target of declaration.previewTargets ?? []) {
|
||||
if (!target.page)
|
||||
add(
|
||||
"TARGET_PAGE_UNSPECIFIED",
|
||||
{ target: target.key },
|
||||
"信任子区域需要明确展示页面,不能自动出现在该类型的全部页面。",
|
||||
);
|
||||
targets.push({ ...target, page: target.page ?? declaration.pages?.[0], source: "trust" });
|
||||
}
|
||||
}
|
||||
const matching = (key, page) => targets.find((t) => t.key === key && t.page === page);
|
||||
for (const field of fields) {
|
||||
const target = matching(field.previewTarget, field.page);
|
||||
const context = { page: field.page, target: field.previewTarget, binding: field.binding };
|
||||
if (!target) add("FIELD_TARGET_MISSING", context, "字段必须绑定同页已声明区域。");
|
||||
else if (target.composite)
|
||||
add("COMPOSITE_HAS_FIELDS", context, "组合区域应导航到字段所属子区。");
|
||||
else if (field.binding?.split(".")[0] !== target.source)
|
||||
add(
|
||||
"TARGET_SOURCE_MISMATCH",
|
||||
context,
|
||||
"区域来源与字段不一致,现有单记录编辑器无法完整保存。",
|
||||
);
|
||||
}
|
||||
for (const target of targets) {
|
||||
const context = { page: target.page, target: target.key };
|
||||
if (targets.filter((t) => t.key === target.key && t.page === target.page).length > 1)
|
||||
add("DUPLICATE_TARGET", context, "同页区域标识必须唯一。");
|
||||
if (target.composite) {
|
||||
if (!target.children?.length) add("COMPOSITE_EMPTY", context, "组合区域必须有子区。");
|
||||
for (const child of target.children ?? [])
|
||||
if (!matching(child, target.page) || matching(child, target.page)?.composite)
|
||||
add("COMPOSITE_CHILD_INVALID", context, "子区必须是同页直接区域:" + child);
|
||||
}
|
||||
}
|
||||
const ast = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
||||
for (const diagnostic of ast.parseDiagnostics)
|
||||
add("RENDERER_PARSE_ERROR", {}, ts.flattenDiagnosticMessageText(diagnostic.messageText, " "));
|
||||
const standardBrand = new Set([
|
||||
"name",
|
||||
"tagline",
|
||||
"description",
|
||||
"avatarUrl",
|
||||
"contactPhone",
|
||||
"contactWechat",
|
||||
"address",
|
||||
"hours",
|
||||
"deliveryNote",
|
||||
"coverUrl",
|
||||
]);
|
||||
const normalize = (path) => {
|
||||
if (
|
||||
path.startsWith("brand.") &&
|
||||
!path.startsWith("brand.config.") &&
|
||||
!standardBrand.has(path.split(".")[1])
|
||||
)
|
||||
return path.replace("brand.", "brand.config.");
|
||||
return path;
|
||||
};
|
||||
function inspectFunction(fn) {
|
||||
const aliases = new Map([["workspace", "workspace"]]);
|
||||
const offeringAliases = {
|
||||
image: "offering.config.detailHero",
|
||||
desc: "offering.summary",
|
||||
priceRange: "offering.priceText",
|
||||
category: "offering.config.categoryLabel",
|
||||
area: "offering.config.area",
|
||||
series: "offering.config.series",
|
||||
keywords: "offering.config.keywords",
|
||||
details: "offering.config.details",
|
||||
};
|
||||
const resolve = (node) => {
|
||||
if (!node) return undefined;
|
||||
if (ts.isIdentifier(node)) return aliases.get(node.text);
|
||||
if (ts.isPropertyAccessExpression(node)) {
|
||||
const base = resolve(node.expression);
|
||||
if (!base) return undefined;
|
||||
if (base === "offering" && offeringAliases[node.name.text])
|
||||
return offeringAliases[node.name.text];
|
||||
if (base === "offering.presentation") return "offering.config." + node.name.text;
|
||||
const path = base + "." + node.name.text;
|
||||
if (path === "workspace.brand") return "brand";
|
||||
if (path === "workspace.templateData") return "brand.config.templateData";
|
||||
if (path === "workspace.trustPresentation") return "trust.config";
|
||||
if (path === "workspace.offerings") return "offering[]";
|
||||
if (path === "workspace.trustItems") return "trust[]";
|
||||
return path;
|
||||
}
|
||||
if (ts.isCallExpression(node) && ["object", "rows"].includes(node.expression.getText(ast)))
|
||||
return resolve(node.arguments[0]);
|
||||
if (ts.isCallExpression(node) && node.expression.getText(ast) === "offering")
|
||||
return "offering";
|
||||
if (ts.isCallExpression(node) && node.expression.getText(ast) === "slot") return "trust";
|
||||
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) {
|
||||
const base = resolve(node.expression.expression);
|
||||
if (["filter", "slice"].includes(node.expression.name.text)) return base;
|
||||
if (node.expression.name.text === "find") return base?.replace(/\[\]$/, "");
|
||||
if (node.expression.name.text === "map" && node.arguments[0]?.getText(ast) === "offering")
|
||||
return "offering[]";
|
||||
}
|
||||
if (
|
||||
ts.isCallExpression(node) &&
|
||||
node.expression.getText(ast) === "presentation" &&
|
||||
node.arguments[1] &&
|
||||
ts.isStringLiteral(node.arguments[1])
|
||||
)
|
||||
return "brand.config.visualPresentation." + node.arguments[1].text;
|
||||
return undefined;
|
||||
};
|
||||
function collect(node) {
|
||||
if (node !== fn && ts.isFunctionDeclaration(node)) return;
|
||||
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
|
||||
const path = resolve(node.initializer);
|
||||
if (path) aliases.set(node.name.text, path);
|
||||
}
|
||||
ts.forEachChild(node, collect);
|
||||
}
|
||||
collect(fn);
|
||||
function visit(node, active) {
|
||||
if (
|
||||
ts.isCallExpression(node) &&
|
||||
ts.isPropertyAccessExpression(node.expression) &&
|
||||
node.expression.name.text === "map"
|
||||
) {
|
||||
const collection = resolve(node.expression.expression);
|
||||
const callback = node.arguments[0];
|
||||
if (
|
||||
collection &&
|
||||
callback &&
|
||||
ts.isArrowFunction(callback) &&
|
||||
callback.parameters[0] &&
|
||||
ts.isIdentifier(callback.parameters[0].name)
|
||||
) {
|
||||
const name = callback.parameters[0].name.text;
|
||||
const previous = aliases.get(name);
|
||||
aliases.set(name, collection.replace(/\[\]$/, ""));
|
||||
visit(callback.body, active);
|
||||
if (previous) aliases.set(name, previous);
|
||||
else aliases.delete(name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (ts.isConditionalExpression(node)) {
|
||||
add(
|
||||
"CONDITIONAL_RENDER_REVIEW",
|
||||
{
|
||||
target: active?.target,
|
||||
line: ast.getLineAndCharacterOfPosition(node.getStart(ast)).line + 1,
|
||||
},
|
||||
"条件显示需验证全部分支与空数据入口。",
|
||||
"review",
|
||||
);
|
||||
visit(node.whenTrue, active);
|
||||
visit(node.whenFalse, active);
|
||||
return;
|
||||
}
|
||||
if (
|
||||
ts.isBinaryExpression(node) &&
|
||||
node.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken
|
||||
) {
|
||||
add(
|
||||
"CONDITIONAL_RENDER_REVIEW",
|
||||
{
|
||||
target: active?.target,
|
||||
line: ast.getLineAndCharacterOfPosition(node.getStart(ast)).line + 1,
|
||||
},
|
||||
"条件显示需验证全部分支与空数据入口。",
|
||||
"review",
|
||||
);
|
||||
visit(node.right, active);
|
||||
return;
|
||||
}
|
||||
if (ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node)) {
|
||||
const opening = ts.isJsxElement(node) ? node.openingElement : node;
|
||||
const attrs = opening.attributes.properties;
|
||||
const attr = attrs.find(
|
||||
(a) => ts.isJsxAttribute(a) && a.name.getText(ast) === "data-editor-target",
|
||||
);
|
||||
if (attr) {
|
||||
if (attr.initializer && ts.isStringLiteral(attr.initializer)) {
|
||||
const key = attr.initializer.text;
|
||||
const candidates = targets.filter((t) => t.key === key);
|
||||
active = {
|
||||
target: key,
|
||||
page: candidates[0]?.page,
|
||||
line: ast.getLineAndCharacterOfPosition(node.getStart(ast)).line + 1,
|
||||
bindings: new Set(),
|
||||
};
|
||||
regions.push(active);
|
||||
if (!candidates.length)
|
||||
add("RENDER_TARGET_UNDECLARED", active, "renderer 区域没有声明。");
|
||||
if (
|
||||
candidates.some((t) => ["trust", "offering"].includes(t.source)) &&
|
||||
!attrs.some(
|
||||
(a) => ts.isJsxAttribute(a) && a.name.getText(ast) === "data-editor-record-id",
|
||||
)
|
||||
)
|
||||
add(
|
||||
"RECORD_ID_MISSING",
|
||||
{ target: key, page: active.page, line: active.line },
|
||||
"记录区域必须携带实际记录 ID。",
|
||||
);
|
||||
} else {
|
||||
add(
|
||||
"DYNAMIC_TARGET_REVIEW",
|
||||
{ line: ast.getLineAndCharacterOfPosition(node.getStart(ast)).line + 1 },
|
||||
"动态目标需用真实 manifest 和实际记录验证分支。",
|
||||
"review",
|
||||
);
|
||||
active = undefined;
|
||||
}
|
||||
}
|
||||
// 属性中的事件和 recordId 不属于展示变量,只检查图片等展示属性。
|
||||
for (const a of attrs)
|
||||
if (
|
||||
ts.isJsxAttribute(a) &&
|
||||
["src", "title", "placeholder", "value"].includes(a.name.getText(ast)) &&
|
||||
a.initializer
|
||||
)
|
||||
visit(a.initializer, active);
|
||||
if (ts.isJsxElement(node)) for (const child of node.children) visit(child, active);
|
||||
return;
|
||||
}
|
||||
if (ts.isPropertyAccessExpression(node) && !ts.isPropertyAccessExpression(node.parent)) {
|
||||
if (ts.isElementAccessExpression(node.parent) && node.parent.expression === node) return;
|
||||
const path = resolve(node);
|
||||
if (path && /^(brand|trust|offering)\./.test(path)) {
|
||||
const binding = normalize(path);
|
||||
if (active) active.bindings.add(binding);
|
||||
else
|
||||
add(
|
||||
"UNSCOPED_RENDER_VALUE",
|
||||
{ binding, line: ast.getLineAndCharacterOfPosition(node.getStart(ast)).line + 1 },
|
||||
"展示变量没有可证明的编辑区域。",
|
||||
"review",
|
||||
);
|
||||
} else if (active)
|
||||
add(
|
||||
"UNRESOLVED_RENDER_VALUE",
|
||||
{
|
||||
target: active.target,
|
||||
page: active.page,
|
||||
expression: node.getText(ast),
|
||||
line: ast.getLineAndCharacterOfPosition(node.getStart(ast)).line + 1,
|
||||
},
|
||||
"需要核对循环项或间接映射,不能视为已覆盖。",
|
||||
"review",
|
||||
);
|
||||
}
|
||||
if (ts.isElementAccessExpression(node) && active)
|
||||
add(
|
||||
"DYNAMIC_VALUE_REVIEW",
|
||||
{ target: active.target, page: active.page, expression: node.getText(ast) },
|
||||
"动态索引必须验证实际分支。",
|
||||
"review",
|
||||
);
|
||||
ts.forEachChild(node, (child) => visit(child, active));
|
||||
}
|
||||
// 只从 JSX 开始,避免把数据准备和事件中的读取当成展示值。
|
||||
function roots(node) {
|
||||
if (ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node)) return visit(node, undefined);
|
||||
ts.forEachChild(node, roots);
|
||||
}
|
||||
roots(fn);
|
||||
}
|
||||
for (const statement of ast.statements) {
|
||||
if (ts.isFunctionDeclaration(statement)) inspectFunction(statement);
|
||||
else {
|
||||
const check = (node) => {
|
||||
if (ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node)) {
|
||||
add(
|
||||
"UNSUPPORTED_COMPONENT_REVIEW",
|
||||
{ line: ast.getLineAndCharacterOfPosition(node.getStart(ast)).line + 1 },
|
||||
"非函数声明组件需扩展解析器或补真实交互验证,不能静态认定覆盖。",
|
||||
"review",
|
||||
);
|
||||
return;
|
||||
}
|
||||
ts.forEachChild(node, check);
|
||||
};
|
||||
check(statement);
|
||||
}
|
||||
}
|
||||
for (const region of regions) {
|
||||
const context = { page: region.page, target: region.target, line: region.line };
|
||||
const declared = fields.filter(
|
||||
(f) => f.previewTarget === region.target && f.page === region.page,
|
||||
);
|
||||
for (const binding of region.bindings)
|
||||
if (
|
||||
!(
|
||||
region.page === "detail" && binding.startsWith("offering.")
|
||||
? fields.filter((f) => f.binding?.startsWith("offering."))
|
||||
: declared
|
||||
).some((f) => f.binding === binding || binding.startsWith(f.binding + "."))
|
||||
)
|
||||
add(
|
||||
"REGION_FIELD_MISSING",
|
||||
{ ...context, binding },
|
||||
"选区实际展示该变量但当前面板没有对应字段;补真实绑定或明确只读模式。",
|
||||
);
|
||||
for (const binding of region.bindings) {
|
||||
const field = declared.find((f) => f.input === "list" && binding.startsWith(f.binding + "."));
|
||||
if (!field || field.itemValueKey) continue;
|
||||
const child = binding.slice(field.binding.length + 1).split(".")[0];
|
||||
if (["map", "slice", "length", "filter", "join"].includes(child)) continue;
|
||||
if (!field.itemFields?.some((item) => item.key === child))
|
||||
add(
|
||||
"LIST_FIELD_MISSING",
|
||||
{ ...context, binding },
|
||||
"列表展示的子字段必须在 itemFields 中提供编辑入口。",
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const target of targets)
|
||||
if (!target.implicit && !regions.some((r) => r.target === target.key))
|
||||
add(
|
||||
"TARGET_RENDER_UNVERIFIED",
|
||||
{ page: target.page, target: target.key },
|
||||
"未找到字面量区域,需验证动态目标或组件路径。",
|
||||
"review",
|
||||
);
|
||||
return {
|
||||
template: manifest.packageKey,
|
||||
staticStatus: issues.length ? "fail" : "pass",
|
||||
completionStatus: "unverified",
|
||||
requiredEvidence: [
|
||||
"real-manifest-interaction",
|
||||
"save-reload-publish-isolation",
|
||||
"visual-reference",
|
||||
"empty-error-branches",
|
||||
],
|
||||
regions: regions.map((r) => ({ ...r, bindings: [...r.bindings] })),
|
||||
issues,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { diagnoseTemplate } from "./lib/template-doctor.mjs";
|
||||
|
||||
try {
|
||||
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const args = process.argv.slice(2);
|
||||
const options = {};
|
||||
for (let i = 0; i < args.length; i += 2) {
|
||||
if (!["--template", "--output"].includes(args[i]) || !args[i + 1])
|
||||
throw new Error("Usage: template-doctor [--template key] [--output path]");
|
||||
options[args[i]] = args[i + 1];
|
||||
}
|
||||
const reports = [];
|
||||
for (const dir of fs.readdirSync(path.join(root, "packages")).sort()) {
|
||||
const manifestPath = path.join(root, "packages", dir, "template.json");
|
||||
if (!dir.startsWith("template-") || !fs.existsSync(manifestPath)) continue;
|
||||
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
||||
if (options["--template"] && manifest.packageKey !== options["--template"]) continue;
|
||||
const renderer = path.join(root, "packages", dir, "taro/index.tsx");
|
||||
if (!fs.existsSync(renderer)) throw new Error("Missing renderer: " + renderer);
|
||||
reports.push(
|
||||
diagnoseTemplate(manifest, fs.readFileSync(renderer, "utf8"), path.relative(root, renderer)),
|
||||
);
|
||||
}
|
||||
if (!reports.length) throw new Error("No matching templates found");
|
||||
const report = { schemaVersion: 1, completionStatus: "unverified", templates: reports };
|
||||
const output = JSON.stringify(report, null, 2) + "\n";
|
||||
if (options["--output"]) {
|
||||
fs.mkdirSync(path.dirname(path.resolve(options["--output"])), { recursive: true });
|
||||
fs.writeFileSync(options["--output"], output);
|
||||
}
|
||||
process.stdout.write(output);
|
||||
process.exitCode = reports.some((r) => r.staticStatus === "fail") ? 1 : 0;
|
||||
} catch (error) {
|
||||
console.error(error.message);
|
||||
process.exitCode = 2;
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import fs from "node:fs";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { diagnoseTemplate } from "../lib/template-doctor.mjs";
|
||||
|
||||
test("真实模板自动发现并读取完整声明,生成可追溯区域报告", () => {
|
||||
const root = new URL("../../packages/", import.meta.url);
|
||||
const dirs = fs
|
||||
.readdirSync(root)
|
||||
.filter(
|
||||
(dir) => dir.startsWith("template-") && fs.existsSync(new URL(dir + "/template.json", root)),
|
||||
);
|
||||
assert.ok(dirs.length > 0);
|
||||
for (const dir of dirs) {
|
||||
const manifest = JSON.parse(fs.readFileSync(new URL(dir + "/template.json", root), "utf8"));
|
||||
const source = fs.readFileSync(new URL(dir + "/taro/index.tsx", root), "utf8");
|
||||
const report = diagnoseTemplate(manifest, source);
|
||||
assert.equal(report.template, manifest.packageKey);
|
||||
assert.ok(report.regions.length > 0);
|
||||
assert.equal(report.completionStatus, "unverified");
|
||||
for (const region of report.regions) {
|
||||
for (const binding of region.bindings) {
|
||||
const fields = [
|
||||
...manifest.admin.sections.flatMap((s) => s.fields),
|
||||
...(manifest.templateFields ?? []),
|
||||
...(manifest.trustContentTypes ?? []).flatMap((t) =>
|
||||
(t.configFields ?? []).map((f) => ({
|
||||
...f,
|
||||
previewTarget: f.previewTarget ?? "trust:" + t.type,
|
||||
})),
|
||||
),
|
||||
...(manifest.trustContentTypes ?? []).flatMap((t) =>
|
||||
["title", "summary", "content"]
|
||||
.filter((name) => !t.hideStandardFields?.includes(name))
|
||||
.map((name) => ({ binding: "trust." + name, previewTarget: "trust:" + t.type })),
|
||||
),
|
||||
];
|
||||
assert.ok(
|
||||
fields.some(
|
||||
(f) =>
|
||||
(f.previewTarget === region.target ||
|
||||
(region.page === "detail" && f.binding.startsWith("offering."))) &&
|
||||
(f.binding === binding || binding.startsWith(f.binding + ".")),
|
||||
) ||
|
||||
report.issues.some(
|
||||
(i) =>
|
||||
i.code === "REGION_FIELD_MISSING" &&
|
||||
i.target === region.target &&
|
||||
i.binding === binding,
|
||||
),
|
||||
`${manifest.packageKey}: ${region.target} ${binding}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("CLI 自动发现全部模板,问题退出 1,未知模板退出 2", () => {
|
||||
const cli = new URL("../template-doctor.mjs", import.meta.url);
|
||||
const all = spawnSync(process.execPath, [cli.pathname], { encoding: "utf8" });
|
||||
const report = JSON.parse(all.stdout);
|
||||
assert.equal(all.status, report.templates.some((t) => t.staticStatus === "fail") ? 1 : 0);
|
||||
assert.ok(report.templates.length > 0);
|
||||
const missing = spawnSync(
|
||||
process.execPath,
|
||||
[cli.pathname, "--template", "missing-template-fixture"],
|
||||
{ encoding: "utf8" },
|
||||
);
|
||||
assert.equal(missing.status, 2);
|
||||
});
|
||||
|
||||
const manifest = () => ({
|
||||
packageKey: "independent-template",
|
||||
admin: {
|
||||
sections: [
|
||||
{
|
||||
page: "home",
|
||||
target: "brand",
|
||||
previewTargets: [{ key: "identity", label: "人物介绍" }],
|
||||
fields: [
|
||||
{ binding: "brand.config.visualPresentation.home.badge", previewTarget: "identity" },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
const renderer =
|
||||
'function Home({workspace}) { const b = workspace.brand; const p = presentation(workspace, "home"); return <View data-editor-target="identity"><Text>{b.founder}</Text><Text>{p.badge}</Text></View>; }';
|
||||
test("整卡仅有标签字段不能通过,报告具体缺失变量", () => {
|
||||
const result = diagnoseTemplate(manifest(), renderer);
|
||||
assert.equal(result.staticStatus, "fail");
|
||||
assert.ok(
|
||||
result.issues.some(
|
||||
(i) => i.code === "REGION_FIELD_MISSING" && i.binding === "brand.config.founder",
|
||||
),
|
||||
);
|
||||
});
|
||||
test("完整字段通过静态检查但不能被宣称模板完成", () => {
|
||||
const input = manifest();
|
||||
input.admin.sections[0].fields.push({
|
||||
binding: "brand.config.founder",
|
||||
previewTarget: "identity",
|
||||
});
|
||||
const result = diagnoseTemplate(input, renderer);
|
||||
assert.equal(result.staticStatus, "pass");
|
||||
assert.equal(result.completionStatus, "unverified");
|
||||
});
|
||||
test("嵌套子区独立检查,父区字段不能遮蔽子区缺失", () => {
|
||||
const input = manifest();
|
||||
input.admin.sections[0].previewTargets.push({ key: "child", label: "姓名" });
|
||||
input.admin.sections[0].fields.push({
|
||||
binding: "brand.config.founder",
|
||||
previewTarget: "identity",
|
||||
});
|
||||
const result = diagnoseTemplate(
|
||||
input,
|
||||
renderer.replace(
|
||||
"<Text>{b.founder}</Text>",
|
||||
'<Text data-editor-target="child">{b.founder}</Text>',
|
||||
),
|
||||
);
|
||||
assert.ok(result.issues.some((i) => i.code === "REGION_FIELD_MISSING" && i.target === "child"));
|
||||
});
|
||||
test("组合根上的模板字段同样算误绑", () => {
|
||||
const input = manifest();
|
||||
input.admin.sections[0].previewTargets[0].composite = true;
|
||||
input.templateFields = [
|
||||
{ page: "home", binding: "brand.config.templateData.method", previewTarget: "identity" },
|
||||
];
|
||||
const result = diagnoseTemplate(input, renderer);
|
||||
assert.ok(
|
||||
result.issues.some((i) => i.code === "COMPOSITE_HAS_FIELDS" && i.binding.endsWith(".method")),
|
||||
);
|
||||
});
|
||||
test("跨页目标不能作为合法字段入口", () => {
|
||||
const input = manifest();
|
||||
input.templateFields = [
|
||||
{ page: "profile", binding: "brand.config.templateData.method", previewTarget: "identity" },
|
||||
];
|
||||
assert.ok(
|
||||
diagnoseTemplate(input, renderer).issues.some((i) => i.code === "FIELD_TARGET_MISSING"),
|
||||
);
|
||||
});
|
||||
test("信任子目标必须声明页面,品牌字段不可归给 trust 记录编辑器", () => {
|
||||
const input = manifest();
|
||||
input.trustContentTypes = [
|
||||
{
|
||||
type: "credential",
|
||||
pages: ["home", "profile"],
|
||||
previewTargets: [{ key: "stats" }],
|
||||
configFields: [],
|
||||
},
|
||||
];
|
||||
input.admin.sections[0].fields.push({ binding: "brand.description", previewTarget: "stats" });
|
||||
const result = diagnoseTemplate(input, renderer);
|
||||
assert.ok(result.issues.some((i) => i.code === "TARGET_PAGE_UNSPECIFIED"));
|
||||
assert.ok(result.issues.some((i) => i.code === "TARGET_SOURCE_MISMATCH"));
|
||||
});
|
||||
test("动态路径保留 review,不能静默绿色", () => {
|
||||
const result = diagnoseTemplate(manifest(), renderer.replace("b.founder", "b[key]"));
|
||||
assert.ok(
|
||||
result.issues.some((i) => i.code === "DYNAMIC_VALUE_REVIEW" && i.severity === "review"),
|
||||
);
|
||||
});
|
||||
test("记录区域缺失 recordId 会报告", () => {
|
||||
const input = manifest();
|
||||
input.admin.sections[0].target = "offering";
|
||||
input.admin.sections[0].fields = [{ binding: "offering.name", previewTarget: "identity" }];
|
||||
assert.ok(diagnoseTemplate(input, renderer).issues.some((i) => i.code === "RECORD_ID_MISSING"));
|
||||
});
|
||||
test("section 为 inquiry 但字段全是品牌时按运行时推导品牌来源", () => {
|
||||
const input = manifest();
|
||||
input.admin.sections[0].target = "inquiry";
|
||||
input.admin.sections[0].fields.push({
|
||||
binding: "brand.config.founder",
|
||||
previewTarget: "identity",
|
||||
});
|
||||
assert.equal(diagnoseTemplate(input, renderer).staticStatus, "pass");
|
||||
});
|
||||
test("同一区域混合记录和品牌字段不能豁免", () => {
|
||||
const input = manifest();
|
||||
input.admin.sections[0].fields.push({ binding: "offering.name", previewTarget: "identity" });
|
||||
assert.ok(
|
||||
diagnoseTemplate(input, renderer).issues.some((i) => i.code === "TARGET_SOURCE_MISMATCH"),
|
||||
);
|
||||
});
|
||||
test("箭头组件不能被跳过而静默通过", () => {
|
||||
assert.ok(
|
||||
diagnoseTemplate(
|
||||
manifest(),
|
||||
'const Home = () => <View data-editor-target="identity">{brand.founder}</View>',
|
||||
).issues.some((i) => i.code === "UNSUPPORTED_COMPONENT_REVIEW"),
|
||||
);
|
||||
});
|
||||
test("条件只控制子区显示时不误判成父区展示字段", () => {
|
||||
const input = manifest();
|
||||
const source =
|
||||
'function Home({workspace}) { const b=workspace.brand; return <View data-editor-target="identity">{b.avatarUrl && <Text>头像</Text>}</View>; }';
|
||||
const result = diagnoseTemplate(input, source);
|
||||
assert.ok(result.issues.some((i) => i.code === "CONDITIONAL_RENDER_REVIEW"));
|
||||
assert.ok(!result.issues.some((i) => i.code === "REGION_FIELD_MISSING"));
|
||||
});
|
||||
test("JSON 合法但 TSX 损坏不能通过诊断", () => {
|
||||
assert.ok(
|
||||
diagnoseTemplate(manifest(), "function Home( { return <View>").issues.some(
|
||||
(i) => i.code === "RENDERER_PARSE_ERROR",
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test("循环业务卡片追踪实际字段,删除字段会失败", () => {
|
||||
const input = manifest();
|
||||
input.admin.sections[0].target = "offering";
|
||||
input.admin.sections[0].fields = [{ binding: "offering.name", previewTarget: "identity" }];
|
||||
const source =
|
||||
'function Home({workspace}) { return <View>{workspace.offerings.map(offering).map(item => <View data-editor-target="identity" data-editor-record-id={item.id}>{item.name}{item.area}</View>)}</View>; }';
|
||||
const result = diagnoseTemplate(input, source);
|
||||
assert.ok(
|
||||
result.issues.some(
|
||||
(i) => i.code === "REGION_FIELD_MISSING" && i.binding === "offering.config.area",
|
||||
),
|
||||
);
|
||||
input.admin.sections[0].fields.push({
|
||||
binding: "offering.config.area",
|
||||
previewTarget: "identity",
|
||||
});
|
||||
assert.ok(!diagnoseTemplate(input, source).issues.some((i) => i.code === "REGION_FIELD_MISSING"));
|
||||
});
|
||||
|
||||
test("结构化信任列表追踪子字段,不跨循环混用别名", () => {
|
||||
const input = manifest();
|
||||
input.admin.sections[0].target = "trust";
|
||||
input.admin.sections[0].fields = [{ binding: "trust.config.stats", previewTarget: "identity" }];
|
||||
const source =
|
||||
'function Home({workspace}) { const t=workspace.trustPresentation; return <View data-editor-target="identity" data-editor-record-id="fixture">{t.stats.map(item => <Text>{item.value}</Text>)}</View>; }';
|
||||
const result = diagnoseTemplate(input, source);
|
||||
assert.ok(result.regions[0].bindings.includes("trust.config.stats.value"));
|
||||
assert.ok(!result.issues.some((i) => i.code === "REGION_FIELD_MISSING"));
|
||||
});
|
||||
|
||||
test("动态索引保留待核查,但不把容器对象误当可编辑字段", () => {
|
||||
const input = manifest();
|
||||
const source =
|
||||
'function Home({workspace}) { const b=workspace.brand; return <View data-editor-target="identity">{b.config[key]}</View>; }';
|
||||
const result = diagnoseTemplate(input, source);
|
||||
assert.ok(result.issues.some((i) => i.code === "DYNAMIC_VALUE_REVIEW"));
|
||||
assert.ok(
|
||||
!result.issues.some((i) => i.code === "REGION_FIELD_MISSING" && i.binding === "brand.config"),
|
||||
);
|
||||
});
|
||||
|
||||
test("列表声明存在仍须检查每个展示子字段", () => {
|
||||
const input = manifest();
|
||||
input.admin.sections[0].target = "trust";
|
||||
input.admin.sections[0].fields = [
|
||||
{
|
||||
binding: "trust.config.stats",
|
||||
previewTarget: "identity",
|
||||
input: "list",
|
||||
itemFields: [{ key: "value" }],
|
||||
},
|
||||
];
|
||||
const source =
|
||||
'function Home({workspace}) { const t=workspace.trustPresentation; return <View data-editor-target="identity" data-editor-record-id="fixture">{t.stats.map(item => <Text>{item.value}{item.sub}</Text>)}</View>; }';
|
||||
assert.ok(
|
||||
diagnoseTemplate(input, source).issues.some(
|
||||
(i) => i.code === "LIST_FIELD_MISSING" && i.binding === "trust.config.stats.sub",
|
||||
),
|
||||
);
|
||||
input.admin.sections[0].fields[0].itemFields.push({ key: "sub" });
|
||||
assert.ok(!diagnoseTemplate(input, source).issues.some((i) => i.code === "LIST_FIELD_MISSING"));
|
||||
});
|
||||
Reference in new issue
Block a user