feat: game lobby service

This commit is contained in:
Ilia Denisov
2026-04-25 23:20:55 +02:00
committed by GitHub
parent 32dc29359a
commit 48b0056b49
336 changed files with 57074 additions and 1418 deletions
@@ -0,0 +1,116 @@
package redisstate_test
import (
"context"
"encoding/base64"
"testing"
"time"
"galaxy/lobby/internal/adapters/redisstate"
"galaxy/lobby/internal/domain/common"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func newGapActivationTestStore(t *testing.T) (*redisstate.GapActivationStore, *miniredis.Miniredis) {
t.Helper()
server := miniredis.RunT(t)
client := redis.NewClient(&redis.Options{Addr: server.Addr()})
t.Cleanup(func() { _ = client.Close() })
store, err := redisstate.NewGapActivationStore(client)
require.NoError(t, err)
return store, server
}
func TestNewGapActivationStoreRejectsNilClient(t *testing.T) {
t.Parallel()
_, err := redisstate.NewGapActivationStore(nil)
require.Error(t, err)
}
func TestMarkActivatedWritesRecord(t *testing.T) {
t.Parallel()
ctx := context.Background()
store, server := newGapActivationTestStore(t)
at := time.Date(2026, 4, 25, 10, 0, 0, 0, time.UTC)
require.NoError(t, store.MarkActivated(ctx, common.GameID("game-a"), at))
encoded := base64.RawURLEncoding.EncodeToString([]byte("game-a"))
stored, err := server.Get("lobby:gap_activated_at:" + encoded)
require.NoError(t, err)
assert.Contains(t, stored, "1777111200000")
}
func TestMarkActivatedIsNoOpOnSecondCall(t *testing.T) {
t.Parallel()
ctx := context.Background()
store, server := newGapActivationTestStore(t)
first := time.Date(2026, 4, 25, 10, 0, 0, 0, time.UTC)
second := first.Add(time.Hour)
require.NoError(t, store.MarkActivated(ctx, common.GameID("game-a"), first))
require.NoError(t, store.MarkActivated(ctx, common.GameID("game-a"), second))
encoded := base64.RawURLEncoding.EncodeToString([]byte("game-a"))
stored, err := server.Get("lobby:gap_activated_at:" + encoded)
require.NoError(t, err)
assert.Contains(t, stored, "1777111200000")
}
func TestMarkActivatedRejectsInvalidGameID(t *testing.T) {
t.Parallel()
ctx := context.Background()
store, _ := newGapActivationTestStore(t)
err := store.MarkActivated(ctx, common.GameID(""), time.Now().UTC())
require.Error(t, err)
}
func TestMarkActivatedRejectsZeroTime(t *testing.T) {
t.Parallel()
ctx := context.Background()
store, _ := newGapActivationTestStore(t)
err := store.MarkActivated(ctx, common.GameID("game-a"), time.Time{})
require.Error(t, err)
}
func TestGapActivationStoreGetReturnsRecordedTime(t *testing.T) {
t.Parallel()
ctx := context.Background()
store, _ := newGapActivationTestStore(t)
at := time.Date(2026, 4, 25, 10, 0, 0, 0, time.UTC)
require.NoError(t, store.MarkActivated(ctx, common.GameID("game-a"), at))
got, ok, err := store.Get(ctx, common.GameID("game-a"))
require.NoError(t, err)
require.True(t, ok)
assert.True(t, got.Equal(at))
}
func TestGapActivationStoreGetReturnsFalseWhenMissing(t *testing.T) {
t.Parallel()
ctx := context.Background()
store, _ := newGapActivationTestStore(t)
got, ok, err := store.Get(ctx, common.GameID("game-missing"))
require.NoError(t, err)
assert.False(t, ok)
assert.True(t, got.IsZero())
}
func TestGapActivationStoreGetRejectsInvalidGameID(t *testing.T) {
t.Parallel()
ctx := context.Background()
store, _ := newGapActivationTestStore(t)
_, _, err := store.Get(ctx, common.GameID(""))
require.Error(t, err)
}