Files
bworld/tests/juice_test.ts
T
2026-10-25 16:57:38 -03:00

122 lines
4.6 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// juice: one fluid named and colored after the bworld:juiceable item in its data, and the juicer that makes it
import { assert, assertEquals } from "@std/assert";
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
import type { FluidJson } from "$/common/mod_data.ts";
import { display_name } from "$/common/utils.ts";
import { copy_dir, stone_floor, test_game } from "./helpers.ts";
const CLOUDBERRY = "testberries:cloudberry";
// mods/bworld and another mod with a juiceable berry, so there's always one whatever bworld's items have
async function setup() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_juice_" });
copy_dir("mods/bworld", `${dir}/bworld`);
const write = (path: string, json: unknown) => {
Deno.mkdirSync(`${dir}/testberries/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
Deno.writeTextFileSync(`${dir}/testberries/${path}`, JSON.stringify(json));
};
write("manifest.json", {
format_version: 1,
id: "testberries",
name: "Test Berries",
version: "1.0.0",
dependencies: [{ id: "bworld", version: "*" }],
});
write("items/cloudberry.json", {
format_version: 1,
item: {
id: CLOUDBERRY,
texture: "engine:missing",
components: { "bworld:juiceable": { juice: 100, color: "#e0a030" } },
},
});
const t = await test_game(dir);
Deno.removeSync(dir, { recursive: true });
return { ...t, fluids: t.game.mods.fluid_rules.api() };
}
Deno.test("juice made from any item with bworld:juiceable is named after it, in the component's color", async () => {
const { fluids } = await setup();
const juiceable = EverythingRegistry.entries<ItemRegistry>("items")
.filter(([, item]) => item.components?.["bworld:juiceable"]);
assert(juiceable.length > 0);
for (const [id, item] of juiceable) {
const juice = { id: "bworld:juice", data: { item: id } };
assertEquals(fluids.name(juice), `${display_name(id)} Juice`);
assertEquals(fluids.color(juice), (item.components!["bworld:juiceable"] as { color: string }).color);
}
// juice that isn't from an item is plain juice
const plain = EverythingRegistry.get<FluidJson>("fluids", "bworld:juice")!;
assertEquals([fluids.name({ id: "bworld:juice" }), fluids.color({ id: "bworld:juice" })], [
plain.name,
plain.color,
]);
});
Deno.test("the juicer takes juiceable items, juices one every 4 presses, and fills empty containers from it", async () => {
const t = await setup();
t.join(1, "alice");
stone_floor(t.game);
const move = (sneaking: boolean) =>
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0, ...(sneaking && { sneaking }) });
move(false);
const player = t.game.players()[0];
const api = t.game.mods.player(player);
api.give_item(CLOUDBERRY, 4);
api.give_item("bworld:glass_bottle", 2);
// selects the slot with id, or an empty one without
const hold = (id?: string) =>
t.send(1, {
type: "select_slot",
slot: player.inventory.to_data().findIndex((item) => (item?.id ?? null) === (id ?? null)),
});
const use = () => t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const press = (times: number) => {
hold();
move(true);
for (let i = 0; i < times; i++) use();
move(false);
};
// the bottles, empty ones first
const bottles = () =>
player.inventory.to_data().filter((item) => item?.id === "bworld:glass_bottle")
.sort((a, b) => Number(a!.data !== undefined) - Number(b!.data !== undefined));
t.game.set_block(1, 100, 0, "bworld:juicer");
hold(CLOUDBERRY);
use();
assertEquals(player.inventory.to_data().filter((item) => item?.id === CLOUDBERRY), []);
// right clicking with an empty hand without sneaking doesn't press it
hold();
use();
// 2 cloudberries and 3 presses toward the next are 200 mB, not a bottle's 250
press(11);
hold("bworld:glass_bottle");
use();
assertEquals(bottles(), [{ id: "bworld:glass_bottle", count: 2 }]);
// the 3rd makes 300 mB, a bottle and 50 mB left, which isn't another
press(1);
hold("bworld:glass_bottle");
use();
use();
const juice = { id: "bworld:juice", data: { item: CLOUDBERRY } };
assertEquals(bottles(), [
{ id: "bworld:glass_bottle", count: 1 },
{ id: "bworld:glass_bottle", count: 1, data: { fluid: juice } },
]);
// the cloudberry it didn't press yet comes out when it's broken
t.send(1, { type: "break_block", x: 1, y: 100, z: 0 });
const dropped = t.game.item_entities().map((entity) => entity.item.to_data());
assertEquals(dropped.sort((a, b) => a.id.localeCompare(b.id)), [
{ id: "bworld:juicer", count: 1 },
{ id: CLOUDBERRY, count: 1 },
]);
});