BlueGit

timfa questward

Files

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

tile-states.test.js

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