Files
bworld/tests/cutting_board_test.ts
T
2026-11-01 00:49:05 -03:00

124 lines
5.0 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// farmer's delight's cutting board: holds a stack of anything, shows it on top, and a knife cuts it one at a time
import { assert, assertEquals } from "@std/assert";
import { get_state_value } from "$/common/utils.ts";
import type { ItemData } from "$/common/inventory.ts";
import { stone_floor, test_game } from "./helpers.ts";
const BOARD = [0, 100, 0] as const;
async function setup(yaw = 0) {
const t = await test_game("mods");
t.join(1, "alice");
stone_floor(t.game);
t.send(1, { type: "move", x: 2.5, y: 100, z: 2.5, yaw, pitch: 0 });
t.send(1, { type: "chat", text: "/give bworld:cutting_board" });
t.send(1, { type: "select_slot", slot: 0 });
t.send(1, { type: "use_block", x: BOARD[0], y: BOARD[1] - 1, z: BOARD[2], face: "top" });
assertEquals(t.game.world.get_block_id(...BOARD), "bworld:cutting_board");
const use = (slot: number) => {
t.send(1, { type: "select_slot", slot });
t.send(1, { type: "use_block", x: BOARD[0], y: BOARD[1], z: BOARD[2], face: "top" });
};
const inventory = () => t.game.players()[0].inventory.to_data().filter((i) => i).map((i) => `${i!.id} ${i!.count}`);
const state = (name: string) =>
get_state_value(t.game.world.get_block_value(...BOARD), t.game.world.get_block_info(...BOARD)!, name);
const on_board = () => {
const item = t.game.world.get_tile(...BOARD)?.client_data?.item as ItemData | undefined;
return item ? `${item.id} ${item.count}` : "";
};
const dropped = () => t.game.item_entities().map((e) => `${e.item.type_id} ${e.item.amount}`).sort();
return { ...t, use, inventory, state, on_board, dropped };
}
Deno.test("the board faces whoever placed it", async () => {
// looking north it faces south, back at them
assertEquals((await setup(0)).state("facing"), 2);
// looking west it faces east
assertEquals((await setup(Math.PI / 2)).state("facing"), 1);
assertEquals((await setup(Math.PI)).state("facing"), 0);
assertEquals((await setup(-Math.PI / 2)).state("facing"), 3);
});
Deno.test("so does any block with a facing state, like the furnace, keeping its other states", async () => {
const t = await setup(Math.PI / 2);
const at = [0, 100, 2] as const;
t.send(1, { type: "chat", text: "/give bworld:furnace" });
t.send(1, { type: "select_slot", slot: 0 });
t.send(1, { type: "use_block", x: at[0], y: at[1] - 1, z: at[2], face: "top" });
const info = t.game.world.get_block_info(...at)!;
assertEquals(info.id, "bworld:furnace");
assertEquals(get_state_value(t.game.world.get_block_value(...at), info, "facing"), 1);
assertEquals(get_state_value(t.game.world.get_block_value(...at), info, "lit"), 0);
});
Deno.test("a stack goes on the board, and a knife cuts one at a time by its recipe", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give bworld:cod 3" });
t.send(1, { type: "chat", text: "/give bworld:iron_knife" });
t.use(0);
assertEquals(t.on_board(), "bworld:cod 3");
assertEquals(t.inventory(), ["bworld:iron_knife 1"]);
t.use(1);
assertEquals(t.on_board(), "bworld:cod 2");
assertEquals(t.dropped(), ["bworld:bone_meal 1", "bworld:cod_slice 2"]);
t.use(1);
t.use(1);
assertEquals(t.on_board(), "");
assertEquals(t.dropped().length, 6);
// on an empty board the knife goes on it, like the java mod, and cutting nothing does nothing
t.use(1);
assertEquals(t.on_board(), "bworld:iron_knife 1");
t.send(1, { type: "chat", text: "/give bworld:stone_knife" });
t.use(0);
assertEquals(t.on_board(), "bworld:iron_knife 1");
assertEquals(t.inventory(), ["bworld:stone_knife 1"]);
});
Deno.test("an empty hand takes it all back, and breaking the board drops it", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give bworld:cabbage 5" });
t.use(0);
assertEquals(t.on_board(), "bworld:cabbage 5");
t.use(1);
assertEquals(t.on_board(), "");
assertEquals(t.inventory(), ["bworld:cabbage 5"]);
t.use(0);
t.game.destroy_block(...BOARD);
assertEquals(t.dropped(), ["bworld:cabbage 5", "bworld:cutting_board 1"]);
assertEquals(t.game.world.get_tile(...BOARD), undefined);
});
Deno.test("what a block shows goes to players with its chunk and when it changes, and is saved", async () => {
const t = await setup();
t.send(1, { type: "chat", text: "/give bworld:salmon 2" });
// players only hear about blocks in chunks they have
for (let i = 0; i < 5; i++) t.game.tick();
t.take(1);
t.use(0);
const shown = (conn: number) =>
t.take(conn).filter((m) => m.type === "block_data" && "item" in m.data)
.map((m) => m.data.item && `${m.data.item.id} ${m.data.item.count}`);
assertEquals(shown(1), ["bworld:salmon 2"]);
// someone joining gets it with the chunk
t.join(2, "bob");
for (let i = 0; i < 5; i++) t.game.tick();
assert(shown(2).includes("bworld:salmon 2"));
const loaded = await test_game("mods", t.game.save());
assertEquals(loaded.game.world.get_tile(...BOARD)?.client_data?.item, { id: "bworld:salmon", count: 2 });
// breaking it takes it away for everyone
t.game.destroy_block(...BOARD);
assertEquals(shown(1), [null]);
assertEquals(shown(2), [null]);
});