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, 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("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("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); });