+ {
+ name: "other refusal drops the reply",
+ status: http.StatusForbidden,
+ always: true,
+ methods: []string{"sendRichMessage"},
+ lines: 1,
+ logText: "<3>reply lost: 403",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ telegram := newTelegramFake(t)
+ failed := false
+ telegram.fail = func(call TelegramFakeCall) (int, string) {
+ if call.method != "sendRichMessage" || failed && !tc.always {
+ return 0, ""
+ }
+
+ failed = true
+
+ return tc.status, "refused for bot" + telegramTestToken
+ }
+
+ bot, logs := telegramStartTestBot(t, telegram)
+ bot.post(t.Context(), Event{
+ Kind: "reply",
+ Text: "a **secret** word",
+ })
+
+ var methods []string
+
+ for _, call := range telegram.Calls() {
+ methods = append(methods, call.method)
+ }
+
+ require.Equal(t, tc.methods, methods)
+ require.Equal(t, tc.sent, telegram.Sent())
+ require.Equal(t, tc.lines, strings.Count(logs.String(), "\n"))
+ require.Contains(t, logs.String(), tc.logText)
+
+ if tc.lines == 2 {
+ require.Contains(t, logs.String(), "<6>send back after=")
+ }
+
+ require.NotContains(t, logs.String(), telegramTestToken)
+ require.NotContains(t, logs.String(), "secret word")
+
+ calls := telegram.Calls()
+ if calls[len(calls)-1].method == "sendMessage" {
+ require.NotNil(t, calls[len(calls)-1].params["entities"])
+ }
+ })
+ }
+}
+
+func TestLongReplyGoesAsRichMessagesSplitBetweenBlocks(t *testing.T) {
+ paragraphs := strings.Repeat("A paragraph of the reply.\n\n", telegramRichBytes/len("A paragraph of the reply.\n\n"))
+ table := "| a | b |\n|---|---|\n| 1 | 2 |\n"
+ reply := paragraphs + table
+
+ telegram := newTelegramFake(t)
+ bot, _ := telegramStartTestBot(t, telegram)
+ bot.post(t.Context(), Event{
+ Kind: "reply",
+ Text: reply,
+ })
+
+ 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 TestRefusedRichPartGoesWithTheTextAsWritten(t *testing.T) {
+ paragraph := "A <b> & c.\n\n"
+ reply := strings.Repeat(paragraph, telegramRichBytes/len(paragraph))
+
+ telegram := newTelegramFake(t)
+ refused := false
+ telegram.fail = func(call TelegramFakeCall) (int, string) {
+ if call.method != "sendRichMessage" || refused {
+ return 0, ""
+ }
+
+ refused = true
+
+ return http.StatusBadRequest, "Bad Request"
+ }
+
+ bot, _ := telegramStartTestBot(t, telegram)
+ bot.post(t.Context(), Event{
+ Kind: "reply",
+ Text: reply,
+ })
+
+ var fallback, rich []string
+
+ for _, call := range telegram.Calls() {
+ switch {
+ case !call.ok:
+ case call.method == "sendMessage":
+ fallback = append(fallback, call.params["text"].(string))
+ case call.method == "sendRichMessage":
+ rich = append(rich, call.params["rich_message"].(map[string]any)["markdown"].(string))
+ }
+ }
+
+ require.Greater(t, len(fallback), 1)
+ require.Len(t, rich, 1)
+ require.True(t, strings.HasPrefix(reply, strings.Join(fallback, "")), "fallback text is not the reply as written")
+
+ for _, text := range fallback {
+ require.LessOrEqual(t, telegramUTF16Units(text), telegramMessageUnits)
+ }
+
+ require.Equal(t, reply, strings.Join(fallback, "")+telegramUnescapeTags(rich[0]), "reply is not sent once and whole")