BlueGit

timfa questward

Files

questward / server / tests / quests-conditions.test.js

quests-conditions.test.js

quests-conditions.test.js 4.2 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 evaluateConditionSet,
5 normalizeConditionSet,
6 ConditionKind
7} from "../../shared/quests/conditions.js";
8import {
9 countableObjectiveProgress,
10 formatObjectiveDisplayText
11} from "../../shared/quests/objectiveProgress.js";
12import { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";
13import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage, formatDialogueTakeMessage } from "../../shared/quests/dialogueRewards.js";
14
15test("ConditionSet AND + NOT", () =>
16{
17 const set = normalizeConditionSet({
18 all: [
19 { condition: { kind: ConditionKind.hasItem, itemId: 1, quantity: 1 } },
20 { not: true, condition: { kind: ConditionKind.questState, questId: "q1", state: "completed" } }
21 ]
22 });
23 assert.equal(evaluateConditionSet(set, {
24 countItem: (id) => (id === 1 ? 2 : 0),
25 questState: () => "not-started"
26 }), true);
27 assert.equal(evaluateConditionSet(set, {
28 countItem: (id) => (id === 1 ? 2 : 0),
29 questState: () => "completed"
30 }), false);
31});
32
33test("normalize quest and dialogue", () =>
34{
35 const quest = normalizeQuestDef({
36 questId: "Fetch Ore!",
37 title: "Fetch Ore",
38 steps: [{
39 objectives: [{ text: "Have tin", when: { all: [{ kind: "hasItem", itemId: 10, quantity: 1 }] } }]
40 }]
41 });
42 assert.equal(quest.questId, "fetch-ore");
43 assert.equal(quest.steps[0].objectives[0].when.all[0].condition.kind, "hasItem");
44
45 const tree = normalizeDialogueTree({
46 dialogueId: "Smith",
47 unique: true,
48 steps: [{
49 stepId: "hi",
50 lines: [{ speaker: "npc", text: "Hello" }],
51 itemRewards: [
52 { itemId: 10, quantity: 2 },
53 { itemId: 10, quantity: 9 },
54 { itemId: 0, quantity: 1 }
55 ],
56 exit: { kind: "end" }
57 }]
58 });
59 assert.equal(tree.dialogueId, "smith");
60 assert.equal(tree.unique, true);
61 assert.equal(tree.steps[0].exit.kind, "end");
62 assert.deepEqual(tree.steps[0].itemRewards, [{ itemId: 10, quantity: 2 }]);
63 assert.deepEqual(tree.steps[0].questRewards, []);
64});
65
66test("dialogue reward messages bag vs bank and lists", () =>
67{
68 assert.equal(
69 formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 1 }], "bag"),
70 "Tin ore has been added to your bag."
71 );
72 assert.equal(
73 formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 3 }], "bank"),
74 "3 Tin ore have been sent to your bank."
75 );
76 assert.equal(
77 formatDialogueRewardMessage([
78 { displayName: "Tin ore", quantity: 1 },
79 { displayName: "Copper ore", quantity: 1 }
80 ], "bag"),
81 "Tin ore and Copper ore have been added to your bag."
82 );
83 assert.equal(
84 formatDialogueRewardMessage([
85 { displayName: "A", quantity: 1 },
86 { displayName: "B", quantity: 1 },
87 { displayName: "C", quantity: 1 }
88 ], "bag"),
89 "A, B and C have been added to your bag."
90 );
91 assert.equal(
92 formatDialogueQuestRewardMessage(["Tin Delivery"]),
93 "The Tin Delivery quest has been added to your journal."
94 );
95 assert.equal(
96 formatDialogueQuestRewardMessage(["Alpha", "Beta"]),
97 "The Alpha and Beta quests have been added to your journal."
98 );
99 assert.equal(
100 formatDialogueTakeMessage([{ displayName: "Copper ore", quantity: 2 }]),
101 "2 Copper ore have been removed from your bag."
102 );
103});
104
105test("countable objective progress for hasItem killCount and skillLevelAtLeast", () =>
106{
107 const hasItem = normalizeConditionSet({
108 all: [{ condition: { kind: ConditionKind.hasItem, itemId: 10, quantity: 30 } }]
109 });
110 assert.deepEqual(
111 countableObjectiveProgress(hasItem, { countItem: (id) => (id === 10 ? 5 : 0) }),
112 { current: 5, required: 30 }
113 );
114 assert.equal(
115 formatObjectiveDisplayText("Collect Pine Logs", { current: 5, required: 30 }),
116 "Collect Pine Logs (5 / 30)"
117 );
118
119 const kills = normalizeConditionSet({
120 all: [{ condition: { kind: ConditionKind.killCount, mobTypeId: "rat", count: 3 } }]
121 });
122 assert.deepEqual(
123 countableObjectiveProgress(kills, {
124 killCount: (c) => (c.mobTypeId === "rat" ? 2 : 0)
125 }),
126 { current: 2, required: 3 }
127 );
128
129 const skill = normalizeConditionSet({
130 all: [{ condition: { kind: ConditionKind.skillLevelAtLeast, skillId: "woodcutting", level: 20 } }]
131 });
132 assert.deepEqual(
133 countableObjectiveProgress(skill, { skillLevel: (id) => (id === "woodcutting" ? 12 : 1) }),
134 { current: 12, required: 20 }
135 );
136});