import test from "node:test"; import assert from "node:assert/strict"; import { normalizeQuestDef } from "../../shared/quests/normalize.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; } test("initial quests grant on apply after quest def becomes live", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); const quest = normalizeQuestDef({ questId: "starter-hello", title: "Hello Adventurer", steps: [{ stepId: "step-1", title: "Begin", objectives: [{ objectiveId: "obj-1", text: "Talk to someone", when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] } }], completionJournalText: "Done." }] }); // Persist settings like the editor (common order: initial quest before Apply). const live = { ...harness.worldDb.getLiveSettings(), initialQuests: ["starter-hello"] }; harness.worldDb.saveLiveSettings(live, 1); harness.simulation.applySettings(live, { grantInitialQuests: true }); assert.equal( harness.stateDb.getQuestState(character.character_id, "starter-hello").state, "not-started" ); const revisionId = harness.worldDb.ensurePendingRevision(1); harness.worldDb.saveQuestDef(revisionId, quest); harness.worldDb.promotePendingRevision(); harness.simulation.reloadWorld(); assert.equal( harness.stateDb.getQuestState(character.character_id, "starter-hello").state, "step-1" ); const snap = harness.simulation.quests.questSnapshot(character.character_id); assert.equal(snap.quests.length, 1); assert.equal(snap.quests[0].questId, "starter-hello"); assert.equal(snap.quests[0].title, "Hello Adventurer"); } finally { harness.close(); } }); test("request-quests ensures missing initial quests before snapshot", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); harness.simulation.spawnCharacter(1, character); harness.simulation.step(); saveLiveQuest(harness, { questId: "late-grant", title: "Late Grant", steps: [{ objectives: [{ text: "Wait", when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] } }] }] }); harness.simulation.applySettings({ initialQuests: ["late-grant"] }, { grantInitialQuests: false }); assert.equal( harness.stateDb.getQuestState(character.character_id, "late-grant").state, "not-started" ); harness.simulation.enqueue(1, { type: "request-quests", characterId: character.character_id }); const result = harness.simulation.step(); const snap = (result.events ?? []).find((e) => e.type === "quests-snapshot"); assert.ok(snap); assert.equal(snap.quests.length, 1); assert.equal(snap.quests[0].questId, "late-grant"); assert.equal( harness.stateDb.getQuestState(character.character_id, "late-grant").state, "step-1" ); } finally { harness.close(); } }); test("request-quests re-checks hasItem objectives against the live bag", () => { 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 quest = saveLiveQuest(harness, { questId: "fetch-rock", title: "Fetch Rock", steps: [{ stepId: "step-1", title: "Gather", objectives: [ { objectiveId: "obj-rock", text: "Obtain a rock", when: { all: [{ condition: { kind: "hasItem", itemId: 4242, quantity: 1 } }] } }, { objectiveId: "obj-gem", text: "Obtain a gem", when: { all: [{ condition: { kind: "hasItem", itemId: 4243, quantity: 1 } }] } } ], completionJournalText: "Got both." }] }); harness.simulation.quests.startQuest( character.character_id, quest.questId, harness.simulation.quests.playerViewFor(character.character_id), [] ); const before = harness.simulation.quests.questSnapshot(character.character_id); assert.equal(before.quests[0].step.objectives[0].done, false); assert.equal(before.quests[0].step.objectives[1].done, false); const inventory = emptySlots(INVENTORY_SLOTS); inventory[0] = { itemId: 4242, quantity: 1 }; harness.simulation.applyBag(playerId, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); harness.simulation.enqueue(1, { type: "request-quests", characterId: character.character_id }); const result = harness.simulation.step(); const snap = (result.events ?? []).find((e) => e.type === "quests-snapshot"); assert.ok(snap); const questSnap = snap.quests.find((q) => q.questId === "fetch-rock"); assert.ok(questSnap); assert.equal(questSnap.state, "step-1"); assert.equal(questSnap.step.objectives.find((o) => o.objectiveId === "obj-rock")?.done, true); assert.equal(questSnap.step.objectives.find((o) => o.objectiveId === "obj-gem")?.done, false); } finally { harness.close(); } }); test("quest snapshot shows partial hasItem counts and bag changes complete objectives", () => { 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 quest = saveLiveQuest(harness, { questId: "pine-logs", title: "Pine Logs", steps: [{ stepId: "step-1", title: "Chop", objectives: [ { objectiveId: "obj-logs", text: "Collect Pine Logs", when: { all: [{ condition: { kind: "hasItem", itemId: 5555, quantity: 30 } }] } }, { objectiveId: "obj-extra", text: "Keep going", when: { all: [{ condition: { kind: "hasItem", itemId: 5556, quantity: 1 } }] } } ], completionJournalText: "Done." }] }); harness.simulation.quests.startQuest( character.character_id, quest.questId, harness.simulation.quests.playerViewFor(character.character_id), [] ); const inventory = emptySlots(INVENTORY_SLOTS); inventory[0] = { itemId: 5555, quantity: 5 }; harness.simulation.applyBag(playerId, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const afterPartial = harness.simulation.step(); const partialSnap = (afterPartial.events ?? []).find((e) => e.type === "quests-snapshot"); assert.ok(partialSnap, "bag change should refresh quest snapshot"); const partialObj = partialSnap.quests .find((q) => q.questId === "pine-logs") ?.step?.objectives?.find((o) => o.objectiveId === "obj-logs"); assert.equal(partialObj?.current, 5); assert.equal(partialObj?.required, 30); assert.equal(partialObj?.done, false); inventory[0] = { itemId: 5555, quantity: 30 }; harness.simulation.applyBag(playerId, { inventory, equipment: emptyEquipment(), vault: [], vaultReserved: [] }); const afterDone = harness.simulation.step(); assert.ok( (afterDone.events ?? []).some((e) => e.type === "quest-objective-complete" && e.questId === "pine-logs" && e.objectiveId === "obj-logs" && e.text === "Collect Pine Logs" ), "completing an objective emits quest-objective-complete" ); const doneSnap = (afterDone.events ?? []).find((e) => e.type === "quests-snapshot"); const doneObj = doneSnap?.quests .find((q) => q.questId === "pine-logs") ?.step?.objectives?.find((o) => o.objectiveId === "obj-logs"); assert.equal(doneObj?.done, true); assert.equal(doneObj?.current, 30); assert.equal(doneObj?.required, 30); } finally { harness.close(); } });