Files
CosmicClash/server/testkit/pipeline_test.go
T
2026-08-31 21:20:12 +01:00

71 lines
3.2 KiB
Go

package testkit
import (
"testing"
"time"
"github.com/cosmic-clash/cosmic-clash/server/domain"
)
func TestOfflineMatchmakingPipelineReachesDurableResult(t *testing.T) {
now := time.Unix(1000, 0)
queue := domain.NewQueue()
for i := 0; i < 6; i++ {
playerID := string(rune('a' + i))
ticketID := "ticket-" + playerID
candidate := domain.Candidate{TicketID: ticketID, PlayerID: playerID, Rating: 1500 + float64(i), EnqueuedAt: now, PredictedRTT: map[string]float64{"EU": 30}}
if _, err := queue.Create(playerID, ticketID, "create-key-"+playerID+"-123456", candidate, now); err != nil {
t.Fatal(err)
}
}
candidates := queue.Candidates(now)
selection, err := domain.SelectCandidates(candidates[0], candidates[1:], 6, now)
if err != nil || len(selection.Players) != 6 || selection.Region != "EU" {
t.Fatalf("selection = %+v err=%v", selection, err)
}
playerIDs := make([]string, 0, len(selection.Players))
for _, candidate := range selection.Players {
playerIDs = append(playerIDs, candidate.PlayerID)
}
proposal, err := domain.NewProposal("proposal-1234567890123456", domain.Ranked, playerIDs, now)
if err != nil {
t.Fatal(err)
}
for _, participant := range proposal.Participants {
proposal, err = proposal.Respond(participant.PlayerID, "accept-"+participant.PlayerID+"-123456", true, proposal.Revision, now)
if err != nil {
t.Fatal(err)
}
}
if proposal.State != domain.Accepted {
t.Fatalf("proposal did not accept: %+v", proposal)
}
allocator, err := domain.NewAllocator([]domain.ReadyServer{{ServerID: "server-1", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet", State: domain.ServerReady}})
if err != nil {
t.Fatal(err)
}
allocation, err := allocator.Allocate(domain.AllocationRequest{AllocationID: "allocation-1234567890123456", MatchID: "match-1234567890123456", Region: "EU", Build: "build-1", Protocol: 1, Transport: "enet"}, now)
if err != nil {
t.Fatal(err)
}
manifest := domain.AllocationManifest{AllocationID: allocation.AllocationID, MatchID: allocation.MatchID, ServerID: allocation.ServerID, Region: allocation.Region, Build: allocation.Build, Protocol: allocation.Protocol, Transport: allocation.Transport, RosterDigest: "roster-digest"}
digest := domain.ManifestDigest(manifest)
assignment, err := domain.VerifyAssignment(allocation, manifest, "127.0.0.1:31001", digest[:], func(_, signature []byte) bool { return string(signature) == string(digest[:]) })
if err != nil || assignment.Endpoint == "" {
t.Fatalf("assignment = %+v err=%v", assignment, err)
}
binding := domain.WorkloadBinding{Issuer: "issuer", Audience: "audience", Namespace: "games", ServiceAcct: "match-server", PodUID: "pod-1", GameServerUID: "gs-1", AllocationID: allocation.AllocationID, MatchID: allocation.MatchID, ServerID: allocation.ServerID}
store, err := domain.NewResultStore(binding)
if err != nil {
t.Fatal(err)
}
result := domain.MatchResult{MatchID: allocation.MatchID, ServerID: allocation.ServerID, ResultNonce: "result-nonce-123456", Team0Score: 3, Team1Score: 2, IntegrityState: domain.IntegrityCertified}
receipt, created, err := store.Submit("result-1234567890123456", result, binding, now)
if err != nil || !created || !domain.RatingEligible(receipt) {
t.Fatalf("receipt = %+v created=%v err=%v", receipt, created, err)
}
}