Files
bworld/tests/create/recipes_test.ts
T

122 lines
5.6 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// create's recipe types: read and checked when the server starts, and shown by the recipe viewer
import { assert, assertEquals, assertThrows } from "@std/assert";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import { tag_api } from "$/common/tags.ts";
import type { ClientContext } from "$/common/mod_api/client.ts";
import {
COMPACTING,
parse_processing,
PRESSING,
processing_recipes,
} from "$/mods/create/scripts/recipes/processing.ts";
import { inputs, outputs } from "$/mods/recipe_viewer/scripts/recipes.ts";
import { RecipeIndex } from "$/mods/recipe_viewer/scripts/recipes_screen.ts";
import { test_game } from "../helpers.ts";
const lookup = () => ({
expand: (choice: string | string[]) => tag_api().expand(choice),
item_exists: (id: string) => EverythingRegistry.get("items", id) !== undefined,
fluid_exists: (id: string) => EverythingRegistry.get("fluids", id) !== undefined,
});
Deno.test("create's pressing recipes are read with every item each ingredient stands for", async () => {
const { game } = await test_game("mods");
const recipes = processing_recipes(game.recipes.by_type.get(PRESSING.id) ?? [], PRESSING, lookup());
const sheet = recipes.find((recipe) => recipe.results[0].id === "create:iron_sheet");
assert(sheet, "iron is pressed into sheets");
assert(sheet.ingredients[0].includes("bworld:iron_ingot"));
assertEquals(sheet.results, [{ id: "create:iron_sheet", count: 1, chance: 1 }]);
});
Deno.test("a pressing recipe that a press couldn't make says what's wrong with it", async () => {
await test_game("mods");
const problems = (json: unknown) => {
const parsed = parse_processing(json, PRESSING, lookup());
return "problems" in parsed ? parsed.problems : [];
};
const sheet = { id: "create:iron_sheet" };
assertEquals(problems({ ingredients: ["bworld:iron_ingot"], results: [sheet] }), []);
// a press takes one item, and makes up to two
assertEquals(problems({ ingredients: ["bworld:iron_ingot", "bworld:gold_ingot"], results: [sheet] }).length, 1);
assertEquals(problems({ ingredients: ["bworld:iron_ingot"], results: [sheet, sheet, sheet] }).length, 1);
assertEquals(problems({ ingredients: ["bworld:no_such_ingot"], results: [sheet] }).length, 1);
assertEquals(
problems({ ingredients: ["bworld:iron_ingot"], results: [{ id: "create:iron_sheet", chance: 2 }] }).length,
1,
);
assertEquals(problems({ ingredients: ["bworld:iron_ingot"], results: [sheet], processing_time: 100 }).length, 1);
assertEquals(problems({ results: [sheet] }).length, 1);
// the server doesn't start with one, and says which
assertThrows(
() => processing_recipes([{ ingredients: ["bworld:no_such_ingot"], results: [sheet] }], PRESSING, lookup()),
Error,
"bworld:no_such_ingot",
);
});
Deno.test("compacting recipes can make fluids as well as items, and pressing ones can't", async () => {
await test_game("mods");
const juice = { fluid: { id: "bworld:juice", data: { item: "bworld:carrot" } }, amount: 200 };
const parsed = parse_processing({ ingredients: ["bworld:carrot"], results: [juice] }, COMPACTING, lookup());
assertEquals(parsed, { ingredients: [["bworld:carrot"]], results: [], fluid_results: [juice] });
const problems = (json: unknown, type = COMPACTING) => {
const parsed = parse_processing(json, type, lookup());
return "problems" in parsed ? parsed.problems : [];
};
assertEquals(problems({ ingredients: ["bworld:carrot"], results: [juice] }, PRESSING).length, 1);
assertEquals(problems({ ingredients: ["bworld:carrot"], results: [juice, juice, juice] }).length, 1);
assertEquals(
problems({ ingredients: ["bworld:carrot"], results: [{ ...juice, fluid: { id: "a:none" } }] }).length,
1,
);
assertEquals(problems({ ingredients: ["bworld:carrot"], results: [{ ...juice, amount: 0 }] }).length, 1);
// fluids going in aren't supported yet
assertEquals(
problems({ ingredients: [{ fluid: "bworld:juice", amount: 10 }], results: [{ id: "bworld:carrot" }] }),
[
"fluid ingredients aren't supported yet",
],
);
});
Deno.test("the recipe viewer shows pressing recipes under the press, with what they take and make", async () => {
const { game } = await test_game("mods");
const pressing = game.recipes.all.filter((recipe) => recipe.type === PRESSING.id);
const sheet = pressing.find((recipe) => outputs(recipe)[0]?.ids[0] === "create:iron_sheet");
assert(sheet);
assert(inputs(sheet, tag_api())[0].ids.includes("bworld:iron_ingot"));
const ctx = {
recipes: { list: () => game.recipes.all },
tags: tag_api(),
items: {
exists: (id: string) => EverythingRegistry.get("items", id) !== undefined,
ids: () => EverythingRegistry.entries("items").map(([id]) => id),
components: () => ({}),
},
blocks: { exists: () => false, components: () => ({}) },
loot: { table: () => undefined },
} as unknown as ClientContext;
const index = new RecipeIndex(ctx);
const category = index.categories.find((category) => category.type === PRESSING.id);
assertEquals([category?.title, category?.catalysts], ["Pressing", ["create:mechanical_press"]]);
// juice is made by compacting too, by the press over a basin, and drawn as a fluid
const compacting = index.find({ kind: "fluid", id: "bworld:juice" }, "recipes")
.find((found) => found.category.type === COMPACTING.id)!;
assertEquals(compacting.category.catalysts, ["create:mechanical_press", "create:basin"]);
const fluids: unknown[] = [];
const ignore = () => {};
compacting.category.draw(compacting.recipes[0], {
slot: ignore,
texture: ignore,
text: ignore,
fluid: (fluid) => fluids.push(fluid),
});
assertEquals(fluids.length, 1);
});