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