Skip to content

[TEST] : Add unit tests for leaderboard ranking utilities #329

Description

@SujalMahapatra

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions