mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-10 16:04:04 +00:00
72 lines
2.9 KiB
Go
72 lines
2.9 KiB
Go
package store
|
|
|
|
import (
|
|
"github.com/cosmic-clash/cosmic-clash/server/domain"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestQueueSQLUsesDurableIdempotencyAndOwnerScopedRecovery(t *testing.T) {
|
|
for query, fragments := range map[string][]string{
|
|
QueueIdempotencyInsertSQL: {"idempotency_keys", "ON CONFLICT (scope, idempotency_key) DO NOTHING", "payload_digest"},
|
|
QueueIdempotencySelectSQL: {"scope = $1", "idempotency_key = $2", "FOR UPDATE"},
|
|
QueueTicketSelectSQL: {"ticket_id = $1", "player_id = $2"},
|
|
QueueTicketInsertSQL: {"player_id", "playlist", "client_build", "protocol_version"},
|
|
QueueTicketHeartbeatSQL: {"player_id = $2", "revision = $3", "expires_at > $4", "RETURNING"},
|
|
QueueTicketCancelSQL: {"player_id = $2", "revision = $3", "state NOT IN", "RETURNING"},
|
|
QueueCandidateProjectionSQL: {"playlist = $1", "predicted_rtt", "expires_at > $2", "LIMIT $3"},
|
|
RankedParticipantSQL: {"steam_id", "player_id = ANY($1)", "ORDER BY player_id"},
|
|
ProposalInsertSQL: {"match_region", "match_protocol", "NULLIF($4, '')"},
|
|
ProposalParticipantInsertSQL: {"team", "slot", "'PENDING'"},
|
|
} {
|
|
for _, fragment := range fragments {
|
|
if !contains(query, fragment) {
|
|
t.Fatalf("query %q missing %q", query, fragment)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadRankedParticipantsRejectsNonSixPlayerLookupsWithoutDatabase(t *testing.T) {
|
|
if _, err := LoadRankedParticipants(nil, nil, []string{"player-1"}); err == nil {
|
|
t.Fatal("partial ranked identity lookup was accepted")
|
|
}
|
|
}
|
|
|
|
func TestListQueuedCandidatesRejectsUnscopedOrUnboundedReads(t *testing.T) {
|
|
now := time.Unix(1000, 0)
|
|
for _, playlist := range []domain.Playlist{"", "invalid"} {
|
|
if _, err := ListQueuedCandidates(nil, nil, playlist, now, 4); err == nil {
|
|
t.Fatalf("playlist %q accepted", playlist)
|
|
}
|
|
}
|
|
if _, err := ListQueuedCandidates(nil, nil, domain.Casual, now, 0); err == nil {
|
|
t.Fatal("zero limit accepted")
|
|
}
|
|
if _, err := ListQueuedCandidates(nil, nil, domain.Casual, now, 1001); err == nil {
|
|
t.Fatal("unbounded limit accepted")
|
|
}
|
|
}
|
|
|
|
func TestQueueMutationAdaptersRejectInvalidArgumentsWithoutDatabase(t *testing.T) {
|
|
now := time.Unix(1000, 0)
|
|
if _, err := HeartbeatQueueTicket(nil, nil, "player-1", "ticket-1", "short", 0, now); err == nil {
|
|
t.Fatal("invalid heartbeat accepted")
|
|
}
|
|
if _, err := CancelQueueTicket(nil, nil, "player-1", "ticket-1", "short", 0, now); err == nil {
|
|
t.Fatal("invalid cancel accepted")
|
|
}
|
|
}
|
|
|
|
func TestCreateQueueTicketRejectsInvalidArgumentsWithoutDatabase(t *testing.T) {
|
|
if _, err := CreateQueueTicket(nil, nil, "ticket-1", "player-1", "short", domain.QueueSpec{Playlist: domain.Casual, ClientBuild: "build-1", ProtocolVersion: 1}, time.Unix(1000, 0)); err == nil {
|
|
t.Fatal("invalid arguments accepted")
|
|
}
|
|
}
|
|
|
|
func TestQueueRecoveryRequiresAuthoritativeClock(t *testing.T) {
|
|
if _, err := GetQueueTicket(nil, nil, "player-1", "ticket-1", time.Time{}); err == nil {
|
|
t.Fatal("recovery without a clock was accepted")
|
|
}
|
|
}
|