Files
bworld/tests/recipe_viewer_test.ts
T
2026-11-14 12:18:41 -03:00

225 lines
10 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the recipe viewer mod reads every mod's recipes, its own types included
import { assert, assertEquals } from "@std/assert";
import { category_name, type RecipeLayout } from "../mods/recipe_viewer/scripts/categories.ts";
import { inputs as recipe_inputs, outputs } from "../mods/recipe_viewer/scripts/recipes.ts";
import { RecipeIndex } from "../mods/recipe_viewer/scripts/recipes_screen.ts";
import { tag_api } from "@/common/tags.ts";
import { FluidRules } from "@/common/fluids.ts";
import { type BlockRegistry, EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
import type { ClientContext } from "cubi/client";
import { test_game } from "./helpers.ts";
const ids = (ingredients: { ids: string[] }[]) => ingredients.map((i) => i.ids.join("|"));
// deno-lint-ignore no-explicit-any
const inputs = (recipe: any) => recipe_inputs(recipe, tag_api());
// what the recipe viewer reads of ctx.items, from the registry like the client's
function client_items() {
return {
exists: (id: string) => EverythingRegistry.get("items", id) !== undefined,
ids: () => EverythingRegistry.entries("items").map(([id]) => id),
components: (id: string) => EverythingRegistry.get<ItemRegistry>("items", id)?.components ?? {},
};
}
// and of ctx.blocks and ctx.loot
function client_blocks() {
return {
exists: (id: string) => EverythingRegistry.get("blocks", id) !== undefined,
components: (id: string) => EverythingRegistry.get<BlockRegistry>("blocks", id)?.components ?? {},
};
}
const client_loot = () => ({ table: (id: string) => EverythingRegistry.get("loot_tables", id) });
// the context the recipe viewer gets, with these recipes from the mods' files
// deno-lint-ignore no-explicit-any
function client_ctx(recipes: any[] = []) {
return {
recipes: { list: () => recipes },
tags: tag_api(),
items: client_items(),
blocks: client_blocks(),
loot: client_loot(),
fluids: new FluidRules(() => undefined).api(),
} as unknown as ClientContext;
}
const item = (id: string) => ({ kind: "item" as const, id });
const fluid = (id: string) => ({ kind: "fluid" as const, id });
// a recipe layout that keeps nothing, for the parts a test doesn't look at
const ignore: RecipeLayout = { slot: () => {}, fluid: () => {}, texture: () => {}, text: () => {} };
Deno.test("the recipe viewer finds what makes and uses an item, in any recipe type", async () => {
const { game } = await test_game("mods");
const all = game.recipes.all;
const making = (item: string) => all.filter((r) => outputs(r).some((o) => o.ids.includes(item)));
const using = (item: string) => all.filter((r) => inputs(r).some((i) => i.ids.includes(item)));
// a shaped recipe, with its count
const [bowl] = making("bworld:bowl");
assertEquals(bowl.type, "shaped");
assertEquals(outputs(bowl), [{ ids: ["bworld:bowl"], count: 4 }]);
// the cooking pot's own type: ingredients, alternatives, and the bowl it's served in
const [rolls] = making("bworld:cabbage_rolls");
assertEquals(category_name(rolls.type), "Cooking");
assertEquals(ids(inputs(rolls)), [
"bworld:cabbage",
"bworld:carrot|bworld:potato|bworld:onion|bworld:tomato",
]);
const [rice] = making("bworld:cooked_rice");
assertEquals(ids(inputs(rice)), ["bworld:rice", "bworld:bowl"]);
// uses: the bowl serves meals, coal is fuel and in the stove, potatoes are in cabbage rolls
const served_in_bowls = using("bworld:bowl").map((r) => outputs(r)[0].ids[0]);
assert(served_in_bowls.includes("bworld:cooked_rice"));
assert(served_in_bowls.includes("bworld:tomato_sauce"));
assert(using("bworld:coal").some((r) => r.type === "bworld:fuel"));
assert(using("bworld:coal").some((r) => r.type === "shaped" && outputs(r)[0].ids[0] === "bworld:stove"));
assert(using("bworld:potato").includes(rolls));
// smelting and smithing
assert(
using("bworld:iron_ore").some((r) =>
r.type === "bworld:smelting" && outputs(r)[0].ids[0] === "bworld:iron_ingot"
),
);
assertEquals(ids(inputs(making("bworld:stone_pickaxe")[0])), [
"bworld:wood_pickaxe",
"bworld:stone",
"bworld:brass_coin",
]);
assertEquals(category_name("bworld:smithing"), "Smithing");
});
Deno.test("recipes are found by category in jei's order, and a catalyst's uses are everything made in it", async () => {
const { game } = await test_game("mods");
const ctx = client_ctx(game.recipes.all);
const index = new RecipeIndex(ctx);
const titles = index.categories.map((category) => category.title);
assertEquals(titles.slice(0, 4), ["Crafting", "Smelting", "Fuel", "Smithing"]);
assert(titles.includes("Cooking"));
assertEquals(index.categories.find((c) => c.type === "bworld:cooking")?.catalysts, [
"bworld:cooking_pot",
]);
const furnace_uses = index.find(item("bworld:furnace"), "uses").map((
{ category, recipes },
) => [category.type, recipes]);
const all = game.recipes.all;
assertEquals(
furnace_uses.find(([type]) => type === "bworld:smelting")?.[1],
all.filter((r) => r.type === "bworld:smelting"),
);
assertEquals(
furnace_uses.find(([type]) => type === "bworld:fuel")?.[1],
all.filter((r) => r.type === "bworld:fuel"),
);
assertEquals(index.find(item("cubi:air"), "recipes"), []);
});
Deno.test("clicking a fish shows when and where it bites, and the fishing rod's uses are every fish", async () => {
await test_game("mods");
const ctx = client_ctx();
const index = new RecipeIndex(ctx);
const [fishing] = index.find(item("bworld:red_mullet"), "recipes");
assertEquals(fishing.category.title, "Fishing");
assertEquals(fishing.category.catalysts, ["bworld:fishing_rod"]);
const lines: string[] = [];
const slots: string[] = [];
fishing.category.draw(fishing.recipes[0], {
...ignore,
slot: (ingredient) => slots.push(ingredient!.ids[0]),
texture: () => {},
text: (text) => lines.push(text),
});
assertEquals(slots, ["bworld:red_mullet"]);
assertEquals(lines, ["Seasons: Summer, Winter", "Biomes: Ocean", "Time: 6:00 to 19:00", "In: Water"]);
// a lava fish without limits, and every fish under the rod
const lava_lines: string[] = [];
const [lava] = index.find(item("bworld:prismfish"), "recipes");
lava.category.draw(lava.recipes[0], { ...ignore, text: (text) => lava_lines.push(text) });
assertEquals(lava_lines, ["Seasons: all", "Biomes: all", "Time: all day", "In: Lava"]);
const fish = index.find(item("bworld:fishing_rod"), "uses")[0].recipes.map((recipe) => recipe.result);
assert(fish.includes("bworld:cod") && fish.includes("bworld:prismfish"));
// a fish isn't a use of the water it lives in
assertEquals(index.find(item("bworld:water"), "uses"), []);
});
Deno.test("a crab pot's recipes are what each bait catches in fresh and salt water, with their chances", async () => {
await test_game("mods");
const ctx = client_ctx();
const index = new RecipeIndex(ctx);
// grubs are bait for both waters, under the crab pot's tab
const [grubs] = index.find(item("bworld:grubs"), "uses");
assertEquals(grubs.category.title, "Crab Pot");
assertEquals(grubs.category.catalysts, ["bworld:crabpot"]);
assertEquals(grubs.recipes.map((recipe) => recipe.crabpot.water), ["freshwater", "saltwater"]);
// a seashroom comes from grubs in salt water, one in 16
const [seashroom] = index.find(item("bworld:red_seashroom"), "recipes");
assertEquals(seashroom.recipes.length, 1);
const recipe = seashroom.recipes[0];
assertEquals(recipe.bait, "bworld:grubs");
const slots: [string, string[] | undefined][] = [];
const lines: string[] = [];
seashroom.category.draw(recipe, {
...ignore,
slot: (ingredient, _x, _y, tooltip) => slots.push([ingredient!.ids[0], tooltip]),
texture: () => {},
text: (text) => lines.push(text),
});
assertEquals(lines, ["Saltwater"]);
assertEquals(slots[0], ["bworld:grubs", ["Bait"]]);
assertEquals(slots.length, 1 + 16);
assertEquals(slots.find(([id]) => id === "bworld:red_seashroom")?.[1], ["6.3% chance"]);
// worms in fresh water catch a third each of three things
const worms = index.find(item("bworld:worms"), "uses").find((found) => found.category.title === "Crab Pot")!;
const freshwater = worms.recipes.find((r) => r.crabpot.water === "freshwater");
assertEquals(freshwater.result.toSorted(), ["bworld:chorus_snail", "bworld:crayfish", "bworld:leech"]);
assertEquals(freshwater.crabpot.catches.map((c: { chance: number }) => c.chance), [1 / 3, 1 / 3, 1 / 3]);
});
Deno.test("each juiceable item has a juicing recipe that presses it into its own juice", async () => {
// registered by the base game's shared phase, see mods/bworld/scripts/juicing.ts
const { game } = await test_game("mods");
const index = new RecipeIndex(client_ctx(game.recipes.all));
// a strawberry's uses include the juicer pressing it into strawberry juice, shown in a fluid slot
const juicing = index.find(item("bworld:strawberry"), "uses").find((found) => found.category.title === "Juicing")!;
assertEquals(juicing.category.catalysts, ["bworld:juicer"]);
assertEquals(juicing.recipes.length, 1);
const slots: string[] = [];
const fluids: [unknown, number][] = [];
juicing.category.draw(juicing.recipes[0], {
...ignore,
slot: (ingredient) => slots.push(ingredient!.ids[0]),
fluid: (fluid, amount) => fluids.push([fluid, amount]),
});
assertEquals(slots, ["bworld:strawberry"]);
const { juice } = EverythingRegistry.get<ItemRegistry>("items", "bworld:strawberry")!.components![
"bworld:juiceable"
] as { juice: number };
assertEquals(fluids, [[{ id: "bworld:juice", data: { item: "bworld:strawberry" } }, juice]]);
// the juicer's uses are every juiceable item
const pressed = index.find(item("bworld:juicer"), "uses")[0].recipes.map((recipe) => recipe.input);
assert(pressed.includes("bworld:strawberry") && pressed.includes("bworld:melon"));
// juice, whatever it's from, is made by every one of them, and nothing uses it yet
const [making_juice] = index.find(fluid("bworld:juice"), "recipes");
assertEquals(making_juice.category.title, "Juicing");
assertEquals(making_juice.recipes.map((recipe) => recipe.input), pressed);
assertEquals(index.find(fluid("bworld:juice"), "uses"), []);
// a fluid isn't an item of the same id
assertEquals(index.find(item("bworld:juice"), "recipes"), []);
});