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, "测试品牌"); });