import test from "node:test"; import assert from "node:assert/strict"; import { BlockType } from "../../shared/definitions/tiles.js"; import { ItemId } from "../../shared/definitions/items.js"; import { emptyEquipment } from "../../shared/items/inventory.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("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("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(); } });