import { describe, expect, it } from "vitest"; import type { DialogueNode } from "@/lib/npc-dialogue/dialogue"; import { connectHop, hopFromHandle } from "@/lib/npc-dialogue/dialogue"; /** * Connection writing is the canvas's core edit: a wrong hop either loses what the * author typed (a choice button, its gate) or silently rewires the conversation. * These are the rules the drag handlers rely on, tested without a browser. */ const node = ( id: string, type: DialogueNode["type"], data: DialogueNode["data"] = {}, ): DialogueNode => ({ id, type, position: { x: 0, y: 0 }, data }); const choice = node("choice", "choice", { choices: [ { label: "Ask about the radio", target: "radio", tooltip: "**Radio** status", once: true }, { label: "Leave", target: "end" }, ], }); const dialogue = node("d", "dialogue", { body: "Line", next: { target: "choice" } }); const other = node("other", "dialogue", { body: "Elsewhere" }); const end = node("end", "end", { closing: "Done" }); const start = node("start", "dialogue", { body: "Opening", isStart: true }); const base = [start, dialogue, choice, other, end]; const find = (nodes: DialogueNode[], id: string) => nodes.find((n) => n.id === id)!; describe("connectHop", () => { it("routes one choice row without touching its siblings", () => { const result = connectHop(base, { sourceId: "choice", targetId: "other", sourceHandle: "c1" }); expect(result.changed).toBe(true); const choices = find(result.nodes, "choice").data.choices ?? []; expect(choices[0].target).toBe("radio"); expect(choices[1].target).toBe("other"); // A labelled row needs no editor popup. expect(result.labelNodeId).toBeNull(); }); it("keeps a choice's label, tooltip and once flag when a reconnect carries the hop", () => { const carried = hopFromHandle(choice, "c0"); expect(carried?.label).toBe("Ask about the radio"); const result = connectHop(base, { sourceId: "choice", targetId: "other", sourceHandle: "c0", carried, }); const row = (find(result.nodes, "choice").data.choices ?? [])[0]; expect(row.target).toBe("other"); expect(row.label).toBe("Ask about the radio"); expect(row.tooltip).toBe("**Radio** status"); expect(row.once).toBe(true); }); it("creates the first choice from a generic handle and asks for its label", () => { const empty = node("fresh", "choice", { choices: [] }); const result = connectHop([start, empty, other], { sourceId: "fresh", targetId: "other", sourceHandle: null, }); const choices = find(result.nodes, "fresh").data.choices ?? []; expect(choices).toHaveLength(1); expect(choices[0].target).toBe("other"); expect(result.labelNodeId).toBe("fresh"); }); it("does not ask for a label when a carried choice already has one", () => { const empty = node("fresh", "choice", { choices: [] }); const carried = { label: "Ask about the radio", target: "choice", tooltip: "T" }; const result = connectHop([start, empty, other], { sourceId: "fresh", targetId: "other", sourceHandle: null, carried, }); expect(result.labelNodeId).toBeNull(); expect((find(result.nodes, "fresh").data.choices ?? [])[0].label).toBe("Ask about the radio"); }); it("routes a single continuation", () => { const result = connectHop(base, { sourceId: "d", targetId: "other" }); expect(find(result.nodes, "d").data.next?.target).toBe("other"); }); it("fills a condition's named branch, and the first free branch when the card is hit", () => { const cond = node("cond", "condition", { condition: { kind: "flag", key: "ready", op: "is_true" }, }); const trueBranch = connectHop([start, cond, other], { sourceId: "cond", targetId: "other", sourceHandle: "true", }); expect(find(trueBranch.nodes, "cond").data.trueTarget).toBe("other"); expect(find(trueBranch.nodes, "cond").data.falseTarget).toBeUndefined(); const byCard = connectHop([start, cond, other, dialogue], { sourceId: "cond", targetId: "other", }); expect(find(byCard.nodes, "cond").data.trueTarget).toBe("other"); expect(find(byCard.nodes, "cond").data.falseTarget).toBeUndefined(); const second = connectHop(byCard.nodes, { sourceId: "cond", targetId: "d" }); expect(find(second.nodes, "cond").data.falseTarget).toBe("d"); // Both branches taken: a third drop changes nothing. const full = connectHop(second.nodes, { sourceId: "cond", targetId: "choice" }); expect(full.changed).toBe(false); }); it("refuses to point at the conversation start", () => { const result = connectHop(base, { sourceId: "d", targetId: "start" }); expect(result.changed).toBe(false); expect(result.nodes).toBe(base); }); it("refuses a self-link and an unknown node", () => { expect(connectHop(base, { sourceId: "d", targetId: "d" }).changed).toBe(false); expect(connectHop(base, { sourceId: "d", targetId: "ghost" }).changed).toBe(false); }); it("refuses to draw a way out of an end node", () => { expect(connectHop(base, { sourceId: "end", targetId: "other" }).changed).toBe(false); }); it("never mutates the input nodes", () => { const snapshot = JSON.stringify(base); connectHop(base, { sourceId: "choice", targetId: "other", sourceHandle: "c0" }); connectHop(base, { sourceId: "d", targetId: "other" }); expect(JSON.stringify(base)).toBe(snapshot); }); }); describe("hopFromHandle", () => { it("maps a choice handle to its row", () => { expect(hopFromHandle(choice, "c1")?.label).toBe("Leave"); }); it("maps an absent handle to a single continuation", () => { expect(hopFromHandle(dialogue, undefined)?.target).toBe("choice"); }); it("maps condition handles to their branches", () => { const cond = node("cond", "condition", { condition: { kind: "flag", key: "ready", op: "is_true" }, trueTarget: "other", falseTarget: "end", }); expect(hopFromHandle(cond, "true")?.target).toBe("other"); expect(hopFromHandle(cond, "false")?.target).toBe("end"); }); it("returns null for a handle that does not exist, and for a node with no hops", () => { expect(hopFromHandle(choice, "c9")).toBeNull(); expect(hopFromHandle(end, undefined)).toBeNull(); }); });