moth/cmd/mothd/state_test.go

142 lines
3.0 KiB
Go
Raw Normal View History

2019-12-01 18:58:09 -07:00
package main
import (
2019-12-01 20:53:13 -07:00
"bytes"
2019-12-01 18:58:09 -07:00
"os"
2020-08-19 15:38:13 -06:00
"strings"
2019-12-01 18:58:09 -07:00
"testing"
2020-08-18 17:04:23 -06:00
"time"
2020-08-14 20:26:04 -06:00
"github.com/spf13/afero"
2019-12-01 18:58:09 -07:00
)
2020-08-17 17:43:57 -06:00
func NewTestState() *State {
s := NewState(new(afero.MemMapFs))
2020-08-18 17:04:23 -06:00
s.refresh()
2020-08-17 17:43:57 -06:00
return s
}
2019-12-01 18:58:09 -07:00
func TestState(t *testing.T) {
2020-08-17 17:43:57 -06:00
s := NewTestState()
2019-12-01 18:58:09 -07:00
mustExist := func(path string) {
2020-08-17 17:43:57 -06:00
_, err := s.Fs.Stat(path)
2019-12-01 18:58:09 -07:00
if os.IsNotExist(err) {
t.Errorf("File %s does not exist", path)
}
}
2019-12-01 20:53:13 -07:00
pl := s.PointsLog()
if len(pl) != 0 {
t.Errorf("Empty points log is not empty")
}
2019-12-01 20:47:46 -07:00
2019-12-01 18:58:09 -07:00
mustExist("initialized")
mustExist("enabled")
mustExist("hours")
2019-12-01 20:53:13 -07:00
2020-08-17 17:43:57 -06:00
teamIDsBuf, err := afero.ReadFile(s.Fs, "teamids.txt")
2019-12-01 20:47:46 -07:00
if err != nil {
2019-12-01 20:53:13 -07:00
t.Errorf("Reading teamids.txt: %v", err)
2019-12-01 20:47:46 -07:00
}
2019-12-01 20:53:13 -07:00
2020-08-17 17:43:57 -06:00
teamIDs := bytes.Split(teamIDsBuf, []byte("\n"))
if (len(teamIDs) != 101) || (len(teamIDs[100]) > 0) {
t.Errorf("There weren't 100 teamIDs, there were %d", len(teamIDs))
2019-12-01 20:47:46 -07:00
}
2020-08-17 17:43:57 -06:00
teamID := string(teamIDs[0])
2019-12-01 20:53:13 -07:00
2019-12-01 20:47:46 -07:00
if err := s.SetTeamName("bad team ID", "bad team name"); err == nil {
2019-12-01 20:53:13 -07:00
t.Errorf("Setting bad team ID didn't raise an error")
2019-12-01 20:47:46 -07:00
}
2019-12-01 20:53:13 -07:00
2020-08-17 17:43:57 -06:00
if err := s.SetTeamName(teamID, "My Team"); err != nil {
2019-12-01 20:53:13 -07:00
t.Errorf("Setting team name: %v", err)
2019-12-01 20:47:46 -07:00
}
2019-12-07 21:17:13 -07:00
2019-12-05 21:50:43 -07:00
category := "poot"
points := 3928
2020-08-17 17:43:57 -06:00
s.AwardPoints(teamID, category, points)
2020-08-18 17:04:23 -06:00
s.refresh()
2019-12-07 21:17:13 -07:00
2019-12-05 21:50:43 -07:00
pl = s.PointsLog()
if len(pl) != 1 {
t.Errorf("After awarding points, points log has length %d", len(pl))
2020-08-17 17:43:57 -06:00
} else if (pl[0].TeamID != teamID) || (pl[0].Category != category) || (pl[0].Points != points) {
2019-12-05 21:50:43 -07:00
t.Errorf("Incorrect logged award %v", pl)
}
2019-12-07 21:17:13 -07:00
2020-08-17 17:43:57 -06:00
s.Fs.Remove("initialized")
2020-08-18 17:04:23 -06:00
s.refresh()
2019-12-07 21:17:13 -07:00
2019-12-05 21:54:32 -07:00
pl = s.PointsLog()
if len(pl) != 0 {
t.Errorf("After reinitialization, points log has length %d", len(pl))
}
2019-12-01 18:58:09 -07:00
}
2020-08-18 17:04:23 -06:00
func TestStateEvents(t *testing.T) {
s := NewTestState()
s.LogEvent("moo")
2020-08-19 18:01:21 -06:00
s.LogEvent("moo 2")
2020-08-18 17:04:23 -06:00
if msg := <-s.eventStream; msg != "moo" {
t.Error("Wrong message from event stream", msg)
}
if msg := <-s.eventStream; msg != "moo 2" {
t.Error("Formatted event is wrong:", msg)
}
}
func TestStateMaintainer(t *testing.T) {
2020-08-19 15:38:13 -06:00
updateInterval := 10 * time.Millisecond
2020-08-18 17:04:23 -06:00
s := NewTestState()
2020-08-19 15:38:13 -06:00
go s.Maintain(updateInterval)
if _, err := s.Stat("initialized"); err != nil {
t.Error(err)
}
teamIDLines, err := afero.ReadFile(s, "teamids.txt")
if err != nil {
t.Error(err)
}
teamIDList := strings.Split(string(teamIDLines), "\n")
if len(teamIDList) != 101 {
t.Error("TeamIDList length is", len(teamIDList))
}
teamID := teamIDList[0]
if len(teamID) < 6 {
t.Error("Team ID too short:", teamID)
}
2020-08-18 17:04:23 -06:00
s.LogEvent("Hello!")
2020-08-19 15:38:13 -06:00
if len(s.PointsLog()) != 0 {
t.Error("Points log is not empty")
}
if err := s.SetTeamName(teamID, "The Patricks"); err != nil {
t.Error(err)
}
if err := s.AwardPoints(teamID, "pategory", 31337); err != nil {
t.Error(err)
}
time.Sleep(updateInterval)
pl := s.PointsLog()
if len(pl) != 1 {
t.Error("Points log should have one entry")
}
if (pl[0].Category != "pategory") || (pl[0].TeamID != teamID) {
t.Error("Wrong points event was recorded")
}
time.Sleep(updateInterval)
eventLog, err := afero.ReadFile(s.Fs, "event.log")
if err != nil {
t.Error(err)
} else if len(eventLog) != 18 {
2020-08-18 17:04:23 -06:00
t.Error("Wrong event log length:", len(eventLog))
}
}