BlueGit

timfa questward

Files

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

tile-states.test.js

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