- Rewrite chat rate limiter with Redis sliding window (sorted sets) and automatic in-memory fallback when Redis is unavailable - Add ChatPresenceService with Redis-backed online/offline/heartbeat tracking (2min TTL), integrated into Hub register/unregister - Add migration 113: tsvector column with GIN index and auto-update trigger on messages table for full-text search - Update Search repository method to use ts_rank ordering instead of ILIKE - Wire Redis client into chat WebSocket setup in router.go - Add comprehensive tests: rate limiter, presence, 100-user concurrent benchmark
34 lines
885 B
Go
34 lines
885 B
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"go.uber.org/zap/zaptest"
|
|
)
|
|
|
|
func TestPresenceService_NilRedis_NoErrors(t *testing.T) {
|
|
logger := zaptest.NewLogger(t)
|
|
svc := NewChatPresenceService(nil, logger)
|
|
ctx := context.Background()
|
|
userID := uuid.New()
|
|
|
|
assert.NoError(t, svc.SetOnline(ctx, userID))
|
|
assert.NoError(t, svc.SetOffline(ctx, userID))
|
|
assert.NoError(t, svc.Heartbeat(ctx, userID))
|
|
|
|
info, err := svc.GetPresence(ctx, userID)
|
|
assert.NoError(t, err)
|
|
assert.False(t, info.Online)
|
|
assert.Equal(t, userID, info.UserID)
|
|
}
|
|
|
|
func TestPresenceService_PresenceKeyFormat(t *testing.T) {
|
|
svc := NewChatPresenceService(nil, nil)
|
|
userID := uuid.MustParse("550e8400-e29b-41d4-a716-446655440000")
|
|
|
|
key := svc.presenceKey(userID)
|
|
assert.Equal(t, "chat:presence:550e8400-e29b-41d4-a716-446655440000", key)
|
|
}
|