BlueGit

timfa questward

Files

questward / server / tests / tile-states.test.js

tile-states.test.js

tile-states.test.js 2.6 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { fileURLToPath } from "node:url";
4import { join } from "node:path";
5import { buildSpriteCatalog } from "../src/assets/spriteCatalog.js";
6import { cycleState, resolveState } from "../../shared/definitions/tileSprites.js";
7import { decodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js";
8import { GroundType, BlockType, normalizeGround, setBlockVisualState } from "../../shared/definitions/tiles.js";
9import { diffChunks } from "../src/editor/service.js";
10
11const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url)));
12
13test("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
23test("state cycling wraps and unknown values fall back to default", () =>
24{
25 assert.equal(resolveState(["closed", "open"], null), "closed");
26 assert.equal(resolveState(["closed", "open"], "open"), "open");
27 assert.equal(resolveState(["closed", "open"], "missing"), "closed");
28 assert.equal(cycleState(["1", "2"], "1", 1), "2");
29 assert.equal(cycleState(["1", "2"], "2", 1), "1");
30 assert.equal(cycleState(["1", "2"], "1", -1), "2");
31});
32
33test("old numeric grounds decode as default visual state", () =>
34{
35 const data = decodeChunkBlob({
36 open: true,
37 grounds: [GroundType.wood],
38 blocks: [{ typeId: BlockType.stoneWall, config: null }],
39 light: [0]
40 });
41 assert.deepEqual(data.grounds[0], { typeId: GroundType.wood, state: null });
42 assert.equal(data.blocks[0].state, null);
43 assert.equal(data.blocks[0].typeId, BlockType.stoneWall);
44});
45
46test("gameplay can set a block visual state", () =>
47{
48 const data = createEmptyChunkData();
49 data.blocks[0] = { typeId: BlockType.stoneWall, state: null, config: null };
50 setBlockVisualState(data.blocks[0], "2");
51 assert.equal(data.blocks[0].state, "2");
52});
53
54test("chunk diffs mention visual state changes", () =>
55{
56 const previous = createEmptyChunkData();
57 const next = createEmptyChunkData();
58 previous.blocks[0] = { typeId: BlockType.stoneWall, state: "1", config: null };
59 next.blocks[0] = { typeId: BlockType.stoneWall, state: "2", config: null };
60 const bullets = diffChunks(0, 0, previous, next, true);
61 assert.ok(bullets.some((line) => line.includes("state changed from 1 to 2")));
62 assert.equal(normalizeGround(GroundType.grass).typeId, GroundType.grass);
63});