mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-07 05:46:38 +08:00
Improve Telegram notifications
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/model"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestChannelSubscribesCanDisableAllEvents(t *testing.T) {
|
||||
@@ -37,3 +41,53 @@ func TestChannelSubscribesSupportsExplicitAllAndSpecificEvents(t *testing.T) {
|
||||
t.Fatal("unlisted event should not be subscribed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTelegramDispatchUsesPhotoAndFormattedCaption(t *testing.T) {
|
||||
var gotPath string
|
||||
var gotForm map[string]string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
if err := r.ParseForm(); err != nil {
|
||||
t.Fatalf("parse form: %v", err)
|
||||
}
|
||||
gotForm = map[string]string{}
|
||||
for key := range r.Form {
|
||||
gotForm[key] = r.Form.Get(key)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"ok":true}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
svc := NewNotifyChannelService(zap.NewNop(), nil)
|
||||
channel := model.NotifyChannel{
|
||||
Type: "telegram",
|
||||
Config: `{
|
||||
"bot_token":"123456:ABC",
|
||||
"group_chat_id":"-10001",
|
||||
"api_base_url":"` + server.URL + `"
|
||||
}`,
|
||||
}
|
||||
err := svc.dispatchOneEvent(t.Context(), channel, NotifyEvent{
|
||||
Type: EventDownloadComplete,
|
||||
Title: "MediaStationGo 下载完成",
|
||||
Message: "任务:示例影片\n保存路径:/downloads/movie\nHash:abcdef",
|
||||
Data: map[string]interface{}{
|
||||
"poster_url": server.URL + "/poster.jpg",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if gotPath != "/bot123456:ABC/sendPhoto" {
|
||||
t.Fatalf("path = %q, want sendPhoto", gotPath)
|
||||
}
|
||||
if gotForm["chat_id"] != "-10001" || gotForm["photo"] == "" {
|
||||
t.Fatalf("telegram form = %#v", gotForm)
|
||||
}
|
||||
caption := gotForm["caption"]
|
||||
for _, want := range []string{"<b>MediaStationGo</b>", "<b>下载完成</b>", "<b>任务</b>: 示例影片", "<code>/downloads/movie</code>", "<code>abcdef</code>"} {
|
||||
if !strings.Contains(caption, want) {
|
||||
t.Fatalf("caption missing %q: %s", want, caption)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user