af30846091
Validation of a player's order now applies every command against a transient game-state snapshot and records the per-command outcome (cmdApplied, cmdErrorCode) in each command's meta. The order is persisted even when some commands are rejected, and the response is 202 + UserGamesOrder so clients can surface the partial failure without the chain collapsing into "downstream service is unavailable". Pkg/error consts are reshelved onto three explicit ranges with a package doc and helpers (IsInternalCode/IsInputCode/IsGameStateCode): 1xxx internal/server (500/501), 2xxx structural input (400), 3xxx game-state per-command rejection (400 when escaping HTTP, otherwise recorded as cmdErrorCode). Two pre-existing typos fixed mechanically (ErrBeakGroupNumberNotEnough -> ErrBreakGroupNumberNotEnough, ErrRaceExinct -> ErrRaceExtinct) along with all callsites. Engine errorResponse maps *GenericError by shelf rather than mapping everything to 500. The Quit-not-last structural check in Controller.ValidateOrder is preserved and its type assertion fixed (was a value assertion against a pointer-typed command, so the check silently never fired). Backend, gateway and UI are unchanged — they were already correct on the 202 path; only the engine collapsing per-command rejection into 500 was needed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
65 lines
2.3 KiB
Go
65 lines
2.3 KiB
Go
package controller_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
e "galaxy/error"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"galaxy/game/internal/model/game"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestShipGroupSend(t *testing.T) {
|
|
c, g := newCache()
|
|
// group #1 - in_orbit, free to upgrade
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 10))
|
|
// group #2 - in_space
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1))
|
|
c.ShipGroup(1).StateInSpace = &InSpace
|
|
// group #3 - in_orbit, unmovable
|
|
g.ShipClassCreate(Race_0.Name, "Fortress", 0, 50, 30, 100, 0)
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, "Fortress", R0_Planet_0_num, 1))
|
|
|
|
shiplessRace := "Shipless"
|
|
ri, _ := c.AddRace(shiplessRace)
|
|
assert.NoError(t, c.ShipClassCreate(ri, "Drone", 1, 0, 0, 0, 0))
|
|
sgi := c.CreateShipsUnsafe_T(ri, c.MustShipClass(ri, "Drone").ID, R0_Planet_0_num, 1)
|
|
|
|
assert.ErrorContains(t,
|
|
g.ShipGroupSend(UnknownRace, c.ShipGroup(0).ID, 2),
|
|
e.GenericErrorText(e.ErrInputUnknownRace))
|
|
assert.ErrorContains(t,
|
|
g.ShipGroupSend(Race_Extinct.Name, c.ShipGroup(0).ID, 2),
|
|
e.GenericErrorText(e.ErrRaceExtinct))
|
|
assert.ErrorContains(t,
|
|
g.ShipGroupSend(Race_0.Name, uuid.New(), 2),
|
|
e.GenericErrorText(e.ErrInputEntityNotExists))
|
|
assert.ErrorContains(t,
|
|
g.ShipGroupSend(Race_0.Name, c.ShipGroup(0).ID, 222),
|
|
e.GenericErrorText(e.ErrInputEntityNotExists))
|
|
assert.ErrorContains(t,
|
|
g.ShipGroupSend(Race_0.Name, c.ShipGroup(1).ID, 1),
|
|
e.GenericErrorText(e.ErrShipsBusy))
|
|
assert.ErrorContains(t,
|
|
g.ShipGroupSend(shiplessRace, c.ShipGroup(sgi).ID, 2),
|
|
e.GenericErrorText(e.ErrSendShipOwnerHasNoPlanets))
|
|
assert.ErrorContains(t,
|
|
g.ShipGroupSend(Race_0.Name, c.ShipGroup(2).ID, 2),
|
|
e.GenericErrorText(e.ErrSendShipHasNoDrives))
|
|
assert.ErrorContains(t,
|
|
g.ShipGroupSend(Race_0.Name, c.ShipGroup(0).ID, 3),
|
|
e.GenericErrorText(e.ErrSendUnreachableDestination))
|
|
|
|
assert.NoError(t, g.ShipGroupSend(Race_0.Name, c.ShipGroup(0).ID, R0_Planet_2_num)) // send #0
|
|
assert.Equal(t, game.StateLaunched, c.ShipGroup(0).State())
|
|
assert.NotNil(t, c.ShipGroup(0).StateInSpace)
|
|
assert.Nil(t, c.ShipGroup(0).StateInSpace.X)
|
|
assert.Nil(t, c.ShipGroup(0).StateInSpace.Y)
|
|
|
|
assert.NoError(t, g.ShipGroupSend(Race_0.Name, c.ShipGroup(0).ID, R0_Planet_0_num)) // un-send #0
|
|
assert.Equal(t, game.StateInOrbit, c.ShipGroup(0).State())
|
|
}
|