import test from "node:test"; import assert from "node:assert/strict"; import { evaluateConditionSet, normalizeConditionSet, ConditionKind, ConditionSetMode } from "../../shared/quests/conditions.js"; import { countableObjectiveProgress, formatObjectiveDisplayText } from "../../shared/quests/objectiveProgress.js"; import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js"; import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage, formatDialogueTakeMessage } from "../../shared/quests/dialogueRewards.js"; test("ConditionSet AND + NOT", () => { const set = normalizeConditionSet({ all: [ { condition: { kind: ConditionKind.hasItem, itemId: 1, quantity: 1 } }, { not: true, condition: { kind: ConditionKind.questState, questId: "q1", state: "completed" } } ] }); assert.equal(set.mode, ConditionSetMode.allOf); assert.equal(evaluateConditionSet(set, { countItem: (id) => (id === 1 ? 2 : 0), questState: () => "not-started" }), true); assert.equal(evaluateConditionSet(set, { countItem: (id) => (id === 1 ? 2 : 0), questState: () => "completed" }), false); }); test("ConditionSet modes oneOrMoreOf onlyOneOf and noneOf", () => { const clauses = [ { condition: { kind: ConditionKind.hasItem, itemId: 1, quantity: 1 } }, { condition: { kind: ConditionKind.hasItem, itemId: 2, quantity: 1 } } ]; const ctxNone = { countItem: () => 0 }; const ctxOne = { countItem: (id) => (id === 1 ? 1 : 0) }; const ctxBoth = { countItem: () => 1 }; const oneOrMore = normalizeConditionSet({ mode: ConditionSetMode.oneOrMoreOf, all: clauses }); assert.equal(evaluateConditionSet(oneOrMore, ctxNone), false); assert.equal(evaluateConditionSet(oneOrMore, ctxOne), true); assert.equal(evaluateConditionSet(oneOrMore, ctxBoth), true); const onlyOne = normalizeConditionSet({ mode: ConditionSetMode.onlyOneOf, all: clauses }); assert.equal(evaluateConditionSet(onlyOne, ctxNone), false); assert.equal(evaluateConditionSet(onlyOne, ctxOne), true); assert.equal(evaluateConditionSet(onlyOne, ctxBoth), false); const none = normalizeConditionSet({ mode: ConditionSetMode.noneOf, all: clauses }); assert.equal(evaluateConditionSet(none, ctxNone), true); assert.equal(evaluateConditionSet(none, ctxOne), false); assert.equal(evaluateConditionSet(none, ctxBoth), false); assert.equal( normalizeConditionSet({ mode: "nope", all: clauses }).mode, ConditionSetMode.allOf ); }); test("normalize quest and dialogue", () => { const quest = normalizeQuestDef({ questId: "Fetch Ore!", title: "Fetch Ore", steps: [{ objectives: [{ text: "Have tin", when: { all: [{ kind: "hasItem", itemId: 10, quantity: 1 }] } }] }] }); assert.equal(quest.questId, "fetch-ore"); assert.equal(quest.steps[0].objectives[0].when.all[0].condition.kind, "hasItem"); const tree = normalizeDialogueTree({ dialogueId: "Smith", unique: true, steps: [{ stepId: "hi", lines: [{ speaker: "npc", text: "Hello" }], itemRewards: [ { itemId: 10, quantity: 2 }, { itemId: 10, quantity: 9 }, { itemId: 0, quantity: 1 } ], exit: { kind: "end" } }] }); assert.equal(tree.dialogueId, "smith"); assert.equal(tree.unique, true); assert.equal(tree.steps[0].exit.kind, "end"); assert.deepEqual(tree.steps[0].itemRewards, [{ itemId: 10, quantity: 2 }]); assert.deepEqual(tree.steps[0].questRewards, []); }); test("dialogue reward messages bag vs bank and lists", () => { assert.equal( formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 1 }], "bag"), "Tin ore has been added to your bag." ); assert.equal( formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 3 }], "bank"), "3 Tin ore have been sent to your bank." ); assert.equal( formatDialogueRewardMessage([ { displayName: "Tin ore", quantity: 1 }, { displayName: "Copper ore", quantity: 1 } ], "bag"), "Tin ore and Copper ore have been added to your bag." ); assert.equal( formatDialogueRewardMessage([ { displayName: "A", quantity: 1 }, { displayName: "B", quantity: 1 }, { displayName: "C", quantity: 1 } ], "bag"), "A, B and C have been added to your bag." ); assert.equal( formatDialogueQuestRewardMessage(["Tin Delivery"]), "The Tin Delivery quest has been added to your journal." ); assert.equal( formatDialogueQuestRewardMessage(["Alpha", "Beta"]), "The Alpha and Beta quests have been added to your journal." ); assert.equal( formatDialogueTakeMessage([{ displayName: "Copper ore", quantity: 2 }]), "2 Copper ore have been removed from your bag." ); }); test("countable objective progress for hasItem killCount and skillLevelAtLeast", () => { const hasItem = normalizeConditionSet({ all: [{ condition: { kind: ConditionKind.hasItem, itemId: 10, quantity: 30 } }] }); assert.deepEqual( countableObjectiveProgress(hasItem, { countItem: (id) => (id === 10 ? 5 : 0) }), { current: 5, required: 30 } ); assert.equal( formatObjectiveDisplayText("Collect Pine Logs", { current: 5, required: 30 }), "Collect Pine Logs (5 / 30)" ); const kills = normalizeConditionSet({ all: [{ condition: { kind: ConditionKind.killCount, mobTypeId: "rat", count: 3 } }] }); assert.deepEqual( countableObjectiveProgress(kills, { killCount: (c) => (c.mobTypeId === "rat" ? 2 : 0) }), { current: 2, required: 3 } ); const fromSpawner = normalizeConditionSet({ all: [{ condition: { kind: ConditionKind.killMobFromSpawner, mobTypeId: "rat", spawnerId: "rats-1", count: 4 } }] }); assert.deepEqual( countableObjectiveProgress(fromSpawner, { killCount: (c) => (c.kind === ConditionKind.killMobFromSpawner && c.spawnerId === "rats-1" ? 1 : 0) }), { current: 1, required: 4 } ); assert.equal( evaluateConditionSet(fromSpawner, { killCount: (c) => (c.spawnerId === "rats-1" ? 4 : 0) }), true ); const skill = normalizeConditionSet({ all: [{ condition: { kind: ConditionKind.skillLevelAtLeast, skillId: "woodcutting", level: 20 } }] }); assert.deepEqual( countableObjectiveProgress(skill, { skillLevel: (id) => (id === "woodcutting" ? 12 : 1) }), { current: 12, required: 20 } ); });