Files
scrabble-game/gateway/internal/transcode/transcode_social_test.go
T
Ilia Denisov d733ce3119
Tests · Go / test (push) Successful in 7s
Tests · Integration / integration (push) Successful in 13s
Tests · UI / test (push) Successful in 16s
Stage 8: UI social/account/history surfaces
Wire the deferred Stage 7 surfaces end-to-end (UI -> gateway transcode ->
backend REST -> existing domain services): friends (incl. one-time friend
codes), per-user blocks, friend-game invitations, profile editing + email
binding, the statistics screen, and the in-game history + GCG export.

Friends gain two add paths (interview decision, a deliberate plan change):
one-time 6-digit codes (friend_codes table, 12h TTL, single-use, rate-limited
redeem); and play-gated requests (shared game required) where an explicit
decline is permanent, an ignored request lapses after 30 days, and a code
bypasses a decline. Migration 00006 widens friendships_status_chk and adds
friend_codes.

Lobby notification badge is poll + push: a new generic `notify` event drives
it live; the client polls on open/focus. Language stays a single Settings
control that writes through to the durable account's preferred_language. GCG
export is finished-only (game.ErrGameActive) and shares/downloads the .gcg file.

Tests: backend unit + inttest (friend gate/decline/code, ListInvitations,
GetStats, GCG gate), gateway transcode round-trips + notify constructor, UI
vitest (codecs, win-rate, share choice) + Playwright social specs. Docs: PLAN
(Stage 8 done + refinements + TODO-5), ARCHITECTURE, FUNCTIONAL(+ru), UI_DESIGN,
TESTING, module READMEs.
2026-06-03 19:47:40 +02:00

239 lines
8.6 KiB
Go

package transcode_test
import (
"context"
"net/http"
"testing"
flatbuffers "github.com/google/flatbuffers/go"
"scrabble/gateway/internal/transcode"
fb "scrabble/pkg/fbs/scrabblefb"
)
// targetPayload builds a TargetRequest payload (friend request/cancel, block).
func targetPayload(accountID string) []byte {
b := flatbuffers.NewBuilder(32)
id := b.CreateString(accountID)
fb.TargetRequestStart(b)
fb.TargetRequestAddAccountId(b, id)
b.Finish(fb.TargetRequestEnd(b))
return b.FinishedBytes()
}
func TestFriendsListRoundTripDecodesNames(t *testing.T) {
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("X-User-ID"); got != "u-1" {
t.Errorf("X-User-ID = %q, want u-1", got)
}
if r.URL.Path != "/api/v1/user/friends" {
t.Errorf("unexpected path %q", r.URL.Path)
}
_, _ = w.Write([]byte(`{"friends":[{"account_id":"a-1","display_name":"Ann"},{"account_id":"a-2","display_name":"Bob"}]}`))
})
defer cleanup()
reg := transcode.NewRegistry(backend, nil)
op, ok := reg.Lookup(transcode.MsgFriendsList)
if !ok {
t.Fatal("friends.list not registered")
}
payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1"})
if err != nil {
t.Fatalf("handler: %v", err)
}
fl := fb.GetRootAsFriendList(payload, 0)
if fl.FriendsLength() != 2 {
t.Fatalf("friends length = %d, want 2", fl.FriendsLength())
}
var f fb.AccountRef
fl.Friends(&f, 1)
if string(f.AccountId()) != "a-2" || string(f.DisplayName()) != "Bob" {
t.Fatalf("friend[1] = (%q, %q), want (a-2, Bob)", f.AccountId(), f.DisplayName())
}
}
func TestFriendRequestForwardsTarget(t *testing.T) {
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("X-User-ID"); got != "u-1" {
t.Errorf("X-User-ID = %q, want u-1", got)
}
if r.URL.Path != "/api/v1/user/friends/request" {
t.Errorf("unexpected path %q", r.URL.Path)
}
_, _ = w.Write([]byte(`{"ok":true}`))
})
defer cleanup()
reg := transcode.NewRegistry(backend, nil)
op, _ := reg.Lookup(transcode.MsgFriendRequest)
payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: targetPayload("b-1")})
if err != nil {
t.Fatalf("handler: %v", err)
}
if ack := fb.GetRootAsAck(payload, 0); !ack.Ok() {
t.Fatal("ack not ok")
}
}
func TestFriendCodeIssueAndRedeem(t *testing.T) {
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/user/friends/code":
_, _ = w.Write([]byte(`{"code":"123456","expires_at_unix":1717000000}`))
case "/api/v1/user/friends/code/redeem":
_, _ = w.Write([]byte(`{"friend":{"account_id":"a-7","display_name":"Kaya"}}`))
default:
t.Errorf("unexpected path %q", r.URL.Path)
}
})
defer cleanup()
reg := transcode.NewRegistry(backend, nil)
issue, _ := reg.Lookup(transcode.MsgFriendCodeIssue)
p1, err := issue.Handler(context.Background(), transcode.Request{UserID: "u-1"})
if err != nil {
t.Fatalf("issue: %v", err)
}
fc := fb.GetRootAsFriendCode(p1, 0)
if string(fc.Code()) != "123456" || fc.ExpiresAtUnix() != 1717000000 {
t.Fatalf("friend code = (%q, %d)", fc.Code(), fc.ExpiresAtUnix())
}
b := flatbuffers.NewBuilder(32)
code := b.CreateString("123456")
fb.RedeemCodeRequestStart(b)
fb.RedeemCodeRequestAddCode(b, code)
b.Finish(fb.RedeemCodeRequestEnd(b))
redeem, _ := reg.Lookup(transcode.MsgFriendCodeRedeem)
p2, err := redeem.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: b.FinishedBytes()})
if err != nil {
t.Fatalf("redeem: %v", err)
}
rr := fb.GetRootAsRedeemResult(p2, 0)
if f := rr.Friend(nil); f == nil || string(f.AccountId()) != "a-7" || string(f.DisplayName()) != "Kaya" {
t.Fatalf("redeem friend decoded wrong: %+v", f)
}
}
func TestInvitationCreateRoundTrip(t *testing.T) {
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/user/invitations" {
t.Errorf("unexpected path %q", r.URL.Path)
}
_, _ = w.Write([]byte(`{"id":"i-1","inviter":{"account_id":"u-1","display_name":"Me"},"invitees":[{"account_id":"inv-1","display_name":"Friend","seat":1,"response":"pending"}],"variant":"english","turn_timeout_secs":86400,"hints_allowed":true,"hints_per_player":1,"dropout_tiles":"remove","status":"pending","expires_at_unix":42}`))
})
defer cleanup()
reg := transcode.NewRegistry(backend, nil)
op, _ := reg.Lookup(transcode.MsgInvitationCreate)
b := flatbuffers.NewBuilder(128)
inviteeID := b.CreateString("inv-1")
fb.CreateInvitationRequestStartInviteeIdsVector(b, 1)
b.PrependUOffsetT(inviteeID)
ids := b.EndVector(1)
variant := b.CreateString("english")
dropout := b.CreateString("remove")
fb.CreateInvitationRequestStart(b)
fb.CreateInvitationRequestAddInviteeIds(b, ids)
fb.CreateInvitationRequestAddVariant(b, variant)
fb.CreateInvitationRequestAddTurnTimeoutSecs(b, 86400)
fb.CreateInvitationRequestAddHintsAllowed(b, true)
fb.CreateInvitationRequestAddHintsPerPlayer(b, 1)
fb.CreateInvitationRequestAddDropoutTiles(b, dropout)
b.Finish(fb.CreateInvitationRequestEnd(b))
payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: b.FinishedBytes()})
if err != nil {
t.Fatalf("handler: %v", err)
}
inv := fb.GetRootAsInvitation(payload, 0)
if string(inv.Id()) != "i-1" || inv.InviteesLength() != 1 || string(inv.Variant()) != "english" {
t.Fatalf("invitation decoded wrong: id=%q invitees=%d variant=%q", inv.Id(), inv.InviteesLength(), inv.Variant())
}
if iv := inv.Inviter(nil); iv == nil || string(iv.DisplayName()) != "Me" {
t.Fatalf("inviter decoded wrong: %+v", iv)
}
}
func TestStatsRoundTrip(t *testing.T) {
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/user/stats" {
t.Errorf("unexpected path %q", r.URL.Path)
}
_, _ = w.Write([]byte(`{"wins":5,"losses":3,"draws":1,"max_game_points":420,"max_word_points":90}`))
})
defer cleanup()
reg := transcode.NewRegistry(backend, nil)
op, _ := reg.Lookup(transcode.MsgStatsGet)
payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1"})
if err != nil {
t.Fatalf("handler: %v", err)
}
st := fb.GetRootAsStatsView(payload, 0)
if st.Wins() != 5 || st.Losses() != 3 || st.Draws() != 1 || st.MaxGamePoints() != 420 || st.MaxWordPoints() != 90 {
t.Fatalf("stats decoded wrong: %+v", st)
}
}
func TestGcgRoundTrip(t *testing.T) {
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/user/games/g-1/gcg" {
t.Errorf("unexpected path %q", r.URL.Path)
}
_, _ = w.Write([]byte(`{"game_id":"g-1","filename":"game-g-1.gcg","content":"#character-encoding UTF-8\n"}`))
})
defer cleanup()
reg := transcode.NewRegistry(backend, nil)
op, _ := reg.Lookup(transcode.MsgGameGCG)
payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: gameActionPayload("g-1")})
if err != nil {
t.Fatalf("handler: %v", err)
}
gcg := fb.GetRootAsGcgExport(payload, 0)
if string(gcg.Filename()) != "game-g-1.gcg" || len(gcg.Content()) == 0 {
t.Fatalf("gcg decoded wrong: filename=%q content=%q", gcg.Filename(), gcg.Content())
}
}
func TestProfileUpdateRoundTripAway(t *testing.T) {
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut || r.URL.Path != "/api/v1/user/profile" {
t.Errorf("unexpected %s %q", r.Method, r.URL.Path)
}
_, _ = w.Write([]byte(`{"user_id":"u-1","display_name":"Kaya","preferred_language":"ru","time_zone":"Europe/Moscow","away_start":"00:00","away_end":"07:30"}`))
})
defer cleanup()
reg := transcode.NewRegistry(backend, nil)
op, _ := reg.Lookup(transcode.MsgProfileUpdate)
b := flatbuffers.NewBuilder(128)
name := b.CreateString("Kaya")
lang := b.CreateString("ru")
tz := b.CreateString("Europe/Moscow")
as := b.CreateString("00:00")
ae := b.CreateString("07:30")
fb.UpdateProfileRequestStart(b)
fb.UpdateProfileRequestAddDisplayName(b, name)
fb.UpdateProfileRequestAddPreferredLanguage(b, lang)
fb.UpdateProfileRequestAddTimeZone(b, tz)
fb.UpdateProfileRequestAddAwayStart(b, as)
fb.UpdateProfileRequestAddAwayEnd(b, ae)
b.Finish(fb.UpdateProfileRequestEnd(b))
payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1", Payload: b.FinishedBytes()})
if err != nil {
t.Fatalf("handler: %v", err)
}
p := fb.GetRootAsProfile(payload, 0)
if string(p.AwayStart()) != "00:00" || string(p.AwayEnd()) != "07:30" || string(p.PreferredLanguage()) != "ru" {
t.Fatalf("profile away round-trip wrong: start=%q end=%q lang=%q", p.AwayStart(), p.AwayEnd(), p.PreferredLanguage())
}
}