// @vitest-environment node import { describe, expect, it } from "vitest"; import type { DialogueNode } from "@/lib/npc-dialogue/dialogue"; import { automaticTarget, describeCondition, isTerminal, nodeForTarget, previewChoices, previewEffectLines, previewStep, } from "@/lib/npc-dialogue/preview"; /** * The editor's draft preview walks an unsaved graph, so its reading of the * author's nodes has to match the runtime's: same choice order, same branch for * a condition, and effects described rather than applied. */ function node(partial: Partial & { id: string; type: DialogueNode["type"] }): DialogueNode { return { position: { x: 0, y: 0 }, data: {}, ...partial, } as DialogueNode; } describe("draft preview reading the graph", () => { it("lists choices in runtime order with their gate text", () => { const choice = node({ id: "c1", type: "choice", data: { leadIn: "Pick one.", choices: [ { label: "First", target: "a" }, { label: "", target: "b" }, { label: "Third", target: "c", condition: { kind: "flag", key: "rank", op: "gt", amount: 2 } }, ], }, }); const choices = previewChoices(choice); expect(choices.map((c) => c.index)).toEqual([0, 1, 2]); expect(choices[1]?.label).toBe("(unlabeled choice)"); expect(choices[2]?.conditionNote).toBe("rank > 2"); }); it("describes condition groups in plain language", () => { expect( describeCondition({ kind: "group", op: "and", children: [ { kind: "flag", key: "a", op: "is_true" }, { kind: "flag", key: "b", op: "is_undefined" }, ], }), ).toBe("a is true and b is undefined"); expect(describeCondition(undefined)).toBeNull(); }); it("sends a condition node down the branch its answer picks", () => { const condition = node({ id: "k1", type: "condition", data: { condition: { kind: "flag", key: "a", op: "is_true" }, trueTarget: "yes", falseTarget: "no" }, }); expect(automaticTarget(condition, null)).toEqual({ target: null, waitingOnGate: true }); expect(automaticTarget(condition, true)).toEqual({ target: "yes", waitingOnGate: false }); expect(automaticTarget(condition, false)).toEqual({ target: "no", waitingOnGate: false }); }); it("keeps the single continuation for linear nodes", () => { const dialogue = node({ id: "d1", type: "dialogue", data: { body: "Hi", next: { target: "d2" } } }); expect(automaticTarget(dialogue, null).target).toBe("d2"); expect(automaticTarget(node({ id: "e1", type: "end", data: {} }), null).target).toBeNull(); }); it("only treats end nodes as terminal", () => { expect(isTerminal(node({ id: "e1", type: "end", data: {} }))).toBe(true); expect(isTerminal(node({ id: "d1", type: "dialogue", data: {} }))).toBe(false); expect(isTerminal(node({ id: "y1", type: "delay", data: {} }))).toBe(false); }); it("describes flag effects by their actual write, never as applied", () => { expect(previewEffectLines({ type: "flag", flagKey: "trust", flagOp: "set_number", flagAmount: 3 })).toEqual([ "Would set flag trust to 3", ]); expect(previewEffectLines({ type: "flag", flagKey: "trust", flagOp: "add", flagAmount: 2 })).toEqual([ "Would add 2 to flag trust", ]); expect(previewEffectLines({ type: "notify", title: "Heads up" })).toEqual([ "Would notify the player: Heads up", ]); expect(previewEffectLines(undefined)).toEqual([]); }); it("reports a step's text, attachments and voice without resolving them", () => { const step = previewStep( node({ id: "d1", type: "dialogue", data: { body: "You owe {{debt}} credits.", speaker: "Quartermaster", attachments: [{ mediaId: 1 }], voice: { mediaId: 2, filename: "line.mp3" }, }, }), ); expect(step.text).toBe("You owe {{debt}} credits."); expect(step.speaker).toBe("Quartermaster"); expect(step.effectLines).toEqual([ "1 attachment(s) the player can open", "Voice clip: line.mp3", ]); }); it("uses the closing text for an end node and the lead-in for a choice", () => { expect(previewStep(node({ id: "e1", type: "end", data: { closing: "Goodbye." } })).text).toBe("Goodbye."); expect(previewStep(node({ id: "c1", type: "choice", data: { leadIn: "Well?" } })).text).toBe("Well?"); }); it("finds a target only when the card still exists", () => { const nodes = [node({ id: "a", type: "dialogue", data: {} })]; expect(nodeForTarget(nodes, "a")?.id).toBe("a"); expect(nodeForTarget(nodes, "gone")).toBeNull(); expect(nodeForTarget(nodes, undefined)).toBeNull(); }); });