| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const Reward = require('../src/reward.js'); |
| 6 | |
| 7 | function runToEnd (seq, dt, maxT) |
| 8 | { |
| 9 | const events = []; |
| 10 | let t = 0; |
| 11 | let spawned = 0; |
| 12 | let arrived = 0; |
| 13 | let lastCounter = null; |
| 14 | let monotonic = true; |
| 15 | while (t < maxT) |
| 16 | { |
| 17 | const out = seq.tick(dt); |
| 18 | t += dt; |
| 19 | spawned += out.spawn.length; |
| 20 | arrived += out.arrived; |
| 21 | if (lastCounter != null && out.counter < lastCounter) |
| 22 | { monotonic = false; } |
| 23 | lastCounter = out.counter; |
| 24 | events.push({ t, phase: out.phase, counter: out.counter }); |
| 25 | if (out.done) |
| 26 | { break; } |
| 27 | } |
| 28 | return { events, t, spawned, arrived, monotonic, last: events[events.length - 1] }; |
| 29 | } |
| 30 | |
| 31 | describe('Reward spawn schedule (t1175: 5 + 3t blocks/s)', () => |
| 32 | { |
| 33 | it('block i leaves when 5t + 3t²/2 = i', () => |
| 34 | { |
| 35 | assert.equal(Reward.RATE0, 5); |
| 36 | assert.equal(Reward.ACCEL, 3); |
| 37 | assert.equal(Reward.spawnTime(0), 0); |
| 38 | for (const i of [1, 5, 10, 50, 100, 252, 1000]) |
| 39 | { |
| 40 | const t = Reward.spawnTime(i); |
| 41 | assert.ok(Math.abs(5 * t + 3 * t * t / 2 - i) < 1e-9, 'i=' + i + ' t=' + t); |
| 42 | } |
| 43 | // spawn times strictly increase, gaps shrink (accelerating) |
| 44 | let prevGap = Infinity; |
| 45 | for (let i = 1; i < 300; i++) |
| 46 | { |
| 47 | const gap = Reward.spawnTime(i) - Reward.spawnTime(i - 1); |
| 48 | assert.ok(gap > 0); |
| 49 | assert.ok(gap <= prevGap + 1e-12, 'gap grew at ' + i); |
| 50 | prevGap = gap; |
| 51 | } |
| 52 | }); |
| 53 | |
| 54 | it('starts at 5/s and the 253rd block leaves at ≈ 11.4 s (Tim\'s example)', () => |
| 55 | { |
| 56 | // 1/5 s between the first two blocks. |
| 57 | assert.ok(Math.abs(Reward.spawnTime(1) - (-5 + Math.sqrt(31)) / 3) < 1e-12); |
| 58 | assert.ok(Reward.spawnTime(1) > 0.18 && Reward.spawnTime(1) < 0.2); |
| 59 | const last = (-5 + Math.sqrt(25 + 6 * 252)) / 3; |
| 60 | assert.ok(Math.abs(Reward.spawnTime(252) - last) < 1e-9); |
| 61 | assert.ok(last > 11.3 && last < 11.5, 'last=' + last); |
| 62 | assert.equal(Reward.spawnedBy(last + 1e-6, 253), 253); |
| 63 | assert.equal(Reward.spawnedBy(last - 0.01, 253), 252); |
| 64 | assert.ok(Math.abs(Reward.countDuration(253) - (last + Reward.FLIGHT)) < 1e-9); |
| 65 | }); |
| 66 | |
| 67 | it('spawnedBy matches floor(5t + 3t²/2) + 1, capped by n', () => |
| 68 | { |
| 69 | for (let t = 0; t <= 20; t += 0.37) |
| 70 | { |
| 71 | const expect = Math.min(253, Math.floor(5 * t + 3 * t * t / 2 + 1e-9) + 1); |
| 72 | assert.equal(Reward.spawnedBy(t, 253), expect, 't=' + t); |
| 73 | } |
| 74 | assert.equal(Reward.spawnedBy(-1, 10), 0); |
| 75 | assert.equal(Reward.spawnedBy(5, 0), 0); |
| 76 | assert.equal(Reward.spawnedBy(100, 7), 7); |
| 77 | assert.equal(Reward.arrivedBy(Reward.FLIGHT - 0.01, 10), 0); |
| 78 | assert.equal(Reward.arrivedBy(Reward.FLIGHT, 10), 1); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | describe('Reward sequence (t1173)', () => |
| 83 | { |
| 84 | it('500 + 253 → counts every point from 500 up to 753 in ~12.1 s', () => |
| 85 | { |
| 86 | const seq = Reward.createSequence({ won: true }); |
| 87 | seq.setResult({ before: 500, after: 753 }); |
| 88 | const r = runToEnd(seq, 1 / 60, 60); |
| 89 | assert.equal(r.last.phase, 'done'); |
| 90 | assert.equal(r.spawned, 253); |
| 91 | assert.equal(r.arrived, 253); |
| 92 | assert.ok(r.monotonic, 'counter never goes down'); |
| 93 | assert.equal(seq.counter, 753); |
| 94 | const counting = r.events.filter((e) => e.phase === 'count'); |
| 95 | assert.ok(counting.length > 0); |
| 96 | assert.ok(counting[0].counter >= 500 && counting[0].counter < 510); |
| 97 | // intro + rise + count + hold |
| 98 | const expected = Reward.INTRO + Reward.RISE + Reward.countDuration(253) + Reward.HOLD; |
| 99 | assert.ok(Math.abs(r.t - expected) < 0.1, 't=' + r.t + ' expected ' + expected); |
| 100 | }); |
| 101 | |
| 102 | it('phases run intro → rise → wait → count → hold → done', () => |
| 103 | { |
| 104 | const seq = Reward.createSequence({}); |
| 105 | const phases = []; |
| 106 | const push = (p) => { if (phases[phases.length - 1] !== p) { phases.push(p); } }; |
| 107 | push(seq.phase); |
| 108 | for (let i = 0; i < 60; i++) { push(seq.tick(0.05).phase); } // 3 s, no result yet |
| 109 | assert.deepEqual(phases, ['intro', 'rise', 'wait']); |
| 110 | assert.equal(seq.counter, 0); |
| 111 | seq.setResult({ before: 10, after: 13 }); |
| 112 | for (let i = 0; i < 200; i++) { push(seq.tick(0.05).phase); } |
| 113 | assert.deepEqual(phases, ['intro', 'rise', 'wait', 'count', 'hold', 'done']); |
| 114 | assert.equal(seq.counter, 13); |
| 115 | }); |
| 116 | |
| 117 | it('title stays centred for one second before rising', () => |
| 118 | { |
| 119 | const seq = Reward.createSequence({}); |
| 120 | assert.equal(seq.tick(0.99).phase, 'intro'); |
| 121 | assert.equal(seq.tick(0.02).phase, 'rise'); |
| 122 | }); |
| 123 | |
| 124 | it('nothing is counted before the server result arrives', () => |
| 125 | { |
| 126 | const seq = Reward.createSequence({ waitMax: 100 }); |
| 127 | let spawned = 0; |
| 128 | for (let i = 0; i < 100; i++) { spawned += seq.tick(0.05).spawn.length; } |
| 129 | assert.equal(spawned, 0); |
| 130 | assert.equal(seq.phase, 'wait'); |
| 131 | }); |
| 132 | |
| 133 | it('earned 0 shows the number briefly, no blocks', () => |
| 134 | { |
| 135 | const seq = Reward.createSequence({}); |
| 136 | seq.setResult({ before: 42, after: 42 }); |
| 137 | const r = runToEnd(seq, 0.05, 30); |
| 138 | assert.equal(r.spawned, 0); |
| 139 | assert.equal(seq.counter, 42); |
| 140 | assert.ok(r.events.some((e) => e.phase === 'hold' && e.counter === 42)); |
| 141 | assert.equal(r.last.phase, 'done'); |
| 142 | const expected = Reward.INTRO + Reward.RISE + Reward.HOLD_ZERO; |
| 143 | assert.ok(Math.abs(r.t - expected) < 0.11); |
| 144 | }); |
| 145 | |
| 146 | it('server failure: no count, still continues', () => |
| 147 | { |
| 148 | const seq = Reward.createSequence({}); |
| 149 | seq.setResult(null); |
| 150 | const r = runToEnd(seq, 0.05, 30); |
| 151 | assert.equal(r.spawned, 0); |
| 152 | assert.equal(seq.failed, true); |
| 153 | assert.equal(r.last.phase, 'done'); |
| 154 | }); |
| 155 | |
| 156 | it('no answer at all times out and continues', () => |
| 157 | { |
| 158 | const seq = Reward.createSequence({ waitMax: 2 }); |
| 159 | const r = runToEnd(seq, 0.05, 30); |
| 160 | assert.equal(r.last.phase, 'done'); |
| 161 | assert.equal(seq.failed, true); |
| 162 | }); |
| 163 | |
| 164 | it('before > after or a missing before is clamped (never counts down)', () => |
| 165 | { |
| 166 | const a = Reward.createSequence({}); |
| 167 | a.setResult({ before: 900, after: 800 }); |
| 168 | assert.equal(a.earned, 0); |
| 169 | assert.equal(a.counter, 800); |
| 170 | const b = Reward.createSequence({}); |
| 171 | b.setResult({ after: 77 }); |
| 172 | assert.equal(b.earned, 0); |
| 173 | assert.equal(b.before, 77); |
| 174 | }); |
| 175 | |
| 176 | it('a huge frame (tab in background) still lands exactly on the total', () => |
| 177 | { |
| 178 | const seq = Reward.createSequence({}); |
| 179 | seq.setResult({ before: 0, after: 40 }); |
| 180 | let spawned = 0; |
| 181 | let out; |
| 182 | for (let i = 0; i < 10; i++) |
| 183 | { |
| 184 | out = seq.tick(5); |
| 185 | spawned += out.spawn.length; |
| 186 | } |
| 187 | assert.equal(spawned, 40); |
| 188 | assert.equal(seq.counter, 40); |
| 189 | assert.equal(out.done, true); |
| 190 | }); |
| 191 | |
| 192 | it('only the hold phase can be finished early (tap to continue), not the count', () => |
| 193 | { |
| 194 | const seq = Reward.createSequence({}); |
| 195 | seq.setResult({ before: 0, after: 100 }); |
| 196 | seq.tick(2); |
| 197 | assert.equal(seq.phase, 'count'); |
| 198 | assert.equal(seq.finish(), false); |
| 199 | assert.equal(seq.phase, 'count'); |
| 200 | while (seq.phase === 'count') { seq.tick(0.1); } |
| 201 | assert.equal(seq.phase, 'hold'); |
| 202 | assert.equal(seq.counter, 100); |
| 203 | assert.equal(seq.finish(), true); |
| 204 | assert.equal(seq.tick(0.01).done, true); |
| 205 | }); |
| 206 | }); |
projects-timfalken-com meteors