package router_test import ( "encoding/json" "net/http" "net/http/httptest" "testing" "galaxy/model/rest" "galaxy/util" "galaxy/game/internal/router" "github.com/google/uuid" "github.com/stretchr/testify/assert" ) func TestInit(t *testing.T) { root, cleanup := util.CreateWorkDir(t) defer cleanup() r := router.SetupRouter(newService(t, root)) payload := generateInitRequest(10) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/admin/init", asBody(payload)) r.ServeHTTP(w, req) assert.Equal(t, http.StatusCreated, w.Code, w.Body) var initResponse rest.StateResponse assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &initResponse)) assert.Equal(t, payload.GameID, initResponse.ID, "engine must echo the orchestrator-supplied gameId") } func TestInitValidators(t *testing.T) { r := setupRouter() payload := generateInitRequest(9) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/admin/init", asBody(payload)) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code, w.Body) } func TestInitRejectsNilUUID(t *testing.T) { root, cleanup := util.CreateWorkDir(t) defer cleanup() r := router.SetupRouter(newService(t, root)) payload := generateInitRequest(10) payload.GameID = uuid.Nil w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/admin/init", asBody(payload)) r.ServeHTTP(w, req) assert.Equal(t, http.StatusBadRequest, w.Code, w.Body) } func TestInitRejectsExistingGameWithDifferentID(t *testing.T) { root, cleanup := util.CreateWorkDir(t) defer cleanup() r := router.SetupRouter(newService(t, root)) first := generateInitRequest(10) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/admin/init", asBody(first)) r.ServeHTTP(w, req) assert.Equal(t, http.StatusCreated, w.Code, w.Body) second := generateInitRequest(10) second.GameID = uuid.New() w = httptest.NewRecorder() req, _ = http.NewRequest("POST", "/api/v1/admin/init", asBody(second)) r.ServeHTTP(w, req) assert.Equal(t, http.StatusConflict, w.Code, w.Body) }