Implement a test for transfer and fix bugs
This commit is contained in:
parent
fdfcc88540
commit
57621d169a
2 changed files with 66 additions and 13 deletions
|
@ -1,6 +1,7 @@
|
|||
package rove
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/mdiluz/rove/pkg/maths"
|
||||
|
@ -159,6 +160,57 @@ func TestCommand_Salvage(t *testing.T) {
|
|||
assert.Equal(t, roveapi.Object_ObjectUnknown, obj.Type)
|
||||
}
|
||||
|
||||
func TestCommand_Transfer(t *testing.T) {
|
||||
w := NewWorld(8)
|
||||
acc, err := w.Accountant.RegisterAccount("tmp")
|
||||
assert.NoError(t, err)
|
||||
nameA, err := w.SpawnRover(acc.Name)
|
||||
assert.NoError(t, err)
|
||||
|
||||
infoA, err := w.GetRover(nameA)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Drop a dormant rover on the current position
|
||||
infoB := DefaultRover()
|
||||
infoB.Name = "abc"
|
||||
infoB.Pos = infoA.Pos
|
||||
data, err := json.Marshal(infoB)
|
||||
assert.NoError(t, err)
|
||||
w.Atlas.SetObject(infoA.Pos, Object{Type: roveapi.Object_RoverDormant, Data: data})
|
||||
|
||||
// Enqueue a transfer as well as a dud command
|
||||
err = w.Enqueue(nameA,
|
||||
&roveapi.Command{Command: roveapi.CommandType_transfer},
|
||||
&roveapi.Command{Command: roveapi.CommandType_broadcast, Data: []byte("xyz")})
|
||||
assert.NoError(t, err)
|
||||
w.Tick()
|
||||
|
||||
// Ensure both command queues are empty
|
||||
assert.Empty(t, w.CommandQueue[nameA])
|
||||
assert.Empty(t, w.CommandQueue[infoB.Name])
|
||||
|
||||
// Verify the account now controls the new rover
|
||||
accountRover, err := w.Accountant.GetValue(acc.Name, "rover")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, infoB.Name, accountRover)
|
||||
|
||||
// Verify the position now has a dormant rover
|
||||
_, obj := w.Atlas.QueryPosition(infoA.Pos)
|
||||
assert.Equal(t, roveapi.Object_RoverDormant, obj.Type)
|
||||
|
||||
// Verify the stored data matches
|
||||
var stored Rover
|
||||
err = json.Unmarshal(obj.Data, &stored)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, infoA.Name, stored.Name)
|
||||
|
||||
// Verify the new rover data matches what we put in
|
||||
infoB2, err := w.GetRover(infoB.Name)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, infoB.Name, infoB2.Name)
|
||||
|
||||
}
|
||||
|
||||
func TestCommand_Invalid(t *testing.T) {
|
||||
w := NewWorld(8)
|
||||
name, err := w.SpawnRover("tmp")
|
||||
|
|
|
@ -378,28 +378,26 @@ func (w *World) RoverTransfer(rover string) (string, error) {
|
|||
oldRover.AddLogEntryf("transferring to dormant rover %s", newRover.Name)
|
||||
newRover.AddLogEntryf("transferred from rover %s", oldRover.Name)
|
||||
|
||||
// Clear the old owner
|
||||
// Transfer the ownership
|
||||
w.Accountant.AssignData(oldRover.Owner, "rover", newRover.Name)
|
||||
newRover.Owner = oldRover.Owner
|
||||
oldRover.Owner = ""
|
||||
|
||||
// Marshal old rover
|
||||
// Place the old rover in the world
|
||||
oldRoverData, err := json.Marshal(oldRover)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Add this new rover to tracking
|
||||
w.Rovers[newRover.Name] = &newRover
|
||||
|
||||
// Swap account rover to the dormant one
|
||||
newRover.Owner = oldRover.Owner
|
||||
w.Accountant.AssignData(oldRover.Owner, "rover", newRover.Name)
|
||||
|
||||
// Place the old rover into the world
|
||||
w.Atlas.SetObject(oldRover.Pos, Object{Type: roveapi.Object_RoverDormant, Data: oldRoverData})
|
||||
|
||||
// Remove old rover from current tracking
|
||||
// Swap the rovers in the tracking
|
||||
w.Rovers[newRover.Name] = &newRover
|
||||
delete(w.Rovers, oldRover.Name)
|
||||
|
||||
// Clear the command queues for both rovers
|
||||
delete(w.CommandQueue, oldRover.Name)
|
||||
delete(w.CommandQueue, newRover.Name)
|
||||
|
||||
return newRover.Name, nil
|
||||
}
|
||||
|
||||
|
@ -599,7 +597,10 @@ func (w *World) Tick() {
|
|||
}
|
||||
|
||||
// Extract the first command in the queue
|
||||
w.CommandQueue[rover] = cmds[1:]
|
||||
// Only if the command queue still has entries
|
||||
if _, ok := w.CommandQueue[rover]; ok {
|
||||
w.CommandQueue[rover] = cmds[1:]
|
||||
}
|
||||
|
||||
} else {
|
||||
// Clean out the empty entry
|
||||
|
|
Loading…
Add table
Reference in a new issue