// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // recipes a shared script registers with ctx.recipes.register: there on the server and every client alike, for their // scripts and the crafting grid, without being sent import { assert, assertEquals, assertRejects } from "@std/assert"; import { EverythingRegistry } from "$/common/everything_registry.ts"; import type { ModListing } from "$/common/mod_loader.ts"; import { read_bmod } from "$/common/bmod.ts"; import { load_client_mods } from "$/client/mods.ts"; import { load_and_check } from "$/tools/check_mods.ts"; import { pack_mod } from "$/tools/pack_mod.ts"; import { copy_dir, test_game } from "./helpers.ts"; // sifts into what its component says, and a shaped recipe made in code const SHARED = ` import type { SharedContext } from "bworld/shared"; export function setup(ctx: SharedContext) { for (const id of ctx.items.ids()) { const sifts = ctx.items.components(id)["testrecipes:siftable"] as { into: string } | undefined; if (sifts) ctx.recipes.register("testrecipes:sifting", { input: id, result: { id: sifts.into, count: 1 } }); } ctx.recipes.register("shaped", { pattern: ["#"], key: { "#": "testrecipes:gravel" }, result: { id: "testrecipes:flint", count: 2 } }); SHARED_EXTRA } `; // what the server and client scripts see of them while they set up. the server's registers the siftable component const SEEN = (side: string) => ` export function setup(ctx: any) { ctx.components?.register_item?.("testrecipes:siftable", {}); (globalThis as any).testrecipes_${side} = ctx.recipes.list("testrecipes:sifting"); (globalThis as any).testrecipes_late?.(); } `; // the base game, and testrecipes: gravel that sifts into flint, sand that doesn't sift, and the scripts above // and recipes/ files by name function recipe_mod(shared_extra = "", recipes: Record = {}) { const dir = Deno.makeTempDirSync({ prefix: "bworld_script_recipes_" }); copy_dir("mods/bworld", `${dir}/bworld`); const write = (path: string, json: unknown) => { Deno.mkdirSync(`${dir}/testrecipes/${path.split("/").slice(0, -1).join("/")}`, { recursive: true }); Deno.writeTextFileSync(`${dir}/testrecipes/${path}`, typeof json === "string" ? json : JSON.stringify(json)); }; write("manifest.json", { format_version: 1, id: "testrecipes", name: "Test Recipes", version: "1.0.0", scripts: { shared: "scripts/shared.ts", server: "scripts/server.ts", client: "scripts/client.ts" }, }); const item = (id: string, components?: unknown) => write(`items/${id}.json`, { format_version: 1, item: { id: `testrecipes:${id}`, texture: "engine:missing", ...(components ? { components } : {}) }, }); item("gravel", { "testrecipes:siftable": { into: "testrecipes:flint" } }); item("sand"); item("flint"); write("scripts/shared.ts", SHARED.replace("SHARED_EXTRA", shared_extra)); write("scripts/server.ts", SEEN("server")); write("scripts/client.ts", SEEN("client")); for (const [name, recipe] of Object.entries(recipes)) write(`recipes/${name}`, { format_version: 1, recipe }); return dir; } const SIFTING = [{ type: "testrecipes:sifting", input: "testrecipes:gravel", result: { id: "testrecipes:flint", count: 1 }, }]; Deno.test("recipes a shared script registers are on the server and clients, for their scripts and crafting", async () => { const dir = recipe_mod(); const t = await test_game(dir); const seen = globalThis as unknown as Record; assertEquals(seen.testrecipes_server, SIFTING); assertEquals(t.game.recipes.by_type.get("testrecipes:sifting") as unknown, SIFTING); assert( t.game.recipes.shaped.some((recipe) => recipe.result.id === "testrecipes:flint" && recipe.result.count === 2), ); const mod = load_and_check(dir).find((m) => m.id === "testrecipes")!; assertEquals(mod.report.errors, []); const bmod = read_bmod(await pack_mod(mod), "testrecipes.bmod"); Deno.removeSync(dir, { recursive: true }); const listing: ModListing = { id: "testrecipes", name: "Test", version: "1.0.0", sha256: "", file: "", size: 0 }; EverythingRegistry.clear(); await load_client_mods([listing], [bmod]); assertEquals(seen.testrecipes_client, SIFTING); }); Deno.test("a shared script lists the recipes there are so far, from files and the scripts before it", async () => { // a washing recipe for every sifting one, whatever made it const dir = recipe_mod( `for (const sifting of ctx.recipes.list("testrecipes:sifting")) { ctx.recipes.register("testrecipes:washing", { input: sifting.result.id, result: { id: sifting.input, count: 1 } }); }`, { "sand.json": { type: "testrecipes:sifting", input: "testrecipes:sand", result: { id: "testrecipes:flint", count: 1 }, }, }, ); const t = await test_game(dir); Deno.removeSync(dir, { recursive: true }); const washing = t.game.recipes.by_type.get("testrecipes:washing")!.map((recipe) => recipe.result); assertEquals(washing as unknown, [{ id: "testrecipes:sand", count: 1 }, { id: "testrecipes:gravel", count: 1 }]); }); Deno.test("a broken recipe from a shared script stops the mod loading, like a broken file", async () => { const dir = recipe_mod( `ctx.recipes.register("shaped", { pattern: ["#"], key: {}, result: { id: "testrecipes:sand", count: 1 } });`, ); await assertRejects(() => test_game(dir), Error, "testrecipes: recipe of type shaped"); Deno.removeSync(dir, { recursive: true }); }); Deno.test("recipes can only be registered while shared scripts set up", async () => { const dir = recipe_mod( `(globalThis as any).testrecipes_late = () => ctx.recipes.register("testrecipes:sifting", {});`, ); try { await assertRejects(() => test_game(dir), Error, "recipes can only be registered during setup"); } finally { delete (globalThis as Record).testrecipes_late; Deno.removeSync(dir, { recursive: true }); } });