From 78e07731c8cb99945a63be8b7308f67c43ccde4b Mon Sep 17 00:00:00 2001 From: Hamcha Date: Fri, 28 Jun 2019 23:07:05 +0200 Subject: [PATCH] Add more tests! --- draftbot/draftbot_test.go | 173 +++++++++++++++++++++++++++++++++++--- draftbot/go.mod | 2 +- draftbot/go.sum | 2 + draftbot/main.go | 7 ++ draftbot/testdata/cube.go | 130 ++++++++++++++++++++++++++++ 5 files changed, 302 insertions(+), 12 deletions(-) create mode 100644 draftbot/testdata/cube.go diff --git a/draftbot/draftbot_test.go b/draftbot/draftbot_test.go index ea0f268..0ab1c63 100644 --- a/draftbot/draftbot_test.go +++ b/draftbot/draftbot_test.go @@ -1,36 +1,45 @@ package main_test import ( + "fmt" + "net/http" + "net/http/httptest" + "os" "testing" "time" "git.fromouter.space/mcg/draft" "git.fromouter.space/mcg/draft/mlp" + mlptestdata "git.fromouter.space/mcg/draft/mlp/testdata" + + draftbot "git.fromouter.space/mcg/mlp-server-tools/draftbot" + testdata "git.fromouter.space/mcg/mlp-server-tools/draftbot/testdata" "git.fromouter.space/mcg/cardgage/client/bot" lobby "git.fromouter.space/mcg/cardgage/lobby/proto" room "git.fromouter.space/mcg/cardgage/room/api" - draftbot "git.fromouter.space/mcg/mlp-server-tools/draftbot" ) const TestBotName = "test-bot" const TestRoomName = "test-room" type MockServer struct { - in chan room.ServerMessage - out chan room.BotMessage - t *testing.T - timeout time.Duration + in chan room.ServerMessage + out chan room.BotMessage + t *testing.T + timeout time.Duration + roomName string } func makeMockServer(t *testing.T, timeout int) *MockServer { in := make(chan room.ServerMessage, 99) out := make(chan room.BotMessage, 99) srv := &MockServer{ - in: in, - out: out, - t: t, - timeout: time.Duration(timeout) * time.Second, + in: in, + out: out, + t: t, + timeout: time.Duration(timeout) * time.Second, + roomName: TestRoomName, } return srv } @@ -211,6 +220,124 @@ func TestDraftSessionButEverythingGoesWrong(t *testing.T) { //TODO More picking, etc shenanigans } +func TestDraftTypes(t *testing.T) { + // Load all sets into memory + err := mlp.LoadAllSets() + if err != nil { + t.Fatalf("Could not load MLP sets needed for some drafts: %s", err.Error()) + } + + mock := makeMockServer(t, 5) + drafter := draftbot.NewDraftBot(mock, TestBotName) + go mock.Bind(drafter.OnMessage) + + makeSession := func(roomName string, options draftbot.DraftOptions) { + // Create a new room + mock.in <- room.ServerMessage{ + RoomID: roomName, + Type: room.MsgEvent, + Event: &room.Event{ + Type: room.EvtNewRoom, + Room: &lobby.Room{ + Id: roomName, + Name: "Test draft room", + Creator: "test-owner", + }, + }, + } + mock.roomName = roomName + + // Create new session + mock.message("test-owner", "create", draftbot.SessionOptions{ + Players: 8, // Two players, six bots + Options: options, + }) + + mock.expect("session-open") + } + + // Make Set draft session + makeSession("set", draftbot.DraftOptions{ + Type: draftbot.DraftSet, + Positioning: draftbot.PosEven, + Set: mlp.SetAbsoluteDiscord, + PackCount: 4, + }) + + // Make Block draft session + makeSession("block", draftbot.DraftOptions{ + Type: draftbot.DraftBlock, + Positioning: draftbot.PosRandom, + Block: mlp.BlockDefenders, + }) + + // Make a plain Cube draft session + makeSession("cube", draftbot.DraftOptions{ + Type: draftbot.DraftCube, + Positioning: draftbot.PosEven, + CubeURL: mlp.HTTPSource + "cube", + PackSize: 4, + }) + + // Make a I8PCube draft session + makeSession("i8pcube", draftbot.DraftOptions{ + Type: draftbot.DraftI8PCube, + Positioning: draftbot.PosEven, + CubeURL: mlp.HTTPSource + "i8pcube", + MainCount: 1, + ProblemCount: 1, + }) +} + +func TestDraftPositioning(t *testing.T) { + mock := makeMockServer(t, 5) + drafter := draftbot.NewDraftBot(mock, TestBotName) + go mock.Bind(drafter.OnMessage) + + makeSession := func(roomName string, pos string) { + // Create a new room + mock.in <- room.ServerMessage{ + RoomID: roomName, + Type: room.MsgEvent, + Event: &room.Event{ + Type: room.EvtNewRoom, + Room: &lobby.Room{ + Id: roomName, + Name: "Test draft room", + Creator: "test-owner", + }, + }, + } + mock.roomName = roomName + + // Create new session + mock.message("test-owner", "create", draftbot.SessionOptions{ + Players: 8, // Two players, six bots + Options: draftbot.DraftOptions{ + Type: draftbot.DraftSet, + Positioning: pos, + Set: mlp.SetAbsoluteDiscord, + PackCount: 4, + }, + }) + + mock.expect("session-open") + + // Make two random players join + mock.message("a", "join", nil) + mock.expect("player-joined-session") + mock.message("b", "join", nil) + mock.expect("player-joined-session") + + // Start the session + mock.message("test-owner", "start", nil) + mock.multiexpect("session-start", "draft-order", "draft-newpack", "draft-newpick", "draft-newpick") + } + + makeSession("even", draftbot.PosEven) + makeSession("random", draftbot.PosRandom) +} + func (m *MockServer) expect(typ string) *room.Message { for { select { @@ -223,7 +350,7 @@ func (m *MockServer) expect(typ string) *room.Message { // Check expected type if msg.Message.Type != typ { // Oh noes - m.t.Fatalf("Expected message \"%s\" but got \"%s\"", typ, msg.Message.Type) + m.t.Fatalf("Expected message \"%s\" but got \"%s\" (%v)", typ, msg.Message.Type, msg.Message.Data) } return msg.Message case <-time.After(m.timeout): @@ -271,7 +398,7 @@ func (m *MockServer) multiexpect(types ...string) { func (m *MockServer) message(from string, typ string, data interface{}) { m.t.Logf("<- <%s> %s (%v)", from, typ, data) m.in <- room.ServerMessage{ - RoomID: TestRoomName, + RoomID: m.roomName, Type: room.MsgMessage, Message: &room.Message{ From: from, @@ -281,3 +408,27 @@ func (m *MockServer) message(from string, typ string, data interface{}) { }, } } + +func TestMain(m *testing.M) { + testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + switch req.URL.Path { + case "/nopenope.json": + // 404 + http.Error(res, "Not found", http.StatusNotFound) + case "/broken.json": + // Broken response + fmt.Fprintf(res, "{{{{") + case "/pr.json": + fmt.Fprintf(res, mlptestdata.SetPremiere) + case "/cube": + fmt.Fprintf(res, testdata.TestGenericCubeFile) + case "/i8pcube": + fmt.Fprintf(res, testdata.TestI8PCubeFile) + default: + fmt.Fprintf(res, mlptestdata.SetFriendForever) + } + })) + mlp.HTTPSource = testServer.URL + "/" + defer testServer.Close() + os.Exit(m.Run()) +} diff --git a/draftbot/go.mod b/draftbot/go.mod index 7e88dad..cc8f5bc 100644 --- a/draftbot/go.mod +++ b/draftbot/go.mod @@ -5,7 +5,7 @@ go 1.12 require ( git.fromouter.space/Artificiale/moa v0.0.1-p2 git.fromouter.space/mcg/cardgage v0.0.4 - git.fromouter.space/mcg/draft v0.0.3 + git.fromouter.space/mcg/draft v0.0.4 github.com/go-kit/kit v0.8.0 github.com/mitchellh/mapstructure v1.1.2 ) diff --git a/draftbot/go.sum b/draftbot/go.sum index 4d1cbb1..b18cad1 100644 --- a/draftbot/go.sum +++ b/draftbot/go.sum @@ -5,6 +5,8 @@ git.fromouter.space/mcg/cardgage v0.0.4 h1:LHMUeNMh0QiMkM3TgsLe9l5sDmanQrej6UiWS git.fromouter.space/mcg/cardgage v0.0.4/go.mod h1:vCmJ9HRdRGSWg2YQW9oNG7geYACdgWYmzL+zZdrsYhQ= git.fromouter.space/mcg/draft v0.0.3 h1:+Bq7cMPuBH6c8UCexaegJjc/UbFTE1+mAikcuvjtW+c= git.fromouter.space/mcg/draft v0.0.3/go.mod h1:QQmDm9FgAZL3b2/pIDd4Eo608SxMiCQQe5vIybe/CDY= +git.fromouter.space/mcg/draft v0.0.4 h1:BuyjLuKnNUrPQe33CaEMCv+OIe1vPBDdeD2Kl2Fklvs= +git.fromouter.space/mcg/draft v0.0.4/go.mod h1:QQmDm9FgAZL3b2/pIDd4Eo608SxMiCQQe5vIybe/CDY= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/DataDog/datadog-go v2.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= diff --git a/draftbot/main.go b/draftbot/main.go index 080496f..58fcc04 100644 --- a/draftbot/main.go +++ b/draftbot/main.go @@ -13,6 +13,7 @@ import ( "git.fromouter.space/Artificiale/moa/sd" botapi "git.fromouter.space/mcg/cardgage/client/bot" + "git.fromouter.space/mcg/draft/mlp" "github.com/go-kit/kit/log" ) @@ -69,6 +70,12 @@ func runBot(name string, games, tags []string) error { return err } + // Load all sets into memory + err = mlp.LoadAllSets() + if err != nil { + return err + } + addr := fmt.Sprintf("ws://%s:%d/bot", roomsvc.Service.Address, roomsvc.Service.Port) wsbot, err := botapi.NewBot(addr, botapi.Params{ diff --git a/draftbot/testdata/cube.go b/draftbot/testdata/cube.go new file mode 100644 index 0000000..011a660 --- /dev/null +++ b/draftbot/testdata/cube.go @@ -0,0 +1,130 @@ +package testdata + +// TestGenericCubeFile is an example file for a generic cube draft +const TestGenericCubeFile = `pr1 +pr2 +pr3 +pr4 +pr5 +pr6 +pr7 +pr8 +pr9 +pr10 +pr11 +pr12 +pr13 +pr14 +pr15 +pr16 +pr17 +pr18 +pr19 +pr20 +pr21 +pr22 +pr23 +pr24 +pr25 +pr26 +pr27 +pr28 +pr29 +pr30 +` + +// TestI8PCubeFile is an example file for a I8Pages' style cube draft +const TestI8PCubeFile = `{ + "Schema": [ + { "Amount": 1, "Type": "blue" }, + { "Amount": 1, "Type": "orange" }, + { "Amount": 1, "Type": "pink" }, + { "Amount": 1, "Type": "purple" }, + { "Amount": 1, "Type": "white" }, + { "Amount": 1, "Type": "yellow" }, + { "Amount": 1, "Type": "none" }, + { "Amount": 2, "Type": "multi" }, + { "Amount": 2, "Type": "entry" }, + { "Amount": 1, "Type": "all" } + ], + "Cards": { + "blue": [ + "pr54", + "pr54", + "pr54" + ], + "orange": [ + "pr54", + "pr54", + "pr54" + ], + "pink": [ + "pr54", + "pr54", + "pr54" + ], + "purple": [ + "pr54", + "pr54", + "pr54" + ], + "white": [ + "pr54", + "pr54", + "pr54" + ], + "yellow": [ + "pr54", + "pr54", + "pr54" + ], + "none": [ + "pr54", + "pr54", + "pr54" + ], + "multi": [ + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54" + ], + "entry": [ + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54" + ], + "problem": [ + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54", + "pr54" + ] + } +}`