BlueGit

timfa questward

Files

questward / server / tests / inventory.test.js

inventory.test.js

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