Review findings, all in code this range introduced. Attaching a branch to a node that already had a next step replaced that hop, and "come back where it attached" pointed the branch back at the caller, which loops: the caller then leads into the branch it just returned from. Everything downstream was orphaned. The branch now continues into the step that hop used to point at, so attaching inserts in front of it instead of deleting it. Rejoining a condition is refused outright, because re-entering re-evaluates the same flags and retakes the same branch. Return candidates are limited to node types that read `next`: a choice or condition open end would have received a hop no walker follows. The dialog says which of the three applies, and only offers the toggle when a return exists, so an untouched default is not sent and reported back as a refusal. Condition true/false dots were drawn on the input handle: the hit-area class sets left and right on the handle itself, which over-constrains React Flow's `right: 0`, so both dots drifted to the card's left edge. They now take the hit area as a child, like the other handles. Refusals are honest: `connectHop` reports `changed: false` for a write it did not make (it used to report true and return a new array), the editor explains instead of rewriting an identical graph, and a refused tail-drag keeps the original hop rather than deleting it first. A passing connection test now really is invalidated when the configuration changes. That ran in `afterChange`, whose return value is only the operation's response, so the stored row kept showing a stale pass while the test asserted the response. It moved to `beforeChange`, counts only fields actually submitted (so the Test button's own result write does not invalidate itself), and the test reads the row back. Also: the fragment prompt no longer teaches `isStart: true` through the worked example or asks for "the whole conversation" on retry; requests carry a ten minute ceiling so an unreachable-but-accepting host cannot hang Generate, Compose or the Test button; the API key description no longer claims at-rest encryption, which does not exist; the per-dialogue voice override gets the same dash and length cleaning as the global it outranks; and the unused `describeAttachmentPoint` is gone, with the attachment output now included in the hint the model receives.
176 lines
6.6 KiB
TypeScript
176 lines
6.6 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("reports no change when the named choice row does not exist", () => {
|
|
const result = connectHop(base, { sourceId: "choice", targetId: "other", sourceHandle: "c7" });
|
|
expect(result.changed).toBe(false);
|
|
expect(result.nodes).toBe(base);
|
|
});
|
|
|
|
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. It must name an existing
|
|
// node, or the refusal comes from the unknown target and proves nothing.
|
|
const full = connectHop(second.nodes, { sourceId: "cond", targetId: "other" });
|
|
expect(full.changed).toBe(false);
|
|
expect(full.nodes).toBe(second.nodes);
|
|
});
|
|
|
|
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();
|
|
});
|
|
});
|