84 lines
2.6 KiB
JavaScript
84 lines
2.6 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const test = require("node:test");
|
|
const vm = require("node:vm");
|
|
|
|
const source = fs.readFileSync(path.join(__dirname, "../utils/runtime-workspace.js"), "utf8");
|
|
|
|
function loadWithResponse(response) {
|
|
const module = { exports: {} };
|
|
const storage = {
|
|
getStorageSync() {
|
|
throw new Error("不应读取旧缓存");
|
|
},
|
|
setStorageSync() {
|
|
throw new Error("不应缓存公开内容");
|
|
},
|
|
};
|
|
vm.runInNewContext(source, {
|
|
module,
|
|
wx: storage,
|
|
require(id) {
|
|
if (id === "./request") return { request: () => Promise.resolve(response) };
|
|
if (id === "./module-registry") return { resolveRuntime: () => ({}) };
|
|
throw new Error(`意外依赖:${id}`);
|
|
},
|
|
});
|
|
return module.exports.loadWorkspaceWithFallback();
|
|
}
|
|
|
|
test("未发布品牌不会使用旧 workspace 缓存", async () => {
|
|
await assert.rejects(
|
|
loadWithResponse({ publication: { status: "unpublished" }, brand: null }),
|
|
/品牌尚未发布/,
|
|
);
|
|
});
|
|
|
|
test("已发布 workspace 正常返回", async () => {
|
|
const workspace = await loadWithResponse({
|
|
publication: { status: "published" },
|
|
brand: { name: "测试品牌" },
|
|
offerings: [],
|
|
});
|
|
assert.equal(workspace.brand.name, "测试品牌");
|
|
});
|
|
|
|
test("运行时只消费 canonical workspace 字段", async () => {
|
|
const workspace = await loadWithResponse({
|
|
publication: { status: "published" },
|
|
brand: { name: "测试品牌" },
|
|
offerings: [],
|
|
trustPresentation: { bio: "品牌介绍" },
|
|
inquiryForms: [],
|
|
});
|
|
assert.deepEqual(workspace.trustContent, { bio: "品牌介绍" });
|
|
assert.equal(workspace.fields, undefined);
|
|
assert.equal(workspace.inquiryForm, undefined);
|
|
});
|
|
|
|
test("模板声明的业务提供项展示字段会保留在小程序 view model", async () => {
|
|
const workspace = await loadWithResponse({
|
|
publication: { status: "published" },
|
|
brand: { name: "测试品牌" },
|
|
offerings: [
|
|
{
|
|
id: "offering-1",
|
|
name: "别墅入户门",
|
|
summary: "山庄项目",
|
|
coverUrl: "/cover.jpg",
|
|
priceText: "按项目报价",
|
|
presentation: {
|
|
categoryLabel: "入户大门",
|
|
series: "万籁系列",
|
|
area: "莫干山",
|
|
hardware: "隐藏式地轴",
|
|
},
|
|
},
|
|
],
|
|
});
|
|
assert.equal(workspace.offerings[0].series, "万籁系列");
|
|
assert.equal(workspace.offerings[0].area, "莫干山");
|
|
assert.equal(workspace.offerings[0].hardware, "隐藏式地轴");
|
|
});
|