/** * 冒烟测试:用假的 ctx 调 apply(),确认插件不会抛异常,路由能应答。 * 用法:node tools/smoke-host.mjs */ import { apply, name, inject } from '../lib/host.js' const routes = [] const events = [] const effects = [] const ctx = { logger: { info: (...a) => console.log('[mock]', ...a) }, webServer: { port: 3099, host: '127.0.0.1', register: (r) => { routes.push(r); return () => { } }, }, get: () => undefined, set: () => { }, on: (ev, fn) => { events.push([ev, fn]); return () => { } }, effect: (fn) => { const d = fn(); effects.push(d); return () => { } }, } console.log('name =', name, '| inject =', JSON.stringify(inject)) apply(ctx) console.log('routes =', routes.map((r) => `${r.kind}:${r.path}`).join(', ')) console.log('events =', events.map((e) => e[0]).join(', ')) console.log('effects =', effects.length) function fakeRes() { return { status: 0, headers: null, body: '', writeHead(s, h) { this.status = s; this.headers = h }, write(chunk) { this.body += chunk; return true }, end(chunk) { if (chunk) this.body += chunk }, } } const route = routes[0] for (const p of ['/dsh-float-window/health', '/dsh-float-window/state', '/dsh-float-window/nope']) { const res = fakeRes() await route.handler({ method: 'GET', url: p, on: () => { } }, res) console.log(p, '->', res.status, String(res.body).slice(0, 120)) } // SSE 分支:只验证不抛异常且写出了 data: const res = fakeRes() await route.handler({ method: 'GET', url: '/dsh-float-window/events', on: () => { } }, res) console.log('events ->', res.status, res.headers['Content-Type'], JSON.stringify(res.body.slice(0, 80))) console.log('SMOKE OK')