Add more tests!
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Hamcha 2019-06-28 23:07:05 +02:00
parent 98ccf5fe8a
commit 78e07731c8
Signed by: hamcha
GPG key ID: A40413D21021EAEE
5 changed files with 302 additions and 12 deletions

View file

@ -1,16 +1,23 @@
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"
@ -21,6 +28,7 @@ type MockServer struct {
out chan room.BotMessage
t *testing.T
timeout time.Duration
roomName string
}
func makeMockServer(t *testing.T, timeout int) *MockServer {
@ -31,6 +39,7 @@ func makeMockServer(t *testing.T, timeout int) *MockServer {
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())
}

View file

@ -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
)

View file

@ -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=

View file

@ -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{

130
draftbot/testdata/cube.go vendored Normal file
View file

@ -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"
]
}
}`