feat(server/auth): Register 加 inviteCode + ReferralHook(注册后 best-effort 归因)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-07-13 07:33:42 +08:00
parent 2fb7391a17
commit 54d106ca8b
6 changed files with 83 additions and 26 deletions
@@ -0,0 +1,43 @@
package auth
import (
"context"
"testing"
)
// fakeHook captures the arguments passed to OnRegister so tests can assert on
// them without depending on the real reward.Service.
type fakeHook struct {
gotInvitee int64
gotCode string
gotDev string
}
func (f *fakeHook) OnRegister(_ context.Context, inviteeID int64, code, dev string) {
f.gotInvitee, f.gotCode, f.gotDev = inviteeID, code, dev
}
// TestRegister_CallsReferralHookWithInviteCode verifies that a successful
// registration best-effort invokes the ReferralHook with the invite code and
// this registration's device UUID (not some stale/historical value).
func TestRegister_CallsReferralHookWithInviteCode(t *testing.T) {
svc, _, _ := newService(t, ServiceConfig{})
ctx := context.Background()
const email = "new@x.com"
if _, err := svc.SendCode(ctx, email, "1.2.3.4"); err != nil {
t.Fatalf("SendCode: %v", err)
}
code := codeInRedis(t, svc, email)
h := &fakeHook{}
svc.SetReferralHook(h)
_, apiErr := svc.Register(ctx, email, code, "password123", "1.2.3.4", DeviceMeta{DeviceID: "dev-1"}, "INVCODE9")
if apiErr != nil {
t.Fatalf("Register: %v", apiErr)
}
if h.gotCode != "INVCODE9" || h.gotDev != "dev-1" || h.gotInvitee == 0 {
t.Fatalf("hook got invitee=%d code=%q dev=%q", h.gotInvitee, h.gotCode, h.gotDev)
}
}