BlueGit

timfa questward

Files

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

tile-states.test.js

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