BlueGit

timfa questward

Files

questward / server / tests / skills.test.js

skills.test.js

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