78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
package redisstate_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"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 newGuardStore(t *testing.T) (*redisstate.EvaluationGuardStore, *miniredis.Miniredis) {
|
|
t.Helper()
|
|
server := miniredis.RunT(t)
|
|
client := redis.NewClient(&redis.Options{Addr: server.Addr()})
|
|
t.Cleanup(func() { _ = client.Close() })
|
|
|
|
store, err := redisstate.NewEvaluationGuardStore(client)
|
|
require.NoError(t, err)
|
|
return store, server
|
|
}
|
|
|
|
func TestEvaluationGuardStoreIsEvaluatedReturnsFalseWhenMissing(t *testing.T) {
|
|
store, _ := newGuardStore(t)
|
|
|
|
evaluated, err := store.IsEvaluated(context.Background(), common.GameID("game-guard-1"))
|
|
require.NoError(t, err)
|
|
assert.False(t, evaluated)
|
|
}
|
|
|
|
func TestEvaluationGuardStoreMarkThenIsEvaluated(t *testing.T) {
|
|
store, _ := newGuardStore(t)
|
|
gameID := common.GameID("game-guard-2")
|
|
|
|
require.NoError(t, store.MarkEvaluated(context.Background(), gameID))
|
|
|
|
evaluated, err := store.IsEvaluated(context.Background(), gameID)
|
|
require.NoError(t, err)
|
|
assert.True(t, evaluated)
|
|
}
|
|
|
|
func TestEvaluationGuardStoreMarkIsIdempotent(t *testing.T) {
|
|
store, _ := newGuardStore(t)
|
|
gameID := common.GameID("game-guard-3")
|
|
|
|
require.NoError(t, store.MarkEvaluated(context.Background(), gameID))
|
|
require.NoError(t, store.MarkEvaluated(context.Background(), gameID))
|
|
|
|
evaluated, err := store.IsEvaluated(context.Background(), gameID)
|
|
require.NoError(t, err)
|
|
assert.True(t, evaluated)
|
|
}
|
|
|
|
func TestEvaluationGuardStoreInvalidGameID(t *testing.T) {
|
|
store, _ := newGuardStore(t)
|
|
|
|
_, err := store.IsEvaluated(context.Background(), common.GameID(""))
|
|
require.Error(t, err)
|
|
|
|
err = store.MarkEvaluated(context.Background(), common.GameID(""))
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestEvaluationGuardStoreSetsTTL(t *testing.T) {
|
|
store, server := newGuardStore(t)
|
|
gameID := common.GameID("game-guard-ttl")
|
|
|
|
require.NoError(t, store.MarkEvaluated(context.Background(), gameID))
|
|
|
|
keyspace := redisstate.Keyspace{}
|
|
ttl := server.TTL(keyspace.CapabilityEvaluationGuard(gameID))
|
|
assert.Equal(t, redisstate.CapabilityEvaluationGuardTTL, ttl)
|
|
}
|