]> git.giacomolli.uk - onechat/blobdiff - telegram_test.go
Move shared test helpers into helper files
[onechat] / telegram_test.go
index 44c9c8fe16de2bb285b58c976947a0be583e8a43..cf46106874d3d4d641e6a663158216f93e45dfb4 100644 (file)
@@ -390,6 +390,67 @@ func TestLongReplyGoesAsRichMessagesSplitBetweenBlocks(t *testing.T) {
        require.Equal(t, []string{paragraphs, table}, telegram.Sent())
 }
 
        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 \\<details> tag and `<b>`"},
+               },
+               {
+                       name:   "entity fallback shows the tag as written",
+                       status: http.StatusBadRequest,
+                       sent:   []string{"a <details> tag and <b>"},
+               },
+       }
+
+       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 <details> tag and `<b>`",
+                       })
+
+                       require.Equal(t, tc.sent, telegram.Sent())
+               })
+       }
+}
+
+func TestLongReplySplitCountsTheEscapedTags(t *testing.T) {
+       paragraph := "A <b> 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) {
 func TestRefusedEntitiesGoAgainAsPlainText(t *testing.T) {
        telegram := newTelegramFake(t)
        telegram.fail = func(call TelegramFakeCall) (int, string) {