| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { fileURLToPath } from "node:url"; |
| 4 | import { join } from "node:path"; |
| 5 | import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js"; |
| 6 | import { cycleState, isCompanionSpriteStem, parseSpriteStem, resolveState } from "../../shared/definitions/tileSprites.js"; |
| 7 | import { decodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js"; |
| 8 | import { GroundType, BlockType, normalizeGround, setBlockVisualState } from "../../shared/definitions/tiles.js"; |
| 9 | import { diffChunks } from "../src/editor/service.js"; |
| 10 | |
| 11 | const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url))); |
| 12 | |
| 13 | test("sprite folders expose alphabetical states; first is default", () => |
| 14 | { |
| 15 | const catalog = buildSpriteCatalog(ASSET_ROOT); |
| 16 | assert.deepEqual(catalog.blocks["stone-wall"].states, ["1", "2"]); |
| 17 | assert.equal(catalog.blocks["stone-wall"].defaultState, "1"); |
| 18 | assert.ok(catalog.images.some((image) => image.name === "block:stone-wall:2")); |
| 19 | assert.equal(catalog.images[0].subImgTotal, 1); |
| 20 | assert.equal(catalog.images[0].perRow, 1); |
| 21 | }); |
| 22 | |
| 23 | test("bloom companions are not visual states", () => |
| 24 | { |
| 25 | const catalog = buildSpriteCatalog(ASSET_ROOT); |
| 26 | assert.deepEqual(catalog.blocks.torch.states, ["torch"]); |
| 27 | assert.equal(catalog.blocks.torch.blooms.torch, true); |
| 28 | assert.ok(catalog.images.some((image) => image.name === "block:torch:torch:bloom")); |
| 29 | assert.ok(catalog.images.some((image) => image.name === "item:stick:stick:equip")); |
| 30 | assert.ok(!catalog.images.some((image) => image.name === "item:stick:stick:equip:bloom")); |
| 31 | assert.ok(catalog.images.some((image) => image.name === "item:paper:paper")); |
| 32 | assert.ok(catalog.images.some((image) => image.name === "mob:deer:deer")); |
| 33 | assert.ok(catalog.images.some((image) => image.name === "mob:pigeon:pigeon-flying")); |
| 34 | const pigeon = catalog.images.find((image) => image.name === "mob:pigeon:pigeon-flying"); |
| 35 | assert.equal(pigeon.subImgTotal, 2); |
| 36 | assert.equal(pigeon.perRow, 2); |
| 37 | assert.ok(catalog.images.some((image) => image.name === "character:player-body-male:player-body-male")); |
| 38 | assert.ok(!catalog.blocks.torch.states.includes("torch-bloom")); |
| 39 | assert.ok(!catalog.blocks.torch.states.some((state) => state.includes("equip"))); |
| 40 | assert.deepEqual(catalog.items.stick.states, ["stick"]); |
| 41 | assert.equal(catalog.items.stick.equips.stick, true); |
| 42 | }); |
| 43 | |
| 44 | test("sprite companion stems peel equip-bloom combinations", () => |
| 45 | { |
| 46 | assert.deepEqual(parseSpriteStem("torch"), { base: "torch", companion: false, bloom: false, equip: false }); |
| 47 | assert.deepEqual(parseSpriteStem("torch-bloom"), { base: "torch", companion: true, bloom: true, equip: false }); |
| 48 | assert.deepEqual(parseSpriteStem("stick-equip"), { base: "stick", companion: true, bloom: false, equip: true }); |
| 49 | assert.deepEqual(parseSpriteStem("stick-equip-bloom"), { base: "stick", companion: true, bloom: true, equip: true }); |
| 50 | assert.equal(isCompanionSpriteStem("1-equip-bloom"), true); |
| 51 | assert.equal(isCompanionSpriteStem("1"), false); |
| 52 | assert.equal(resolveState(["1", "2"], "1-bloom"), "1"); |
| 53 | }); |
| 54 | |
| 55 | test("state cycling wraps and unknown values fall back to default", () => |
| 56 | { |
| 57 | assert.equal(resolveState(["closed", "open"], null), "closed"); |
| 58 | assert.equal(resolveState(["closed", "open"], "open"), "open"); |
| 59 | assert.equal(resolveState(["closed", "open"], "missing"), "closed"); |
| 60 | assert.equal(cycleState(["1", "2"], "1", 1), "2"); |
| 61 | assert.equal(cycleState(["1", "2"], "2", 1), "1"); |
| 62 | assert.equal(cycleState(["1", "2"], "1", -1), "2"); |
| 63 | }); |
| 64 | |
| 65 | test("legacy flower blocks get a tile-seeded colour on decode", () => |
| 66 | { |
| 67 | const data = decodeChunkBlob({ |
| 68 | open: true, |
| 69 | grounds: [], |
| 70 | blocks: [{ typeId: BlockType.flower, state: "flower", config: null }], |
| 71 | light: [] |
| 72 | }, undefined, 0, 0); |
| 73 | assert.notEqual(data.blocks[0].state, "flower"); |
| 74 | assert.ok( |
| 75 | ["red", "blue", "cyan", "violet", "yellow", "magenta", "white", "black", "pink"] |
| 76 | .includes(data.blocks[0].state) |
| 77 | ); |
| 78 | const again = decodeChunkBlob({ |
| 79 | open: true, |
| 80 | grounds: [], |
| 81 | blocks: [{ typeId: BlockType.flower, state: "flower", config: null }], |
| 82 | light: [] |
| 83 | }, undefined, 0, 0); |
| 84 | assert.equal(again.blocks[0].state, data.blocks[0].state); |
| 85 | const coloured = decodeChunkBlob({ |
| 86 | open: true, |
| 87 | grounds: [], |
| 88 | blocks: [{ typeId: BlockType.flower, state: "blue", config: null }], |
| 89 | light: [] |
| 90 | }, undefined, 0, 0); |
| 91 | assert.equal(coloured.blocks[0].state, "blue"); |
| 92 | }); |
| 93 | |
| 94 | test("old numeric grounds decode as default visual state", () => |
| 95 | { |
| 96 | const data = decodeChunkBlob({ |
| 97 | open: true, |
| 98 | grounds: [GroundType.wood], |
| 99 | blocks: [{ typeId: BlockType.stoneWall, config: null }], |
| 100 | light: [0] |
| 101 | }); |
| 102 | assert.deepEqual(data.grounds[0], { typeId: GroundType.wood, state: null }); |
| 103 | assert.equal(data.blocks[0].state, null); |
| 104 | assert.equal(data.blocks[0].typeId, BlockType.stoneWall); |
| 105 | }); |
| 106 | |
| 107 | test("gameplay can set a block visual state", () => |
| 108 | { |
| 109 | const data = createEmptyChunkData(); |
| 110 | data.blocks[0] = { typeId: BlockType.stoneWall, state: null, config: null }; |
| 111 | setBlockVisualState(data.blocks[0], "2"); |
| 112 | assert.equal(data.blocks[0].state, "2"); |
| 113 | }); |
| 114 | |
| 115 | test("chunk diffs mention visual state changes", () => |
| 116 | { |
| 117 | const previous = createEmptyChunkData(); |
| 118 | const next = createEmptyChunkData(); |
| 119 | previous.blocks[0] = { typeId: BlockType.stoneWall, state: "1", config: null }; |
| 120 | next.blocks[0] = { typeId: BlockType.stoneWall, state: "2", config: null }; |
| 121 | const bullets = diffChunks(0, 0, previous, next, true); |
| 122 | assert.ok(bullets.some((line) => line.includes("state changed from 1 to 2"))); |
| 123 | assert.equal(normalizeGround(GroundType.grass).typeId, GroundType.grass); |
| 124 | }); |