601970b028
Three-stage refactor of the game-engine plumbing (game logic untouched): Stage 1 — lock-free persistence + admin serialisation. Remove the file lock from repo/fs (the .lock file, the Read/Write-vs-*Safe duality and the dead ReadSafe polling) and replace the two-step rename with a single atomic rename so concurrent reads are torn-free without a lock. Serialise the state-mutating admin writers (init/turn/banish) with one shared router LimitMiddleware, rewritten to block on the request context instead of a racy shared 100ms timer. Stage 2 — remove the obsolete immediate-command path end to end. Players submit through PUT /api/v1/order; the legacy PUT /api/v1/command path is deleted across game (route, handler, 24 command factories, Ctrl), backend (Commands handler/route, engineclient.ExecuteCommands), gateway (dispatch + executeUserGamesCommand + routing entry), the FlatBuffers/model contract (UserGamesCommand[Response]) and transcoder, plus every affected OpenAPI/README/FUNCTIONAL/ARCHITECTURE doc. The integration proxy test is converted to the order path. Stage 3 — flatten the REST->engine wrapper. Replace the executor adapter, the controller package functions and RepoController with one concrete controller.Service; drop the single-implementation Repo and Storage interfaces (repo.Repo / fs.FS are now concrete). Handlers depend on a thin handler.Engine seam and own the domain->REST projection; storage is resolved once at startup instead of per request. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
135 lines
3.4 KiB
Go
135 lines
3.4 KiB
Go
package router_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"galaxy/model/order"
|
|
"galaxy/model/report"
|
|
|
|
"galaxy/game/internal/controller"
|
|
"galaxy/game/internal/model/game"
|
|
"galaxy/game/internal/router"
|
|
"galaxy/game/internal/router/handler"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var (
|
|
commandNoErrorsStatus = http.StatusAccepted
|
|
commandDefaultActor = "Gorlum"
|
|
apiCommandMethod = "PUT"
|
|
apiOrderPath = "/api/v1/order"
|
|
validId1 = id()
|
|
validId2 = id()
|
|
invalidId = "fd091c69-5976-4775-b2f9-7ba77735afb"
|
|
)
|
|
|
|
func id() string {
|
|
return uuid.New().String()
|
|
}
|
|
|
|
type dummyExecutor struct {
|
|
CommandsExecuted int
|
|
|
|
// ValidateOrderResult, when non-nil, is returned from ValidateOrder.
|
|
// When nil, ValidateOrder synthesises an order from the received args
|
|
// so the response body is non-empty for status assertions.
|
|
ValidateOrderResult *order.UserGamesOrder
|
|
ValidateOrderErr error
|
|
|
|
// FetchOrder controls and observes calls to FetchOrder.
|
|
FetchOrderActor string
|
|
FetchOrderTurn uint
|
|
FetchOrderResult *order.UserGamesOrder
|
|
FetchOrderOK bool
|
|
FetchOrderErr error
|
|
|
|
// FetchBattle controls and observes calls to FetchBattle.
|
|
FetchBattleTurn uint
|
|
FetchBattleID uuid.UUID
|
|
FetchBattleResult *report.BattleReport
|
|
FetchBattleOK bool
|
|
FetchBattleErr error
|
|
}
|
|
|
|
func (e *dummyExecutor) ValidateOrder(actor string, cmd ...order.DecodableCommand) (*order.UserGamesOrder, error) {
|
|
e.CommandsExecuted = len(cmd)
|
|
if e.ValidateOrderErr != nil {
|
|
return nil, e.ValidateOrderErr
|
|
}
|
|
if e.ValidateOrderResult != nil {
|
|
return e.ValidateOrderResult, nil
|
|
}
|
|
return &order.UserGamesOrder{
|
|
GameID: uuid.New(),
|
|
UpdatedAt: 1,
|
|
Commands: append([]order.DecodableCommand(nil), cmd...),
|
|
}, nil
|
|
}
|
|
|
|
func (e *dummyExecutor) FetchOrder(actor string, turn uint) (*order.UserGamesOrder, bool, error) {
|
|
e.FetchOrderActor = actor
|
|
e.FetchOrderTurn = turn
|
|
return e.FetchOrderResult, e.FetchOrderOK, e.FetchOrderErr
|
|
}
|
|
|
|
func (e *dummyExecutor) FetchBattle(turn uint, ID uuid.UUID) (*report.BattleReport, bool, error) {
|
|
e.FetchBattleTurn = turn
|
|
e.FetchBattleID = ID
|
|
return e.FetchBattleResult, e.FetchBattleOK, e.FetchBattleErr
|
|
}
|
|
|
|
func (e *dummyExecutor) GenerateGame(gameID uuid.UUID, races []string) (game.State, error) {
|
|
return game.State{ID: gameID}, nil
|
|
}
|
|
|
|
func (e *dummyExecutor) GenerateTurn() (game.State, error) {
|
|
return game.State{}, nil
|
|
}
|
|
|
|
func (e *dummyExecutor) BanishRace(raceName string) error {
|
|
return nil
|
|
}
|
|
|
|
func (e *dummyExecutor) GameState() (game.State, error) {
|
|
return game.State{}, nil
|
|
}
|
|
|
|
func (e *dummyExecutor) LoadReport(actor string, turn uint) (*report.Report, error) {
|
|
return &report.Report{}, nil
|
|
}
|
|
|
|
func setupRouter() *gin.Engine {
|
|
return setupRouterExecutor(newExecutor())
|
|
}
|
|
|
|
func setupRouterExecutor(e handler.Engine) *gin.Engine {
|
|
return router.SetupRouter(e)
|
|
}
|
|
|
|
func newExecutor() handler.Engine {
|
|
return &dummyExecutor{}
|
|
}
|
|
|
|
// newService builds a real controller.Service backed by a storage directory,
|
|
// for handler tests that exercise the engine end to end rather than the fake.
|
|
func newService(t *testing.T, root string) *controller.Service {
|
|
t.Helper()
|
|
svc, err := controller.NewService(root)
|
|
if err != nil {
|
|
t.Fatalf("new service: %v", err)
|
|
}
|
|
return svc
|
|
}
|
|
|
|
func encodeCommand(cmd any) json.RawMessage {
|
|
v, err := json.Marshal(cmd)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|