BlueGit

timfa questward

Files

questward / server / tests / central-exchange.test.js

central-exchange.test.js

rweaks
timfa Tim Falken · pushed by timfa · 2 months ago
2b34e2dd History
central-exchange.test.js 6.3 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 NeighborBit,
5 computeNeighborConnectMask,
6 neighborConnectStateName,
7 parseNeighborConnectMask,
8 composeNeighborConnectState,
9 neighborConnectBlocksMatch,
10 parseMaterialNeighborConnectState
11} from "../../shared/world/neighborConnect.js";
12import { CENTRAL_EXCHANGE_STAFF, pickExchangeClerk } from "../../shared/market/exchange.js";
13import { MobId, normalizeSpawnerConfig, isCentralExchangeSpawner } from "../../shared/definitions/mobs.js";
14import { BlockType, BLOCK_DEFINITIONS } from "../../shared/definitions/tiles.js";
15import { ItemId } from "../../shared/definitions/items.js";
16import { emptyEquipment, emptySlots, addCoinsToSlots } from "../../shared/items/inventory.js";
17import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
18import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
19import { CHUNK_SIZE } from "../../shared/protocol/constants.js";
20import { Component } from "../src/ecs/components.js";
21
22test("neighbor connect masks cover all NESW combinations", () =>
23{
24 assert.equal(neighborConnectStateName(0), "0");
25 assert.equal(neighborConnectStateName(NeighborBit.n | NeighborBit.e | NeighborBit.s | NeighborBit.w), "15");
26 assert.equal(parseNeighborConnectMask("6"), NeighborBit.e | NeighborBit.s);
27 const mask = computeNeighborConnectMask((dx, dy) => dx === 1 || dy === -1);
28 assert.equal(mask, NeighborBit.n | NeighborBit.e);
29});
30
31test("material neighbor-connect states keep wood id and only match same material", () =>
32{
33 const def = BLOCK_DEFINITIONS[BlockType.table];
34 assert.equal(composeNeighborConnectState(def, "oak", 3), "oak-3");
35 assert.deepEqual(parseMaterialNeighborConnectState("birch-10", def), { material: "birch", mask: 10 });
36 const pine = { typeId: BlockType.table, state: "pine-0" };
37 const pineNeighbor = { typeId: BlockType.table, state: "pine-2" };
38 const oakNeighbor = { typeId: BlockType.table, state: "oak-2" };
39 assert.equal(neighborConnectBlocksMatch(pine, pineNeighbor, def), true);
40 assert.equal(neighborConnectBlocksMatch(pine, oakNeighbor, def), false);
41});
42
43test("CE staff entries include sex labels and pickExchangeClerk returns a name", () =>
44{
45 assert.ok(CENTRAL_EXCHANGE_STAFF.length >= 2);
46 for (const staff of CENTRAL_EXCHANGE_STAFF)
47 {
48 assert.ok(staff.name);
49 assert.ok(staff.sex === "male" || staff.sex === "female");
50 }
51 const name = pickExchangeClerk(() => 0);
52 assert.equal(name, CENTRAL_EXCHANGE_STAFF[0].name);
53});
54
55test("central exchange spawner config locks staff count and strips shop options", () =>
56{
57 const config = normalizeSpawnerConfig({
58 mobTypeId: MobId.centralExchange,
59 maxAlive: 2,
60 sells: [{ itemId: ItemId.apple, quantity: 1 }],
61 buysItems: true,
62 displayName: "ignored"
63 }, "ce:0");
64 assert.equal(config.centralExchange, true);
65 assert.equal(config.maxAlive, CENTRAL_EXCHANGE_STAFF.length);
66 assert.equal(config.sells.length, 0);
67 assert.equal(config.buysItems, false);
68 assert.equal(isCentralExchangeSpawner(config), true);
69});
70
71test("central exchange spawner places every clerk with matching names", () =>
72{
73 const harness = makeSimulation({ workerCount: 1 });
74 try
75 {
76 const data = createEmptyChunkData();
77 data.open = true;
78 data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" };
79 data.blocks[8 * CHUNK_SIZE + 10] = { typeId: BlockType.bank, state: null };
80 data.spawners = [{
81 localX: 8,
82 localY: 8,
83 config: normalizeSpawnerConfig({
84 mobTypeId: MobId.centralExchange,
85 spawnRangeTiles: 6,
86 maxWanderRangeTiles: 6,
87 equipment: { chest: ItemId.clothShirt }
88 }, "ce-test")
89 }];
90 harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data));
91 harness.simulation.reloadWorld();
92 harness.advance(50);
93 for (let i = 0; i < 30; i++)
94 { harness.simulation.step(); }
95
96 const record = harness.simulation.entityRecord(
97 harness.simulation.spawnCharacter(1, makeCharacter(harness.stateDb, { tileX: 7, tileY: 7 }))
98 );
99 const worker = harness.simulation.workers[record.workerId];
100 for (let i = 0; i < 40; i++)
101 { harness.simulation.step(); }
102
103 const clerks = [];
104 for (const entity of worker.world.ecs.query(Component.spawnOrigin, Component.appearance))
105 {
106 if (!entity.spawnOrigin?.centralExchange)
107 { continue; }
108 clerks.push(entity.appearance.firstName);
109 }
110 assert.equal(clerks.length, CENTRAL_EXCHANGE_STAFF.length);
111 for (const staff of CENTRAL_EXCHANGE_STAFF)
112 { assert.ok(clerks.includes(staff.name), staff.name); }
113 }
114 finally
115 { harness.close(); }
116});
117
118test("opening the market stations a CE clerk on the opposite side of the counter", () =>
119{
120 const harness = makeSimulation({ workerCount: 1 });
121 try
122 {
123 const data = createEmptyChunkData();
124 data.open = true;
125 // Counter at (9,8); player will stand at (8,8); staff station = (10,8).
126 data.blocks[8 * CHUNK_SIZE + 9] = { typeId: BlockType.marketplace, state: "0" };
127 data.blocks[8 * CHUNK_SIZE + 11] = { typeId: BlockType.bank, state: null };
128 data.spawners = [{
129 localX: 12,
130 localY: 8,
131 config: normalizeSpawnerConfig({
132 mobTypeId: MobId.centralExchange,
133 spawnRangeTiles: 8,
134 maxWanderRangeTiles: 8
135 }, "ce-station")
136 }];
137 harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data));
138 harness.simulation.reloadWorld();
139
140 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
141 const funded = addCoinsToSlots(emptySlots(), 10);
142 harness.stateDb.saveBag(character.character_id, {
143 inventory: funded.slots,
144 equipment: emptyEquipment(),
145 vault: [],
146 vaultReserved: []
147 });
148 const playerId = harness.simulation.spawnCharacter(1, character);
149 for (let i = 0; i < 40; i++)
150 { harness.simulation.step(); }
151
152 harness.simulation.enqueue(1, {
153 type: "open-market",
154 characterId: character.character_id
155 });
156 harness.simulation.step();
157
158 const record = harness.simulation.entityRecord(playerId);
159 const worker = harness.simulation.workers[record.workerId];
160 let stationed = false;
161 for (const entity of worker.world.ecs.query(Component.spawnOrigin))
162 {
163 if (!entity.spawnOrigin?.centralExchange)
164 { continue; }
165 if (entity.spawnOrigin.stationTileX === 10 && entity.spawnOrigin.stationTileY === 8)
166 { stationed = true; }
167 }
168 assert.equal(stationed, true);
169 }
170 finally
171 { harness.close(); }
172});