6b6baf5710
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 8s
CI / integration (pull_request) Successful in 11s
CI / ui (pull_request) Successful in 31s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m19s
Lobby: group the my-games list into your-turn / opponent-turn / finished (empty sections hidden), ordered by last activity (your-turn oldest-first, the other two newest-first), as a compact line-separated list. gameDTO and FB GameView gain last_activity_unix (turn start while active, finish time once finished); a pure lib/lobbysort.ts holds the grouping/ordering. Friends: the in-game 'add to friends' item is now server-derived via a new GET /user/friends/outgoing (+ friends.outgoing op), returning addressees with a pending OR declined request (both read as 'request sent'), so it is correct across reloads; it shows a disabled '✓ in friends' once accepted. It live-updates when the opponent answers: RespondFriendRequest now publishes friend_added (accept) / friend_declined (new notify sub-kind, decline) to the original requester, whose open game re-derives its friend state. Tests: lobbysort unit test; gateway outgoing + last_activity transcode tests; backend integration ListOutgoingRequests + respond-publishes-to-requester; e2e updated for the new lobby section labels + a non-friend active opponent. Docs: ARCHITECTURE notify catalog, FUNCTIONAL(+ru) lobby/friends, PLAN.
282 lines
10 KiB
Go
282 lines
10 KiB
Go
package transcode_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"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 TestFriendsOutgoingRoundTrip(t *testing.T) {
|
|
backend, cleanup := fakeBackend(t, func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/api/v1/user/friends/outgoing" {
|
|
t.Errorf("unexpected path %q", r.URL.Path)
|
|
}
|
|
_, _ = w.Write([]byte(`{"requests":[{"account_id":"o-1","display_name":"Pat"}]}`))
|
|
})
|
|
defer cleanup()
|
|
|
|
reg := transcode.NewRegistry(backend, nil)
|
|
op, ok := reg.Lookup(transcode.MsgFriendsOutgoing)
|
|
if !ok {
|
|
t.Fatal("friends.outgoing not registered")
|
|
}
|
|
payload, err := op.Handler(context.Background(), transcode.Request{UserID: "u-1"})
|
|
if err != nil {
|
|
t.Fatalf("handler: %v", err)
|
|
}
|
|
ol := fb.GetRootAsOutgoingRequestList(payload, 0)
|
|
if ol.RequestsLength() != 1 {
|
|
t.Fatalf("outgoing length = %d, want 1", ol.RequestsLength())
|
|
}
|
|
var ref fb.AccountRef
|
|
ol.Requests(&ref, 0)
|
|
if string(ref.AccountId()) != "o-1" || string(ref.DisplayName()) != "Pat" {
|
|
t.Fatalf("outgoing[0] = (%q, %q), want (o-1, Pat)", ref.AccountId(), ref.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) {
|
|
var gotBody map[string]any
|
|
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)
|
|
}
|
|
_ = json.NewDecoder(r.Body).Decode(&gotBody)
|
|
// Respond with notifications_in_app_only=false to exercise the encode path
|
|
// carrying a non-default value back to the client.
|
|
_, _ = 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","notifications_in_app_only":false}`))
|
|
})
|
|
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)
|
|
fb.UpdateProfileRequestAddNotificationsInAppOnly(b, true)
|
|
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())
|
|
}
|
|
// The request's in-app-only flag (true) must reach the backend, and the backend's
|
|
// value (false) must come back in the encoded Profile.
|
|
if v, ok := gotBody["notifications_in_app_only"].(bool); !ok || v != true {
|
|
t.Errorf("forwarded notifications_in_app_only = %v (ok=%v), want true", gotBody["notifications_in_app_only"], ok)
|
|
}
|
|
if p.NotificationsInAppOnly() {
|
|
t.Error("response notifications_in_app_only = true, want false")
|
|
}
|
|
}
|