// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // stacks of one item only merge when their data is the same, whatever order its keys are in import { assertEquals } from "@std/assert"; import { click_slot, Container, type Cursor, ItemStack, LEFT_CLICK } from "@/common/inventory.ts"; import { test_game } from "./helpers.ts"; const coal = (count: number, data?: unknown) => { const stack = new ItemStack("bworld:coal", count); stack.data = data; return stack; }; Deno.test("stacks with different data don't merge in an inventory, and ones whose data only differs in key order do", async () => { await test_game("mods"); const container = new Container(2); container.add_item(coal(3, { mine: "north", grade: 1 })); container.add_item(coal(2, { mine: "south" })); assertEquals(container.to_data().map((item) => item?.count), [3, 2]); assertEquals(container.add_item(coal(4, { mine: "east" })), 4, "there's no slot left for a third kind"); container.add_item(coal(1, { grade: 1, mine: "north" })); assertEquals(container.get_item(0)?.amount, 4); // clicking a stack onto one with other data swaps them instead const cursor: Cursor = { item: coal(5, { mine: "east" }) }; click_slot(container, 1, cursor, LEFT_CLICK); assertEquals([container.get_item(1)?.amount, cursor.item?.amount], [5, 2]); cursor.item = coal(5, { mine: "east" }); click_slot(container, 1, cursor, LEFT_CLICK); assertEquals([container.get_item(1)?.amount, cursor.item], [10, undefined]); // no data is the same as {} const plain = new Container(1); plain.add_item(coal(1)); plain.add_item(coal(1, {})); assertEquals(plain.get_item(0)?.amount, 2); }); Deno.test("stacks with different data don't merge on the ground, and ones whose data only differs in key order do", async () => { const { game, join } = await test_game("mods"); // items only move near a player join(1, "alice"); for (let x = -2; x <= 2; x++) { for (let z = -2; z <= 2; z++) game.set_block(x, 99, z, "bworld:stone"); } game.spawn_item(0.5, 100, 0.5, coal(1, { mine: "north", grade: 1 })); game.spawn_item(0.5, 100, 0.5, coal(1, { mine: "south" })); game.spawn_item(0.5, 100, 0.5, coal(1, { grade: 1, mine: "north" })); for (let i = 0; i < 4; i++) game.tick(); const counts = game.item_entities().map(( entity, ) => [(entity.item.data as { mine: string }).mine, entity.item.amount]); assertEquals(counts.sort(), [["north", 2], ["south", 1]]); });