BlueGit

timfa questward

Files

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

tile-states.test.js

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