BlueGit

projects-timfalken-com meteors

Files

meteors / test / planet.test.js

planet.test.js

planet.test.js 11.6 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const Planet = require('../src/planet.js');
6const almost = (a, b, eps) => Math.abs(a - b) <= eps;
7
8describe('Planet field and defaults', () =>
9{
10 it('has a 10 by 13 field', () =>
11 {
12 assert.equal(Planet.FIELD_WIDTH, 10);
13 assert.equal(Planet.FIELD_HEIGHT, 13);
14 });
15
16 it('default planet is Terra with mid-range physics', () =>
17 {
18 const p = Planet.defaultPlanet();
19 assert.equal(p.name, 'Terra');
20 assert.equal(p.people, 'Terrans');
21 assert.equal(p.jumpsToClear, 2);
22 assert.equal(p.fallTime, 3);
23 assert.equal(p.riseTime, 1.5);
24 assert.equal(p.colors.length, 5);
25 assert.equal(p.baseJumpHeight, 1.5 * 13 / 2);
26 assert.equal(p.fallSpeed, 13 / 3);
27 assert.equal(p.clusterFallSpeed, 0.5 * 13 / 3);
28 });
29
30 it('clamps gravity, air and mass to the stage-3 ranges', () =>
31 {
32 const lo = Planet.createPlanet({ jumpsToClear: 0, fallTime: 0, riseTime: 0 });
33 assert.equal(lo.jumpsToClear, 2, 'gravity minimum is 2 since t1199');
34 assert.equal(Planet.createPlanet({ jumpsToClear: 1.5 }).jumpsToClear, 2);
35 assert.equal(Planet.createPlanet({ jumpsToClear: 2.4 }).jumpsToClear, 2.4);
36 assert.equal(lo.fallTime, 1);
37 assert.equal(lo.riseTime, 0.5);
38
39 const hi = Planet.createPlanet({ jumpsToClear: 9, fallTime: 99, riseTime: 99 });
40 assert.equal(hi.jumpsToClear, 4);
41 assert.equal(hi.fallTime, 5);
42 assert.equal(hi.riseTime, 3);
43 });
44
45 it('keeps colors and shapes data-driven with a unique shape per color', () =>
46 {
47 const colors = Planet.defaultColors();
48 assert.equal(colors.length, 5);
49 const shapes = new Set(colors.map((c) => c.shape));
50 const images = new Set(colors.map((c) => c.image));
51 assert.equal(shapes.size, 5);
52 assert.equal(images.size, 5);
53 for (const c of colors)
54 {
55 assert.ok(c.hex.startsWith('#'));
56 assert.ok(c.image.startsWith('images/'));
57 assert.ok(typeof c.name === 'string' && c.name.length > 0);
58 }
59 });
60});
61
62describe('Jump height rules', () =>
63{
64 const planet = Planet.defaultPlanet();
65 const base = planet.baseJumpHeight;
66
67 it('a horizontal match of 3 uses the base jump height', () =>
68 {
69 assert.equal(Planet.jumpHeight(planet, 3, false), base);
70 });
71
72 it('a vertical match of 3 is 120% of a horizontal row', () =>
73 {
74 assert.equal(Planet.jumpHeight(planet, 3, true), base * 1.2);
75 });
76
77 it('each extra block beyond 3 adds 5% jump height', () =>
78 {
79 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, false) - base * 1.05) < 1e-12);
80 assert.ok(Math.abs(Planet.jumpHeight(planet, 5, false) - base * 1.10) < 1e-12);
81 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, true) - base * (1.2 + 0.05)) < 1e-12);
82 assert.ok(Math.abs(Planet.jumpHeight(planet, 6, true) - base * (1.2 + 0.15)) < 1e-12);
83 });
84
85 it('at min gravity (2, t1199) one horizontal-3 jump is 75% of the field', () =>
86 {
87 const easy = Planet.createPlanet({ jumpsToClear: 2 });
88 assert.equal(Planet.jumpHeight(easy, 3, false), 1.5 * 13 / 2);
89 assert.equal(Planet.JUMP_HEIGHT_SCALE, 1.5);
90 });
91
92 it('at max gravity four horizontal-3 jumps equal 1.5 times the field height', () =>
93 {
94 const hard = Planet.createPlanet({ jumpsToClear: 4 });
95 assert.equal(Planet.jumpHeight(hard, 3, false) * 4, 1.5 * 13);
96 });
97});
98
99describe('Launch and fall physics parameters', () =>
100{
101 it('a jump of height H peaks in riseTime at constant deceleration', () =>
102 {
103 const p = Planet.defaultPlanet();
104 const H = 6.5;
105 const v0 = Planet.launchVelocity(p, H);
106 const a = Planet.launchDeceleration(p, H);
107 assert.equal(v0, 2 * H / p.riseTime);
108 assert.equal(a, 2 * H / (p.riseTime * p.riseTime));
109 // v = v0 - a t; v=0 at t=riseTime; s = v0 t - 0.5 a t^2 = H
110 const t = p.riseTime;
111 assert.ok(Math.abs((v0 - a * t)) < 1e-12);
112 assert.ok(Math.abs((v0 * t - 0.5 * a * t * t) - H) < 1e-12);
113 });
114
115 it('apex time is independent of jump height (mass)', () =>
116 {
117 const p = Planet.defaultPlanet();
118 const a1 = Planet.launchDeceleration(p, 4);
119 const v1 = Planet.launchVelocity(p, 4);
120 const a2 = Planet.launchDeceleration(p, 10);
121 const v2 = Planet.launchVelocity(p, 10);
122 assert.ok(Math.abs(v1 / a1 - p.riseTime) < 1e-12);
123 assert.ok(Math.abs(v2 / a2 - p.riseTime) < 1e-12);
124 });
125
126 it('single-block fall speed is field height over fallTime; cluster float-down is half (t1124)', () =>
127 {
128 const slow = Planet.createPlanet({ fallTime: 5 });
129 const fast = Planet.createPlanet({ fallTime: 1 });
130 assert.equal(Planet.DESCENT_SPEED_SCALE, 0.5);
131 assert.equal(slow.fallSpeed, 13 / 5);
132 assert.equal(fast.fallSpeed, 13);
133 assert.equal(slow.clusterFallSpeed, 0.5 * 13 / 5);
134 assert.equal(fast.clusterFallSpeed, 0.5 * 13);
135 assert.ok(fast.fallSpeed > slow.fallSpeed);
136 // Relative difference between planets is unchanged (1 / fallTime).
137 assert.ok(Math.abs(fast.fallSpeed / slow.fallSpeed - 5) < 1e-12);
138 assert.ok(Math.abs(fast.clusterFallSpeed / slow.clusterFallSpeed - 5) < 1e-12);
139 // Apex ease duration is shared; cluster accel scales with the halved speed.
140 assert.ok(Math.abs(slow.clusterFallAccel - slow.clusterFallSpeed / slow.apexEaseTime) < 1e-12);
141 assert.ok(Math.abs(slow.fallAccel - slow.fallSpeed / slow.apexEaseTime) < 1e-12);
142 assert.equal(slow.clusterFallSpeed, Planet.DESCENT_SPEED_SCALE * slow.fallSpeed);
143 });
144});
145
146describe('Power and display values', () =>
147{
148 it('easiest planet (gravity 2 since t1199) has power 1/9, ~54% miss, shown as 10%', () =>
149 {
150 const easy = Planet.createPlanet({ jumpsToClear: 2, fallTime: 5, riseTime: 3 });
151 assert.ok(Math.abs(Planet.power(easy) - 1 / 9) < 1e-12);
152 assert.ok(Math.abs(Planet.missChance(easy) - (0.60 - 0.55 / 9)) < 1e-12);
153 assert.equal(Planet.displayPower(easy), 10);
154 // The Power scale itself is unchanged: an unclamped gravity-1 input
155 // would still be 0 (the formula keeps its old 1..4 axis).
156 assert.equal(Planet.power({ jumpsToClear: 1, fallTime: 5, riseTime: 3 }), 0);
157 });
158
159 it('hardest planet has power 1 and 5% miss chance', () =>
160 {
161 const hard = Planet.createPlanet({ jumpsToClear: 4, fallTime: 1, riseTime: 0.5 });
162 assert.ok(Math.abs(Planet.power(hard) - 1) < 1e-12);
163 assert.ok(Math.abs(Planet.missChance(hard) - 0.05) < 1e-12);
164 });
165
166 it('display values are numeric gravity, density, newtons and power %', () =>
167 {
168 const p = Planet.defaultPlanet();
169 const d = Planet.displayValues(p);
170 assert.ok(d.gravity > 0);
171 assert.ok(d.density > 0);
172 assert.ok(d.newtons > 0);
173 assert.equal(typeof d.power, 'number');
174 assert.ok(d.power >= 0 && d.power <= 100);
175 // Mid-range Terra (jumps=2) shows Earth-like 9.81
176 assert.ok(Math.abs(d.gravity - 9.81) < 1e-9);
177 });
178
179 it('displayed Power is 1 − missChance / 0.60, so 0% miss shows as 100% Power', () =>
180 {
181 const easy = { jumpsToClear: 1, fallTime: 5, riseTime: 3 }; // formula input, not a real planet (t1199)
182 const hard = Planet.createPlanet({ jumpsToClear: 4, fallTime: 1, riseTime: 0.5 });
183 const terra = Planet.defaultPlanet();
184 // 60% miss → 0% Power; 5% miss → 92% (not 100 — the floor is 5%, not 0).
185 assert.equal(Planet.displayPower(easy), 0);
186 assert.equal(Planet.displayPower(hard), Math.round((1 - 0.05 / 0.60) * 100));
187 assert.equal(Planet.displayPower(hard), 92);
188 const terraMiss = Planet.missChance(terra);
189 assert.equal(Planet.displayPower(terra), Math.round((1 - terraMiss / 0.60) * 100));
190 assert.equal(Planet.displayValues(terra).power, 44);
191 // Gameplay power() and missChance() are unchanged.
192 assert.equal(Planet.power(easy), 0);
193 assert.ok(Math.abs(Planet.power(hard) - 1) < 1e-12);
194 assert.equal(Planet.missChance(easy), 0.60);
195 assert.ok(Math.abs(Planet.missChance(hard) - 0.05) < 1e-12);
196 // Monotonic: a lower miss chance means a higher displayed Power.
197 assert.ok(Planet.displayPower(hard) > Planet.displayPower(terra));
198 assert.ok(Planet.displayPower(terra) > Planet.displayPower(easy));
199 });
200});
201
202describe('Planet catalog', () =>
203{
204 it('has 8–12 named worlds, each with a people name and five unique shapes', () =>
205 {
206 const all = Planet.allPlanets();
207 assert.ok(all.length >= 8 && all.length <= 12, 'catalog size ' + all.length);
208 const ids = new Set();
209 const images = new Set();
210 for (const p of all)
211 {
212 assert.ok(p.id && p.name && p.people, p.id);
213 assert.ok(!ids.has(p.id), 'duplicate id ' + p.id);
214 ids.add(p.id);
215 assert.equal(p.colors.length, 5);
216 const shapes = new Set(p.colors.map((c) => c.shape));
217 assert.equal(shapes.size, 5, p.id + ' shapes');
218 for (const c of p.colors)
219 {
220 assert.ok(c.image.startsWith('images/block-' + p.id + '-'), c.image);
221 assert.ok(!images.has(c.image), 'duplicate image ' + c.image);
222 images.add(c.image);
223 }
224 assert.ok(p.jumpsToClear >= 1 && p.jumpsToClear <= 4);
225 assert.ok(p.fallTime >= 1 && p.fallTime <= 5);
226 assert.ok(p.riseTime >= 0.5 && p.riseTime <= 3);
227 }
228 assert.ok(ids.has('terra'));
229 assert.equal(Planet.defaultPlanet().id, 'terra');
230 assert.equal(Planet.planetById('cinder').name, 'Cinder');
231 });
232
233 it('Aurel is the easiest (gravity 2 since t1199: Power 1/9, shown as 10%) and Cinder the hardest (gameplay Power 1, 5% miss, shown as 92%)', () =>
234 {
235 const aurel = Planet.planetById('aurel');
236 const cinder = Planet.planetById('cinder');
237 assert.equal(aurel.jumpsToClear, 2);
238 assert.ok(Math.abs(Planet.power(aurel) - 1 / 9) < 1e-12);
239 for (const p of Planet.officialPlanets()) assert.ok(Planet.power(p) >= Planet.power(aurel) - 1e-12, p.id);
240 assert.ok(Math.abs(Planet.power(cinder) - 1) < 1e-12);
241 assert.ok(Math.abs(Planet.missChance(cinder) - 0.05) < 1e-12);
242 assert.equal(Planet.displayValues(aurel).power, 10);
243 assert.equal(Planet.displayValues(cinder).power, 92);
244 });
245});
246
247describe('Gravity minimum and percentage display (t1199)', () =>
248{
249 it('constants: range 2..4, 9.81 g at 100%', () =>
250 {
251 assert.equal(Planet.GRAVITY_MIN, 2);
252 assert.equal(Planet.GRAVITY_MAX, 4);
253 assert.equal(Planet.EARTH_G, 9.81);
254 });
255
256 it('percentage is linear: (v − 1)·100, so 2 → 100%, 3 → 200%, 4 → 300%', () =>
257 {
258 assert.equal(Planet.gravityPercent(2), 100);
259 assert.equal(Planet.gravityPercent(2.5), 150);
260 assert.equal(Planet.gravityPercent(3), 200);
261 assert.equal(Planet.gravityPercent(4), 300);
262 assert.equal(Planet.gravityPercent(1), 100, 'below the minimum shows the minimum');
263 });
264
265 it('g follows the percentage: 9.81·(v − 1)', () =>
266 {
267 assert.ok(almost(Planet.gravityG(2), 9.81, 1e-12));
268 assert.ok(almost(Planet.gravityG(3), 19.62, 1e-12));
269 assert.ok(almost(Planet.gravityG(4), 29.43, 1e-12));
270 assert.ok(almost(Planet.displayValues(Planet.defaultPlanet()).gravity, 9.81, 1e-12));
271 assert.equal(Planet.displayValues(Planet.createPlanet({ jumpsToClear: 4 })).gravityPct, 300);
272 });
273
274 it('stats text shows "g 100% (9.81g)" for Terra and 300% for Cinder', () =>
275 {
276 assert.ok(Planet.formatStats(Planet.defaultPlanet()).startsWith('g 100% (9.81g) '));
277 assert.ok(Planet.formatStats(Planet.planetById('cinder')).startsWith('g 300% (29.43g) '));
278 assert.equal(Planet.formatGravity(2.5), '150% (14.71g)');
279 });
280
281 it('every standard planet has gravity ≥ 2', () =>
282 {
283 for (const p of Planet.officialPlanets()) assert.ok(p.jumpsToClear >= 2, p.id);
284 const raw = require('../server/lib/planets.json').planets;
285 for (const p of raw) assert.ok(p.jumpsToClear >= 2, 'catalog ' + p.id);
286 for (const id of ['aurel', 'lunara', 'zephyr']) assert.equal(Planet.planetById(id).jumpsToClear, 2, id);
287 });
288
289 it('a saved custom planet below 2 loads as 2', () =>
290 {
291 const p = Planet.planetFromData({ id: 'c_old', name: 'Oud', people: 'Ouden', jumpsToClear: 1, fallTime: 3, riseTime: 1.5, custom: true });
292 assert.equal(p.jumpsToClear, 2);
293 });
294
295 it('i18n has stats.gravity in every language', () =>
296 {
297 const cat = require('../server/lib/i18n.json');
298 for (const loc of cat.localeIds)
299 {
300 const s = cat.locales[loc]['stats.gravity'];
301 assert.ok(s && s.includes('{pct}') && s.includes('{g}'), loc);
302 }
303 });
304});