Files
archipelago/channels/telegram/main_test.go
T
sam 86501fa1d4 feat(messaging): run Telegram as an archipelago channel extension (#1399)
The bundled internal/channels/telegram package (17 files) is deleted; the
channel now ships as archipelago/channels/telegram, launched by archiemessaging
through the existing channel-extension machinery. An existing [chat.telegram]
config enables it with the same keys: the token stays a secret reference
(token_ref) the host resolves, allowed_user_ids and show_tool_calls map onto
extension settings. Per-org bots compose the same extension per org from the
org's credential binding. Reload support is now derived from a channels.Reloader
interface instead of a *telegram.Gateway assertion. The five host-only seams
(/update, release announcements, running versions, in-place show_tool_calls,
reload) are dropped from the channel and need a contract home or deletion.
2026-10-31 15:30:27 +11:00

132 lines
4.1 KiB
Go

package main
import (
"strings"
"testing"
"github.com/go-telegram/bot/models"
"github.com/samcharles93/archipelago/sdk/channel"
)
func TestParseSettings(t *testing.T) {
for _, tc := range []struct {
name string
cfg channel.Config
wantErr string
check func(t *testing.T, s settings)
}{
{
name: "token and allowlist",
cfg: channel.Config{
Settings: map[string]string{"allowed_user_ids": " 111, 222 ,,333 "},
Secrets: map[string]string{"token": "123:ABC"},
},
check: func(t *testing.T, s settings) {
if s.token != "123:ABC" {
t.Errorf("token = %q", s.token)
}
if len(s.allowed) != 3 || s.allowed[0] != 111 || s.allowed[2] != 333 {
t.Errorf("allowed = %v", s.allowed)
}
if s.showTools {
t.Error("showTools defaulted true")
}
},
},
{
name: "show_tool_calls on",
cfg: channel.Config{
Settings: map[string]string{"allowed_user_ids": "1", "show_tool_calls": "true"},
Secrets: map[string]string{"token": "t"},
},
check: func(t *testing.T, s settings) {
if !s.showTools {
t.Error("showTools = false, want true")
}
},
},
{name: "missing token", cfg: channel.Config{Settings: map[string]string{"allowed_user_ids": "1"}}, wantErr: "token_ref"},
{name: "missing allowlist", cfg: channel.Config{Secrets: map[string]string{"token": "t"}}, wantErr: "allowed_user_ids"},
{name: "empty allowlist", cfg: channel.Config{Secrets: map[string]string{"token": "t"}, Settings: map[string]string{"allowed_user_ids": " ,"}}, wantErr: "allowed_user_ids"},
{name: "bad user id", cfg: channel.Config{Secrets: map[string]string{"token": "t"}, Settings: map[string]string{"allowed_user_ids": "abc"}}, wantErr: "not a positive"},
{name: "negative user id", cfg: channel.Config{Secrets: map[string]string{"token": "t"}, Settings: map[string]string{"allowed_user_ids": "-5"}}, wantErr: "not a positive"},
} {
t.Run(tc.name, func(t *testing.T) {
s, err := parseSettings(tc.cfg)
if tc.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
t.Fatalf("err = %v, want it to mention %q", err, tc.wantErr)
}
return
}
if err != nil {
t.Fatalf("parseSettings: %v", err)
}
if tc.check != nil {
tc.check(t, s)
}
})
}
}
func TestSplitText(t *testing.T) {
long := strings.Repeat("a", 9000)
parts := splitText(long, messageMaxLen)
if len(parts) != 3 {
t.Fatalf("splitText gave %d parts, want 3", len(parts))
}
for _, p := range parts {
if len([]rune(p)) > messageMaxLen {
t.Errorf("part is %d runes, over the limit", len([]rune(p)))
}
}
if got := splitText("short", messageMaxLen); len(got) != 1 || got[0] != "short" {
t.Errorf("splitText(short) = %v", got)
}
paras := splitText("one\n\n"+long, messageMaxLen)
if paras[0] != "one" {
t.Errorf("first part = %q, want %q", paras[0], "one")
}
}
func TestConversationKey(t *testing.T) {
for _, tc := range []struct {
chatID, thread int64
want string
}{
{123, 0, "123"},
{123, 7, "123/7"},
{-100, 42, "-100/42"},
} {
msg := &models.Message{Chat: models.Chat{ID: tc.chatID}, MessageThreadID: int(tc.thread)}
if got := conversationKey(msg); got != tc.want {
t.Errorf("conversationKey(%d, %d) = %q, want %q", tc.chatID, tc.thread, got, tc.want)
}
}
}
func TestInboundMessage(t *testing.T) {
msg := &models.Message{
ID: 42,
Chat: models.Chat{ID: -100},
MessageThreadID: 7,
From: &models.User{ID: 111, Username: "sam"},
Text: "hello",
Date: 1700000000,
}
m := inboundMessage(msg, "hello", nil)
if m.GetMessageId() != "42" || m.GetSourceId() != "42" {
t.Errorf("ids = %q/%q, want both 42", m.GetMessageId(), m.GetSourceId())
}
if m.GetChannelId() != "-100" || m.GetThreadId() != "7" {
t.Errorf("channel/thread = %q/%q", m.GetChannelId(), m.GetThreadId())
}
if m.GetFrom() != "sam" || m.GetSenderId() != "111" || m.GetPlatform() != "telegram" {
t.Errorf("from/sender/platform = %q/%q/%q", m.GetFrom(), m.GetSenderId(), m.GetPlatform())
}
if m.GetAt() == nil || m.GetAt().GetSeconds() != 1700000000 {
t.Errorf("at = %v", m.GetAt())
}
}