// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later import { assert, assertEquals } from "@std/assert"; import { EverythingRegistry } from "@/common/everything_registry.ts"; import type { FluidJson } from "@/common/mod_data.ts"; import type { ModListing } from "@/common/mod_loader.ts"; import type { FluidApi } from "@/common/mod_api/shared.ts"; import { read_mod } from "@/common/mod.ts"; import { register_mod_data } from "@/common/mod_loader.ts"; import { load_client_mods } from "@/client/mods.ts"; import { fluid_rules } from "@/client/fluid_rules.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"; // testfluids: a manifest, the given fluids/ files by file name, and maybe a shared phase and an item for it to read function fluid_mod(dir: string, fluids: Record, shared_script?: string) { const mod = `${dir}/testfluids`; const write = (path: string, json: unknown) => { Deno.mkdirSync(`${mod}/${path.split("/").slice(0, -1).join("/")}`, { recursive: true }); Deno.writeTextFileSync(`${mod}/${path}`, typeof json === "string" ? json : JSON.stringify(json)); }; write("manifest.json", { format_version: 1, id: "testfluids", name: "Test Fluids", version: "1.0.0", ...(shared_script && { main: "scripts/shared.ts" }), }); for (const [file, fluid] of Object.entries(fluids)) write(`fluids/${file}`, { format_version: 1, fluid }); if (shared_script) { write("scripts/shared.ts", shared_script); write("items/maple.json", { format_version: 1, item: { id: "testfluids:maple", texture: "cubi:missing", name: "Sugar Maple" }, }); } } Deno.test("a mod's fluids/ file registers a fluid, packed into its .cubi", async () => { const dir = Deno.makeTempDirSync({ prefix: "bworld_fluids_" }); const syrup = { id: "testfluids:syrup", name: "Maple Syrup", color: "#a0522d", texture: "testfluids:syrup_still" }; fluid_mod(dir, { "syrup.json": syrup }); const mods = await load_and_check(dir); assertEquals(mods[0].report.errors, []); // it has no textures assertEquals(mods[0].report.warnings, [ "fluids/syrup.json: texture testfluids:syrup_still doesn't exist, it will show as missing", ]); const mod = read_mod(await pack_mod(mods[0]), "testfluids.cubi"); EverythingRegistry.clear(); register_mod_data([{ id: "testfluids", data: mod.data }]); assertEquals(EverythingRegistry.get("fluids", "testfluids:syrup"), syrup); Deno.removeSync(dir, { recursive: true }); }); Deno.test("a fluid with a bad color fails loading", async () => { const dir = Deno.makeTempDirSync({ prefix: "bworld_fluids_" }); fluid_mod(dir, { "syrup.json": { id: "testfluids:syrup", color: "brown" } }); const errors = (await load_and_check(dir))[0].report.errors; assert(errors.some((e) => e.startsWith("fluids/syrup.json:") && e.includes("color")), errors.join("\n")); Deno.removeSync(dir, { recursive: true }); }); // syrup is named after the tree in its data and has the color its data says, sap has no rules const RULES = ` import type { SharedContext } from "cubi/shared"; export function shared(ctx: SharedContext) { ctx.fluids.register("testfluids:syrup", { name: (data: any) => data?.tree && ctx.items.name(data.tree) + " Syrup", color: (data: any) => data?.color, }); } `; const FLUIDS = { "syrup.json": { id: "testfluids:syrup", name: "Syrup", color: "#a0522d" }, "sap.json": { id: "testfluids:tree_sap", color: "#e8d9a0" }, }; // what the server's and a client's ctx.fluids say about the fluids above async function both_sides(ask: (fluids: FluidApi) => unknown) { const dir = Deno.makeTempDirSync({ prefix: "bworld_fluids_" }); copy_dir("mods/bworld", `${dir}/bworld`); fluid_mod(dir, FLUIDS, RULES); const { game } = await test_game(dir); const server = ask(game.mods.fluid_rules.api()); const mod = (await load_and_check(dir)).find((m) => m.id === "testfluids")!; const packed = read_mod(await pack_mod(mod), "testfluids.cubi"); Deno.removeSync(dir, { recursive: true }); const listing: ModListing = { id: "testfluids", name: "Test", version: "1.0.0", sha256: "", file: "", size: 0 }; EverythingRegistry.clear(); await load_client_mods([listing], [packed]); return { server, client: ask(fluid_rules.api()) }; } Deno.test("a fluid's rules name and color it by its data, the same on the server and clients", async () => { const { server, client } = await both_sides((fluids) => [ [ fluids.name({ id: "testfluids:syrup", data: { tree: "testfluids:maple" } }), fluids.color({ id: "testfluids:syrup", data: { color: "#c87533" }, }), ], // the rules say nothing, or something that isn't a color, so the json's are used [fluids.name({ id: "testfluids:syrup" }), fluids.color({ id: "testfluids:syrup", data: { color: "brown" } })], // no rules, and no name in its json [ fluids.name({ id: "testfluids:tree_sap" }), fluids.color({ id: "testfluids:tree_sap", data: { color: "#000000" } }), ], ]); assertEquals(server, [["Sugar Maple Syrup", "#c87533"], ["Syrup", "#a0522d"], ["Tree Sap", "#e8d9a0"]]); assertEquals(client, server); }); Deno.test("fluids are the same when their ids and data are, whatever order the data's keys are in", async () => { const { server, client } = await both_sides((fluids) => { const syrup = (data?: unknown) => ({ id: "testfluids:syrup", data }); return [ fluids.same( syrup({ tree: "testfluids:maple", grade: "a" }), syrup({ grade: "a", tree: "testfluids:maple" }), ), fluids.same(syrup({}), syrup()), fluids.same(syrup({ tree: "testfluids:maple" }), syrup({ tree: "testfluids:birch" })), fluids.same(syrup({ tree: "testfluids:maple" }), syrup()), fluids.same(syrup(), { id: "testfluids:tree_sap" }), ]; }); assertEquals(server, [true, true, false, false, false]); assertEquals(client, server); }); Deno.test("every fluid's id is listed once, without data, on the server and clients", async () => { const sides = await both_sides((fluids) => fluids.ids()); for (const ids of [sides.server, sides.client] as string[][]) { assert(ids.includes("testfluids:syrup") && ids.includes("testfluids:tree_sap"), ids.join()); assertEquals(new Set(ids).size, ids.length); } });