BlueGit

timfa questward

Files

questward / server / tests / skills.test.js

skills.test.js

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