DEV Community

Cover image for I'm Not on the DEV Team. I Still Debugged Their Missing Badges From Public Code. (And My First Test Was Wrong.)
Dhruv Jani
Dhruv Jani Subscriber

Posted on AI-assisted

I'm Not on the DEV Team. I Still Debugged Their Missing Badges From Public Code. (And My First Test Was Wrong.)

A humbling reminder that passing tests can lie

I’m not on the DEV team. I don’t have an admin panel, production database access, or internal Slack. Everything I know about how DEV works comes from reading the public forem/forem repository. 🕵️‍♂️

Today, that was enough to diagnose why nobody on DEV was receiving multi-commit contributor badges, ship the service fix upstream, and get a "Thank you for this" and merge from @benhalpern Sir himself. 🚀

4x DEV Contributor Club badge
The 4x DEV Contributor Club badge: "Awarded for 4+ merged commits to Forem — Thank you so much for your contributions!"

It also taught me a humbling lesson about tests that pass for the wrong reason. 🧪


1. The Itch: 4 Merged PRs, 1 Badge 🤔

Over the past few days, I went on a contribution sprint in forem/forem and merged four pull requests:

  • #24003 — Docs: fix broken Kamal TOC anchor and index missing guides
  • #24056 — A11y: remove duplicate aria-describedby attributes in admin forms
  • #24094 — A11y: remove dangling aria-describedby references in creator settings
  • #24095 — Views/Liquids: quote src and add missing alt in user_subscription tag

My 1x (dev-contributor) badge showed up right away after PR #1. ✅

When PR #4 merged, I knew from config/schedule.yml that BadgeAchievements::BadgeAwardWorker runs every hour at :20 past the hour (cron: "20 * * * *"). I checked GitHub’s Contributors API (/repos/forem/forem/contributors) and confirmed my account was sitting on page 2 with contributions: 4. Because Forem sets Octokit.auto_paginate = true, the worker was definitely reaching page 2.

One hourly cron passed. Then two. Then three. Still no 4x-commit-club badge. 💀

Mr. Bean waiting and checking his watch
Me at :20 past the hour, every hour, refreshing my profile like it owes me money.

Instead of refreshing my profile for another 24 hours, I cracked open app/services/badges/award_contributor_from_github.rb. 🔍


2. Reading the Code Like a Detective 🕵️‍♂️

Here is how Badges::AwardContributorFromGithub looked on main:

BADGE_SLUGS = {
  "dev-contributor": 1,
  "4x-commit-club": 4,
  "8x-commit-club": 8,
  "16x-commit-club": 16,
  "32x-commit-club": 32
}.freeze
Enter fullscreen mode Exit fullscreen mode

When I compared award_single_commit_contributors (which awarded my 1x badge) against award_multi_commit_contributors (which handles 4x, 8x, 16x, and 32x), three bugs jumped right off the screen:

🐛 Problem 1: Silent Slug Drops

Each key in BADGE_SLUGS is resolved via Badge.id_for_slug(slug). If a slug in the production badges table doesn't match the hardcoded constant, id_for_slug returns nil. The service called .compact on the resulting hash and moved on. No error. No log line. Just a badge tier that quietly vanishes into the void. 👻

Because Forem only exposes resources :badges, only: [:index] without public /badges/:slug show routes, you can't query individual badge slugs directly. However, inspecting the public dev.to/badges catalog gave me a massive clue:

Badge #423 on /badges:

  • Title: 4x DEV Contributor Club
  • Description: 4x-commit-club

Wait—why was 4x-commit-club sitting in the description field while the title was 4x DEV Contributor Club? Because Badge#generate_slug parameterizes the title when no explicit slug is set (4x-dev-contributor-club), the stored slug column in production didn't match the hardcoded "4x-commit-club" constant at all! 💡

Charlie Day conspiracy board
Connecting Badge#generate_slug to DOM element IDs on /badges at 9 AM with zero database access.

💌 Problem 2: Missing Context Messages

Single-commit badges were awarded with:

BadgeAchievement
  .where(user_id: i.user_id, badge_id: dev_contributor_badge_id)
  .first_or_create(rewarding_context_message_markdown: msg)
Enter fullscreen mode Exit fullscreen mode

Multi-commit badges, however, were awarded with a bare .create:

BadgeAchievement.create(user_id: user_id, badge_id: slug_id)
Enter fullscreen mode Exit fullscreen mode

Even when a multi-commit badge was created, it omitted rewarding_context_message_markdown: msg ("Thank you so much for your contributions!"). Four times the commits, zero times the thank-you note. 😭

🔁 Problem 3: Missing Idempotency Guard

create_badges_for_user called BadgeAchievement.create(...) on every hourly run instead of .where(...).first_or_create(...). Every hour, it re-attempted already-earned badges for every contributor on the list, relying on ActiveRecord's uniqueness validation to fail silently. Worse, if a badge ever had allow_multiple_awards: true, bare .create would insert a duplicate achievement every hour—firing after_create_commit :notify_recipient and send_email_notification (flooding your inbox from [email protected]) each time. 📬💥


3. Filing the Issue & Shipping PR #24113 🛠️

I opened Issue #24114 laying out the slug mismatch on badge IDs 423–426, and paired it with PR #24113 to fix the service itself:

  1. ✅ Attached rewarding_context_message_markdown: msg to multi-commit badge awards.
  2. 🔒 Replaced bare BadgeAchievement.create with .where(...).first_or_create(...).
  3. 📊 Deferred slug lookup behind the GitHub auth provider check and added logging whenever configured slugs are missing from the database.

4. The Plot Twist in Code Review 🎬

The initial deep-dive review on @benhalpern Sir’s account was posted by Forem's automated review agent (OpenHands), and it did something awesome—it ran a read-only lookup against the badges table and confirmed my exact diagnosis from Issue #24114:

"I ran a read-only lookup on the badges table for the five BADGE_SLUGS keys. Only one of the five slugs resolves. The four Nx-commit-club slugs have no matching row, so create_badges_for_user skips them and nobody can earn them today. The issue reporter's guess is correct."

Bingo. 🎯

Even better, the review caught two subtle flaws in my PR that taught me more than the bug itself:

📢 Lesson A: My Rails.logger.warn Was Log Spam

I originally logged a Rails.logger.warn inside the loop for each missing slug. Why was that bad?

  1. Until the production slugs were aligned, DEV would log 4 warn lines every hour.
  2. Forem powers many independent communities. Most self-hosted Forems enable GitHub auth without seeding DEV-specific contributor badges. Five warn lines an hour for expected configuration turns warn into permanent background noise.

The Fix (5e36999): Collect all missing slugs and emit a single Rails.logger.info line per run. 🧹

🤡 Lesson B: My Regression Test Passed on main (For the Wrong Reason!)

To test idempotency, I wrote a spec that ran described_class.call twice and asserted that the user still had only 1 badge achievement for 4x-commit-club.

It passed! Green dots everywhere. 🟢

Except the review pointed out something brutal: that test also passed on origin/main without my fix.

Surprised Pikachu
When your shiny new regression test passes on main without your bugfix.

Why? The badge factory defaults to allow_multiple_awards: false. On main, the second BadgeAchievement.create hit validates :badge_id, uniqueness: { scope: :user_id, if: :single_award_badge? }, failed validation silently, and left the count at 1. My test wasn't testing first_or_create at all—it was testing ActiveRecord's uniqueness validator! 🤦‍♂️

The Fix (5e36999): I updated the spec to create the badge with allow_multiple_awards: true (where bare .create genuinely inserts duplicates on main) and asserted expect { described_class.call }.not_to change(BadgeAchievement, :count) on the second run.

After I pushed 5e36999 and all 34 checks went green, @benhalpern Sir stepped in personally, dropped a comment:

Ben Halpern approving PR #24113 with

That "Thank you for this" from @benhalpern Sir genuinely made my entire day! 🙌 And right after that, he merged #24113 into forem:main as d1b962e (my 5th merged PR!). 🎉

5 merged pull requests in forem/forem

Shortly after, the badge email from [email protected] hit my inbox and the 4x DEV Contributor Club badge landed on my profile—complete with the "Thank you so much for your contributions!" context message my PR wired up. The code is cleaner, the PR is merged, and the green 4x circuit board is finally home. 💚


💡 3 Takeaways I’d Tell Past Me

  1. 🔴 A regression test isn't real until you watch it fail on main. Before you celebrate a green spec, git stash your change and run the spec against main. If it still passes, you haven't written a regression test—you've written a test that agrees with you.
  2. 🪵 Log levels are an operator contract. warn means someone should act. If a platform supports optional data seeds, missing optional rows are info, not warn.
  3. 🔓 You don't need production access to debug production. You just need the public source code, a falsifiable hypothesis, and the curiosity to follow the trail.

Your turn: What’s the sneakiest time one of your tests passed for the completely wrong reason? Drop it in the comments 👇


Top comments (24)

Collapse
 
dj29 profile image
Dhruv Jani •

🐛 Plot twist: the bug wasn't the only thing I had to debug. My own test was wrong, too.

This started with four merged PRs and a missing badge. It turned into a dive through Forem's public code, a fix that got merged, and a code review that taught me why a passing test isn't always proof that your fix works.

Huge thanks to everyone who reviewed the PR and helped me catch what I'd missed!

Your turn: Have you ever had a test pass green while completely missing the bug it was supposed to catch? I'd love to hear that story. 👀

Collapse
 
dj29 profile image
Dhruv Jani •

And Sir @francistrdev, I'd love a comment in here because I know you've contributed to forem before and skilled in ruby on rails.

Don't want to bother you, only when you're free!😅

Collapse
 
ben profile image
Ben Halpern •

Thank you for this!

This bug has been around so long it had completely faded from everyone's consciounseness.

Picked as gem
Collapse
 
dj29 profile image
Dhruv Jani •

Thank you so much, Sir! That really means a lot. It was such an awesome rabbit hole to trace from the outside.
Also, your reply really made my day!
Always happy to support the DEV Community!

Have A Great Day!

Collapse
 
hanzla profile image
Hanzla Baig •

feat: Add post pinning support for organizations #24058

What type of PR is this? (check all applicable)

  • [ ] Refactor
  • [x] Feature
  • [ ] Bug Fix
  • [ ] Optimization
  • [ ] Documentation Update

Description

Extends the existing ProfilePin model (previously User-only) to also support Organization as a profile_type, reusing the polymorphic pin/unpin pattern already used on individual user profiles.

  • ProfilePin now validates Organization as a valid profile_type and checks that a pinned article belongs to the organization (via organization_id) when the profile is an Organization.
  • New OrganizationProfilePinsController (slug-scoped, reuses OrganizationAdminScoped for authorization) handles create/destroy of organization pins. Only org admins (or site admins) can pin/unpin.
  • stories_controller#handle_organization_index now loads the organization's pinned articles separately from the regular chronological feed and excludes them from it, mirroring assign_user_stories for users.
  • organizations/_main_feed renders a dedicated "Pinned" section above the regular feed when pinned posts exist, reusing the same badge/styling as the user profile pinned section.
  • Added a "Pin to Organization" section to the post management page, visible to org admins managing an org-owned post, to pin/unpin from there.
  • Up to 5 pins per organization (same cap as user profiles); reordering is not implemented, matching parity with the existing user-profile pin feature which also orders purely by published_at.

Related Tickets & Documents

  • Related Issue #24057
  • Closes #24057

QA Instructions, Screenshots, Recordings

  1. As an organization admin, go to /dashboard, open "Manage" on a post that belongs to your organization.
  2. Under the new "Pin to Organization" section, click "Pin to Organization".
  3. Visit the organization's page (/<org-slug>) — the post now appears in a "Pinned" card above the regular feed, and does not also appear in the regular feed below.
  4. Pin up to 5 posts; a 6th pin attempt is rejected.
  5. Unpin from either the "Manage" page; the post returns to the regular chronological feed.
  6. A non-admin org member or an unaffiliated user cannot pin/unpin via POST /:slug/profile_pins (raises Pundit::NotAuthorizedError).

UI accessibility checklist

  • [x] Semantic HTML implemented? (reuses existing _main_feed/single_story markup and existing pin badge markup)
  • [x] Keyboard operability supported? (plain <button type="submit"> form controls)
  • [ ] Checked with axe DevTools and addressed Critical and Serious issues?
  • [x] Color contrast tested?

Added/updated tests?

  • [x] Yes
    • spec/models/profile_pin_spec.rb: organization pin validity, ownership check, 5-pin cap.
    • spec/requests/organization_profile_pins_spec.rb: create/destroy as org admin, five-pin cap, rejecting pins for articles outside the org, authorization failures for member/outsider/signed-out user.
    • spec/requests/stories_index_spec.rb: organization page renders the pinned section and does not duplicate the pinned post in the regular feed.

[optional] Are there any post deployment tasks we need to perform?

None.

Collapse
 
annavi11arrea1 profile image
Anna Villarreal •

Thats so awesome! Good job!

Collapse
 
dj29 profile image
Dhruv Jani •

Thanks! Have A Great Day!

Collapse
 
nyaomaru profile image
nyaomaru •

Great work! Nice contribution 👍

Collapse
 
dj29 profile image
Dhruv Jani •

Thanks!😄
Have A Great Day!

Collapse
 
kaj_ilands profile image
Kaj •

Same class, from the other side. I published a fix for a 422 "Invalid authenticity token" on a scripted comment POST, then tested it by posting a comment. Green. Posted a second one to be sure. Green.

Then I ran the same recipe cold, fresh cookie jar, and it 422'd. The token I'd been grabbing off the article page belonged to a different form. It only ever worked because of state left over in a warm session.

The test wasn't wrong about the fix. It was wrong about the world. Same shape as your factory default: the thing that made it green was a default I didn't know I'd chosen.

Borrowed from your takeaway: for anything auth-shaped, "it passed" usually means "it passed with the session I happened to be holding." The regression run has to start from nobody. That's the git stash step, just for cookies.

(Token-side writeup, if the details are useful: dev.to/kaj_ilands/your-scripted-po...)

Collapse
 
amintacccp profile image
tamina •

The Lesson B section is the one I will keep. A test that passes on main without the fix is not a regression test at all — it is just agreeing with the current behaviour, and the factory default hiding that is exactly the kind of quiet escape route that is easy to miss. The git stash check is cheap insurance, and your point about warn versus info lands too: a warning that fires on expected configuration just trains everyone to ignore warnings.

Collapse
 
stefan_07d23f966225156b50 profile image
Stefan •

There's something pretty funny about earning your fifth merged PR by fixing the badge for your fourth 😄 The factory default was a sneaky one: the count was right, but the service was still attempting the duplicate. Nice example of why checking the final state alone can miss what went wrong.

Collapse
 
dj29 profile image
Dhruv Jani •

Sure is, for whole morning I was like where's the badge?😅
And then I went like let me give my PR #5 a legendary arc.😄

But it was a really nice day!

Have A Great Day!

Collapse
 
emilywoodsnyc profile image
Emily Woods •

The allow_multiple_awards change is the part I'd turn into a reusable test habit: name the other mechanism that could make the assertion pass, then choose a fixture that removes that escape route. In this buggy movie-search exercise, I'd include both a published and an unpublished movie matching the same search. A broken matcher that returns nothing must not accidentally pass the filter test.

Collapse
 
dj29 profile image
Dhruv Jani •

Spot on—explicitly closing the 'escape route' in the test fixture so the assertion can only pass through the exact code path you're testing is the real game-changer!

Collapse
 
launchgatecheck profile image
Launch Gate •

Lesson B deserves its systematic form: mutation testing is "run the spec against main" automated. Every surviving mutant is precisely a test that agrees with you - green for a reason other than the one you wrote it for. The cheaper habit that catches most of them is asserting on the mechanism, not only the outcome: your second-run test counted achievements, which the uniqueness validator also satisfies; counting create attempts would have separated first_or_create from silent validation failure.

Lesson A is the same rule as filtering a noisy error tracker: a warning that fires on expected configuration teaches operators to ignore the channel, and the cost is paid when a real one arrives. "warn means someone should act" has a corollary - on this install, with this config; expected absences are info by definition. And the OpenHands read-only lookup deserves a nod: confirming a falsifiable hypothesis against production data without mutating anything is the right instinct everywhere, not just in reviews.

Collapse
 
paul_emmanuel profile image
Emmanuel Paul •

Nicely done 👏

Collapse
 
dj29 profile image
Dhruv Jani •

Thanks!😄
Have A Great Day!