From f3f70e10f4952db4f7b7440212b9ca27f6e542f5 Mon Sep 17 00:00:00 2001 From: Bel LaPointe <153096461+breel-render@users.noreply.github.com> Date: Sun, 15 Dec 2024 12:39:36 -0700 Subject: [PATCH] test UserGameServer unstarted state has all empty players --- cmd/server/games.go | 12 +++++++ cmd/server/usergameserver_test.go | 54 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 cmd/server/usergameserver_test.go diff --git a/cmd/server/games.go b/cmd/server/games.go index f474422..2c79b4e 100644 --- a/cmd/server/games.go +++ b/cmd/server/games.go @@ -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 { diff --git a/cmd/server/usergameserver_test.go b/cmd/server/usergameserver_test.go new file mode 100644 index 0000000..3aed585 --- /dev/null +++ b/cmd/server/usergameserver_test.go @@ -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) + } + } + }) +}