rwadurian/backend/services/referral-service/test/domain/services/leaderboard-calculation.ser...

86 lines
3.0 KiB
TypeScript

import { LeaderboardCalculationService } from '../../../src/domain/services/leaderboard-calculation.service';
import { LeaderboardScore } from '../../../src/domain/value-objects';
describe('LeaderboardCalculationService', () => {
let service: LeaderboardCalculationService;
beforeEach(() => {
service = new LeaderboardCalculationService();
});
describe('calculateScore', () => {
it('should calculate score correctly', () => {
const stats = [
{ referralId: 1n, teamCount: 30 },
{ referralId: 2n, teamCount: 40 },
{ referralId: 3n, teamCount: 30 },
];
const score = service.calculateScore(100, stats);
expect(score.totalTeamCount).toBe(100);
expect(score.maxDirectTeamCount).toBe(40);
expect(score.score).toBe(60);
});
it('should return zero score for empty teams', () => {
const score = service.calculateScore(0, []);
expect(score.score).toBe(0);
});
});
describe('updateScoreOnPlanting', () => {
it('should update score when planting added to direct referral team', () => {
const currentScore = LeaderboardScore.calculate(100, [30, 40, 30]);
const stats = [
{ referralId: 1n, teamCount: 30 },
{ referralId: 2n, teamCount: 40 },
{ referralId: 3n, teamCount: 30 },
];
const newScore = service.updateScoreOnPlanting(currentScore, 10, 1n, stats);
// New total = 110, new max = 40 (unchanged), score = 70
expect(newScore.totalTeamCount).toBe(110);
expect(newScore.score).toBe(70);
});
it('should update score when max team increases', () => {
const currentScore = LeaderboardScore.calculate(100, [30, 40, 30]);
const stats = [
{ referralId: 1n, teamCount: 30 },
{ referralId: 2n, teamCount: 40 },
{ referralId: 3n, teamCount: 30 },
];
const newScore = service.updateScoreOnPlanting(currentScore, 20, 2n, stats);
// New total = 120, new max = 60, score = 60
expect(newScore.totalTeamCount).toBe(120);
expect(newScore.maxDirectTeamCount).toBe(60);
expect(newScore.score).toBe(60);
});
});
describe('compareRank', () => {
it('should correctly compare for ranking', () => {
const scoreA = LeaderboardScore.calculate(100, [40, 30, 30]); // score = 60
const scoreB = LeaderboardScore.calculate(80, [30, 30, 20]); // score = 50
expect(service.compareRank(scoreA, scoreB)).toBeLessThan(0); // A ranks higher
});
});
describe('validateScore', () => {
it('should return true for valid score', () => {
const score = LeaderboardScore.calculate(100, [40, 30, 30]);
expect(service.validateScore(score, 100, 40)).toBe(true);
});
it('should return false for invalid score', () => {
const score = LeaderboardScore.calculate(100, [40, 30, 30]);
expect(service.validateScore(score, 100, 50)).toBe(false);
});
});
});