408da3f201
New public ingress and the first network edge. Framework + a vertical slice of operations end-to-end; remaining ops reuse the same transcode pattern in Stage 7. Contracts (new module scrabble/pkg): - push.proto (backend->gateway gRPC server-stream) + scrabble.fbs (FlatBuffers edge payloads), committed generated Go; buf/flatc Makefiles (dev-time codegen). Backend: - REST handlers on the /api/v1 groups: internal session endpoints (telegram/guest/email login -> mint, resolve, revoke) and the user slice (profile, submit_play, state, lobby enqueue/poll, chat). - internal/notify in-process Publisher hub + internal/pushgrpc gRPC server (BACKEND_GRPC_ADDR) streaming your_turn/opponent_moved/chat/nudge/match_found; emission in game.commit, social, matchmaker. - migration 00005 accounts.is_guest; guests are durable rows excluded from stats; ProvisionGuest; email-as-login (RequestLoginCode/LoginWithCode). Gateway (new module scrabble/gateway): - Connect Gateway service over h2c (Execute + Subscribe), FlatBuffers<->JSON transcode registry, Telegram initData HMAC validator (seam), session cache, token-bucket rate limiter (3 classes), push fan-out hub, backend REST + push gRPC client, admin Basic-Auth reverse proxy. go.work: use ./pkg, ./gateway + replace scrabble/pkg. CI: gateway/**, pkg/** path filters; unit build/vet/test span all three modules. Docs (PLAN, ARCHITECTURE, FUNCTIONAL+ru, TESTING, READMEs) updated; gateway/pkg unit tests + guest/email-login integration tests.
142 lines
4.6 KiB
Go
142 lines
4.6 KiB
Go
package transcode_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
flatbuffers "github.com/google/flatbuffers/go"
|
|
|
|
"scrabble/gateway/internal/backendclient"
|
|
"scrabble/gateway/internal/transcode"
|
|
fb "scrabble/pkg/fbs/scrabblefb"
|
|
)
|
|
|
|
// fakeBackend serves the subset of backend endpoints the slice handlers call.
|
|
func fakeBackend(t *testing.T, h http.HandlerFunc) (*backendclient.Client, func()) {
|
|
t.Helper()
|
|
srv := httptest.NewServer(h)
|
|
c, err := backendclient.New(srv.URL, "localhost:9090", 2_000_000_000)
|
|
if err != nil {
|
|
t.Fatalf("backendclient: %v", err)
|
|
}
|
|
return c, func() {
|
|
_ = c.Close()
|
|
srv.Close()
|
|
}
|
|
}
|
|
|
|
func TestGuestAuthRoundTrip(t *testing.T) {
|
|
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/api/v1/internal/sessions/guest" {
|
|
t.Errorf("unexpected path %q", r.URL.Path)
|
|
}
|
|
_, _ = w.Write([]byte(`{"token":"tok-1","user_id":"u-1","is_guest":true,"display_name":"Guest"}`))
|
|
})
|
|
defer cleanup()
|
|
|
|
reg := transcode.NewRegistry(backend, nil)
|
|
op, ok := reg.Lookup(transcode.MsgAuthGuest)
|
|
if !ok {
|
|
t.Fatal("auth.guest not registered")
|
|
}
|
|
payload, err := op.Handler(context.Background(), transcode.Request{})
|
|
if err != nil {
|
|
t.Fatalf("handler: %v", err)
|
|
}
|
|
sess := fb.GetRootAsSession(payload, 0)
|
|
if string(sess.Token()) != "tok-1" || string(sess.UserId()) != "u-1" || !sess.IsGuest() {
|
|
t.Fatalf("session decoded wrong: token=%q user=%q guest=%v", sess.Token(), sess.UserId(), sess.IsGuest())
|
|
}
|
|
}
|
|
|
|
func TestGameStateRoundTripForwardsUserID(t *testing.T) {
|
|
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
|
|
if got := r.Header.Get("X-User-ID"); got != "u-7" {
|
|
t.Errorf("X-User-ID = %q, want u-7", got)
|
|
}
|
|
if r.URL.Path != "/api/v1/user/games/g-1/state" {
|
|
t.Errorf("unexpected path %q", r.URL.Path)
|
|
}
|
|
_, _ = w.Write([]byte(`{"game":{"id":"g-1","variant":"english","status":"active","players":2,"to_move":1,"seats":[{"seat":0,"account_id":"u-7","score":5}]},"seat":0,"rack":["A","B"],"bag_len":80,"hints_remaining":1}`))
|
|
})
|
|
defer cleanup()
|
|
|
|
reg := transcode.NewRegistry(backend, nil)
|
|
op, _ := reg.Lookup(transcode.MsgGameState)
|
|
|
|
b := flatbuffers.NewBuilder(32)
|
|
gid := b.CreateString("g-1")
|
|
fb.StateRequestStart(b)
|
|
fb.StateRequestAddGameId(b, gid)
|
|
b.Finish(fb.StateRequestEnd(b))
|
|
|
|
payload, err := op.Handler(context.Background(), transcode.Request{Payload: b.FinishedBytes(), UserID: "u-7"})
|
|
if err != nil {
|
|
t.Fatalf("handler: %v", err)
|
|
}
|
|
st := fb.GetRootAsStateView(payload, 0)
|
|
if st.BagLen() != 80 || st.RackLength() != 2 || st.HintsRemaining() != 1 {
|
|
t.Fatalf("state decoded wrong: bag=%d rack=%d hints=%d", st.BagLen(), st.RackLength(), st.HintsRemaining())
|
|
}
|
|
game := st.Game(nil)
|
|
if game == nil || string(game.Id()) != "g-1" || string(game.Variant()) != "english" || game.ToMove() != 1 {
|
|
t.Fatalf("nested game decoded wrong: %+v", game)
|
|
}
|
|
}
|
|
|
|
func TestEnqueueRoundTripEncodesMatch(t *testing.T) {
|
|
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"matched":true,"game":{"id":"g-9","variant":"english","status":"active","players":2,"to_move":0,"seats":[]}}`))
|
|
})
|
|
defer cleanup()
|
|
|
|
reg := transcode.NewRegistry(backend, nil)
|
|
op, _ := reg.Lookup(transcode.MsgLobbyEnqueue)
|
|
|
|
b := flatbuffers.NewBuilder(32)
|
|
v := b.CreateString("english")
|
|
fb.EnqueueRequestStart(b)
|
|
fb.EnqueueRequestAddVariant(b, v)
|
|
b.Finish(fb.EnqueueRequestEnd(b))
|
|
|
|
payload, err := op.Handler(context.Background(), transcode.Request{Payload: b.FinishedBytes(), UserID: "u-1"})
|
|
if err != nil {
|
|
t.Fatalf("handler: %v", err)
|
|
}
|
|
m := fb.GetRootAsMatchResult(payload, 0)
|
|
if !m.Matched() {
|
|
t.Fatal("match result should be matched")
|
|
}
|
|
if g := m.Game(nil); g == nil || string(g.Id()) != "g-9" {
|
|
t.Fatalf("match game decoded wrong: %+v", g)
|
|
}
|
|
}
|
|
|
|
func TestDomainErrorSurfacesBackendCode(t *testing.T) {
|
|
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusConflict)
|
|
_, _ = w.Write([]byte(`{"error":{"code":"not_your_turn","message":"nope"}}`))
|
|
})
|
|
defer cleanup()
|
|
|
|
reg := transcode.NewRegistry(backend, nil)
|
|
op, _ := reg.Lookup(transcode.MsgGameState)
|
|
|
|
b := flatbuffers.NewBuilder(32)
|
|
gid := b.CreateString("g-1")
|
|
fb.StateRequestStart(b)
|
|
fb.StateRequestAddGameId(b, gid)
|
|
b.Finish(fb.StateRequestEnd(b))
|
|
|
|
_, err := op.Handler(context.Background(), transcode.Request{Payload: b.FinishedBytes(), UserID: "u-1"})
|
|
if err == nil {
|
|
t.Fatal("expected backend error")
|
|
}
|
|
code, ok := transcode.DomainCode(err)
|
|
if !ok || code != "not_your_turn" {
|
|
t.Fatalf("DomainCode = (%q, %v), want (not_your_turn, true)", code, ok)
|
|
}
|
|
}
|