BlueGit

timfa questward

Files

questward / server / tests / inventory.test.js

inventory.test.js

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