BlueGit

timfa questward

Files

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

tile-states.test.js

tile-states.test.js 8.2 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, preview: false });
73 assert.deepEqual(parseSpriteStem("torch-bloom"), { base: "torch", companion: true, bloom: true, equip: false, preview: false });
74 assert.deepEqual(parseSpriteStem("stick-equip"), { base: "stick", companion: true, bloom: false, equip: true, preview: false });
75 assert.deepEqual(parseSpriteStem("stick-equip-bloom"), { base: "stick", companion: true, bloom: true, equip: true, preview: false });
76 assert.deepEqual(parseSpriteStem("water-preview"), { base: "water", companion: true, bloom: false, equip: false, preview: true });
77 assert.equal(isCompanionSpriteStem("1-equip-bloom"), true);
78 assert.equal(isCompanionSpriteStem("1"), false);
79 assert.equal(resolveState(["1", "2"], "1-bloom"), "1");
80});
81
82test("state cycling wraps and unknown values fall back to default", () =>
83{
84 assert.equal(resolveState(["closed", "open"], null), "closed");
85 assert.equal(resolveState(["closed", "open"], "open"), "open");
86 assert.equal(resolveState(["closed", "open"], "missing"), "closed");
87 assert.equal(cycleState(["1", "2"], "1", 1), "2");
88 assert.equal(cycleState(["1", "2"], "2", 1), "1");
89 assert.equal(cycleState(["1", "2"], "1", -1), "2");
90});
91
92test("legacy flower blocks get a tile-seeded colour on decode", () =>
93{
94 const data = decodeChunkBlob({
95 open: true,
96 grounds: [],
97 blocks: [{ typeId: BlockType.flower, state: "flower", config: null }],
98 light: []
99 }, undefined, 0, 0);
100 assert.notEqual(data.blocks[0].state, "flower");
101 assert.ok(
102 ["red", "blue", "cyan", "violet", "yellow", "magenta", "white", "black", "pink"]
103 .includes(data.blocks[0].state)
104 );
105 const again = decodeChunkBlob({
106 open: true,
107 grounds: [],
108 blocks: [{ typeId: BlockType.flower, state: "flower", config: null }],
109 light: []
110 }, undefined, 0, 0);
111 assert.equal(again.blocks[0].state, data.blocks[0].state);
112 const coloured = decodeChunkBlob({
113 open: true,
114 grounds: [],
115 blocks: [{ typeId: BlockType.flower, state: "blue", config: null }],
116 light: []
117 }, undefined, 0, 0);
118 assert.equal(coloured.blocks[0].state, "blue");
119});
120
121test("old numeric grounds decode as default visual state", () =>
122{
123 const data = decodeChunkBlob({
124 open: true,
125 grounds: [GroundType.wood],
126 blocks: [{ typeId: BlockType.stoneWall, config: null }],
127 light: [0]
128 });
129 assert.deepEqual(data.grounds[0], { typeId: GroundType.wood, state: null });
130 assert.equal(data.blocks[0].state, null);
131 assert.equal(data.blocks[0].typeId, BlockType.stoneWall);
132});
133
134test("gameplay can set a block visual state", () =>
135{
136 const data = createEmptyChunkData();
137 data.blocks[0] = { typeId: BlockType.stoneWall, state: null, config: null };
138 setBlockVisualState(data.blocks[0], "thick-stone-wall-e");
139 assert.equal(data.blocks[0].state, "thick-stone-wall-e");
140});
141
142test("chunk diffs mention visual state changes", () =>
143{
144 const previous = createEmptyChunkData();
145 const next = createEmptyChunkData();
146 previous.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-n", config: null };
147 next.blocks[0] = { typeId: BlockType.stoneWall, state: "thick-stone-wall-e", config: null };
148 const bullets = diffChunks(0, 0, previous, next, true);
149 assert.ok(bullets.some((line) =>
150 line.includes("state changed from thick-stone-wall-n to thick-stone-wall-e")));
151 assert.equal(normalizeGround(GroundType.grass).typeId, GroundType.grass);
152});
153
154test("normalizeBlock migrates legacy numeric wall states to named suffixes", () =>
155{
156 const migrated = normalizeBlock({ typeId: BlockType.stoneWall, state: "14", config: null });
157 assert.equal(migrated.state, "thick-stone-wall-esw");
158 const alone = normalizeBlock({ typeId: BlockType.stoneWallThin, state: "0", config: null });
159 assert.equal(alone.state, "thin-stone-wall");
160 const corner = normalizeBlock({ typeId: BlockType.stoneWallThin, state: "19", config: null });
161 assert.equal(corner.state, "thin-stone-wall-ne");
162});