A condition in a draft could only be walked the way the author's flags really resolve, so a conversation gated behind a flag they have not earned was untestable in the preview: the other branch was unreachable without granting themselves the flag for real. A condition node now offers both exits. One is labelled "your flags" and is the branch a player with the author's flags would take; the other is an amber override that walks the opposite branch anyway. The same applies to a choice whose gate fails: it stays clickable, reads as an override, and says so in its gate note. conditionBranches in preview.ts pairs the two targets and labels them, so the component holds no branch logic of its own; an unwired branch is reported as unwired rather than silently offering nothing. The footer counts overrides against the author's flags, so an override walk is never mistaken for what a player would see, and Back/Restart behave as before. Verified in the running editor with a draft whose condition tests a flag that is false for the dev account: the gate card showed "false for you, so a player matching your flags takes the false branch" with both buttons, and taking the true branch landed on the true card with "1 override against your flags" in the footer. 3 pure tests cover the pairing and the unwired case; 223 npc-dialogue tests pass.
166 lines
5.9 KiB
TypeScript
166 lines
5.9 KiB
TypeScript
// @vitest-environment node
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import type { DialogueNode } from "@/lib/npc-dialogue/dialogue";
|
|
import {
|
|
automaticTarget,
|
|
conditionBranches,
|
|
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();
|
|
});
|
|
});
|
|
|
|
describe("condition branches in the preview", () => {
|
|
const gate = node({
|
|
id: "gate",
|
|
type: "condition",
|
|
data: { trueTarget: "yes", falseTarget: "no" },
|
|
});
|
|
|
|
it("labels the real branch and the override one", () => {
|
|
expect(conditionBranches(gate, true)).toEqual({
|
|
actual: { label: "true", target: "yes" },
|
|
override: { label: "false", target: "no" },
|
|
});
|
|
expect(conditionBranches(gate, false)).toEqual({
|
|
actual: { label: "false", target: "no" },
|
|
override: { label: "true", target: "yes" },
|
|
});
|
|
});
|
|
|
|
it("reports an unwired branch as no target instead of inventing one", () => {
|
|
const halfWired = node({
|
|
id: "half",
|
|
type: "condition",
|
|
data: { trueTarget: "yes" },
|
|
});
|
|
// The condition is false for this author, and only the true side is wired.
|
|
expect(conditionBranches(halfWired, false)?.actual.target).toBeNull();
|
|
expect(conditionBranches(halfWired, false)?.override.target).toBe("yes");
|
|
});
|
|
|
|
it("is null for a node that is not a condition", () => {
|
|
const line = node({ id: "line", type: "dialogue", data: { next: { target: "next" } } });
|
|
expect(conditionBranches(line, true)).toBeNull();
|
|
});
|
|
});
|