// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // mods' config.json: their settings, which servers and tests can change, see docs/modding/misc/config.md import { assert, assertEquals, assertRejects } from "@std/assert"; import { read_mod, write_mod } from "@/common/mod.ts"; import type { ModData } from "@/common/mod_loader.ts"; import { apply_config, load_mods } from "@/server/load_mods.ts"; import { load_and_check } from "@/tools/check_mods.ts"; import { create_mod } from "@/tools/new_mod.ts"; import { copy_dir, test_game } from "./helpers.ts"; const config_file = (config: unknown) => JSON.stringify({ format_version: 1, config }); Deno.test("changing a mod's config only changes settings it has, to the same kind of value", () => { const config = { max_speed: 256, stress: { enabled: true, impact: { press: 8, mixer: 4 } }, fuels: ["coal"] }; const changed = apply_config(config, { max_speed: 64, stress: { impact: { press: 2 } }, fuels: [] }); assertEquals(changed.problems, []); assertEquals(changed.config, { max_speed: 64, stress: { enabled: true, impact: { press: 2, mixer: 4 } }, fuels: [], }); assertEquals(config.stress.impact.press, 8, "the mod's own settings stay as they were"); const wrong = apply_config(config, { max_sped: 64, stress: { enabled: "no", impact: 8 }, fuels: "coal" }); assertEquals(wrong.problems.map((problem) => problem.split(" ")[0]), [ "max_sped", "stress.enabled", "stress.impact", "fuels", ]); }); Deno.test("a mod's server phase reads its mod's config, with the changes a test makes", async () => { const dir = Deno.makeTempDirSync({ prefix: "bworld_config_" }); copy_dir("mods/bworld", `${dir}/bworld`); create_mod("copper_tools", "Copper Tools", dir); Deno.writeTextFileSync( `${dir}/copper_tools/scripts/main.ts`, `import type { ServerContext } from "cubi/server"; export function server(ctx: ServerContext) { ctx.components.register_block("copper_tools:announce", {}); ctx.commands.register("settings", { description: "Shows the settings", usage: "/settings", run: (_args, player) => player.send_message(JSON.stringify(ctx.config)), }); } `, ); // what the script sees const config_of = async (changes?: Record>) => { const { join, send, take } = await test_game(dir, undefined, undefined, changes); join(1, "bob"); send(1, { type: "chat", text: "/settings" }); return JSON.parse(take(1).find((m) => m.type === "chat" && m.text.startsWith("{")).text); }; assertEquals(await config_of(), {}, "a mod without config.json has no settings"); Deno.writeTextFileSync(`${dir}/copper_tools/config.json`, config_file({ speed: 16, ore: { size: 4, depth: 30 } })); assertEquals(await config_of(), { speed: 16, ore: { size: 4, depth: 30 } }); assertEquals(await config_of({ copper_tools: { ore: { size: 8 } } }), { speed: 16, ore: { size: 8, depth: 30 } }); await assertRejects(() => config_of({ copper_tools: { sped: 1 } }), Error, "sped"); Deno.writeTextFileSync(`${dir}/copper_tools/config.json`, config_file([16])); const errors = (await load_and_check(dir)).find((mod) => mod.id === "copper_tools")!.report.errors; assert(errors.some((error) => error.startsWith("config.json")), errors.join("\n")); Deno.removeSync(dir, { recursive: true }); }); Deno.test("a server changes a mod's settings with config/.json, and players never get them", async () => { const dir = Deno.makeTempDirSync({ prefix: "bworld_config_server_" }); const data: ModData = { blocks: [], models: [], items: [], recipes: [], ores: [], spawns: [], entities: [], entity_models: [], effects: [], fluids: [], tags: [], config: { speed: 16, ore: { size: 4, depth: 30 } }, }; Deno.mkdirSync(`${dir}/server_mods`); Deno.mkdirSync(`${dir}/config`); Deno.writeFileSync( `${dir}/server_mods/tiny.cubi`, write_mod({ manifest: { format_version: 1, id: "tiny", name: "Tiny", version: "1.0.0" }, data, textures: new Map(), }), ); const config_of = async () => (await load_mods(`${dir}/server_mods`, [], `${dir}/config`))[0].mod.data.config; assertEquals(await config_of(), data.config, "without a file, the mod's own"); Deno.writeTextFileSync(`${dir}/config/tiny.json`, config_file({ ore: { depth: 50 } })); assertEquals(await config_of(), { speed: 16, ore: { size: 4, depth: 50 } }); const [mod] = await load_mods(`${dir}/server_mods`, [], `${dir}/config`); assertEquals(read_mod(mod.client_bytes, mod.listing.file).data.config, undefined); Deno.writeTextFileSync(`${dir}/config/tiny.json`, config_file({ ore: { depht: 50 } })); await assertRejects(config_of, Error, "ore.depht"); Deno.writeTextFileSync(`${dir}/config/tiny.json`, JSON.stringify({ ore: { depth: 50 } })); await assertRejects(config_of, Error, "tiny.json"); Deno.removeSync(dir, { recursive: true }); });