| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { normalizeDialogueTree } from "../../shared/quests/normalize.js"; |
| 4 | import { ConditionKind } from "../../shared/quests/conditions.js"; |
| 5 | import { formatDialogueTakeMessage } from "../../shared/quests/dialogueRewards.js"; |
| 6 | import { dialogueExitEdges, unguaranteedDialogueTakes } from "../../shared/quests/dialoguePathTakes.js"; |
| 7 | import { |
| 8 | countItemInSlots, |
| 9 | emptyEquipment, |
| 10 | emptySlots, |
| 11 | INVENTORY_SLOTS, |
| 12 | takeMatchingUpToFromSlots |
| 13 | } from "../../shared/items/inventory.js"; |
| 14 | import { ItemId } from "../../shared/definitions/items.js"; |
| 15 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 16 | |
| 17 | test("normalizeDialogueTree keeps itemTakes", () => |
| 18 | { |
| 19 | const tree = normalizeDialogueTree({ |
| 20 | dialogueId: "Take Tin", |
| 21 | steps: [{ |
| 22 | stepId: "ask", |
| 23 | lines: [{ speaker: "npc", text: "Pay up." }], |
| 24 | itemTakes: [{ itemId: ItemId.tinOre, quantity: 3 }, { itemId: ItemId.tinOre, quantity: 9 }] |
| 25 | }] |
| 26 | }); |
| 27 | assert.deepEqual(tree.steps[0].itemTakes, [{ itemId: ItemId.tinOre, quantity: 3 }]); |
| 28 | }); |
| 29 | |
| 30 | test("formatDialogueTakeMessage lists removed bag items", () => |
| 31 | { |
| 32 | assert.equal( |
| 33 | formatDialogueTakeMessage([{ displayName: "Tin ore", quantity: 1 }]), |
| 34 | "Tin ore has been removed from your bag." |
| 35 | ); |
| 36 | assert.equal( |
| 37 | formatDialogueTakeMessage([{ displayName: "Tin ore", quantity: 3 }]), |
| 38 | "3 Tin ore have been removed from your bag." |
| 39 | ); |
| 40 | }); |
| 41 | |
| 42 | test("takeMatchingUpToFromSlots soft-takes what is available", () => |
| 43 | { |
| 44 | const slots = emptySlots(4); |
| 45 | slots[0] = { itemId: ItemId.bones, quantity: 2 }; |
| 46 | const result = takeMatchingUpToFromSlots(slots, { |
| 47 | itemId: ItemId.bones, |
| 48 | quantity: 5, |
| 49 | compact: false |
| 50 | }); |
| 51 | assert.equal(result.ok, true); |
| 52 | assert.equal(result.takenQuantity, 2); |
| 53 | assert.equal(countItemInSlots(result.slots, ItemId.bones), 0); |
| 54 | }); |
| 55 | |
| 56 | test("unguaranteedDialogueTakes warns unless every path guarantees hasItem", () => |
| 57 | { |
| 58 | const unguaranteed = normalizeDialogueTree({ |
| 59 | dialogueId: "unguarded-take", |
| 60 | entryStepId: "start", |
| 61 | steps: [ |
| 62 | { |
| 63 | stepId: "start", |
| 64 | lines: [{ text: "Hi" }], |
| 65 | exit: { |
| 66 | kind: "branches", |
| 67 | branches: [{ |
| 68 | label: "go", |
| 69 | when: { all: [] }, |
| 70 | nextStepId: "pay" |
| 71 | }] |
| 72 | } |
| 73 | }, |
| 74 | { |
| 75 | stepId: "pay", |
| 76 | lines: [{ text: "Hand it over." }], |
| 77 | itemTakes: [{ itemId: ItemId.bones, quantity: 2 }] |
| 78 | } |
| 79 | ] |
| 80 | }); |
| 81 | const warn = unguaranteedDialogueTakes(unguaranteed); |
| 82 | assert.equal(warn.get("pay")?.has(ItemId.bones), true); |
| 83 | |
| 84 | const guaranteed = normalizeDialogueTree({ |
| 85 | dialogueId: "guarded-take", |
| 86 | entryStepId: "start", |
| 87 | steps: [ |
| 88 | { |
| 89 | stepId: "start", |
| 90 | lines: [{ text: "Hi" }], |
| 91 | exit: { |
| 92 | kind: "branches", |
| 93 | branches: [{ |
| 94 | label: "go", |
| 95 | when: { |
| 96 | all: [{ |
| 97 | condition: { |
| 98 | kind: ConditionKind.hasItem, |
| 99 | itemId: ItemId.bones, |
| 100 | quantity: 2 |
| 101 | } |
| 102 | }] |
| 103 | }, |
| 104 | nextStepId: "pay" |
| 105 | }] |
| 106 | } |
| 107 | }, |
| 108 | { |
| 109 | stepId: "pay", |
| 110 | lines: [{ text: "Hand it over." }], |
| 111 | itemTakes: [{ itemId: ItemId.bones, quantity: 2 }] |
| 112 | } |
| 113 | ] |
| 114 | }); |
| 115 | const ok = unguaranteedDialogueTakes(guaranteed); |
| 116 | assert.equal(ok.has("pay"), false); |
| 117 | }); |
| 118 | |
| 119 | test("dialogue advance soft-takes itemTakes then reports a notice", () => |
| 120 | { |
| 121 | const harness = makeSimulation({ workerCount: 1 }); |
| 122 | try |
| 123 | { |
| 124 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 125 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 126 | harness.simulation.step(); |
| 127 | |
| 128 | const tree = normalizeDialogueTree({ |
| 129 | dialogueId: "collect-bones", |
| 130 | steps: [{ |
| 131 | stepId: "greeting", |
| 132 | lines: [{ speaker: "npc", text: "Those bones look useful." }], |
| 133 | itemTakes: [{ itemId: ItemId.bones, quantity: 5 }] |
| 134 | }] |
| 135 | }); |
| 136 | const revisionId = harness.worldDb.getActiveRevisionId(); |
| 137 | harness.worldDb.saveDialogueTree(revisionId, tree); |
| 138 | harness.simulation.questContent.reload(); |
| 139 | |
| 140 | const inventory = emptySlots(INVENTORY_SLOTS); |
| 141 | inventory[0] = { itemId: ItemId.bones, quantity: 2 }; |
| 142 | harness.simulation.applyBag(playerId, { |
| 143 | inventory, |
| 144 | equipment: emptyEquipment(), |
| 145 | vault: [], |
| 146 | vaultReserved: [] |
| 147 | }); |
| 148 | |
| 149 | const npcId = 9001; |
| 150 | const opened = harness.simulation.quests.tryOpenDialogue( |
| 151 | character.character_id, |
| 152 | npcId, |
| 153 | "collect-bones" |
| 154 | ); |
| 155 | assert.equal(opened.ok, true); |
| 156 | |
| 157 | const advanced = harness.simulation.quests.advanceDialogue(character.character_id); |
| 158 | assert.equal(advanced.ok, true); |
| 159 | const notice = (advanced.events ?? []).find((e) => e.type === "dialogueItemRewards"); |
| 160 | assert.ok(notice); |
| 161 | assert.match(notice.messages.join(" "), /2 Bones have been removed from your bag/); |
| 162 | |
| 163 | const bag = harness.simulation.bagOf(playerId); |
| 164 | assert.equal(countItemInSlots(bag.inventory, ItemId.bones), 0); |
| 165 | } |
| 166 | finally |
| 167 | { harness.close(); } |
| 168 | }); |
| 169 | |
| 170 | test("dialogueHeard is set only after the step fully completes", () => |
| 171 | { |
| 172 | const harness = makeSimulation({ workerCount: 1 }); |
| 173 | try |
| 174 | { |
| 175 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 176 | const playerId = harness.simulation.spawnCharacter(1, character); |
| 177 | harness.simulation.step(); |
| 178 | |
| 179 | const tree = normalizeDialogueTree({ |
| 180 | dialogueId: "heard-timing", |
| 181 | steps: [{ |
| 182 | stepId: "greeting", |
| 183 | lines: [{ speaker: "npc", text: "Hello." }], |
| 184 | itemRewards: [{ itemId: ItemId.bones, quantity: 1 }] |
| 185 | }] |
| 186 | }); |
| 187 | const revisionId = harness.worldDb.getActiveRevisionId(); |
| 188 | harness.worldDb.saveDialogueTree(revisionId, tree); |
| 189 | harness.simulation.questContent.reload(); |
| 190 | |
| 191 | const opened = harness.simulation.quests.tryOpenDialogue( |
| 192 | character.character_id, |
| 193 | 9002, |
| 194 | "heard-timing" |
| 195 | ); |
| 196 | assert.equal(opened.ok, true); |
| 197 | assert.equal( |
| 198 | harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"), |
| 199 | false, |
| 200 | "opening / entering lines must not mark heard" |
| 201 | ); |
| 202 | |
| 203 | const afterLines = harness.simulation.quests.advanceDialogue(character.character_id); |
| 204 | assert.equal(afterLines.ok, true); |
| 205 | assert.ok((afterLines.events ?? []).some((e) => e.type === "dialogueItemRewards")); |
| 206 | assert.equal( |
| 207 | harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"), |
| 208 | false, |
| 209 | "reward notices must be acknowledged before heard" |
| 210 | ); |
| 211 | |
| 212 | const afterRewards = harness.simulation.quests.advanceDialogue(character.character_id); |
| 213 | assert.equal(afterRewards.ok, true); |
| 214 | assert.equal( |
| 215 | harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"), |
| 216 | true, |
| 217 | "heard only after lines + rewards are finished" |
| 218 | ); |
| 219 | assert.equal(countItemInSlots(harness.simulation.bagOf(playerId).inventory, ItemId.bones), 1); |
| 220 | } |
| 221 | finally |
| 222 | { harness.close(); } |
| 223 | }); |
| 224 | |
| 225 | test("normalizeDialogueTree supports continue exits and editor coordinates", () => |
| 226 | { |
| 227 | const tree = normalizeDialogueTree({ |
| 228 | dialogueId: "chain", |
| 229 | entryStepId: "a", |
| 230 | steps: [ |
| 231 | { |
| 232 | stepId: "a", |
| 233 | label: "Start", |
| 234 | lines: [{ text: "Go on." }], |
| 235 | editorX: 120.5, |
| 236 | editorY: 40, |
| 237 | exit: { kind: "continue", nextStepId: "b" } |
| 238 | }, |
| 239 | { |
| 240 | stepId: "b", |
| 241 | lines: [{ text: "Done." }], |
| 242 | editorX: 300, |
| 243 | editorY: 80, |
| 244 | exit: { kind: "end" } |
| 245 | } |
| 246 | ] |
| 247 | }); |
| 248 | assert.equal(tree.steps[0].exit.kind, "continue"); |
| 249 | assert.equal(tree.steps[0].exit.nextStepId, "b"); |
| 250 | assert.equal(tree.steps[0].editorX, 120.5); |
| 251 | assert.equal(tree.steps[0].editorY, 40); |
| 252 | assert.equal(tree.steps[1].editorX, 300); |
| 253 | assert.equal(tree.steps[1].editorY, 80); |
| 254 | }); |
| 255 | |
| 256 | test("dialogueExitEdges treats continue as a single edge", () => |
| 257 | { |
| 258 | const edges = dialogueExitEdges({ kind: "continue", nextStepId: "next" }); |
| 259 | assert.deepEqual(edges, [{ nextStepId: "next", when: { all: [] } }]); |
| 260 | assert.deepEqual(dialogueExitEdges({ kind: "continue", nextStepId: null }), []); |
| 261 | assert.deepEqual(dialogueExitEdges({ kind: "end" }), []); |
| 262 | }); |
| 263 | |
| 264 | test("unguaranteedDialogueTakes follows continue edges", () => |
| 265 | { |
| 266 | const tree = normalizeDialogueTree({ |
| 267 | dialogueId: "continue-take", |
| 268 | entryStepId: "start", |
| 269 | steps: [ |
| 270 | { |
| 271 | stepId: "start", |
| 272 | lines: [{ text: "Hi" }], |
| 273 | exit: { kind: "continue", nextStepId: "pay" } |
| 274 | }, |
| 275 | { |
| 276 | stepId: "pay", |
| 277 | lines: [{ text: "Hand it over." }], |
| 278 | itemTakes: [{ itemId: ItemId.bones, quantity: 1 }] |
| 279 | } |
| 280 | ] |
| 281 | }); |
| 282 | const warn = unguaranteedDialogueTakes(tree); |
| 283 | assert.equal(warn.get("pay")?.has(ItemId.bones), true); |
| 284 | }); |