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) }; 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("运行时保留模板业务数据,不再解释视觉模块", async () => { const workspace = await loadWithResponse({ publication: { status: "published" }, brand: { name: "测试品牌" }, offerings: [], trustPresentation: { bio: "品牌介绍" }, inquiryForms: [], modules: [{ key: "brand-hero" }], visualTemplateManifest: { pages: [] }, }); assert.deepEqual(workspace.trustPresentation, { bio: "品牌介绍" }); assert.equal(workspace.fields, undefined); assert.equal(workspace.inquiryForm, undefined); assert.equal(workspace.modules[0].key, "brand-hero"); assert.deepEqual(workspace.visualTemplateManifest, { pages: [] }); }); 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, "隐藏式地轴"); });