BlueGit

projects-timfalken-com meteors

Files

meteors / test / blocks.test.js

blocks.test.js

blocks.test.js 5.6 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const Blocks = require('../src/blocks.js');
6const Planet = require('../src/planet.js');
7const Designer = require('../src/designer.js');
8const fs = require('node:fs');
9const path = require('node:path');
10
11describe('Block registry (t1139)', () =>
12{
13 it('loads the append-only registry with Terra first', () =>
14 {
15 const all = Blocks.all();
16 assert.ok(all.length >= 48, 'got ' + all.length);
17 assert.equal(all[0].id, 'terra-ember');
18 assert.equal(all[4].id, 'terra-orchid');
19 assert.equal(all[5].id, 'rune-fehu');
20 assert.equal(Blocks.TERRA_COUNT, 5);
21 assert.equal(Blocks.FIRST_THRESHOLD, 500);
22 assert.ok(Math.abs(Blocks.GROWTH - 1.145) < 1e-12);
23 });
24
25 it('Terra is free, the next block is 500, then 14.5% more and strictly increasing', () =>
26 {
27 for (let i = 0; i < 5; i++)
28 { assert.equal(Blocks.threshold(i), 0, 'index ' + i); }
29 assert.equal(Blocks.threshold(5), 500);
30 let prev = 500;
31 const all = Blocks.all();
32 for (let i = 6; i < all.length; i++)
33 {
34 const t = Blocks.threshold(i);
35 assert.ok(t > prev, 'index ' + i + ': ' + t + ' vs ' + prev);
36 const expected = Math.max(prev + 1, Math.round(prev * 1.145));
37 assert.equal(t, expected);
38 prev = t;
39 }
40 });
41
42 it('unlock is a requirement, not a spend', () =>
43 {
44 assert.equal(Blocks.unlocked(0, 0), true);
45 assert.equal(Blocks.unlocked(5, 499), false);
46 assert.equal(Blocks.unlocked(5, 500), true);
47 assert.equal(Blocks.unlocked(6, 500), false);
48 assert.equal(Blocks.unlockedId('rune-fehu', 500), true);
49 assert.equal(Blocks.unlockedId('no-such', 999999), false);
50 });
51
52 it('present() marks each block with threshold and unlock state', () =>
53 {
54 const list = Blocks.present(500);
55 assert.equal(list.length, Blocks.all().length);
56 assert.equal(list[0].unlocked, true);
57 assert.equal(list[5].unlocked, true);
58 assert.equal(list[5].threshold, 500);
59 assert.equal(list[6].unlocked, false);
60 assert.ok(list[5].image.startsWith('images/'));
61 });
62
63 it('asColor() fills a planet slot from a registry id', () =>
64 {
65 const c = Blocks.asColor('terra-ember', 2);
66 assert.equal(c.id, 2);
67 assert.equal(c.block, 'terra-ember');
68 assert.equal(c.name, 'Ember');
69 assert.ok(c.hex && c.image);
70 });
71
72 it('every registry image file exists on disk', () =>
73 {
74 const root = path.join(__dirname, '..');
75 for (const b of Blocks.all())
76 {
77 const file = path.join(root, 'build', b.image);
78 assert.ok(fs.existsSync(file), 'missing ' + b.image);
79 }
80 });
81});
82
83describe('Custom planets in the picker (t1140)', () =>
84{
85 it('allPlanets and pickerPlanets include owned customs, officialPlanets does not', () =>
86 {
87 const before = Planet.officialPlanets().length;
88 assert.ok(before >= 8 && before <= 12);
89 Planet.setCustomPlanets([
90 {
91 id: 'c_abc123',
92 name: 'Home',
93 people: 'Homers',
94 jumpsToClear: 2,
95 fallTime: 3,
96 riseTime: 1.5,
97 custom: true,
98 owner_id: 'u1',
99 colors: [0, 1, 2, 3, 4].map((i) => Blocks.asColor(Blocks.all()[i].id, i))
100 }
101 ]);
102 assert.equal(Planet.officialPlanets().length, before);
103 assert.equal(Planet.customPlanets().length, 1);
104 const all = Planet.allPlanets();
105 assert.equal(all.length, before + 1);
106 assert.equal(all[all.length - 1].id, 'c_abc123');
107 assert.equal(all[all.length - 1].custom, true);
108 const picker = Planet.pickerPlanets();
109 assert.ok(picker.some((p) => p.id === 'c_abc123'));
110 assert.equal(Planet.planetById('c_abc123').name, 'Home');
111 Planet.setCustomPlanets([]);
112 assert.equal(Planet.customPlanets().length, 0);
113 assert.equal(Planet.allPlanets().length, before);
114 });
115
116 it('rememberPlanet is used for opponents and is not in the picker', () =>
117 {
118 const opp = Planet.rememberPlanet({
119 id: 'c_opponent',
120 name: 'Rival',
121 people: 'Rivals',
122 jumpsToClear: 3,
123 fallTime: 2,
124 riseTime: 1,
125 custom: true,
126 owner_id: 'u2',
127 colors: [0, 1, 2, 3, 4].map((i) => Blocks.asColor(Blocks.all()[i].id, i))
128 });
129 assert.equal(opp.name, 'Rival');
130 assert.equal(Planet.planetById('c_opponent').name, 'Rival');
131 assert.ok(!Planet.pickerPlanets().some((p) => p.id === 'c_opponent'));
132 assert.ok(!Planet.allPlanets().some((p) => p.id === 'c_opponent'));
133 Planet.forgetRemembered();
134 });
135
136 it('spriteKey for a registry colour uses the preloaded block: id', () =>
137 {
138 const colors = [0, 1, 2, 3, 4].map((i) => Blocks.asColor(Blocks.all()[i].id, i));
139 const p = Planet.planetFromData({
140 id: 'c_key',
141 name: 'K',
142 people: 'K',
143 jumpsToClear: 2,
144 fallTime: 3,
145 riseTime: 1.5,
146 custom: true,
147 colors: colors
148 });
149 assert.equal(Planet.spriteKey(p, 0), 'block:terra-ember');
150 assert.equal(Planet.spriteKey(p, 4), 'block:terra-orchid');
151 const terra = Planet.planetById('terra');
152 assert.ok(!String(Planet.spriteKey(terra, 0)).startsWith('block:')
153 || terra.colors[0].block,
154 'official Terra keeps its catalog sprite unless it has a block field');
155 });
156});
157
158describe('Designer draft uses the existing Power formula', () =>
159{
160 it('displayPower matches Planet.displayPower on a draft', () =>
161 {
162 const colors = [0, 1, 2, 3, 4].map((i) => Blocks.asColor(Blocks.all()[i].id, i));
163 const p = Planet.createPlanet({
164 name: 'Draft',
165 people: 'Drafts',
166 jumpsToClear: 3,
167 fallTime: 2,
168 riseTime: 1,
169 colors: colors
170 });
171 const power = Planet.displayPower(p);
172 assert.equal(typeof power, 'number');
173 assert.equal(power, Planet.displayValues(p).power);
174 assert.ok(power >= 0 && power <= 100);
175 });
176
177 it('Designer.defaultSlots returns Terra\'s five', () =>
178 {
179 const slots = Designer.defaultSlots();
180 assert.equal(slots.length, 5);
181 assert.equal(slots[0], 'terra-ember');
182 assert.equal(slots[4], 'terra-orchid');
183 });
184});