import test from "node:test"; import assert from "node:assert/strict"; import { ItemId, itemDefinition } from "../../shared/definitions/items.js"; import { emptyEquipment, emptySlots } from "../../shared/items/inventory.js"; import { addGroundItem, placeOrMergeGroundStack } from "../src/items/bags.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; function emptyWorld (settings = {}) { return { groundItems: new Map(), groundByTile: new Map(), nextGroundItemId: 1, settings: { groundItemDespawnDurationMs: 600_000, ...settings } }; } function giveInventory (harness, entityId, items) { const inventory = emptySlots(); items.forEach((stack, index) => { inventory[index] = { itemId: stack.itemId, quantity: stack.quantity, ...(stack.instance ? { instance: structuredClone(stack.instance) } : {}) }; }); harness.simulation.applyBag(entityId, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); } test("placeOrMergeGroundStack merges matching piles and resets despawn", () => { const world = emptyWorld(); const now = 10_000; addGroundItem(world, { groundItemId: 1, tileX: 4, tileY: 5, stack: { itemId: ItemId.coins, quantity: 25 }, droppedAt: 1_000, expiresAt: 5_000, source: "mob-drop" }); const placed = placeOrMergeGroundStack(world, { tileX: 4, tileY: 5, stack: { itemId: ItemId.coins, quantity: 10 }, now, source: "player-drop", droppedByCharacterId: 7, groundItemId: 99 }); assert.equal(world.groundItems.size, 1); const pile = world.groundItems.get(1); assert.equal(pile.stack.quantity, 35); assert.equal(pile.expiresAt, now + 600_000); assert.equal(pile.droppedAt, now); assert.equal(pile.source, "mob-drop"); assert.equal(placed.events.length, 1); assert.equal(placed.events[0].groundItemId, 1); assert.equal(placed.events[0].quantity, 35); assert.equal(placed.events[0].expiresAt, now + 600_000); assert.equal(world.groundItems.has(99), false); }); test("placeOrMergeGroundStack overflows into a new pile at max stack", () => { const world = emptyWorld(); const max = itemDefinition(ItemId.apple).maxStackSize ?? 99; addGroundItem(world, { groundItemId: 1, tileX: 1, tileY: 1, stack: { itemId: ItemId.apple, quantity: max }, droppedAt: 1_000, expiresAt: 2_000, source: "player-drop" }); const placed = placeOrMergeGroundStack(world, { tileX: 1, tileY: 1, stack: { itemId: ItemId.apple, quantity: 5 }, now: 50_000, source: "player-drop", groundItemId: 2 }); assert.equal(world.groundItems.size, 2); assert.equal(world.groundItems.get(1).stack.quantity, max); assert.equal(world.groundItems.get(1).expiresAt, 2_000); assert.equal(world.groundItems.get(2).stack.quantity, 5); assert.equal(world.groundItems.get(2).expiresAt, 50_000 + 600_000); assert.equal(placed.events.length, 1); assert.equal(placed.events[0].groundItemId, 2); }); test("placeOrMergeGroundStack does not merge non-stackable equipment", () => { const world = emptyWorld(); const equipmentId = Object.values(ItemId).find((id) => { const def = itemDefinition(id); return def?.equipment && def.equipment.kind !== "arrowAmmo"; }); assert.ok(equipmentId); addGroundItem(world, { groundItemId: 1, tileX: 2, tileY: 2, stack: { itemId: equipmentId, quantity: 1 }, droppedAt: 1, expiresAt: 100, source: "mob-drop" }); placeOrMergeGroundStack(world, { tileX: 2, tileY: 2, stack: { itemId: equipmentId, quantity: 1 }, now: 200, source: "mob-drop", groundItemId: 2 }); assert.equal(world.groundItems.size, 2); }); test("dropping coins onto an existing ground coin pile merges and refreshes despawn", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); giveInventory(harness, entityId, [ { itemId: ItemId.coins, quantity: 40 } ]); const bag = harness.simulation.bagOf(entityId); const coinSlot = bag.inventory.findIndex((slot) => slot?.itemId === ItemId.coins); assert.ok(coinSlot >= 0); harness.simulation.enqueue(1, { type: "drop-item", characterId: character.character_id, slotIndex: coinSlot, quantity: 15 }); const first = harness.simulation.step().events.find((event) => event.type === "ground-item-appear"); assert.ok(first); assert.equal(first.quantity, 15); const firstExpires = first.expiresAt; const groundId = first.groundItemId; harness.advance(5_000); harness.simulation.enqueue(1, { type: "drop-item", characterId: character.character_id, slotIndex: coinSlot, quantity: 10 }); const secondEvents = harness.simulation.step().events.filter((event) => event.type === "ground-item-appear"); assert.equal(secondEvents.length, 1); assert.equal(secondEvents[0].groundItemId, groundId); assert.equal(secondEvents[0].quantity, 25); assert.ok(secondEvents[0].expiresAt > firstExpires); const snapshot = harness.simulation.snapshotChunk(0, 0); const piles = (snapshot.groundItems ?? []).filter((item) => item.itemId === ItemId.coins); assert.equal(piles.length, 1); assert.equal(piles[0].quantity, 25); } finally { harness.close(); } });