BlueGit

timfa questward

Files

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

quests-conditions.test.js

quests-conditions.test.js 2.9 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 { normalizeQuestDef, normalizeDialogueTree } from "../../shared/quests/normalize.js";
9import { formatDialogueRewardMessage, formatDialogueQuestRewardMessage } from "../../shared/quests/dialogueRewards.js";
10
11test("ConditionSet AND + NOT", () =>
12{
13 const set = normalizeConditionSet({
14 all: [
15 { condition: { kind: ConditionKind.hasItem, itemId: 1, quantity: 1 } },
16 { not: true, condition: { kind: ConditionKind.questState, questId: "q1", state: "completed" } }
17 ]
18 });
19 assert.equal(evaluateConditionSet(set, {
20 countItem: (id) => (id === 1 ? 2 : 0),
21 questState: () => "not-started"
22 }), true);
23 assert.equal(evaluateConditionSet(set, {
24 countItem: (id) => (id === 1 ? 2 : 0),
25 questState: () => "completed"
26 }), false);
27});
28
29test("normalize quest and dialogue", () =>
30{
31 const quest = normalizeQuestDef({
32 questId: "Fetch Ore!",
33 title: "Fetch Ore",
34 steps: [{
35 objectives: [{ text: "Have tin", when: { all: [{ kind: "hasItem", itemId: 10, quantity: 1 }] } }]
36 }]
37 });
38 assert.equal(quest.questId, "fetch-ore");
39 assert.equal(quest.steps[0].objectives[0].when.all[0].condition.kind, "hasItem");
40
41 const tree = normalizeDialogueTree({
42 dialogueId: "Smith",
43 unique: true,
44 steps: [{
45 stepId: "hi",
46 lines: [{ speaker: "npc", text: "Hello" }],
47 itemRewards: [
48 { itemId: 10, quantity: 2 },
49 { itemId: 10, quantity: 9 },
50 { itemId: 0, quantity: 1 }
51 ],
52 exit: { kind: "end" }
53 }]
54 });
55 assert.equal(tree.dialogueId, "smith");
56 assert.equal(tree.unique, true);
57 assert.equal(tree.steps[0].exit.kind, "end");
58 assert.deepEqual(tree.steps[0].itemRewards, [{ itemId: 10, quantity: 2 }]);
59 assert.deepEqual(tree.steps[0].questRewards, []);
60});
61
62test("dialogue reward messages bag vs bank and lists", () =>
63{
64 assert.equal(
65 formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 1 }], "bag"),
66 "Tin ore has been added to your bag."
67 );
68 assert.equal(
69 formatDialogueRewardMessage([{ displayName: "Tin ore", quantity: 3 }], "bank"),
70 "3 Tin ore have been sent to your bank."
71 );
72 assert.equal(
73 formatDialogueRewardMessage([
74 { displayName: "Tin ore", quantity: 1 },
75 { displayName: "Copper ore", quantity: 1 }
76 ], "bag"),
77 "Tin ore and Copper ore have been added to your bag."
78 );
79 assert.equal(
80 formatDialogueRewardMessage([
81 { displayName: "A", quantity: 1 },
82 { displayName: "B", quantity: 1 },
83 { displayName: "C", quantity: 1 }
84 ], "bag"),
85 "A, B and C have been added to your bag."
86 );
87 assert.equal(
88 formatDialogueQuestRewardMessage(["Tin Delivery"]),
89 "The Tin Delivery quest has been added to your journal."
90 );
91 assert.equal(
92 formatDialogueQuestRewardMessage(["Alpha", "Beta"]),
93 "The Alpha and Beta quests have been added to your journal."
94 );
95});