Files
bworld/tests/tooltip_test.ts
T
2026-11-14 12:18:41 -03:00

78 lines
2.8 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// item tooltips: the game's lines, the ones client phases add and where they go
import { assertEquals } from "@std/assert";
import { item_tooltip } from "@/client/gui/tooltip.ts";
import { ItemStack } from "@/common/inventory.ts";
import { copy_dir, load_as_player } from "./helpers.ts";
const SNACK = "tips:snack";
// mods/bworld, then tips: a snack with food and lore that sells for 4, and a client phase adding lines to its tooltip
async function load(client_script: string) {
const dir = Deno.makeTempDirSync({ prefix: "bworld_tooltips_" });
copy_dir("mods/bworld", `${dir}/bworld`);
const write = (path: string, text: string) => {
Deno.mkdirSync(`${dir}/tips/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
Deno.writeTextFileSync(`${dir}/tips/${path}`, text);
};
write(
"manifest.json",
JSON.stringify({
format_version: 1,
id: "tips",
name: "Tips",
version: "1.0.0",
main: "scripts/client.ts",
}),
);
write(
"items/snack.json",
JSON.stringify({
format_version: 1,
item: {
id: SNACK,
texture: "cubi:missing",
food: { nutrition: 3, saturation_modifier: 0.5 },
lore: "Crunchy.",
base_sell_value: 4,
},
}),
);
write("scripts/client.ts", client_script);
try {
await load_as_player(dir);
} finally {
Deno.removeSync(dir, { recursive: true });
}
return (id: string) => item_tooltip(new ItemStack(id)).map((line) => line.text);
}
Deno.test("a mod's client phase's lines show in item tooltips, and bworld's sell value goes after the food", async () => {
const tooltip = await load(`
import type { ClientContext } from "cubi/client";
export function client(ctx: ClientContext) {
ctx.ui.add_item_tooltip((item, lines) => lines.push({ text: "from tips: " + item.id }));
}
`);
assertEquals(tooltip(SNACK), ["Snack", "Restores 3 hunger", "4", "from tips: tips:snack", "Crunchy."]);
assertEquals(tooltip("bworld:stick").at(-1), "from tips: bworld:stick");
});
Deno.test("tooltip lines go where their priority says, and ones with the same priority keep their order", async () => {
const tooltip = await load(`
import { type ClientContext, TOOLTIP_PRIORITY } from "cubi/client";
export function client(ctx: ClientContext) {
ctx.ui.add_item_tooltip((item, lines) => {
if (item.id !== "${SNACK}") return;
lines.push({ text: "last", priority: TOOLTIP_PRIORITY.lore + 1 });
lines.push({ text: "first", priority: TOOLTIP_PRIORITY.food });
lines.push({ text: "second", priority: TOOLTIP_PRIORITY.food });
});
}
`);
// after the game's own food line, then bworld's sell value at food + 1
assertEquals(tooltip(SNACK), ["Snack", "Restores 3 hunger", "first", "second", "4", "Crunchy.", "last"]);
});