57c778f9b2
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 13s
CI / ui (pull_request) Successful in 54s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 57s
Collapse the two per-language Telegram bots into one unified bot and
replace language-based variant gating with explicit per-user variant
preferences.
- Telegram: one bot; drop service_language and the supported_languages
set everywhere (DB, account, auth, FlatBuffers Session wire, gateway,
connector proto). The single bot renders chat and out-of-app push in
the recipient's preferred_language; remove the game-language push
routing override (notify Intent.Language / push Event.language).
- Preferences: new accounts.variant_preferences (text[], DB default
{erudit_ru}, CHECK non-empty + subset of the three variants). Gates
the New Game picker, vs-AI and the friend invitation the player
creates, enforced server-side (HTTP 400 otherwise); an invited friend
may still accept any variant. Edited on the Settings screen; variants
are Erudit-first everywhere.
- Admin: drop the per-bot language selectors (broadcast / send-to-user)
and the feedback channel_lang column/field.
- Env/CI: collapse TELEGRAM_BOT_TOKEN_{EN,RU}, TELEGRAM_GAME_CHANNEL_ID_{EN,RU},
VITE_TELEGRAM_LINK{,_EN,_RU} and VITE_TELEGRAM_GAME_CHANNEL_NAME_{EN,RU}
to single unsuffixed names; drop GATEWAY_DEFAULT_SUPPORTED_LANGUAGES.
- Docs updated (ARCHITECTURE, FUNCTIONAL + _ru, platform/telegram, gateway,
backend, ui, UI_DESIGN, PRERELEASE).
The migration squash is deferred to a follow-up PR.
187 lines
6.7 KiB
Go
187 lines
6.7 KiB
Go
package connector
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
flatbuffers "github.com/google/flatbuffers/go"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"scrabble/pkg/fbs/scrabblefb"
|
|
telegramv1 "scrabble/pkg/proto/telegram/v1"
|
|
"scrabble/platform/telegram/internal/initdata"
|
|
"scrabble/platform/telegram/internal/loginwidget"
|
|
)
|
|
|
|
// stubValidator returns a fixed user / error from Validate.
|
|
type stubValidator struct {
|
|
user initdata.User
|
|
err error
|
|
}
|
|
|
|
func (s stubValidator) Validate(string) (initdata.User, error) { return s.user, s.err }
|
|
|
|
// stubWidgetValidator returns a fixed user / error from the Login Widget Validate.
|
|
type stubWidgetValidator struct {
|
|
user loginwidget.User
|
|
err error
|
|
}
|
|
|
|
func (s stubWidgetValidator) Validate(string) (loginwidget.User, error) { return s.user, s.err }
|
|
|
|
// fakeSender records the delivery calls the server makes.
|
|
type fakeSender struct {
|
|
notify []notifyCall
|
|
text []textCall
|
|
err error
|
|
}
|
|
|
|
type notifyCall struct {
|
|
chatID int64
|
|
text, buttonText, startParam string
|
|
}
|
|
type textCall struct {
|
|
chatID int64
|
|
text string
|
|
}
|
|
|
|
func (f *fakeSender) Notify(_ context.Context, chatID int64, text, buttonText, startParam string) error {
|
|
f.notify = append(f.notify, notifyCall{chatID, text, buttonText, startParam})
|
|
return f.err
|
|
}
|
|
|
|
func (f *fakeSender) SendText(_ context.Context, chatID int64, text string) error {
|
|
f.text = append(f.text, textCall{chatID, text})
|
|
return f.err
|
|
}
|
|
|
|
// botRT assembles the bot runtime for a test.
|
|
func botRT(sender Sender, channelID int64, iv initdata.Validator, wv loginwidget.Validator) BotRuntime {
|
|
return BotRuntime{Sender: sender, ChannelID: channelID, InitValidator: iv, WidgetValidator: wv}
|
|
}
|
|
|
|
func yourTurnPayload(gameID string) []byte {
|
|
b := flatbuffers.NewBuilder(0)
|
|
gid := b.CreateString(gameID)
|
|
scrabblefb.YourTurnEventStart(b)
|
|
scrabblefb.YourTurnEventAddGameId(b, gid)
|
|
b.Finish(scrabblefb.YourTurnEventEnd(b))
|
|
return b.FinishedBytes()
|
|
}
|
|
|
|
func TestValidateInitData(t *testing.T) {
|
|
want := initdata.User{ExternalID: "42", Username: "neo", FirstName: "Thomas", LanguageCode: "en-GB"}
|
|
srv := NewServer(botRT(&fakeSender{}, 0, stubValidator{user: want}, stubWidgetValidator{}), nil)
|
|
resp, err := srv.ValidateInitData(context.Background(), &telegramv1.ValidateInitDataRequest{InitData: "x"})
|
|
if err != nil {
|
|
t.Fatalf("validate: %v", err)
|
|
}
|
|
if resp.GetExternalId() != "42" || resp.GetUsername() != "neo" || resp.GetFirstName() != "Thomas" || resp.GetLanguageCode() != "en-GB" {
|
|
t.Errorf("resp = %+v, want %+v", resp, want)
|
|
}
|
|
|
|
bad := NewServer(botRT(&fakeSender{}, 0, stubValidator{err: initdata.ErrInvalidInitData}, stubWidgetValidator{}), nil)
|
|
if _, err := bad.ValidateInitData(context.Background(), &telegramv1.ValidateInitDataRequest{}); status.Code(err) != codes.InvalidArgument {
|
|
t.Errorf("err code = %v, want InvalidArgument", status.Code(err))
|
|
}
|
|
}
|
|
|
|
func TestValidateLoginWidget(t *testing.T) {
|
|
want := loginwidget.User{ExternalID: "42", Username: "neo", FirstName: "Thomas"}
|
|
srv := NewServer(botRT(&fakeSender{}, 0, stubValidator{}, stubWidgetValidator{user: want}), nil)
|
|
resp, err := srv.ValidateLoginWidget(context.Background(), &telegramv1.ValidateLoginWidgetRequest{Data: "x"})
|
|
if err != nil {
|
|
t.Fatalf("validate: %v", err)
|
|
}
|
|
if resp.GetExternalId() != "42" || resp.GetUsername() != "neo" || resp.GetFirstName() != "Thomas" {
|
|
t.Errorf("resp = %+v, want %+v", resp, want)
|
|
}
|
|
|
|
bad := NewServer(botRT(&fakeSender{}, 0, stubValidator{}, stubWidgetValidator{err: loginwidget.ErrInvalidLoginWidget}), nil)
|
|
if _, err := bad.ValidateLoginWidget(context.Background(), &telegramv1.ValidateLoginWidgetRequest{}); status.Code(err) != codes.InvalidArgument {
|
|
t.Errorf("err code = %v, want InvalidArgument", status.Code(err))
|
|
}
|
|
}
|
|
|
|
func TestNotifyDelivers(t *testing.T) {
|
|
const gameID = "7c9e6679-7425-40de-944b-e07fc1f90ae7"
|
|
sender := &fakeSender{}
|
|
srv := NewServer(botRT(sender, 0, stubValidator{}, stubWidgetValidator{}), nil)
|
|
resp, err := srv.Notify(context.Background(), &telegramv1.NotifyRequest{
|
|
ExternalId: "12345", Kind: "your_turn", Payload: yourTurnPayload(gameID), Language: "en",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("notify: %v", err)
|
|
}
|
|
if !resp.GetDelivered() {
|
|
t.Fatal("expected delivered=true")
|
|
}
|
|
if len(sender.notify) != 1 {
|
|
t.Fatalf("notify calls = %d, want 1", len(sender.notify))
|
|
}
|
|
if got := sender.notify[0]; got.chatID != 12345 || got.startParam != "g"+gameID {
|
|
t.Errorf("notify call = %+v, want chatID 12345 startParam g%s", got, gameID)
|
|
}
|
|
}
|
|
|
|
func TestNotifySkipsUnrenderedKind(t *testing.T) {
|
|
sender := &fakeSender{}
|
|
srv := NewServer(botRT(sender, 0, stubValidator{}, stubWidgetValidator{}), nil)
|
|
resp, err := srv.Notify(context.Background(), &telegramv1.NotifyRequest{
|
|
ExternalId: "12345", Kind: "opponent_moved", Language: "en",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("notify: %v", err)
|
|
}
|
|
if resp.GetDelivered() {
|
|
t.Error("expected delivered=false for an unrendered kind")
|
|
}
|
|
if len(sender.notify) != 0 {
|
|
t.Errorf("sender called %d times, want 0", len(sender.notify))
|
|
}
|
|
}
|
|
|
|
func TestNotifyInvalidExternalID(t *testing.T) {
|
|
srv := NewServer(botRT(&fakeSender{}, 0, stubValidator{}, stubWidgetValidator{}), nil)
|
|
_, err := srv.Notify(context.Background(), &telegramv1.NotifyRequest{
|
|
ExternalId: "not-a-number", Kind: "your_turn", Payload: yourTurnPayload("g"), Language: "en",
|
|
})
|
|
if status.Code(err) != codes.InvalidArgument {
|
|
t.Errorf("err code = %v, want InvalidArgument", status.Code(err))
|
|
}
|
|
}
|
|
|
|
func TestSendToUser(t *testing.T) {
|
|
sender := &fakeSender{}
|
|
srv := NewServer(botRT(sender, 0, stubValidator{}, stubWidgetValidator{}), nil)
|
|
resp, err := srv.SendToUser(context.Background(), &telegramv1.SendToUserRequest{ExternalId: "999", Text: "hi"})
|
|
if err != nil {
|
|
t.Fatalf("send to user: %v", err)
|
|
}
|
|
if !resp.GetDelivered() || len(sender.text) != 1 || sender.text[0].chatID != 999 || sender.text[0].text != "hi" {
|
|
t.Errorf("send to user = %v / calls %+v", resp.GetDelivered(), sender.text)
|
|
}
|
|
}
|
|
|
|
func TestSendToGameChannel(t *testing.T) {
|
|
t.Run("unconfigured", func(t *testing.T) {
|
|
srv := NewServer(botRT(&fakeSender{}, 0, stubValidator{}, stubWidgetValidator{}), nil)
|
|
_, err := srv.SendToGameChannel(context.Background(), &telegramv1.SendToGameChannelRequest{Text: "x"})
|
|
if status.Code(err) != codes.FailedPrecondition {
|
|
t.Errorf("err code = %v, want FailedPrecondition", status.Code(err))
|
|
}
|
|
})
|
|
t.Run("configured", func(t *testing.T) {
|
|
sender := &fakeSender{}
|
|
srv := NewServer(botRT(sender, 555, stubValidator{}, stubWidgetValidator{}), nil)
|
|
resp, err := srv.SendToGameChannel(context.Background(), &telegramv1.SendToGameChannelRequest{Text: "news"})
|
|
if err != nil {
|
|
t.Fatalf("send to channel: %v", err)
|
|
}
|
|
if !resp.GetDelivered() || len(sender.text) != 1 || sender.text[0].chatID != 555 {
|
|
t.Errorf("send to channel: %+v", sender.text)
|
|
}
|
|
})
|
|
}
|