Files
bworld/tests/juice_test.ts
T

57 lines
2.2 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// juice: one fluid named and colored after the bworld:juiceable item in its data
import { assert, assertEquals } from "@std/assert";
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
import type { FluidJson } from "$/common/mod_data.ts";
import { display_name } from "$/common/utils.ts";
import { copy_dir, test_game } from "./helpers.ts";
// mods/bworld and another mod with a juiceable berry, so there's always one whatever bworld's items have
async function setup() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_juice_" });
copy_dir("mods/bworld", `${dir}/bworld`);
const write = (path: string, json: unknown) => {
Deno.mkdirSync(`${dir}/testberries/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
Deno.writeTextFileSync(`${dir}/testberries/${path}`, JSON.stringify(json));
};
write("manifest.json", {
format_version: 1,
id: "testberries",
name: "Test Berries",
version: "1.0.0",
dependencies: [{ id: "bworld", version: "*" }],
});
write("items/cloudberry.json", {
format_version: 1,
item: {
id: "testberries:cloudberry",
texture: "engine:missing",
components: { "bworld:juiceable": { juice: 100, color: "#e0a030" } },
},
});
const { game } = await test_game(dir);
Deno.removeSync(dir, { recursive: true });
return game.mods.fluid_rules.api();
}
Deno.test("juice made from any item with bworld:juiceable is named after it, in the component's color", async () => {
const fluids = await setup();
const juiceable = EverythingRegistry.entries<ItemRegistry>("items")
.filter(([, item]) => item.components?.["bworld:juiceable"]);
assert(juiceable.length > 0);
for (const [id, item] of juiceable) {
const juice = { id: "bworld:juice", data: { item: id } };
assertEquals(fluids.name(juice), `${display_name(id)} Juice`);
assertEquals(fluids.color(juice), (item.components!["bworld:juiceable"] as { color: string }).color);
}
// juice that isn't from an item is plain juice
const plain = EverythingRegistry.get<FluidJson>("fluids", "bworld:juice")!;
assertEquals([fluids.name({ id: "bworld:juice" }), fluids.color({ id: "bworld:juice" })], [
plain.name,
plain.color,
]);
});