Files
scrabble-game/platform/telegram/internal/config/config_test.go
T
Ilia Denisov 6a602aefae
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 12s
CI / integration (pull_request) Successful in 16s
CI / ui (pull_request) Successful in 56s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m20s
feat(telegram): bot support relay — per-user forum topics
Users who DM the bot anything but /start are relayed into a private
forum supergroup, one topic per user. Operators (the chat's admins)
reply in the topic and the bot copies it back to the user; an info card
opening each topic carries a Block/Unblock toggle and a Clear button.
State is a small JSON file on a new /data volume — the bot host has no
database. Off by default (TELEGRAM_SUPPORT_CHAT_ID=0): the prod bot is
unchanged until the operator sets the chat id and adds the bot as a
forum admin.

- internal/support: concurrency-safe JSON store (topic map, block list,
  relayed message ids) with field-targeted mutators and atomic save
- bot/support.go: relay both ways via copyMessage, short-TTL admin
  cache, callback buttons, per-user topic-create lock, loop guard
  (skip the bot's own posts), reopen a deleted topic on the next message
- config + compose + CI/prod-deploy: TELEGRAM_SUPPORT_CHAT_ID per
  contour + TELEGRAM_SUPPORT_STATE_DIR; bot-state named volume; /data
  pre-owned by UID 65532 so a fresh volume is writable under distroless
- docs: ARCHITECTURE §15 + decision record, FUNCTIONAL (+ru), README
2026-06-23 17:47:00 +02:00

206 lines
6.4 KiB
Go

package config
import (
"testing"
pkgtel "scrabble/pkg/telemetry"
)
// setBotRequired sets every required bot variable so LoadBot reaches the optional
// parsing and telemetry checks.
func setBotRequired(t *testing.T) {
t.Helper()
t.Setenv("TELEGRAM_BOT_TOKEN", "bot-token")
t.Setenv("TELEGRAM_MINIAPP_URL", "https://example.org/app")
t.Setenv("TELEGRAM_GATEWAY_ADDR", "gateway.example.org:9443")
t.Setenv("TELEGRAM_BOTLINK_SERVER_NAME", "gateway.example.org")
t.Setenv("TELEGRAM_BOTLINK_TLS_CERT", "/certs/bot.crt")
t.Setenv("TELEGRAM_BOTLINK_TLS_KEY", "/certs/bot.key")
t.Setenv("TELEGRAM_BOTLINK_TLS_CA", "/certs/ca.crt")
}
// TestLoadValidator verifies the validator reads the token, defaults its address
// and names its telemetry service.
func TestLoadValidator(t *testing.T) {
t.Setenv("TELEGRAM_BOT_TOKEN", "secret")
c, err := LoadValidator()
if err != nil {
t.Fatalf("LoadValidator: %v", err)
}
if c.Token != "secret" {
t.Errorf("Token = %q, want secret", c.Token)
}
if c.GRPCAddr != defaultValidatorGRPCAddr {
t.Errorf("GRPCAddr = %q, want %q", c.GRPCAddr, defaultValidatorGRPCAddr)
}
if c.Telemetry.ServiceName != "scrabble-telegram-validator" {
t.Errorf("ServiceName = %q, want scrabble-telegram-validator", c.Telemetry.ServiceName)
}
}
// TestLoadValidatorRequiresToken verifies the validator fails without a token.
func TestLoadValidatorRequiresToken(t *testing.T) {
t.Setenv("TELEGRAM_BOT_TOKEN", "")
if _, err := LoadValidator(); err == nil {
t.Fatal("LoadValidator: expected an error without a token, got nil")
}
}
// TestLoadBot verifies the bot parses the token, channel id and owns_updates and
// names its telemetry service.
func TestLoadBot(t *testing.T) {
setBotRequired(t)
t.Setenv("TELEGRAM_GAME_CHANNEL_ID", "-100111")
c, err := LoadBot()
if err != nil {
t.Fatalf("LoadBot: %v", err)
}
if c.Token != "bot-token" || c.GameChannelID != -100111 {
t.Errorf("config = token %q / channel %d, want bot-token / -100111", c.Token, c.GameChannelID)
}
if !c.OwnsUpdates {
t.Error("OwnsUpdates = false, want true by default")
}
if c.SendRatePerSecond != defaultSendRatePerSecond {
t.Errorf("SendRatePerSecond = %d, want %d", c.SendRatePerSecond, defaultSendRatePerSecond)
}
if c.Telemetry.ServiceName != "scrabble-telegram-bot" {
t.Errorf("ServiceName = %q, want scrabble-telegram-bot", c.Telemetry.ServiceName)
}
}
// TestLoadBotOwnsUpdatesOptOut verifies TELEGRAM_OWNS_UPDATES=false disables the
// long-poll ownership.
func TestLoadBotOwnsUpdatesOptOut(t *testing.T) {
setBotRequired(t)
t.Setenv("TELEGRAM_OWNS_UPDATES", "false")
c, err := LoadBot()
if err != nil {
t.Fatalf("LoadBot: %v", err)
}
if c.OwnsUpdates {
t.Error("OwnsUpdates = true, want false")
}
}
// TestLoadBotRequired verifies LoadBot fails when a required variable is missing.
func TestLoadBotRequired(t *testing.T) {
cases := []string{
"TELEGRAM_BOT_TOKEN",
"TELEGRAM_MINIAPP_URL",
"TELEGRAM_GATEWAY_ADDR",
"TELEGRAM_BOTLINK_SERVER_NAME",
"TELEGRAM_BOTLINK_TLS_CERT",
}
for _, missing := range cases {
t.Run(missing, func(t *testing.T) {
setBotRequired(t)
t.Setenv(missing, "")
if _, err := LoadBot(); err == nil {
t.Fatalf("LoadBot: expected an error without %s, got nil", missing)
}
})
}
}
// TestLoadBotChatAndPromo verifies the moderated-chat id and the promo-bot
// configuration parse, the @-prefix is stripped from the username, and a promo token
// without a username/link is rejected.
func TestLoadBotChatAndPromo(t *testing.T) {
t.Run("parsed", func(t *testing.T) {
setBotRequired(t)
t.Setenv("TELEGRAM_CHAT_ID", "-100222")
t.Setenv("TELEGRAM_PROMO_BOT_TOKEN", "promo-token")
t.Setenv("TELEGRAM_BOT_USERNAME", "@ScrabbleBot")
t.Setenv("TELEGRAM_BOT_LINK", "https://t.me/ScrabbleBot/app")
c, err := LoadBot()
if err != nil {
t.Fatalf("LoadBot: %v", err)
}
if c.ChatID != -100222 {
t.Errorf("ChatID = %d, want -100222", c.ChatID)
}
if c.PromoBotToken != "promo-token" {
t.Errorf("PromoBotToken = %q", c.PromoBotToken)
}
if c.BotUsername != "ScrabbleBot" {
t.Errorf("BotUsername = %q, want the leading @ stripped", c.BotUsername)
}
if c.BotLinkURL != "https://t.me/ScrabbleBot/app" {
t.Errorf("BotLinkURL = %q", c.BotLinkURL)
}
})
t.Run("promo token requires username and link", func(t *testing.T) {
setBotRequired(t)
t.Setenv("TELEGRAM_PROMO_BOT_TOKEN", "promo-token")
if _, err := LoadBot(); err == nil {
t.Fatal("LoadBot: expected an error for a promo token without username/link")
}
})
t.Run("disabled by default", func(t *testing.T) {
setBotRequired(t)
c, err := LoadBot()
if err != nil {
t.Fatalf("LoadBot: %v", err)
}
if c.PromoBotToken != "" || c.ChatID != 0 {
t.Errorf("defaults: promo=%q chat=%d, want empty/0", c.PromoBotToken, c.ChatID)
}
})
}
// TestLoadBotSupportRelay verifies the support-relay chat id parses, the state
// directory defaults to /data, and the relay is disabled (chat id 0) by default.
func TestLoadBotSupportRelay(t *testing.T) {
t.Run("parsed", func(t *testing.T) {
setBotRequired(t)
t.Setenv("TELEGRAM_SUPPORT_CHAT_ID", "-1001234567890")
t.Setenv("TELEGRAM_SUPPORT_STATE_DIR", "/srv/state")
c, err := LoadBot()
if err != nil {
t.Fatalf("LoadBot: %v", err)
}
if c.SupportChatID != -1001234567890 {
t.Errorf("SupportChatID = %d, want -1001234567890", c.SupportChatID)
}
if c.SupportStateDir != "/srv/state" {
t.Errorf("SupportStateDir = %q, want /srv/state", c.SupportStateDir)
}
})
t.Run("defaults", func(t *testing.T) {
setBotRequired(t)
c, err := LoadBot()
if err != nil {
t.Fatalf("LoadBot: %v", err)
}
if c.SupportChatID != 0 {
t.Errorf("SupportChatID = %d, want 0 (relay disabled)", c.SupportChatID)
}
if c.SupportStateDir != "/data" {
t.Errorf("SupportStateDir = %q, want /data", c.SupportStateDir)
}
})
t.Run("malformed chat id rejected", func(t *testing.T) {
setBotRequired(t)
t.Setenv("TELEGRAM_SUPPORT_CHAT_ID", "not-a-number")
if _, err := LoadBot(); err == nil {
t.Fatal("LoadBot: expected an error for a malformed support chat id, got nil")
}
})
}
// TestLoadRejectsUnsupportedExporter verifies an exporter outside the supported set
// fails validation (the validator path).
func TestLoadRejectsUnsupportedExporter(t *testing.T) {
t.Setenv("TELEGRAM_BOT_TOKEN", "secret")
t.Setenv("TELEGRAM_OTEL_TRACES_EXPORTER", "jaeger")
if _, err := LoadValidator(); err == nil {
t.Fatal("LoadValidator: expected an error for an unsupported exporter, got nil")
}
_ = pkgtel.ExporterNone
}