test status

This commit is contained in:
Bel LaPointe
2023-03-27 06:50:31 -06:00
parent 8314bdc457
commit 88a78c489f
3 changed files with 121 additions and 38 deletions

View File

@@ -1,6 +1,7 @@
package v01
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
@@ -74,3 +75,93 @@ func TestPatchConfig(t *testing.T) {
}
}
}
func TestServeGM(t *testing.T) {
ctx, can := context.WithCancel(context.Background())
defer can()
do := func(v01 *V01, path, body string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
v01.ServeHTTP(w, r)
return w
}
t.Run("status", func(t *testing.T) {
v01 := NewV01(ctx, nil)
var result struct {
Players int `yaml:"Players"`
Users map[string]struct {
Lag string
LastActivity string
} `yaml:"Users"`
}
t.Run("empty", func(t *testing.T) {
resp := do(v01, "/gm/rpc/status", "")
if resp.Code != http.StatusOK {
t.Error(resp.Code)
}
t.Log(string(resp.Body.Bytes()))
if err := yaml.Unmarshal(resp.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
if result.Players != 0 {
t.Error(result.Players)
}
if len(result.Users) != 0 {
t.Error(result.Users)
}
})
t.Run("full", func(t *testing.T) {
v01.cfg.Players = []configPlayer{
{},
{},
{},
{},
}
v01.cfg.Users = map[string]configUser{
"bel": configUser{},
"zach": configUser{},
"chase": configUser{},
"mason": configUser{},
"nat": configUser{},
"roxy": configUser{},
"bill": configUser{},
}
resp := do(v01, "/gm/rpc/status", "")
if resp.Code != http.StatusOK {
t.Error(resp.Code)
}
t.Log(string(resp.Body.Bytes()))
if err := yaml.Unmarshal(resp.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
if result.Players != 4 {
t.Error(result.Players)
}
if len(result.Users) != 7 {
t.Error(result.Users)
}
})
})
t.Run("publicStatus", func(t *testing.T) {
})
t.Run("publicStatus", func(t *testing.T) {
})
t.Run("publicStatus", func(t *testing.T) {
})
t.Run("publicStatus", func(t *testing.T) {
})
t.Run("publicStatus", func(t *testing.T) {
})
t.Run("publicStatus", func(t *testing.T) {
})
}