BlueGit

timfa questward

Files

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

initial-quests.test.js

initial-quests.test.js 3.3 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { normalizeQuestDef } from "../../shared/quests/normalize.js";
4import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
5
6function saveLiveQuest (harness, raw)
7{
8 const revisionId = harness.worldDb.getActiveRevisionId();
9 const quest = normalizeQuestDef(raw);
10 harness.worldDb.saveQuestDef(revisionId, quest);
11 harness.simulation.questContent.reload();
12 return quest;
13}
14
15test("initial quests grant on apply after quest def becomes live", () =>
16{
17 const harness = makeSimulation({ workerCount: 1 });
18 try
19 {
20 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
21 harness.simulation.spawnCharacter(1, character);
22 harness.simulation.step();
23
24 const quest = normalizeQuestDef({
25 questId: "starter-hello",
26 title: "Hello Adventurer",
27 steps: [{
28 stepId: "step-1",
29 title: "Begin",
30 objectives: [{
31 objectiveId: "obj-1",
32 text: "Talk to someone",
33 when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] }
34 }],
35 completionJournalText: "Done."
36 }]
37 });
38
39 // Persist settings like the editor (common order: initial quest before Apply).
40 const live = {
41 ...harness.worldDb.getLiveSettings(),
42 initialQuests: ["starter-hello"]
43 };
44 harness.worldDb.saveLiveSettings(live, 1);
45 harness.simulation.applySettings(live, { grantInitialQuests: true });
46 assert.equal(
47 harness.stateDb.getQuestState(character.character_id, "starter-hello").state,
48 "not-started"
49 );
50
51 const revisionId = harness.worldDb.ensurePendingRevision(1);
52 harness.worldDb.saveQuestDef(revisionId, quest);
53 harness.worldDb.promotePendingRevision();
54 harness.simulation.reloadWorld();
55
56 assert.equal(
57 harness.stateDb.getQuestState(character.character_id, "starter-hello").state,
58 1
59 );
60 const snap = harness.simulation.quests.questSnapshot(character.character_id);
61 assert.equal(snap.quests.length, 1);
62 assert.equal(snap.quests[0].questId, "starter-hello");
63 assert.equal(snap.quests[0].title, "Hello Adventurer");
64 }
65 finally
66 { harness.close(); }
67});
68
69test("request-quests ensures missing initial quests before snapshot", () =>
70{
71 const harness = makeSimulation({ workerCount: 1 });
72 try
73 {
74 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
75 harness.simulation.spawnCharacter(1, character);
76 harness.simulation.step();
77
78 saveLiveQuest(harness, {
79 questId: "late-grant",
80 title: "Late Grant",
81 steps: [{
82 objectives: [{
83 text: "Wait",
84 when: { all: [{ condition: { kind: "hasItem", itemId: 99999, quantity: 1 } }] }
85 }]
86 }]
87 });
88 harness.simulation.applySettings({ initialQuests: ["late-grant"] }, { grantInitialQuests: false });
89 assert.equal(
90 harness.stateDb.getQuestState(character.character_id, "late-grant").state,
91 "not-started"
92 );
93
94 harness.simulation.enqueue(1, {
95 type: "request-quests",
96 characterId: character.character_id
97 });
98 const result = harness.simulation.step();
99 const snap = (result.events ?? []).find((e) => e.type === "quests-snapshot");
100 assert.ok(snap);
101 assert.equal(snap.quests.length, 1);
102 assert.equal(snap.quests[0].questId, "late-grant");
103 assert.equal(
104 harness.stateDb.getQuestState(character.character_id, "late-grant").state,
105 1
106 );
107 }
108 finally
109 { harness.close(); }
110});