BlueGit

projects-timfalken-com meteors

Files

meteors / test / planet.test.js

planet.test.js

planet.test.js 6.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');
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, 13 / 2);
25 assert.equal(p.fallSpeed, 13 / 3);
26 });
27
28 it('clamps gravity, air and mass to the stage-3 ranges', () =>
29 {
30 const lo = Planet.createPlanet({ jumpsToClear: 0, fallTime: 0, riseTime: 0 });
31 assert.equal(lo.jumpsToClear, 1);
32 assert.equal(lo.fallTime, 1);
33 assert.equal(lo.riseTime, 0.5);
34
35 const hi = Planet.createPlanet({ jumpsToClear: 9, fallTime: 99, riseTime: 99 });
36 assert.equal(hi.jumpsToClear, 4);
37 assert.equal(hi.fallTime, 5);
38 assert.equal(hi.riseTime, 3);
39 });
40
41 it('keeps colors and shapes data-driven with a unique shape per color', () =>
42 {
43 const colors = Planet.defaultColors();
44 assert.equal(colors.length, 5);
45 const shapes = new Set(colors.map((c) => c.shape));
46 const images = new Set(colors.map((c) => c.image));
47 assert.equal(shapes.size, 5);
48 assert.equal(images.size, 5);
49 for (const c of colors)
50 {
51 assert.ok(c.hex.startsWith('#'));
52 assert.ok(c.image.startsWith('images/'));
53 assert.ok(typeof c.name === 'string' && c.name.length > 0);
54 }
55 });
56});
57
58describe('Jump height rules', () =>
59{
60 const planet = Planet.defaultPlanet();
61 const base = planet.baseJumpHeight;
62
63 it('a horizontal match of 3 uses the base jump height', () =>
64 {
65 assert.equal(Planet.jumpHeight(planet, 3, false), base);
66 });
67
68 it('a vertical match of 3 is 120% of a horizontal row', () =>
69 {
70 assert.equal(Planet.jumpHeight(planet, 3, true), base * 1.2);
71 });
72
73 it('each extra block beyond 3 adds 5% jump height', () =>
74 {
75 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, false) - base * 1.05) < 1e-12);
76 assert.ok(Math.abs(Planet.jumpHeight(planet, 5, false) - base * 1.10) < 1e-12);
77 assert.ok(Math.abs(Planet.jumpHeight(planet, 4, true) - base * (1.2 + 0.05)) < 1e-12);
78 assert.ok(Math.abs(Planet.jumpHeight(planet, 6, true) - base * (1.2 + 0.15)) < 1e-12);
79 });
80
81 it('at min gravity one horizontal-3 jump equals the field height', () =>
82 {
83 const easy = Planet.createPlanet({ jumpsToClear: 1 });
84 assert.equal(Planet.jumpHeight(easy, 3, false), 13);
85 });
86
87 it('at max gravity four horizontal-3 jumps equal the field height', () =>
88 {
89 const hard = Planet.createPlanet({ jumpsToClear: 4 });
90 assert.equal(Planet.jumpHeight(hard, 3, false) * 4, 13);
91 });
92});
93
94describe('Launch and fall physics parameters', () =>
95{
96 it('a jump of height H peaks in riseTime at constant deceleration', () =>
97 {
98 const p = Planet.defaultPlanet();
99 const H = 6.5;
100 const v0 = Planet.launchVelocity(p, H);
101 const a = Planet.launchDeceleration(p, H);
102 assert.equal(v0, 2 * H / p.riseTime);
103 assert.equal(a, 2 * H / (p.riseTime * p.riseTime));
104 // v = v0 - a t; v=0 at t=riseTime; s = v0 t - 0.5 a t^2 = H
105 const t = p.riseTime;
106 assert.ok(Math.abs((v0 - a * t)) < 1e-12);
107 assert.ok(Math.abs((v0 * t - 0.5 * a * t * t) - H) < 1e-12);
108 });
109
110 it('apex time is independent of jump height (mass)', () =>
111 {
112 const p = Planet.defaultPlanet();
113 const a1 = Planet.launchDeceleration(p, 4);
114 const v1 = Planet.launchVelocity(p, 4);
115 const a2 = Planet.launchDeceleration(p, 10);
116 const v2 = Planet.launchVelocity(p, 10);
117 assert.ok(Math.abs(v1 / a1 - p.riseTime) < 1e-12);
118 assert.ok(Math.abs(v2 / a2 - p.riseTime) < 1e-12);
119 });
120
121 it('fall speed is field height over fallTime (air resistance)', () =>
122 {
123 const slow = Planet.createPlanet({ fallTime: 5 });
124 const fast = Planet.createPlanet({ fallTime: 1 });
125 assert.equal(slow.fallSpeed, 13 / 5);
126 assert.equal(fast.fallSpeed, 13);
127 assert.ok(fast.fallSpeed > slow.fallSpeed);
128 });
129});
130
131describe('Power and display values', () =>
132{
133 it('easiest planet has power 0 and 60% miss chance', () =>
134 {
135 const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3 });
136 assert.equal(Planet.power(easy), 0);
137 assert.equal(Planet.missChance(easy), 0.60);
138 });
139
140 it('hardest planet has power 1 and 5% miss chance', () =>
141 {
142 const hard = Planet.createPlanet({ jumpsToClear: 4, fallTime: 1, riseTime: 0.5 });
143 assert.ok(Math.abs(Planet.power(hard) - 1) < 1e-12);
144 assert.ok(Math.abs(Planet.missChance(hard) - 0.05) < 1e-12);
145 });
146
147 it('display values are numeric gravity, density, newtons and power %', () =>
148 {
149 const p = Planet.defaultPlanet();
150 const d = Planet.displayValues(p);
151 assert.ok(d.gravity > 0);
152 assert.ok(d.density > 0);
153 assert.ok(d.newtons > 0);
154 assert.equal(typeof d.power, 'number');
155 assert.ok(d.power >= 0 && d.power <= 100);
156 // Mid-range Terra (jumps=2) shows Earth-like 9.81
157 assert.ok(Math.abs(d.gravity - 9.81) < 1e-9);
158 });
159});
160
161describe('Planet catalog', () =>
162{
163 it('has 8–12 named worlds, each with a people name and five unique shapes', () =>
164 {
165 const all = Planet.allPlanets();
166 assert.ok(all.length >= 8 && all.length <= 12, 'catalog size ' + all.length);
167 const ids = new Set();
168 const images = new Set();
169 for (const p of all)
170 {
171 assert.ok(p.id && p.name && p.people, p.id);
172 assert.ok(!ids.has(p.id), 'duplicate id ' + p.id);
173 ids.add(p.id);
174 assert.equal(p.colors.length, 5);
175 const shapes = new Set(p.colors.map((c) => c.shape));
176 assert.equal(shapes.size, 5, p.id + ' shapes');
177 for (const c of p.colors)
178 {
179 assert.ok(c.image.startsWith('images/block-' + p.id + '-'), c.image);
180 assert.ok(!images.has(c.image), 'duplicate image ' + c.image);
181 images.add(c.image);
182 }
183 assert.ok(p.jumpsToClear >= 1 && p.jumpsToClear <= 4);
184 assert.ok(p.fallTime >= 1 && p.fallTime <= 5);
185 assert.ok(p.riseTime >= 0.5 && p.riseTime <= 3);
186 }
187 assert.ok(ids.has('terra'));
188 assert.equal(Planet.defaultPlanet().id, 'terra');
189 assert.equal(Planet.planetById('cinder').name, 'Cinder');
190 });
191
192 it('Aurel is the easiest (Power 0, 60% miss) and Cinder the hardest (Power 1, 5% miss)', () =>
193 {
194 const aurel = Planet.planetById('aurel');
195 const cinder = Planet.planetById('cinder');
196 assert.equal(Planet.power(aurel), 0);
197 assert.equal(Planet.missChance(aurel), 0.60);
198 assert.ok(Math.abs(Planet.power(cinder) - 1) < 1e-12);
199 assert.ok(Math.abs(Planet.missChance(cinder) - 0.05) < 1e-12);
200 assert.equal(Planet.displayValues(aurel).power, 0);
201 assert.equal(Planet.displayValues(cinder).power, 100);
202 });
203});