2020-05-31 11:18:26 +01:00
|
|
|
package server
|
2020-05-29 17:56:26 +01:00
|
|
|
|
|
|
|
import (
|
2020-05-31 11:22:20 +01:00
|
|
|
"bytes"
|
2020-05-29 17:56:26 +01:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2020-06-02 17:59:33 +01:00
|
|
|
|
2020-06-04 21:59:00 +01:00
|
|
|
"github.com/mdiluz/rove/pkg/game"
|
2020-06-02 17:59:33 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-29 17:56:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleStatus(t *testing.T) {
|
|
|
|
request, _ := http.NewRequest(http.MethodGet, "/status", nil)
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
2020-05-31 19:15:57 +01:00
|
|
|
s := NewServer()
|
2020-06-04 17:53:25 +01:00
|
|
|
s.wrapHandler(http.MethodGet, HandleStatus)(response, request)
|
2020-05-29 17:56:26 +01:00
|
|
|
|
2020-05-31 11:18:26 +01:00
|
|
|
var status StatusResponse
|
2020-05-29 17:56:26 +01:00
|
|
|
json.NewDecoder(response.Body).Decode(&status)
|
|
|
|
|
|
|
|
if status.Ready != true {
|
|
|
|
t.Errorf("got false for /status")
|
|
|
|
}
|
2020-06-02 15:54:22 +01:00
|
|
|
|
|
|
|
if len(status.Version) == 0 {
|
|
|
|
t.Errorf("got empty version info")
|
|
|
|
}
|
2020-05-29 17:56:26 +01:00
|
|
|
}
|
2020-05-31 11:22:20 +01:00
|
|
|
|
|
|
|
func TestHandleRegister(t *testing.T) {
|
|
|
|
data := RegisterData{Name: "one"}
|
|
|
|
b, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request, _ := http.NewRequest(http.MethodPost, "/register", bytes.NewReader(b))
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
2020-05-31 19:15:57 +01:00
|
|
|
s := NewServer()
|
2020-06-04 17:53:25 +01:00
|
|
|
s.wrapHandler(http.MethodPost, HandleRegister)(response, request)
|
2020-05-31 11:22:20 +01:00
|
|
|
|
|
|
|
var status RegisterResponse
|
|
|
|
json.NewDecoder(response.Body).Decode(&status)
|
|
|
|
|
|
|
|
if status.Success != true {
|
|
|
|
t.Errorf("got false for /register")
|
|
|
|
}
|
|
|
|
}
|
2020-06-02 17:59:33 +01:00
|
|
|
|
|
|
|
func TestHandleSpawn(t *testing.T) {
|
|
|
|
s := NewServer()
|
|
|
|
a, err := s.accountant.RegisterAccount("test")
|
|
|
|
assert.NoError(t, err, "Error registering account")
|
2020-06-04 18:36:26 +01:00
|
|
|
data := SpawnData{Id: a.Id.String()}
|
2020-06-02 17:59:33 +01:00
|
|
|
|
|
|
|
b, err := json.Marshal(data)
|
|
|
|
assert.NoError(t, err, "Error marshalling data")
|
|
|
|
|
|
|
|
request, _ := http.NewRequest(http.MethodPost, "/spawn", bytes.NewReader(b))
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
2020-06-04 17:53:25 +01:00
|
|
|
s.wrapHandler(http.MethodPost, HandleSpawn)(response, request)
|
2020-06-02 17:59:33 +01:00
|
|
|
|
|
|
|
var status SpawnResponse
|
|
|
|
json.NewDecoder(response.Body).Decode(&status)
|
|
|
|
|
|
|
|
if status.Success != true {
|
|
|
|
t.Errorf("got false for /spawn")
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 18:40:19 +01:00
|
|
|
|
|
|
|
func TestHandleCommands(t *testing.T) {
|
|
|
|
s := NewServer()
|
|
|
|
a, err := s.accountant.RegisterAccount("test")
|
|
|
|
assert.NoError(t, err, "Error registering account")
|
|
|
|
|
2020-06-04 21:17:43 +01:00
|
|
|
// Spawn the rover rover for the account
|
|
|
|
_, inst, err := s.SpawnRoverForAccount(a.Id)
|
2020-06-03 18:40:19 +01:00
|
|
|
|
2020-06-04 21:59:00 +01:00
|
|
|
pos, err := s.world.RoverPosition(inst)
|
|
|
|
assert.NoError(t, err, "Couldn't get rover position")
|
|
|
|
|
2020-06-03 18:40:19 +01:00
|
|
|
data := CommandsData{
|
2020-06-04 18:36:26 +01:00
|
|
|
Id: a.Id.String(),
|
2020-06-03 18:40:19 +01:00
|
|
|
Commands: []Command{
|
|
|
|
{
|
2020-06-04 18:54:33 +01:00
|
|
|
Command: CommandMove,
|
2020-06-05 16:37:52 +01:00
|
|
|
Bearing: "N",
|
2020-06-04 18:54:33 +01:00
|
|
|
Duration: 1,
|
2020-06-03 18:40:19 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(data)
|
|
|
|
assert.NoError(t, err, "Error marshalling data")
|
|
|
|
|
|
|
|
request, _ := http.NewRequest(http.MethodPost, "/commands", bytes.NewReader(b))
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
2020-06-04 17:53:25 +01:00
|
|
|
s.wrapHandler(http.MethodPost, HandleCommands)(response, request)
|
2020-06-03 18:40:19 +01:00
|
|
|
|
2020-06-04 18:36:26 +01:00
|
|
|
var status CommandsResponse
|
2020-06-03 18:40:19 +01:00
|
|
|
json.NewDecoder(response.Body).Decode(&status)
|
|
|
|
|
|
|
|
if status.Success != true {
|
|
|
|
t.Errorf("got false for /commands")
|
|
|
|
}
|
|
|
|
|
2020-06-04 21:59:00 +01:00
|
|
|
attrib, err := s.world.RoverAttributes(inst)
|
|
|
|
assert.NoError(t, err, "Couldn't get rover attribs")
|
2020-06-04 18:54:33 +01:00
|
|
|
|
2020-06-04 21:59:00 +01:00
|
|
|
pos2, err := s.world.RoverPosition(inst)
|
|
|
|
assert.NoError(t, err, "Couldn't get rover position")
|
|
|
|
pos.Add(game.Vector{X: 0.0, Y: attrib.Speed * 1}) // Should have moved north by the speed and duration
|
|
|
|
assert.Equal(t, pos, pos2, "Rover should have moved by bearing")
|
2020-06-03 18:40:19 +01:00
|
|
|
}
|
2020-06-04 22:02:33 +01:00
|
|
|
|
2020-06-04 22:14:55 +01:00
|
|
|
func TestHandleRadar(t *testing.T) {
|
2020-06-04 22:02:33 +01:00
|
|
|
s := NewServer()
|
|
|
|
a, err := s.accountant.RegisterAccount("test")
|
|
|
|
assert.NoError(t, err, "Error registering account")
|
|
|
|
|
|
|
|
// Spawn the rover rover for the account
|
|
|
|
_, _, err = s.SpawnRoverForAccount(a.Id)
|
|
|
|
|
2020-06-04 22:14:55 +01:00
|
|
|
data := RadarData{
|
2020-06-04 22:02:33 +01:00
|
|
|
Id: a.Id.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(data)
|
|
|
|
assert.NoError(t, err, "Error marshalling data")
|
|
|
|
|
2020-06-04 22:14:55 +01:00
|
|
|
request, _ := http.NewRequest(http.MethodPost, "/radar", bytes.NewReader(b))
|
2020-06-04 22:02:33 +01:00
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
2020-06-04 22:14:55 +01:00
|
|
|
s.wrapHandler(http.MethodPost, HandleRadar)(response, request)
|
2020-06-04 22:02:33 +01:00
|
|
|
|
2020-06-04 22:14:55 +01:00
|
|
|
var status RadarResponse
|
2020-06-04 22:02:33 +01:00
|
|
|
json.NewDecoder(response.Body).Decode(&status)
|
|
|
|
|
|
|
|
if status.Success != true {
|
2020-06-04 22:14:55 +01:00
|
|
|
t.Errorf("got false for /radar")
|
2020-06-04 22:02:33 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 22:14:55 +01:00
|
|
|
// TODO: Verify the radar information
|
2020-06-04 22:02:33 +01:00
|
|
|
}
|