BlueGit

timfa questward

Files

questward / server / tests / skills.test.js

skills.test.js

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