Files
scrabble-game/platform/telegram/internal/botlink/client_test.go
T
Ilia Denisov e71e40eef5
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 11s
CI / integration (pull_request) Successful in 19s
CI / ui (pull_request) Successful in 57s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m39s
feat(telegram): promo bot + channel-chat moderation gate
Add a second standalone promo bot to the bot container (answers /start with a
localized message + a URL button into the main bot's Mini App) and gate write
access in a channel's linked discussion chat: grant on join when the Telegram
user is registered and neither admin-suspended nor holding a new chat_muted
role, and revoke/grant on the matching moderation change for a member currently
in the chat.

Eligibility (registered AND NOT suspended AND NOT chat_muted; the game
suspension dominates) is resolved once in the backend and reached two ways: the
bot's join-time unary ResolveChatEligibility over the existing mTLS bot-link,
and a backend chat_access_changed event -> gateway -> ChatGate command
(idempotent; a temporary-block-expiry sweeper may over-emit). The bot guards the
block/unblock path with getChatMember, since bots cannot list members.

A web_app button cannot open another bot's Mini App (it signs initData with the
sending bot's token), so the promo button is a t.me ?startapp URL reusing the
UI's VITE_TELEGRAM_LINK. The bot must be a chat admin with the restrict-members
right and chat_member in its allowed updates.

No schema change: chat_muted reuses the data-driven account_roles table.
2026-06-21 14:46:51 +02:00

92 lines
2.4 KiB
Go

package botlink
import (
"fmt"
"net"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
botlinkv1 "scrabble/pkg/proto/botlink/v1"
telegramv1 "scrabble/pkg/proto/telegram/v1"
)
// testLinkServer registers one bot, pushes a single Notify command, and records the
// Ack the client returns.
type testLinkServer struct {
botlinkv1.UnimplementedBotLinkServer
acks chan *botlinkv1.Ack
}
func (s *testLinkServer) Link(stream grpc.BidiStreamingServer[botlinkv1.FromBot, botlinkv1.ToBot]) error {
hello, err := stream.Recv()
if err != nil {
return err
}
if hello.GetHello() == nil {
return fmt.Errorf("first message was not Hello")
}
if err := stream.Send(&botlinkv1.ToBot{Command: &botlinkv1.Command{
CommandId: "c1",
Payload: &botlinkv1.Command_Notify{Notify: &telegramv1.NotifyRequest{
ExternalId: "12345", Kind: "your_turn",
Payload: yourTurnPayload("7c9e6679-7425-40de-944b-e07fc1f90ae7"), Language: "en",
}},
}}); err != nil {
return err
}
for {
msg, err := stream.Recv()
if err != nil {
return err
}
if ack := msg.GetAck(); ack != nil {
s.acks <- ack
return nil
}
}
}
// TestClientServesCommands verifies the bot client dials, registers, executes a
// pushed command through the executor, and acks it.
func TestClientServesCommands(t *testing.T) {
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
ts := &testLinkServer{acks: make(chan *botlinkv1.Ack, 1)}
srv := grpc.NewServer()
botlinkv1.RegisterBotLinkServer(srv, ts)
go func() { _ = srv.Serve(lis) }()
t.Cleanup(srv.Stop)
sender := &fakeSender{}
client, err := NewClient(ClientConfig{
GatewayAddr: lis.Addr().String(),
InstanceID: "test",
Creds: insecure.NewCredentials(),
ReconnectDelay: 50 * time.Millisecond,
}, NewExecutor(sender, 0, nil), nil)
if err != nil {
t.Fatalf("new client: %v", err)
}
t.Cleanup(func() { _ = client.Close() })
go func() { _ = client.Run(t.Context()) }()
select {
case ack := <-ts.acks:
if ack.GetCommandId() != "c1" || !ack.GetDelivered() {
t.Errorf("ack = %+v, want command_id c1 delivered=true", ack)
}
case <-time.After(2 * time.Second):
t.Fatal("server received no ack")
}
// The channel receive above happens-after the client wrote the sender call.
if len(sender.notify) != 1 || sender.notify[0].chatID != 12345 {
t.Errorf("sender notify calls = %+v, want one call to chat 12345", sender.notify)
}
}