Files
galaxy-game/game/internal/controller/generate_game_test.go
T
Ilia Denisov 15d35f6f1f
Tests · Go / test (push) Successful in 1m57s
Tests · Integration / integration (pull_request) Successful in 1m48s
Tests · Go / test (pull_request) Successful in 2m0s
feat(game): canonical gameId in POST /api/v1/admin/init
Engine no longer mints its own game UUID. The orchestrator (backend)
generates the game UUID at game-create time and passes it in the
admin/init request body as the required `gameId` field, so the value
that names the engine container and host bind-mount directory also
ends up inside the engine's state.json.

The engine rejects the zero UUID with 400 and any init that conflicts
with an existing state.json with 409 (a second init on the same gameId
is also a conflict; full idempotency is not part of the contract).

Updates rest.InitRequest, openapi.yaml (schema + 409 response),
controller.GenerateGame/NewGame/buildGameOnMap signatures, the engine
HTTP handler/executor, the backend runtime worker, and the relevant
unit and contract tests. Documentation in game/README.md,
docs/ARCHITECTURE.md, backend/README.md, and backend/docs/{runtime,flows}.md
is updated in the same patch.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-29 13:13:31 +02:00

104 lines
2.7 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()
assert.NoError(t, r.Lock())
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)
assert.NoError(t, r.Release())
}
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)
}
configure := func(p *controller.Param) { p.StoragePath = root }
firstID := uuid.New()
_, err := controller.GenerateGame(configure, firstID, races)
assert.NoError(t, err)
_, err = controller.GenerateGame(configure, 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)
}
_, err := controller.GenerateGame(func(p *controller.Param) { p.StoragePath = root }, uuid.Nil, races)
assert.ErrorIs(t, err, controller.ErrGameInitNilUUID)
}