import assert from 'node:assert/strict'; import test from 'node:test'; import vm from 'node:vm'; import { readFileSync } from 'node:fs'; import { resolve, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { createRequire } from 'node:module'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const require = createRequire(import.meta.url); const vue = require('vue'); const plain = (value) => JSON.parse(JSON.stringify(value)); const deferred = () => { let resolvePromise; const promise = new Promise(resolve => { resolvePromise = resolve; }); return { promise, resolve: resolvePromise }; }; // Execute actual store/API modules and page setup scripts with real Vue // reactivity. Only native uni APIs and lifecycle scheduling are substituted. function runtime(initial = {}) { const storage = new Map(Object.entries({ token: 'session-A', identifier: 'A', ...initial })); const calls = []; const requests = []; const mounted = []; const shown = []; const hidden = []; const unmounted = []; const timers = []; const sessionStorage = new Map(); const api = {}; const props = vue.reactive({}); const payment = { consumePendingPaymentRefresh: () => false, PAYMENT_REFRESH_TARGETS: { HOME: 'home', PACKAGE_ORDER: 'package-order', ORDER_LIST: 'order-list', MY_WALLET: 'my-wallet' }, handlePaymentError() {}, isValidAlipayPaymentLink: () => false, isValidWechatPayConfig: () => false, markPendingPaymentRefresh() {}, openAlipayPayment: async () => {}, showPaymentToast() {}, wechatH5Pay: async () => {} }; let inWechat = false; let sdkInit = async () => {}; let options = {}; const uni = { getStorageSync: key => storage.get(key) ?? '', setStorageSync: (key, value) => storage.set(key, plain(value)), removeStorageSync: key => storage.delete(key), clearStorageSync: () => storage.clear(), request: request => { requests.push(request); }, showToast: value => calls.push({ kind: 'toast', ...value }), showModal: value => calls.push({ kind: 'modal', ...value }), showLoading: value => calls.push({ kind: 'loading', ...value }), hideLoading: () => calls.push({ kind: 'hideLoading' }), reLaunch: value => calls.push({ kind: 'reLaunch', ...value }), redirectTo: value => calls.push({ kind: 'redirectTo', ...value }), navigateTo: value => calls.push({ kind: 'navigateTo', ...value }), navigateBack: () => calls.push({ kind: 'navigateBack' }) }; const context = vm.createContext({ uni, Intl, Date, URLSearchParams, URL, console: { log() {}, error() {}, warn() {} }, setTimeout: callback => { timers.push(callback); return timers.length; }, setInterval: () => 1, clearInterval() {}, getCurrentPages: () => [{ options }], sessionStorage: { getItem: key => sessionStorage.get(key) || '', removeItem: key => sessionStorage.delete(key), setItem: (key, value) => sessionStorage.set(key, value) }, window: { location: { search: '', pathname: '/', hash: '', origin: 'https://example.invalid' }, history: { replaceState() {} } }, defineProps: () => props, defineEmits: () => () => {}, defineExpose() {} }); const cache = new Map(); const synthetic = (key, values) => { if (!cache.has(key)) { cache.set(key, new vm.SyntheticModule(Object.keys(values), function () { for (const [name, value] of Object.entries(values)) this.setExport(name, value); }, { context, identifier: key })); } return cache.get(key); }; function moduleFor(path, expose = []) { if (cache.has(path)) return cache.get(path); let source = readFileSync(path, 'utf8'); if (path.endsWith('.vue')) { source = source.match(/