109 lines
3.0 KiB
Go
109 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func newTestGames(t *testing.T) Games {
|
|
db := newTestDB(t)
|
|
|
|
games, err := NewGames(context.Background(), db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return games
|
|
}
|
|
|
|
func TestGames(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
games := newTestGames(t)
|
|
|
|
if v, err := games.GamesForUser(ctx, ""); err != nil {
|
|
t.Error("err getting games for empty user:", err)
|
|
} else if len(v) > 0 {
|
|
t.Error(v)
|
|
}
|
|
|
|
if v, err := games.GameByName(ctx, "", ""); err != nil {
|
|
t.Error("err getting game by empty name for empty user:", err)
|
|
} else if len(v) > 0 {
|
|
t.Error(v)
|
|
}
|
|
|
|
if v, err := games.GameState(ctx, ""); err != nil {
|
|
t.Error("err getting game state for empty:", err)
|
|
} else if len(v.Players) > 0 || !v.Completed.IsZero() {
|
|
t.Error(v)
|
|
}
|
|
})
|
|
|
|
t.Run("mvp", func(t *testing.T) {
|
|
games := newTestGames(t)
|
|
|
|
id, err := games.CreateGame(ctx, "g1")
|
|
if err != nil {
|
|
t.Fatal("err creating game:", err)
|
|
} else if id2, err := games.CreateGame(ctx, "g1"); err != nil {
|
|
t.Fatal("err creating game redundantly:", err)
|
|
} else if id != id2 {
|
|
t.Fatal("redundant create game didnt return same id:", id2)
|
|
}
|
|
|
|
if err := games.CreateEventPlayerJoin(ctx, id, "p0"); err != nil {
|
|
t.Fatal("err creating event player join:", err)
|
|
} else if err := games.CreateEventPlayerLeave(ctx, id, "p0"); err != nil {
|
|
t.Fatal("err creating event player leave:", err)
|
|
}
|
|
|
|
for i := 0; i < 4; i++ {
|
|
p := fmt.Sprintf("p%d", i+1)
|
|
if err := games.CreateEventPlayerJoin(ctx, id, p); err != nil {
|
|
t.Fatal(p, "err creating event player join", err)
|
|
}
|
|
}
|
|
|
|
if err := games.CreateEventAssignmentRotation(ctx, id, "", "", ""); err != nil {
|
|
t.Fatal("err creating rotation:", err)
|
|
}
|
|
|
|
if v, err := games.GamesForUser(ctx, "p1"); err != nil {
|
|
t.Error("err getting games for user:", err)
|
|
} else if len(v) < 1 {
|
|
t.Error("no games found for user:", v)
|
|
} else if v[0] != id {
|
|
t.Error("wrong game found for user:", v)
|
|
}
|
|
|
|
if v, err := games.GameByName(ctx, "p1", "g1"); err != nil {
|
|
t.Error("err getting game by name for user:", err)
|
|
} else if v != id {
|
|
t.Error("wrong game by name for user:", v)
|
|
}
|
|
|
|
if v, err := games.GameState(ctx, id); err != nil {
|
|
t.Error("err getting game state:", err)
|
|
} else if len(v.Players) != 4 || !v.Completed.IsZero() {
|
|
t.Error("wrong game state:", v)
|
|
} else {
|
|
for i := 0; i < 4; i++ {
|
|
p := fmt.Sprintf("p%d", i+1)
|
|
if v.Players[p].KillWords.Global == "" {
|
|
t.Error(p, "no killwords.global")
|
|
} else if v.Players[p].KillWords.Assigned.IsZero() {
|
|
t.Error(p, "no killwords.assigned")
|
|
} else if v.Players[p].KillWords.Assignee == "" {
|
|
t.Error(p, "no killwords.assignee")
|
|
} else if len(v.Players[p].KillWords.Assignment.Public) == 0 {
|
|
t.Error(p, "no killwords.assigment.public")
|
|
} else if len(v.Players[p].KillWords.Assignment.Private) == 0 {
|
|
t.Error(p, "no killwords.assigment.private")
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|