BlueGit

timfa questward

Files

questward / server / tests / inventory.test.js

inventory.test.js

inventory.test.js 11.3 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import
4 {
5 emptyEquipment,
6 emptySlots,
7 insertStack,
8 moveInventorySlot,
9 takeFromSlot
10 } from "../../shared/items/inventory.js";
11import { ItemOriginKind } from "../../shared/items/origin.js";
12import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
13import { Component } from "../src/ecs/components.js";
14import { encodeChunkBlob, createEmptyChunkData } from "../src/world/chunkData.js";
15import
16 {
17 makeCharacter,
18 makeSimulation,
19 stepTimes
20 } from "./helpers/simulationHarness.js";
21
22function workerOf (simulation, entityId)
23{
24 const record = simulation.entityRecord(entityId);
25 assert.ok(record, "entity must be remembered");
26 return simulation.workers[record.workerId];
27}
28
29test("stackable items merge until max stack then need a new slot", () =>
30{
31 const slots = emptySlots();
32 const def = itemDefinition(ItemId.apple);
33 assert.equal(insertStack(slots, { itemId: ItemId.apple, quantity: 40 }, def).ok, true);
34 const first = insertStack(slots, { itemId: ItemId.apple, quantity: 40 }, def);
35 assert.equal(first.ok, true);
36 const second = insertStack(first.slots, { itemId: ItemId.apple, quantity: 40 }, def);
37 assert.equal(second.ok, true);
38 const taken = takeFromSlot(second.slots, 0, 10);
39 assert.equal(taken.ok, true);
40 assert.equal(taken.taken.quantity, 10);
41 assert.equal(taken.slots[0].quantity, 70);
42});
43
44test("moveInventorySlot moves, swaps, and merges stacks", () =>
45{
46 const def = itemDefinition(ItemId.apple);
47 let slots = emptySlots();
48 slots = insertStack(slots, { itemId: ItemId.apple, quantity: 5 }, def).slots;
49 slots[3] = { itemId: ItemId.paper, quantity: 1 };
50 const moved = moveInventorySlot(slots, 0, 2);
51 assert.equal(moved.ok, true);
52 assert.equal(moved.slots[0], null);
53 assert.equal(moved.slots[2]?.itemId, ItemId.apple);
54 assert.equal(moved.slots[2]?.quantity, 5);
55
56 const swapped = moveInventorySlot(moved.slots, 2, 3);
57 assert.equal(swapped.slots[2]?.itemId, ItemId.paper);
58 assert.equal(swapped.slots[3]?.itemId, ItemId.apple);
59
60 slots = emptySlots();
61 slots[0] = { itemId: ItemId.apple, quantity: 10 };
62 slots[1] = { itemId: ItemId.apple, quantity: 5 };
63 const merged = moveInventorySlot(slots, 0, 1);
64 assert.equal(merged.ok, true);
65 assert.equal(merged.slots[0], null);
66 assert.equal(merged.slots[1]?.quantity, 15);
67});
68
69test("player can rearrange bag slots via move-inventory-slot", () =>
70{
71 const harness = makeSimulation({ workerCount: 1 });
72 try
73 {
74 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
75 const entityId = harness.simulation.spawnCharacter(1, character);
76 const bag = harness.simulation.bagOf(entityId);
77 const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
78 assert.ok(appleSlot >= 0);
79 const emptySlot = bag.inventory.findIndex((slot) => !slot);
80 assert.ok(emptySlot >= 0);
81 harness.simulation.enqueue(1, {
82 type: "move-inventory-slot",
83 characterId: character.character_id,
84 fromSlotIndex: appleSlot,
85 toSlotIndex: emptySlot
86 });
87 const stepped = harness.simulation.step();
88 assert.equal(stepped.events.some((event) => event.type === "command-error"), false);
89 const after = harness.simulation.bagOf(entityId);
90 assert.equal(after.inventory[appleSlot], null);
91 assert.equal(after.inventory[emptySlot]?.itemId, ItemId.apple);
92 }
93 finally
94 { harness.close(); }
95});
96
97test("a player can drop and pick up an item on their tile", () =>
98{
99 const harness = makeSimulation({ workerCount: 1 });
100 try
101 {
102 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
103 const entityId = harness.simulation.spawnCharacter(1, character);
104 const bag = harness.simulation.bagOf(entityId);
105 const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
106 assert.ok(appleSlot >= 0);
107 harness.simulation.enqueue(1, {
108 type: "drop-item",
109 characterId: character.character_id,
110 slotIndex: appleSlot,
111 quantity: 1
112 });
113 const events = harness.simulation.step().events;
114 const appear = events.find((event) => event.type === "ground-item-appear");
115 assert.ok(appear);
116 assert.equal(appear.tileX, 8);
117 assert.equal(appear.itemId, ItemId.apple);
118 const afterDrop = harness.simulation.bagOf(entityId);
119 assert.equal(afterDrop.inventory[appleSlot].quantity, 2);
120 harness.simulation.enqueue(1, {
121 type: "pickup-item",
122 characterId: character.character_id,
123 groundItemId: appear.groundItemId
124 });
125 harness.simulation.step();
126 const afterPick = harness.simulation.bagOf(entityId);
127 assert.equal(afterPick.inventory[appleSlot].quantity, 3);
128 const snapshot = harness.simulation.snapshotChunk(0, 0);
129 assert.equal((snapshot.groundItems ?? []).length, 0);
130 }
131 finally
132 { harness.close(); }
133});
134
135test("walkTilesPerSecond controls how many tiles move per simulated second", () =>
136{
137 const harness = makeSimulation({ workerCount: 1 });
138 try
139 {
140 harness.simulation.applySettings({ walkTilesPerSecond: 2, simulationTickHz: 20 });
141 const slow = makeCharacter(harness.stateDb, { tileX: 2, tileY: 8 });
142 const slowId = harness.simulation.spawnCharacter(1, slow);
143 harness.simulation.enqueue(1, { type: "move-to", characterId: slow.character_id, tileX: 20, tileY: 8 });
144 stepTimes(harness.simulation, 20);
145 assert.equal(harness.simulation.entityRecord(slowId).tileX, 4);
146
147 harness.simulation.applySettings({ walkTilesPerSecond: 4, simulationTickHz: 20 });
148 harness.simulation.enqueue(1, { type: "move-to", characterId: slow.character_id, tileX: 20, tileY: 8 });
149 stepTimes(harness.simulation, 20);
150 assert.equal(harness.simulation.entityRecord(slowId).tileX, 8);
151 }
152 finally
153 { harness.close(); }
154});
155
156test("creation clothing equips cotton shirt with equip sprite", () =>
157{
158 const harness = makeSimulation({ workerCount: 1 });
159 try
160 {
161 const character = makeCharacter(harness.stateDb);
162 const entityId = harness.simulation.spawnCharacter(1, character);
163 const bag = harness.simulation.bagOf(entityId);
164 assert.equal(bag.equipment?.slots?.chest?.itemId, ItemId.clothShirt);
165 assert.ok(bag.equipment?.slots?.legs?.itemId);
166 const view = harness.simulation.entityRecord(entityId);
167 assert.ok(view.layers.some((layer) => layer.sprite === "item:clothing:shirt:equip"));
168 }
169 finally
170 { harness.close(); }
171});
172
173test("equipping from a stack takes one item and leaves the remainder", () =>
174{
175 const harness = makeSimulation({ workerCount: 1 });
176 try
177 {
178 const character = makeCharacter(harness.stateDb);
179 harness.stateDb.saveBag(character.character_id, {
180 inventory: (() =>
181 {
182 const slots = emptySlots();
183 slots[0] = {
184 itemId: ItemId.stick,
185 quantity: 3,
186 origins: [{ origin: { kind: ItemOriginKind.crafted, displayName: "Ada" }, quantity: 3 }]
187 };
188 return slots;
189 })(),
190 equipment: emptyEquipment(),
191 vault: [],
192 vaultReserved: []
193 });
194 const entityId = harness.simulation.spawnCharacter(1, character);
195 harness.simulation.enqueue(1, {
196 type: "equip-item",
197 characterId: character.character_id,
198 slotIndex: 0
199 });
200 harness.simulation.step();
201 const bag = harness.simulation.bagOf(entityId);
202 assert.equal(bag.inventory[0].quantity, 2);
203 assert.equal(bag.equipment.slots.weapon.quantity, 1);
204 assert.deepEqual(bag.equipment.slots.weapon.origins, [{
205 origin: { kind: ItemOriginKind.crafted, displayName: "Ada" },
206 quantity: 1
207 }]);
208 }
209 finally
210 { harness.close(); }
211});
212
213test("unequipping returns one item to inventory", () =>
214{
215 const harness = makeSimulation({ workerCount: 1 });
216 try
217 {
218 const character = makeCharacter(harness.stateDb);
219 const origin = { kind: ItemOriginKind.crafted, displayName: "Ada" };
220 harness.stateDb.saveBag(character.character_id, {
221 inventory: emptySlots(),
222 equipment: {
223 slots: {
224 weapon: {
225 itemId: ItemId.stick,
226 quantity: 1,
227 origins: [{ origin, quantity: 1 }]
228 }
229 }
230 },
231 vault: [],
232 vaultReserved: []
233 });
234 const entityId = harness.simulation.spawnCharacter(1, character);
235 harness.simulation.enqueue(1, {
236 type: "unequip-item",
237 characterId: character.character_id,
238 equipSlot: "weapon"
239 });
240 harness.simulation.step();
241 const bag = harness.simulation.bagOf(entityId);
242 assert.equal(bag.equipment.slots.weapon, null);
243 assert.equal(bag.inventory[0].quantity, 1);
244 assert.deepEqual(bag.inventory[0].origins, [{ origin, quantity: 1 }]);
245 }
246 finally
247 { harness.close(); }
248});
249
250test("unequipping uses a second slot when the item cannot stack in inventory", () =>
251{
252 const harness = makeSimulation({ workerCount: 1 });
253 try
254 {
255 const character = makeCharacter(harness.stateDb);
256 const origin = { kind: ItemOriginKind.crafted, displayName: "Ada" };
257 harness.stateDb.saveBag(character.character_id, {
258 inventory: (() =>
259 {
260 const slots = emptySlots();
261 slots[0] = {
262 itemId: ItemId.stick,
263 quantity: 1,
264 origins: [{ origin, quantity: 1 }]
265 };
266 return slots;
267 })(),
268 equipment: {
269 slots: {
270 weapon: {
271 itemId: ItemId.stick,
272 quantity: 1,
273 origins: [{ origin, quantity: 1 }]
274 }
275 }
276 },
277 vault: [],
278 vaultReserved: []
279 });
280 const entityId = harness.simulation.spawnCharacter(1, character);
281 harness.simulation.enqueue(1, {
282 type: "unequip-item",
283 characterId: character.character_id,
284 equipSlot: "weapon"
285 });
286 harness.simulation.step();
287 const bag = harness.simulation.bagOf(entityId);
288 assert.equal(bag.inventory[0].quantity, 1);
289 assert.equal(bag.inventory[1].quantity, 1);
290 assert.equal(bag.inventory.filter(Boolean).length, 2);
291 }
292 finally
293 { harness.close(); }
294});
295
296test("a spawner in a simulating open chunk creates deer up to maxAlive", () =>
297{
298 const harness = makeSimulation({ workerCount: 1 });
299 try
300 {
301 const data = createEmptyChunkData();
302 data.open = true;
303 data.spawners = [{
304 localX: 8,
305 localY: 8,
306 config: { spawnerId: "0:0:136", mobTypeId: "deer", maxAlive: 2, spawnRangeTiles: 3 }
307 }];
308 harness.worldDb.saveChunkVersion(0, 0, harness.worldDb.getActiveRevisionId(), true, encodeChunkBlob(data));
309 harness.simulation.reloadWorld();
310 const character = makeCharacter(harness.stateDb, { tileX: 4, tileY: 4 });
311 harness.simulation.spawnCharacter(1, character);
312 harness.simulation.step();
313 const deer = [...harness.simulation.directory.values()].filter((entity) => entity.firstName === "Deer");
314 assert.equal(deer.length, 2);
315 const pigeons = [...harness.simulation.directory.values()].filter((entity) => entity.firstName === "Pigeon");
316 assert.equal(pigeons.length, 0);
317 }
318 finally
319 { harness.close(); }
320});
321
322test("eating food restores player health", () =>
323{
324 const harness = makeSimulation({ workerCount: 1 });
325 try
326 {
327 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
328 const playerId = harness.simulation.spawnCharacter(1, character);
329 const worker = workerOf(harness.simulation, playerId);
330 const handle = worker.world.ecs.handleOf(playerId);
331 const health = worker.world.ecs.get(handle, Component.health);
332 health.current = 6;
333 const bag = harness.simulation.bagOf(playerId);
334 const appleSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
335 assert.ok(appleSlot >= 0);
336 harness.simulation.enqueue(1, {
337 type: "eat-item",
338 characterId: character.character_id,
339 slotIndex: appleSlot
340 });
341 harness.simulation.step();
342 const after = worker.world.ecs.get(handle, Component.health);
343 assert.equal(after.current, 10);
344 assert.equal(after.max, 10);
345 const afterBag = harness.simulation.bagOf(playerId);
346 assert.equal(afterBag.inventory[appleSlot].quantity, 2);
347 }
348 finally
349 { harness.close(); }
350});