/** * 验收验证:模拟 DSH host 的 resolveOverlayPackage + bundle 加载 * - 确认 apes-sidebar 在 profile node_modules 可解析 * - 确认 host entry (lib/index.js) 的命名导出 shape { name, inject, apply } * - 确认 client entry (lib/client.js) 存在且是 __ModuleLoader__ bundle(browser artifact) * - 确认 invariant surface 可用 */ const path = require('path') const fs = require('fs') const { createRequire } = require('module') const PROFILE = process.env.HOME + '/.dsh/profiles/desktop' const req = createRequire(path.join(PROFILE, 'noop.js')) let pass = 0, fail = 0 const check = (label, cond, detail = '') => { if (cond) { pass++; console.log(' ✓', label) } else { fail++; console.log(' ✗', label, detail) } } console.log('### apes-sidebar 挂载验收') // 1. 包可解析 try { const pkgPath = req.resolve('apes-sidebar/package.json') const pkg = req('apes-sidebar/package.json') check('package.json 可解析', true, pkgPath) check('name=apes-sidebar', pkg.name === 'apes-sidebar') check('version=2.0.0', pkg.version === '2.0.0') } catch (e) { check('package.json 可解析', false, e.message) } // 2. host entry 加载 + shape try { const host = req('apes-sidebar') check('host entry 加载成功', true) check('导出 name (string)', typeof host.name === 'string', 'got ' + typeof host.name) check('导出 inject (string[])', Array.isArray(host.inject), 'got ' + typeof host.inject) check('inject === [logger]', JSON.stringify(host.inject) === '["logger"]', JSON.stringify(host.inject)) check('导出 apply (function)', typeof host.apply === 'function') check('无 default export(namespace 规则)', !('default' in host)) console.log(' name =', JSON.stringify(host.name)) } catch (e) { check('host entry 加载', false, e.message) } // 3. invariant surface try { const inv = req('apes-sidebar/invariant') check('invariant 可加载', true) check('invariant.assert 是函数', typeof inv.assert === 'function') } catch (e) { check('invariant 可加载', false, e.message) } // 4. client entry 存在 + 是 browser artifact try { const clientPath = req.resolve('apes-sidebar/client') const clientSrc = fs.readFileSync(clientPath, 'utf8') check('client entry 文件存在', true, clientPath) check('client 是 __ModuleLoader__ bundle(line 1 window)', clientSrc.split('\n')[0].includes('window.__ModuleLoader__.load'), 'line1=' + clientSrc.split('\n')[0].slice(0, 50)) } catch (e) { check('client entry', false, e.message) } // 5. peer dep dsh-better-sidebar 版本满足 ^0.18.0 try { const bs = req('dsh-better-sidebar/package.json') const v = bs.version check('dsh-better-sidebar 版本满足 ^0.18.0', v === '0.18.0', 'got ' + v) } catch (e) { check('dsh-better-sidebar 版本', false, e.message) } console.log('\n=== 验收结果: ' + pass + ' passed, ' + fail + ' failed ===') process.exit(fail > 0 ? 1 : 0)