// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // juice: one fluid named, colored and valued after the bworld:juiceable item in its data, and the juicer that makes it import { assert, assertEquals, assertRejects } from "@std/assert"; import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts"; import type { FluidJson } from "@/common/mod_data.ts"; import type { ClientContext, Instances, Visual, VisualBlock } from "@/common/mod_api/client.ts"; import { section_index } from "@/common/section.ts"; import { block_value, display_name } from "@/common/utils.ts"; import { sell_value } from "@/common/inventory.ts"; import { block_visuals, Visuals } from "@/client/rendering/visuals.ts"; import { AssetManager } from "@/client/assets.ts"; import { fluid_rules, set_fluid_rules } from "@/client/fluid_rules.ts"; import { client as setup_client } from "@/mods/bworld/scripts/client.ts"; import { JUICE_VALUE, type JuiceableParams, WINE_VALUE } from "@/mods/bworld/scripts/shared.ts"; import type { FermentableRecipe } from "@/mods/bworld/scripts/fermenting.ts"; import { copy_dir, stone_floor, test_game } from "./helpers.ts"; const CLOUDBERRY = "testberries:cloudberry"; const PEBBLE = "testberries:pebble"; // mods/bworld and another mod with a juiceable berry, so there's always one whatever bworld's items have, a pebble // that isn't juiceable, and recipes files by name async function setup(recipes: Record = {}) { 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: "cubi:missing", components: { "bworld:juiceable": { juice: 100, color: "#e0a030" } }, }, }); write("items/pebble.json", { format_version: 1, item: { id: PEBBLE, texture: "cubi:missing" } }); for (const [name, recipe] of Object.entries(recipes)) write(`recipes/${name}`, { format_version: 1, recipe }); try { const t = await test_game(dir); return { ...t, fluids: t.game.mods.fluid_rules.api() }; } finally { Deno.removeSync(dir, { recursive: true }); } } 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("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("fluids", "bworld:juice")!; assertEquals([fluids.name({ id: "bworld:juice" }), fluids.color({ id: "bworld:juice" })], [ plain.name, plain.color, ]); }); Deno.test("juice in any container sells for JUICE_VALUE times the items it took, rounded up", async () => { const { game } = await setup(); const sellable = EverythingRegistry.entries("items") .filter(([, item]) => item.components?.["bworld:juiceable"] && item.base_sell_value !== undefined); const containers = EverythingRegistry.entries("items").filter(([, item]) => item.fluid_container); assert(sellable.length > 0 && containers.length > 0); for (const [id, item] of sellable) { const { juice } = item.components!["bworld:juiceable"] as JuiceableParams; for (const [container, info] of containers) { const full = { fluid: { id: "bworld:juice", data: { item: id } } }; const value = sell_value(container, full, game.mods.fluid_rules)! - (info.base_sell_value ?? 0); const exact = JUICE_VALUE * item.base_sell_value! * info.fluid_container!.capacity / juice; assert(Number.isInteger(value) && value >= exact - 1e-9 && value < exact + 1, `${container} of ${id}`); } } // juice from what can't be sold, the cloudberry, can't be sold either assertEquals(game.mods.fluid_rules.sell_value({ id: "bworld:juice", data: { item: CLOUDBERRY } }), undefined); }); 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"); // what's in it and its juice, which clients see const shown = () => t.game.world.get_tile(1, 100, 0)?.client_data?.stack; const tank = () => t.game.world.get_tile(1, 100, 0)?.client_data?.tank; const presses = () => t.game.world.get_tile(1, 100, 0)?.client_data?.presses; const juice = { id: "bworld:juice", data: { item: CLOUDBERRY } }; hold(CLOUDBERRY); use(); assertEquals(player.inventory.to_data().filter((item) => item?.id === CLOUDBERRY), []); assertEquals(shown(), { id: CLOUDBERRY, count: 4 }); // 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); assertEquals([tank(), presses()], [{ fluid: juice, amount: 200 }, 3]); 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); assertEquals([shown(), presses()], [{ id: CLOUDBERRY, count: 1 }, undefined]); hold("bworld:glass_bottle"); use(); use(); assertEquals(tank(), { fluid: juice, amount: 50 }); 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 }, ]); }); Deno.test("the juicer juices by bworld:juicing recipes: one for each juiceable item, and any a mod adds", async () => { const pebble_juice = { fluid: { id: "bworld:juice", data: { item: PEBBLE } }, amount: 500 }; const t = await setup({ "pebble_juice.json": { type: "bworld:juicing", input: PEBBLE, fluid_results: [pebble_juice] }, }); const juicing = t.game.recipes.by_type.get("bworld:juicing")!; const cloudberry = juicing.find((recipe) => recipe.input === CLOUDBERRY)!; assertEquals(cloudberry.fluid_results, [{ fluid: { id: "bworld:juice", data: { item: CLOUDBERRY } }, amount: 100, }]); // the pebble isn't juiceable, but its recipe juices it 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]; t.game.mods.player(player).give_item(PEBBLE, 1); t.game.set_block(1, 100, 0, "bworld:juicer"); const use = () => t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); use(); t.send(1, { type: "select_slot", slot: 1 }); move(true); for (let i = 0; i < 4; i++) use(); assertEquals(t.game.world.get_tile(1, 100, 0)?.client_data?.tank, pebble_juice); }); Deno.test("with create's press and basin there, every juicing recipe is also a create:compacting one", async () => { // every mod in mods/, create among them const { game } = await test_game("mods"); const juicing = game.recipes.by_type.get("bworld:juicing")!; const compacting = game.recipes.by_type.get("create:compacting") ?? []; assert(juicing.length > 0); assertEquals( compacting as unknown, juicing.map((recipe) => ({ type: "create:compacting", ingredients: [recipe.input], results: recipe.fluid_results, })), ); // without create there are none const t = await setup(); assertEquals(t.game.recipes.by_type.get("create:compacting"), undefined); }); Deno.test("a broken juicing recipe stops the server from starting", async () => { const broken = { type: "bworld:juicing", input: PEBBLE, fluid_results: [{ fluid: { id: "nope:nothing" }, amount: 1 }], }; await assertRejects(() => setup({ "broken.json": broken }), Error, "bworld:juicing recipe"); }); Deno.test("every juice ferments into wine from the same fruit, named after it in its color", async () => { const { game, fluids } = await setup(); const fermentable = game.recipes.by_type.get("bworld:fermentable") as unknown as FermentableRecipe[]; const juiceable = EverythingRegistry.entries("items") .filter(([, item]) => item.components?.["bworld:juiceable"]); assert(juiceable.some(([id]) => id === CLOUDBERRY)); for (const [id, item] of juiceable) { const recipes = fermentable.filter((recipe) => fluids.same(recipe.input.fluid, { id: "bworld:juice", data: { item: id } }) ); assertEquals(recipes.length, 1, id); const [recipe] = recipes; assertEquals(recipe.output.fluid, { id: "bworld:wine", data: { item: id } }); assert( recipe.input.amount > 0 && recipe.output.amount > 0 && Number.isInteger(recipe.ticks) && recipe.ticks > 0, ); assertEquals(fluids.name(recipe.output.fluid), `${display_name(id)} Wine`); assertEquals( fluids.color(recipe.output.fluid), (item.components!["bworld:juiceable"] as { color: string }).color, ); } }); Deno.test("wine sells for WINE_VALUE times the juice from the same fruit, and not at all when that juice can't be sold", async () => { const { game } = await setup(); const rules = game.mods.fluid_rules; const sellable = EverythingRegistry.entries("items") .filter(([, item]) => item.components?.["bworld:juiceable"] && item.base_sell_value !== undefined); assert(sellable.length > 0); for (const [id] of sellable) { const juice = rules.sell_value({ id: "bworld:juice", data: { item: id } })!; assertEquals(rules.sell_value({ id: "bworld:wine", data: { item: id } }), WINE_VALUE * juice); } // the cloudberry can't be sold, so neither can its juice or wine assertEquals(rules.sell_value({ id: "bworld:wine", data: { item: CLOUDBERRY } }), undefined); }); Deno.test("a broken bworld:fermentable recipe stops the server from starting", async () => { const juice = { fluid: { id: "bworld:juice" }, amount: 100 }; for ( const broken of [ { input: juice, output: { fluid: { id: "nope:nothing" }, amount: 100 }, ticks: 1 }, { input: juice, output: juice, ticks: 0 }, { output: juice, ticks: 1 }, ] ) { await assertRejects( () => setup({ "broken.json": { type: "bworld:fermentable", ...broken } }), Error, "bworld:fermentable recipe", ); } }); // a juicer at 1 100 1 drawn by bworld's client phase, and a way to set a field of its client data. its fluids have the // server's rules, the same as a client's async function drawn_juicer() { const { game } = await setup(); AssetManager.instance.assets["cubi:textures_info"] = {}; const client_rules = fluid_rules; set_fluid_rules(game.mods.fluid_rules); // bworld's client phase, with only what it needs for its visuals const made = new Map Visual>(); setup_client({ ui: { register_menu_screen() {}, add_item_tooltip() {} }, render: { register_visual: (id: string, create: never) => made.set(id, create) }, hud: { register() {}, on_overlay_message() {} }, } as unknown as ClientContext); const juicer = EverythingRegistry.get_id("blocks", "bworld:juicer")!; block_visuals.clear(); block_visuals.set(juicer, { mod: "bworld", replaces_model: false, create: made.get("bworld:juicer")! }); const data: Record = {}; const visuals = new Visuals({ get_block_data: () => data, get_light: () => 0 }); visuals.meshed(0, 0, 96, Uint32Array.of(section_index(1, 100, 1), block_value(juicer, 0))); const set = (field: string, value: unknown) => { data[field] = value; visuals.data_changed(1, 100, 1); }; const done = () => { block_visuals.clear(); set_fluid_rules(client_rules); }; return { visuals, set, done }; } Deno.test("the juicer draws its press, what's in it, more of it the more there is, and nothing when it's empty", async () => { const { visuals, set, done } = await drawn_juicer(); // the press, whatever's in it const press = [...visuals.groups()].filter((group) => group.instances.length > 0); assertEquals(press.map((group) => group.instances.length), [1]); // one item, however many of it are drawn const drawn = () => [...visuals.groups()].filter((group) => !press.includes(group) && group.instances.length > 0) .map((group) => group.instances.length); set("stack", { id: CLOUDBERRY, count: 1 }); const one = drawn(); assertEquals(one.length, 1); set("stack", { id: CLOUDBERRY, count: 64 }); assertEquals(drawn().length, 1); assert(drawn()[0] > one[0], `${drawn()} for 64, ${one} for 1`); set("stack", undefined); assertEquals(drawn(), []); done(); }); Deno.test("the juicer's juice fills its 12 by 12 floor, a pixel deep while there's any and 12 when it's full", async () => { const { visuals, set, done } = await drawn_juicer(); const boxes = () => [...visuals.groups()].filter((group) => group.translucent); // where the box is on each axis, in pixels of the block const box = () => [0, 1, 2].map((axis) => { const at = boxes()[0].quads.flatMap((quad) => quad.positions.filter((_, i) => i % 3 === axis)); return [Math.min(...at) * 16, Math.max(...at) * 16]; }); const juice = { id: "bworld:juice", data: { item: CLOUDBERRY } }; set("tank", { fluid: juice, amount: 10 }); assertEquals(box(), [[2, 14], [2, 3], [2, 14]]); // in the color of the juice of what it was made from assertEquals(boxes()[0].quads[0].tint, [0xe0 / 255, 0xa0 / 255, 0x30 / 255]); set("tank", { fluid: juice, amount: 500 }); assertEquals(box(), [[2, 14], [2, 8], [2, 14]]); set("tank", { fluid: juice, amount: 1000 }); assertEquals(box(), [[2, 14], [2, 14], [2, 14]]); set("tank", undefined); assertEquals(boxes(), []); done(); }); Deno.test("the juicer's press goes down a share of the way each press, easing there, and back up once there's juice", async () => { const { visuals, set, done } = await drawn_juicer(); const [press] = [...visuals.groups()].filter((group) => group.instances.length > 0); // how far below its block it is const down = () => 100 - press.instances[0].placement[13]; const frame = (time: number) => visuals.frame([{ x: 0, z: 0 }], time); frame(0); assertEquals(down(), 0); // half way to the 4th press, which would take it 6 pixels down to the floor set("presses", 2); frame(1); assert(down() > 0 && down() < 3 / 16, `${down()}`); frame(5); assertEquals(down(), 3 / 16); // the juice is made, so it's back up set("presses", undefined); frame(9); assertEquals(down(), 0); done(); });