| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- // 纯函数单元测试(node tests/unit.mjs):抽查公式与 GDD 数值表一致
- import * as C from '/opt/data/git-repos/worldtree-escape/js/config.js';
- import * as F from '/opt/data/git-repos/worldtree-escape/js/formulas.js';
- import * as S from '/opt/data/git-repos/worldtree-escape/js/state.js';
- import * as R from '/opt/data/git-repos/worldtree-escape/js/rift.js';
- import { fmt } from '/opt/data/git-repos/worldtree-escape/js/util.js';
- let pass = 0, fail = 0;
- const near = (a, b, eps = 1e-9) => Math.abs(a - b) <= eps * Math.max(1, Math.abs(b));
- function ok(name, cond, extra = '') { if (cond) { pass++; console.log('✔', name, extra); } else { fail++; console.log('✘', name, extra); } }
- // 1) 跳关期望 E[k] = 20p/15
- for (const p of [0, 0.1, 0.25, 0.45, 0.6]) ok(`跳关期望 E[k](p=${p}) = 26·min(p,0.45)/15`, near(C.skipExpectation(p), 26 * Math.min(p, 0.45) / 15, 1e-9));
- ok('时间乘区 = 1+E[k](p≥封顶 → 1+26×0.45/15 = 1.78)', near(C.timeMul(0.6), 1 + 26 * 0.45 / 15));
- ok('跳关分布 P1:P2:P3:P4 = 8:4:2:1', (() => { const pl = C.skipPlan(0.6); return near(pl.probs[0] / pl.probs[3], 8); })());
- ok('跳关触发率封顶 45%', C.skipPlan(0.99).p === 0.45);
- // 2) 升级成本几何曲线 C(R) = 10 × 1.4^R 与批量成本
- ok('根须单级成本 C(0)=10, C(10)=10×1.4^10', near(C.costOf('root', 0), 10) && near(C.costOf('root', 10), 10 * Math.pow(1.4, 10)));
- ok('根须批量成本 = 几何和', C.batchCost('root', 0, 5) === Math.ceil(10 * (Math.pow(1.4, 5) - 1) / 0.4));
- ok('树冠成本 C(K)=40×2.2^K', C.costOf('canopy', 3) === 40 * Math.pow(2.2, 3));
- ok('maxAffordable 与 batchCost 一致', (() => { const n = C.maxAffordable('root', 7, 5000); return n > 0 && C.batchCost('root', 7, n) <= 5000 && (n >= C.PURCHASE_CAP || C.batchCost('root', 7, n + 1) > 5000); })());
- // 3) 乘区:根须产出 ×1.059/级(1.10^0.6)
- ok('根须产出乘区 1.10^0.6R', near(C.rootProdMul(10), Math.pow(1.1, 6)));
- ok('根须速度 ×1.10/级', near(C.rootSpeedMul(20), Math.pow(1.1, 20)));
- ok('世界树等级 ×1.05/级、速度 ×1.08/级', near(C.treeProdMul(5), Math.pow(1.05, 5)) && near(C.treeSpeedMul(5), Math.pow(1.08, 5)));
- ok('世界树成本 30×1.9^D', C.treeCost(4) === Math.ceil(30 * Math.pow(1.9, 4)));
- ok('遗物 ×1.25/件,成本 50×2.6^i', near(C.relicMul(8), Math.pow(1.25, 8)) && C.relicCost(2) === Math.ceil(50 * Math.pow(2.6, 2)));
- ok('兵种价格 = 基础×1.10^(n−1)×max(1, κ(π−π₀))', (() => {
- const pi = C.piOf(40, 0); const expect = 25 * Math.pow(1.1, 2) * Math.max(1, C.KAPPA * Math.max(0, pi - C.PI0));
- return near(C.lifeCostOf(25, 3, pi), expect);
- })());
- ok('兵种基础成本 25×3^i', near(C.lifeBase(2), 225));
- // 4) 转生收益 R = ⌊0.5·T^1.3·E·M·ruleMod⌋
- ok('转生公式 T=100, E=1, M=1 → ⌊0.5·100^1.6⌋', C.rebirthReward({ maxLayer: 100 }) === Math.floor(0.5 * Math.pow(100, 1.6)));
- ok('探索见闻 E = 1 + Σ探索/100', C.rebirthReward({ maxLayer: 10, exploreSum: 100 }) === Math.floor(0.5 * Math.pow(10, 1.6) * 2));
- const st0 = S.newState();
- ok('UI 预期 = state.rebirth() 实得', (() => {
- const a = S.newState(); a.maxLayerRound = 120; a.layer = 120; a.lives = { constsign: 4 }; a.exploreSum = 60; a.planes[0].explore = 60;
- const exp = F.rebirthEstimate(a); const r = S.rebirth(a); return r.gain === exp;
- })());
- ok('回溯起点 F = ⌊(L − 16√L)/2⌋×2(偶层对齐,夹在 [0, L/2])', (() => { const L = 123.7; const back = Math.floor((L - 16 * Math.sqrt(L)) / 2) * 2; const exp = Math.min(Math.max(0, back), Math.floor(L / 2) * 2); return C.earlyLayer(L, C.PLANE_RULES[0]) === exp; })());
- ok('终局后(自由轮回)起点 = 1700', C.earlyLayer(2500, C.PLANE_RULES[0], true) === C.earlyAfterEscape(C.PLANE_RULES[0]));
- ok('修仙道基起始层 +100', C.earlyLayer(400, C.PLANE_RULES[3]) === C.earlyLayer(400, C.PLANE_RULES[0]) + 100);
- ok('里世界中主世界冻结:炼化预期为 0', (() => { const a = S.newState(); a.planeRun = { state: 'fight', active: true }; a.layer = 50; a.maxLayerRound = 50; return F.rebirthEstimate(a) === 0; })());
- // 5) 本源产出积分:L=100 时 = (1.05^100 − 1)/(1.05·ln1.05)
- ok('产出积分公式(前 30 层解析;100 层带软上限,偏差 <100%)', Math.abs(C.essenceBase(100, 1) / ((Math.pow(1.05, 100) - 1) / Math.log(1.05)) - 1) < 1 && near(C.essenceBase(20, 1), C.EARLY_BOOST * (Math.pow(1.05, 20) - 1) / Math.log(1.05), 1e-6));
- ok('π(L=30) 公式', near(C.piOf(30, 0), Math.log(1.05) / (1 - Math.exp(-Math.log(1.05) * 30))));
- // 6) 里世界结算:探索度增量 通关 ⌊6+分/50⌋ / 失败 ⌊6+分/100⌋
- ok('里世界探索增量(一阶通关 300 分)', C.riftExploreDelta(7, 300, true) === 12);
- ok('里世界探索增量(一阶失败 100 分)', C.riftExploreDelta(4, 100, false) === 7);
- ok('高阶探索增量更高(四阶通关 300 分)', C.riftExploreDelta(7, 300, true, 3) === Math.floor(12 * (1 + 1.05)));
- ok('里世界扣血判定:结果 ≤ pK 为阻断', (() => {
- const run = R.riftNewRun(0); run.budget = 12; run.attrs = { knowledge: 3, oath: 3, belief: 3, judge: 3 }; R.buildDone(run, 0);
- const m = run.mouth; const atk = R.atkOf(run); const need = m.threat + 1 - atk; const res = R.slash(run, need <= 0 ? 0 : Math.min(5, Math.ceil(need)));
- return ['win', 'crit'].includes(res.res);
- })());
- // 7) 大数格式化
- ok('fmt 12345 → 1.23万', fmt(12345) === '1.23万');
- ok('fmt 1.5e8 → 1.5亿', fmt(1.5e8) === '1.5亿');
- ok('fmt 2e44 → 2载', fmt(2e44) === '2载');
- ok('fmt 3e52 → 3恒河沙', fmt(3e52) === '3恒河沙');
- // 8) 存档往返 / 非法输入
- ok('存档往返(序列化→解析)', (() => { const a = S.newState(); a.layer = 42; a.crystals = 7; const txt = S.save.serialize(a, null); const r = S.save.parse(txt); return r.ok && r.data.state.layer === 42 && r.data.state.crystals === 7; })());
- ok('非法 JSON 被拒绝', S.save.parse('not json').ok === false);
- ok('缺 state 的 JSON 被拒绝', S.save.parse('{"v":1}').ok === false);
- ok('未来版本被拒绝', S.save.parse(JSON.stringify({ v: 99, state: {} })).ok === false);
- ok('坏数值被清洗为 0', (() => { const r = S.save.parse(JSON.stringify({ v: 1, state: { essence: 'NaN', layer: -5 } })); return r.ok && r.data.state.essence === 0 && r.data.state.layer === 0; })());
- console.log(`\n结果:${pass} 通过,${fail} 失败`);
- process.exit(fail ? 1 : 0);
|