'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const Reward = require('../src/reward.js'); function runToEnd (seq, dt, maxT) { const events = []; let t = 0; let spawned = 0; let arrived = 0; let lastCounter = null; let monotonic = true; while (t < maxT) { const out = seq.tick(dt); t += dt; spawned += out.spawn.length; arrived += out.arrived; if (lastCounter != null && out.counter < lastCounter) { monotonic = false; } lastCounter = out.counter; events.push({ t, phase: out.phase, counter: out.counter }); if (out.done) { break; } } return { events, t, spawned, arrived, monotonic, last: events[events.length - 1] }; } describe('Reward spawn schedule (t1194: 3 blocks/s, last block at 4 s)', () => { it('constants and per-reward acceleration a = 2((n−1) − 3·4) / 4²', () => { assert.equal(Reward.RATE0, 3); assert.equal(Reward.SPAWN_END, 4); assert.deepEqual(Reward.spawnParams(253), { r0: 3, a: 2 * (252 - 12) / 16 }); assert.deepEqual(Reward.spawnParams(13), { r0: 3, a: 0 }); assert.deepEqual(Reward.spawnParams(5), { r0: 1, a: 0 }, 'n ≤ 13: constant (n−1)/4'); assert.deepEqual(Reward.spawnParams(1), { r0: 0, a: 0 }); }); it('the last block always leaves at 4 s, whatever the point count', () => { for (const n of [2, 5, 12, 13, 14, 50, 253, 800, 2000, 10000]) { const last = Reward.spawnTime(n - 1, n); assert.ok(Math.abs(last - 4) < 1e-9, 'n=' + n + ' last=' + last); assert.ok(Reward.spawnedBy(4 - 1e-3, n) < n, 'n=' + n + ' not all out just before 4 s'); assert.equal(Reward.spawnedBy(4 + 1e-6, n), n, 'n=' + n + ' at 4 s'); assert.ok(Math.abs(Reward.countDuration(n) - (4 + Reward.FLIGHT)) < 1e-9); } assert.equal(Reward.spawnTime(0, 1), 0); assert.equal(Reward.countDuration(1), Reward.FLIGHT); }); it('block i leaves when 3t + a·t²/2 = i; starts at 3/s and accelerates', () => { const n = 253; const { r0, a } = Reward.spawnParams(n); for (const i of [1, 5, 10, 50, 100, 252]) { const t = Reward.spawnTime(i, n); assert.ok(Math.abs(r0 * t + a * t * t / 2 - i) < 1e-9, 'i=' + i + ' t=' + t); } // First gap ≈ 1/3 s at the start rate (slightly less: it accelerates). const g1 = Reward.spawnTime(1, n); assert.ok(g1 < 1 / 3 && g1 > 0.1, 'first gap ' + g1); let prevGap = Infinity; for (let i = 1; i < n; i++) { const gap = Reward.spawnTime(i, n) - Reward.spawnTime(i - 1, n); assert.ok(gap > 0); assert.ok(gap <= prevGap + 1e-12, 'gap grew at ' + i); prevGap = gap; } }); it('spawnedBy matches floor(r0·t + a·t²/2) + 1, capped by n', () => { const { r0, a } = Reward.spawnParams(253); for (let t = 0; t <= 6; t += 0.137) { const expect = Math.min(253, Math.floor(r0 * t + a * t * t / 2 + 1e-9) + 1); assert.equal(Reward.spawnedBy(t, 253), expect, 't=' + t); } assert.equal(Reward.spawnedBy(-1, 10), 0); assert.equal(Reward.spawnedBy(5, 0), 0); assert.equal(Reward.spawnedBy(100, 7), 7); assert.equal(Reward.arrivedBy(Reward.FLIGHT - 0.01, 10), 0); assert.equal(Reward.arrivedBy(Reward.FLIGHT, 10), 1); }); it('measured end times through the real sequence (60 fps): ~4 s for 5…2000 points', () => { for (const n of [5, 50, 253, 800, 2000]) { const seq = Reward.createSequence({ won: true }); seq.setResult({ before: 100, after: 100 + n }); let t = 0, firstSpawn = null, lastSpawn = null, lastArrive = null, prev = 0; for (let k = 0; k < 60 * 30; k++) { const out = seq.tick(1 / 60); t += 1 / 60; if (out.spawn.length) { if (firstSpawn == null) firstSpawn = t; lastSpawn = t; } if (seq.arrived === n && lastArrive == null && prev < n) lastArrive = t; prev = seq.arrived; if (out.done) break; } const spawnSpan = lastSpawn - firstSpawn; assert.ok(Math.abs(spawnSpan - 4) < 0.05, 'n=' + n + ' last spawn after ' + spawnSpan.toFixed(3) + ' s'); assert.equal(seq.counter, 100 + n); } }); }); describe('Reward sequence (t1173)', () => { it('500 + 253 → counts every point from 500 up to 753 (last block at 4 s)', () => { const seq = Reward.createSequence({ won: true }); seq.setResult({ before: 500, after: 753 }); const r = runToEnd(seq, 1 / 60, 60); assert.equal(r.last.phase, 'done'); assert.equal(r.spawned, 253); assert.equal(r.arrived, 253); assert.ok(r.monotonic, 'counter never goes down'); assert.equal(seq.counter, 753); const counting = r.events.filter((e) => e.phase === 'count'); assert.ok(counting.length > 0); assert.ok(counting[0].counter >= 500 && counting[0].counter < 510); // intro + rise + count + hold const expected = Reward.INTRO + Reward.RISE + Reward.countDuration(253) + Reward.HOLD; assert.ok(Math.abs(r.t - expected) < 0.1, 't=' + r.t + ' expected ' + expected); }); it('phases run intro → rise → wait → count → hold → done', () => { const seq = Reward.createSequence({}); const phases = []; const push = (p) => { if (phases[phases.length - 1] !== p) { phases.push(p); } }; push(seq.phase); for (let i = 0; i < 60; i++) { push(seq.tick(0.05).phase); } // 3 s, no result yet assert.deepEqual(phases, ['intro', 'rise', 'wait']); assert.equal(seq.counter, 0); seq.setResult({ before: 10, after: 13 }); for (let i = 0; i < 200; i++) { push(seq.tick(0.05).phase); } assert.deepEqual(phases, ['intro', 'rise', 'wait', 'count', 'hold', 'done']); assert.equal(seq.counter, 13); }); it('title stays centred for one second before rising', () => { const seq = Reward.createSequence({}); assert.equal(seq.tick(0.99).phase, 'intro'); assert.equal(seq.tick(0.02).phase, 'rise'); }); it('nothing is counted before the server result arrives', () => { const seq = Reward.createSequence({ waitMax: 100 }); let spawned = 0; for (let i = 0; i < 100; i++) { spawned += seq.tick(0.05).spawn.length; } assert.equal(spawned, 0); assert.equal(seq.phase, 'wait'); }); it('earned 0 shows the number briefly, no blocks', () => { const seq = Reward.createSequence({}); seq.setResult({ before: 42, after: 42 }); const r = runToEnd(seq, 0.05, 30); assert.equal(r.spawned, 0); assert.equal(seq.counter, 42); assert.ok(r.events.some((e) => e.phase === 'hold' && e.counter === 42)); assert.equal(r.last.phase, 'done'); const expected = Reward.INTRO + Reward.RISE + Reward.HOLD_ZERO; assert.ok(Math.abs(r.t - expected) < 0.11); }); it('server failure: no count, still continues', () => { const seq = Reward.createSequence({}); seq.setResult(null); const r = runToEnd(seq, 0.05, 30); assert.equal(r.spawned, 0); assert.equal(seq.failed, true); assert.equal(r.last.phase, 'done'); }); it('no answer at all times out and continues', () => { const seq = Reward.createSequence({ waitMax: 2 }); const r = runToEnd(seq, 0.05, 30); assert.equal(r.last.phase, 'done'); assert.equal(seq.failed, true); }); it('before > after or a missing before is clamped (never counts down)', () => { const a = Reward.createSequence({}); a.setResult({ before: 900, after: 800 }); assert.equal(a.earned, 0); assert.equal(a.counter, 800); const b = Reward.createSequence({}); b.setResult({ after: 77 }); assert.equal(b.earned, 0); assert.equal(b.before, 77); }); it('numeric strings are accepted (t1278)', () => { const seq = Reward.createSequence({ intro: 0, rise: 0, flight: 0, hold: 0 }); seq.setResult({ before: '100', after: '109' }); assert.equal(seq.failed, false); assert.equal(seq.earned, 9); assert.equal(seq.before, 100); assert.equal(seq.after, 109); const out = seq.tick(0); assert.equal(out.phase, 'count'); }); it('a huge frame (tab in background) still lands exactly on the total', () => { const seq = Reward.createSequence({}); seq.setResult({ before: 0, after: 40 }); let spawned = 0; let out; for (let i = 0; i < 10; i++) { out = seq.tick(5); spawned += out.spawn.length; } assert.equal(spawned, 40); assert.equal(seq.counter, 40); assert.equal(out.done, true); }); it('only the hold phase can be finished early (tap to continue), not the count', () => { const seq = Reward.createSequence({}); seq.setResult({ before: 0, after: 100 }); seq.tick(2); assert.equal(seq.phase, 'count'); assert.equal(seq.finish(), false); assert.equal(seq.phase, 'count'); while (seq.phase === 'count') { seq.tick(0.1); } assert.equal(seq.phase, 'hold'); assert.equal(seq.counter, 100); assert.equal(seq.finish(), true); assert.equal(seq.tick(0.01).done, true); }); });