import test from "node:test"; import assert from "node:assert/strict"; import { normalizeDialogueTree } from "../../shared/quests/normalize.js"; import { ConditionKind } from "../../shared/quests/conditions.js"; import { formatDialogueTakeMessage } from "../../shared/quests/dialogueRewards.js"; import { dialogueExitEdges, unguaranteedDialogueTakes } from "../../shared/quests/dialoguePathTakes.js"; import { countItemInSlots, emptyEquipment, emptySlots, INVENTORY_SLOTS, takeMatchingUpToFromSlots } from "../../shared/items/inventory.js"; import { ItemId } from "../../shared/definitions/items.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; test("normalizeDialogueTree keeps itemTakes", () => { const tree = normalizeDialogueTree({ dialogueId: "Take Tin", steps: [{ stepId: "ask", lines: [{ speaker: "npc", text: "Pay up." }], itemTakes: [{ itemId: ItemId.tinOre, quantity: 3 }, { itemId: ItemId.tinOre, quantity: 9 }] }] }); assert.deepEqual(tree.steps[0].itemTakes, [{ itemId: ItemId.tinOre, quantity: 3 }]); }); test("formatDialogueTakeMessage lists removed bag items", () => { assert.equal( formatDialogueTakeMessage([{ displayName: "Tin ore", quantity: 1 }]), "Tin ore has been removed from your bag." ); assert.equal( formatDialogueTakeMessage([{ displayName: "Tin ore", quantity: 3 }]), "3 Tin ore have been removed from your bag." ); }); test("takeMatchingUpToFromSlots soft-takes what is available", () => { const slots = emptySlots(4); slots[0] = { itemId: ItemId.bones, quantity: 2 }; const result = takeMatchingUpToFromSlots(slots, { itemId: ItemId.bones, quantity: 5, compact: false }); assert.equal(result.ok, true); assert.equal(result.takenQuantity, 2); assert.equal(countItemInSlots(result.slots, ItemId.bones), 0); }); test("unguaranteedDialogueTakes warns unless every path guarantees hasItem", () => { const unguaranteed = normalizeDialogueTree({ dialogueId: "unguarded-take", entryStepId: "start", steps: [ { stepId: "start", lines: [{ text: "Hi" }], exit: { kind: "branches", branches: [{ label: "go", when: { all: [] }, nextStepId: "pay" }] } }, { stepId: "pay", lines: [{ text: "Hand it over." }], itemTakes: [{ itemId: ItemId.bones, quantity: 2 }] } ] }); const warn = unguaranteedDialogueTakes(unguaranteed); assert.equal(warn.get("pay")?.has(ItemId.bones), true); const guaranteed = normalizeDialogueTree({ dialogueId: "guarded-take", entryStepId: "start", steps: [ { stepId: "start", lines: [{ text: "Hi" }], exit: { kind: "branches", branches: [{ label: "go", when: { all: [{ condition: { kind: ConditionKind.hasItem, itemId: ItemId.bones, quantity: 2 } }] }, nextStepId: "pay" }] } }, { stepId: "pay", lines: [{ text: "Hand it over." }], itemTakes: [{ itemId: ItemId.bones, quantity: 2 }] } ] }); const ok = unguaranteedDialogueTakes(guaranteed); assert.equal(ok.has("pay"), false); }); test("dialogue advance soft-takes itemTakes then reports a notice", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const tree = normalizeDialogueTree({ dialogueId: "collect-bones", steps: [{ stepId: "greeting", lines: [{ speaker: "npc", text: "Those bones look useful." }], itemTakes: [{ itemId: ItemId.bones, quantity: 5 }] }] }); const revisionId = harness.worldDb.getActiveRevisionId(); harness.worldDb.saveDialogueTree(revisionId, tree); harness.simulation.questContent.reload(); const inventory = emptySlots(INVENTORY_SLOTS); inventory[0] = { itemId: ItemId.bones, quantity: 2 }; harness.simulation.applyBag(playerId, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const npcId = 9001; const opened = harness.simulation.quests.tryOpenDialogue( character.character_id, npcId, "collect-bones" ); assert.equal(opened.ok, true); const advanced = harness.simulation.quests.advanceDialogue(character.character_id); assert.equal(advanced.ok, true); const notice = (advanced.events ?? []).find((e) => e.type === "dialogueItemRewards"); assert.ok(notice); assert.match(notice.messages.join(" "), /2 Bones have been removed from your bag/); const bag = harness.simulation.bagOf(playerId); assert.equal(countItemInSlots(bag.inventory, ItemId.bones), 0); } finally { harness.close(); } }); test("dialogueHeard is set only after the step fully completes", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const tree = normalizeDialogueTree({ dialogueId: "heard-timing", steps: [{ stepId: "greeting", lines: [{ speaker: "npc", text: "Hello." }], itemRewards: [{ itemId: ItemId.bones, quantity: 1 }] }] }); const revisionId = harness.worldDb.getActiveRevisionId(); harness.worldDb.saveDialogueTree(revisionId, tree); harness.simulation.questContent.reload(); const opened = harness.simulation.quests.tryOpenDialogue( character.character_id, 9002, "heard-timing" ); assert.equal(opened.ok, true); assert.equal( harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"), false, "opening / entering lines must not mark heard" ); const afterLines = harness.simulation.quests.advanceDialogue(character.character_id); assert.equal(afterLines.ok, true); assert.ok((afterLines.events ?? []).some((e) => e.type === "dialogueItemRewards")); assert.equal( harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"), false, "reward notices must be acknowledged before heard" ); const afterRewards = harness.simulation.quests.advanceDialogue(character.character_id); assert.equal(afterRewards.ok, true); assert.equal( harness.stateDb.isDialogueHeard(character.character_id, "heard-timing", "greeting"), true, "heard only after lines + rewards are finished" ); assert.equal(countItemInSlots(harness.simulation.bagOf(playerId).inventory, ItemId.bones), 1); } finally { harness.close(); } }); test("normalizeDialogueTree supports continue exits and editor coordinates", () => { const tree = normalizeDialogueTree({ dialogueId: "chain", entryStepId: "a", steps: [ { stepId: "a", label: "Start", lines: [{ text: "Go on." }], editorX: 120.5, editorY: 40, exit: { kind: "continue", nextStepId: "b" } }, { stepId: "b", lines: [{ text: "Done." }], editorX: 300, editorY: 80, exit: { kind: "end" } } ] }); assert.equal(tree.steps[0].exit.kind, "continue"); assert.equal(tree.steps[0].exit.nextStepId, "b"); assert.equal(tree.steps[0].editorX, 120.5); assert.equal(tree.steps[0].editorY, 40); assert.equal(tree.steps[1].editorX, 300); assert.equal(tree.steps[1].editorY, 80); }); test("dialogueExitEdges treats continue as a single edge", () => { const edges = dialogueExitEdges({ kind: "continue", nextStepId: "next" }); assert.deepEqual(edges, [{ nextStepId: "next", when: { all: [] } }]); assert.deepEqual(dialogueExitEdges({ kind: "continue", nextStepId: null }), []); assert.deepEqual(dialogueExitEdges({ kind: "end" }), []); }); test("unguaranteedDialogueTakes follows continue edges", () => { const tree = normalizeDialogueTree({ dialogueId: "continue-take", entryStepId: "start", steps: [ { stepId: "start", lines: [{ text: "Hi" }], exit: { kind: "continue", nextStepId: "pay" } }, { stepId: "pay", lines: [{ text: "Hand it over." }], itemTakes: [{ itemId: ItemId.bones, quantity: 1 }] } ] }); const warn = unguaranteedDialogueTakes(tree); assert.equal(warn.get("pay")?.has(ItemId.bones), true); });