| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { EffectKind, normalizeEffects } from "../../shared/quests/conditions.js"; |
| 4 | import { normalizeQuestDef } from "../../shared/quests/normalize.js"; |
| 5 | import { formatQuestBankRewardLetterBody } from "../../shared/courier/questRewardLetters.js"; |
| 6 | import { ItemId } from "../../shared/definitions/items.js"; |
| 7 | import { SkillId } from "../../shared/definitions/skills.js"; |
| 8 | import { countItemInSlots } from "../../shared/items/inventory.js"; |
| 9 | import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; |
| 10 | |
| 11 | test("normalizeEffects keeps giveItem toBank and cleans giveXp", () => |
| 12 | { |
| 13 | const effects = normalizeEffects([ |
| 14 | { kind: EffectKind.giveItem, itemId: ItemId.bones, quantity: 2, toBank: true }, |
| 15 | { kind: EffectKind.giveXp, skillId: SkillId.woodworking, amount: 15 }, |
| 16 | { kind: EffectKind.giveXp, skillId: SkillId.swordfighting, amount: 0 }, |
| 17 | { kind: "nope", itemId: 1 } |
| 18 | ]); |
| 19 | assert.deepEqual(effects, [ |
| 20 | { kind: EffectKind.giveItem, itemId: ItemId.bones, quantity: 2, toBank: true }, |
| 21 | { kind: EffectKind.giveXp, skillId: SkillId.woodworking, amount: 15 } |
| 22 | ]); |
| 23 | }); |
| 24 | |
| 25 | test("quest bank reward letter body names the quest and items", () => |
| 26 | { |
| 27 | const body = formatQuestBankRewardLetterBody({ |
| 28 | characterDisplayName: "Ada Worker", |
| 29 | questTitle: "Reward Test", |
| 30 | items: [{ itemId: ItemId.bones, quantity: 2 }] |
| 31 | }); |
| 32 | assert.match(body, /^Dear Ada Worker,/); |
| 33 | assert.match(body, /quest "Reward Test"/); |
| 34 | assert.match(body, /• 2× Bones/); |
| 35 | assert.doesNotMatch(body, /Signed/); |
| 36 | }); |
| 37 | |
| 38 | test("quest completion grants XP, bags items, banks when flagged, and emits questComplete", () => |
| 39 | { |
| 40 | const harness = makeSimulation({ workerCount: 1 }); |
| 41 | try |
| 42 | { |
| 43 | const revisionId = harness.worldDb.getActiveRevisionId(); |
| 44 | const quest = normalizeQuestDef({ |
| 45 | questId: "reward-test", |
| 46 | title: "Reward Test", |
| 47 | steps: [{ |
| 48 | stepId: "step-1", |
| 49 | objectives: [{ objectiveId: "obj-1", text: "Done", when: { all: [] } }], |
| 50 | completionJournalText: "Finished." |
| 51 | }], |
| 52 | rewards: [ |
| 53 | { kind: EffectKind.giveXp, skillId: SkillId.woodworking, amount: 15 }, |
| 54 | { kind: EffectKind.giveXp, skillId: SkillId.swordfighting, amount: 30 }, |
| 55 | { kind: EffectKind.giveItem, itemId: ItemId.bones, quantity: 2, toBank: true }, |
| 56 | { kind: EffectKind.giveItem, itemId: ItemId.glassBottle, quantity: 1, toBank: false } |
| 57 | ] |
| 58 | }); |
| 59 | harness.worldDb.saveQuestDef(revisionId, quest); |
| 60 | harness.simulation.questContent.reload(); |
| 61 | |
| 62 | const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); |
| 63 | const entityId = harness.simulation.spawnCharacter(1, character); |
| 64 | harness.simulation.step(); |
| 65 | |
| 66 | const sticksBefore = countItemInSlots( |
| 67 | harness.simulation.bagOf(entityId).inventory, |
| 68 | ItemId.stick |
| 69 | ); |
| 70 | const events = []; |
| 71 | harness.simulation.quests.startQuest( |
| 72 | character.character_id, |
| 73 | "reward-test", |
| 74 | harness.simulation.quests.playerViewFor(character.character_id), |
| 75 | events |
| 76 | ); |
| 77 | |
| 78 | const complete = events.find((e) => e.type === "questComplete"); |
| 79 | assert.ok(complete); |
| 80 | assert.equal(complete.title, "Reward Test"); |
| 81 | assert.deepEqual(complete.xp, [ |
| 82 | { skillId: SkillId.woodworking, displayName: "Woodworking", amount: 15 }, |
| 83 | { skillId: SkillId.swordfighting, displayName: "Swords", amount: 30 } |
| 84 | ]); |
| 85 | assert.deepEqual(complete.items, [ |
| 86 | { displayName: "Glass bottle", quantity: 1, itemId: ItemId.glassBottle } |
| 87 | ]); |
| 88 | |
| 89 | const bag = harness.simulation.bagOf(entityId); |
| 90 | assert.equal(countItemInSlots(bag.inventory, ItemId.bones), 0); |
| 91 | assert.equal(countItemInSlots(bag.vault, ItemId.bones), 2); |
| 92 | assert.equal(countItemInSlots(bag.inventory, ItemId.glassBottle), 1); |
| 93 | assert.equal(countItemInSlots(bag.inventory, ItemId.stick), sticksBefore); |
| 94 | |
| 95 | const skills = harness.stateDb.getSkills(character.character_id); |
| 96 | assert.equal(skills.xp.woodworking, 15); |
| 97 | assert.equal(skills.xp.swordfighting, 30); |
| 98 | |
| 99 | const state = harness.stateDb.getQuestState(character.character_id, "reward-test"); |
| 100 | assert.equal(state.state, "completed"); |
| 101 | } |
| 102 | finally |
| 103 | { harness.close(); } |
| 104 | }); |