import test from "node:test"; import assert from "node:assert/strict"; import { normalizeQuestDef } from "../../shared/quests/normalize.js"; import { ConditionKind } from "../../shared/quests/conditions.js"; import { normalizeDoorConfig } from "../../shared/world/doorRequirements.js"; import { BlockType } from "../../shared/definitions/tiles.js"; import { ItemId } from "../../shared/definitions/items.js"; import { SkillId } from "../../shared/definitions/skills.js"; import { emptyEquipment, emptySlots, INVENTORY_SLOTS } from "../../shared/items/inventory.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; function saveLiveQuest (harness, raw) { const revisionId = harness.worldDb.getActiveRevisionId(); const quest = normalizeQuestDef(raw); harness.worldDb.saveQuestDef(revisionId, quest); harness.simulation.questContent.reload(); return quest; } function stuckObjective (itemId = 99999) { return [{ objectiveId: "stay", text: "Stay", when: { all: [{ condition: { kind: "hasItem", itemId, quantity: 1 } }] } }]; } function openObjective () { return [{ objectiveId: "go", text: "Go", when: { all: [] } }]; } function workerOf (simulation, entityId) { const record = simulation.entityRecord(entityId); assert.ok(record); return simulation.workers[record.workerId]; } function placeDoor (worker, tileX, tileY, when) { const data = worker.world.chunkData(Math.floor(tileX / 16), Math.floor(tileY / 16)); const localX = ((tileX % 16) + 16) % 16; const localY = ((tileY % 16) + 16) % 16; data.blocks[localY * 16 + localX] = { typeId: BlockType.door, state: "oak-n-closed", config: normalizeDoorConfig({ when, blockedMessage: "Not yet." }) }; } test("linear quests still advance one step per evaluation and reward once", () => { const harness = makeSimulation({ workerCount: 1 }); try { const quest = saveLiveQuest(harness, { questId: "linear-chain", title: "Linear", steps: [ { stepId: "s1", title: "One", objectives: openObjective(), completionJournalText: "One done." }, { stepId: "s2", title: "Two", objectives: openObjective(), completionJournalText: "Two done." }, { stepId: "s3", title: "Three", objectives: openObjective(), completionJournalText: "Three done." } ], rewards: [{ kind: "giveXp", skillId: SkillId.woodworking, amount: 11 }] }); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const view = harness.simulation.quests.playerViewFor(character.character_id); const events = []; harness.simulation.quests.startQuest(character.character_id, quest.questId, view, events); assert.equal(events.some((event) => event.type === "questComplete"), false); let state = harness.stateDb.getQuestState(character.character_id, quest.questId); assert.equal(state.state, "s2"); assert.deepEqual(state.progress.completedStepIds, ["s1"]); harness.simulation.quests.reevaluateQuests(character.character_id, view, []); state = harness.stateDb.getQuestState(character.character_id, quest.questId); assert.equal(state.state, "s3"); assert.deepEqual(state.progress.completedStepIds, ["s1", "s2"]); assert.equal(harness.stateDb.getSkills(character.character_id).xp.woodworking, 0); harness.simulation.quests.reevaluateQuests(character.character_id, view, []); state = harness.stateDb.getQuestState(character.character_id, quest.questId); assert.equal(state.state, "completed"); assert.deepEqual(state.progress.completedStepIds, ["s1", "s2", "s3"]); assert.equal(harness.stateDb.getSkills(character.character_id).xp.woodworking, 11); harness.simulation.quests.reevaluateQuests(character.character_id, view, []); assert.equal(harness.stateDb.getSkills(character.character_id).xp.woodworking, 11); assert.equal( harness.stateDb.getQuestState(character.character_id, quest.questId).state, "completed" ); } finally { harness.close(); } }); test("branch exit takes the first matching condition set only", () => { const harness = makeSimulation({ workerCount: 1 }); try { const quest = saveLiveQuest(harness, { questId: "branch-first", title: "Branch", steps: [{ stepId: "hub", title: "Hub", exit: { kind: "branches", branches: [ { label: "Bones", text: "Bring bones", when: { all: [{ condition: { kind: "hasItem", itemId: ItemId.bones, quantity: 1 } }] }, nextStepId: "bones-path" }, { label: "Other", text: "Otherwise", when: { all: [] }, nextStepId: "other-path" } ] } }, { stepId: "bones-path", title: "Bones", objectives: stuckObjective() }, { stepId: "other-path", title: "Other", objectives: stuckObjective() }] }); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const inventory = emptySlots(INVENTORY_SLOTS); inventory[0] = { itemId: ItemId.bones, quantity: 1 }; harness.simulation.applyBag(playerId, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const view = harness.simulation.quests.playerViewFor(character.character_id); harness.simulation.quests.startQuest(character.character_id, quest.questId, view, []); const state = harness.stateDb.getQuestState(character.character_id, quest.questId); assert.equal(state.state, "bones-path"); assert.deepEqual(state.progress.completedStepIds, ["hub"]); const snap = harness.simulation.quests.questSnapshot(character.character_id); assert.equal(snap.quests[0].step.stepId, "bones-path"); } finally { harness.close(); } }); test("unmatched branch step shows branch texts with kill progress", () => { const harness = makeSimulation({ workerCount: 1 }); try { const quest = saveLiveQuest(harness, { questId: "branch-log", title: "Log", steps: [{ stepId: "choice", title: "Choose", exit: { kind: "branches", branches: [ { text: "Kill rats", when: { all: [{ condition: { kind: "killCount", mobTypeId: "rat", count: 3 } }] }, nextStepId: "done" }, { text: "Bring bones", when: { all: [{ condition: { kind: "hasItem", itemId: ItemId.bones, quantity: 2 } }] }, nextStepId: "done" } ] } }, { stepId: "done", title: "Done", objectives: stuckObjective() }] }); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const view = harness.simulation.quests.playerViewFor(character.character_id); harness.simulation.quests.startQuest(character.character_id, quest.questId, view, []); let snap = harness.simulation.quests.questSnapshot(character.character_id); assert.deepEqual( snap.quests[0].step.objectives.map((line) => line.text), ["Kill rats", "Bring bones"] ); assert.equal(snap.quests[0].step.objectives[0].current, 0); assert.equal(snap.quests[0].step.objectives[0].required, 3); harness.simulation.quests.noteKill(character.character_id, { mobTypeId: "rat", spawnerId: null }); snap = harness.simulation.quests.questSnapshot(character.character_id); assert.equal(snap.quests[0].step.stepId, "choice"); assert.equal(snap.quests[0].step.objectives[0].current, 1); assert.equal( harness.stateDb.getQuestState(character.character_id, quest.questId).state, "choice" ); } finally { harness.close(); } }); test("noteKill counts only the active step resolved by step id", () => { const harness = makeSimulation({ workerCount: 1 }); try { const quest = saveLiveQuest(harness, { questId: "kill-which", title: "Which", steps: [{ stepId: "first", title: "Deer", objectives: [{ objectiveId: "deer", text: "Deer", when: { all: [{ condition: { kind: "killCount", mobTypeId: "deer", count: 1 } }] } }] }, { stepId: "second", title: "Rats", objectives: [{ objectiveId: "rats", text: "Rats", when: { all: [{ condition: { kind: "killCount", mobTypeId: "rat", count: 2 } }] } }] }] }); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); harness.stateDb.saveQuestState(character.character_id, quest.questId, "second", { completedObjectiveIds: [], counters: {}, completedStepIds: ["first"] }, Date.now(), null); harness.simulation.quests.noteKill(character.character_id, { mobTypeId: "deer", spawnerId: null }); harness.simulation.quests.noteKill(character.character_id, { mobTypeId: "rat", spawnerId: null }); const state = harness.stateDb.getQuestState(character.character_id, quest.questId); assert.equal(state.state, "second"); assert.equal(state.progress.counters?.["mob:deer"], undefined); assert.equal(state.progress.counters?.["mob:rat"], 1); assert.deepEqual(state.progress.completedStepIds, ["first"]); } finally { harness.close(); } }); test("completed step ids survive a loop back onto the same step", () => { const harness = makeSimulation({ workerCount: 1 }); try { const quest = saveLiveQuest(harness, { questId: "loop-q", title: "Loop", steps: [{ stepId: "alpha", title: "Alpha", exit: { kind: "branches", branches: [{ text: "To beta", when: { all: [] }, nextStepId: "beta" }] } }, { stepId: "beta", title: "Beta", exit: { kind: "branches", branches: [{ text: "To alpha", when: { all: [] }, nextStepId: "alpha" }] } }] }); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const view = harness.simulation.quests.playerViewFor(character.character_id); harness.simulation.quests.startQuest(character.character_id, quest.questId, view, []); let state = harness.stateDb.getQuestState(character.character_id, quest.questId); assert.equal(state.state, "beta"); assert.deepEqual(state.progress.completedStepIds, ["alpha"]); harness.simulation.quests.reevaluateQuests(character.character_id, view, []); state = harness.stateDb.getQuestState(character.character_id, quest.questId); assert.equal(state.state, "alpha"); assert.deepEqual(state.progress.completedStepIds, ["alpha", "beta"]); harness.simulation.quests.reevaluateQuests(character.character_id, view, []); state = harness.stateDb.getQuestState(character.character_id, quest.questId); assert.equal(state.state, "beta"); assert.deepEqual(state.progress.completedStepIds, ["alpha", "beta"]); assert.equal(state.state === "completed", false); } finally { harness.close(); } }); test("legacy numeric quest index resolves the step and unlocks doors", () => { const harness = makeSimulation({ workerCount: 1 }); try { const quest = saveLiveQuest(harness, { questId: "door-steps", title: "Door Steps", steps: [{ stepId: "gate-a", title: "First", objectives: stuckObjective(99991) }, { stepId: "gate-b", title: "Second", objectives: stuckObjective(99992) }] }); const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const entityId = harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const worker = workerOf(harness.simulation, entityId); placeDoor(worker, 9, 8, { all: [{ condition: { kind: ConditionKind.questStepAtLeast, questId: quest.questId, step: 2 } }] }); assert.equal(worker.world.walkableFor(entityId, 9, 8), false); harness.stateDb.saveQuestState(character.character_id, quest.questId, 2, {}, Date.now(), null); harness.simulation.syncCharacterConditionAccess(character.character_id); const snap = harness.simulation.quests.questSnapshot(character.character_id); assert.equal(snap.quests[0].step.stepId, "gate-b"); assert.equal(worker.world.walkableFor(entityId, 9, 8), true, "at least step 2 means gate-a was completed"); placeDoor(worker, 9, 8, { all: [{ condition: { kind: ConditionKind.hasCompletedStep, questId: quest.questId, stepId: "gate-b" } }] }); assert.equal(worker.world.walkableFor(entityId, 9, 8), false); placeDoor(worker, 9, 8, { all: [{ condition: { kind: ConditionKind.questState, questId: quest.questId, state: "gate-b" } }] }); assert.equal(worker.world.walkableFor(entityId, 9, 8), true); placeDoor(worker, 9, 8, { all: [{ condition: { kind: ConditionKind.questState, questId: quest.questId, state: "gate-a" } }] }); assert.equal(worker.world.walkableFor(entityId, 9, 8), false); } finally { harness.close(); } });