import test from "node:test"; import assert from "node:assert/strict"; import { BlockType } from "../../shared/definitions/tiles.js"; import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; import { RangedItemId } from "../../shared/definitions/rangedItems.js"; import { FlowerColorId, flowerItemInstance } from "../../shared/definitions/dyes.js"; import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; import { CHEQUE_LIFETIME_MS } from "../../shared/items/cheque.js"; import { createEmptyChunkData, encodeChunkBlob } from "../src/world/chunkData.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; function placeBank (worldDb, tileX, tileY) { const chunkX = Math.floor(tileX / 16); const chunkY = Math.floor(tileY / 16); const data = createEmptyChunkData(); data.open = true; const localX = tileX - chunkX * 16; const localY = tileY - chunkY * 16; data.blocks[localY * 16 + localX] = { typeId: BlockType.bank, state: 0 }; worldDb.saveChunkVersion( chunkX, chunkY, worldDb.getActiveRevisionId(), true, encodeChunkBlob(data) ); } function countItem (slots, itemId) { return (slots ?? []) .filter((slot) => slot?.itemId === itemId) .reduce((sum, slot) => sum + (slot.quantity | 0), 0); } test("blank cheques top up inventory to 99 at a bank", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); harness.simulation.step(); const bag = harness.simulation.bagOf(entityId); assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99); } finally { harness.close(); } }); test("bank deposit and withdraw move stacks with quantity", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); const before = harness.simulation.bagOf(entityId); const appleIndex = before.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); assert.ok(appleIndex >= 0); const qty = before.inventory[appleIndex].quantity; harness.simulation.enqueue(1, { type: "bank-deposit", characterId: character.character_id, slotIndex: appleIndex, quantity: 1 }); harness.simulation.step(); let bag = harness.simulation.bagOf(entityId); assert.equal(countItem(bag.vault, ItemId.apple), 1); assert.equal(bag.inventory[appleIndex]?.quantity ?? 0, qty - 1); const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple); harness.simulation.enqueue(1, { type: "bank-withdraw", characterId: character.character_id, slotIndex: vaultIndex, quantity: 1 }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); assert.equal(countItem(bag.vault, ItemId.apple), 0); } finally { harness.close(); } }); test("bank withdraw transfers what fits and still reports not enough space", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); const tipId = RangedItemId.bronzeArrowTip; assert.equal(itemDefinition(tipId).maxStackSize, 99); const inventory = emptySlots(); inventory[0] = { itemId: ItemId.apple, quantity: 1 }; for (let i = 2; i < inventory.length; i++) { inventory[i] = { itemId: ItemId.paper, quantity: 1 }; } harness.simulation.applyBag(entityId, { inventory, equipment: emptyEquipment(), vault: [{ itemId: tipId, quantity: 120 }], vaultReserved: [] }); harness.simulation.enqueue(1, { type: "bank-withdraw", characterId: character.character_id, slotIndex: 0, quantity: 120 }); const events = harness.simulation.step().events; const bag = harness.simulation.bagOf(entityId); assert.equal(countItem(bag.inventory, tipId), 99); assert.equal(countItem(bag.vault, tipId), 21); assert.ok(events.some((event) => event.type === "command-error" && event.message === "Not enough space.")); } finally { harness.close(); } }); test("write cheque reserves vault goods and redeem returns them", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); harness.simulation.step(); let bag = harness.simulation.bagOf(entityId); const appleIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); harness.simulation.enqueue(1, { type: "bank-deposit", characterId: character.character_id, slotIndex: appleIndex, quantity: null }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple); harness.simulation.enqueue(1, { type: "write-cheque", characterId: character.character_id, slotIndex: blankIndex, vaultSlotIndex: vaultIndex, quantity: 2 }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); assert.equal((bag.vaultReserved ?? []).length, 1); assert.equal(bag.vaultReserved[0].quantity, 2); const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); assert.ok(chequeIndex >= 0); harness.simulation.enqueue(1, { type: "redeem-cheque", characterId: character.character_id, slotIndex: chequeIndex }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); assert.equal((bag.vaultReserved ?? []).length, 0); assert.ok(countItem(bag.vault, ItemId.apple) >= 2); } finally { harness.close(); } }); test("redeem still works after bag is saved and reapplied", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); harness.simulation.step(); let bag = harness.simulation.bagOf(entityId); harness.simulation.enqueue(1, { type: "bank-deposit", characterId: character.character_id, slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), quantity: null }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); harness.simulation.enqueue(1, { type: "write-cheque", characterId: character.character_id, slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), quantity: 2 }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); assert.ok(bag.vaultReserved?.[0]?.chequeId, "reserved stack must keep chequeId through bagView/save"); const saved = harness.stateDb.getBag(character.character_id); assert.ok(saved.vaultReserved?.[0]?.chequeId); harness.simulation.applyBag(entityId, saved); const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); harness.simulation.enqueue(1, { type: "redeem-cheque", characterId: character.character_id, slotIndex: chequeIndex }); const result = harness.simulation.step(); assert.equal(result.events.some((event) => event.type === "command-error"), false); bag = harness.simulation.bagOf(entityId); assert.equal((bag.vaultReserved ?? []).length, 0); assert.ok(countItem(bag.vault, ItemId.apple) >= 2); } finally { harness.close(); } }); test("bank deposit of a filled cheque redeems reserved goods", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); harness.simulation.step(); let bag = harness.simulation.bagOf(entityId); const appleIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple); harness.simulation.enqueue(1, { type: "bank-deposit", characterId: character.character_id, slotIndex: appleIndex, quantity: null }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); const vaultIndex = bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple); harness.simulation.enqueue(1, { type: "write-cheque", characterId: character.character_id, slotIndex: blankIndex, vaultSlotIndex: vaultIndex, quantity: 2 }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); assert.ok(chequeIndex >= 0); harness.simulation.enqueue(1, { type: "bank-deposit", characterId: character.character_id, slotIndex: chequeIndex, quantity: null }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); assert.equal(bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque), -1); assert.equal((bag.vaultReserved ?? []).length, 0); assert.ok(countItem(bag.vault, ItemId.apple) >= 2); } finally { harness.close(); } }); test("new characters start with coin stacks in inventory", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const bag = harness.stateDb.getBag(character.character_id); assert.equal(countItem(bag.inventory, ItemId.coins), 25); } finally { harness.close(); } }); test("legacy currency counters migrate into coin stacks", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.stateDb.saveBag(character.character_id, { inventory: [], equipment: emptyEquipment(), currency: 100, vault: [], vaultReserved: [], vaultCurrency: 50 }); const bag = harness.stateDb.getBag(character.character_id); assert.equal(countItem(bag.inventory, ItemId.coins), 100); assert.equal(countItem(bag.vault, ItemId.coins), 50); } finally { harness.close(); } }); test("stored inventory with excess blank cheques is capped on load", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.stateDb.saveBag(character.character_id, { inventory: [{ itemId: ItemId.blankCheque, quantity: 120 }], equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const bag = harness.stateDb.getBag(character.character_id); assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99); } finally { harness.close(); } }); test("blank cheque ground pickup stops at 99 carried", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); harness.simulation.step(); let bag = harness.simulation.bagOf(entityId); const blankIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque); assert.ok(blankIndex >= 0); harness.simulation.enqueue(1, { type: "drop-item", characterId: character.character_id, slotIndex: blankIndex, quantity: 10 }); const dropEvents = harness.simulation.step().events; const appear = dropEvents.find((event) => event.type === "ground-item-appear"); assert.ok(appear); harness.simulation.enqueue(1, { type: "pickup-item", characterId: character.character_id, groundItemId: appear.groundItemId }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); assert.equal(countItem(bag.inventory, ItemId.blankCheque), 99); const snapshot = harness.simulation.snapshotChunk(0, 0); assert.equal((snapshot.groundItems ?? []).length, 0); } finally { harness.close(); } }); test("dropping a filled cheque keeps reservation; despawn returns goods to vault", () => { const harness = makeSimulation({ workerCount: 1, now: 10_000 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); harness.simulation.applySettings({ groundItemDespawnDurationMs: 1_000 }); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); harness.simulation.step(); let bag = harness.simulation.bagOf(entityId); const appleQtyBefore = countItem(bag.inventory, ItemId.apple); harness.simulation.enqueue(1, { type: "bank-deposit", characterId: character.character_id, slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), quantity: null }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); harness.simulation.enqueue(1, { type: "write-cheque", characterId: character.character_id, slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), quantity: 2 }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); assert.equal((bag.vaultReserved ?? []).length, 1); assert.equal(bag.vaultReserved[0].quantity, 2); const vaultApplesAfterWrite = countItem(bag.vault, ItemId.apple); const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); assert.ok(chequeIndex >= 0); harness.simulation.enqueue(1, { type: "drop-item", characterId: character.character_id, slotIndex: chequeIndex, quantity: 1 }); const drop = harness.simulation.step(); assert.ok(drop.events.some((event) => event.type === "ground-item-appear")); bag = harness.simulation.bagOf(entityId); assert.equal((bag.vaultReserved ?? []).length, 1, "drop must keep goods reserved"); assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite); harness.setNow(harness.now() + 5_000); const expired = harness.simulation.step(); assert.ok( expired.events.some((event) => event.type === "ground-item-despawn"), JSON.stringify(expired.events.map((event) => event.type)) ); bag = harness.simulation.bagOf(entityId); assert.equal((bag.vaultReserved ?? []).length, 0); assert.equal(countItem(bag.vault, ItemId.apple), vaultApplesAfterWrite + 2); assert.ok(appleQtyBefore >= 2); } finally { harness.close(); } }); test("write cheque reserves only the selected flower colour stack", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); harness.simulation.step(); const bagView = harness.simulation.bagOf(entityId); bagView.inventory = emptySlots(); bagView.inventory[0] = { itemId: ItemId.blankCheque, quantity: 5 }; bagView.equipment = emptyEquipment(); bagView.vault = [ { itemId: ItemId.flower, quantity: 5, instance: flowerItemInstance(FlowerColorId.red) }, { itemId: ItemId.flower, quantity: 2, instance: flowerItemInstance(FlowerColorId.blue) } ]; bagView.vaultReserved = []; harness.simulation.applyBag(entityId, bagView); const blueIndex = 1; harness.simulation.enqueue(1, { type: "write-cheque", characterId: character.character_id, slotIndex: 0, vaultSlotIndex: blueIndex, quantity: 2 }); const result = harness.simulation.step(); assert.equal(result.events.some((event) => event.type === "command-error"), false); const bag = harness.simulation.bagOf(entityId); assert.equal((bag.vaultReserved ?? []).length, 1); assert.equal(bag.vaultReserved[0].quantity, 2); assert.equal(bag.vaultReserved[0].instance?.colorId, FlowerColorId.blue); assert.equal(bag.vault.find((slot) => slot?.instance?.colorId === FlowerColorId.red)?.quantity, 5); assert.equal(bag.vault.find((slot) => slot?.instance?.colorId === FlowerColorId.blue), undefined); const cheque = bag.inventory.find((slot) => slot?.itemId === ItemId.filledCheque); assert.equal(cheque?.instance?.reservedInstance?.colorId, FlowerColorId.blue); assert.equal(cheque?.instance?.quantity, 2); } finally { harness.close(); } }); test("write cheque rejects quantity beyond matching metadata stacks", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); harness.simulation.step(); const bagView = harness.simulation.bagOf(entityId); bagView.inventory = emptySlots(); bagView.inventory[0] = { itemId: ItemId.blankCheque, quantity: 5 }; bagView.equipment = emptyEquipment(); bagView.vault = [ { itemId: ItemId.flower, quantity: 5, instance: flowerItemInstance(FlowerColorId.red) }, { itemId: ItemId.flower, quantity: 2, instance: flowerItemInstance(FlowerColorId.blue) } ]; bagView.vaultReserved = []; harness.simulation.applyBag(entityId, bagView); harness.simulation.enqueue(1, { type: "write-cheque", characterId: character.character_id, slotIndex: 0, vaultSlotIndex: 1, quantity: 3 }); const result = harness.simulation.step(); const error = result.events.find((event) => event.type === "command-error"); assert.ok(error); assert.match(error.message, /Not enough/i); const bag = harness.simulation.bagOf(entityId); assert.equal((bag.vaultReserved ?? []).length, 0); assert.equal(bag.vault.find((slot) => slot?.instance?.colorId === FlowerColorId.blue)?.quantity, 2); } finally { harness.close(); } }); test("filled cheque expires after 30 days into expired cheque and releases goods", () => { const harness = makeSimulation({ workerCount: 1 }); try { placeBank(harness.worldDb, 8, 8); harness.simulation.reloadWorld(); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 9 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.enqueue(1, { type: "get-blank-cheques", characterId: character.character_id }); harness.simulation.step(); let bag = harness.simulation.bagOf(entityId); const appleQty = countItem(bag.inventory, ItemId.apple); harness.simulation.enqueue(1, { type: "bank-deposit", characterId: character.character_id, slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.apple), quantity: null }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); harness.simulation.enqueue(1, { type: "write-cheque", characterId: character.character_id, slotIndex: bag.inventory.findIndex((slot) => slot?.itemId === ItemId.blankCheque), vaultSlotIndex: bag.vault.findIndex((slot) => slot?.itemId === ItemId.apple), quantity: 2 }); harness.simulation.step(); bag = harness.simulation.bagOf(entityId); assert.equal((bag.vaultReserved ?? []).length, 1); const chequeIndex = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.filledCheque); assert.ok(chequeIndex >= 0); bag.inventory[chequeIndex].instance.createdAt = harness.now() - CHEQUE_LIFETIME_MS - 1000; harness.simulation.applyBag(entityId, bag); harness.simulation.sweepExpiredCheques(character.character_id, { now: harness.now() }); bag = harness.simulation.bagOf(entityId); assert.equal(bag.inventory[chequeIndex]?.itemId, ItemId.expiredCheque); assert.equal((bag.vaultReserved ?? []).length, 0); assert.equal(countItem(bag.vault, ItemId.apple), appleQty); } finally { harness.close(); } });