diff --git a/tests/int/npc-dialogue-interpolate.int.spec.ts b/tests/int/npc-dialogue-interpolate.int.spec.ts new file mode 100644 index 00000000..ff5942a7 --- /dev/null +++ b/tests/int/npc-dialogue-interpolate.int.spec.ts @@ -0,0 +1,161 @@ +import { getPayload, Payload } from "payload"; +import config from "@/payload.config"; + +import { afterAll, beforeAll, describe, expect, it } from "vitest"; + +import type { DialogueNode } from "@/lib/npc-dialogue/dialogue"; +import { + collectNodeTokens, + extractFlagTokens, + formatFlagValue, + hasFlagTokens, + interpolateFlags, + resolveNodeTexts, +} from "@/lib/npc-dialogue/interpolate"; + +let payload: Payload; + +const RUN = `dlg_${Date.now().toString(36)}`; +const TIMEOUT = 30_000; + +describe("flag token extraction + interpolation (pure)", () => { + it("extracts distinct lowercased keys and ignores single-brace markup", () => { + expect(extractFlagTokens("Status: {{network_status}}. {{NETWORK_STATUS}} again")).toEqual([ + "network_status", + ]); + expect(extractFlagTokens("{green}no tokens here{/} **bold**")).toEqual([]); + expect(extractFlagTokens(undefined)).toEqual([]); + expect(hasFlagTokens("a {{x}} b")).toBe(true); + expect(hasFlagTokens("{red}x{/}")).toBe(false); + }); + + it("substitutes values, resolves missing keys to empty, keeps markup intact", () => { + const out = interpolateFlags("Link {{network_status}}, rep {{rep_merchants}}", { + network_status: "{green}NOMINAL{/}", + }); + expect(out).toBe("Link {green}NOMINAL{/}, rep "); + }); + + it("is case-insensitive on keys and never re-scans substituted values", () => { + const out = interpolateFlags("{{Key}} says {{key}}", { + key: "{{key}}", + }); + // Single pass: the injected token must survive literally, not expand. + expect(out).toBe("{{key}} says {{key}}"); + }); + + it("formats flag values per type for interpolation", () => { + expect(formatFlagValue({ flagType: "text", textValue: "{green}OK{/}" })).toBe("{green}OK{/}"); + expect(formatFlagValue({ flagType: "text" })).toBe(""); + expect(formatFlagValue({ flagType: "number", numberValue: 7 })).toBe("7"); + expect(formatFlagValue({ flagType: "number" })).toBe("0"); + expect(formatFlagValue({ flagType: "boolean", boolValue: true })).toBe("true"); + expect(formatFlagValue({ flagType: "boolean", boolValue: false })).toBe("false"); + }); + + it("collects node tokens across body, lead-in, closing, effect message, labels", () => { + const node: DialogueNode = { + id: "n1", + type: "choice", + position: { x: 0, y: 0 }, + data: { + leadIn: "Ops report: {{ops_status}}", + choices: [ + { label: "Ack ({{clearance_level}})", target: "a" }, + { label: "No tokens", target: "b" }, + ], + }, + }; + expect(new Set(collectNodeTokens(node))).toEqual(new Set(["ops_status", "clearance_level"])); + }); + + it("resolves only the fields a node defines, index-aligning labels", () => { + const node: DialogueNode = { + id: "n1", + type: "dialogue", + position: { x: 0, y: 0 }, + data: { body: "Status {{network_status}}", next: { target: "x" } }, + }; + const resolved = resolveNodeTexts(node, { network_status: "{red}DOWN{/}" }); + expect(resolved).toEqual({ body: "Status {red}DOWN{/}" }); + + const choice: DialogueNode = { + id: "n2", + type: "choice", + position: { x: 0, y: 0 }, + data: { + leadIn: "{{greeting}}", + choices: [ + { label: "{{a}}", target: "t" }, + { label: "plain", target: "u" }, + ], + }, + }; + const r2 = resolveNodeTexts(choice, { greeting: "Hi", a: "1" }); + expect(r2.leadIn).toBe("Hi"); + expect(r2.labels).toEqual(["1", "plain"]); + expect(r2.body).toBeUndefined(); + }); +}); + +describe("text flags (player-flags collection)", () => { + beforeAll(async () => { + payload = await getPayload({ config }); + }, TIMEOUT); + + afterAll(async () => { + const res = await payload.find({ + collection: "player-flags", + where: { key: { like: `${RUN}%` } }, + limit: 50, + overrideAccess: true, + }); + for (const doc of res.docs) { + await payload.delete({ collection: "player-flags", id: doc.id, overrideAccess: true }); + } + }); + + it("stores and reads a global text flag with markup", async () => { + const created = await payload.create({ + collection: "player-flags", + data: { + user: null, + key: `${RUN}_network_status`, + flagType: "text", + textValue: "{green}NOMINAL{/}", + }, + overrideAccess: true, + }); + expect(created.flagType).toBe("text"); + expect(created.textValue).toBe("{green}NOMINAL{/}"); + + const fetched = await payload.findByID({ + collection: "player-flags", + id: created.id, + overrideAccess: true, + }); + expect(fetched.textValue).toBe("{green}NOMINAL{/}"); + + // Interpolates into dialogue text exactly as authored. + const values = { [`${RUN}_network_status`]: formatFlagValue(fetched) }; + const line = interpolateFlags(`Grid says {{${RUN}_network_status}}`, values); + expect(line).toBe(`Grid says {green}NOMINAL{/}`); + }); + + it("converts an existing boolean flag to text via update (set_text semantics)", async () => { + const created = await payload.create({ + collection: "player-flags", + data: { user: null, key: `${RUN}_converted`, flagType: "boolean", boolValue: true }, + overrideAccess: true, + }); + const updated = await payload.update({ + collection: "player-flags", + id: created.id, + data: { flagType: "text", textValue: "{amber}REDACTED{/}" }, + overrideAccess: true, + }); + expect(updated.flagType).toBe("text"); + expect(updated.textValue).toBe("{amber}REDACTED{/}"); + expect(formatFlagValue(updated)).toBe("{amber}REDACTED{/}"); + }); +});