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>
104 lines
2.6 KiB
Go
104 lines
2.6 KiB
Go
package controller_test
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"galaxy/util"
|
|
|
|
"galaxy/game/internal/controller"
|
|
"galaxy/game/internal/model/game"
|
|
"galaxy/game/internal/repo"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNewGame(t *testing.T) {
|
|
root, cleanup := util.CreateWorkDir(t)
|
|
defer cleanup()
|
|
|
|
r, err := repo.NewFileRepo(root)
|
|
assert.NoError(t, err)
|
|
|
|
players := 20
|
|
races := make([]string, players)
|
|
for i := range players {
|
|
races[i] = fmt.Sprintf("race_%02d", i)
|
|
}
|
|
requestedID := uuid.New()
|
|
gameID, err := controller.NewGame(r, requestedID, races)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, requestedID, gameID, "NewGame must echo the supplied gameID")
|
|
|
|
assert.FileExists(t, filepath.Join(root, "state.json"))
|
|
assert.FileExists(t, filepath.Join(root, "0000/state.json"))
|
|
|
|
g, err := r.LoadState()
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, requestedID, g.ID, "persisted game.ID must match the supplied gameID")
|
|
assert.Equal(t, uint(0), g.Turn)
|
|
assert.Equal(t, players, len(g.Race))
|
|
|
|
for r := range g.Race {
|
|
assert.NotEqual(t, uuid.Nil, g.Race[r].ID)
|
|
assert.Equal(t, players-1, len(g.Race[r].Relations))
|
|
assert.Equal(t, uint(10), g.Race[r].TTL)
|
|
for i := range g.Race[r].Relations {
|
|
assert.NotEqual(t, uuid.Nil, g.Race[r].Relations[i].RaceID)
|
|
if g.Race[r].Relations[i].RaceID == g.Race[r].ID {
|
|
assert.Fail(t, "race relation with itself")
|
|
}
|
|
assert.Equal(t, game.RelationWar, g.Race[r].Relations[i].Relation)
|
|
}
|
|
}
|
|
|
|
numShuffled := false
|
|
for i := range g.Map.Planet {
|
|
p := &g.Map.Planet[i]
|
|
if strings.HasPrefix(p.Name, "HW") || strings.HasPrefix(p.Name, "DW") {
|
|
assert.True(t, p.Owned())
|
|
assert.NotNil(t, p.Owner)
|
|
assert.NotEqual(t, uuid.Nil, *p.Owner)
|
|
}
|
|
numShuffled = numShuffled || p.Number != uint(i)
|
|
}
|
|
assert.True(t, numShuffled)
|
|
}
|
|
|
|
func TestGenerateGameRejectsExistingState(t *testing.T) {
|
|
root, cleanup := util.CreateWorkDir(t)
|
|
defer cleanup()
|
|
|
|
races := make([]string, 10)
|
|
for i := range races {
|
|
races[i] = fmt.Sprintf("race_%02d", i)
|
|
}
|
|
svc, err := controller.NewService(root)
|
|
assert.NoError(t, err)
|
|
|
|
firstID := uuid.New()
|
|
_, err = svc.GenerateGame(firstID, races)
|
|
assert.NoError(t, err)
|
|
|
|
_, err = svc.GenerateGame(uuid.New(), races)
|
|
assert.ErrorIs(t, err, controller.ErrGameAlreadyInit)
|
|
}
|
|
|
|
func TestGenerateGameRejectsNilUUID(t *testing.T) {
|
|
root, cleanup := util.CreateWorkDir(t)
|
|
defer cleanup()
|
|
|
|
races := make([]string, 10)
|
|
for i := range races {
|
|
races[i] = fmt.Sprintf("race_%02d", i)
|
|
}
|
|
|
|
svc, err := controller.NewService(root)
|
|
assert.NoError(t, err)
|
|
_, err = svc.GenerateGame(uuid.Nil, races)
|
|
assert.ErrorIs(t, err, controller.ErrGameInitNilUUID)
|
|
}
|