Files
galaxy-game/gateway/internal/backendclient/routes_test.go
T
Ilia Denisov 601970b028
Tests · Go / test (push) Successful in 2m27s
Tests · UI / test (push) Waiting to run
Tests · Integration / integration (pull_request) Successful in 1m45s
Tests · Go / test (pull_request) Successful in 3m13s
Tests · UI / test (pull_request) Successful in 3m8s
refactor(game): lock-free storage, remove /command, flatten engine wrapper
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>
2026-05-30 13:37:07 +02:00

107 lines
3.4 KiB
Go

package backendclient_test
import (
"context"
"testing"
"galaxy/gateway/internal/backendclient"
"galaxy/gateway/internal/downstream"
lobbymodel "galaxy/model/lobby"
ordermodel "galaxy/model/order"
reportmodel "galaxy/model/report"
usermodel "galaxy/model/user"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Phase 14 follow-up: every authenticated message-type constant
// declared in `pkg/model/<service>` must be wired into the matching
// route table. Without this regression test, adding a new constant
// without registering it surfaces only at runtime as
// `unimplemented: message_type is not routed` — exactly what the
// owner saw when an outdated gateway image missed
// `user.games.order.get`.
func TestRoutesCoverAllAuthenticatedMessageTypes(t *testing.T) {
t.Parallel()
cases := map[string]struct {
expected []string
actual map[string]downstream.Client
}{
"user": {
expected: []string{
usermodel.MessageTypeGetMyAccount,
usermodel.MessageTypeUpdateMyProfile,
usermodel.MessageTypeUpdateMySettings,
usermodel.MessageTypeListMySessions,
usermodel.MessageTypeRevokeMySession,
usermodel.MessageTypeRevokeAllMySessions,
},
actual: backendclient.UserRoutes(nil),
},
"lobby": {
expected: []string{
lobbymodel.MessageTypeMyGamesList,
lobbymodel.MessageTypePublicGamesList,
lobbymodel.MessageTypeMyApplicationsList,
lobbymodel.MessageTypeMyInvitesList,
lobbymodel.MessageTypeOpenEnrollment,
lobbymodel.MessageTypeGameCreate,
lobbymodel.MessageTypeApplicationSubmit,
lobbymodel.MessageTypeInviteRedeem,
lobbymodel.MessageTypeInviteDecline,
},
actual: backendclient.LobbyRoutes(nil),
},
"game": {
expected: []string{
ordermodel.MessageTypeUserGamesOrder,
ordermodel.MessageTypeUserGamesOrderGet,
reportmodel.MessageTypeUserGamesReport,
reportmodel.MessageTypeUserGamesBattle,
},
actual: backendclient.GameRoutes(nil),
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
t.Parallel()
require.Len(t, tc.actual, len(tc.expected),
"%s routes table size diverges from the expected message-type list", name)
for _, mt := range tc.expected {
client, ok := tc.actual[mt]
assert.Truef(t, ok, "%s routes are missing %q", name, mt)
assert.NotNilf(t, client, "%s routes resolve %q to a nil client", name, mt)
}
})
}
}
// Sanity-check that the order-get route really points at the game
// command client (and not, say, the lobby one if a future refactor
// reshuffles the helpers): the route table must dispatch through
// `gameCommandClient.ExecuteCommand`, which in turn calls
// `RESTClient.ExecuteGameCommand`. We exercise this through the
// public Router contract.
func TestUserGamesOrderGetRoutedToGameClient(t *testing.T) {
t.Parallel()
routes := backendclient.GameRoutes(nil)
router := downstream.NewStaticRouter(routes)
client, err := router.Route(ordermodel.MessageTypeUserGamesOrderGet)
require.NoError(t, err)
require.NotNil(t, client)
// Without a live RESTClient the client is the unavailable stub —
// calling ExecuteCommand surfaces the canonical "downstream
// service is unavailable" sentinel rather than the "not routed"
// error we want to keep regression-tested.
_, err = client.ExecuteCommand(context.Background(), downstream.AuthenticatedCommand{
MessageType: ordermodel.MessageTypeUserGamesOrderGet,
})
assert.ErrorIs(t, err, downstream.ErrDownstreamUnavailable)
}