// U0 client-build verification: the bundled client must keep the // __ModuleLoader__ contract, load like the legacy hand-written file, and // register the same settings seats with the same option shapes. import { execFileSync } from 'node:child_process' import { existsSync, readFileSync } from 'node:fs' import { createRequire } from 'node:module' import vm from 'node:vm' const require = createRequire(import.meta.url) const React = require('react') const results = [] function check(name, ok, detail = '') { results.push({ name, ok }) console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail === '' ? '' : ' :: ' + detail)) } const bundle = readFileSync(new URL('./client/index.js', import.meta.url), 'utf8') check('client bundle uses the __ModuleLoader__ contract', bundle.includes('__ModuleLoader__.load')) check('client bundle id stays dsh-brain', /id:\s*['"]dsh-brain['"]/.test(bundle)) for (const seat of ['settings.section', 'plugins.bundle.config']) { check('client bundle registers ' + seat, bundle.includes(seat)) } for (const label of ['连接与上报', '技能管理', 'MCP 管理']) { check('client bundle keeps tab label ' + label, bundle.includes(label)) } check('client bundle is a single file with only react externals', (() => { const requires = [...bundle.matchAll(/require\(\s*['"]([^'"]+)['"]\s*\)/g)].map((match) => match[1]) return requires.every((name) => name === 'react' || name === 'react/jsx-runtime') })()) function loadBundle(file) { const code = readFileSync(file, 'utf8') let definition = null const sandbox = { window: { __ModuleLoader__: { load(value) { definition = value }, }, localStorage: { getItem: () => null, setItem: () => {} }, open: () => undefined, }, console, setTimeout, clearTimeout, fetch: async () => { throw new Error('fetch not available in verification') }, AbortController, } sandbox.window.window = sandbox.window sandbox.globalThis = sandbox vm.runInNewContext(code, sandbox, { filename: String(file) }) if (definition === null) throw new Error('bundle did not register with __ModuleLoader__: ' + file) return definition.factory((name) => { if (name === 'react') return React if (name === 'react/jsx-runtime') return require('react/jsx-runtime') throw new Error('unexpected require: ' + name) }) } function registrationShape(file) { const mod = loadBundle(file) const records = [] let component = null const ctx = { slots: { inject(seat, callback) { component = null const value = callback() records.push({ seat, value, component }) }, register(options, registeredComponent) { component = registeredComponent return options }, }, } mod.apply(ctx) return { name: mod.name, inject: mod.inject, records: records.map((entry) => ({ seat: entry.seat, name: entry.value.name, id: entry.value.id, key: entry.value.key, order: entry.value.order, label: typeof entry.value.label === 'function' ? entry.value.label() : undefined, })), components: records.map((entry) => entry.component), } } const current = registrationShape('client/index.js') check('apply registers exactly the settings seats', current.records.length === 2) check('settings.section keeps its id/order/label', JSON.stringify(current.records[0]) === JSON.stringify({ seat: 'settings.section', name: 'settings.section', id: 'dsh-brain', key: undefined, order: 100, label: '云端记忆' })) check('plugins.bundle.config keeps its key', JSON.stringify(current.records[1]) === JSON.stringify({ seat: 'plugins.bundle.config', name: 'plugins.bundle.config', id: undefined, key: 'dsh-brain', order: undefined, label: undefined })) check('module exports name dsh-brain', current.name === 'dsh-brain' && Array.isArray(current.inject) && current.inject.includes('slots')) // Each registered callback must produce a renderable React element without a DOM. let rendered = 0 for (const component of current.components) { const element = typeof component === 'function' ? React.createElement(component) : null if (element !== null && typeof element === 'object' && element.type !== undefined) rendered += 1 } check('registered components render without throwing', rendered === current.components.length) const legacyFile = new URL('./artifacts/analysis/client-legacy.js', import.meta.url) if (existsSync(legacyFile)) { const legacy = registrationShape(legacyFile) check('migration preserves apply registration shape', JSON.stringify({ name: current.name, inject: current.inject, records: current.records }) === JSON.stringify({ name: legacy.name, inject: legacy.inject, records: legacy.records }), JSON.stringify({ current, legacy })) } else { console.log('SKIP legacy comparison (artifacts/analysis/client-legacy.js missing)') } console.log('\n' + (results.every((result) => result.ok) ? 'ALL ' + results.length + ' CHECKS PASSED' : results.filter((result) => !result.ok).length + ' FAILED')) process.exit(results.every((result) => result.ok) ? 0 : 1)