49 lines
2.2 KiB
JavaScript
49 lines
2.2 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const { execFileSync } = require("node:child_process");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const { pathToFileURL } = require("node:url");
|
|
const { test } = require("node:test");
|
|
|
|
const root = path.resolve(__dirname, "..");
|
|
const configs = require("../client-configs.json");
|
|
|
|
for (const client of ["zhinian", "niannian", "nianhelper", "duohua", "tianmu"]) {
|
|
test(`switch ${client}: configuration, identity and theme stay consistent`, async (t) => {
|
|
const fixture = fs.mkdtempSync(path.join(os.tmpdir(), "client-switch-"));
|
|
t.after(() => fs.rmSync(fixture, { recursive: true, force: true }));
|
|
for (const file of [
|
|
"client-configs.json",
|
|
"project.config.json",
|
|
"src/manifest.json",
|
|
"src/constant/base.js",
|
|
"src/uni.scss",
|
|
]) {
|
|
const target = path.join(fixture, file);
|
|
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
fs.copyFileSync(path.join(root, file), target);
|
|
}
|
|
fs.mkdirSync(path.join(fixture, "scripts"));
|
|
fs.copyFileSync(
|
|
path.join(__dirname, "update-appid.js"),
|
|
path.join(fixture, "scripts/update-appid.cjs")
|
|
);
|
|
fs.writeFileSync(path.join(fixture, "package.json"), '{"type":"module"}');
|
|
execFileSync(process.execPath, [path.join(fixture, "scripts/update-appid.cjs"), client]);
|
|
|
|
const base = await import(pathToFileURL(path.join(fixture, "src/constant/base.js")).href);
|
|
assert.deepEqual(base.getCurrentConfig(), configs[client]);
|
|
assert.equal(base.currentClientType(), client.toUpperCase());
|
|
assert.equal(base.isZhiNian, client === "zhinian");
|
|
const project = JSON.parse(fs.readFileSync(path.join(fixture, "project.config.json"), "utf8"));
|
|
assert.equal(project.appid, configs[client].appId);
|
|
const manifest = fs.readFileSync(path.join(fixture, "src/manifest.json"), "utf8");
|
|
assert.equal(manifest.match(/"mp-weixin":\s*{[^}]*"appid":\s*"([^"]*)"/)[1], configs[client].appId);
|
|
const scss = fs.readFileSync(path.join(fixture, "src/uni.scss"), "utf8");
|
|
for (const [key, value] of Object.entries(configs[client].theme)) {
|
|
assert.ok(scss.includes(`$${key}: ${value};`), `${key} should be ${value}`);
|
|
}
|
|
});
|
|
}
|