BlueGit

timfa questward

Files

questward / server / tests / bank.test.js

bank.test.js

bank.test.js 15.2 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId, itemDefinition } from "../../shared/definitions/items.js";
5import { RangedItemId } from "../../shared/definitions/rangedItems.js";
6import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js";
7import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
8import {
9 makeCharacter,
10 makeSimulation
11} from "./helpers/simulationHarness.js";
12
13function placeBank (worldDb, tileX, tileY)
14{
15 const chunkX = Math.floor(tileX / 16);
16 const chunkY = Math.floor(tileY / 16);
17 const data = createEmptyChunkData();
18 data.open = true;
19 const localX = tileX - chunkX * 16;
20 const localY = tileY - chunkY * 16;
21 data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 };
22 worldDb.saveChunkVersion(
23 chunkX,
24 chunkY,
25 worldDb.getActiveRevisionId(),
26 true,
27 encodeChunkBlob(data)
28 );
29}
30
31function countItem (slots, itemId)
32{
33 return (slots ?? [])
34 .filter((slot) => slot?.itemId === itemId)
35 .reduce((sum, slot) => sum + (slot.quantity | 0), 0);
36}
37
38test("blank cheques top up inventory to 99 at a bank", () =>
39{
40 const harness = makeSimulation({ workerCount: 1 });
41 try
42 {
43 placeBank(harness.worldDb, 8, 8);
44 harness.simulation.reloadWorld();
45 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
46 const entityId = harness.simulation.spawnCharacter(1, character);
47 harness.simulation.enqueue(1, {
48 type: "get-blank-cheques",
49 characterId: character.character_id
50 });
51 harness.simulation.step();
52 const bag = harness.simulation.bagOf(entityId);
53 assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99);
54 }
55 finally
56 { harness.close(); }
57});
58
59test("bank deposit and withdraw move stacks with quantity", () =>
60{
61 const harness = makeSimulation({ workerCount: 1 });
62 try
63 {
64 placeBank(harness.worldDb, 8, 8);
65 harness.simulation.reloadWorld();
66 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
67 const entityId = harness.simulation.spawnCharacter(1, character);
68 const before = harness.simulation.bagOf(entityId);
69 const appleIndex = before.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
70 assert.ok(appleIndex >= 0);
71 const qty = before.inventory[appleIndex].quantity;
72 harness.simulation.enqueue(1, {
73 type: "bank-deposit",
74 characterId: character.character_id,
75 slotIndex: appleIndex,
76 quantity: 1
77 });
78 harness.simulation.step();
79 let bag = harness.simulation.bagOf(entityId);
80 assert.equal(countItem(bag.vault, ItemId.apple), 1);
81 assert.equal(bag.inventory[appleIndex]?.quantity ?? 0, qty - 1);
82 const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple);
83 harness.simulation.enqueue(1, {
84 type: "bank-withdraw",
85 characterId: character.character_id,
86 slotIndex: vaultIndex,
87 quantity: 1
88 });
89 harness.simulation.step();
90 bag = harness.simulation.bagOf(entityId);
91 assert.equal(countItem(bag.vault, ItemId.apple), 0);
92 }
93 finally
94 { harness.close(); }
95});
96
97test("bank withdraw transfers what fits and still reports not enough space", () =>
98{
99 const harness = makeSimulation({ workerCount: 1 });
100 try
101 {
102 placeBank(harness.worldDb, 8, 8);
103 harness.simulation.reloadWorld();
104 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
105 const entityId = harness.simulation.spawnCharacter(1, character);
106 const tipId = RangedItemId.bronzeArrowTip;
107 assert.equal(itemDefinition(tipId).maxStackSize, 99);
108
109 const inventory = emptySlots();
110 inventory[0] = { itemId: ItemId.apple, quantity: 1 };
111 for (let i = 2; i < inventory.length; i++)
112 { inventory[i] = { itemId: ItemId.paper, quantity: 1 }; }
113 harness.simulation.applyBag(entityId, {
114 inventory,
115 equipment: emptyEquipment(),
116 vault: [{ itemId: tipId, quantity: 120 }],
117 vaultReserved: []
118 });
119
120 harness.simulation.enqueue(1, {
121 type: "bank-withdraw",
122 characterId: character.character_id,
123 slotIndex: 0,
124 quantity: 120
125 });
126 const events = harness.simulation.step().events;
127 const bag = harness.simulation.bagOf(entityId);
128 assert.equal(countItem(bag.inventory, tipId), 99);
129 assert.equal(countItem(bag.vault, tipId), 21);
130 assert.ok(events.some((event) =>
131 event.type === "command-error" && event.message === "Not enough space."));
132 }
133 finally
134 { harness.close(); }
135});
136
137test("write cheque reserves vault goods and redeem returns them", () =>
138{
139 const harness = makeSimulation({ workerCount: 1 });
140 try
141 {
142 placeBank(harness.worldDb, 8, 8);
143 harness.simulation.reloadWorld();
144 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
145 const entityId = harness.simulation.spawnCharacter(1, character);
146 harness.simulation.enqueue(1, {
147 type: "get-blank-cheques",
148 characterId: character.character_id
149 });
150 harness.simulation.step();
151 let bag = harness.simulation.bagOf(entityId);
152 const appleIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
153 harness.simulation.enqueue(1, {
154 type: "bank-deposit",
155 characterId: character.character_id,
156 slotIndex: appleIndex,
157 quantity: null
158 });
159 harness.simulation.step();
160 bag = harness.simulation.bagOf(entityId);
161 const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque);
162 const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple);
163 harness.simulation.enqueue(1, {
164 type: "write-cheque",
165 characterId: character.character_id,
166 slotIndex: blankIndex,
167 vaultSlotIndex: vaultIndex,
168 quantity: 2
169 });
170 harness.simulation.step();
171 bag = harness.simulation.bagOf(entityId);
172 assert.equal((bag.vaultReserved ?? []).length, 1);
173 assert.equal(bag.vaultReserved[0].quantity, 2);
174 const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque);
175 assert.ok(chequeIndex >= 0);
176 harness.simulation.enqueue(1, {
177 type: "redeem-cheque",
178 characterId: character.character_id,
179 slotIndex: chequeIndex
180 });
181 harness.simulation.step();
182 bag = harness.simulation.bagOf(entityId);
183 assert.equal((bag.vaultReserved ?? []).length, 0);
184 assert.ok(countItem(bag.vault, ItemId.apple) >= 2);
185 }
186 finally
187 { harness.close(); }
188});
189
190test("redeem still works after bag is saved and reapplied", () =>
191{
192 const harness = makeSimulation({ workerCount: 1 });
193 try
194 {
195 placeBank(harness.worldDb, 8, 8);
196 harness.simulation.reloadWorld();
197 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
198 const entityId = harness.simulation.spawnCharacter(1, character);
199 harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id });
200 harness.simulation.step();
201 let bag = harness.simulation.bagOf(entityId);
202 harness.simulation.enqueue(1, {
203 type: "bank-deposit",
204 characterId: character.character_id,
205 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple),
206 quantity: null
207 });
208 harness.simulation.step();
209 bag = harness.simulation.bagOf(entityId);
210 harness.simulation.enqueue(1, {
211 type: "write-cheque",
212 characterId: character.character_id,
213 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque),
214 vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple),
215 quantity: 2
216 });
217 harness.simulation.step();
218 bag = harness.simulation.bagOf(entityId);
219 assert.ok(bag.vaultReserved?.[0]?.chequeId, "reserved stack must keep chequeId through bagView/save");
220 const saved = harness.stateDb.getBag(character.character_id);
221 assert.ok(saved.vaultReserved?.[0]?.chequeId);
222 harness.simulation.applyBag(entityId, saved);
223 const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque);
224 harness.simulation.enqueue(1, {
225 type: "redeem-cheque",
226 characterId: character.character_id,
227 slotIndex: chequeIndex
228 });
229 const result = harness.simulation.step();
230 assert.equal(result.events.some((event) => event.type === "command-error"), false);
231 bag = harness.simulation.bagOf(entityId);
232 assert.equal((bag.vaultReserved ?? []).length, 0);
233 assert.ok(countItem(bag.vault, ItemId.apple) >= 2);
234 }
235 finally
236 { harness.close(); }
237});
238
239test("bank deposit of a filled cheque redeems reserved goods", () =>
240{
241 const harness = makeSimulation({ workerCount: 1 });
242 try
243 {
244 placeBank(harness.worldDb, 8, 8);
245 harness.simulation.reloadWorld();
246 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
247 const entityId = harness.simulation.spawnCharacter(1, character);
248 harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id });
249 harness.simulation.step();
250 let bag = harness.simulation.bagOf(entityId);
251 const appleIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
252 harness.simulation.enqueue(1, {
253 type: "bank-deposit",
254 characterId: character.character_id,
255 slotIndex: appleIndex,
256 quantity: null
257 });
258 harness.simulation.step();
259 bag = harness.simulation.bagOf(entityId);
260 const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque);
261 const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple);
262 harness.simulation.enqueue(1, {
263 type: "write-cheque",
264 characterId: character.character_id,
265 slotIndex: blankIndex,
266 vaultSlotIndex: vaultIndex,
267 quantity: 2
268 });
269 harness.simulation.step();
270 bag = harness.simulation.bagOf(entityId);
271 const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque);
272 assert.ok(chequeIndex >= 0);
273 harness.simulation.enqueue(1, {
274 type: "bank-deposit",
275 characterId: character.character_id,
276 slotIndex: chequeIndex,
277 quantity: null
278 });
279 harness.simulation.step();
280 bag = harness.simulation.bagOf(entityId);
281 assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1);
282 assert.equal((bag.vaultReserved ?? []).length, 0);
283 assert.ok(countItem(bag.vault, ItemId.apple) >= 2);
284 }
285 finally
286 { harness.close(); }
287});
288
289test("new characters start with coin stacks in inventory", () =>
290{
291 const harness = makeSimulation({ workerCount: 1 });
292 try
293 {
294 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
295 const bag = harness.stateDb.getBag(character.character_id);
296 assert.equal(countItem(bag.inventory, ItemId.coins), 25);
297 }
298 finally
299 { harness.close(); }
300});
301
302test("legacy currency counters migrate into coin stacks", () =>
303{
304 const harness = makeSimulation({ workerCount: 1 });
305 try
306 {
307 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
308 harness.stateDb.saveBag(character.character_id, {
309 inventory: [],
310 equipment: emptyEquipment(),
311 currency: 100,
312 vault: [],
313 vaultReserved: [],
314 vaultCurrency: 50
315 });
316 const bag = harness.stateDb.getBag(character.character_id);
317 assert.equal(countItem(bag.inventory, ItemId.coins), 100);
318 assert.equal(countItem(bag.vault, ItemId.coins), 50);
319 }
320 finally
321 { harness.close(); }
322});
323
324test("stored inventory with excess blank cheques is capped on load", () =>
325{
326 const harness = makeSimulation({ workerCount: 1 });
327 try
328 {
329 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
330 harness.stateDb.saveBag(character.character_id, {
331 inventory: [{ itemId: ItemId.blankCheque, quantity: 120 }],
332 equipment: emptyEquipment(),
333 vault: [],
334 vaultReserved: []
335 });
336 const bag = harness.stateDb.getBag(character.character_id);
337 assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99);
338 }
339 finally
340 { harness.close(); }
341});
342test("blank cheque ground pickup stops at 99 carried", () =>
343{
344 const harness = makeSimulation({ workerCount: 1 });
345 try
346 {
347 placeBank(harness.worldDb, 8, 8);
348 harness.simulation.reloadWorld();
349 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
350 const entityId = harness.simulation.spawnCharacter(1, character);
351 harness.simulation.enqueue(1, {
352 type: "get-blank-cheques",
353 characterId: character.character_id
354 });
355 harness.simulation.step();
356 let bag = harness.simulation.bagOf(entityId);
357 const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque);
358 assert.ok(blankIndex >= 0);
359 harness.simulation.enqueue(1, {
360 type: "drop-item",
361 characterId: character.character_id,
362 slotIndex: blankIndex,
363 quantity: 10
364 });
365 const dropEvents = harness.simulation.step().events;
366 const appear = dropEvents.find((event) => event.type === "ground-item-appear");
367 assert.ok(appear);
368 harness.simulation.enqueue(1, {
369 type: "pickup-item",
370 characterId: character.character_id,
371 groundItemId: appear.groundItemId
372 });
373 harness.simulation.step();
374 bag = harness.simulation.bagOf(entityId);
375 assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99);
376 const snapshot = harness.simulation.snapshotChunk(0, 0);
377 assert.equal((snapshot.groundItems ?? []).length, 0);
378 }
379 finally
380 { harness.close(); }
381});
382
383test("dropping a filled cheque keeps reservation; despawn returns goods to vault", () =>
384{
385 const harness = makeSimulation({ workerCount: 1, now: 10_000 });
386 try
387 {
388 placeBank(harness.worldDb, 8, 8);
389 harness.simulation.reloadWorld();
390 harness.simulation.applySettings({ groundItemDespawnDurationMs: 1_000 });
391 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
392 const entityId = harness.simulation.spawnCharacter(1, character);
393 harness.simulation.enqueue(1, {
394 type: "get-blank-cheques",
395 characterId: character.character_id
396 });
397 harness.simulation.step();
398 let bag = harness.simulation.bagOf(entityId);
399 const appleQtyBefore = countItem(bag.inventory, ItemId.apple);
400 harness.simulation.enqueue(1, {
401 type: "bank-deposit",
402 characterId: character.character_id,
403 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple),
404 quantity: null
405 });
406 harness.simulation.step();
407 bag = harness.simulation.bagOf(entityId);
408 harness.simulation.enqueue(1, {
409 type: "write-cheque",
410 characterId: character.character_id,
411 slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque),
412 vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple),
413 quantity: 2
414 });
415 harness.simulation.step();
416 bag = harness.simulation.bagOf(entityId);
417 assert.equal((bag.vaultReserved ?? []).length, 1);
418 assert.equal(bag.vaultReserved[0].quantity, 2);
419 const vaultApplesAfterWrite = countItem(bag.vault, ItemId.apple);
420 const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque);
421 assert.ok(chequeIndex >= 0);
422
423 harness.simulation.enqueue(1, {
424 type: "drop-item",
425 characterId: character.character_id,
426 slotIndex: chequeIndex,
427 quantity: 1
428 });
429 const drop = harness.simulation.step();
430 assert.ok(drop.events.some((event) => event.type === "ground-item-appear"));
431 bag = harness.simulation.bagOf(entityId);
432 assert.equal((bag.vaultReserved ?? []).length, 1, "drop must keep goods reserved");
433 assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite);
434
435 harness.setNow(harness.now() + 5_000);
436 const expired = harness.simulation.step();
437 assert.ok(
438 expired.events.some((event) => event.type === "ground-item-despawn"),
439 JSON.stringify(expired.events.map((event) => event.type))
440 );
441 bag = harness.simulation.bagOf(entityId);
442 assert.equal((bag.vaultReserved ?? []).length, 0);
443 assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite + 2);
444 assert.ok(appleQtyBefore >= 2);
445 }
446 finally
447 { harness.close(); }
448});