Files
ChronicleOfTheImmortalClan/tests/plugin.test.ts
T
thzxx 10ed1e7d9b v0.1.21: 大势流转(线程定论 + era景气双向闭环 + 世界史表 + 渲染管线)
【线程架构定论(实测)】
- 引擎与 UI 同在 renderer 主线程(单线程);2160 月实测 advanceMonth 平均 0.05ms/tick
- 结论:Worker 化无收益——0.05ms 不需要换线程;此前 60ms 告警来自 UI 渲染+存档(已修复)

【世界自演进闭环(双向因果,自演进最高形态)】
- era↔景气双向闭环:世界温度(marketPool 景气均值)调制 era 转移权重
  (tempEraK=0.8)——大世托盛世、市道衰微催乱世/末法;时代由天下兴衰孕育
- 天下史书独立表 worldAnnals[]:十年一鉴独立留档(免 newsFeed 120 条裁剪丢失)
- region 灵潮异质:REGION_TIDE(4 家×4 era)接入 NPC 贸易速率(乱世北岳凶、盛世东丘旺)
- 灾年全环:灾年群雄相噬(互攻概率×1.5)

【渲染管线(性能)】
- LogFeed 精订阅(去掉 revision 级联重渲)+ 滑动窗 120
- WorldPanel newsFeed useMemo 预聚合(不再每 render 全量重扫)

【测试】1018 全绿(43 套件):tide-0.1.21 7 例(延续档/温度/史表/区域/灾年/指纹敏感/滑动窗);
双金钟罩 0.1.21 基线固化;build 通过
2026-08-23 15:22:19 +08:00

118 lines
4.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { World } from '../src/renderer/game/engine/runtime/World'
import { GameFacade } from '../src/renderer/game/engine/runtime/ApiFacade'
import { installCoreSystems, buildClock } from '../src/renderer/game/engine/runtime/clocks'
import { emptyClock } from '../src/renderer/game/engine/runtime/clocks'
import { CORE_PLUGINS } from '../src/renderer/game/engine/runtime/plugin-bootstrap'
import { examplePlugin, brokenPlugin } from './helpers/examplePlugin'
import { stateFingerprint, longRun } from './fingerprint.helper'
import { findEvent } from '../src/renderer/game/engine/runtime/Systems/events'
import { resetSaveBus, attachLogSink } from './world.helpers'
function baseWorld(seed: string): World {
const w = World.create({ seed, surname: '夏', familyName: '夏家', motto: 'm', difficulty: 'normal' })
resetSaveBus()
attachLogSink(w)
return w
}
describe('PluginCore 插件协议', () => {
it('内置三插件常驻且受保护', () => {
const w = baseWorld('pl-a')
const list = w.pluginList()
expect(list.length).toBe(3)
for (const p of list) {
expect(p.installed).toBe(true)
expect(p.enabled).toBe(true)
expect(p.protected).toBe(true)
}
const ids = list.map((p) => p.id)
expect(ids).toContain('core-systems')
expect(ids).toContain('core-data')
expect(ids).toContain('core-events')
})
it('核心插件不可卸载(保护)', () => {
const w = baseWorld('pl-b')
const r = w.removePlugin('core-systems')
expect(r.ok).toBe(false)
})
it('依赖缺失拒绝安装;依赖满足则放行', () => {
const w = baseWorld('pl-c')
expect(w.installPlugin(brokenPlugin).ok).toBe(false)
expect(w.installPlugin(examplePlugin).ok).toBe(true)
expect(w.pluginList().length).toBe(4)
})
it('示例安装后:消息池注入生效 + 山神庇佑加值;卸载后两清', () => {
const w = baseWorld('pl-d')
w.installPlugin(examplePlugin)
expect(w.eventPoolIds()).toContain('demo-peaks')
const x5 = w.state.members['x5']
x5.realm = { major: 'qi', minor: 1 }
const p0 = x5.realmProgress
// 推进到跨年触发年首庇佑
for (let i = 0; i < 13; i++) w.advanceMonth()
expect(x5.realmProgress).toBeGreaterThan(p0)
// 事件注入可被解析(findEvent 命中 demo 池 = 注入生效;确定性校验不赌概率)
expect(findEvent('ev-demo-guardian', w)).toBeTruthy()
// 卸载
expect(w.removePlugin('demo-peaks').ok).toBe(true)
expect(w.eventPoolIds()).not.toContain('demo-peaks')
expect(w.pluginList().length).toBe(3)
})
it('remove 后时钟钩子计数恢复(真摘钩)', () => {
const w = baseWorld('pl-e')
const before = w.clock.subscriptionCount()
w.installPlugin(examplePlugin)
const mounted = w.clock.subscriptionCount()
expect(mounted).toBe(before + 1)
w.removePlugin('demo-peaks')
expect(w.clock.subscriptionCount()).toBe(before)
})
it('安装序确定性:同版本两次世界插件列表一致', () => {
const a = baseWorld('pl-f')
const b = baseWorld('pl-f')
expect(JSON.stringify(a.pluginList())).toBe(JSON.stringify(b.pluginList()))
})
it('默认管线金钟罩不受插件层影响', () => {
expect(stateFingerprint(longRun('bell-seed-1').state)).toBe('879a909d')
expect(stateFingerprint(longRun('bell-seed-2').state)).toBe('33d511bc')
expect(stateFingerprint(longRun('bell-seed-3').state)).toBe('e6936427')
})
it('facade 插件查询与 about.plugins', () => {
const w = baseWorld('pl-g')
const f = new GameFacade(w, 1)
const pl = f.query('plugins') as { list: { id: string }[] }
expect(pl.list.length).toBe(3)
expect(f.about().plugins).toBe(3)
})
it('订阅可捕获 plugin 生命周期事件', () => {
const w = baseWorld('pl-h')
const f = new GameFacade(w, 1)
const events: string[] = []
const unsub = f.subscribe((e) => {
if (e.type === 'plugin') events.push((e as { action: string }).action)
})
w.installPlugin(examplePlugin)
w.removePlugin('demo-peaks')
expect(events).toContain('install')
expect(events).toContain('remove')
unsub()
})
it('buildClock 兼容路径仍完整注册(测试/工具用)', () => {
const clock = buildClock()
expect(clock.subscriptionCount()).toBeGreaterThanOrEqual(10)
const empty = emptyClock()
expect(empty.subscriptionCount()).toBe(0)
void installCoreSystems
})
})