import test from "node:test"; import assert from "node:assert/strict"; import { SKILL_XP_BASE, xpToAdvanceFromLevel, totalXpForLevel, maxTotalSkillXp, xpProgress, levelFromTotalXp, combatDamageXp, spellCastXpFromDifficulty } from "../../shared/skills/progression.js"; import { normalizeSkillXp, normalizePlayerSkills, defaultPlayerSkills, addSkillXp, applySkillXp, publicSkillsView, enforceMinimumSkillLevels } from "../../shared/skills/state.js"; import { skillLevel, SkillId, playerMaxHealth } from "../../shared/definitions/skills.js"; import { resolveMeleeSwing, attackProfileOf } from "../src/combat/service.js"; import { mobComponents } from "../src/simulation/spawners.js"; import { MobId } from "../../shared/definitions/mobs.js"; import { makeCharacter, makeSimulation } from "./helpers/simulationHarness.js"; import { installDamagingCombatRng } from "./helpers/combatRng.js"; test("enforceMinimumSkillLevels raises Health to 5 and leaves higher XP alone", () => { const low = { xp: { health: 0, strength: 0 } }; assert.equal(enforceMinimumSkillLevels(low), true); assert.equal(skillLevel(low, SkillId.health), 5); assert.equal(skillLevel(low, SkillId.strength), 1); assert.equal(skillLevel(low, SkillId.stamina), 5); const high = { xp: { health: totalXpForLevel(12), strength: totalXpForLevel(5), stamina: totalXpForLevel(5) } }; assert.equal(enforceMinimumSkillLevels(high), false); assert.equal(skillLevel(high, SkillId.health), 12); assert.equal(skillLevel(high, SkillId.strength), 5); }); test("login floors under-minimum skills and persists them", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb); harness.stateDb.db.prepare("UPDATE character_bags SET skills_blob = ? WHERE character_id = ?") .run(JSON.stringify({ xp: { health: 3, strength: 0 } }), character.character_id); harness.simulation.spawnCharacter(1, character); const stored = harness.stateDb.getSkills(character.character_id); assert.equal(skillLevel(stored, SkillId.health), 5); assert.equal(skillLevel(stored, SkillId.strength), 1); assert.equal(skillLevel(stored, SkillId.defence), 1); assert.equal(skillLevel(stored, SkillId.stamina), 5); } finally { harness.close(); } }); test("normalizeSkillXp floors Health to the level-5 starting XP", () => { assert.equal(normalizeSkillXp({}).health, totalXpForLevel(5)); assert.equal(normalizeSkillXp({ health: 14 }).health, totalXpForLevel(5)); assert.equal(normalizeSkillXp({ health: totalXpForLevel(11) }).health, totalXpForLevel(11)); }); test("legacy empty skills_blob loads as Health level 5 max HP", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb); harness.stateDb.db.prepare("UPDATE character_bags SET skills_blob = ? WHERE character_id = ?") .run("{}", character.character_id); const skills = harness.stateDb.getSkills(character.character_id); assert.equal(skillLevel(skills, SkillId.health), 5); assert.equal(playerMaxHealth(skills), 5); const stored = JSON.parse( harness.stateDb.db.prepare("SELECT skills_blob FROM character_bags WHERE character_id = ?") .get(character.character_id).skills_blob ); assert.ok((stored.xp?.health ?? stored.health) >= totalXpForLevel(5)); } finally { harness.close(); } }); test("xp to advance grows 10% per level starting at 50", () => { assert.equal(xpToAdvanceFromLevel(1), 50); assert.equal(xpToAdvanceFromLevel(2), 55); assert.equal(xpToAdvanceFromLevel(3), 60); assert.equal(xpToAdvanceFromLevel(99), 0); }); test("level is derived from total xp", () => { assert.equal(levelFromTotalXp(0), 1); assert.equal(levelFromTotalXp(SKILL_XP_BASE - 1), 1); assert.equal(levelFromTotalXp(SKILL_XP_BASE), 2); assert.equal(totalXpForLevel(2), SKILL_XP_BASE); assert.equal(totalXpForLevel(3), SKILL_XP_BASE + xpToAdvanceFromLevel(2)); }); test("skills cap at level 99", () => { const capped = maxTotalSkillXp(); assert.equal(levelFromTotalXp(capped), 99); const progress = xpProgress(capped); assert.equal(progress.level, 99); assert.equal(progress.xpToNext, 0); }); test("xp progress reports in-level remainder", () => { const progress = xpProgress(SKILL_XP_BASE + 25); assert.equal(progress.level, 2); assert.equal(progress.xpInLevel, 25); assert.equal(progress.xpToNext, xpToAdvanceFromLevel(2)); }); test("normalizeSkillXp fills every skill id", () => { const xp = normalizeSkillXp({ [SkillId.strength]: 42 }); assert.equal(xp[SkillId.strength], 42); assert.equal(xp[SkillId.magic], 0); }); test("skillLevel reads player xp and mob levels", () => { assert.equal(skillLevel(normalizePlayerSkills(null), SkillId.clubfighting), 1); assert.equal(skillLevel({ xp: { [SkillId.strength]: SKILL_XP_BASE } }, SkillId.strength), 2); assert.equal(skillLevel({ levels: { [SkillId.unarmed]: 5 } }, SkillId.unarmed), 5); }); test("new characters start with Health level 5", () => { const skills = defaultPlayerSkills(); assert.equal(skillLevel(skills, SkillId.health), 5); assert.equal(skillLevel(skills, SkillId.strength), 1); assert.equal(playerMaxHealth(skills), 5); }); test("publicSkillsView exposes progress fields for the client", () => { const view = publicSkillsView(normalizePlayerSkills({ [SkillId.clubfighting]: 50 })); const club = view.find((entry) => entry.skillId === SkillId.clubfighting); assert.equal(club.level, 2); assert.equal(club.xp, 0); assert.equal(club.xpToNext, xpToAdvanceFromLevel(2)); assert.match(club.icon, /clubfighting\.svg$/); }); test("addSkillXp mutates the player skills component", () => { const skills = normalizePlayerSkills(null); addSkillXp(skills, SkillId.strength, 10); assert.equal(skills.xp[SkillId.strength], 10); }); test("applySkillXp reports a level-up when XP crosses a threshold", () => { const skills = normalizePlayerSkills(null); const result = applySkillXp(skills, SkillId.strength, SKILL_XP_BASE); assert.equal(result.changed, true); assert.equal(result.levelUp?.skillId, SkillId.strength); assert.equal(result.levelUp?.level, 2); assert.match(result.levelUp?.icon, /strength\.svg$/); }); test("combat xp equals damage dealt to hit and damage skills", () => { assert.equal(combatDamageXp(7), 7); assert.equal(combatDamageXp(0), 0); assert.equal(spellCastXpFromDifficulty(4), 8); }); test("successful player melee hits award xp equal to damage", () => { const harness = makeSimulation({ workerCount: 1 }); const restore = installDamagingCombatRng(); try { const character = makeCharacter(harness.stateDb, { tileX: 8, tileY: 8 }); const playerId = harness.simulation.spawnCharacter(1, character); const ratId = harness.simulation.spawnEntity(mobComponents({ mobTypeId: MobId.rat, tileX: 9, tileY: 8 })); const worker = harness.simulation.workers[0]; const profile = attackProfileOf(worker.world.ecs, worker.world.ecs.handleOf(playerId)); const events = resolveMeleeSwing(worker.world, playerId, ratId, profile, Date.now()); const hit = events.find((event) => event.type === "combat-hit"); const updated = events.find((event) => event.type === "skills-updated"); assert.ok(hit); assert.ok(updated); const xp = combatDamageXp(hit.damage); assert.equal(updated.stored[profile.hitSkillId], xp); assert.equal(updated.stored[profile.damageSkillId], xp); } finally { restore(); harness.close(); } }); test("skills persist when a character despawns", () => { const harness = makeSimulation({ workerCount: 1 }); try { const character = makeCharacter(harness.stateDb); const playerId = harness.simulation.spawnCharacter(1, character); const worker = harness.simulation.workers[0]; const skills = worker.world.ecs.get(worker.world.ecs.handleOf(playerId), "skills"); skills.xp[SkillId.strength] = SKILL_XP_BASE + 10; harness.simulation.despawnCharacter(character.character_id); const stored = harness.stateDb.getSkills(character.character_id); assert.equal(stored.xp[SkillId.strength], SKILL_XP_BASE + 10); } finally { harness.close(); } });