Skip to content

fix(gamification): use canonical getRank for rank badge criterion - #15742

Merged
diegosouzapw merged 1 commit into
diegosouzapw:release/v3.8.52from
SoloDrex52:fix/gamification-getrank-dedupe
Oct 8, 2026
Merged

diegosouzapw merged 1 commit into
diegosouzapw:release/v3.8.52from
SoloDrex52:fix/gamification-getrank-dedupe

Conversation

@SoloDrex52

Copy link
Copy Markdown
Contributor

⚠️ base-red inherited: #15306

Summary

src/lib/gamification/badges.ts carried a private copy of the leaderboard rank query that duplicated getRank() in src/lib/db/gamification.ts, with one behavioral difference: the local copy returned Infinity for a key with no leaderboard entry, while the canonical function returns 0.

This PR drops the local copy and has the rank badge criterion call the canonical getRank(). Because the canonical function reports "unranked" as 0, the criterion now checks rank > 0 && rank <= threshold — a straight swap would have let 0 <= threshold award rank badges (e.g. community-leader) to every key that never scored.

Tests

New: tests/unit/gamification/badge-rank-criteria.test.ts

  • unranked key → getRank() === 0 and community-leader is not unlocked
  • key at rank 1 → community-leader is unlocked

Validation (Hard Rule #18):

  • node --import tsx/esm --test tests/unit/gamification/badge-rank-criteria.test.ts → 2/2 pass
  • Mutation check: removing the rank > 0 && guard makes the unranked test fail (1 fail / 1 pass); guard restored
  • node --import tsx/esm --test tests/unit/gamification/*.test.ts → 120/120 pass (18 files, incl. leaderboard.test.ts, badges.test.ts)

badges.ts carried its own copy of the leaderboard rank query, duplicating
db/gamification getRank() but returning Infinity for unranked keys instead
of 0. Drop the local copy and call the canonical function, treating 0 as
unranked so `0 <= threshold` cannot award rank badges to keys that never
scored.

Adds tests/unit/gamification/badge-rank-criteria.test.ts covering both the
unranked and ranked-within-threshold cases.
@SoloDrex52

Copy link
Copy Markdown
Contributor Author

Hi @diegosouzapw — this is my first PR from a fork, so the CI, Quality Gates, semgrep and API Route Typecheck workflows are waiting on maintainer approval (action_required). Could you approve the workflow runs when you get a chance? Thanks!

@diegosouzapw
diegosouzapw merged commit a813cee into diegosouzapw:release/v3.8.52 Oct 8, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants