Files
bworld/tests/script_recipes_test.ts
T
2026-11-13 00:01:59 -03:00

226 lines
8.9 KiB
TypeScript

// 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_mod } from "@/common/mod.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, load_as_player, test_game } from "./helpers.ts";
// sifts into what its component says, and a shaped recipe made in code. then sifting and washing as testrecipes' own
// types, found by the item put in
const SHARED = `
import type { SharedContext } from "cubi/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 } });
for (const type of ["testrecipes:sifting", "testrecipes:washing"]) {
ctx.recipes.register_type(type, {
parse: (json, read) => ({ input: read.item(json.input, "input"), result: read.stack(json.result, "result") }),
matches: (recipe, input) => recipe.input === input,
});
}
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_${side}_parsed = {
all: ctx.recipes.all("testrecipes:sifting"),
sand: ctx.recipes.find("testrecipes:sifting", "testrecipes:sand"),
};
(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<string, unknown> = {}) {
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: "cubi: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 },
}];
const seen = globalThis as unknown as Record<string, unknown>;
// packs testrecipes from dir, which it then removes, and loads it on a client like one joining would
async function load_client(dir: string) {
const mod = (await load_and_check(dir)).find((m) => m.id === "testrecipes")!;
assertEquals(mod.report.errors, []);
const packed = read_mod(await pack_mod(mod), "testrecipes.cubi");
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], [packed]);
}
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);
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),
);
await load_client(dir);
assertEquals(seen.testrecipes_client, SIFTING);
});
// a sifting recipe registered after the type
const SAND_AFTER =
`ctx.recipes.register("testrecipes:sifting", { input: "testrecipes:sand", result: { id: "testrecipes:gravel", count: 1 } });`;
const SAND_FILE = {
"sand.json": {
type: "testrecipes:sifting",
input: "testrecipes:sand",
result: { id: "testrecipes:flint", count: 1 },
},
};
Deno.test("a recipe type parses its recipes, and find gives the first one an input makes, on every side", async () => {
const dir = recipe_mod(SAND_AFTER, SAND_FILE);
await test_game(dir);
await load_client(dir);
for (const side of ["server", "client"]) {
const parsed = seen[`testrecipes_${side}_parsed`] as { all: { input: string }[]; sand: unknown };
// the file's, then the ones registered before the type and after it
assertEquals(parsed.all.map((recipe) => recipe.input), [
"testrecipes:sand",
"testrecipes:gravel",
"testrecipes:sand",
]);
assertEquals(parsed.sand, { input: "testrecipes:sand", result: { id: "testrecipes:flint", count: 1 } });
}
});
Deno.test("a recipe its type can't parse stops the mod loading, naming it", async () => {
const dir = recipe_mod("", {
"sand.json": {
type: "testrecipes:sifting",
input: "testrecipes:nothing",
result: { id: "testrecipes:flint", count: 1 },
},
});
await assertRejects(
() => test_game(dir),
Error,
"testrecipes:sifting recipe 1: input: there's no item testrecipes:nothing",
);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a recipe of a type no mod registers stops its mod loading, like a misspelled one", async () => {
const dir = recipe_mod("", {
"sand.json": {
type: "testrecipes:sieving",
input: "testrecipes:sand",
result: { id: "testrecipes:flint", count: 1 },
},
});
await assertRejects(
() => test_game(dir),
Error,
"testrecipes: it has testrecipes:sieving recipes, but no mod registers that type",
);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a mod can only register recipe types in its own namespace", async () => {
const dir = recipe_mod(
`ctx.recipes.register_type("bworld:sifting", { parse: (json) => json, matches: () => true });`,
);
await assertRejects(
() => test_game(dir),
Error,
'recipe type bworld:sifting must be in the namespace "testrecipes"',
);
Deno.removeSync(dir, { recursive: true });
});
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<string, unknown>).testrecipes_late;
Deno.removeSync(dir, { recursive: true });
}
});
Deno.test("every mod's recipe types are registered on a client too, so it loads them", async () => {
await load_as_player("mods");
});