Files
bworld/tests/recipe_viewer_test.ts
T
2026-10-03 13:41:01 -03:00

72 lines
3.7 KiB
TypeScript

// the recipe viewer mod reads every mod's recipes, its own types included
import { assert, assertEquals } from "@std/assert";
import { category_name } 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 { EverythingRegistry } from "$/common/everything_registry.ts";
import type { ClientContext } from "bworld/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());
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("farmersdelight:cabbage_rolls");
assertEquals(category_name(rolls.type), "Cooking");
assertEquals(ids(inputs(rolls)), [
"farmersdelight:cabbage",
"bworld:carrot|bworld:potato|farmersdelight:onion|farmersdelight:tomato",
]);
const [rice] = making("farmersdelight:cooked_rice");
assertEquals(ids(inputs(rice)), ["farmersdelight: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("farmersdelight:cooked_rice"));
assert(served_in_bowls.includes("farmersdelight:tomato_sauce"));
assert(using("bworld:coal").some((r) => r.type === "fuel"));
assert(using("bworld:coal").some((r) => r.type === "shaped" && outputs(r)[0].ids[0] === "farmersdelight:stove"));
assert(using("bworld:potato").includes(rolls));
// smelting and smithing
assertEquals(ids(making("bworld:iron_ingot").flatMap(inputs)), ["bworld:iron_ore"]);
assertEquals(ids(inputs(making("bworld:stone_pickaxe")[0])), ["bworld:wood_pickaxe", "bworld:stone"]);
assertEquals(category_name("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 = {
recipes: { list: () => game.recipes.all },
tags: tag_api(),
items: { exists: (id: string) => EverythingRegistry.get("items", id) !== undefined },
} as unknown as ClientContext;
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 === "farmersdelight:cooking")?.catalysts, [
"farmersdelight:cooking_pot",
]);
const furnace_uses = index.find("bworld:furnace", "uses").map(({ category, recipes }) => [category.type, recipes]);
const all = game.recipes.all;
assertEquals(furnace_uses.find(([type]) => type === "furnace")?.[1], all.filter((r) => r.type === "furnace"));
assertEquals(furnace_uses.find(([type]) => type === "fuel")?.[1], all.filter((r) => r.type === "fuel"));
assertEquals(index.find("bworld:air", "recipes"), []);
});