test: reward spawn schedule and sequencing (t1173, t1174)
@@ -0,0 +1,204 @@
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 (t1174: 5 + t blocks/s)', () =>
32
{
33
it('block i leaves when 5t + t²/2 = i', () =>
34
{
35
assert.equal(Reward.RATE0, 5);
36
assert.equal(Reward.ACCEL, 1);
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 + 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 18 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(27))) < 1e-12);
58
assert.ok(Reward.spawnTime(1) > 0.19 && Reward.spawnTime(1) < 0.2);
59
assert.ok(Math.abs(Reward.spawnTime(252) - 18) < 1e-9);
60
assert.equal(Reward.spawnedBy(18, 253), 253);
61
assert.equal(Reward.spawnedBy(17.99, 253), 252);
62
assert.ok(Math.abs(Reward.countDuration(253) - (18 + Reward.FLIGHT)) < 1e-9);
63
});
64
65
it('spawnedBy matches floor(5t + t²/2) + 1, capped by n', () =>
66
{
67
for (let t = 0; t <= 20; t += 0.37)
68
{
69
const expect = Math.min(253, Math.floor(5 * t + t * t / 2 + 1e-9) + 1);
70
assert.equal(Reward.spawnedBy(t, 253), expect, 't=' + t);
71
}
72
assert.equal(Reward.spawnedBy(-1, 10), 0);
73
assert.equal(Reward.spawnedBy(5, 0), 0);
74
assert.equal(Reward.spawnedBy(100, 7), 7);
75
assert.equal(Reward.arrivedBy(Reward.FLIGHT - 0.01, 10), 0);
76
assert.equal(Reward.arrivedBy(Reward.FLIGHT, 10), 1);
77
});
78
});
79
80
describe('Reward sequence (t1173)', () =>
81
{
82
it('500 + 253 → counts every point from 500 up to 753 in ~18.7 s', () =>
83
{
84
const seq = Reward.createSequence({ won: true });
85
seq.setResult({ before: 500, after: 753 });
86
const r = runToEnd(seq, 1 / 60, 60);
87
assert.equal(r.last.phase, 'done');
88
assert.equal(r.spawned, 253);
89
assert.equal(r.arrived, 253);
90
assert.ok(r.monotonic, 'counter never goes down');
91
assert.equal(seq.counter, 753);
92
const counting = r.events.filter((e) => e.phase === 'count');
93
assert.ok(counting.length > 0);
94
assert.ok(counting[0].counter >= 500 && counting[0].counter < 510);
95
// intro + rise + count + hold
96
const expected = Reward.INTRO + Reward.RISE + Reward.countDuration(253) + Reward.HOLD;
97
assert.ok(Math.abs(r.t - expected) < 0.1, 't=' + r.t + ' expected ' + expected);
98
});
99
100
it('phases run intro → rise → wait → count → hold → done', () =>
101
{
102
const seq = Reward.createSequence({});
103
const phases = [];
104
const push = (p) => { if (phases[phases.length - 1] !== p) { phases.push(p); } };
105
push(seq.phase);
106
for (let i = 0; i < 60; i++) { push(seq.tick(0.05).phase); } // 3 s, no result yet
107
assert.deepEqual(phases, ['intro', 'rise', 'wait']);
108
assert.equal(seq.counter, 0);
109
seq.setResult({ before: 10, after: 13 });
110
for (let i = 0; i < 200; i++) { push(seq.tick(0.05).phase); }
111
assert.deepEqual(phases, ['intro', 'rise', 'wait', 'count', 'hold', 'done']);
112
assert.equal(seq.counter, 13);
113
});
114
115
it('title stays centred for one second before rising', () =>
116
{
117
const seq = Reward.createSequence({});
118
assert.equal(seq.tick(0.99).phase, 'intro');
119
assert.equal(seq.tick(0.02).phase, 'rise');
120
});
121
122
it('nothing is counted before the server result arrives', () =>
123
{
124
const seq = Reward.createSequence({ waitMax: 100 });
125
let spawned = 0;
126
for (let i = 0; i < 100; i++) { spawned += seq.tick(0.05).spawn.length; }
127
assert.equal(spawned, 0);
128
assert.equal(seq.phase, 'wait');
129
});
130
131
it('earned 0 shows the number briefly, no blocks', () =>
132
{
133
const seq = Reward.createSequence({});
134
seq.setResult({ before: 42, after: 42 });
135
const r = runToEnd(seq, 0.05, 30);
136
assert.equal(r.spawned, 0);
137
assert.equal(seq.counter, 42);
138
assert.ok(r.events.some((e) => e.phase === 'hold' && e.counter === 42));
139
assert.equal(r.last.phase, 'done');
140
const expected = Reward.INTRO + Reward.RISE + Reward.HOLD_ZERO;
141
assert.ok(Math.abs(r.t - expected) < 0.11);
142
});
143
144
it('server failure: no count, still continues', () =>
145
{
146
const seq = Reward.createSequence({});
147
seq.setResult(null);
148
const r = runToEnd(seq, 0.05, 30);
149
assert.equal(r.spawned, 0);
150
assert.equal(seq.failed, true);
151
assert.equal(r.last.phase, 'done');
152
});
153
154
it('no answer at all times out and continues', () =>
155
{
156
const seq = Reward.createSequence({ waitMax: 2 });
157
const r = runToEnd(seq, 0.05, 30);
158
assert.equal(r.last.phase, 'done');
159
assert.equal(seq.failed, true);
160
});
161
162
it('before > after or a missing before is clamped (never counts down)', () =>
163
{
164
const a = Reward.createSequence({});
165
a.setResult({ before: 900, after: 800 });
166
assert.equal(a.earned, 0);
167
assert.equal(a.counter, 800);
168
const b = Reward.createSequence({});
169
b.setResult({ after: 77 });
170
assert.equal(b.earned, 0);
171
assert.equal(b.before, 77);
172
});
173
174
it('a huge frame (tab in background) still lands exactly on the total', () =>
175
{
176
const seq = Reward.createSequence({});
177
seq.setResult({ before: 0, after: 40 });
178
let spawned = 0;
179
let out;
180
for (let i = 0; i < 10; i++)
181
{
182
out = seq.tick(5);
183
spawned += out.spawn.length;
184
}
185
assert.equal(spawned, 40);
186
assert.equal(seq.counter, 40);
187
assert.equal(out.done, true);
188
});
189
190
it('only the hold phase can be finished early (tap to continue), not the count', () =>
191
{
192
const seq = Reward.createSequence({});
193
seq.setResult({ before: 0, after: 100 });
194
seq.tick(2);
195
assert.equal(seq.phase, 'count');
196
assert.equal(seq.finish(), false);
197
assert.equal(seq.phase, 'count');
198
while (seq.phase === 'count') { seq.tick(0.1); }
199
assert.equal(seq.phase, 'hold');
200
assert.equal(seq.counter, 100);
201
assert.equal(seq.finish(), true);
202
assert.equal(seq.tick(0.01).done, true);
203
});
204
});
@@ -0,0 +1,204 @@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 (t1174: 5 + t blocks/s)', () =>32
{33
it('block i leaves when 5t + t²/2 = i', () =>34
{35
assert.equal(Reward.RATE0, 5);36
assert.equal(Reward.ACCEL, 1);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 + 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 18 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(27))) < 1e-12);58
assert.ok(Reward.spawnTime(1) > 0.19 && Reward.spawnTime(1) < 0.2);59
assert.ok(Math.abs(Reward.spawnTime(252) - 18) < 1e-9);60
assert.equal(Reward.spawnedBy(18, 253), 253);61
assert.equal(Reward.spawnedBy(17.99, 253), 252);62
assert.ok(Math.abs(Reward.countDuration(253) - (18 + Reward.FLIGHT)) < 1e-9);63
});64
65
it('spawnedBy matches floor(5t + t²/2) + 1, capped by n', () =>66
{67
for (let t = 0; t <= 20; t += 0.37)68
{69
const expect = Math.min(253, Math.floor(5 * t + t * t / 2 + 1e-9) + 1);70
assert.equal(Reward.spawnedBy(t, 253), expect, 't=' + t);71
}72
assert.equal(Reward.spawnedBy(-1, 10), 0);73
assert.equal(Reward.spawnedBy(5, 0), 0);74
assert.equal(Reward.spawnedBy(100, 7), 7);75
assert.equal(Reward.arrivedBy(Reward.FLIGHT - 0.01, 10), 0);76
assert.equal(Reward.arrivedBy(Reward.FLIGHT, 10), 1);77
});78
});79
80
describe('Reward sequence (t1173)', () =>81
{82
it('500 + 253 → counts every point from 500 up to 753 in ~18.7 s', () =>83
{84
const seq = Reward.createSequence({ won: true });85
seq.setResult({ before: 500, after: 753 });86
const r = runToEnd(seq, 1 / 60, 60);87
assert.equal(r.last.phase, 'done');88
assert.equal(r.spawned, 253);89
assert.equal(r.arrived, 253);90
assert.ok(r.monotonic, 'counter never goes down');91
assert.equal(seq.counter, 753);92
const counting = r.events.filter((e) => e.phase === 'count');93
assert.ok(counting.length > 0);94
assert.ok(counting[0].counter >= 500 && counting[0].counter < 510);95
// intro + rise + count + hold96
const expected = Reward.INTRO + Reward.RISE + Reward.countDuration(253) + Reward.HOLD;97
assert.ok(Math.abs(r.t - expected) < 0.1, 't=' + r.t + ' expected ' + expected);98
});99
100
it('phases run intro → rise → wait → count → hold → done', () =>101
{102
const seq = Reward.createSequence({});103
const phases = [];104
const push = (p) => { if (phases[phases.length - 1] !== p) { phases.push(p); } };105
push(seq.phase);106
for (let i = 0; i < 60; i++) { push(seq.tick(0.05).phase); } // 3 s, no result yet107
assert.deepEqual(phases, ['intro', 'rise', 'wait']);108
assert.equal(seq.counter, 0);109
seq.setResult({ before: 10, after: 13 });110
for (let i = 0; i < 200; i++) { push(seq.tick(0.05).phase); }111
assert.deepEqual(phases, ['intro', 'rise', 'wait', 'count', 'hold', 'done']);112
assert.equal(seq.counter, 13);113
});114
115
it('title stays centred for one second before rising', () =>116
{117
const seq = Reward.createSequence({});118
assert.equal(seq.tick(0.99).phase, 'intro');119
assert.equal(seq.tick(0.02).phase, 'rise');120
});121
122
it('nothing is counted before the server result arrives', () =>123
{124
const seq = Reward.createSequence({ waitMax: 100 });125
let spawned = 0;126
for (let i = 0; i < 100; i++) { spawned += seq.tick(0.05).spawn.length; }127
assert.equal(spawned, 0);128
assert.equal(seq.phase, 'wait');129
});130
131
it('earned 0 shows the number briefly, no blocks', () =>132
{133
const seq = Reward.createSequence({});134
seq.setResult({ before: 42, after: 42 });135
const r = runToEnd(seq, 0.05, 30);136
assert.equal(r.spawned, 0);137
assert.equal(seq.counter, 42);138
assert.ok(r.events.some((e) => e.phase === 'hold' && e.counter === 42));139
assert.equal(r.last.phase, 'done');140
const expected = Reward.INTRO + Reward.RISE + Reward.HOLD_ZERO;141
assert.ok(Math.abs(r.t - expected) < 0.11);142
});143
144
it('server failure: no count, still continues', () =>145
{146
const seq = Reward.createSequence({});147
seq.setResult(null);148
const r = runToEnd(seq, 0.05, 30);149
assert.equal(r.spawned, 0);150
assert.equal(seq.failed, true);151
assert.equal(r.last.phase, 'done');152
});153
154
it('no answer at all times out and continues', () =>155
{156
const seq = Reward.createSequence({ waitMax: 2 });157
const r = runToEnd(seq, 0.05, 30);158
assert.equal(r.last.phase, 'done');159
assert.equal(seq.failed, true);160
});161
162
it('before > after or a missing before is clamped (never counts down)', () =>163
{164
const a = Reward.createSequence({});165
a.setResult({ before: 900, after: 800 });166
assert.equal(a.earned, 0);167
assert.equal(a.counter, 800);168
const b = Reward.createSequence({});169
b.setResult({ after: 77 });170
assert.equal(b.earned, 0);171
assert.equal(b.before, 77);172
});173
174
it('a huge frame (tab in background) still lands exactly on the total', () =>175
{176
const seq = Reward.createSequence({});177
seq.setResult({ before: 0, after: 40 });178
let spawned = 0;179
let out;180
for (let i = 0; i < 10; i++)181
{182
out = seq.tick(5);183
spawned += out.spawn.length;184
}185
assert.equal(spawned, 40);186
assert.equal(seq.counter, 40);187
assert.equal(out.done, true);188
});189
190
it('only the hold phase can be finished early (tap to continue), not the count', () =>191
{192
const seq = Reward.createSequence({});193
seq.setResult({ before: 0, after: 100 });194
seq.tick(2);195
assert.equal(seq.phase, 'count');196
assert.equal(seq.finish(), false);197
assert.equal(seq.phase, 'count');198
while (seq.phase === 'count') { seq.tick(0.1); }199
assert.equal(seq.phase, 'hold');200
assert.equal(seq.counter, 100);201
assert.equal(seq.finish(), true);202
assert.equal(seq.tick(0.01).done, true);203
});204
});