Files
archie/internal/channels/telegram/telegram_test.go
T
sam f36b6f2e41 feat: gateway architecture with Telegram channel
internal/gateway/ — Gateway interface, Router with gateway-local
command dispatch (commands handled directly, no LLM needed).
ModelManager interface for /model and /models support.

internal/channels/telegram/ — Telegram Gateway using go-telegram/bot
v1.18.0 (ported from GopherClaw). Webhook support, middleware,
allowed-chat filtering, long-message splitting.

internal/config/ — ChatConfig + TelegramConfig optional TOML block.

Cmd wiring in archied/main.go: starts Telegram gateway when
[chat.telegram] token_env is configured.

Gateway-local commands implemented: /status, /help, /model, /models.
Write actions (/spawn, /approve, /cancel) follow in abg.13.

Closes: archie-core-abg.5, archie-core-abg.14, archie-core-abg.15
2026-07-25 23:50:52 +10:00

99 lines
3.2 KiB
Go

package telegram
import (
"context"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/samcharles93/archie-core/internal/gateway"
)
func TestName(t *testing.T) {
g := New("token", "", "", nil, slog.Default())
if got := g.Name(); got != "telegram" {
t.Errorf("Name() = %q, want telegram", got)
}
}
func TestNewStoresFields(t *testing.T) {
g := New("abc123", "https://example.com/webhook", "secret", []int64{42}, slog.Default())
if g.Token != "abc123" {
t.Errorf("Token = %q", g.Token)
}
if g.WebhookURL != "https://example.com/webhook" {
t.Errorf("WebhookURL = %q", g.WebhookURL)
}
if len(g.AllowedChatIDs) != 1 || g.AllowedChatIDs[0] != 42 {
t.Errorf("AllowedChatIDs = %v", g.AllowedChatIDs)
}
}
func TestStartWithoutToken(t *testing.T) {
g := New("", "", "", nil, slog.Default())
router := gateway.NewRouter(nil, nil, "telegram")
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
if err := g.Start(ctx, router); err == nil {
t.Error("expected error when starting without token")
}
}
func TestWebhookHandlerReturns503WhenNotStarted(t *testing.T) {
g := New("token", "", "", nil, slog.Default())
rec := httptest.NewRecorder()
g.WebhookHandler().ServeHTTP(rec, httptest.NewRequest(http.MethodPost, "/", nil))
if rec.Code != http.StatusServiceUnavailable {
t.Errorf("status = %d, want 503", rec.Code)
}
}
func TestStartAndStopLifecycle(t *testing.T) {
// Use a noop webhook server so the bot can initialize without hitting
// the real Telegram API.
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"ok":true,"result":true}`))
}))
defer api.Close()
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn}))
g := New("test-token", "", "", nil, log)
// Override the API base URL — the go-telegram/bot library uses
// https://api.telegram.org by default. We want it to hit our test
// server instead. The library doesn't expose a BaseURL override, so
// we test what we can: ensure Start fails predictably when the API
// rejects us, rather than trying to mock the full Bot API wire
// protocol.
router := gateway.NewRouter(nil, nil, "telegram")
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
// This will fail because our test server returns {"ok":true} but
// go-telegram/bot's Start() calls getMe() to validate the token,
// and our test server doesn't return a valid User object.
// The point: we exercise the Start/Stop path; accept either
// success or failure.
_ = g.Start(ctx, router)
// Best-effort stop — don't assert on Start result since we can't
// fully mock the Bot API without the library supporting a base URL
// override.
stopCtx, stopCancel := context.WithTimeout(context.Background(), 2*time.Second)
defer stopCancel()
if err := g.Stop(stopCtx); err != nil {
t.Logf("Stop: %v (expected in test env)", err)
}
}
// Compile-time guard: Gateway implements gateway.Gateway.
var _ gateway.Gateway = (*Gateway)(nil)
var _ = (*bot.Bot)(nil)
var _ = models.Update{}