unit.mjs 6.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // 纯函数单元测试(node tests/unit.mjs):抽查公式与 GDD 数值表一致
  2. import * as C from '/opt/data/git-repos/worldtree-escape/js/config.js';
  3. import * as F from '/opt/data/git-repos/worldtree-escape/js/formulas.js';
  4. import * as S from '/opt/data/git-repos/worldtree-escape/js/state.js';
  5. import * as R from '/opt/data/git-repos/worldtree-escape/js/rift.js';
  6. import { fmt } from '/opt/data/git-repos/worldtree-escape/js/util.js';
  7. let pass = 0, fail = 0;
  8. const near = (a, b, eps = 1e-9) => Math.abs(a - b) <= eps * Math.max(1, Math.abs(b));
  9. function ok(name, cond, extra = '') { if (cond) { pass++; console.log('✔', name, extra); } else { fail++; console.log('✘', name, extra); } }
  10. // 1) 跳关期望 E[k] = 20p/15
  11. for (const p of [0, 0.1, 0.25, 0.45, 0.6]) ok(`跳关期望 E[k](p=${p}) = 26p/15`, near(C.skipExpectation(p), 26 * p / 15, 1e-9), JSON.stringify([C.skipExpectation(p), 26 * p / 15]));
  12. ok('时间乘区 = 1+E[k](p=0.6 → 1+26×0.6/15 = 2.04)', near(C.timeMul(0.6), 1 + 26 * 0.6 / 15));
  13. ok('跳关分布 P1:P2:P3:P4 = 8:4:2:1', (() => { const pl = C.skipPlan(0.6); return near(pl.probs[0] / pl.probs[3], 8); })());
  14. ok('跳关触发率封顶 60%', C.skipPlan(0.99).p === 0.6);
  15. // 2) 升级成本几何曲线 C(R) = 10 × 1.4^R 与批量成本
  16. 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)));
  17. ok('根须批量成本 = 几何和', C.batchCost('root', 0, 5) === Math.ceil(10 * (Math.pow(1.4, 5) - 1) / 0.4));
  18. ok('树冠成本 C(K)=40×2.2^K', C.costOf('canopy', 3) === 40 * Math.pow(2.2, 3));
  19. 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); })());
  20. // 3) 乘区:根须产出 ×1.059/级(1.10^0.6)
  21. ok('根须产出乘区 1.10^0.6R', near(C.rootProdMul(10), Math.pow(1.1, 6)));
  22. ok('根须速度 ×1.10/级', near(C.rootSpeedMul(20), Math.pow(1.1, 20)));
  23. ok('世界树等级 ×1.05/级、速度 ×1.08/级', near(C.treeProdMul(5), Math.pow(1.05, 5)) && near(C.treeSpeedMul(5), Math.pow(1.08, 5)));
  24. ok('世界树成本 30×1.9^D', C.treeCost(4) === Math.ceil(30 * Math.pow(1.9, 4)));
  25. 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)));
  26. ok('兵种价格 = 基础×1.10^(n−1)×max(1, κ(π−π₀))', (() => {
  27. 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));
  28. return near(C.lifeCostOf(25, 3, pi), expect);
  29. })());
  30. ok('兵种基础成本 25×3^i', near(C.lifeBase(2), 225));
  31. // 4) 转生收益 R = ⌊0.5·T^1.3·E·M·ruleMod⌋
  32. 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)));
  33. ok('探索见闻 E = 1 + Σ探索/100', C.rebirthReward({ maxLayer: 10, exploreSum: 100 }) === Math.floor(0.5 * Math.pow(10, 1.6) * 2));
  34. const st0 = S.newState();
  35. ok('UI 预期 = state.rebirth() 实得', (() => {
  36. const a = S.newState(); a.maxLayerRound = 120; a.layer = 120; a.lives = { constsign: 4 }; a.exploreSum = 60; a.planes[0].explore = 60;
  37. const exp = F.rebirthEstimate(a); const r = S.rebirth(a); return r.gain === exp;
  38. })());
  39. 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; })());
  40. ok('终局后(自由轮回)起点 = 1700', C.earlyLayer(2500, C.PLANE_RULES[0], true) === C.earlyAfterEscape(C.PLANE_RULES[0]));
  41. ok('修仙道基起始层 +100', C.earlyLayer(400, C.PLANE_RULES[3]) === C.earlyLayer(400, C.PLANE_RULES[0]) + 100);
  42. ok('里世界中主世界冻结:炼化预期为 0', (() => { const a = S.newState(); a.planeRun = { state: 'fight', active: true }; a.layer = 50; a.maxLayerRound = 50; return F.rebirthEstimate(a) === 0; })());
  43. // 5) 本源产出积分:L=100 时 = (1.05^100 − 1)/(1.05·ln1.05)
  44. ok('产出积分公式', near(C.essenceBase(100, 1), (Math.pow(1.05, 100) - 1) / (1.05 * Math.log(1.05))));
  45. ok('π(L=30) 公式', near(C.piOf(30, 0), Math.log(1.05) / (1 - Math.exp(-Math.log(1.05) * 30))));
  46. // 6) 里世界结算:探索度增量 通关 ⌊6+分/50⌋ / 失败 ⌊6+分/100⌋
  47. ok('里世界探索增量(通关 300 分)', C.riftExploreDelta(7, 300, true) === 12);
  48. ok('里世界探索增量(失败 100 分)', C.riftExploreDelta(4, 100, false) === 7);
  49. ok('里世界扣血判定:结果 ≤ pK 为阻断', (() => {
  50. const run = R.riftNewRun(0); run.budget = 12; run.attrs = { knowledge: 3, oath: 3, belief: 3, judge: 3 }; R.buildDone(run, 0);
  51. 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)));
  52. return ['win', 'crit'].includes(res.res);
  53. })());
  54. // 7) 大数格式化
  55. ok('fmt 12345 → 1.23万', fmt(12345) === '1.23万');
  56. ok('fmt 1.5e8 → 1.5亿', fmt(1.5e8) === '1.5亿');
  57. ok('fmt 2e44 → 2载', fmt(2e44) === '2载');
  58. ok('fmt 3e52 → 3恒河沙', fmt(3e52) === '3恒河沙');
  59. // 8) 存档往返 / 非法输入
  60. 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; })());
  61. ok('非法 JSON 被拒绝', S.save.parse('not json').ok === false);
  62. ok('缺 state 的 JSON 被拒绝', S.save.parse('{"v":1}').ok === false);
  63. ok('未来版本被拒绝', S.save.parse(JSON.stringify({ v: 99, state: {} })).ok === false);
  64. 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; })());
  65. console.log(`\n结果:${pass} 通过,${fail} 失败`);
  66. process.exit(fail ? 1 : 0);