BlueGit

timfa questward

Files

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

tile-states.test.js

tile-states.test.js 7.8 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, isCompanionSpriteStem, parseSpriteStem, resolveState } from "../../shared/definitions/tileSprites.js";
7import { decodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js";
8import { GroundType, BlockType, normalizeGround, normalizeBlock, setBlockVisualState } from "../../shared/definitions/tiles.js";
9import { WOOD_TIER_ORDER } from "../../shared/definitions/materialTiers.js";
10import { diffChunks } from "../src/editor/service.js";
11
12const ASSET_ROOT = join(fileURLToPath(new URL("../../client/assets", import.meta.url)));
13
14test("sprite folders expose alphabetical states; first is default", () =>
15{
16 const catalog = buildSpriteCatalog(ASSET_ROOT);
17 assert.equal(catalog.blocks["thick-stone-wall"].defaultState, "thick-stone-wall");
18 assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall"));
19 assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall-esw"));
20 assert.ok(catalog.blocks["thick-stone-wall"].states.includes("thick-stone-wall-nesw"));
21 assert.equal(catalog.blocks["thick-stone-wall"].states.length, 16);
22 assert.ok(catalog.images.some((image) => image.name === "block:thick-stone-wall:thick-stone-wall-nesw"));
23 assert.equal(catalog.blocks["thin-stone-wall"].defaultState, "thin-stone-wall");
24 assert.equal(catalog.blocks["thin-stone-wall"].states.length, 16);
25 assert.ok(catalog.images.some((image) => image.name === "block:thin-stone-wall:thin-stone-wall-wne"));
26 assert.equal(catalog.blocks["thick-smooth-stone-wall"].states.length, 16);
27 assert.equal(catalog.blocks["thin-smooth-stone-wall"].states.length, 16);
28 assert.ok(catalog.images.some((image) => image.name === "block:thick-smooth-stone-wall:thick-smooth-stone-wall"));
29 assert.equal(catalog.blocks["thick-brick-wall"].states.length, 16);
30 assert.equal(catalog.blocks["thin-brick-wall"].states.length, 16);
31 assert.ok(catalog.images.some((image) => image.name === "block:thick-brick-wall:thick-brick-wall-we"));
32 assert.ok(catalog.images.some((image) => image.name === "block:thick-wood-wall:pine-0"));
33 assert.ok(catalog.images.some((image) => image.name === "block:thin-wood-wall:oak-15"));
34 assert.equal(catalog.blocks["thick-wood-wall"].states.length, WOOD_TIER_ORDER.length * 16);
35 assert.equal(catalog.images[0].subImgTotal, 1);
36 assert.equal(catalog.images[0].perRow, 1);
37});
38
39test("bloom companions are not visual states", () =>
40{
41 const catalog = buildSpriteCatalog(ASSET_ROOT);
42 assert.deepEqual(catalog.blocks.torch.states, ["torch"]);
43 assert.equal(catalog.blocks.torch.blooms.torch, true);
44 assert.ok(catalog.images.some((image) => image.name === "block:torch:torch:bloom"));
45 assert.ok(catalog.images.some((image) => image.name === "item:stick:stick:equip"));
46 assert.ok(!catalog.images.some((image) => image.name === "item:stick:stick:equip:bloom"));
47 assert.ok(catalog.images.some((image) => image.name === "item:paper:paper"));
48 assert.ok(catalog.images.some((image) => image.name === "mob:deer:deer"));
49 assert.ok(catalog.images.some((image) => image.name === "mob:pigeon:pigeon-flying"));
50 const pigeon = catalog.images.find((image) => image.name === "mob:pigeon:pigeon-flying");
51 assert.equal(pigeon.subImgTotal, 2);
52 assert.equal(pigeon.perRow, 2);
53 const tinderEquip = catalog.images.find((image) => image.name === "item:tinderbox:tinderbox:equip");
54 assert.ok(tinderEquip);
55 assert.equal(tinderEquip.subImgTotal, 1);
56 assert.equal(tinderEquip.perRow, 1);
57 const tinderIcon = catalog.images.find((image) => image.name === "item:tinderbox:tinderbox");
58 assert.equal(tinderIcon.subImgTotal, 6);
59 assert.equal(tinderIcon.perRow, 3);
60 assert.ok(catalog.images.some((image) => image.name === "character:player-body-male:player-body-male"));
61 assert.ok(!catalog.blocks.torch.states.includes("torch-bloom"));
62 assert.ok(!catalog.blocks.torch.states.some((state) => state.includes("equip")));
63 assert.deepEqual(catalog.items.stick.states, ["stick"]);
64 assert.equal(catalog.items.stick.equips.stick, true);
65});
66
67test("sprite companion stems peel equip-bloom combinations", () =>
68{
69 assert.deepEqual(parseSpriteStem("torch"), { base: "torch", companion: false, bloom: false, equip: false });
70 assert.deepEqual(parseSpriteStem("torch-bloom"), { base: "torch", companion: true, bloom: true, equip: false });
71 assert.deepEqual(parseSpriteStem("stick-equip"), { base: "stick", companion: true, bloom: false, equip: true });
72 assert.deepEqual(parseSpriteStem("stick-equip-bloom"), { base: "stick", companion: true, bloom: true, equip: true });
73 assert.equal(isCompanionSpriteStem("1-equip-bloom"), true);
74 assert.equal(isCompanionSpriteStem("1"), false);
75 assert.equal(resolveState(["1", "2"], "1-bloom"), "1");
76});
77
78test("state cycling wraps and unknown values fall back to default", () =>
79{
80 assert.equal(resolveState(["closed", "open"], null), "closed");
81 assert.equal(resolveState(["closed", "open"], "open"), "open");
82 assert.equal(resolveState(["closed", "open"], "missing"), "closed");
83 assert.equal(cycleState(["1", "2"], "1", 1), "2");
84 assert.equal(cycleState(["1", "2"], "2", 1), "1");
85 assert.equal(cycleState(["1", "2"], "1", -1), "2");
86});
87
88test("legacy flower blocks get a tile-seeded colour on decode", () =>
89{
90 const data = decodeChunkBlob({
91 open: true,
92 grounds: [],
93 blocks: [{ typeId: BlockType.flower, state: "flower", config: null }],
94 light: []
95 }, undefined, 0, 0);
96 assert.notEqual(data.blocks[0].state, "flower");
97 assert.ok(
98 ["red", "blue", "cyan", "violet", "yellow", "magenta", "white", "black", "pink"]
99 .includes(data.blocks[0].state)
100 );
101 const again = decodeChunkBlob({
102 open: true,
103 grounds: [],
104 blocks: [{ typeId: BlockType.flower, state: "flower", config: null }],
105 light: []
106 }, undefined, 0, 0);
107 assert.equal(again.blocks[0].state, data.blocks[0].state);
108 const coloured = decodeChunkBlob({
109 open: true,
110 grounds: [],
111 blocks: [{ typeId: BlockType.flower, state: "blue", config: null }],
112 light: []
113 }, undefined, 0, 0);
114 assert.equal(coloured.blocks[0].state, "blue");
115});
116
117test("old numeric grounds decode as default visual state", () =>
118{
119 const data = decodeChunkBlob({
120 open: true,
121 grounds: [GroundType.wood],
122 blocks: [{ typeId: BlockType.stoneWall, config: null }],
123 light: [0]
124 });
125 assert.deepEqual(data.grounds[0], { typeId: GroundType.wood, state: null });
126 assert.equal(data.blocks[0].state, null);
127 assert.equal(data.blocks[0].typeId, BlockType.stoneWall);
128});
129
130test("gameplay can set a block visual state", () =>
131{
132 const data = createEmptyChunkData();
133 data.blocks[0] = { typeId: BlockType.stoneWall, state: null, config: null };
134 setBlockVisualState(data.blocks[0], "thick-stone-wall-e");
135 assert.equal(data.blocks[0].state, "thick-stone-wall-e");
136});
137
138test("chunk diffs mention visual state changes", () =>
139{
140 const previous = createEmptyChunkData();
141 const next = createEmptyChunkData();
142 previous.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-n", config: null };
143 next.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-e", config: null };
144 const bullets = diffChunks(0, 0, previous, next, true);
145 assert.ok(bullets.some((line) =>
146 line.includes("state changed from thick-stone-wall-n to thick-stone-wall-e")));
147 assert.equal(normalizeGround(GroundType.grass).typeId, GroundType.grass);
148});
149
150test("normalizeBlock migrates legacy numeric wall states to named suffixes", () =>
151{
152 const migrated = normalizeBlock({ typeId: BlockType.stoneWall, state: "14", config: null });
153 assert.equal(migrated.state, "thick-stone-wall-esw");
154 const alone = normalizeBlock({ typeId: BlockType.stoneWallThin, state: "0", config: null });
155 assert.equal(alone.state, "thin-stone-wall");
156 const corner = normalizeBlock({ typeId: BlockType.stoneWallThin, state: "19", config: null });
157 assert.equal(corner.state, "thin-stone-wall-ne");
158});