Three defects in the flow editor, all of which made it look like drag and drop was simply broken: - Reconnection was a silent no-op. React Flow enables it by default, so the drag drew a line and then threw the drop away with no handler: the edge snapped back. Reconnects now write the hop, and a drop over a node card (which arrives with no resolved connection) retargets, carrying the existing choice text, tooltip, once flag and gate instead of blanking the button. - A refused drop said nothing. Dropping on the start node looked like the canvas had frozen; it now explains why nothing can point there. - Handle dots were unmissable at 100% zoom and unusable zoomed out. A child element widens the press target, generously horizontally where the only neighbour is empty canvas and barely vertically because choice rows stack a few pixels apart when zoomed out. Dropping a condition branch on the card filled true and false with the same node, which made the condition pointless; it now takes the first free branch. Connection writing moved into `connectHop` in the lib so the rules are unit-tested rather than exercised only by hand.
168 lines
6.2 KiB
TypeScript
168 lines
6.2 KiB
TypeScript
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();
|
|
});
|
|
});
|