Files
persionalBrand/packages/template-compiler/test/cli.test.mjs
T

127 lines
5.0 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");
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.renderers.home, "acheng-home");
assert.match(fs.readFileSync(path.join(output, "app.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,
);
assert.equal(fs.existsSync(path.join(output, "components/runtime-tab-bar")), false);
assert.equal(
fs
.readFileSync(path.join(output, "pages/index/index.wxml"), "utf8")
.includes("runtime-tab-bar"),
false,
);
assert.equal(
fs.existsSync(path.join(output, "components/templates/acheng-home/acheng-home.wxml")),
false,
);
assert.equal(fs.existsSync(path.join(output, "components/template-pages/home/home.wxml")), true);
assert.equal(fs.existsSync(path.join(output, "components/templates")), false);
assert.match(
fs.readFileSync(path.join(output, "pages/index/index.wxml"), "utf8"),
/<template-home/,
);
assert.doesNotMatch(
fs.readFileSync(path.join(output, "pages/index/index.wxml"), "utf8"),
/acheng-home/,
);
assert.equal(
fs.readFileSync(path.join(output, "pages/index/index.wxml"), "utf8").trim().length > 20,
true,
);
assert.doesNotMatch(
fs.readFileSync(path.join(output, "pages/index/index.wxml"), "utf8"),
/pageRenderer ===/,
);
assert.equal(fs.existsSync(path.join(output, "pages/stall-service/index.wxml")), true);
assert.match(
fs.readFileSync(path.join(output, "pages/services/index.wxml"), "utf8"),
/<template-offerings/,
);
assert.match(
fs.readFileSync(path.join(output, "pages/bookings/index.wxml"), "utf8"),
/<template-profile/,
);
assert.equal(
fs
.readFileSync(path.join(output, "pages/inquiry/index.wxml"), "utf8")
.includes("runtime-tab-bar"),
false,
);
});
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.renderers.home, "linjing-home");
assert.equal(runtime.renderers.offerings, "linjing-offerings");
assert.match(fs.readFileSync(path.join(output, "app.js"), "utf8"), /linjing-doors/);
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")),
false,
);
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.match(
fs.readFileSync(path.join(output, "pages/services/index.wxml"), "utf8"),
/<template-offerings/,
);
});
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);
}
});