1
0
Fork 0

test(npc-dialogue): cover inline images and choice tooltips

This commit is contained in:
Jason Fraley 2026-10-06 16:21:53 -04:00
parent 59fe616024
commit 43cf29c6f4

View file

@ -1,9 +1,12 @@
import { getPayload, Payload } from "payload";
import config from "@/payload.config";
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import type { DialogueNode } from "@/lib/npc-dialogue/dialogue";
import { DialogueText } from "@/lib/npc-dialogue/DialogueText";
import {
collectNodeTokens,
extractFlagTokens,
@ -159,3 +162,59 @@ describe("text flags (player-flags collection)", () => {
expect(formatFlagValue(updated)).toBe("{amber}REDACTED{/}");
});
});
describe("inline images in dialogue markup", () => {
const render = (text: string) =>
renderToStaticMarkup(createElement(DialogueText, { text }));
it("renders site-relative and absolute-http images as inline icons", () => {
const html = render("Pay 5 ![coin](/api/media/file/coin.png) now");
expect(html).toContain('<img src="/api/media/file/coin.png" alt="coin"');
expect(render("![net](https://example.com/a.png)")).toContain("<img");
});
it("leaves unsafe or malformed image URLs literal", () => {
// Protocol-relative and scheme-less/bad URLs never become <img>.
expect(render("![x](//evil.com/a.png)")).not.toContain("<img");
expect(render("![x](javascript:alert)")).not.toContain("<img");
expect(render("![x](ftp://host/a.png)")).not.toContain("<img");
});
it("keeps existing markup working alongside images", () => {
const html = render("**b** {green}g{/} ![c](/c.png) `code`");
expect(html).toContain("<strong");
expect(html).toContain("text-green-400");
expect(html).toContain("<img");
expect(html).toContain("<code");
});
});
describe("choice tooltip interpolation", () => {
it("collects tokens from tooltips and resolves them index-aligned", () => {
const node: DialogueNode = {
id: "n",
type: "choice",
position: { x: 0, y: 0 },
data: {
choices: [
{ label: "A", target: "t", tooltip: "Cost {{cost}} ![coin](/i.png)" },
{ label: "B", target: "u" },
],
},
};
expect(collectNodeTokens(node)).toContain("cost");
const resolved = resolveNodeTexts(node, { cost: "5" });
expect(resolved.tooltips?.[0]).toBe("Cost 5 ![coin](/i.png)");
expect(resolved.tooltips?.[1]).toBe("");
});
it("omits the tooltips array when no choice has a tooltip", () => {
const node: DialogueNode = {
id: "n",
type: "choice",
position: { x: 0, y: 0 },
data: { choices: [{ label: "A", target: "t" }] },
};
expect(resolveNodeTexts(node, {}).tooltips).toBeUndefined();
});
});