92 lines
3.4 KiB
TypeScript
92 lines
3.4 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// item tooltips: the game's lines, the ones client scripts add and where they go
|
|
import { assertEquals } from "@std/assert";
|
|
import { item_tooltip } from "@/client/gui/tooltip.ts";
|
|
import { load_client_mods } from "@/client/mods.ts";
|
|
import { EverythingRegistry } from "@/common/everything_registry.ts";
|
|
import { ItemStack } from "@/common/inventory.ts";
|
|
import type { ModListing } from "@/common/mod_loader.ts";
|
|
import { read_mod } from "@/common/mod.ts";
|
|
import { load_and_check, load_order } from "@/tools/check_mods.ts";
|
|
import { pack_mod } from "@/tools/pack_mod.ts";
|
|
import { copy_dir } from "./helpers.ts";
|
|
|
|
const SNACK = "tips:snack";
|
|
|
|
// mods/bworld, then tips: a snack with food and lore that sells for 4, and a client script 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",
|
|
scripts: { client: "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);
|
|
const mods = load_order(await load_and_check(dir));
|
|
const packed = await Promise.all(mods.map(async (mod) => read_mod(await pack_mod(mod), `${mod.id}.cubi`)));
|
|
Deno.removeSync(dir, { recursive: true });
|
|
const listings: ModListing[] = mods.map((mod) => ({
|
|
id: mod.id,
|
|
name: mod.id,
|
|
version: "1.0.0",
|
|
sha256: "",
|
|
file: "",
|
|
size: 0,
|
|
}));
|
|
EverythingRegistry.clear();
|
|
await load_client_mods(listings, packed);
|
|
return (id: string) => item_tooltip(new ItemStack(id)).map((line) => line.text);
|
|
}
|
|
|
|
Deno.test("a client script'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 setup(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 setup(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"]);
|
|
});
|