Files
bworld/tests/worm_bin_test.ts
T
2026-10-12 15:09:22 -03:00

132 lines
5.7 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the worm bin: one worm_binable item in its input makes 5 more of it every morning, into its output
import { assert, assertEquals } from "@std/assert";
import { CYCLE_TICKS } from "$/common/time.ts";
import { Container, ItemStack } from "$/common/inventory.ts";
import { menu_items, opened_menu, test_game } from "./helpers.ts";
// the container's slots, see register_worm_bin
const OUTPUT = 0;
const INPUT = 1;
// the menu's: the input, the output, then the inventory
const MENU_INPUT = 0;
const MENU_OUTPUT = 1;
const inventory_slot = (index: number) => 2 + index;
// a worm bin at 0 100 0 next to alice, with the items given in her inventory from the first hotbar slot on
async function setup(items: [string, number][] = []) {
const t = await test_game("mods");
t.join(1, "alice");
t.game.set_block(0, 99, 0, "bworld:stone");
t.game.set_block(0, 100, 0, "bworld:worm_bin");
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
for (const [id, count] of items) t.send(1, { type: "chat", text: `/give ${id} ${count}` });
const tile = t.game.world.get_tile(0, 100, 0)!;
const container = () => t.game.containers.get((tile.mod_data as { container: string }).container)!;
const next_morning = () => {
t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1);
t.game.tick();
};
const open = () => {
t.take(1);
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
return opened_menu(t.take(1));
};
const click = (menu: { id: number }, slot: number, shift = false) =>
t.send(1, { type: "click", menu: menu.id, slot, button: 0, shift });
return { t, tile, container, next_morning, open, click };
}
Deno.test("right clicking a worm bin opens its input, an arrow's width from its output", async () => {
const { open } = await setup();
const opened = open();
assertEquals([opened.menu_type, opened.title], ["bworld:worm_bin", "Worm Bin"]);
assertEquals(opened.slots.length, 2 + 36);
assertEquals(opened.slots[MENU_INPUT], { x: 55, y: 34, icon: "bworld:empty_slot_bait", max_count: 1 });
assertEquals(opened.slots[MENU_OUTPUT], { x: 115, y: 34, output: true });
});
Deno.test("the input takes one worm_binable item, and nothing else", async () => {
const { t, container, open, click } = await setup([["bworld:worms", 10], ["bworld:coal", 3], ["bworld:grubs", 4]]);
const menu = open();
// coal isn't worm_binable
click(menu, inventory_slot(1));
click(menu, MENU_INPUT);
assertEquals(container().get_item(INPUT), undefined);
click(menu, inventory_slot(1));
// of a stack of worms, one goes in and the rest stay on the cursor
click(menu, inventory_slot(0));
click(menu, MENU_INPUT);
assertEquals(container().get_item(INPUT)?.to_data(), { id: "bworld:worms", count: 1 });
click(menu, MENU_INPUT);
assertEquals(container().get_item(INPUT)?.amount, 1);
click(menu, inventory_slot(0));
assertEquals(menu_items(t.take(1))[inventory_slot(0)], { id: "bworld:worms", count: 9 });
// a stack of grubs doesn't swap with it, it wouldn't fit
click(menu, inventory_slot(2));
click(menu, MENU_INPUT);
assertEquals(container().get_item(INPUT)?.type_id, "bworld:worms");
click(menu, inventory_slot(2));
// shift clicking moves just one in too, once it's empty
click(menu, MENU_INPUT, true);
assertEquals(container().get_item(INPUT), undefined);
click(menu, inventory_slot(2), true);
assertEquals(container().get_item(INPUT)?.to_data(), { id: "bworld:grubs", count: 1 });
assertEquals(menu_items(t.take(1))[inventory_slot(2)], { id: "bworld:grubs", count: 3 });
});
Deno.test("every morning a worm bin makes 5 of what's in its input, up to a full stack", async () => {
const { container, next_morning } = await setup();
// an empty input makes nothing
next_morning();
assertEquals(container().get_item(OUTPUT), undefined);
container().set_item(INPUT, new ItemStack("bworld:worms"));
next_morning();
assertEquals(container().get_item(OUTPUT)?.to_data(), { id: "bworld:worms", count: 5 });
next_morning();
assertEquals(container().get_item(OUTPUT)?.amount, 10);
container().set_item(OUTPUT, new ItemStack("bworld:worms", 62));
next_morning();
assertEquals(container().get_item(OUTPUT)?.amount, 64);
// the input is never used up
assertEquals(container().get_item(INPUT)?.to_data(), { id: "bworld:worms", count: 1 });
// grubs make grubs, once the worms are taken out
container().set_item(INPUT, new ItemStack("bworld:grubs"));
next_morning();
assertEquals(container().get_item(OUTPUT)?.to_data(), { id: "bworld:worms", count: 64 });
container().set_item(OUTPUT, undefined);
next_morning();
assertEquals(container().get_item(OUTPUT)?.to_data(), { id: "bworld:grubs", count: 5 });
});
Deno.test("a worm bin from before the input keeps its worms", async () => {
const { t, tile, container, next_morning } = await setup();
const old = new Container(1);
old.set_item(0, new ItemStack("bworld:worms", 20));
t.game.containers.set("old_worm_bin", old);
tile.mod_data = { container: "old_worm_bin" };
next_morning();
assertEquals(container().size, 2);
assertEquals(container().get_item(OUTPUT)?.to_data(), { id: "bworld:worms", count: 20 });
assertEquals(t.game.containers.has("old_worm_bin"), false);
});
Deno.test("breaking a worm bin drops it, its input and what it made", async () => {
const { t, container, next_morning } = await setup();
container().set_item(INPUT, new ItemStack("bworld:worms"));
next_morning();
t.send(1, { type: "break_block", x: 0, y: 100, z: 0 });
const dropped = t.game.item_entities().map((e) => `${e.item.type_id} ${e.item.amount}`).sort();
assertEquals(dropped, ["bworld:worm_bin 1", "bworld:worms 1", "bworld:worms 5"]);
assert(t.game.world.get_block_id(0, 100, 0) === "bworld:air");
});