import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import vm from 'node:vm' const source = readFileSync(new URL('../miniapp/pages/index/index.ts', import.meta.url), 'utf8') let page const sandbox = { require(request) { if (request === '../../stores') { return { authStore: { state: { isLoggedIn: false } }, appStore: { toastSuccess() {} }, } } if (request === '../../utils/api') { return { get: async () => { throw new Error('未登录时不应请求首页接口') } } } throw new Error(`未预期依赖:${request}`) }, Page(config) { page = config }, wx: { getAccountInfoSync() { return { miniProgram: { envVersion: 'develop' } } }, }, console, setTimeout, } vm.runInNewContext(source, sandbox, { filename: 'miniapp/pages/index/index.ts' }) assert.ok(page, '首页必须注册 Page 配置') const pageState = { ...page.data } await page.checkAuthAndLoad.call({ data: pageState, setData(data) { Object.assign(pageState, data) }, loadHomeData() { throw new Error('未登录时不应进入首页数据加载流程') }, }) assert.equal(pageState.isOnboarding, true, '未登录时应进入登录引导状态') assert.equal(pageState.userState, null, '未登录状态不得伪造实名用户,实名状态必须来自服务端数据库') console.log('Phase 03 verification contracts passed')