test(npc-dialogue): cover inline images and choice tooltips
This commit is contained in:
parent
59fe616024
commit
43cf29c6f4
1 changed files with 59 additions and 0 deletions
|
|
@ -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  now");
|
||||
expect(html).toContain('<img src="/api/media/file/coin.png" alt="coin"');
|
||||
expect(render("")).toContain("<img");
|
||||
});
|
||||
|
||||
it("leaves unsafe or malformed image URLs literal", () => {
|
||||
// Protocol-relative and scheme-less/bad URLs never become <img>.
|
||||
expect(render("")).not.toContain("<img");
|
||||
expect(render("")).not.toContain("<img");
|
||||
expect(render("")).not.toContain("<img");
|
||||
});
|
||||
|
||||
it("keeps existing markup working alongside images", () => {
|
||||
const html = render("**b** {green}g{/}  `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}} " },
|
||||
{ label: "B", target: "u" },
|
||||
],
|
||||
},
|
||||
};
|
||||
expect(collectNodeTokens(node)).toContain("cost");
|
||||
const resolved = resolveNodeTexts(node, { cost: "5" });
|
||||
expect(resolved.tooltips?.[0]).toBe("Cost 5 ");
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue