| 1 | const { describe, it } = require('node:test'); |
| 2 | const assert = require('node:assert/strict'); |
| 3 | const LevelFormat = require('../src/levelFormat.js'); |
| 4 | |
| 5 | describe('LevelFormat schema v1', () => { |
| 6 | it('validates default empty level in a pack', () => { |
| 7 | const pack = LevelFormat.defaultPack(); |
| 8 | pack.levels.push(LevelFormat.emptyLevel('Test', 5, 5)); |
| 9 | const out = LevelFormat.validatePack(pack); |
| 10 | assert.equal(out.format_version, 2); |
| 11 | assert.equal(out.levels[0].walls.length, 25); |
| 12 | assert.equal(out.levels[0].floors.length, 25); |
| 13 | assert.ok(Array.isArray(out.levels[0].lights)); |
| 14 | }); |
| 15 | |
| 16 | it('rejects unknown format_version', () => { |
| 17 | assert.throws(() => LevelFormat.validatePack({ format_version: 99, levels: [] }), /format_version/); |
| 18 | }); |
| 19 | |
| 20 | it('rejects walls length mismatch', () => { |
| 21 | const L = LevelFormat.emptyLevel('Bad', 4, 4); |
| 22 | L.walls.pop(); |
| 23 | assert.throws(() => LevelFormat.validateLevel(L, 0), /walls lengte/); |
| 24 | }); |
| 25 | |
| 26 | it('rejects unknown floor texture', () => { |
| 27 | const L = LevelFormat.emptyLevel('Bad', 4, 4); |
| 28 | L.floors[5] = 'marble'; |
| 29 | assert.throws(() => LevelFormat.validateLevel(L, 0), /onbekende textuur/); |
| 30 | }); |
| 31 | |
| 32 | it('migrates legacy Levels[] to pack with floors/ceilings/lights', () => { |
| 33 | const legacy = [{ |
| 34 | name: 'Old', |
| 35 | width: 3, |
| 36 | height: 3, |
| 37 | spawnX: 1, |
| 38 | spawnY: 1, |
| 39 | tiles: [1, 1, 1, 1, 0, 1, 1, 1, 1] |
| 40 | }]; |
| 41 | const pack = LevelFormat.fromLegacyLevels(legacy, [[{ x: 48, y: 48, facingDeg: 0 }]]); |
| 42 | assert.equal(pack.format_version, 2); |
| 43 | assert.equal(pack.levels[0].floors[4], 'stone'); |
| 44 | assert.equal(pack.levels[0].floors[0], null); |
| 45 | assert.equal(pack.levels[0].npcs.length, 1); |
| 46 | assert.deepEqual(pack.levels[0].lights, []); |
| 47 | LevelFormat.validatePack(pack); |
| 48 | }); |
| 49 | |
| 50 | it('findLevelIndex by id, name, and index', () => { |
| 51 | const pack = LevelFormat.defaultPack(); |
| 52 | const a = LevelFormat.emptyLevel('Alpha', 4, 4); |
| 53 | a.id = 'alpha'; |
| 54 | const b = LevelFormat.emptyLevel('Beta', 4, 4); |
| 55 | b.id = 'beta'; |
| 56 | pack.levels.push(a, b); |
| 57 | assert.equal(LevelFormat.findLevelIndex(pack, 'beta'), 1); |
| 58 | assert.equal(LevelFormat.findLevelIndex(pack, 'Alpha'), 0); |
| 59 | assert.equal(LevelFormat.findLevelIndex(pack, '1'), 1); |
| 60 | assert.equal(LevelFormat.findLevelIndex(pack, 'nope'), -1); |
| 61 | }); |
| 62 | |
| 63 | it('toRuntimeLevel maps walls→tiles and spawn yaw', () => { |
| 64 | const L = LevelFormat.emptyLevel('R', 4, 4); |
| 65 | L.spawn.yawRad = 1.25; |
| 66 | const rt = LevelFormat.toRuntimeLevel(L); |
| 67 | assert.equal(rt.tiles, L.walls); |
| 68 | assert.equal(rt.spawnYaw, 1.25); |
| 69 | assert.equal(rt.spawnX, L.spawn.tileX); |
| 70 | }); |
| 71 | |
| 72 | it('migrates v1 pack to v2 with ambient defaults', () => { |
| 73 | const v1 = { |
| 74 | format_version: 1, |
| 75 | textures: [{ id: 'stone', url: 'images/stone.png' }, { id: 'wood', url: 'images/wood.png' }], |
| 76 | levels: [LevelFormat.emptyLevel('Old', 4, 4)] |
| 77 | }; |
| 78 | v1.levels[0].id = 'old'; |
| 79 | delete v1.levels[0].ambient; |
| 80 | v1.format_version = 1; |
| 81 | const out = LevelFormat.validatePack(JSON.parse(JSON.stringify(v1))); |
| 82 | assert.equal(out.format_version, 2); |
| 83 | assert.equal(typeof out.levels[0].ambient, 'number'); |
| 84 | assert.ok(Array.isArray(out.levels[0].lights)); |
| 85 | }); |
| 86 | |
| 87 | it('parses build/levels.json', () => { |
| 88 | const fs = require('fs'); |
| 89 | const path = require('path'); |
| 90 | const text = fs.readFileSync(path.join(__dirname, '../build/levels.json'), 'utf8'); |
| 91 | const pack = LevelFormat.parse(text); |
| 92 | assert.ok(pack.levels.length >= 1); |
| 93 | assert.equal(pack.levels[0].id, 'level-1'); |
| 94 | assert.ok(Array.isArray(pack.levels[0].lights)); |
| 95 | }); |
| 96 | }); |