Description
The leaderboard ranking logic in scripts/sync-leaderboard.js currently lacks dedicated unit test coverage.
The functions:
stableSortByScore()
assignCompetitionRanks()
are responsible for determining leaderboard ordering and ranking behavior, but they are currently embedded inside the sync script and are not easily testable.
Proposed Changes
Refactor
Extract ranking utilities into a reusable module (for example: scripts/utils/ranking.js) or export them from the current file so they can be imported by tests.
Add Test Coverage
stableSortByScore()
- sorts users by score in descending order
- handles equal scores consistently
- preserves expected ordering for mixed scores
- verifies tiebreaker behavior
assignCompetitionRanks()
- assigns rank 1 to highest score
- assigns identical ranks for tied scores
- skips rank numbers correctly after ties
- handles empty datasets
- handles single-user datasets
Benefits
- Prevents regressions in leaderboard calculations
- Makes ranking behavior easier to maintain
- Improves confidence during future refactors
- Encourages separation of business logic from script execution
Notes
This is primarily a testing contribution and should not change existing leaderboard behavior.
Description
The leaderboard ranking logic in
scripts/sync-leaderboard.jscurrently lacks dedicated unit test coverage.The functions:
stableSortByScore()assignCompetitionRanks()are responsible for determining leaderboard ordering and ranking behavior, but they are currently embedded inside the sync script and are not easily testable.
Proposed Changes
Refactor
Extract ranking utilities into a reusable module (for example:
scripts/utils/ranking.js) or export them from the current file so they can be imported by tests.Add Test Coverage
stableSortByScore()
assignCompetitionRanks()
Benefits
Notes
This is primarily a testing contribution and should not change existing leaderboard behavior.