mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-02 06:56:36 +08:00
efa75558af
telebot types LongPoller.Timeout as time.Duration and sends int(timeout / time.Second) to getUpdates, so the literal 10 meant ten nanoseconds: Telegram received timeout=0, long polling never held the connection, and the adapter polled the Bot API in a tight loop instead. Use 10 seconds and extract the settings so the conversion is asserted. The adapter also has no media temp-dir cleanup (downloadMedia creates an MkdirTemp per attachment and nothing removes it); that is left as a separate change rather than bundled here.
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
// Copyright 2026 Arctel.net
|
||
// SPDX-License-Identifier: Apache-2.0
|
||
|
||
package telegram
|
||
|
||
import (
|
||
"Wavelet/plugins/domain/message_gateway/model"
|
||
"context"
|
||
"testing"
|
||
"time"
|
||
|
||
tele "gopkg.in/telebot.v4"
|
||
)
|
||
|
||
// TestBuildTeleSettingsLongPollWindow 回归:LongPoller.Timeout 是 time.Duration,
|
||
// telebot 以 int(timeout/time.Second) 下发给 getUpdates。写成裸整数会被解释为
|
||
// 纳秒,令 timeout=0,长轮询退化为对 Bot API 的空转轮询。
|
||
func TestBuildTeleSettingsLongPollWindow(t *testing.T) {
|
||
pref := buildTeleSettings(model.ChannelConfig{
|
||
Credentials: map[string]string{"bot_token": "token"},
|
||
Extra: map[string]string{"base_url": "https://tg.example.com/api/"},
|
||
})
|
||
|
||
poller, ok := pref.Poller.(*tele.LongPoller)
|
||
if !ok {
|
||
t.Fatalf("expected *tele.LongPoller, got %T", pref.Poller)
|
||
}
|
||
if got := int(poller.Timeout / time.Second); got != 10 {
|
||
t.Errorf("getUpdates would receive timeout=%d seconds, want 10", got)
|
||
}
|
||
if pref.URL != "https://tg.example.com/api" {
|
||
t.Errorf("base_url trailing slash should be trimmed, got %q", pref.URL)
|
||
}
|
||
}
|
||
|
||
func TestHandleUpdate_DropsGroups(t *testing.T) {
|
||
var got int
|
||
a := &Adapter{onInbound: func(ctx context.Context, msg model.InboundMessage) error {
|
||
got++
|
||
return nil
|
||
}}
|
||
a.handleTeleMessage(context.Background(), &tele.Message{
|
||
ID: 1,
|
||
Text: "hi",
|
||
Chat: &tele.Chat{ID: -100, Type: tele.ChatGroup},
|
||
Sender: &tele.User{ID: 1},
|
||
})
|
||
if got != 0 {
|
||
t.Fatalf("group must be ignored")
|
||
}
|
||
}
|
||
|
||
func TestHandleUpdate_PrivateText(t *testing.T) {
|
||
var got model.InboundMessage
|
||
a := &Adapter{
|
||
cfg: model.ChannelConfig{ID: 7, Type: "telegram"},
|
||
onInbound: func(ctx context.Context, msg model.InboundMessage) error {
|
||
got = msg
|
||
return nil
|
||
},
|
||
}
|
||
a.handleTeleMessage(context.Background(), &tele.Message{
|
||
ID: 9,
|
||
Text: "hi",
|
||
Chat: &tele.Chat{ID: 42, Type: tele.ChatPrivate},
|
||
Sender: &tele.User{ID: 42},
|
||
})
|
||
if got.Text != "hi" || got.PlatformUserID != "42" || got.ChannelID != 7 {
|
||
t.Fatalf("%+v", got)
|
||
}
|
||
}
|
||
|
||
func TestNew_RequiresToken(t *testing.T) {
|
||
_, err := New(model.ChannelConfig{}, nil)
|
||
if err == nil {
|
||
t.Fatal("expected error")
|
||
}
|
||
}
|