diff --git a/plugins/hylouis233/expiry-guardian/.minimax-plugin/plugin.json b/plugins/hylouis233/expiry-guardian/.minimax-plugin/plugin.json index fbefe7c7..09d901bf 100644 --- a/plugins/hylouis233/expiry-guardian/.minimax-plugin/plugin.json +++ b/plugins/hylouis233/expiry-guardian/.minimax-plugin/plugin.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "name": "expiry-guardian", "displayName": "蹬王之王", - "version": "2.0.0", + "version": "2.0.1", "description": "基于你自己维护的本地账本追踪积分与重置卡的到期时间,并给出「先过期先用」的建议:用卡还是留积分,并把理由说清楚。只读,不做任何写操作 —— 不扣卡、不暂停目标,不读 MiniMax Code 的登录凭据或内部数据库,也不读其它插件的数据。", "author": "User", "icon": "icon.png", diff --git a/plugins/hylouis233/expiry-guardian/README.md b/plugins/hylouis233/expiry-guardian/README.md index e344a1eb..70969676 100644 --- a/plugins/hylouis233/expiry-guardian/README.md +++ b/plugins/hylouis233/expiry-guardian/README.md @@ -150,6 +150,7 @@ a silently dead channel, so the config shape has to be changed in **three** plac | Rust (`daemon/rust`) | **25/25** | | Python decision unit tests | **20/20** | | Mini App (`tests/`) | **102/102** | +| Mini App start lifecycle (`tests/start-lifecycle.test.mjs`) | **6/6** | | Three-way parity | **19/19** | | Upstream `npm run validate` | **OK** for this plugin (0 errors, 0 warnings); `repository: OK` | | Upstream `npm test` | 69/69 | @@ -161,6 +162,13 @@ a typo in the dashboard's data layer made the whole snapshot throw, so the panel because they only cover the pure `decide()` function. The data layer itself was never executed. The guard makes that class of error fail loudly. +A second Mini App test (`tests/start-lifecycle.test.mjs`) starts the plugin for real: it +calls `start(context)` and requires it to resolve, to listen, and to dispose. It exists +because no test ever called `start()`, so nothing noticed that its first statement +assigned to `hostLogger` — an identifier the module never declared. An ES module is +strict, so that assignment threw a `ReferenceError` and the Host could not load this +plugin at all. + ## What v1.x used to do The full history is in git (the v1.1–v1.6 commits). v1.x spent cards, paused goals, diff --git a/plugins/hylouis233/expiry-guardian/check-test-parity.sh b/plugins/hylouis233/expiry-guardian/check-test-parity.sh index 49e6067d..2efc792a 100755 --- a/plugins/hylouis233/expiry-guardian/check-test-parity.sh +++ b/plugins/hylouis233/expiry-guardian/check-test-parity.sh @@ -19,6 +19,7 @@ PAIRS=( "decision.test.mjs" "mcp.test.mjs" "server-lint.test.mjs" + "start-lifecycle.test.mjs" ) fail=0 diff --git a/plugins/hylouis233/expiry-guardian/miniapp/node/server.mjs b/plugins/hylouis233/expiry-guardian/miniapp/node/server.mjs index b29394bf..9683ee86 100644 --- a/plugins/hylouis233/expiry-guardian/miniapp/node/server.mjs +++ b/plugins/hylouis233/expiry-guardian/miniapp/node/server.mjs @@ -661,6 +661,14 @@ function invalidateCache(slot) { let inflightSnapshot = null; /** 上一次成功的快照:取数失败时回退用,避免整页被打空。 */ let lastGoodSnapshot = null; +/** + * Host 日志出口:由 `start(context)` 注入,供**模块级**的辅助函数 + * (重试等)打日志用 —— 模块级的函数拿不到 context,只能靠这个出口。 + * + * ⚠️ 必须在模块作用域声明:本包是 ESM(严格模式), + * 给未声明的标识符赋值会抛 ReferenceError ⇒ 插件根本起不来。 + */ +let hostLogger = null; function buildSnapshot() { if (inflightSnapshot) return inflightSnapshot; diff --git a/plugins/hylouis233/expiry-guardian/package.json b/plugins/hylouis233/expiry-guardian/package.json index 2564014e..589dbed1 100644 --- a/plugins/hylouis233/expiry-guardian/package.json +++ b/plugins/hylouis233/expiry-guardian/package.json @@ -1,6 +1,6 @@ { "name": "expiry-guardian", - "version": "2.0.0", + "version": "2.0.1", "private": true, "type": "module", "mcode": { diff --git a/plugins/hylouis233/expiry-guardian/tests/start-lifecycle.test.mjs b/plugins/hylouis233/expiry-guardian/tests/start-lifecycle.test.mjs new file mode 100644 index 00000000..1ac18fe7 --- /dev/null +++ b/plugins/hylouis233/expiry-guardian/tests/start-lifecycle.test.mjs @@ -0,0 +1,102 @@ +// 启动生命周期测试:`start(context)` 必须真的能把插件拉起来。 +// +// 2026-12-02 实测事故: +// `start()` 的第一条语句给 `hostLogger` 赋值,而这个标识符**全文件从未声明**。 +// 本包是 ESM(package.json 里 "type": "module")⇒ 严格模式下给未声明的 +// 标识符赋值就是 ReferenceError ⇒ 宿主加载插件必然失败 ⇒ +// 面板、MCP 端点、定时判定**全部**不存在。 +// +// ⚠️ 这个缺陷从插件合入的第一次提交起就在(5664d58 / PR #47)—— +// 也就是说这个插件从第一天起就没能启动过。 +// 为什么没人发现?因为当时的测试只覆盖纯函数 `decide()`, +// **从来没有真的调用过 `start()`** —— 数据层与生命周期层是零覆盖的。 +// +// ⇒ 这条测试把「start() 能不能起来」变成一条可执行的断言: +// 声明漏了 ⇒ 立刻红,而不是等到宿主加载时才发现。 +import { existsSync, readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath, pathToFileURL } from 'node:url'; + +const here = dirname(fileURLToPath(import.meta.url)); +// 插件包根目录:tests/ 的上一级;工作区源码则是 miniapp/node/ 的上两级。 +const candidates = [join(here, '..'), join(here, '..', '..')]; +const root = candidates.find((p) => existsSync(join(p, 'miniapp', 'node', 'server.mjs'))); +if (!root) { + console.error(' FAIL 找不到插件根目录(既不在 ../ 也不在 ../../)'); + process.exit(1); +} + +let pass = 0; +let total = 0; +const check = (name, ok, extra = '') => { + total += 1; + if (ok) pass += 1; + console.log(`${ok ? ' PASS' : ' FAIL'} ${name}${extra ? ` ${extra}` : ''}`); +}; + +/** 收集日志的假 logger:Host 的 logger 至少有 info/warn/error。 */ +function makeLogger() { + const lines = []; + const push = (level) => (...args) => { + lines.push({ level, args }); + }; + return { + lines, + info: push('info'), + warn: push('warn'), + error: push('error'), + }; +} + +console.log('=== start(context) 不能抛异常(未声明标识符 ⇒ ReferenceError)==='); +{ + const mod = await import(pathToFileURL(join(root, 'miniapp', 'node', 'server.mjs')).href); + const logger = makeLogger(); + const controller = new AbortController(); + const context = { + pluginRoot: root, + logger, + // 端口 0 ⇒ 让系统分配一个空闲端口,避免测试之间撞端口。 + listen: { host: '127.0.0.1', port: 0 }, + signal: controller.signal, + }; + + let lifecycle = null; + let error = null; + try { + lifecycle = await mod.start(context); + } catch (e) { + error = e; + } + check('start() resolve 了(没有抛)', error === null, error ? `${error.name}: ${error.message}` : ''); + check('返回的 lifecycle 带 dispose', typeof lifecycle?.dispose === 'function'); + + // 真的监听了端口:Host 的 logger 会收到这一条 ⇒ 说明不是「起个空架子」。 + const listening = logger.lines.some((l) => l.level === 'info' && l.args[0] === 'expiryGuardian.listening'); + check('服务器真的开始监听了', listening, logger.lines.map((l) => l.args[0]).join(',')); + + // ⚠️ 必须关掉,否则测试进程会挂住。 + if (typeof lifecycle?.dispose === 'function') { + let disposeError = null; + try { + await lifecycle.dispose(); + } catch (e) { + disposeError = e; + } + check('dispose() 跑完了(服务已关闭)', disposeError === null, disposeError ? String(disposeError.message ?? disposeError) : ''); + } +} + +console.log('\n=== 根因守卫:hostLogger 必须是模块级已声明的变量 ==='); +{ + const src = readFileSync(join(root, 'miniapp', 'node', 'server.mjs'), 'utf8'); + // 模块级声明在本文件里都是顶格写的 ⇒ 只认顶格的声明。 + const declared = /^(?:let|const|var)\s+hostLogger\b/m.test(src); + check('server.mjs 里声明了 hostLogger', declared); + // start() 里那次注入必须留着:那是模块级重试辅助函数的 Host 日志出口。 + const injected = /hostLogger\s*=\s*context\.logger\s*\?\?\s*null/.test(src); + check('start() 里仍然注入 Host logger', injected); +} + +console.log(`\n${pass}/${total} passed`); +if (pass !== total) process.exitCode = 1;