siegebreak/services/test/rating.test.ts
caelum0x 3a8fec3ed3 feat: player rating (Elo) + XP/gold progression on match completion
- schema: add Player.rating (default 1000) + migration add_player_rating.
- lib/rating.ts: pure Elo update, outcome->score, XP/gold rewards, and
  level-from-XP; fully unit-tested.
- matches: on completion, load participants' rating/xp, apply Elo (1v1),
  and in ONE transaction persist the match + each player's new
  rating/xp/level + wallet gold (upsert). Returns per-player new ratings;
  422 on an unknown player.
- matchmaking: queue at the player's persisted rating instead of the flat
  DEFAULT_MMR constant.
- tests: rating unit tests; matches route tests cover Elo/rewards/422;
  integration proves rating/xp/gold persist (zero-sum) and that the queue
  is scored by real rating. 52 unit + 9 integration green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-25 18:46:11 +03:00

58 lines
1.8 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { resultToScore, computeElo, matchRewards, levelForXp } from "../src/lib/rating";
describe("resultToScore", () => {
it("maps outcomes to Elo scores", () => {
expect(resultToScore("win")).toBe(1);
expect(resultToScore("draw")).toBe(0.5);
expect(resultToScore("loss")).toBe(0);
});
});
describe("computeElo", () => {
it("moves equal-rated players by half K on a decisive result", () => {
// Equal ratings: expected = 0.5, so K*(1-0.5)=16 to the winner, -16 loser.
const { a, b } = computeElo(1000, 1000, 1);
expect(a).toBe(1016);
expect(b).toBe(984);
});
it("leaves equal-rated players unchanged on a draw", () => {
expect(computeElo(1000, 1000, 0.5)).toEqual({ a: 1000, b: 1000 });
});
it("awards fewer points when a favorite beats an underdog", () => {
const { a } = computeElo(1400, 1000, 1); // heavy favorite wins
expect(a - 1400).toBeGreaterThan(0);
expect(a - 1400).toBeLessThan(16); // less than the equal-rating gain
});
it("is zero-sum: points the winner gains, the loser loses", () => {
const ra = 1200;
const rb = 1050;
const { a, b } = computeElo(ra, rb, 1);
expect(a - ra).toBe(rb - b);
});
});
describe("matchRewards", () => {
it("pays out most for a win, least for a loss, and always something", () => {
expect(matchRewards("win")).toEqual({ xp: 30, gold: 10 });
expect(matchRewards("draw")).toEqual({ xp: 15, gold: 5 });
expect(matchRewards("loss")).toEqual({ xp: 10, gold: 2 });
});
});
describe("levelForXp", () => {
it("derives a 1-based level per 1000 XP", () => {
expect(levelForXp(0)).toBe(1);
expect(levelForXp(999)).toBe(1);
expect(levelForXp(1000)).toBe(2);
expect(levelForXp(2500)).toBe(3);
});
it("never returns a level below 1", () => {
expect(levelForXp(-50)).toBe(1);
});
});