| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId } from "../../shared/definitions/items.js"; |
| 5 | import { emptyEquipment } from "../../shared/items/inventory.js"; |
| 6 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 7 | import { |
| 8 | makeCharacter, |
| 9 | makeSimulation |
| 10 | } from "./helpers/simulationHarness.js"; |
| 11 | |
| 12 | function 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 | |
| 30 | function countItem (slots, itemId) |
| 31 | { |
| 32 | return (slots ?? []) |
| 33 | .filter((slot) => slot?.itemId === itemId) |
| 34 | .reduce((sum, slot) => sum + (slot.quantity | 0), 0); |
| 35 | } |
| 36 | |
| 37 | test("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 | |
| 58 | test("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 | |
| 96 | test("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 | |
| 149 | test("redeem still works after bag is saved and reapplied", () => |
| 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 | harness.simulation.enqueue(1, { |
| 162 | type: "bank-deposit", |
| 163 | characterId: character.character_id, |
| 164 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 165 | quantity: null |
| 166 | }); |
| 167 | harness.simulation.step(); |
| 168 | bag = harness.simulation.bagOf(entityId); |
| 169 | harness.simulation.enqueue(1, { |
| 170 | type: "write-cheque", |
| 171 | characterId: character.character_id, |
| 172 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), |
| 173 | vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 174 | quantity: 2 |
| 175 | }); |
| 176 | harness.simulation.step(); |
| 177 | bag = harness.simulation.bagOf(entityId); |
| 178 | assert.ok(bag.vaultReserved?.[0]?.chequeId, "reserved stack must keep chequeId through bagView/save"); |
| 179 | const saved = harness.stateDb.getBag(character.character_id); |
| 180 | assert.ok(saved.vaultReserved?.[0]?.chequeId); |
| 181 | harness.simulation.applyBag(entityId, saved); |
| 182 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 183 | harness.simulation.enqueue(1, { |
| 184 | type: "redeem-cheque", |
| 185 | characterId: character.character_id, |
| 186 | slotIndex: chequeIndex |
| 187 | }); |
| 188 | const result = harness.simulation.step(); |
| 189 | assert.equal(result.events.some((event) => event.type === "command-error"), false); |
| 190 | bag = harness.simulation.bagOf(entityId); |
| 191 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 192 | assert.ok(countItem(bag.vault, ItemId.apple) >= 2); |
| 193 | } |
| 194 | finally |
| 195 | { harness.close(); } |
| 196 | }); |
| 197 | |
| 198 | test("bank deposit of a filled cheque redeems reserved goods", () => |
| 199 | { |
| 200 | const harness = makeSimulation({ workerCount: 1 }); |
| 201 | try |
| 202 | { |
| 203 | placeBank(harness.worldDb, 8, 8); |
| 204 | harness.simulation.reloadWorld(); |
| 205 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 206 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 207 | harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); |
| 208 | harness.simulation.step(); |
| 209 | let bag = harness.simulation.bagOf(entityId); |
| 210 | const appleIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 211 | harness.simulation.enqueue(1, { |
| 212 | type: "bank-deposit", |
| 213 | characterId: character.character_id, |
| 214 | slotIndex: appleIndex, |
| 215 | quantity: null |
| 216 | }); |
| 217 | harness.simulation.step(); |
| 218 | bag = harness.simulation.bagOf(entityId); |
| 219 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 220 | const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 221 | harness.simulation.enqueue(1, { |
| 222 | type: "write-cheque", |
| 223 | characterId: character.character_id, |
| 224 | slotIndex: blankIndex, |
| 225 | vaultSlotIndex: vaultIndex, |
| 226 | quantity: 2 |
| 227 | }); |
| 228 | harness.simulation.step(); |
| 229 | bag = harness.simulation.bagOf(entityId); |
| 230 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 231 | assert.ok(chequeIndex >= 0); |
| 232 | harness.simulation.enqueue(1, { |
| 233 | type: "bank-deposit", |
| 234 | characterId: character.character_id, |
| 235 | slotIndex: chequeIndex, |
| 236 | quantity: null |
| 237 | }); |
| 238 | harness.simulation.step(); |
| 239 | bag = harness.simulation.bagOf(entityId); |
| 240 | assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1); |
| 241 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 242 | assert.ok(countItem(bag.vault, ItemId.apple) >= 2); |
| 243 | } |
| 244 | finally |
| 245 | { harness.close(); } |
| 246 | }); |
| 247 | |
| 248 | test("new characters start with coin stacks in inventory", () => |
| 249 | { |
| 250 | const harness = makeSimulation({ workerCount: 1 }); |
| 251 | try |
| 252 | { |
| 253 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 254 | const bag = harness.stateDb.getBag(character.character_id); |
| 255 | assert.equal(countItem(bag.inventory, ItemId.coins), 25); |
| 256 | } |
| 257 | finally |
| 258 | { harness.close(); } |
| 259 | }); |
| 260 | |
| 261 | test("legacy currency counters migrate into coin stacks", () => |
| 262 | { |
| 263 | const harness = makeSimulation({ workerCount: 1 }); |
| 264 | try |
| 265 | { |
| 266 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 267 | harness.stateDb.saveBag(character.character_id, { |
| 268 | inventory: [], |
| 269 | equipment: emptyEquipment(), |
| 270 | currency: 100, |
| 271 | vault: [], |
| 272 | vaultReserved: [], |
| 273 | vaultCurrency: 50 |
| 274 | }); |
| 275 | const bag = harness.stateDb.getBag(character.character_id); |
| 276 | assert.equal(countItem(bag.inventory, ItemId.coins), 100); |
| 277 | assert.equal(countItem(bag.vault, ItemId.coins), 50); |
| 278 | } |
| 279 | finally |
| 280 | { harness.close(); } |
| 281 | }); |
| 282 | |
| 283 | test("stored inventory with excess blank cheques is capped on load", () => |
| 284 | { |
| 285 | const harness = makeSimulation({ workerCount: 1 }); |
| 286 | try |
| 287 | { |
| 288 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 289 | harness.stateDb.saveBag(character.character_id, { |
| 290 | inventory: [{ itemId: ItemId.blankCheque, quantity: 120 }], |
| 291 | equipment: emptyEquipment(), |
| 292 | vault: [], |
| 293 | vaultReserved: [] |
| 294 | }); |
| 295 | const bag = harness.stateDb.getBag(character.character_id); |
| 296 | assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99); |
| 297 | } |
| 298 | finally |
| 299 | { harness.close(); } |
| 300 | }); |
| 301 | test("blank cheque ground pickup stops at 99 carried", () => |
| 302 | { |
| 303 | const harness = makeSimulation({ workerCount: 1 }); |
| 304 | try |
| 305 | { |
| 306 | placeBank(harness.worldDb, 8, 8); |
| 307 | harness.simulation.reloadWorld(); |
| 308 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 309 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 310 | harness.simulation.enqueue(1, { |
| 311 | type: "get-blank-cheques", |
| 312 | characterId: character.character_id |
| 313 | }); |
| 314 | harness.simulation.step(); |
| 315 | let bag = harness.simulation.bagOf(entityId); |
| 316 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 317 | assert.ok(blankIndex >= 0); |
| 318 | harness.simulation.enqueue(1, { |
| 319 | type: "drop-item", |
| 320 | characterId: character.character_id, |
| 321 | slotIndex: blankIndex, |
| 322 | quantity: 10 |
| 323 | }); |
| 324 | const dropEvents = harness.simulation.step().events; |
| 325 | const appear = dropEvents.find((event) => event.type === "ground-item-appear"); |
| 326 | assert.ok(appear); |
| 327 | harness.simulation.enqueue(1, { |
| 328 | type: "pickup-item", |
| 329 | characterId: character.character_id, |
| 330 | groundItemId: appear.groundItemId |
| 331 | }); |
| 332 | harness.simulation.step(); |
| 333 | bag = harness.simulation.bagOf(entityId); |
| 334 | assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99); |
| 335 | const snapshot = harness.simulation.snapshotChunk(0, 0); |
| 336 | assert.equal((snapshot.groundItems ?? []).length, 0); |
| 337 | } |
| 338 | finally |
| 339 | { harness.close(); } |
| 340 | }); |