package router_test import ( "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/google/uuid" "github.com/iliadenisov/galaxy/internal/controller" "github.com/iliadenisov/galaxy/internal/model/rest" "github.com/iliadenisov/galaxy/internal/router" "github.com/iliadenisov/galaxy/internal/router/handler" "github.com/iliadenisov/galaxy/internal/util" "github.com/stretchr/testify/assert" ) func TestGetTurn(t *testing.T) { root, cleanup := util.CreateWorkDir(t) defer cleanup() r := router.SetupRouter(handler.NewDefaultConfigExecutor(func(p *controller.Param) { p.StoragePath = root })) // create game payload := generateInitRequest(10) w := httptest.NewRecorder() req, _ := http.NewRequest("POST", "/api/v1/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.NoError(t, uuid.Validate(initResponse.ID.String())) assert.NotEqual(t, uuid.Nil, uuid.MustParse(initResponse.ID.String())) assert.Equal(t, uint(0), initResponse.Turn) assert.Equal(t, uint(0), initResponse.Stage) // generate next turn w = httptest.NewRecorder() req, _ = http.NewRequest("PUT", "/api/v1/turn", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code, w.Body) var turnResponse rest.StateResponse assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &turnResponse)) assert.NoError(t, uuid.Validate(turnResponse.ID.String())) assert.Equal(t, initResponse.ID, turnResponse.ID) assert.Equal(t, uint(1), turnResponse.Turn) assert.Equal(t, uint(0), turnResponse.Stage) // validate status w = httptest.NewRecorder() req, _ = http.NewRequest("GET", "/api/v1/status", nil) r.ServeHTTP(w, req) assert.Equal(t, http.StatusOK, w.Code, w.Body) var stateResponse rest.StateResponse assert.NoError(t, json.Unmarshal(w.Body.Bytes(), &stateResponse)) assert.Equal(t, initResponse.ID, stateResponse.ID) assert.Equal(t, uint(1), stateResponse.Turn) assert.Equal(t, uint(0), stateResponse.Stage) }