test UserGameServer unstarted state has all empty players

main
Bel LaPointe 2024-12-15 12:39:36 -07:00
parent 1e4198b291
commit f3f70e10f4
2 changed files with 66 additions and 0 deletions

View File

@ -77,6 +77,14 @@ func (games Games) UserName(ctx context.Context, id string) (string, error) {
return result, err
}
func (a Assignment) Empty() bool {
return len(a.Public) == 0 && len(a.Private) == 0
}
func (s PlayerState) Empty() bool {
return len(s.Kills) == 0 && s.KillWords.Empty()
}
func (s PlayerState) Points() int {
points := 0
for _, kill := range s.Kills {
@ -378,6 +386,10 @@ func (games Games) CreateEventAssignmentRotation(ctx context.Context, id string,
return games.createEvent(ctx, id, event)
}
func (words KillWords) Empty() bool {
return words.Global == (KillWord{}) && words.Assigned.IsZero() && words.Assignee == "" && words.Assignment.Empty()
}
func (words KillWords) Privates() []KillWord {
a := slices.Clone(words.Assignment.Private)
slices.SortFunc(a, func(a, b KillWord) int {

View File

@ -0,0 +1,54 @@
package main
import (
"context"
"fmt"
"testing"
)
func TestUserGameServer(t *testing.T) {
ctx := context.Background()
games := newTestGames(t)
gid, err := games.CreateGame(ctx, "g1")
if err != nil {
t.Fatal(err)
}
pids := []string{}
for i := 0; i < 4; i++ {
pid := fmt.Sprintf("p%d", i+1)
if err := games.CreateEventPlayerJoin(ctx, gid, pid, "player "+pid); err != nil {
t.Fatal(err)
}
pids = append(pids, pid)
}
ugs, err := NewUserGameServer(ctx, Session{ID: "p1"}, games)
if err != nil {
t.Fatal(err)
}
t.Run("unstarted", func(t *testing.T) {
state, err := ugs.State(ctx)
if err != nil {
t.Fatal(err)
}
if state.Started {
t.Error("started after player joins only")
}
if !state.Completed.IsZero() {
t.Error("completed after player joins only")
}
for _, pid := range pids {
if p, ok := state.Players[pid]; !ok {
t.Error("self not in players")
} else if !p.Empty() {
t.Error(pid, p)
}
}
})
}