| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { CARD_W, CARD_H, dialogueOutputPorts } from "../../client/src/editor/dialogueCanvas.js"; |
| 4 | import { diffQuests } from "../src/editor/service.js"; |
| 5 | import { normalizeQuestDef, relinkImplicitQuestExits } from "../../shared/quests/normalize.js"; |
| 6 | import { emptyConditionSet } from "../../shared/quests/conditions.js"; |
| 7 | import { |
| 8 | QUEST_CARD_W, |
| 9 | QUEST_CARD_H, |
| 10 | blankQuestStep, |
| 11 | branchShadowsLater, |
| 12 | layoutQuestSteps, |
| 13 | prepareQuestExitsForSave, |
| 14 | questCanvasPorts |
| 15 | } from "../../shared/quests/questGraph.js"; |
| 16 | |
| 17 | test("dialogue canvas ports stay the default graph and match quest card size", () => |
| 18 | { |
| 19 | assert.equal(QUEST_CARD_W, CARD_W); |
| 20 | assert.equal(QUEST_CARD_H, CARD_H); |
| 21 | |
| 22 | const ended = { exit: { kind: "end" } }; |
| 23 | assert.deepEqual(dialogueOutputPorts(ended), []); |
| 24 | |
| 25 | const step = { |
| 26 | exit: { |
| 27 | kind: "triggers", |
| 28 | triggers: [{ trigger: "go", text: "Walk on", nextStepId: "next" }] |
| 29 | } |
| 30 | }; |
| 31 | const ports = dialogueOutputPorts(step); |
| 32 | assert.equal(ports.length, 1); |
| 33 | assert.equal(ports[0].key, "trigger:0"); |
| 34 | assert.equal(ports[0].label, "Walk on"); |
| 35 | ports[0].setNext("other"); |
| 36 | assert.equal(step.exit.triggers[0].nextStepId, "other"); |
| 37 | |
| 38 | const branched = { |
| 39 | exit: { |
| 40 | kind: "branches", |
| 41 | branches: [{ label: "", nextStepId: null }, { label: "Yes", nextStepId: "y" }] |
| 42 | } |
| 43 | }; |
| 44 | const branchPorts = dialogueOutputPorts(branched); |
| 45 | assert.equal(branchPorts[0].label, "Branch 1"); |
| 46 | assert.equal(branchPorts[1].label, "Yes"); |
| 47 | }); |
| 48 | |
| 49 | test("quest steps keep editor positions and implicit chains", () => |
| 50 | { |
| 51 | const quest = normalizeQuestDef({ |
| 52 | questId: "placed", |
| 53 | steps: [ |
| 54 | { stepId: "a", editorX: 12.5, editorY: 40 }, |
| 55 | { stepId: "b" } |
| 56 | ] |
| 57 | }); |
| 58 | assert.equal(quest.steps[0].editorX, 12.5); |
| 59 | assert.equal(quest.steps[0].editorY, 40); |
| 60 | assert.equal(quest.steps[0].exit.implicit, true); |
| 61 | assert.equal(quest.steps[0].exit.nextStepId, "b"); |
| 62 | assert.equal(quest.steps[1].editorX, undefined); |
| 63 | }); |
| 64 | |
| 65 | test("saving keeps implicit exits unless the author changed the graph", () => |
| 66 | { |
| 67 | const linear = normalizeQuestDef({ |
| 68 | questId: "line", |
| 69 | steps: [{ stepId: "a" }, { stepId: "b" }, { stepId: "c" }] |
| 70 | }); |
| 71 | prepareQuestExitsForSave(linear.steps); |
| 72 | assert.equal(linear.steps[0].exit.implicit, true); |
| 73 | assert.equal(linear.steps[0].exit.nextStepId, "b"); |
| 74 | assert.equal(linear.steps[2].exit.kind, "complete"); |
| 75 | assert.equal(linear.steps[2].exit.implicit, true); |
| 76 | |
| 77 | linear.steps[0].exit = { kind: "continue", nextStepId: "c" }; |
| 78 | prepareQuestExitsForSave(linear.steps); |
| 79 | const saved = normalizeQuestDef(linear); |
| 80 | assert.equal(saved.steps[0].exit.implicit, undefined); |
| 81 | assert.equal(saved.steps[0].exit.nextStepId, "c"); |
| 82 | assert.equal(saved.steps[1].exit.implicit, true); |
| 83 | assert.equal(saved.steps[1].exit.nextStepId, "c"); |
| 84 | assert.equal(saved.steps[2].exit.kind, "complete"); |
| 85 | |
| 86 | const extended = normalizeQuestDef({ |
| 87 | questId: "line", |
| 88 | steps: [{ stepId: "a" }, { stepId: "b" }] |
| 89 | }); |
| 90 | extended.steps.push(blankQuestStep("c", 30, 80)); |
| 91 | prepareQuestExitsForSave(extended.steps); |
| 92 | const withExtra = normalizeQuestDef(extended); |
| 93 | assert.equal(withExtra.steps[0].exit.nextStepId, "b"); |
| 94 | assert.equal(withExtra.steps[0].exit.implicit, true); |
| 95 | assert.equal(withExtra.steps[1].exit.kind, "complete"); |
| 96 | assert.equal(withExtra.steps[1].exit.implicit, undefined); |
| 97 | assert.equal(withExtra.steps[2].exit.kind, "complete"); |
| 98 | assert.equal(withExtra.steps[2].editorX, 30); |
| 99 | assert.equal(withExtra.steps[2].editorY, 80); |
| 100 | }); |
| 101 | |
| 102 | test("deleting a middle implicit step relinks the chain", () => |
| 103 | { |
| 104 | const quest = normalizeQuestDef({ |
| 105 | questId: "line", |
| 106 | steps: [{ stepId: "a" }, { stepId: "b" }, { stepId: "c" }] |
| 107 | }); |
| 108 | quest.steps.splice(1, 1); |
| 109 | relinkImplicitQuestExits(quest.steps); |
| 110 | assert.equal(quest.steps[0].exit.kind, "continue"); |
| 111 | assert.equal(quest.steps[0].exit.nextStepId, "c"); |
| 112 | assert.equal(quest.steps[0].exit.implicit, true); |
| 113 | assert.equal(quest.steps[1].exit.kind, "complete"); |
| 114 | }); |
| 115 | |
| 116 | test("quest canvas ports, first-match warning, and auto layout", () => |
| 117 | { |
| 118 | const filled = { |
| 119 | all: [{ condition: { kind: "hasItem", itemId: 1, quantity: 1 } }] |
| 120 | }; |
| 121 | const quest = normalizeQuestDef({ |
| 122 | questId: "fork", |
| 123 | steps: [ |
| 124 | { |
| 125 | stepId: "a", |
| 126 | title: "Fork", |
| 127 | exit: { |
| 128 | kind: "branches", |
| 129 | branches: [ |
| 130 | { label: "Left", text: "Take the left path", when: emptyConditionSet(), nextStepId: "b" }, |
| 131 | { label: "Right", text: "Take the right path", when: filled, nextStepId: "c" } |
| 132 | ] |
| 133 | } |
| 134 | }, |
| 135 | { stepId: "b", title: "Left", exit: { kind: "continue", nextStepId: "d" } }, |
| 136 | { stepId: "c", title: "Right", exit: { kind: "continue", nextStepId: "d" } }, |
| 137 | { stepId: "d", title: "Join", exit: { kind: "complete" } } |
| 138 | ] |
| 139 | }); |
| 140 | const fork = quest.steps[0]; |
| 141 | assert.equal(fork.exit.implicit, undefined); |
| 142 | assert.equal(branchShadowsLater(fork.exit.branches, 0), true); |
| 143 | assert.equal(branchShadowsLater(fork.exit.branches, 1), false); |
| 144 | |
| 145 | const ports = questCanvasPorts(fork); |
| 146 | assert.equal(ports.length, 2); |
| 147 | assert.equal(ports[0].label, "1. Left"); |
| 148 | assert.equal(ports[1].key, "branch:1"); |
| 149 | assert.equal(questCanvasPorts(quest.steps[1]).length, 1); |
| 150 | assert.equal(questCanvasPorts(quest.steps[3]).length, 0); |
| 151 | |
| 152 | const placed = layoutQuestSteps(quest.steps); |
| 153 | assert.ok(placed.get("a").x < placed.get("b").x); |
| 154 | assert.equal(placed.get("b").x, placed.get("c").x); |
| 155 | assert.ok(placed.get("b").y < placed.get("c").y); |
| 156 | assert.ok(placed.get("d").x > placed.get("b").x); |
| 157 | |
| 158 | quest.steps[0].editorX = 400; |
| 159 | quest.steps[0].editorY = 400; |
| 160 | const mixed = layoutQuestSteps(quest.steps); |
| 161 | assert.deepEqual(mixed.get("a"), { x: 400, y: 400 }); |
| 162 | assert.equal(mixed.get("b").x, placed.get("b").x); |
| 163 | assert.equal(mixed.get("c").y, placed.get("c").y); |
| 164 | |
| 165 | ports[0].setNext("d"); |
| 166 | assert.equal(fork.exit.branches[0].nextStepId, "d"); |
| 167 | assert.equal(fork.exit.branches[1].nextStepId, "c"); |
| 168 | |
| 169 | const linear = normalizeQuestDef({ |
| 170 | questId: "line", |
| 171 | steps: [{ stepId: "a" }, { stepId: "b" }] |
| 172 | }); |
| 173 | const same = questCanvasPorts(linear.steps[0]); |
| 174 | assert.equal(linear.steps[0].exit.implicit, true); |
| 175 | same[0].setNext("b"); |
| 176 | assert.equal(linear.steps[0].exit.implicit, true); |
| 177 | same[0].setNext("elsewhere"); |
| 178 | assert.equal(linear.steps[0].exit.implicit, undefined); |
| 179 | assert.equal(linear.steps[0].exit.nextStepId, "elsewhere"); |
| 180 | }); |
| 181 | |
| 182 | test("quest changelog separates canvas moves from step edits", () => |
| 183 | { |
| 184 | const previous = normalizeQuestDef({ |
| 185 | questId: "q", |
| 186 | title: "Fetch", |
| 187 | steps: [{ stepId: "a", title: "Start" }, { stepId: "b", title: "End" }] |
| 188 | }); |
| 189 | const moved = structuredClone(previous); |
| 190 | moved.steps[0].editorX = 80; |
| 191 | moved.steps[0].editorY = 40; |
| 192 | const movedBullets = diffQuests(previous, moved); |
| 193 | assert.deepEqual(movedBullets, ['Step "Start" (a) moved on the canvas.']); |
| 194 | |
| 195 | const edited = structuredClone(previous); |
| 196 | edited.steps[1].title = "Finish"; |
| 197 | const editedBullets = diffQuests(previous, edited); |
| 198 | assert.ok(editedBullets.some((line) => /updated/.test(line))); |
| 199 | assert.equal(editedBullets.some((line) => /moved/.test(line)), false); |
| 200 | |
| 201 | const reordered = structuredClone(previous); |
| 202 | reordered.steps.reverse(); |
| 203 | const orderBullets = diffQuests(previous, reordered); |
| 204 | assert.ok(orderBullets.some((line) => /Step order changed/.test(line))); |
| 205 | }); |