BlueGit

timfa questward

Files

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

initial-quests.test.js

initial-quests.test.js 8.0 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { normalizeQuestDef } from "../../shared/quests/normalize.js";
4import { emptyEquipment, emptySlots, INVENTORY_SLOTS } from "../../shared/items/inventory.js";
5import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
6
7function saveLiveQuest (harness, raw)
8{
9 const revisionId = harness.worldDb.getActiveRevisionId();
10 const quest = normalizeQuestDef(raw);
11 harness.worldDb.saveQuestDef(revisionId, quest);
12 harness.simulation.questContent.reload();
13 return quest;
14}
15
16test("initial quests grant on apply after quest def becomes live", () =>
17{
18 const harness = makeSimulation({ workerCount: 1 });
19 try
20 {
21 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
22 harness.simulation.spawnCharacter(1, character);
23 harness.simulation.step();
24
25 const quest = normalizeQuestDef({
26 questId: "starter-hello",
27 title: "Hello Adventurer",
28 steps: [{
29 stepId: "step-1",
30 title: "Begin",
31 objectives: [{
32 objectiveId: "obj-1",
33 text: "Talk to someone",
34 when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] }
35 }],
36 completionJournalText: "Done."
37 }]
38 });
39
40 // Persist settings like the editor (common order: initial quest before Apply).
41 const live = {
42 ...harness.worldDb.getLiveSettings(),
43 initialQuests: ["starter-hello"]
44 };
45 harness.worldDb.saveLiveSettings(live, 1);
46 harness.simulation.applySettings(live, { grantInitialQuests: true });
47 assert.equal(
48 harness.stateDb.getQuestState(character.character_id, "starter-hello").state,
49 "not-started"
50 );
51
52 const revisionId = harness.worldDb.ensurePendingRevision(1);
53 harness.worldDb.saveQuestDef(revisionId, quest);
54 harness.worldDb.promotePendingRevision();
55 harness.simulation.reloadWorld();
56
57 assert.equal(
58 harness.stateDb.getQuestState(character.character_id, "starter-hello").state,
59 1
60 );
61 const snap = harness.simulation.quests.questSnapshot(character.character_id);
62 assert.equal(snap.quests.length, 1);
63 assert.equal(snap.quests[0].questId, "starter-hello");
64 assert.equal(snap.quests[0].title, "Hello Adventurer");
65 }
66 finally
67 { harness.close(); }
68});
69
70test("request-quests ensures missing initial quests before snapshot", () =>
71{
72 const harness = makeSimulation({ workerCount: 1 });
73 try
74 {
75 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
76 harness.simulation.spawnCharacter(1, character);
77 harness.simulation.step();
78
79 saveLiveQuest(harness, {
80 questId: "late-grant",
81 title: "Late Grant",
82 steps: [{
83 objectives: [{
84 text: "Wait",
85 when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] }
86 }]
87 }]
88 });
89 harness.simulation.applySettings({ initialQuests: ["late-grant"] }, { grantInitialQuests: false });
90 assert.equal(
91 harness.stateDb.getQuestState(character.character_id, "late-grant").state,
92 "not-started"
93 );
94
95 harness.simulation.enqueue(1, {
96 type: "request-quests",
97 characterId: character.character_id
98 });
99 const result = harness.simulation.step();
100 const snap = (result.events ?? []).find((e) => e.type === "quests-snapshot");
101 assert.ok(snap);
102 assert.equal(snap.quests.length, 1);
103 assert.equal(snap.quests[0].questId, "late-grant");
104 assert.equal(
105 harness.stateDb.getQuestState(character.character_id, "late-grant").state,
106 1
107 );
108 }
109 finally
110 { harness.close(); }
111});
112
113test("request-quests re-checks hasItem objectives against the live bag", () =>
114{
115 const harness = makeSimulation({ workerCount: 1 });
116 try
117 {
118 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
119 const playerId = harness.simulation.spawnCharacter(1, character);
120 harness.simulation.step();
121
122 const quest = saveLiveQuest(harness, {
123 questId: "fetch-rock",
124 title: "Fetch Rock",
125 steps: [{
126 stepId: "step-1",
127 title: "Gather",
128 objectives: [
129 {
130 objectiveId: "obj-rock",
131 text: "Obtain a rock",
132 when: { all: [{ condition: { kind: "hasItem", itemId: 4242, quantity: 1 } }] }
133 },
134 {
135 objectiveId: "obj-gem",
136 text: "Obtain a gem",
137 when: { all: [{ condition: { kind: "hasItem", itemId: 4243, quantity: 1 } }] }
138 }
139 ],
140 completionJournalText: "Got both."
141 }]
142 });
143 harness.simulation.quests.startQuest(
144 character.character_id,
145 quest.questId,
146 harness.simulation.quests.playerViewFor(character.character_id),
147 []
148 );
149
150 const before = harness.simulation.quests.questSnapshot(character.character_id);
151 assert.equal(before.quests[0].step.objectives[0].done, false);
152 assert.equal(before.quests[0].step.objectives[1].done, false);
153
154 const inventory = emptySlots(INVENTORY_SLOTS);
155 inventory[0] = { itemId: 4242, quantity: 1 };
156 harness.simulation.applyBag(playerId, {
157 inventory,
158 equipment: emptyEquipment(),
159 vault: [],
160 vaultReserved: []
161 });
162
163 harness.simulation.enqueue(1, {
164 type: "request-quests",
165 characterId: character.character_id
166 });
167 const result = harness.simulation.step();
168 const snap = (result.events ?? []).find((e) => e.type === "quests-snapshot");
169 assert.ok(snap);
170 const questSnap = snap.quests.find((q) => q.questId === "fetch-rock");
171 assert.ok(questSnap);
172 assert.equal(questSnap.state, 1);
173 assert.equal(questSnap.step.objectives.find((o) => o.objectiveId === "obj-rock")?.done, true);
174 assert.equal(questSnap.step.objectives.find((o) => o.objectiveId === "obj-gem")?.done, false);
175 }
176 finally
177 { harness.close(); }
178});
179
180test("quest snapshot shows partial hasItem counts and bag changes complete objectives", () =>
181{
182 const harness = makeSimulation({ workerCount: 1 });
183 try
184 {
185 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
186 const playerId = harness.simulation.spawnCharacter(1, character);
187 harness.simulation.step();
188
189 const quest = saveLiveQuest(harness, {
190 questId: "pine-logs",
191 title: "Pine Logs",
192 steps: [{
193 stepId: "step-1",
194 title: "Chop",
195 objectives: [
196 {
197 objectiveId: "obj-logs",
198 text: "Collect Pine Logs",
199 when: { all: [{ condition: { kind: "hasItem", itemId: 5555, quantity: 30 } }] }
200 },
201 {
202 objectiveId: "obj-extra",
203 text: "Keep going",
204 when: { all: [{ condition: { kind: "hasItem", itemId: 5556, quantity: 1 } }] }
205 }
206 ],
207 completionJournalText: "Done."
208 }]
209 });
210 harness.simulation.quests.startQuest(
211 character.character_id,
212 quest.questId,
213 harness.simulation.quests.playerViewFor(character.character_id),
214 []
215 );
216
217 const inventory = emptySlots(INVENTORY_SLOTS);
218 inventory[0] = { itemId: 5555, quantity: 5 };
219 harness.simulation.applyBag(playerId, {
220 inventory,
221 equipment: emptyEquipment(),
222 vault: [],
223 vaultReserved: []
224 });
225 const afterPartial = harness.simulation.step();
226 const partialSnap = (afterPartial.events ?? []).find((e) => e.type === "quests-snapshot");
227 assert.ok(partialSnap, "bag change should refresh quest snapshot");
228 const partialObj = partialSnap.quests
229 .find((q) => q.questId === "pine-logs")
230 ?.step?.objectives?.find((o) => o.objectiveId === "obj-logs");
231 assert.equal(partialObj?.current, 5);
232 assert.equal(partialObj?.required, 30);
233 assert.equal(partialObj?.done, false);
234
235 inventory[0] = { itemId: 5555, quantity: 30 };
236 harness.simulation.applyBag(playerId, {
237 inventory,
238 equipment: emptyEquipment(),
239 vault: [],
240 vaultReserved: []
241 });
242 const afterDone = harness.simulation.step();
243 assert.ok(
244 (afterDone.events ?? []).some((e) =>
245 e.type === "quest-objective-complete"
246 && e.questId === "pine-logs"
247 && e.objectiveId === "obj-logs"
248 && e.text === "Collect Pine Logs"
249 ),
250 "completing an objective emits quest-objective-complete"
251 );
252 const doneSnap = (afterDone.events ?? []).find((e) => e.type === "quests-snapshot");
253 const doneObj = doneSnap?.quests
254 .find((q) => q.questId === "pine-logs")
255 ?.step?.objectives?.find((o) => o.objectiveId === "obj-logs");
256 assert.equal(doneObj?.done, true);
257 assert.equal(doneObj?.current, 30);
258 assert.equal(doneObj?.required, 30);
259 }
260 finally
261 { harness.close(); }
262});