Files
bworld/tests/fluid_container_test.ts
T
2026-10-25 15:02:31 -03:00

80 lines
3.5 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// items that hold one fluid, full or empty: how many stack, what they look like, and turning the held one into another
import { assertEquals } from "@std/assert";
import { item_tooltip } from "$/client/gui/tooltip.ts";
import { item_layers } from "$/client/rendering/item_renderer.ts";
import { ItemStack } from "$/common/inventory.ts";
import { copy_dir, test_game } from "./helpers.ts";
const JAR = "testjars:jar";
const FULL = { fluid: { id: "testjars:syrup", data: { tree: "maple" } } };
// mods/bworld, and testjars: syrup, and a jar of 250 mB that stacks to 16 empty and 4 full, drawn with the syrup in it
// and a red label
async function setup() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_jars_" });
copy_dir("mods/bworld", `${dir}/bworld`);
const write = (path: string, json: unknown) => {
Deno.mkdirSync(`${dir}/testjars/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
Deno.writeTextFileSync(`${dir}/testjars/${path}`, JSON.stringify(json));
};
write("manifest.json", { format_version: 1, id: "testjars", name: "Test Jars", version: "1.0.0" });
write("fluids/syrup.json", { format_version: 1, fluid: { id: "testjars:syrup", color: "#a0522d" } });
write("items/jar.json", {
format_version: 1,
item: {
id: JAR,
texture: "engine:missing",
max_stack: 16,
fluid_container: { capacity: 250, full_max_stack: 4 },
layers: [{ texture: "engine:break_0", tint: "fluid" }, { texture: "engine:break_1", tint: "#ff0000" }],
},
});
const t = await test_game(dir);
Deno.removeSync(dir, { recursive: true });
t.join(1, "alice");
const player = t.game.players()[0];
return { ...t, player, api: t.game.mods.player(player) };
}
const slots = (t: Awaited<ReturnType<typeof setup>>) =>
t.player.inventory.to_data().flatMap((item, slot) => item ? [[slot, item.count, item.data !== undefined]] : []);
Deno.test("a full fluid container stacks to its full_max_stack, and an empty one to the item's max_stack", async () => {
const t = await setup();
t.api.give_item(JAR, 20);
t.api.give_item(JAR, 6, FULL);
assertEquals(slots(t), [[0, 16, false], [1, 4, false], [2, 4, true], [3, 2, true]]);
// the fluid's name, from its id since it has none, and how much there is
const tooltip = (slot: number) => item_tooltip(t.player.inventory.get_item(slot)!).map((line) => line.text);
assertEquals(tooltip(2).slice(0, 2), ["Jar", "Syrup, 250 mB"]);
assertEquals(tooltip(0), ["Jar"]);
});
Deno.test("a layer tinted fluid is drawn in the fluid's color while there's one, and others always", async () => {
await setup();
const jar = (data?: unknown) => {
const stack = new ItemStack(JAR);
stack.data = data;
return item_layers(stack);
};
const red = { texture: "engine:break_1", color: [1, 0, 0] };
assertEquals(jar(), [red]);
assertEquals(jar(FULL), [{ texture: "engine:break_0", color: [0xa0 / 255, 0x52 / 255, 0x2d / 255] }, red]);
});
Deno.test("exchange_held puts the result in the hand from a stack of one, and in the inventory from more", async () => {
const t = await setup();
t.api.give_item(JAR, 3);
t.api.exchange_held({ id: JAR, count: 1, data: FULL });
assertEquals(slots(t), [[0, 2, false], [1, 1, true]]);
t.player.inventory.set_item(0, undefined);
t.api.give_item(JAR, 1);
t.api.exchange_held({ id: JAR, count: 1, data: FULL });
// the filled one in the hand, the one filled before still beside it
assertEquals(slots(t), [[0, 1, true], [1, 1, true]]);
});