'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const Blocks = require('../src/blocks.js'); const Planet = require('../src/planet.js'); const Designer = require('../src/designer.js'); const fs = require('node:fs'); const path = require('node:path'); describe('Block registry (t1139)', () => { it('loads the append-only registry with Terra first', () => { const all = Blocks.all(); assert.ok(all.length >= 48, 'got ' + all.length); assert.equal(all[0].id, 'terra-ember'); assert.equal(all[4].id, 'terra-orchid'); assert.equal(all[5].id, 'rune-fehu'); assert.equal(Blocks.TERRA_COUNT, 5); assert.equal(Blocks.FIRST_THRESHOLD, 500); assert.ok(Math.abs(Blocks.GROWTH - 1.145) < 1e-12); }); it('Terra is free, the next block is 500, then 14.5% more and strictly increasing', () => { for (let i = 0; i < 5; i++) { assert.equal(Blocks.threshold(i), 0, 'index ' + i); } assert.equal(Blocks.threshold(5), 500); let prev = 500; const all = Blocks.all(); for (let i = 6; i < all.length; i++) { const t = Blocks.threshold(i); assert.ok(t > prev, 'index ' + i + ': ' + t + ' vs ' + prev); const expected = Math.max(prev + 1, Math.round(prev * 1.145)); assert.equal(t, expected); prev = t; } }); it('unlock is a requirement, not a spend', () => { assert.equal(Blocks.unlocked(0, 0), true); assert.equal(Blocks.unlocked(5, 499), false); assert.equal(Blocks.unlocked(5, 500), true); assert.equal(Blocks.unlocked(6, 500), false); assert.equal(Blocks.unlockedId('rune-fehu', 500), true); assert.equal(Blocks.unlockedId('no-such', 999999), false); }); it('present() marks each block with threshold and unlock state', () => { const list = Blocks.present(500); assert.equal(list.length, Blocks.all().length); assert.equal(list[0].unlocked, true); assert.equal(list[5].unlocked, true); assert.equal(list[5].threshold, 500); assert.equal(list[6].unlocked, false); assert.ok(list[5].image.startsWith('images/')); }); it('asColor() fills a planet slot from a registry id', () => { const c = Blocks.asColor('terra-ember', 2); assert.equal(c.id, 2); assert.equal(c.block, 'terra-ember'); assert.equal(c.name, 'Ember'); assert.ok(c.hex && c.image); }); it('every registry image file exists on disk', () => { const root = path.join(__dirname, '..'); for (const b of Blocks.all()) { const file = path.join(root, 'build', b.image); assert.ok(fs.existsSync(file), 'missing ' + b.image); } }); }); describe('Custom planets in the picker (t1140)', () => { it('allPlanets and pickerPlanets include owned customs, officialPlanets does not', () => { const before = Planet.officialPlanets().length; assert.ok(before >= 8 && before <= 12); Planet.setCustomPlanets([ { id: 'c_abc123', name: 'Home', people: 'Homers', jumpsToClear: 2, fallTime: 3, riseTime: 1.5, custom: true, owner_id: 'u1', colors: [0, 1, 2, 3, 4].map((i) => Blocks.asColor(Blocks.all()[i].id, i)) } ]); assert.equal(Planet.officialPlanets().length, before); assert.equal(Planet.customPlanets().length, 1); const all = Planet.allPlanets(); assert.equal(all.length, before + 1); assert.equal(all[all.length - 1].id, 'c_abc123'); assert.equal(all[all.length - 1].custom, true); const picker = Planet.pickerPlanets(); assert.ok(picker.some((p) => p.id === 'c_abc123')); assert.equal(Planet.planetById('c_abc123').name, 'Home'); Planet.setCustomPlanets([]); assert.equal(Planet.customPlanets().length, 0); assert.equal(Planet.allPlanets().length, before); }); it('rememberPlanet is used for opponents and is not in the picker', () => { const opp = Planet.rememberPlanet({ id: 'c_opponent', name: 'Rival', people: 'Rivals', jumpsToClear: 3, fallTime: 2, riseTime: 1, custom: true, owner_id: 'u2', colors: [0, 1, 2, 3, 4].map((i) => Blocks.asColor(Blocks.all()[i].id, i)) }); assert.equal(opp.name, 'Rival'); assert.equal(Planet.planetById('c_opponent').name, 'Rival'); assert.ok(!Planet.pickerPlanets().some((p) => p.id === 'c_opponent')); assert.ok(!Planet.allPlanets().some((p) => p.id === 'c_opponent')); Planet.forgetRemembered(); }); it('spriteKey for a registry colour uses the preloaded block: id', () => { const colors = [0, 1, 2, 3, 4].map((i) => Blocks.asColor(Blocks.all()[i].id, i)); const p = Planet.planetFromData({ id: 'c_key', name: 'K', people: 'K', jumpsToClear: 2, fallTime: 3, riseTime: 1.5, custom: true, colors: colors }); assert.equal(Planet.spriteKey(p, 0), 'block:terra-ember'); assert.equal(Planet.spriteKey(p, 4), 'block:terra-orchid'); const terra = Planet.planetById('terra'); assert.ok(!String(Planet.spriteKey(terra, 0)).startsWith('block:') || terra.colors[0].block, 'official Terra keeps its catalog sprite unless it has a block field'); }); }); describe('Designer draft uses the existing Power formula', () => { it('displayPower matches Planet.displayPower on a draft', () => { const colors = [0, 1, 2, 3, 4].map((i) => Blocks.asColor(Blocks.all()[i].id, i)); const p = Planet.createPlanet({ name: 'Draft', people: 'Drafts', jumpsToClear: 3, fallTime: 2, riseTime: 1, colors: colors }); const power = Planet.displayPower(p); assert.equal(typeof power, 'number'); assert.equal(power, Planet.displayValues(p).power); assert.ok(power >= 0 && power <= 100); }); it('Designer.defaultSlots returns Terra\'s five', () => { const slots = Designer.defaultSlots(); assert.equal(slots.length, 5); assert.equal(slots[0], 'terra-ember'); assert.equal(slots[4], 'terra-orchid'); }); });