BlueGit

projects-timfalken-com meteors

Files

meteors / test / planet.test.js

planet.test.js

planet.test.js 8.7 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');
6
7describe('Planet field and defaults', () =>
8{
9 it('has a 10 by 13 field', () =>
10 {
11 assert.equal(Planet.FIELD_WIDTH, 10);
12 assert.equal(Planet.FIELD_HEIGHT, 13);
13 });
14
15 it('default planet is Terra with mid-range physics', () =>
16 {
17 const p = Planet.defaultPlanet();
18 assert.equal(p.name, 'Terra');
19 assert.equal(p.people, 'Terrans');
20 assert.equal(p.jumpsToClear, 2);
21 assert.equal(p.fallTime, 3);
22 assert.equal(p.riseTime, 1.5);
23 assert.equal(p.colors.length, 5);
24 assert.equal(p.baseJumpHeight, 1.5 * 13 / 2);
25 assert.equal(p.fallSpeed, 13 / 3);
26 assert.equal(p.clusterFallSpeed, 0.5 * 13 / 3);
27 });
28
29 it('clamps gravity, air and mass to the stage-3 ranges', () =>
30 {
31 const lo = Planet.createPlanet({ jumpsToClear: 0, fallTime: 0, riseTime: 0 });
32 assert.equal(lo.jumpsToClear, 1);
33 assert.equal(lo.fallTime, 1);
34 assert.equal(lo.riseTime, 0.5);
35
36 const hi = Planet.createPlanet({ jumpsToClear: 9, fallTime: 99, riseTime: 99 });
37 assert.equal(hi.jumpsToClear, 4);
38 assert.equal(hi.fallTime, 5);
39 assert.equal(hi.riseTime, 3);
40 });
41
42 it('keeps colors and shapes data-driven with a unique shape per color', () =>
43 {
44 const colors = Planet.defaultColors();
45 assert.equal(colors.length, 5);
46 const shapes = new Set(colors.map((c) => c.shape));
47 const images = new Set(colors.map((c) => c.image));
48 assert.equal(shapes.size, 5);
49 assert.equal(images.size, 5);
50 for (const c of colors)
51 {
52 assert.ok(c.hex.startsWith('#'));
53 assert.ok(c.image.startsWith('images/'));
54 assert.ok(typeof c.name === 'string' && c.name.length > 0);
55 }
56 });
57});
58
59describe('Jump height rules', () =>
60{
61 const planet = Planet.defaultPlanet();
62 const base = planet.baseJumpHeight;
63
64 it('a horizontal match of 3 uses the base jump height', () =>
65 {
66 assert.equal(Planet.jumpHeight(planet, 3, false), base);
67 });
68
69 it('a vertical match of 3 is 120% of a horizontal row', () =>
70 {
71 assert.equal(Planet.jumpHeight(planet, 3, true), base * 1.2);
72 });
73
74 it('each extra block beyond 3 adds 5% jump height', () =>
75 {
76 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, false) - base * 1.05) < 1e-12);
77 assert.ok(Math.abs(Planet.jumpHeight(planet, 5, false) - base * 1.10) < 1e-12);
78 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, true) - base * (1.2 + 0.05)) < 1e-12);
79 assert.ok(Math.abs(Planet.jumpHeight(planet, 6, true) - base * (1.2 + 0.15)) < 1e-12);
80 });
81
82 it('at min gravity one horizontal-3 jump is 50% taller than the field', () =>
83 {
84 const easy = Planet.createPlanet({ jumpsToClear: 1 });
85 assert.equal(Planet.jumpHeight(easy, 3, false), 1.5 * 13);
86 assert.equal(Planet.JUMP_HEIGHT_SCALE, 1.5);
87 });
88
89 it('at max gravity four horizontal-3 jumps equal 1.5 times the field height', () =>
90 {
91 const hard = Planet.createPlanet({ jumpsToClear: 4 });
92 assert.equal(Planet.jumpHeight(hard, 3, false) * 4, 1.5 * 13);
93 });
94});
95
96describe('Launch and fall physics parameters', () =>
97{
98 it('a jump of height H peaks in riseTime at constant deceleration', () =>
99 {
100 const p = Planet.defaultPlanet();
101 const H = 6.5;
102 const v0 = Planet.launchVelocity(p, H);
103 const a = Planet.launchDeceleration(p, H);
104 assert.equal(v0, 2 * H / p.riseTime);
105 assert.equal(a, 2 * H / (p.riseTime * p.riseTime));
106 // v = v0 - a t; v=0 at t=riseTime; s = v0 t - 0.5 a t^2 = H
107 const t = p.riseTime;
108 assert.ok(Math.abs((v0 - a * t)) < 1e-12);
109 assert.ok(Math.abs((v0 * t - 0.5 * a * t * t) - H) < 1e-12);
110 });
111
112 it('apex time is independent of jump height (mass)', () =>
113 {
114 const p = Planet.defaultPlanet();
115 const a1 = Planet.launchDeceleration(p, 4);
116 const v1 = Planet.launchVelocity(p, 4);
117 const a2 = Planet.launchDeceleration(p, 10);
118 const v2 = Planet.launchVelocity(p, 10);
119 assert.ok(Math.abs(v1 / a1 - p.riseTime) < 1e-12);
120 assert.ok(Math.abs(v2 / a2 - p.riseTime) < 1e-12);
121 });
122
123 it('single-block fall speed is field height over fallTime; cluster float-down is half (t1124)', () =>
124 {
125 const slow = Planet.createPlanet({ fallTime: 5 });
126 const fast = Planet.createPlanet({ fallTime: 1 });
127 assert.equal(Planet.DESCENT_SPEED_SCALE, 0.5);
128 assert.equal(slow.fallSpeed, 13 / 5);
129 assert.equal(fast.fallSpeed, 13);
130 assert.equal(slow.clusterFallSpeed, 0.5 * 13 / 5);
131 assert.equal(fast.clusterFallSpeed, 0.5 * 13);
132 assert.ok(fast.fallSpeed > slow.fallSpeed);
133 // Relative difference between planets is unchanged (1 / fallTime).
134 assert.ok(Math.abs(fast.fallSpeed / slow.fallSpeed - 5) < 1e-12);
135 assert.ok(Math.abs(fast.clusterFallSpeed / slow.clusterFallSpeed - 5) < 1e-12);
136 // Apex ease duration is shared; cluster accel scales with the halved speed.
137 assert.ok(Math.abs(slow.clusterFallAccel - slow.clusterFallSpeed / slow.apexEaseTime) < 1e-12);
138 assert.ok(Math.abs(slow.fallAccel - slow.fallSpeed / slow.apexEaseTime) < 1e-12);
139 assert.equal(slow.clusterFallSpeed, Planet.DESCENT_SPEED_SCALE * slow.fallSpeed);
140 });
141});
142
143describe('Power and display values', () =>
144{
145 it('easiest planet has power 0 and 60% miss chance', () =>
146 {
147 const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3 });
148 assert.equal(Planet.power(easy), 0);
149 assert.equal(Planet.missChance(easy), 0.60);
150 });
151
152 it('hardest planet has power 1 and 5% miss chance', () =>
153 {
154 const hard = Planet.createPlanet({ jumpsToClear: 4, fallTime: 1, riseTime: 0.5 });
155 assert.ok(Math.abs(Planet.power(hard) - 1) < 1e-12);
156 assert.ok(Math.abs(Planet.missChance(hard) - 0.05) < 1e-12);
157 });
158
159 it('display values are numeric gravity, density, newtons and power %', () =>
160 {
161 const p = Planet.defaultPlanet();
162 const d = Planet.displayValues(p);
163 assert.ok(d.gravity > 0);
164 assert.ok(d.density > 0);
165 assert.ok(d.newtons > 0);
166 assert.equal(typeof d.power, 'number');
167 assert.ok(d.power >= 0 && d.power <= 100);
168 // Mid-range Terra (jumps=2) shows Earth-like 9.81
169 assert.ok(Math.abs(d.gravity - 9.81) < 1e-9);
170 });
171
172 it('displayed Power is 1 − missChance / 0.60, so 0% miss shows as 100% Power', () =>
173 {
174 const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3 });
175 const hard = Planet.createPlanet({ jumpsToClear: 4, fallTime: 1, riseTime: 0.5 });
176 const terra = Planet.defaultPlanet();
177 // 60% miss → 0% Power; 5% miss → 92% (not 100 — the floor is 5%, not 0).
178 assert.equal(Planet.displayPower(easy), 0);
179 assert.equal(Planet.displayValues(easy).power, 0);
180 assert.equal(Planet.displayPower(hard), Math.round((1 - 0.05 / 0.60) * 100));
181 assert.equal(Planet.displayPower(hard), 92);
182 const terraMiss = Planet.missChance(terra);
183 assert.equal(Planet.displayPower(terra), Math.round((1 - terraMiss / 0.60) * 100));
184 assert.equal(Planet.displayValues(terra).power, 44);
185 // Gameplay power() and missChance() are unchanged.
186 assert.equal(Planet.power(easy), 0);
187 assert.ok(Math.abs(Planet.power(hard) - 1) < 1e-12);
188 assert.equal(Planet.missChance(easy), 0.60);
189 assert.ok(Math.abs(Planet.missChance(hard) - 0.05) < 1e-12);
190 // Monotonic: a lower miss chance means a higher displayed Power.
191 assert.ok(Planet.displayPower(hard) > Planet.displayPower(terra));
192 assert.ok(Planet.displayPower(terra) > Planet.displayPower(easy));
193 });
194});
195
196describe('Planet catalog', () =>
197{
198 it('has 8–12 named worlds, each with a people name and five unique shapes', () =>
199 {
200 const all = Planet.allPlanets();
201 assert.ok(all.length >= 8 && all.length <= 12, 'catalog size ' + all.length);
202 const ids = new Set();
203 const images = new Set();
204 for (const p of all)
205 {
206 assert.ok(p.id && p.name && p.people, p.id);
207 assert.ok(!ids.has(p.id), 'duplicate id ' + p.id);
208 ids.add(p.id);
209 assert.equal(p.colors.length, 5);
210 const shapes = new Set(p.colors.map((c) => c.shape));
211 assert.equal(shapes.size, 5, p.id + ' shapes');
212 for (const c of p.colors)
213 {
214 assert.ok(c.image.startsWith('images/block-' + p.id + '-'), c.image);
215 assert.ok(!images.has(c.image), 'duplicate image ' + c.image);
216 images.add(c.image);
217 }
218 assert.ok(p.jumpsToClear >= 1 && p.jumpsToClear <= 4);
219 assert.ok(p.fallTime >= 1 && p.fallTime <= 5);
220 assert.ok(p.riseTime >= 0.5 && p.riseTime <= 3);
221 }
222 assert.ok(ids.has('terra'));
223 assert.equal(Planet.defaultPlanet().id, 'terra');
224 assert.equal(Planet.planetById('cinder').name, 'Cinder');
225 });
226
227 it('Aurel is the easiest (gameplay Power 0, 60% miss, shown as 0%) and Cinder the hardest (gameplay Power 1, 5% miss, shown as 92%)', () =>
228 {
229 const aurel = Planet.planetById('aurel');
230 const cinder = Planet.planetById('cinder');
231 assert.equal(Planet.power(aurel), 0);
232 assert.equal(Planet.missChance(aurel), 0.60);
233 assert.ok(Math.abs(Planet.power(cinder) - 1) < 1e-12);
234 assert.ok(Math.abs(Planet.missChance(cinder) - 0.05) < 1e-12);
235 assert.equal(Planet.displayValues(aurel).power, 0);
236 assert.equal(Planet.displayValues(cinder).power, 92);
237 });
238});