X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/1e2df788bbb17653a6df44acd41da2edb109d403..39d1a3fd09f8255298590b4413115ae72e0e3928:/telegram_test.go?ds=sidebyside diff --git a/telegram_test.go b/telegram_test.go index 4d2a9e6..f1a76b6 100644 --- a/telegram_test.go +++ b/telegram_test.go @@ -16,7 +16,7 @@ import ( func TestOwnerTalksToTheAgentThroughTelegram(t *testing.T) { server := newModelFake(t) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("Hello from the agent.")}, 100, 0, 5)) } @@ -76,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(), "a Telegram message is ignored") + require.Contains(t, logs.String(), "message ignored") } func TestBotAnswersThatOnlyTextIsSupported(t *testing.T) { @@ -100,7 +100,7 @@ func TestStopFromTelegramInterruptsTheTurn(t *testing.T) { close(release) }) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(_ http.ResponseWriter, _ ModelFakeCall) { close(arrived) <-release } @@ -200,7 +200,7 @@ func TestBotSkipsTheWorkOfWhatsAppAndTheSchedules(t *testing.T) { func TestBotStartsAtTheStoredOffset(t *testing.T) { server := newModelFake(t) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("ok")}, 100, 0, 5)) } @@ -223,7 +223,7 @@ func TestBotStartsAtTheStoredOffset(t *testing.T) { terminal.WaitFor(t, "[connected to OneChat]") telegramWaitForSent(t, telegram, "ok") - require.Equal(t, float64(10), telegram.Calls()[0].params["offset"]) + require.InDelta(t, float64(10), telegram.Calls()[0].params["offset"], 0) require.NotContains(t, terminal.Output(), "handled before the restart") require.Eventually(t, func() bool { @@ -234,7 +234,7 @@ func TestBotStartsAtTheStoredOffset(t *testing.T) { func TestBotTriesAgainAfterFailures(t *testing.T) { server := newModelFake(t) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("ok")}, 100, 0, 5)) } @@ -266,8 +266,8 @@ func TestBotTriesAgainAfterFailures(t *testing.T) { }) telegramWaitForSent(t, telegram, "ok") - require.Contains(t, logs.String(), "answered 502") - require.Contains(t, logs.String(), "answered 429") + require.Contains(t, logs.String(), "502") + require.Contains(t, logs.String(), "429") require.NotContains(t, logs.String(), telegramTestToken) } @@ -292,8 +292,8 @@ func TestDeliverTriesAgainAfterATimeout(t *testing.T) { require.Equal(t, []string{"hello"}, telegram.Sent()) require.Len(t, telegram.Calls(), 2) - 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.Contains(t, logs.String(), "send failed: no answer in 50ms (retry in 1ms)\n") + require.Contains(t, logs.String(), "send back after ") require.Equal(t, 2, strings.Count(logs.String(), "\n")) require.NotContains(t, logs.String(), telegramTestToken) } @@ -301,7 +301,7 @@ func TestDeliverTriesAgainAfterATimeout(t *testing.T) { func TestRepliesGoAsRichMessages(t *testing.T) { reply := "**Done.** The table:\n\n| Name | Size |\n|:-----|-----:|\n| `a.go` | 10 |\n\n- one\n- two\n\n```go\nx := 1\n```" server := newModelFake(t) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { + server.reply = func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem(reply)}, 100, 0, 5)) } @@ -322,6 +322,7 @@ func TestRepliesGoAsRichMessages(t *testing.T) { telegramWaitForSent(t, telegram, telegramTextOnly) var sends []TelegramFakeCall + for _, call := range telegram.Calls() { if call.method == "sendMessage" || call.method == "sendRichMessage" { sends = append(sends, call) @@ -375,7 +376,7 @@ func TestRichMessageFailures(t *testing.T) { methods: []string{"sendRichMessage", "sendMessage"}, sent: []string{"a secret word"}, lines: 1, - logText: "Telegram refused its full formatting", + logText: "reply sent with simple formatting", }, { name: "unknown method sends the reply once with entities", @@ -384,7 +385,7 @@ func TestRichMessageFailures(t *testing.T) { methods: []string{"sendRichMessage", "sendMessage"}, sent: []string{"a secret word"}, lines: 1, - logText: "Telegram refused its full formatting", + logText: "reply sent with simple formatting", }, { name: "rate limit tries the rich message again", @@ -392,7 +393,7 @@ func TestRichMessageFailures(t *testing.T) { methods: []string{"sendRichMessage", "sendRichMessage"}, sent: []string{"a **secret** word"}, lines: 2, - logText: "cannot send replies to Telegram: Telegram answered 429", + logText: "send failed: 429", }, { name: "server error tries the rich message again", @@ -400,7 +401,7 @@ func TestRichMessageFailures(t *testing.T) { methods: []string{"sendRichMessage", "sendRichMessage"}, sent: []string{"a **secret** word"}, lines: 2, - logText: "cannot send replies to Telegram: Telegram answered 500", + logText: "send failed: 500", }, { name: "other refusal drops the reply", @@ -408,7 +409,7 @@ func TestRichMessageFailures(t *testing.T) { always: true, methods: []string{"sendRichMessage"}, lines: 1, - logText: "a reply to Telegram is lost: Telegram answered 403", + logText: "reply lost: 403", }, } @@ -433,6 +434,7 @@ func TestRichMessageFailures(t *testing.T) { }) var methods []string + for _, call := range telegram.Calls() { methods = append(methods, call.method) } @@ -551,6 +553,7 @@ func TestRefusedRichPartGoesWithTheTextAsWritten(t *testing.T) { }) var fallback, rich []string + for _, call := range telegram.Calls() { switch { case !call.ok: @@ -569,7 +572,7 @@ func TestRefusedRichPartGoesWithTheTextAsWritten(t *testing.T) { require.LessOrEqual(t, telegramUTF16Units(text), telegramMessageUnits) } - require.True(t, reply == strings.Join(fallback, "")+telegramUnescapeTags(rich[0]), "reply is not sent once and whole") + require.Equal(t, reply, strings.Join(fallback, "")+telegramUnescapeTags(rich[0]), "reply is not sent once and whole") } func TestRefusedEntitiesGoAgainAsPlainText(t *testing.T) { @@ -595,7 +598,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(), "a reply goes as plain text, because Telegram refused its formatting") + require.Contains(t, logs.String(), "reply sent as plain text") require.NotContains(t, logs.String(), telegramTestToken) require.NotContains(t, logs.String(), "secret word") } @@ -608,14 +611,14 @@ func TestBotLogsNoTokenWhenTheNetworkFails(t *testing.T) { daemonStartTestWithBot(t, newModelFake(t), bot) require.Eventually(t, func() bool { - return strings.Contains(logs.String(), "cannot get messages from Telegram: no answer: ") + return strings.Contains(logs.String(), "receive failed: 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) { + server.reply = func(w http.ResponseWriter, _ ModelFakeCall) { modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("ok")}, 100, 0, 5)) } @@ -640,10 +643,11 @@ func TestBotLogsAnOutageOnlyAtItsStartAndEnd(t *testing.T) { }) 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.Equal(t, "receive failed: 502 Bad Gateway (retry in 1ms)", lines[0]) + require.Regexp(t, `^receive back after [0-9]+m?s$`, lines[1]) require.NotContains(t, logs.String(), "secret word") require.NotContains(t, logs.String(), strconv.FormatInt(telegramTestOwner, 10)) }