Files
persionalBrand/scripts/template-compatibility.test.mjs
que01 0d9f4a7d6f
CI / Static checks (push) Has been cancelled
CI / Production build (push) Has been cancelled
CI / API integration (push) Has been cancelled
CI / Taro builds (acheng-campus-food) (push) Has been cancelled
CI / Taro builds (atelier-solopreneur) (push) Has been cancelled
CI / Taro builds (linjing-editorial) (push) Has been cancelled
CI / Browser regression (push) Has been cancelled
refactor: 拆分品牌业务类型与页面方案
2026-10-20 17:04:30 +08:00

116 lines
4.7 KiB
JavaScript

import assert from "node:assert/strict";
import { execFileSync, spawnSync } from "node:child_process";
import { cpSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import test from "node:test";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const script = path.join(root, "scripts/template-compatibility.cjs");
test("compatibility report discovers deployed package, industry, visual and renderer combinations", () => {
const report = JSON.parse(execFileSync(process.execPath, [script, "--json"], { cwd: root }));
assert.equal(report.schemaVersion, 1);
assert.ok(report.packages.length >= 3);
assert.ok(report.packages.every((item) => item.status === "supported"));
assert.ok(
report.packages.some(
(item) => item.packageKey === "linjing-editorial" && item.industry === "doors",
),
);
assert.equal(
report.packages.some(
(item) => item.packageKey === "linjing-editorial" && item.industry === "personal",
),
false,
"林境建筑画册不得出现在 personal 兼容矩阵中",
);
});
test("compatibility check rejects renderer, capability and toolchain incompatibilities", () => {
const packagesRoot = path.join(root, "packages");
const sourcePath = path.join(packagesRoot, "template-atelier-solopreneur/template.json");
const source = JSON.parse(readFileSync(sourcePath, "utf8"));
const directory = mkdtempSync(path.join(os.tmpdir(), "template-compatibility-"));
const original = {
...source,
packageKey: "compatibility-probe",
visualTemplateKey: "missing-visual",
requiredCapabilities: ["booking", "unknown-capability"],
};
try {
const scriptSource = readFileSync(script, "utf8");
const probeRoot = path.join(directory, "repo");
const probePackages = path.join(probeRoot, "packages");
const probeConfig = path.join(probePackages, "config/src");
const probeRenderer = path.join(probeRoot, "apps/admin/src/features/brand");
const probeTaro = path.join(probeRoot, "apps/taro");
mkdirSync(probeConfig, { recursive: true });
mkdirSync(probeRenderer, { recursive: true });
mkdirSync(probeTaro, { recursive: true });
cpSync(
path.join(packagesRoot, "template-atelier-solopreneur"),
path.join(probePackages, "template-compatibility-probe"),
{ recursive: true },
);
writeFileSync(
path.join(probePackages, "template-compatibility-probe/template.json"),
JSON.stringify(original),
);
cpSync(
path.join(root, "packages/config/src/brand-templates.ts"),
path.join(probeConfig, "brand-templates.ts"),
);
cpSync(
path.join(root, "packages/config/src/visual-templates.ts"),
path.join(probeConfig, "visual-templates.ts"),
);
cpSync(
path.join(root, "packages/config/src/capability-contracts.ts"),
path.join(probeConfig, "capability-contracts.ts"),
);
const platformPackage = JSON.parse(
readFileSync(path.join(root, "apps/taro/package.json"), "utf8"),
);
writeFileSync(
path.join(probeTaro, "package.json"),
JSON.stringify({
...platformPackage,
dependencies: { ...platformPackage.dependencies, "@tarojs/taro": "5.0.0" },
}),
);
cpSync(
path.join(root, "apps/admin/src/features/brand/component-renderers.jsx"),
path.join(probeRenderer, "component-renderers.jsx"),
);
const probeScript = path.join(probeRoot, "scripts/template-compatibility.cjs");
mkdirSync(path.dirname(probeScript), { recursive: true });
writeFileSync(
probeScript,
scriptSource.replace(
'require("typescript")',
`require(${JSON.stringify(path.join(root, "node_modules/typescript"))})`,
),
);
const result = spawnSync(process.execPath, [probeScript, "--json"], {
encoding: "utf8",
cwd: probeRoot,
});
assert.equal(result.status, 1, result.stdout);
const report = JSON.parse(result.stdout);
assert.match(result.stderr, /unknown capabilities unknown-capability/);
assert.ok(report.packages.some((item) => item.packageKey === "compatibility-probe"));
const probe = report.packages.find((item) => item.packageKey === "compatibility-probe");
assert.ok(
probe.status === "rejected" &&
probe.reasons.includes("platform visual renderer is not registered") &&
probe.reasons.includes("industry does not declare capability booking") &&
probe.reasons.some((reason) => reason.includes("toolchain mismatch: @tarojs/taro")),
JSON.stringify(probe),
);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});