mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-16 17:09:09 +00:00
fix(gamification): normalize XP progress inputs (#16231)
XP progress inputs are normalized the same way on every path.
Boarded on release/v3.8.52 at 5b16041b73. Focused tests: 344 passed. File-size kept only the inherited executeTargetAttempt.ts overage (1288 > 1284). This pull request does not touch that file.
⚠️ base-red inherited: #15306
Fixes #16202.
This commit is contained in:
committed by
GitHub
parent
cd12f8d341
commit
e01340c854
1
changelog.d/fixes/16202-xp-progress-inputs.md
Normal file
1
changelog.d/fixes/16202-xp-progress-inputs.md
Normal file
@@ -0,0 +1 @@
|
||||
- **Gamification:** Keep XP-to-next-level finite for non-finite totals and consistent with the level calculation's safe-integer cap, while preserving finite negative XP debt and normal level boundaries. Fixes #16202.
|
||||
@@ -74,6 +74,8 @@ export function calculateLevel(totalXp: number): number {
|
||||
|
||||
/**
|
||||
* Get XP needed to reach the next level from the current total XP.
|
||||
* Non-finite totals start at zero; positive totals share calculateLevel's safe
|
||||
* integer cap. Finite negative totals retain their existing XP debt.
|
||||
*
|
||||
* @param totalXp - Current total XP
|
||||
* @returns Remaining XP until next level-up
|
||||
@@ -83,9 +85,10 @@ export function calculateLevel(totalXp: number): number {
|
||||
* xpToNextLevel(5000) // XP needed from 5000 to next level
|
||||
*/
|
||||
export function xpToNextLevel(totalXp: number): number {
|
||||
const currentLevel = calculateLevel(totalXp);
|
||||
const boundedXp = Number.isFinite(totalXp) ? Math.min(totalXp, Number.MAX_SAFE_INTEGER) : 0;
|
||||
const currentLevel = calculateLevel(boundedXp);
|
||||
const nextLevelXp = cumulativeXpForLevel(currentLevel + 1);
|
||||
return nextLevelXp - totalXp;
|
||||
return nextLevelXp - boundedXp;
|
||||
}
|
||||
|
||||
// ─── Level Titles & Tiers ────────────────────────────────────────────────────
|
||||
|
||||
@@ -224,11 +224,11 @@ test("xp: level titles and tiers at every boundary", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("characterization: xp currently maps non-finite totals to level 1 and leaks NaN/-Infinity", () => {
|
||||
test("xp: non-finite totals consistently start from level 1 with 282 XP remaining", () => {
|
||||
assert.equal(xp.calculateLevel(Number.NaN), 1);
|
||||
assert.equal(xp.calculateLevel(Number.POSITIVE_INFINITY), 1);
|
||||
assert.ok(Number.isNaN(xp.xpToNextLevel(Number.NaN)));
|
||||
assert.equal(xp.xpToNextLevel(Number.POSITIVE_INFINITY), Number.NEGATIVE_INFINITY);
|
||||
assert.equal(xp.xpToNextLevel(Number.NaN), 282);
|
||||
assert.equal(xp.xpToNextLevel(Number.POSITIVE_INFINITY), 282);
|
||||
});
|
||||
|
||||
// ─── (b) Contracts — award pipeline (deterministic fixture) ──────────────────
|
||||
|
||||
58
tests/unit/gamification/xp-progress-inputs-16202.test.ts
Normal file
58
tests/unit/gamification/xp-progress-inputs-16202.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { calculateLevel, xpToNextLevel } from "../../../src/lib/gamification/xp.ts";
|
||||
|
||||
for (const [label, totalXp] of [
|
||||
["NaN", Number.NaN],
|
||||
["positive infinity", Number.POSITIVE_INFINITY],
|
||||
["negative infinity", Number.NEGATIVE_INFINITY],
|
||||
] as const) {
|
||||
test(`non-finite XP (${label}) needs 282 XP from level one`, () => {
|
||||
assert.equal(calculateLevel(totalXp), 1);
|
||||
assert.equal(xpToNextLevel(totalXp), 282);
|
||||
});
|
||||
}
|
||||
|
||||
for (const [label, totalXp] of [
|
||||
["first unsafe integer", Number.MAX_SAFE_INTEGER + 1],
|
||||
["largest finite number", Number.MAX_VALUE],
|
||||
] as const) {
|
||||
test(`XP beyond the safe range (${label}) shares the capped level's progress`, () => {
|
||||
const cappedProgress = xpToNextLevel(Number.MAX_SAFE_INTEGER);
|
||||
const progress = xpToNextLevel(totalXp);
|
||||
assert.ok(Number.isFinite(cappedProgress) && cappedProgress >= 0);
|
||||
assert.equal(calculateLevel(totalXp), calculateLevel(Number.MAX_SAFE_INTEGER));
|
||||
assert.equal(progress, cappedProgress);
|
||||
assert.ok(Number.isFinite(progress) && progress >= 0);
|
||||
});
|
||||
}
|
||||
|
||||
const progressExamples = [
|
||||
{ label: "zero", totalXp: 0, expected: 282 },
|
||||
{ label: "negative zero", totalXp: -0, expected: 282 },
|
||||
{ label: "five XP debt", totalXp: -5, expected: 287 },
|
||||
{ label: "one hundred XP debt", totalXp: -100, expected: 382 },
|
||||
{ label: "one before level two", totalXp: 281, expected: 1 },
|
||||
{ label: "fraction before level two", totalXp: 281.5, expected: 0.5 },
|
||||
{ label: "level two boundary", totalXp: 282, expected: 519 },
|
||||
{ label: "one into level two", totalXp: 283, expected: 518 },
|
||||
{ label: "one before level three", totalXp: 800, expected: 1 },
|
||||
{ label: "level three boundary", totalXp: 801, expected: 800 },
|
||||
{ label: "one into level three", totalXp: 802, expected: 799 },
|
||||
{ label: "five thousand", totalXp: 5000, expected: 1040 },
|
||||
{ label: "one before level ten", totalXp: 14163, expected: 1 },
|
||||
{ label: "level ten boundary", totalXp: 14164, expected: 3648 },
|
||||
{ label: "one million", totalXp: 1_000_000, expected: 2661 },
|
||||
];
|
||||
|
||||
for (const { label, totalXp, expected } of progressExamples) {
|
||||
test(`finite XP preserves the existing curve: ${label}`, () => {
|
||||
assert.equal(xpToNextLevel(totalXp), expected);
|
||||
});
|
||||
}
|
||||
|
||||
test("the largest finite XP debt still has a finite, non-negative distance", () => {
|
||||
const progress = xpToNextLevel(-Number.MAX_VALUE);
|
||||
assert.equal(calculateLevel(-Number.MAX_VALUE), 1);
|
||||
assert.ok(Number.isFinite(progress) && progress >= 0);
|
||||
});
|
||||
Reference in New Issue
Block a user