Files
MichilisandClaude Opus 5.5 f7ceadc462 Phase 3: own referral system (sats)
- Referral links carry a random 8 character base32 code (rf_<code>)
  built from the bot username from getMe. Old ref_<id> links still work
  but are never shown.
- The reward (REFERRAL_REWARD_SATS, old name REFERRAL_REWARD still read)
  is paid through the ledger once the invited user's first purchase
  settles, once per invited user, never for self-referrals.
- REFERRAL_DAILY_CAP limits what an inviter earns per UTC day; rewards
  above it wait as pending and are paid on a later day by the billing
  loop.
- /referral shows invited, converted, earned and pending, with Share,
  Copy link and QR code buttons. The Mini App Me page mirrors it.
- Migration 00008: users.ref_code (unique) and referral_rewards,
  backfilled from earlier referral credits in the ledger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 20:58:43 +00:00

81 lines
2.2 KiB
Go

package db_test
import (
"context"
"testing"
"github.com/noderunners/dlbot/internal/db"
"github.com/noderunners/dlbot/internal/db/dbtest"
)
func TestRefCodesAreShortRandomAndUnique(t *testing.T) {
store := dbtest.Open(t).Store()
ctx := context.Background()
seen := map[string]bool{}
for id := int64(1); id <= 300; id++ {
if _, err := store.UpsertUser(ctx, id, "", "U", "en"); err != nil {
t.Fatal(err)
}
code, err := store.RefCode(ctx, id)
if err != nil {
t.Fatal(err)
}
if !db.ValidRefCode(code) {
t.Fatalf("code %q is not 8 characters of base32", code)
}
if seen[code] {
t.Fatalf("code %q given twice", code)
}
seen[code] = true
// The code is stable once made.
if again, _ := store.RefCode(ctx, id); again != code {
t.Fatalf("code changed from %q to %q", code, again)
}
owner, err := store.UserByRefCode(ctx, code)
if err != nil || owner.ID != id {
t.Fatalf("UserByRefCode(%q) = %+v, %v", code, owner, err)
}
}
// Lower case works (some apps change links), garbage does not.
for code := range seen {
lower := []byte(code)
for i, c := range lower {
if c >= 'A' && c <= 'Z' {
lower[i] = c + 32
}
}
if _, err := store.UserByRefCode(ctx, string(lower)); err != nil {
t.Fatalf("lower case code: %v", err)
}
break
}
for _, bad := range []string{"", "ABC", "ABCDEFG1", "ABCDEFGHI", "12345678", "' OR 1=1"} {
if _, err := store.UserByRefCode(ctx, bad); err != db.ErrNotFound {
t.Errorf("UserByRefCode(%q) = %v, want not found", bad, err)
}
}
}
func TestRefCodeCollisionIsRetried(t *testing.T) {
store := dbtest.Open(t).Store()
ctx := context.Background()
for _, id := range []int64{1, 2} {
if _, err := store.UpsertUser(ctx, id, "", "U", "en"); err != nil {
t.Fatal(err)
}
}
codes := []string{"AAAAAAAA", "AAAAAAAA", "BBBBBBBB"}
restore := *db.RefCodeGen
*db.RefCodeGen = func() string { c := codes[0]; codes = codes[1:]; return c }
defer func() { *db.RefCodeGen = restore }()
first, err := store.RefCode(ctx, 1)
if err != nil || first != "AAAAAAAA" {
t.Fatalf("first = %q, %v", first, err)
}
// The second user draws the same code first, then a free one.
second, err := store.RefCode(ctx, 2)
if err != nil || second != "BBBBBBBB" {
t.Fatalf("second = %q, %v", second, err)
}
}