BlueGit

timfa questward

Files

questward / server / tests / bank.test.js

bank.test.js

bank.test.js 9.4 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { BlockType } from "../../shared/definitions/tiles.js";
4import { ItemId } from "../../shared/definitions/items.js";
5import { emptyEquipment } from "../../shared/items/inventory.js";
6import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js";
7import {
8 makeCharacter,
9 makeSimulation
10} from "./helpers/simulationHarness.js";
11
12function placeBank (worldDb, tileX, tileY)
13{
14 const chunkX = Math.floor(tileX / 16);
15 const chunkY = Math.floor(tileY / 16);
16 const data = createEmptyChunkData();
17 data.open = true;
18 const localX = tileX - chunkX * 16;
19 const localY = tileY - chunkY * 16;
20 data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 };
21 worldDb.saveChunkVersion(
22 chunkX,
23 chunkY,
24 worldDb.getActiveRevisionId(),
25 true,
26 encodeChunkBlob(data)
27 );
28}
29
30function countItem (slots, itemId)
31{
32 return (slots ?? [])
33 .filter((slot) => slot?.itemId === itemId)
34 .reduce((sum, slot) => sum + (slot.quantity | 0), 0);
35}
36
37test("blank cheques top up inventory to 99 at a bank", () =>
38{
39 const harness = makeSimulation({ workerCount: 1 });
40 try
41 {
42 placeBank(harness.worldDb, 8, 8);
43 harness.simulation.reloadWorld();
44 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
45 const entityId = harness.simulation.spawnCharacter(1, character);
46 harness.simulation.enqueue(1, {
47 type: "get-blank-cheques",
48 characterId: character.character_id
49 });
50 harness.simulation.step();
51 const bag = harness.simulation.bagOf(entityId);
52 assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99);
53 }
54 finally
55 { harness.close(); }
56});
57
58test("bank deposit and withdraw move stacks with quantity", () =>
59{
60 const harness = makeSimulation({ workerCount: 1 });
61 try
62 {
63 placeBank(harness.worldDb, 8, 8);
64 harness.simulation.reloadWorld();
65 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
66 const entityId = harness.simulation.spawnCharacter(1, character);
67 const before = harness.simulation.bagOf(entityId);
68 const appleIndex = before.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
69 assert.ok(appleIndex >= 0);
70 const qty = before.inventory[appleIndex].quantity;
71 harness.simulation.enqueue(1, {
72 type: "bank-deposit",
73 characterId: character.character_id,
74 slotIndex: appleIndex,
75 quantity: 1
76 });
77 harness.simulation.step();
78 let bag = harness.simulation.bagOf(entityId);
79 assert.equal(countItem(bag.vault, ItemId.apple), 1);
80 assert.equal(bag.inventory[appleIndex]?.quantity ?? 0, qty - 1);
81 const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple);
82 harness.simulation.enqueue(1, {
83 type: "bank-withdraw",
84 characterId: character.character_id,
85 slotIndex: vaultIndex,
86 quantity: 1
87 });
88 harness.simulation.step();
89 bag = harness.simulation.bagOf(entityId);
90 assert.equal(countItem(bag.vault, ItemId.apple), 0);
91 }
92 finally
93 { harness.close(); }
94});
95
96test("write cheque reserves vault goods and redeem returns them", () =>
97{
98 const harness = makeSimulation({ workerCount: 1 });
99 try
100 {
101 placeBank(harness.worldDb, 8, 8);
102 harness.simulation.reloadWorld();
103 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
104 const entityId = harness.simulation.spawnCharacter(1, character);
105 harness.simulation.enqueue(1, {
106 type: "get-blank-cheques",
107 characterId: character.character_id
108 });
109 harness.simulation.step();
110 let bag = harness.simulation.bagOf(entityId);
111 const appleIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
112 harness.simulation.enqueue(1, {
113 type: "bank-deposit",
114 characterId: character.character_id,
115 slotIndex: appleIndex,
116 quantity: null
117 });
118 harness.simulation.step();
119 bag = harness.simulation.bagOf(entityId);
120 const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque);
121 const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple);
122 harness.simulation.enqueue(1, {
123 type: "write-cheque",
124 characterId: character.character_id,
125 slotIndex: blankIndex,
126 vaultSlotIndex: vaultIndex,
127 quantity: 2
128 });
129 harness.simulation.step();
130 bag = harness.simulation.bagOf(entityId);
131 assert.equal((bag.vaultReserved ?? []).length, 1);
132 assert.equal(bag.vaultReserved[0].quantity, 2);
133 const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque);
134 assert.ok(chequeIndex >= 0);
135 harness.simulation.enqueue(1, {
136 type: "redeem-cheque",
137 characterId: character.character_id,
138 slotIndex: chequeIndex
139 });
140 harness.simulation.step();
141 bag = harness.simulation.bagOf(entityId);
142 assert.equal((bag.vaultReserved ?? []).length, 0);
143 assert.ok(countItem(bag.vault, ItemId.apple) >= 2);
144 }
145 finally
146 { harness.close(); }
147});
148
149test("bank deposit of a filled cheque redeems reserved goods", () =>
150{
151 const harness = makeSimulation({ workerCount: 1 });
152 try
153 {
154 placeBank(harness.worldDb, 8, 8);
155 harness.simulation.reloadWorld();
156 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
157 const entityId = harness.simulation.spawnCharacter(1, character);
158 harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id });
159 harness.simulation.step();
160 let bag = harness.simulation.bagOf(entityId);
161 const appleIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple);
162 harness.simulation.enqueue(1, {
163 type: "bank-deposit",
164 characterId: character.character_id,
165 slotIndex: appleIndex,
166 quantity: null
167 });
168 harness.simulation.step();
169 bag = harness.simulation.bagOf(entityId);
170 const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque);
171 const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple);
172 harness.simulation.enqueue(1, {
173 type: "write-cheque",
174 characterId: character.character_id,
175 slotIndex: blankIndex,
176 vaultSlotIndex: vaultIndex,
177 quantity: 2
178 });
179 harness.simulation.step();
180 bag = harness.simulation.bagOf(entityId);
181 const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque);
182 assert.ok(chequeIndex >= 0);
183 harness.simulation.enqueue(1, {
184 type: "bank-deposit",
185 characterId: character.character_id,
186 slotIndex: chequeIndex,
187 quantity: null
188 });
189 harness.simulation.step();
190 bag = harness.simulation.bagOf(entityId);
191 assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1);
192 assert.equal((bag.vaultReserved ?? []).length, 0);
193 assert.ok(countItem(bag.vault, ItemId.apple) >= 2);
194 }
195 finally
196 { harness.close(); }
197});
198
199test("new characters start with coin stacks in inventory", () =>
200{
201 const harness = makeSimulation({ workerCount: 1 });
202 try
203 {
204 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
205 const bag = harness.stateDb.getBag(character.character_id);
206 assert.equal(countItem(bag.inventory, ItemId.coins), 25);
207 }
208 finally
209 { harness.close(); }
210});
211
212test("legacy currency counters migrate into coin stacks", () =>
213{
214 const harness = makeSimulation({ workerCount: 1 });
215 try
216 {
217 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
218 harness.stateDb.saveBag(character.character_id, {
219 inventory: [],
220 equipment: emptyEquipment(),
221 currency: 100,
222 vault: [],
223 vaultReserved: [],
224 vaultCurrency: 50
225 });
226 const bag = harness.stateDb.getBag(character.character_id);
227 assert.equal(countItem(bag.inventory, ItemId.coins), 100);
228 assert.equal(countItem(bag.vault, ItemId.coins), 50);
229 }
230 finally
231 { harness.close(); }
232});
233
234test("stored inventory with excess blank cheques is capped on load", () =>
235{
236 const harness = makeSimulation({ workerCount: 1 });
237 try
238 {
239 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
240 harness.stateDb.saveBag(character.character_id, {
241 inventory: [{ itemId: ItemId.blankCheque, quantity: 120 }],
242 equipment: emptyEquipment(),
243 vault: [],
244 vaultReserved: []
245 });
246 const bag = harness.stateDb.getBag(character.character_id);
247 assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99);
248 }
249 finally
250 { harness.close(); }
251});
252test("blank cheque ground pickup stops at 99 carried", () =>
253{
254 const harness = makeSimulation({ workerCount: 1 });
255 try
256 {
257 placeBank(harness.worldDb, 8, 8);
258 harness.simulation.reloadWorld();
259 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 });
260 const entityId = harness.simulation.spawnCharacter(1, character);
261 harness.simulation.enqueue(1, {
262 type: "get-blank-cheques",
263 characterId: character.character_id
264 });
265 harness.simulation.step();
266 let bag = harness.simulation.bagOf(entityId);
267 const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque);
268 assert.ok(blankIndex >= 0);
269 harness.simulation.enqueue(1, {
270 type: "drop-item",
271 characterId: character.character_id,
272 slotIndex: blankIndex,
273 quantity: 10
274 });
275 const dropEvents = harness.simulation.step().events;
276 const appear = dropEvents.find((event) => event.type === "ground-item-appear");
277 assert.ok(appear);
278 harness.simulation.enqueue(1, {
279 type: "pickup-item",
280 characterId: character.character_id,
281 groundItemId: appear.groundItemId
282 });
283 harness.simulation.step();
284 bag = harness.simulation.bagOf(entityId);
285 assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99);
286 const snapshot = harness.simulation.snapshotChunk(0, 0);
287 assert.equal((snapshot.groundItems ?? []).length, 0);
288 }
289 finally
290 { harness.close(); }
291});