新增运行时 openPage/takePageQuery,tabBar 页自动使用 switchTab 并传递参数 - 编译器写入 tabPages,并拒绝 navigateTo/redirectTo 到 tabBar 页或 switchTab 带参数 - 阿成首页、套餐、出摊、主理人、详情、记录、成功页跳转统一改用 openPage - 套餐筛选改用后台业务提供项分类;详情跳咨询时携带业务提供项 - 移除 capability-booking:小程序无入口且依赖已删除的 /public/brand,需求由咨询表单覆盖"
240 lines
10 KiB
JavaScript
240 lines
10 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { execFileSync } from "node:child_process";
|
|
import { fileURLToPath } from "node:url";
|
|
import test from "node:test";
|
|
|
|
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
const workspaceRoot = path.resolve(packageRoot, "../..");
|
|
const compiler = path.join(packageRoot, "src/cli.mjs");
|
|
|
|
function assertPageBundle(output, app) {
|
|
for (const page of app.pages) {
|
|
for (const extension of [".js", ".json", ".wxml", ".wxss"])
|
|
assert.equal(fs.existsSync(path.join(output, page + extension)), true, `${page}${extension}`);
|
|
}
|
|
}
|
|
|
|
test("compiler generates an isolated Acheng runtime manifest", () => {
|
|
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
|
|
execFileSync(process.execPath, [compiler, "--template", "acheng-campus-food", "--out", output], {
|
|
cwd: workspaceRoot,
|
|
stdio: "inherit",
|
|
});
|
|
const app = JSON.parse(fs.readFileSync(path.join(output, "app.json"), "utf8"));
|
|
const runtime = JSON.parse(fs.readFileSync(path.join(output, "template.runtime.json"), "utf8"));
|
|
assert.equal(app.window.navigationBarTitleText, "阿成校园餐");
|
|
assert.equal(runtime.packageKey, "acheng-campus-food");
|
|
assert.equal(runtime.entrypoints.home, "pages/index/index");
|
|
assert.match(fs.readFileSync(path.join(output, "runtime-config.js"), "utf8"), /default-brand/);
|
|
assert.equal(runtime.navigation.length, 5);
|
|
assert.equal(runtime.navigation[2].page, "service");
|
|
assert.equal(
|
|
JSON.parse(fs.readFileSync(path.join(output, "app.json"), "utf8")).tabBar.list.length,
|
|
5,
|
|
);
|
|
assertPageBundle(output, app);
|
|
assert.equal(fs.existsSync(path.join(output, "components/runtime-tab-bar")), false);
|
|
assert.equal(fs.existsSync(path.join(output, "components/templates/linjing-home")), false);
|
|
assert.equal(
|
|
fs.existsSync(path.join(output, "components/templates/acheng-home/acheng-home.wxml")),
|
|
true,
|
|
);
|
|
assert.equal(fs.existsSync(path.join(output, "pages/stall-service/index.wxml")), true);
|
|
assert.equal(fs.existsSync(path.join(output, "pages/booking")), false);
|
|
assert.equal(fs.existsSync(path.join(output, "assets/tab/booking.png")), true);
|
|
// 运行时的 npm 构建产物必须完整复制,不能被按名称排除的 src/test/dist 误伤。
|
|
const runtimeNpm = path.join(workspaceRoot, "apps/miniapp/miniprogram_npm");
|
|
if (fs.existsSync(runtimeNpm)) {
|
|
for (const file of fs.readdirSync(runtimeNpm, { recursive: true })) {
|
|
if (fs.statSync(path.join(runtimeNpm, file)).isFile())
|
|
assert.equal(fs.existsSync(path.join(output, "miniprogram_npm", file)), true, file);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("compiler generates the Linjing navigation contract", () => {
|
|
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
|
|
execFileSync(process.execPath, [compiler, "--template", "linjing-editorial", "--out", output], {
|
|
cwd: workspaceRoot,
|
|
stdio: "inherit",
|
|
});
|
|
const runtime = JSON.parse(fs.readFileSync(path.join(output, "template.runtime.json"), "utf8"));
|
|
assert.equal(runtime.packageKey, "linjing-editorial");
|
|
assert.equal(runtime.entrypoints.home, "pages/index/index");
|
|
assert.match(fs.readFileSync(path.join(output, "runtime-config.js"), "utf8"), /linjing-doors/);
|
|
const app = JSON.parse(fs.readFileSync(path.join(output, "app.json"), "utf8"));
|
|
assertPageBundle(output, app);
|
|
assert.equal(runtime.navigation.length, 3);
|
|
assert.equal(runtime.navigation[0].iconPath, "assets/tab/home.png");
|
|
assert.equal(
|
|
JSON.parse(fs.readFileSync(path.join(output, "app.json"), "utf8")).tabBar.list.length,
|
|
3,
|
|
);
|
|
assert.equal(fs.existsSync(path.join(output, "assets/tab/works.png")), true);
|
|
assert.equal(
|
|
fs.existsSync(path.join(output, "components/templates/linjing-home/linjing-home.wxml")),
|
|
true,
|
|
);
|
|
assert.equal(fs.existsSync(path.join(output, "pages/stall-service")), false);
|
|
assert.equal(fs.existsSync(path.join(output, "pages/booking")), false);
|
|
assert.doesNotMatch(
|
|
fs.readFileSync(path.join(output, "app.json"), "utf8"),
|
|
/stall-service|booking\/index/,
|
|
);
|
|
assert.equal(fs.existsSync(path.join(output, "pages/services/index.wxml")), true);
|
|
});
|
|
|
|
test("release manifest pins every workspace template version", () => {
|
|
const releaseConfig = JSON.parse(
|
|
fs.readFileSync(path.join(workspaceRoot, "config/template-build.json"), "utf8"),
|
|
);
|
|
assert.equal(releaseConfig.runtimeContract, "1.0");
|
|
for (const entry of releaseConfig.templates) {
|
|
const manifest = JSON.parse(
|
|
fs.readFileSync(
|
|
path.join(workspaceRoot, `packages/template-${entry.packageKey}/template.json`),
|
|
"utf8",
|
|
),
|
|
);
|
|
assert.equal(manifest.version, entry.version);
|
|
assert.equal(manifest.runtimeContract, releaseConfig.runtimeContract);
|
|
}
|
|
});
|
|
|
|
test("compiler rejects production builds without deployment parameters", () => {
|
|
assert.throws(
|
|
() =>
|
|
execFileSync(
|
|
process.execPath,
|
|
[compiler, "--template", "linjing-editorial", "--env", "prod"],
|
|
{ cwd: workspaceRoot },
|
|
),
|
|
/Production build requires --brand, --appid, --api/,
|
|
);
|
|
});
|
|
|
|
test("a second-industry template compiles without compiler or runtime changes", () => {
|
|
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
|
|
execFileSync(
|
|
process.execPath,
|
|
[
|
|
compiler,
|
|
"--template-dir",
|
|
path.join(packageRoot, "test/fixtures/template-drill-consultant"),
|
|
"--out",
|
|
output,
|
|
],
|
|
{ cwd: workspaceRoot, stdio: "inherit" },
|
|
);
|
|
const app = JSON.parse(fs.readFileSync(path.join(output, "app.json"), "utf8"));
|
|
assertPageBundle(output, app);
|
|
assert.equal(app.pages[0], "pages/index/index");
|
|
for (const page of ["pages/offering/detail", "pages/inquiry/index", "pages/records/index"])
|
|
assert.equal(app.pages.includes(page), true, page);
|
|
assert.deepEqual(
|
|
app.tabBar.list.map((item) => item.pagePath),
|
|
["pages/index/index", "pages/services/index", "pages/profile/index"],
|
|
);
|
|
assert.equal(fs.existsSync(path.join(output, "pages/booking")), false);
|
|
assert.deepEqual(fs.readdirSync(path.join(output, "components/templates")), ["drill-card"]);
|
|
assert.match(fs.readFileSync(path.join(output, "runtime-config.js"), "utf8"), /drill-consultant/);
|
|
});
|
|
|
|
function copyTemplate(key) {
|
|
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-package-"));
|
|
fs.cpSync(path.join(workspaceRoot, `packages/template-${key}`), directory, { recursive: true });
|
|
return directory;
|
|
}
|
|
|
|
test("compiler rejects template files that shadow runtime files without overrides", () => {
|
|
const templateDirectory = copyTemplate("linjing-editorial");
|
|
fs.cpSync(
|
|
path.join(workspaceRoot, "apps/miniapp/pages/records"),
|
|
path.join(templateDirectory, "miniapp/pages/records"),
|
|
{ recursive: true },
|
|
);
|
|
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
|
|
assert.throws(
|
|
() =>
|
|
execFileSync(
|
|
process.execPath,
|
|
[compiler, "--template-dir", templateDirectory, "--out", output],
|
|
{ cwd: workspaceRoot, stdio: "pipe" },
|
|
),
|
|
/conflicts with runtime at pages\/records\/index\.(js|json|wxml|wxss); declare it in overrides/,
|
|
);
|
|
});
|
|
|
|
test("compiler lets a template replace a runtime page declared in overrides", () => {
|
|
const templateDirectory = copyTemplate("linjing-editorial");
|
|
const pageDirectory = path.join(templateDirectory, "miniapp/pages/records");
|
|
fs.cpSync(path.join(workspaceRoot, "apps/miniapp/pages/records"), pageDirectory, {
|
|
recursive: true,
|
|
});
|
|
fs.appendFileSync(path.join(pageDirectory, "index.wxml"), "<!-- template override -->\n");
|
|
const manifestPath = path.join(templateDirectory, "template.json");
|
|
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
manifest.overrides = [...(manifest.overrides || []), "pages/records"];
|
|
fs.writeFileSync(manifestPath, JSON.stringify(manifest));
|
|
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
|
|
execFileSync(process.execPath, [compiler, "--template-dir", templateDirectory, "--out", output], {
|
|
cwd: workspaceRoot,
|
|
stdio: "pipe",
|
|
});
|
|
assert.match(
|
|
fs.readFileSync(path.join(output, "pages/records/index.wxml"), "utf8"),
|
|
/template override/,
|
|
);
|
|
});
|
|
|
|
test("compiler rejects navigateTo calls that target tabBar pages", () => {
|
|
const templateDirectory = copyTemplate("linjing-editorial");
|
|
fs.appendFileSync(
|
|
path.join(templateDirectory, "miniapp/pages/index/index.js"),
|
|
'\nfunction broken() { wx.navigateTo({ url: "/pages/services/index" }); }\n',
|
|
);
|
|
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
|
|
assert.throws(
|
|
() =>
|
|
execFileSync(
|
|
process.execPath,
|
|
[compiler, "--template-dir", templateDirectory, "--out", output],
|
|
{ cwd: workspaceRoot, stdio: "pipe" },
|
|
),
|
|
/wx\.navigateTo cannot open tabBar page pages\/services\/index/,
|
|
);
|
|
});
|
|
|
|
test("compiler writes tabBar pages into runtime-config for openPage", () => {
|
|
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
|
|
execFileSync(process.execPath, [compiler, "--template", "acheng-campus-food", "--out", output], {
|
|
cwd: workspaceRoot,
|
|
stdio: "pipe",
|
|
});
|
|
const config = fs.readFileSync(path.join(output, "runtime-config.js"), "utf8");
|
|
assert.match(config, /"tabPages"/);
|
|
assert.match(config, /"pages\/inquiry\/index"/);
|
|
assert.equal(fs.existsSync(path.join(output, "utils/navigation.js")), true);
|
|
});
|
|
|
|
test("compiler rejects unknown capabilities before copying layers", () => {
|
|
const templateDirectory = copyTemplate("linjing-editorial");
|
|
const manifestPath = path.join(templateDirectory, "template.json");
|
|
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
manifest.requiredCapabilities.push("missing");
|
|
fs.writeFileSync(manifestPath, JSON.stringify(manifest));
|
|
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
|
|
assert.throws(
|
|
() =>
|
|
execFileSync(
|
|
process.execPath,
|
|
[compiler, "--template-dir", templateDirectory, "--out", output],
|
|
{ cwd: workspaceRoot },
|
|
),
|
|
/requires unknown capability: missing/,
|
|
);
|
|
});
|