feat: track authenticated user activity
This commit is contained in:
@@ -23,18 +23,21 @@ import (
|
||||
// --- mock: UserRepository ---
|
||||
|
||||
type mockUserRepo struct {
|
||||
updateBalanceErr error
|
||||
updateBalanceFn func(ctx context.Context, id int64, amount float64) error
|
||||
getByIDUser *User
|
||||
getByIDErr error
|
||||
updateFn func(ctx context.Context, user *User) error
|
||||
updateCalls int
|
||||
upsertAvatarFn func(ctx context.Context, userID int64, input UpsertUserAvatarInput) (*UserAvatar, error)
|
||||
upsertAvatarArgs []UpsertUserAvatarInput
|
||||
deleteAvatarFn func(ctx context.Context, userID int64) error
|
||||
deleteAvatarIDs []int64
|
||||
getAvatarFn func(ctx context.Context, userID int64) (*UserAvatar, error)
|
||||
txCalls int
|
||||
updateBalanceErr error
|
||||
updateBalanceFn func(ctx context.Context, id int64, amount float64) error
|
||||
getByIDUser *User
|
||||
getByIDErr error
|
||||
updateLastActiveErr error
|
||||
updateLastActiveUserIDs []int64
|
||||
updateLastActiveAt []time.Time
|
||||
updateFn func(ctx context.Context, user *User) error
|
||||
updateCalls int
|
||||
upsertAvatarFn func(ctx context.Context, userID int64, input UpsertUserAvatarInput) (*UserAvatar, error)
|
||||
upsertAvatarArgs []UpsertUserAvatarInput
|
||||
deleteAvatarFn func(ctx context.Context, userID int64) error
|
||||
deleteAvatarIDs []int64
|
||||
getAvatarFn func(ctx context.Context, userID int64) (*UserAvatar, error)
|
||||
txCalls int
|
||||
}
|
||||
|
||||
type mockUserRepoTxKey struct{}
|
||||
@@ -144,6 +147,11 @@ func (m *mockUserRepo) UpdateBalance(ctx context.Context, id int64, amount float
|
||||
}
|
||||
return m.updateBalanceErr
|
||||
}
|
||||
func (m *mockUserRepo) UpdateUserLastActiveAt(_ context.Context, userID int64, activeAt time.Time) error {
|
||||
m.updateLastActiveUserIDs = append(m.updateLastActiveUserIDs, userID)
|
||||
m.updateLastActiveAt = append(m.updateLastActiveAt, activeAt)
|
||||
return m.updateLastActiveErr
|
||||
}
|
||||
func (m *mockUserRepo) DeductBalance(context.Context, int64, float64) error { return nil }
|
||||
func (m *mockUserRepo) UpdateConcurrency(context.Context, int64, int) error { return nil }
|
||||
func (m *mockUserRepo) ExistsByEmail(context.Context, string) (bool, error) { return false, nil }
|
||||
@@ -288,6 +296,39 @@ func TestUpdateBalance_CacheFailure_DoesNotAffectReturn(t *testing.T) {
|
||||
}, 2*time.Second, 10*time.Millisecond, "即使失败也应调用 InvalidateUserBalance")
|
||||
}
|
||||
|
||||
func TestTouchLastActive_UpdatesWhenStale(t *testing.T) {
|
||||
stale := time.Now().Add(-11 * time.Minute)
|
||||
repo := &mockUserRepo{
|
||||
getByIDUser: &User{
|
||||
ID: 42,
|
||||
LastActiveAt: &stale,
|
||||
},
|
||||
}
|
||||
svc := NewUserService(repo, nil, nil, nil)
|
||||
|
||||
svc.TouchLastActive(context.Background(), 42)
|
||||
|
||||
require.Equal(t, []int64{42}, repo.updateLastActiveUserIDs)
|
||||
require.Len(t, repo.updateLastActiveAt, 1)
|
||||
require.WithinDuration(t, time.Now(), repo.updateLastActiveAt[0], 2*time.Second)
|
||||
}
|
||||
|
||||
func TestTouchLastActive_SkipsWhenRecent(t *testing.T) {
|
||||
recent := time.Now().Add(-time.Minute)
|
||||
repo := &mockUserRepo{
|
||||
getByIDUser: &User{
|
||||
ID: 42,
|
||||
LastActiveAt: &recent,
|
||||
},
|
||||
}
|
||||
svc := NewUserService(repo, nil, nil, nil)
|
||||
|
||||
svc.TouchLastActive(context.Background(), 42)
|
||||
|
||||
require.Empty(t, repo.updateLastActiveUserIDs)
|
||||
require.Empty(t, repo.updateLastActiveAt)
|
||||
}
|
||||
|
||||
func TestUpdateBalance_RepoError_ReturnsError(t *testing.T) {
|
||||
repo := &mockUserRepo{updateBalanceErr: errors.New("database error")}
|
||||
cache := &mockBillingCache{}
|
||||
|
||||
Reference in New Issue
Block a user