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 = {}; 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 }), 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, 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: '' }, history: { replaceState() {} } }, defineProps: () => ({}), defineEmits: () => () => {} }); 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(/