Authoring meant saving, leaving the admin, and finding the contact on the player site to see whether a branch worked. A Play button in the canvas toolbar now walks the draft exactly as it stands, without saving it. The walker is not the runtime component: that resolves text and choices through server actions keyed on a saved dialogue id, and teaching all four a draft mode would be a much larger change. preview.ts answers what is local (choice order, branch targets, effect descriptions) and the server answers the one thing it cannot, flag values, through a new previewDialogueCondition action gated on npc-dialogues:update. It calls the same evaluateCondition the player runtime uses, so a preview cannot disagree with the outcome. Effects are described, never applied. Choices the author's own flags would hide are shown as hidden rather than silently dropped. Verified on a temp document with an event effect and a condition node: the event step showed 'Would set flag preview_probe_flag to true' with a dry-run note, the condition answered false for a missing flag, the walk followed the false branch to its END node, and player_flags gained no row (5 before, 5 after). Bare admin buttons render with the UA's white background, so each one carries an explicit background; 9 pure tests cover the walker's reading of the graph.
130 lines
4.7 KiB
TypeScript
130 lines
4.7 KiB
TypeScript
// @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<DialogueNode> & { 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();
|
|
});
|
|
});
|