]> git.giacomolli.uk - onechat/blobdiff - telegram_test.go
Log each change of the WhatsApp connection
[onechat] / telegram_test.go
index 5e50ec15755f1b8f3367a44f6120697dcddf6d9b..4d2a9e64feb9bbe48665253365858a1082bf36db 100644 (file)
@@ -6,6 +6,7 @@ import (
        "os"
        "path/filepath"
        "slices"
+       "strconv"
        "strings"
        "testing"
        "time"
@@ -75,7 +76,7 @@ func TestBotIgnoresEveryoneButTheOwner(t *testing.T) {
        require.Empty(t, telegram.Sent())
        require.NotContains(t, terminal.Output(), "secret")
        require.NotContains(t, logs.String(), "secret")
-       require.Contains(t, logs.String(), "update ignored")
+       require.Contains(t, logs.String(), "a Telegram message is ignored")
 }
 
 func TestBotAnswersThatOnlyTextIsSupported(t *testing.T) {
@@ -179,6 +180,24 @@ func TestBotShowsOnlyTheStartAndTheEndOfASubagent(t *testing.T) {
        }, sent)
 }
 
+func TestBotSkipsTheWorkOfWhatsAppAndTheSchedules(t *testing.T) {
+       bot := &TelegramBot{
+               ready: make(chan struct{}, 1),
+       }
+
+       bot.Notify(Event{
+               Kind:   "work",
+               Source: whatsappSource,
+               Text:   "[WhatsApp] Ana Souza",
+       })
+       bot.Notify(Event{
+               Kind:   "work",
+               Source: scheduleSource,
+               Text:   "[Schedule 1] Sunday 2026-11-08 20:05 -03:00, once",
+       })
+       require.Empty(t, bot.queue)
+}
+
 func TestBotStartsAtTheStoredOffset(t *testing.T) {
        server := newModelFake(t)
        server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
@@ -273,7 +292,9 @@ func TestDeliverTriesAgainAfterATimeout(t *testing.T) {
 
        require.Equal(t, []string{"hello"}, telegram.Sent())
        require.Len(t, telegram.Calls(), 2)
-       require.Contains(t, logs.String(), "no valid answer")
+       require.Contains(t, logs.String(), "cannot send replies to Telegram: no answer in 50ms (next try in 1ms)\n")
+       require.Contains(t, logs.String(), "replies go to Telegram again, after ")
+       require.Equal(t, 2, strings.Count(logs.String(), "\n"))
        require.NotContains(t, logs.String(), telegramTestToken)
 }
 
@@ -344,6 +365,7 @@ func TestRichMessageFailures(t *testing.T) {
                always  bool
                methods []string
                sent    []string
+               lines   int
                logText string
        }{
                {
@@ -352,7 +374,8 @@ func TestRichMessageFailures(t *testing.T) {
                        always:  true,
                        methods: []string{"sendRichMessage", "sendMessage"},
                        sent:    []string{"a secret word"},
-                       logText: "rich message refused",
+                       lines:   1,
+                       logText: "Telegram refused its full formatting",
                },
                {
                        name:    "unknown method sends the reply once with entities",
@@ -360,28 +383,32 @@ func TestRichMessageFailures(t *testing.T) {
                        always:  true,
                        methods: []string{"sendRichMessage", "sendMessage"},
                        sent:    []string{"a secret word"},
-                       logText: "rich message refused",
+                       lines:   1,
+                       logText: "Telegram refused its full formatting",
                },
                {
                        name:    "rate limit tries the rich message again",
                        status:  http.StatusTooManyRequests,
                        methods: []string{"sendRichMessage", "sendRichMessage"},
                        sent:    []string{"a **secret** word"},
-                       logText: "answered 429",
+                       lines:   2,
+                       logText: "cannot send replies to Telegram: Telegram answered 429",
                },
                {
                        name:    "server error tries the rich message again",
                        status:  http.StatusInternalServerError,
                        methods: []string{"sendRichMessage", "sendRichMessage"},
                        sent:    []string{"a **secret** word"},
-                       logText: "answered 500",
+                       lines:   2,
+                       logText: "cannot send replies to Telegram: Telegram answered 500",
                },
                {
                        name:    "other refusal drops the reply",
                        status:  http.StatusForbidden,
                        always:  true,
                        methods: []string{"sendRichMessage"},
-                       logText: "message dropped",
+                       lines:   1,
+                       logText: "a reply to Telegram is lost: Telegram answered 403",
                },
        }
 
@@ -412,7 +439,7 @@ func TestRichMessageFailures(t *testing.T) {
 
                        require.Equal(t, tc.methods, methods)
                        require.Equal(t, tc.sent, telegram.Sent())
-                       require.Equal(t, 1, strings.Count(logs.String(), "\n"))
+                       require.Equal(t, tc.lines, strings.Count(logs.String(), "\n"))
                        require.Contains(t, logs.String(), tc.logText)
                        require.NotContains(t, logs.String(), telegramTestToken)
                        require.NotContains(t, logs.String(), "secret word")
@@ -568,7 +595,7 @@ func TestRefusedEntitiesGoAgainAsPlainText(t *testing.T) {
        require.Nil(t, calls[1].params["entities"])
        require.Equal(t, []string{"a secret word"}, telegram.Sent())
        require.Equal(t, 1, strings.Count(logs.String(), "\n"))
-       require.Contains(t, logs.String(), "entities refused")
+       require.Contains(t, logs.String(), "a reply goes as plain text, because Telegram refused its formatting")
        require.NotContains(t, logs.String(), telegramTestToken)
        require.NotContains(t, logs.String(), "secret word")
 }
@@ -581,11 +608,46 @@ func TestBotLogsNoTokenWhenTheNetworkFails(t *testing.T) {
        daemonStartTestWithBot(t, newModelFake(t), bot)
 
        require.Eventually(t, func() bool {
-               return strings.Count(logs.String(), "no valid answer") >= 2
+               return strings.Contains(logs.String(), "cannot get messages from Telegram: no answer: ")
        }, 10*time.Second, time.Millisecond)
        require.NotContains(t, logs.String(), telegramTestToken)
 }
 
+func TestBotLogsAnOutageOnlyAtItsStartAndEnd(t *testing.T) {
+       server := newModelFake(t)
+       server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
+               modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("ok")}, 100, 0, 5))
+       }
+
+       telegram := newTelegramFake(t)
+       failures := 5
+       telegram.fail = func(call TelegramFakeCall) (int, string) {
+               if call.method != "getUpdates" || failures == 0 {
+                       return 0, ""
+               }
+
+               failures--
+
+               return http.StatusBadGateway, "Bad Gateway"
+       }
+
+       bot, logs := telegramStartTestBot(t, telegram)
+       daemonStartTestWithBot(t, server, bot)
+
+       telegram.Add(TelegramUpdate{
+               UpdateID: 1,
+               Message:  telegramOwnerMessage("a secret word"),
+       })
+
+       telegramWaitForSent(t, telegram, "ok")
+       lines := strings.Split(strings.TrimSuffix(logs.String(), "\n"), "\n")
+       require.Len(t, lines, 2)
+       require.Equal(t, "cannot get messages from Telegram: Telegram answered 502 Bad Gateway (next try in 1ms)", lines[0])
+       require.Regexp(t, `^messages from Telegram come again, after [0-9]+m?s$`, lines[1])
+       require.NotContains(t, logs.String(), "secret word")
+       require.NotContains(t, logs.String(), strconv.FormatInt(telegramTestOwner, 10))
+}
+
 func TestSplitMessage(t *testing.T) {
        testCases := []struct {
                name  string