BlueGit

timfa questward

Files

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

tile-states.test.js

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