| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { BlockType } from "../../shared/definitions/tiles.js"; |
| 4 | import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; |
| 5 | import { RangedItemId } from "../../shared/definitions/rangedItems.js"; |
| 6 | import { FlowerColorId, flowerItemInstance } from "../../shared/definitions/dyes.js"; |
| 7 | import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; |
| 8 | import { CHEQUE_LIFETIME_MS } from "../../shared/items/cheque.js"; |
| 9 | import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; |
| 10 | import { |
| 11 | makeCharacter, |
| 12 | makeSimulation |
| 13 | } from "./helpers/simulationHarness.js"; |
| 14 | |
| 15 | function placeBank (worldDb, tileX, tileY) |
| 16 | { |
| 17 | const chunkX = Math.floor(tileX / 16); |
| 18 | const chunkY = Math.floor(tileY / 16); |
| 19 | const data = createEmptyChunkData(); |
| 20 | data.open = true; |
| 21 | const localX = tileX - chunkX * 16; |
| 22 | const localY = tileY - chunkY * 16; |
| 23 | data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 }; |
| 24 | worldDb.saveChunkVersion( |
| 25 | chunkX, |
| 26 | chunkY, |
| 27 | worldDb.getActiveRevisionId(), |
| 28 | true, |
| 29 | encodeChunkBlob(data) |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | function countItem (slots, itemId) |
| 34 | { |
| 35 | return (slots ?? []) |
| 36 | .filter((slot) => slot?.itemId === itemId) |
| 37 | .reduce((sum, slot) => sum + (slot.quantity | 0), 0); |
| 38 | } |
| 39 | |
| 40 | test("blank cheques top up inventory to 99 at a bank", () => |
| 41 | { |
| 42 | const harness = makeSimulation({ workerCount: 1 }); |
| 43 | try |
| 44 | { |
| 45 | placeBank(harness.worldDb, 8, 8); |
| 46 | harness.simulation.reloadWorld(); |
| 47 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 48 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 49 | harness.simulation.enqueue(1, { |
| 50 | type: "get-blank-cheques", |
| 51 | characterId: character.character_id |
| 52 | }); |
| 53 | harness.simulation.step(); |
| 54 | const bag = harness.simulation.bagOf(entityId); |
| 55 | assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99); |
| 56 | } |
| 57 | finally |
| 58 | { harness.close(); } |
| 59 | }); |
| 60 | |
| 61 | test("bank deposit and withdraw move stacks with quantity", () => |
| 62 | { |
| 63 | const harness = makeSimulation({ workerCount: 1 }); |
| 64 | try |
| 65 | { |
| 66 | placeBank(harness.worldDb, 8, 8); |
| 67 | harness.simulation.reloadWorld(); |
| 68 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 69 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 70 | const before = harness.simulation.bagOf(entityId); |
| 71 | const appleIndex = before.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 72 | assert.ok(appleIndex >= 0); |
| 73 | const qty = before.inventory[appleIndex].quantity; |
| 74 | harness.simulation.enqueue(1, { |
| 75 | type: "bank-deposit", |
| 76 | characterId: character.character_id, |
| 77 | slotIndex: appleIndex, |
| 78 | quantity: 1 |
| 79 | }); |
| 80 | harness.simulation.step(); |
| 81 | let bag = harness.simulation.bagOf(entityId); |
| 82 | assert.equal(countItem(bag.vault, ItemId.apple), 1); |
| 83 | assert.equal(bag.inventory[appleIndex]?.quantity ?? 0, qty - 1); |
| 84 | const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 85 | harness.simulation.enqueue(1, { |
| 86 | type: "bank-withdraw", |
| 87 | characterId: character.character_id, |
| 88 | slotIndex: vaultIndex, |
| 89 | quantity: 1 |
| 90 | }); |
| 91 | harness.simulation.step(); |
| 92 | bag = harness.simulation.bagOf(entityId); |
| 93 | assert.equal(countItem(bag.vault, ItemId.apple), 0); |
| 94 | } |
| 95 | finally |
| 96 | { harness.close(); } |
| 97 | }); |
| 98 | |
| 99 | test("bank withdraw transfers what fits and still reports not enough space", () => |
| 100 | { |
| 101 | const harness = makeSimulation({ workerCount: 1 }); |
| 102 | try |
| 103 | { |
| 104 | placeBank(harness.worldDb, 8, 8); |
| 105 | harness.simulation.reloadWorld(); |
| 106 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 107 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 108 | const tipId = RangedItemId.bronzeArrowTip; |
| 109 | assert.equal(itemDefinition(tipId).maxStackSize, 99); |
| 110 | |
| 111 | const inventory = emptySlots(); |
| 112 | inventory[0] = { itemId: ItemId.apple, quantity: 1 }; |
| 113 | for (let i = 2; i < inventory.length; i++) |
| 114 | { inventory[i] = { itemId: ItemId.paper, quantity: 1 }; } |
| 115 | harness.simulation.applyBag(entityId, { |
| 116 | inventory, |
| 117 | equipment: emptyEquipment(), |
| 118 | vault: [{ itemId: tipId, quantity: 120 }], |
| 119 | vaultReserved: [] |
| 120 | }); |
| 121 | |
| 122 | harness.simulation.enqueue(1, { |
| 123 | type: "bank-withdraw", |
| 124 | characterId: character.character_id, |
| 125 | slotIndex: 0, |
| 126 | quantity: 120 |
| 127 | }); |
| 128 | const events = harness.simulation.step().events; |
| 129 | const bag = harness.simulation.bagOf(entityId); |
| 130 | assert.equal(countItem(bag.inventory, tipId), 99); |
| 131 | assert.equal(countItem(bag.vault, tipId), 21); |
| 132 | assert.ok(events.some((event) => |
| 133 | event.type === "command-error" && event.message === "Not enough space.")); |
| 134 | } |
| 135 | finally |
| 136 | { harness.close(); } |
| 137 | }); |
| 138 | |
| 139 | test("write cheque reserves vault goods and redeem returns them", () => |
| 140 | { |
| 141 | const harness = makeSimulation({ workerCount: 1 }); |
| 142 | try |
| 143 | { |
| 144 | placeBank(harness.worldDb, 8, 8); |
| 145 | harness.simulation.reloadWorld(); |
| 146 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 147 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 148 | harness.simulation.enqueue(1, { |
| 149 | type: "get-blank-cheques", |
| 150 | characterId: character.character_id |
| 151 | }); |
| 152 | harness.simulation.step(); |
| 153 | let bag = harness.simulation.bagOf(entityId); |
| 154 | const appleIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 155 | harness.simulation.enqueue(1, { |
| 156 | type: "bank-deposit", |
| 157 | characterId: character.character_id, |
| 158 | slotIndex: appleIndex, |
| 159 | quantity: null |
| 160 | }); |
| 161 | harness.simulation.step(); |
| 162 | bag = harness.simulation.bagOf(entityId); |
| 163 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 164 | const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 165 | harness.simulation.enqueue(1, { |
| 166 | type: "write-cheque", |
| 167 | characterId: character.character_id, |
| 168 | slotIndex: blankIndex, |
| 169 | vaultSlotIndex: vaultIndex, |
| 170 | quantity: 2 |
| 171 | }); |
| 172 | harness.simulation.step(); |
| 173 | bag = harness.simulation.bagOf(entityId); |
| 174 | assert.equal((bag.vaultReserved ?? []).length, 1); |
| 175 | assert.equal(bag.vaultReserved[0].quantity, 2); |
| 176 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 177 | assert.ok(chequeIndex >= 0); |
| 178 | harness.simulation.enqueue(1, { |
| 179 | type: "redeem-cheque", |
| 180 | characterId: character.character_id, |
| 181 | slotIndex: chequeIndex |
| 182 | }); |
| 183 | harness.simulation.step(); |
| 184 | bag = harness.simulation.bagOf(entityId); |
| 185 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 186 | assert.ok(countItem(bag.vault, ItemId.apple) >= 2); |
| 187 | } |
| 188 | finally |
| 189 | { harness.close(); } |
| 190 | }); |
| 191 | |
| 192 | test("redeem still works after bag is saved and reapplied", () => |
| 193 | { |
| 194 | const harness = makeSimulation({ workerCount: 1 }); |
| 195 | try |
| 196 | { |
| 197 | placeBank(harness.worldDb, 8, 8); |
| 198 | harness.simulation.reloadWorld(); |
| 199 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 200 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 201 | harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); |
| 202 | harness.simulation.step(); |
| 203 | let bag = harness.simulation.bagOf(entityId); |
| 204 | harness.simulation.enqueue(1, { |
| 205 | type: "bank-deposit", |
| 206 | characterId: character.character_id, |
| 207 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 208 | quantity: null |
| 209 | }); |
| 210 | harness.simulation.step(); |
| 211 | bag = harness.simulation.bagOf(entityId); |
| 212 | harness.simulation.enqueue(1, { |
| 213 | type: "write-cheque", |
| 214 | characterId: character.character_id, |
| 215 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), |
| 216 | vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 217 | quantity: 2 |
| 218 | }); |
| 219 | harness.simulation.step(); |
| 220 | bag = harness.simulation.bagOf(entityId); |
| 221 | assert.ok(bag.vaultReserved?.[0]?.chequeId, "reserved stack must keep chequeId through bagView/save"); |
| 222 | const saved = harness.stateDb.getBag(character.character_id); |
| 223 | assert.ok(saved.vaultReserved?.[0]?.chequeId); |
| 224 | harness.simulation.applyBag(entityId, saved); |
| 225 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 226 | harness.simulation.enqueue(1, { |
| 227 | type: "redeem-cheque", |
| 228 | characterId: character.character_id, |
| 229 | slotIndex: chequeIndex |
| 230 | }); |
| 231 | const result = harness.simulation.step(); |
| 232 | assert.equal(result.events.some((event) => event.type === "command-error"), false); |
| 233 | bag = harness.simulation.bagOf(entityId); |
| 234 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 235 | assert.ok(countItem(bag.vault, ItemId.apple) >= 2); |
| 236 | } |
| 237 | finally |
| 238 | { harness.close(); } |
| 239 | }); |
| 240 | |
| 241 | test("bank deposit of a filled cheque redeems reserved goods", () => |
| 242 | { |
| 243 | const harness = makeSimulation({ workerCount: 1 }); |
| 244 | try |
| 245 | { |
| 246 | placeBank(harness.worldDb, 8, 8); |
| 247 | harness.simulation.reloadWorld(); |
| 248 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 249 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 250 | harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); |
| 251 | harness.simulation.step(); |
| 252 | let bag = harness.simulation.bagOf(entityId); |
| 253 | const appleIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 254 | harness.simulation.enqueue(1, { |
| 255 | type: "bank-deposit", |
| 256 | characterId: character.character_id, |
| 257 | slotIndex: appleIndex, |
| 258 | quantity: null |
| 259 | }); |
| 260 | harness.simulation.step(); |
| 261 | bag = harness.simulation.bagOf(entityId); |
| 262 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 263 | const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple); |
| 264 | harness.simulation.enqueue(1, { |
| 265 | type: "write-cheque", |
| 266 | characterId: character.character_id, |
| 267 | slotIndex: blankIndex, |
| 268 | vaultSlotIndex: vaultIndex, |
| 269 | quantity: 2 |
| 270 | }); |
| 271 | harness.simulation.step(); |
| 272 | bag = harness.simulation.bagOf(entityId); |
| 273 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 274 | assert.ok(chequeIndex >= 0); |
| 275 | harness.simulation.enqueue(1, { |
| 276 | type: "bank-deposit", |
| 277 | characterId: character.character_id, |
| 278 | slotIndex: chequeIndex, |
| 279 | quantity: null |
| 280 | }); |
| 281 | harness.simulation.step(); |
| 282 | bag = harness.simulation.bagOf(entityId); |
| 283 | assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1); |
| 284 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 285 | assert.ok(countItem(bag.vault, ItemId.apple) >= 2); |
| 286 | } |
| 287 | finally |
| 288 | { harness.close(); } |
| 289 | }); |
| 290 | |
| 291 | test("new characters start with coin stacks in inventory", () => |
| 292 | { |
| 293 | const harness = makeSimulation({ workerCount: 1 }); |
| 294 | try |
| 295 | { |
| 296 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 297 | const bag = harness.stateDb.getBag(character.character_id); |
| 298 | assert.equal(countItem(bag.inventory, ItemId.coins), 25); |
| 299 | } |
| 300 | finally |
| 301 | { harness.close(); } |
| 302 | }); |
| 303 | |
| 304 | test("legacy currency counters migrate into coin stacks", () => |
| 305 | { |
| 306 | const harness = makeSimulation({ workerCount: 1 }); |
| 307 | try |
| 308 | { |
| 309 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 310 | harness.stateDb.saveBag(character.character_id, { |
| 311 | inventory: [], |
| 312 | equipment: emptyEquipment(), |
| 313 | currency: 100, |
| 314 | vault: [], |
| 315 | vaultReserved: [], |
| 316 | vaultCurrency: 50 |
| 317 | }); |
| 318 | const bag = harness.stateDb.getBag(character.character_id); |
| 319 | assert.equal(countItem(bag.inventory, ItemId.coins), 100); |
| 320 | assert.equal(countItem(bag.vault, ItemId.coins), 50); |
| 321 | } |
| 322 | finally |
| 323 | { harness.close(); } |
| 324 | }); |
| 325 | |
| 326 | test("stored inventory with excess blank cheques is capped on load", () => |
| 327 | { |
| 328 | const harness = makeSimulation({ workerCount: 1 }); |
| 329 | try |
| 330 | { |
| 331 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 332 | harness.stateDb.saveBag(character.character_id, { |
| 333 | inventory: [{ itemId: ItemId.blankCheque, quantity: 120 }], |
| 334 | equipment: emptyEquipment(), |
| 335 | vault: [], |
| 336 | vaultReserved: [] |
| 337 | }); |
| 338 | const bag = harness.stateDb.getBag(character.character_id); |
| 339 | assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99); |
| 340 | } |
| 341 | finally |
| 342 | { harness.close(); } |
| 343 | }); |
| 344 | test("blank cheque ground pickup stops at 99 carried", () => |
| 345 | { |
| 346 | const harness = makeSimulation({ workerCount: 1 }); |
| 347 | try |
| 348 | { |
| 349 | placeBank(harness.worldDb, 8, 8); |
| 350 | harness.simulation.reloadWorld(); |
| 351 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 352 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 353 | harness.simulation.enqueue(1, { |
| 354 | type: "get-blank-cheques", |
| 355 | characterId: character.character_id |
| 356 | }); |
| 357 | harness.simulation.step(); |
| 358 | let bag = harness.simulation.bagOf(entityId); |
| 359 | const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); |
| 360 | assert.ok(blankIndex >= 0); |
| 361 | harness.simulation.enqueue(1, { |
| 362 | type: "drop-item", |
| 363 | characterId: character.character_id, |
| 364 | slotIndex: blankIndex, |
| 365 | quantity: 10 |
| 366 | }); |
| 367 | const dropEvents = harness.simulation.step().events; |
| 368 | const appear = dropEvents.find((event) => event.type === "ground-item-appear"); |
| 369 | assert.ok(appear); |
| 370 | harness.simulation.enqueue(1, { |
| 371 | type: "pickup-item", |
| 372 | characterId: character.character_id, |
| 373 | groundItemId: appear.groundItemId |
| 374 | }); |
| 375 | harness.simulation.step(); |
| 376 | bag = harness.simulation.bagOf(entityId); |
| 377 | assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99); |
| 378 | const snapshot = harness.simulation.snapshotChunk(0, 0); |
| 379 | assert.equal((snapshot.groundItems ?? []).length, 0); |
| 380 | } |
| 381 | finally |
| 382 | { harness.close(); } |
| 383 | }); |
| 384 | |
| 385 | test("dropping a filled cheque keeps reservation; despawn returns goods to vault", () => |
| 386 | { |
| 387 | const harness = makeSimulation({ workerCount: 1, now: 10_000 }); |
| 388 | try |
| 389 | { |
| 390 | placeBank(harness.worldDb, 8, 8); |
| 391 | harness.simulation.reloadWorld(); |
| 392 | harness.simulation.applySettings({ groundItemDespawnDurationMs: 1_000 }); |
| 393 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 394 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 395 | harness.simulation.enqueue(1, { |
| 396 | type: "get-blank-cheques", |
| 397 | characterId: character.character_id |
| 398 | }); |
| 399 | harness.simulation.step(); |
| 400 | let bag = harness.simulation.bagOf(entityId); |
| 401 | const appleQtyBefore = countItem(bag.inventory, ItemId.apple); |
| 402 | harness.simulation.enqueue(1, { |
| 403 | type: "bank-deposit", |
| 404 | characterId: character.character_id, |
| 405 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 406 | quantity: null |
| 407 | }); |
| 408 | harness.simulation.step(); |
| 409 | bag = harness.simulation.bagOf(entityId); |
| 410 | harness.simulation.enqueue(1, { |
| 411 | type: "write-cheque", |
| 412 | characterId: character.character_id, |
| 413 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), |
| 414 | vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 415 | quantity: 2 |
| 416 | }); |
| 417 | harness.simulation.step(); |
| 418 | bag = harness.simulation.bagOf(entityId); |
| 419 | assert.equal((bag.vaultReserved ?? []).length, 1); |
| 420 | assert.equal(bag.vaultReserved[0].quantity, 2); |
| 421 | const vaultApplesAfterWrite = countItem(bag.vault, ItemId.apple); |
| 422 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 423 | assert.ok(chequeIndex >= 0); |
| 424 | |
| 425 | harness.simulation.enqueue(1, { |
| 426 | type: "drop-item", |
| 427 | characterId: character.character_id, |
| 428 | slotIndex: chequeIndex, |
| 429 | quantity: 1 |
| 430 | }); |
| 431 | const drop = harness.simulation.step(); |
| 432 | assert.ok(drop.events.some((event) => event.type === "ground-item-appear")); |
| 433 | bag = harness.simulation.bagOf(entityId); |
| 434 | assert.equal((bag.vaultReserved ?? []).length, 1, "drop must keep goods reserved"); |
| 435 | assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite); |
| 436 | |
| 437 | harness.setNow(harness.now() + 5_000); |
| 438 | const expired = harness.simulation.step(); |
| 439 | assert.ok( |
| 440 | expired.events.some((event) => event.type === "ground-item-despawn"), |
| 441 | JSON.stringify(expired.events.map((event) => event.type)) |
| 442 | ); |
| 443 | bag = harness.simulation.bagOf(entityId); |
| 444 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 445 | assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite + 2); |
| 446 | assert.ok(appleQtyBefore >= 2); |
| 447 | } |
| 448 | finally |
| 449 | { harness.close(); } |
| 450 | }); |
| 451 | |
| 452 | test("write cheque reserves only the selected flower colour stack", () => |
| 453 | { |
| 454 | const harness = makeSimulation({ workerCount: 1 }); |
| 455 | try |
| 456 | { |
| 457 | placeBank(harness.worldDb, 8, 8); |
| 458 | harness.simulation.reloadWorld(); |
| 459 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 460 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 461 | harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); |
| 462 | harness.simulation.step(); |
| 463 | |
| 464 | const bagView = harness.simulation.bagOf(entityId); |
| 465 | bagView.inventory = emptySlots(); |
| 466 | bagView.inventory[0] = { itemId: ItemId.blankCheque, quantity: 5 }; |
| 467 | bagView.equipment = emptyEquipment(); |
| 468 | bagView.vault = [ |
| 469 | { itemId: ItemId.flower, quantity: 5, instance: flowerItemInstance(FlowerColorId.red) }, |
| 470 | { itemId: ItemId.flower, quantity: 2, instance: flowerItemInstance(FlowerColorId.blue) } |
| 471 | ]; |
| 472 | bagView.vaultReserved = []; |
| 473 | harness.simulation.applyBag(entityId, bagView); |
| 474 | |
| 475 | const blueIndex = 1; |
| 476 | harness.simulation.enqueue(1, { |
| 477 | type: "write-cheque", |
| 478 | characterId: character.character_id, |
| 479 | slotIndex: 0, |
| 480 | vaultSlotIndex: blueIndex, |
| 481 | quantity: 2 |
| 482 | }); |
| 483 | const result = harness.simulation.step(); |
| 484 | assert.equal(result.events.some((event) => event.type === "command-error"), false); |
| 485 | |
| 486 | const bag = harness.simulation.bagOf(entityId); |
| 487 | assert.equal((bag.vaultReserved ?? []).length, 1); |
| 488 | assert.equal(bag.vaultReserved[0].quantity, 2); |
| 489 | assert.equal(bag.vaultReserved[0].instance?.colorId, FlowerColorId.blue); |
| 490 | assert.equal(bag.vault.find((slot) => slot?.instance?.colorId === FlowerColorId.red)?.quantity, 5); |
| 491 | assert.equal(bag.vault.find((slot) => slot?.instance?.colorId === FlowerColorId.blue), undefined); |
| 492 | |
| 493 | const cheque = bag.inventory.find((slot) => slot?.itemId === ItemId.filledCheque); |
| 494 | assert.equal(cheque?.instance?.reservedInstance?.colorId, FlowerColorId.blue); |
| 495 | assert.equal(cheque?.instance?.quantity, 2); |
| 496 | } |
| 497 | finally |
| 498 | { harness.close(); } |
| 499 | }); |
| 500 | |
| 501 | test("write cheque rejects quantity beyond matching metadata stacks", () => |
| 502 | { |
| 503 | const harness = makeSimulation({ workerCount: 1 }); |
| 504 | try |
| 505 | { |
| 506 | placeBank(harness.worldDb, 8, 8); |
| 507 | harness.simulation.reloadWorld(); |
| 508 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 509 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 510 | harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); |
| 511 | harness.simulation.step(); |
| 512 | |
| 513 | const bagView = harness.simulation.bagOf(entityId); |
| 514 | bagView.inventory = emptySlots(); |
| 515 | bagView.inventory[0] = { itemId: ItemId.blankCheque, quantity: 5 }; |
| 516 | bagView.equipment = emptyEquipment(); |
| 517 | bagView.vault = [ |
| 518 | { itemId: ItemId.flower, quantity: 5, instance: flowerItemInstance(FlowerColorId.red) }, |
| 519 | { itemId: ItemId.flower, quantity: 2, instance: flowerItemInstance(FlowerColorId.blue) } |
| 520 | ]; |
| 521 | bagView.vaultReserved = []; |
| 522 | harness.simulation.applyBag(entityId, bagView); |
| 523 | |
| 524 | harness.simulation.enqueue(1, { |
| 525 | type: "write-cheque", |
| 526 | characterId: character.character_id, |
| 527 | slotIndex: 0, |
| 528 | vaultSlotIndex: 1, |
| 529 | quantity: 3 |
| 530 | }); |
| 531 | const result = harness.simulation.step(); |
| 532 | const error = result.events.find((event) => event.type === "command-error"); |
| 533 | assert.ok(error); |
| 534 | assert.match(error.message, /Not enough/i); |
| 535 | const bag = harness.simulation.bagOf(entityId); |
| 536 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 537 | assert.equal(bag.vault.find((slot) => slot?.instance?.colorId === FlowerColorId.blue)?.quantity, 2); |
| 538 | } |
| 539 | finally |
| 540 | { harness.close(); } |
| 541 | }); |
| 542 | |
| 543 | test("filled cheque expires after 30 days into expired cheque and releases goods", () => |
| 544 | { |
| 545 | const harness = makeSimulation({ workerCount: 1 }); |
| 546 | try |
| 547 | { |
| 548 | placeBank(harness.worldDb, 8, 8); |
| 549 | harness.simulation.reloadWorld(); |
| 550 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); |
| 551 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 552 | harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); |
| 553 | harness.simulation.step(); |
| 554 | let bag = harness.simulation.bagOf(entityId); |
| 555 | const appleQty = countItem(bag.inventory, ItemId.apple); |
| 556 | harness.simulation.enqueue(1, { |
| 557 | type: "bank-deposit", |
| 558 | characterId: character.character_id, |
| 559 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 560 | quantity: null |
| 561 | }); |
| 562 | harness.simulation.step(); |
| 563 | bag = harness.simulation.bagOf(entityId); |
| 564 | harness.simulation.enqueue(1, { |
| 565 | type: "write-cheque", |
| 566 | characterId: character.character_id, |
| 567 | slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), |
| 568 | vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), |
| 569 | quantity: 2 |
| 570 | }); |
| 571 | harness.simulation.step(); |
| 572 | bag = harness.simulation.bagOf(entityId); |
| 573 | assert.equal((bag.vaultReserved ?? []).length, 1); |
| 574 | const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); |
| 575 | assert.ok(chequeIndex >= 0); |
| 576 | |
| 577 | bag.inventory[chequeIndex].instance.createdAt = harness.now() - CHEQUE_LIFETIME_MS - 1000; |
| 578 | harness.simulation.applyBag(entityId, bag); |
| 579 | |
| 580 | harness.simulation.sweepExpiredCheques(character.character_id, { now: harness.now() }); |
| 581 | bag = harness.simulation.bagOf(entityId); |
| 582 | assert.equal(bag.inventory[chequeIndex]?.itemId, ItemId.expiredCheque); |
| 583 | assert.equal((bag.vaultReserved ?? []).length, 0); |
| 584 | assert.equal(countItem(bag.vault, ItemId.apple), appleQty); |
| 585 | } |
| 586 | finally |
| 587 | { harness.close(); } |
| 588 | }); |