X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/1379d7d0d0173416237ddf8ebe820743d2cecfa6..de170709599e849c6b409d247b800dc68b0c7ab7:/telegram_test.go diff --git a/telegram_test.go b/telegram_test.go index 44c9c8f..cf46106 100644 --- a/telegram_test.go +++ b/telegram_test.go @@ -390,6 +390,67 @@ func TestLongReplyGoesAsRichMessagesSplitBetweenBlocks(t *testing.T) { require.Equal(t, []string{paragraphs, table}, telegram.Sent()) } +func TestReplyTagsGoAsText(t *testing.T) { + testCases := []struct { + name string + status int + sent []string + }{ + { + name: "rich message escapes the tag outside code", + sent: []string{"a \\
tag and ``"}, + }, + { + name: "entity fallback shows the tag as written", + status: http.StatusBadRequest, + sent: []string{"a
tag and "}, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + telegram := newTelegramFake(t) + telegram.fail = func(call TelegramFakeCall) (int, string) { + if call.method != "sendRichMessage" { + return 0, "" + } + + return tc.status, "Bad Request" + } + + bot, _ := telegramStartTestBot(t, telegram) + bot.post(t.Context(), Event{ + Kind: "reply", + Text: "a
tag and ``", + }) + + require.Equal(t, tc.sent, telegram.Sent()) + }) + } +} + +func TestLongReplySplitCountsTheEscapedTags(t *testing.T) { + paragraph := "A tag.\n\n" + count := telegramRichBytes / len(paragraph) + reply := strings.Repeat(paragraph, count) + require.LessOrEqual(t, len(reply), telegramRichBytes) + + telegram := newTelegramFake(t) + bot, _ := telegramStartTestBot(t, telegram) + bot.post(t.Context(), Event{ + Kind: "reply", + Text: reply, + }) + + sent := telegram.Sent() + require.Len(t, sent, 2) + require.Equal(t, telegramEscapeTags(reply), strings.Join(sent, "")) + + for _, markdown := range sent { + require.LessOrEqual(t, len(markdown), telegramRichBytes) + } +} + func TestRefusedEntitiesGoAgainAsPlainText(t *testing.T) { telegram := newTelegramFake(t) telegram.fail = func(call TelegramFakeCall) (int, string) {