]> git.giacomolli.uk - onechat/blobdiff - telegram_test.go
Call sendRichMessage of the Bot API
[onechat] / telegram_test.go
index b1cd6ae7348b9c9aedccb77678330fa55a12d54a..2578b77b1c97b98561f3718e56a6c041ec2cc6a7 100644 (file)
@@ -217,7 +217,9 @@ func TestDeliverTriesAgainAfterATimeout(t *testing.T) {
 
        bot, logs := startTestBot(t, telegram)
        bot.api.sendTimeout = 50 * time.Millisecond
-       bot.deliver(t.Context(), "hello")
+       bot.deliver(t.Context(), botPart{
+               text: "hello",
+       })
 
        require.Equal(t, []string{"hello"}, telegram.Sent())
        require.Len(t, telegram.Calls(), 2)
@@ -225,6 +227,81 @@ func TestDeliverTriesAgainAfterATimeout(t *testing.T) {
        require.NotContains(t, logs.String(), testBotToken)
 }
 
+func TestOnlyRepliesGetEntities(t *testing.T) {
+       server := newFakeResponses(t)
+       server.reply = func(w http.ResponseWriter, call fakeCall) {
+               writeEvents(w, completedEvent([]any{messageItem("**Done** with `x`.")}, 100, 0, 5))
+       }
+
+       telegram := newFakeBot(t)
+       bot, _ := startTestBot(t, telegram)
+       socket := startTestDaemonWithBot(t, server, bot)
+       terminal := startTestClient(t, socket)
+       terminal.WaitFor(t, "[connected to OneChat]")
+
+       terminal.Type("make **it** so")
+       waitForSent(t, telegram, "[terminal] make **it** so", "Done with x.")
+
+       telegram.Add(botUpdate{
+               UpdateID: 1,
+               Message:  ownerMessage(""),
+       })
+
+       waitForSent(t, telegram, textOnly)
+
+       entities := map[string]any{}
+       for _, call := range telegram.Calls() {
+               if call.method == "sendMessage" {
+                       entities[call.params["text"].(string)] = call.params["entities"]
+               }
+       }
+
+       require.Equal(t, map[string]any{
+               "[terminal] make **it** so": nil,
+               "Done with x.": []any{
+                       map[string]any{
+                               "type":   "bold",
+                               "offset": float64(0),
+                               "length": float64(4),
+                       },
+                       map[string]any{
+                               "type":   "code",
+                               "offset": float64(10),
+                               "length": float64(1),
+                       },
+               },
+               textOnly: nil,
+       }, entities)
+}
+
+func TestRefusedEntitiesGoAgainAsPlainText(t *testing.T) {
+       telegram := newFakeBot(t)
+       telegram.fail = func(call fakeBotCall) (int, string) {
+               if call.params["entities"] == nil {
+                       return 0, ""
+               }
+
+               return http.StatusBadRequest, "Bad Request: can't parse entities of bot" + testBotToken
+       }
+
+       bot, logs := startTestBot(t, telegram)
+       text, entities := markdownEntities("a **secret** word")
+       bot.deliver(t.Context(), botPart{
+               text:     text,
+               entities: entities,
+       })
+
+       calls := telegram.Calls()
+       require.Len(t, calls, 2)
+       require.NotNil(t, calls[0].params["entities"])
+       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.NotContains(t, logs.String(), testBotToken)
+       require.NotContains(t, logs.String(), "secret word")
+}
+
 func TestBotLogsNoTokenWhenTheNetworkFails(t *testing.T) {
        telegram := newFakeBot(t)
        telegram.Close()
@@ -289,6 +366,49 @@ func TestSplitMessage(t *testing.T) {
        require.Equal(t, long, strings.Join(parts, ""))
 }
 
+func TestSplitEntitiesCutsAnEntityAcrossASplit(t *testing.T) {
+       parts := splitEntities("aabbbbcc", []botEntity{
+               {
+                       Type:   "bold",
+                       Offset: 2,
+                       Length: 4,
+               },
+               {
+                       Type:   "italic",
+                       Offset: 6,
+                       Length: 2,
+               },
+       }, 5)
+
+       require.Equal(t, []botPart{
+               {
+                       text: "aabbb",
+                       entities: []botEntity{
+                               {
+                                       Type:   "bold",
+                                       Offset: 2,
+                                       Length: 3,
+                               },
+                       },
+               },
+               {
+                       text: "bcc",
+                       entities: []botEntity{
+                               {
+                                       Type:   "bold",
+                                       Offset: 0,
+                                       Length: 1,
+                               },
+                               {
+                                       Type:   "italic",
+                                       Offset: 1,
+                                       Length: 2,
+                               },
+                       },
+               },
+       }, parts)
+}
+
 func TestLoadBotConfig(t *testing.T) {
        testCases := []struct {
                name      string