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, normalizeBlock, setBlockVisualState } from "../../shared/definitions/tiles.js"; import { WOOD_TIER_ORDER } from "../../shared/definitions/materialTiers.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.equal(catalog.blocks["thick-stone-wall"].defaultState, "thick-stone-wall"); assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall")); assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall-esw")); assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall-nesw")); assert.equal(catalog.blocks["thick-stone-wall"].states.length, 16); assert.ok(catalog.images.some((image) => image.name === "block:thick-stone-wall:thick-stone-wall-nesw")); assert.equal(catalog.blocks["thin-stone-wall"].defaultState, "thin-stone-wall"); assert.equal(catalog.blocks["thin-stone-wall"].states.length, 16); assert.ok(catalog.images.some((image) => image.name === "block:thin-stone-wall:thin-stone-wall-wne")); assert.equal(catalog.blocks["thin-smooth-stone-wall"].states.length, 16); assert.ok(catalog.images.some((image) => image.name === "block:thin-smooth-stone-wall:thin-smooth-stone-wall")); assert.equal(catalog.blocks["thick-smooth-stone-wall"], undefined); assert.equal(catalog.blocks["thick-brick-wall"].states.length, 16); assert.equal(catalog.blocks["thin-brick-wall"].states.length, 16); assert.ok(catalog.images.some((image) => image.name === "block:thick-brick-wall:thick-brick-wall-we")); assert.ok(catalog.images.some((image) => image.name === "block:thick-wood-wall:pine-0")); assert.ok(catalog.images.some((image) => image.name === "block:thin-wood-wall:oak-15")); assert.equal(catalog.blocks["thick-wood-wall"].states.length, WOOD_TIER_ORDER.length * 16); assert.equal(catalog.blocks["thick-cliff"].states.length, 16); assert.equal(catalog.blocks["thin-cliff"].states.length, 16); assert.ok(catalog.images.some((image) => image.name === "block:thick-cliff:thick-cliff-nesw")); 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); const tinderEquip = catalog.images.find((image) => image.name === "item:tinderbox:tinderbox:equip"); assert.ok(tinderEquip); assert.equal(tinderEquip.subImgTotal, 1); assert.equal(tinderEquip.perRow, 1); const tinderIcon = catalog.images.find((image) => image.name === "item:tinderbox:tinderbox"); assert.equal(tinderIcon.subImgTotal, 6); assert.equal(tinderIcon.perRow, 3); 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, preview: false }); assert.deepEqual(parseSpriteStem("torch-bloom"), { base: "torch", companion: true, bloom: true, equip: false, preview: false }); assert.deepEqual(parseSpriteStem("stick-equip"), { base: "stick", companion: true, bloom: false, equip: true, preview: false }); assert.deepEqual(parseSpriteStem("stick-equip-bloom"), { base: "stick", companion: true, bloom: true, equip: true, preview: false }); assert.deepEqual(parseSpriteStem("water-preview"), { base: "water", companion: true, bloom: false, equip: false, preview: 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], "thick-stone-wall-e"); assert.equal(data.blocks[0].state, "thick-stone-wall-e"); }); test("chunk diffs mention visual state changes", () => { const previous = createEmptyChunkData(); const next = createEmptyChunkData(); previous.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-n", config: null }; next.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-e", config: null }; const bullets = diffChunks(0, 0, previous, next, true); assert.ok(bullets.some((line) => line.includes("state changed from thick-stone-wall-n to thick-stone-wall-e"))); assert.equal(normalizeGround(GroundType.grass).typeId, GroundType.grass); }); test("normalizeBlock migrates legacy numeric wall states to named suffixes", () => { const migrated = normalizeBlock({ typeId: BlockType.stoneWall, state: "14", config: null }); assert.equal(migrated.state, "thick-stone-wall-esw"); const alone = normalizeBlock({ typeId: BlockType.stoneWallThin, state: "0", config: null }); assert.equal(alone.state, "thin-stone-wall"); const corner = normalizeBlock({ typeId: BlockType.stoneWallThin, state: "19", config: null }); assert.equal(corner.state, "thin-stone-wall-ne"); });