BlueGit

timfa questward

Files

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

tile-states.test.js

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