Files
bworld/tests/hud_test.ts
T
2026-11-14 13:49:21 -03:00

92 lines
3.3 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// mods' hud layers, see ctx.hud and client/gui/hud.ts
import { assertEquals, assertRejects } from "@std/assert";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import type { ModListing } from "@/common/mod_loader.ts";
import { read_mod } from "@/common/mod.ts";
import type { Graphics, HudSpace } from "@/common/mod_api/client.ts";
import { hud_layers, load_client_mods, receive_overlay_message, render_hud_layers } from "@/client/mods.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
async function load(script: string) {
const dir = Deno.makeTempDirSync({ prefix: "bworld_hud_" });
try {
Deno.mkdirSync(`${dir}/testmod/scripts`, { recursive: true });
Deno.writeTextFileSync(
`${dir}/testmod/manifest.json`,
JSON.stringify({
format_version: 1,
id: "testmod",
name: "Test",
version: "1.0.0",
main: "scripts/client.ts",
}),
);
Deno.writeTextFileSync(`${dir}/testmod/scripts/client.ts`, script);
const mod = (await load_and_check(dir))[0];
const packed = read_mod(await pack_mod(mod), "testmod.cubi");
const listing: ModListing = { id: "testmod", name: "Test", version: "1.0.0", sha256: "", file: "", size: 0 };
EverythingRegistry.clear();
await load_client_mods([listing], [packed]);
} finally {
Deno.removeSync(dir, { recursive: true });
}
}
Deno.test("hud layers draw in order on the same space, and one that throws is removed", async () => {
await load(`
import type { ClientContext } from "cubi/client";
export function client(ctx: ClientContext) {
ctx.hud.register("testmod:bar", { on_render(_g, hud) { hud.left_height += 10; } });
ctx.hud.register("testmod:broken", { on_render() { throw new Error("broken"); } });
const above = {
seen: [] as number[],
overlay: "",
on_render(_g, hud) { this.seen.push(hud.left_height); },
};
ctx.hud.register("testmod:above", above);
ctx.hud.on_overlay_message((text) => above.overlay = text);
}
`);
assertEquals(hud_layers.map((entry) => entry.id), ["testmod:bar", "testmod:broken", "testmod:above"]);
const above = hud_layers[2].layer as unknown as { seen: number[]; overlay: string };
const error = console.error;
console.error = () => {};
try {
for (let frame = 0; frame < 2; frame++) {
const space: HudSpace = { left_height: 0, right_height: 0 };
render_hud_layers({} as Graphics, space);
assertEquals(space, { left_height: 10, right_height: 0 });
}
} finally {
console.error = error;
}
assertEquals(above.seen, [10, 10]);
assertEquals(hud_layers.map((entry) => entry.id), ["testmod:bar", "testmod:above"]);
receive_overlay_message("Too dark to sleep");
assertEquals(above.overlay, "Too dark to sleep");
});
Deno.test("hud layers are in the mod's namespace, one to an id", async () => {
const layer = `{ on_render() {} }`;
await assertRejects(
() => load(`export function client(ctx) { ctx.hud.register("bworld:hotbar", ${layer}); }`),
Error,
`must be in the namespace "testmod"`,
);
await assertRejects(
() =>
load(`export function client(ctx) {
ctx.hud.register("testmod:bar", ${layer});
ctx.hud.register("testmod:bar", ${layer});
}`),
Error,
"hud layer testmod:bar is registered twice",
);
});