cf66ed7e26
Tests · Go / test (push) Successful in 7s
Tests · Integration / integration (push) Successful in 12s
Tests · Go / test (pull_request) Successful in 6s
Tests · Integration / integration (pull_request) Successful in 11s
Tests · UI / test (pull_request) Successful in 19s
New platform/telegram connector (own container, bot token only there): - go-telegram/bot long-poll loop: /start deep-links + Mini App launch button. - gRPC API pkg/proto/telegram/v1 (Telegram service): ValidateInitData, Notify (renders a localized message + deep-link button), SendToUser/SendToGameChannel (admin, wired in Stage 10). Generic methods are platform-agnostic (external_id). - Bot API base override for Telegram's test environment; Dockerfile + compose (VPN sidecar, no public ingress); README. Gateway: - initData validation relocated from the gateway into the connector; the gateway calls ValidateInitData over gRPC (GATEWAY_CONNECTOR_ADDR), drops the bot token, and deletes internal/auth. - Out-of-app push: runPushPump routes events whose recipient has no live in-app stream to connector.Notify, gated by /internal/push-target + the in-app-only flag (race-free de-dup); HasSubscribers added to the push hub. Backend: - Migration 00007 accounts.notifications_in_app_only (default true) + jetgen. - ProvisionTelegram seeds a new account's language/display name from the launch fields; IdentityExternalID reverse lookup; /internal/push-target handler. UI: - Telegram Mini App launch: detect initData, apply themeParams, authTelegram, route the deep-link start_param (g/i/f); /telegram/ guard redirects outside Telegram. Vite relative base + telegram-web-app.js. In-app-only profile toggle; share-to-Telegram link for a friend code. Vitest + Playwright coverage. Wire/docs/CI: fbs Profile/UpdateProfileRequest gain notifications_in_app_only (Go + TS); go.work uses ./platform/telegram; go-unit.yaml covers it; PLAN, ARCHITECTURE, FUNCTIONAL (+ru), UI_DESIGN, READMEs updated.
156 lines
5.1 KiB
Go
156 lines
5.1 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"
|
|
)
|
|
|
|
// 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 }
|
|
|
|
// 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
|
|
}
|
|
|
|
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: "ru"}
|
|
srv := NewServer(stubValidator{user: want}, &fakeSender{}, 0, 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() != "ru" {
|
|
t.Errorf("resp = %+v, want %+v", resp, want)
|
|
}
|
|
|
|
bad := NewServer(stubValidator{err: initdata.ErrInvalidInitData}, &fakeSender{}, 0, 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 TestNotifyDelivers(t *testing.T) {
|
|
const gameID = "7c9e6679-7425-40de-944b-e07fc1f90ae7"
|
|
sender := &fakeSender{}
|
|
srv := NewServer(stubValidator{}, sender, 0, 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(stubValidator{}, sender, 0, 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(stubValidator{}, &fakeSender{}, 0, 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(stubValidator{}, sender, 0, 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(stubValidator{}, &fakeSender{}, 0, 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(stubValidator{}, sender, 555, 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 calls = %+v", sender.text)
|
|
}
|
|
})
|
|
}
|