6aeb529f13
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 15s
CI / ui (pull_request) Successful in 53s
CI / gate (pull_request) Successful in 1s
CI / deploy (pull_request) Failing after 2m6s
Move all Telegram egress off the main host. The single connector held the bot token, long-polled Telegram and answered the gateway/backend over the trusted internal network, so the whole component (including login validation) shared fate with its VPN sidecar. Split it into two binaries that share the token: - cmd/validator (home, no VPN): Mini App initData + Login Widget HMAC only, never calls the Bot API. The gateway dials it for Telegram auth, so game login is now independent of Telegram reachability. - cmd/bot (remote): Bot API long-poll + sendMessage, the only component reaching Telegram. It holds no inbound port — it dials the gateway over a new reverse mTLS bot-link (pkg/proto/botlink/v1) and executes the send commands the gateway pushes. The gateway funnels sends to the bot-link: out-of-app push is fire-and-forget (at-most-once, dropped if no bot is connected); the backend admin broadcasts reach a gateway-served relay that forwards them and awaits the bot's ack (SendToUser/SendToGameChannel contract preserved). mTLS (pkg/mtls) is the one inter-service link that leaves the trusted segment; validator<->gateway and the relay stay plaintext internal. The bot is Telegram-rate-limited. One bot now; the gateway bot registry, an owns_updates flag and per-command ids leave seams for N later. Webhook rejected (one URL per token, adds inbound + a static address). The unified test contour runs the split (the bot keeps its VPN sidecar and dials the gateway by its internal name; bot-link certs from deploy/gen-certs.sh, generated in CI). The prod wiring — the bot on a separate host (no VPN), the gateway bot-link port published, PROD_ certs with scheduled rotation, an SSH deploy of both hosts together — is the deferred final stage (PRERELEASE.md TX, Stage 18). Docs: ARCHITECTURE, PRERELEASE (phase TX), platform/telegram + gateway + backend + deploy READMEs, FUNCTIONAL(+ru), CLAUDE.md, .env.example.
172 lines
4.6 KiB
Go
172 lines
4.6 KiB
Go
package botlink
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
"google.golang.org/grpc/test/bufconn"
|
|
|
|
botlinkv1 "scrabble/pkg/proto/botlink/v1"
|
|
telegramv1 "scrabble/pkg/proto/telegram/v1"
|
|
)
|
|
|
|
// fakeBot is a test bot that dials the hub, registers, and acks commands with a
|
|
// fixed delivered flag (or never, when ack is false).
|
|
type fakeBot struct {
|
|
delivered bool
|
|
ack bool
|
|
received chan *botlinkv1.Command
|
|
}
|
|
|
|
// startHub registers a Hub on an in-memory gRPC server and returns the hub plus a
|
|
// dialer for fake bots.
|
|
func startHub(t *testing.T) (*Hub, func(t *testing.T) botlinkv1.BotLinkClient) {
|
|
t.Helper()
|
|
lis := bufconn.Listen(1 << 20)
|
|
hub := NewHub(nil, nil)
|
|
srv := grpc.NewServer()
|
|
botlinkv1.RegisterBotLinkServer(srv, hub)
|
|
go func() { _ = srv.Serve(lis) }()
|
|
t.Cleanup(srv.Stop)
|
|
|
|
dial := func(t *testing.T) botlinkv1.BotLinkClient {
|
|
t.Helper()
|
|
conn, err := grpc.NewClient("passthrough:///bufnet",
|
|
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) { return lis.Dial() }),
|
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("dial: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = conn.Close() })
|
|
return botlinkv1.NewBotLinkClient(conn)
|
|
}
|
|
return hub, dial
|
|
}
|
|
|
|
// connect runs a fake bot against client until ctx is cancelled, returning once the
|
|
// hub has registered it.
|
|
func (f *fakeBot) connect(t *testing.T, ctx context.Context, hub *Hub, client botlinkv1.BotLinkClient) {
|
|
t.Helper()
|
|
f.received = make(chan *botlinkv1.Command, 8)
|
|
stream, err := client.Link(ctx)
|
|
if err != nil {
|
|
t.Fatalf("link: %v", err)
|
|
}
|
|
if err := stream.Send(&botlinkv1.FromBot{Msg: &botlinkv1.FromBot_Hello{Hello: &botlinkv1.Hello{InstanceId: "test"}}}); err != nil {
|
|
t.Fatalf("hello: %v", err)
|
|
}
|
|
go func() {
|
|
for {
|
|
msg, err := stream.Recv()
|
|
if err != nil {
|
|
return
|
|
}
|
|
cmd := msg.GetCommand()
|
|
f.received <- cmd
|
|
if !f.ack {
|
|
continue
|
|
}
|
|
_ = stream.Send(&botlinkv1.FromBot{Msg: &botlinkv1.FromBot_Ack{Ack: &botlinkv1.Ack{
|
|
CommandId: cmd.GetCommandId(),
|
|
Delivered: f.delivered,
|
|
}}})
|
|
}
|
|
}()
|
|
waitConnected(t, hub, 1)
|
|
}
|
|
|
|
// waitConnected blocks until the hub reports n connected bots.
|
|
func waitConnected(t *testing.T, hub *Hub, n int) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
hub.mu.Lock()
|
|
got := len(hub.links)
|
|
hub.mu.Unlock()
|
|
if got == n {
|
|
return
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
t.Fatalf("hub did not reach %d connected bots", n)
|
|
}
|
|
|
|
func TestHubSendAwaitDelivered(t *testing.T) {
|
|
hub, dial := startHub(t)
|
|
ctx := t.Context()
|
|
bot := &fakeBot{delivered: true, ack: true}
|
|
bot.connect(t, ctx, hub, dial(t))
|
|
|
|
delivered, err := hub.SendAwait(ctx, SendToUserCommand("42", "hi"))
|
|
if err != nil {
|
|
t.Fatalf("SendAwait: %v", err)
|
|
}
|
|
if !delivered {
|
|
t.Fatal("delivered = false, want true")
|
|
}
|
|
select {
|
|
case cmd := <-bot.received:
|
|
if cmd.GetSendToUser().GetExternalId() != "42" {
|
|
t.Errorf("received external_id = %q, want 42", cmd.GetSendToUser().GetExternalId())
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("bot received no command")
|
|
}
|
|
}
|
|
|
|
func TestHubSendAwaitNoBot(t *testing.T) {
|
|
hub, _ := startHub(t)
|
|
_, err := hub.SendAwait(context.Background(), SendToUserCommand("42", "hi"))
|
|
if !errors.Is(err, ErrNoBot) {
|
|
t.Errorf("err = %v, want ErrNoBot", err)
|
|
}
|
|
}
|
|
|
|
func TestHubSendAwaitDeadline(t *testing.T) {
|
|
hub, dial := startHub(t)
|
|
ctx := t.Context()
|
|
bot := &fakeBot{ack: false} // receives but never acks
|
|
bot.connect(t, ctx, hub, dial(t))
|
|
|
|
awaitCtx, awaitCancel := context.WithTimeout(ctx, 100*time.Millisecond)
|
|
defer awaitCancel()
|
|
delivered, err := hub.SendAwait(awaitCtx, SendToUserCommand("42", "hi"))
|
|
if err != nil {
|
|
t.Fatalf("SendAwait err = %v, want nil on deadline", err)
|
|
}
|
|
if delivered {
|
|
t.Error("delivered = true, want false on deadline")
|
|
}
|
|
}
|
|
|
|
func TestHubSendAsync(t *testing.T) {
|
|
hub, dial := startHub(t)
|
|
ctx := t.Context()
|
|
bot := &fakeBot{ack: false}
|
|
bot.connect(t, ctx, hub, dial(t))
|
|
|
|
hub.Send(NotifyCommand("42", "your_turn", nil, "en"))
|
|
select {
|
|
case cmd := <-bot.received:
|
|
if cmd.GetNotify().GetExternalId() != "42" {
|
|
t.Errorf("received external_id = %q, want 42", cmd.GetNotify().GetExternalId())
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("bot received no command")
|
|
}
|
|
}
|
|
|
|
func TestRelayServerNoBot(t *testing.T) {
|
|
hub, _ := startHub(t)
|
|
relay := NewRelayServer(hub, 200*time.Millisecond)
|
|
if _, err := relay.SendToUser(context.Background(), &telegramv1.SendToUserRequest{ExternalId: "42", Text: "hi"}); err == nil {
|
|
t.Fatal("expected an error with no bot connected")
|
|
}
|
|
}
|