import test from "node:test"; import assert from "node:assert/strict"; import { fileURLToPath } from "node:url"; import { join } from "node:path"; import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js"; import { cycleState, isCompanionSpriteStem, parseSpriteStem, resolveState } from "../../shared/definitions/tileSprites.js"; import { decodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js"; import { GroundType, BlockType, normalizeGround, setBlockVisualState } from "../../shared/definitions/tiles.js"; import { diffChunks } from "../src/editor/service.js"; const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url))); test("sprite folders expose alphabetical states; first is default", () => { const catalog = buildSpriteCatalog(ASSET_ROOT); assert.deepEqual(catalog.blocks["stone-wall"].states, ["1", "2"]); assert.equal(catalog.blocks["stone-wall"].defaultState, "1"); assert.ok(catalog.images.some((image) => image.name === "block:stone-wall:2")); assert.equal(catalog.images[0].subImgTotal, 1); assert.equal(catalog.images[0].perRow, 1); }); test("bloom companions are not visual states", () => { const catalog = buildSpriteCatalog(ASSET_ROOT); assert.deepEqual(catalog.blocks.torch.states, ["torch"]); assert.equal(catalog.blocks.torch.blooms.torch, true); assert.ok(catalog.images.some((image) => image.name === "block:torch:torch:bloom")); assert.ok(catalog.images.some((image) => image.name === "item:stick:stick:equip")); assert.ok(!catalog.images.some((image) => image.name === "item:stick:stick:equip:bloom")); assert.ok(catalog.images.some((image) => image.name === "item:paper:paper")); assert.ok(catalog.images.some((image) => image.name === "mob:deer:deer")); assert.ok(catalog.images.some((image) => image.name === "mob:pigeon:pigeon-flying")); const pigeon = catalog.images.find((image) => image.name === "mob:pigeon:pigeon-flying"); assert.equal(pigeon.subImgTotal, 2); assert.equal(pigeon.perRow, 2); assert.ok(catalog.images.some((image) => image.name === "character:player-body-male:player-body-male")); assert.ok(!catalog.blocks.torch.states.includes("torch-bloom")); assert.ok(!catalog.blocks.torch.states.some((state) => state.includes("equip"))); assert.deepEqual(catalog.items.stick.states, ["stick"]); assert.equal(catalog.items.stick.equips.stick, true); }); test("sprite companion stems peel equip-bloom combinations", () => { assert.deepEqual(parseSpriteStem("torch"), { base: "torch", companion: false, bloom: false, equip: false }); assert.deepEqual(parseSpriteStem("torch-bloom"), { base: "torch", companion: true, bloom: true, equip: false }); assert.deepEqual(parseSpriteStem("stick-equip"), { base: "stick", companion: true, bloom: false, equip: true }); assert.deepEqual(parseSpriteStem("stick-equip-bloom"), { base: "stick", companion: true, bloom: true, equip: true }); assert.equal(isCompanionSpriteStem("1-equip-bloom"), true); assert.equal(isCompanionSpriteStem("1"), false); assert.equal(resolveState(["1", "2"], "1-bloom"), "1"); }); test("state cycling wraps and unknown values fall back to default", () => { assert.equal(resolveState(["closed", "open"], null), "closed"); assert.equal(resolveState(["closed", "open"], "open"), "open"); assert.equal(resolveState(["closed", "open"], "missing"), "closed"); assert.equal(cycleState(["1", "2"], "1", 1), "2"); assert.equal(cycleState(["1", "2"], "2", 1), "1"); assert.equal(cycleState(["1", "2"], "1", -1), "2"); }); test("legacy flower blocks get a tile-seeded colour on decode", () => { const data = decodeChunkBlob({ open: true, grounds: [], blocks: [{ typeId: BlockType.flower, state: "flower", config: null }], light: [] }, undefined, 0, 0); assert.notEqual(data.blocks[0].state, "flower"); assert.ok( ["red", "blue", "cyan", "violet", "yellow", "magenta", "white", "black", "pink"] .includes(data.blocks[0].state) ); const again = decodeChunkBlob({ open: true, grounds: [], blocks: [{ typeId: BlockType.flower, state: "flower", config: null }], light: [] }, undefined, 0, 0); assert.equal(again.blocks[0].state, data.blocks[0].state); const coloured = decodeChunkBlob({ open: true, grounds: [], blocks: [{ typeId: BlockType.flower, state: "blue", config: null }], light: [] }, undefined, 0, 0); assert.equal(coloured.blocks[0].state, "blue"); }); test("old numeric grounds decode as default visual state", () => { const data = decodeChunkBlob({ open: true, grounds: [GroundType.wood], blocks: [{ typeId: BlockType.stoneWall, config: null }], light: [0] }); assert.deepEqual(data.grounds[0], { typeId: GroundType.wood, state: null }); assert.equal(data.blocks[0].state, null); assert.equal(data.blocks[0].typeId, BlockType.stoneWall); }); test("gameplay can set a block visual state", () => { const data = createEmptyChunkData(); data.blocks[0] = { typeId: BlockType.stoneWall, state: null, config: null }; setBlockVisualState(data.blocks[0], "2"); assert.equal(data.blocks[0].state, "2"); }); test("chunk diffs mention visual state changes", () => { const previous = createEmptyChunkData(); const next = createEmptyChunkData(); previous.blocks[0] = { typeId: BlockType.stoneWall, state: "1", config: null }; next.blocks[0] = { typeId: BlockType.stoneWall, state: "2", config: null }; const bullets = diffChunks(0, 0, previous, next, true); assert.ok(bullets.some((line) => line.includes("state changed from 1 to 2"))); assert.equal(normalizeGround(GroundType.grass).typeId, GroundType.grass); });