BlueGit

timfa questward

Files

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

quests-conditions.test.js

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