113 lines
5.4 KiB
TypeScript
113 lines
5.4 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, type ItemRegistry } 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());
|
|
|
|
// 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 ?? {},
|
|
};
|
|
}
|
|
|
|
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: client_items(),
|
|
} 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"), []);
|
|
});
|
|
|
|
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 = {
|
|
recipes: { list: () => [] },
|
|
tags: tag_api(),
|
|
items: client_items(),
|
|
} as unknown as ClientContext;
|
|
const index = new RecipeIndex(ctx);
|
|
const [fishing] = index.find("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], {
|
|
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("bworld:prismfish", "recipes");
|
|
lava.category.draw(lava.recipes[0], { slot: () => {}, texture: () => {}, text: (text) => lava_lines.push(text) });
|
|
assertEquals(lava_lines, ["Seasons: all", "Biomes: all", "Time: all day", "In: Lava"]);
|
|
const fish = index.find("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("bworld:water", "uses"), []);
|
|
});
|