BlueGit

timfa questward

Files

questward / server / tests / skills.test.js

skills.test.js

skills.test.js 7.6 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import {
4 SKILL_XP_BASE,
5 xpToAdvanceFromLevel,
6 totalXpForLevel,
7 xpProgress,
8 levelFromTotalXp,
9 combatHitXpPerSkill
10} from "../../shared/skills/progression.js";
11import {
12 normalizeSkillXp,
13 normalizePlayerSkills,
14 defaultPlayerSkills,
15 addSkillXp,
16 applySkillXp,
17 publicSkillsView,
18 enforceMinimumSkillLevels
19} from "../../shared/skills/state.js";
20import { skillLevel, SkillId, playerMaxHealth } from "../../shared/definitions/skills.js";
21import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js";
22import { mobComponents } from "../src/simulation/spawners.js";
23import { MobId } from "../../shared/definitions/mobs.js";
24import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js";
25
26test("enforceMinimumSkillLevels raises Health to 10 and leaves higher XP alone", () =>
27{
28 const low = { xp: { health: 0, strength: 0 } };
29 assert.equal(enforceMinimumSkillLevels(low), true);
30 assert.equal(skillLevel(low, SkillId.health), 10);
31 assert.equal(skillLevel(low, SkillId.strength), 1);
32 assert.equal(skillLevel(low, SkillId.stamina), 5);
33
34 const high = { xp: { health: totalXpForLevel(12), strength: totalXpForLevel(5), stamina: totalXpForLevel(5) } };
35 assert.equal(enforceMinimumSkillLevels(high), false);
36 assert.equal(skillLevel(high, SkillId.health), 12);
37 assert.equal(skillLevel(high, SkillId.strength), 5);
38});
39
40test("login floors under-minimum skills and persists them", () =>
41{
42 const harness = makeSimulation({ workerCount: 1 });
43 try
44 {
45 const character = makeCharacter(harness.stateDb);
46 harness.stateDb.db.prepare("UPDATE character_bags SET skills_blob = ? WHERE character_id = ?")
47 .run(JSON.stringify({ xp: { health: 3, strength: 0 } }), character.character_id);
48
49 harness.simulation.spawnCharacter(1, character);
50 const stored = harness.stateDb.getSkills(character.character_id);
51 assert.equal(skillLevel(stored, SkillId.health), 10);
52 assert.equal(skillLevel(stored, SkillId.strength), 1);
53 assert.equal(skillLevel(stored, SkillId.defence), 1);
54 assert.equal(skillLevel(stored, SkillId.stamina), 5);
55 }
56 finally
57 { harness.close(); }
58});
59
60test("normalizeSkillXp floors Health to the level-10 starting XP", () =>
61{
62 assert.equal(normalizeSkillXp({}).health, totalXpForLevel(10));
63 assert.equal(normalizeSkillXp({ health: 14 }).health, totalXpForLevel(10));
64 assert.equal(normalizeSkillXp({ health: totalXpForLevel(11) }).health, totalXpForLevel(11));
65});
66
67test("legacy empty skills_blob loads as Health level 10 max HP", () =>
68{
69 const harness = makeSimulation({ workerCount: 1 });
70 try
71 {
72 const character = makeCharacter(harness.stateDb);
73 harness.stateDb.db.prepare("UPDATE character_bags SET skills_blob = ? WHERE character_id = ?")
74 .run("{}", character.character_id);
75 const skills = harness.stateDb.getSkills(character.character_id);
76 assert.equal(skillLevel(skills, SkillId.health), 10);
77 assert.equal(playerMaxHealth(skills), 10);
78 const stored = JSON.parse(
79 harness.stateDb.db.prepare("SELECT skills_blob FROM character_bags WHERE character_id = ?")
80 .get(character.character_id).skills_blob
81 );
82 assert.ok((stored.xp?.health ?? stored.health) >= totalXpForLevel(10));
83 }
84 finally
85 { harness.close(); }
86});
87
88test("xp to advance doubles each level starting at 50", () =>
89{
90 assert.equal(xpToAdvanceFromLevel(1), 50);
91 assert.equal(xpToAdvanceFromLevel(2), 100);
92 assert.equal(xpToAdvanceFromLevel(3), 200);
93 assert.equal(xpToAdvanceFromLevel(99), 0);
94});
95
96test("level is derived from total xp", () =>
97{
98 assert.equal(levelFromTotalXp(0), 1);
99 assert.equal(levelFromTotalXp(SKILL_XP_BASE - 1), 1);
100 assert.equal(levelFromTotalXp(SKILL_XP_BASE), 2);
101 assert.equal(totalXpForLevel(2), SKILL_XP_BASE);
102 assert.equal(totalXpForLevel(3), SKILL_XP_BASE + xpToAdvanceFromLevel(2));
103});
104
105test("skills cap at level 99", () =>
106{
107 const capped = totalXpForLevel(100) - 1;
108 assert.equal(levelFromTotalXp(capped), 99);
109 const progress = xpProgress(capped);
110 assert.equal(progress.level, 99);
111 assert.equal(progress.xpToNext, 0);
112});
113
114test("xp progress reports in-level remainder", () =>
115{
116 const progress = xpProgress(SKILL_XP_BASE + 25);
117 assert.equal(progress.level, 2);
118 assert.equal(progress.xpInLevel, 25);
119 assert.equal(progress.xpToNext, xpToAdvanceFromLevel(2));
120});
121
122test("normalizeSkillXp fills every skill id", () =>
123{
124 const xp = normalizeSkillXp({ [SkillId.strength]: 42 });
125 assert.equal(xp[SkillId.strength], 42);
126 assert.equal(xp[SkillId.magic], 0);
127});
128
129test("skillLevel reads player xp and mob levels", () =>
130{
131 assert.equal(skillLevel(normalizePlayerSkills(null), SkillId.clubfighting), 1);
132 assert.equal(skillLevel({ xp: { [SkillId.strength]: SKILL_XP_BASE } }, SkillId.strength), 2);
133 assert.equal(skillLevel({ levels: { [SkillId.unarmed]: 5 } }, SkillId.unarmed), 5);
134});
135
136test("new characters start with Health level 10", () =>
137{
138 const skills = defaultPlayerSkills();
139 assert.equal(skillLevel(skills, SkillId.health), 10);
140 assert.equal(skillLevel(skills, SkillId.strength), 1);
141 assert.equal(playerMaxHealth(skills), 10);
142});
143
144test("publicSkillsView exposes progress fields for the client", () =>
145{
146 const view = publicSkillsView(normalizePlayerSkills({ [SkillId.clubfighting]: 50 }));
147 const club = view.find((entry) => entry.skillId === SkillId.clubfighting);
148 assert.equal(club.level, 2);
149 assert.equal(club.xp, 0);
150 assert.equal(club.xpToNext, xpToAdvanceFromLevel(2));
151 assert.match(club.icon, /clubfighting\.svg$/);
152});
153
154test("addSkillXp mutates the player skills component", () =>
155{
156 const skills = normalizePlayerSkills(null);
157 addSkillXp(skills, SkillId.strength, 10);
158 assert.equal(skills.xp[SkillId.strength], 10);
159});
160
161test("applySkillXp reports a level-up when XP crosses a threshold", () =>
162{
163 const skills = normalizePlayerSkills(null);
164 const result = applySkillXp(skills, SkillId.strength, SKILL_XP_BASE);
165 assert.equal(result.changed, true);
166 assert.equal(result.levelUp?.skillId, SkillId.strength);
167 assert.equal(result.levelUp?.level, 2);
168 assert.match(result.levelUp?.icon, /strength\.svg$/);
169});
170
171test("combat hit xp is split equally", () =>
172{
173 assert.equal(combatHitXpPerSkill(), 5);
174});
175
176test("successful player melee hits award split xp", () =>
177{
178 const harness = makeSimulation({ workerCount: 1 });
179 const random = Math.random;
180 Math.random = () => 0;
181 try
182 {
183 const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 });
184 const playerId = harness.simulation.spawnCharacter(1, character);
185 const ratId = harness.simulation.spawnEntity(mobComponents({
186 mobTypeId: MobId.rat,
187 tileX: 9,
188 tileY: 8
189 }));
190 const worker = harness.simulation.workers[0];
191 const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(playerId));
192 const events = resolveMeleeSwing(worker.world, playerId, ratId, profile, Date.now());
193 const updated = events.find((event) => event.type === "skills-updated");
194 assert.ok(updated);
195 assert.equal(updated.stored[profile.hitSkillId], combatHitXpPerSkill());
196 assert.equal(updated.stored[profile.damageSkillId], combatHitXpPerSkill());
197 }
198 finally
199 {
200 Math.random = random;
201 harness.close();
202 }
203});
204
205test("skills persist when a character despawns", () =>
206{
207 const harness = makeSimulation({ workerCount: 1 });
208 try
209 {
210 const character = makeCharacter(harness.stateDb);
211 const playerId = harness.simulation.spawnCharacter(1, character);
212 const worker = harness.simulation.workers[0];
213 const skills = worker.world.ecs.get(worker.world.ecs.handleOf(playerId), "skills");
214 skills.xp[SkillId.strength] = SKILL_XP_BASE + 10;
215 harness.simulation.despawnCharacter(character.character_id);
216 const stored = harness.stateDb.getSkills(character.character_id);
217 assert.equal(stored.xp[SkillId.strength], SKILL_XP_BASE + 10);
218 }
219 finally
220 { harness.close(); }
221});