]> git.giacomolli.uk - onechat/blobdiff - telegram_test.go
Move shared test helpers into helper files
[onechat] / telegram_test.go
index 2f944fabddbb1b6117ab047da4b50cbf803a0b41..cf46106874d3d4d641e6a663158216f93e45dfb4 100644 (file)
@@ -21,8 +21,8 @@ func TestOwnerTalksToTheAgentThroughTelegram(t *testing.T) {
 
        telegram := newTelegramFake(t)
        bot, _ := telegramStartTestBot(t, telegram)
-       socket := startTestDaemonWithBot(t, server, bot)
-       terminal := startTestClient(t, socket)
+       socket := daemonStartTestWithBot(t, server, bot)
+       terminal := terminalStartTestClient(t, socket)
        terminal.WaitFor(t, "[connected to OneChat]")
 
        telegram.Add(TelegramUpdate{
@@ -43,8 +43,8 @@ func TestOwnerTalksToTheAgentThroughTelegram(t *testing.T) {
 func TestBotIgnoresEveryoneButTheOwner(t *testing.T) {
        telegram := newTelegramFake(t)
        bot, logs := telegramStartTestBot(t, telegram)
-       socket := startTestDaemonWithBot(t, newModelFake(t), bot)
-       terminal := startTestClient(t, socket)
+       socket := daemonStartTestWithBot(t, newModelFake(t), bot)
+       terminal := terminalStartTestClient(t, socket)
        terminal.WaitFor(t, "[connected to OneChat]")
 
        group := telegramOwnerMessage("secret in a group")
@@ -81,7 +81,7 @@ func TestBotIgnoresEveryoneButTheOwner(t *testing.T) {
 func TestBotAnswersThatOnlyTextIsSupported(t *testing.T) {
        telegram := newTelegramFake(t)
        bot, _ := telegramStartTestBot(t, telegram)
-       startTestDaemonWithBot(t, newModelFake(t), bot)
+       daemonStartTestWithBot(t, newModelFake(t), bot)
 
        telegram.Add(TelegramUpdate{
                UpdateID: 1,
@@ -106,7 +106,7 @@ func TestStopFromTelegramInterruptsTheTurn(t *testing.T) {
 
        telegram := newTelegramFake(t)
        bot, _ := telegramStartTestBot(t, telegram)
-       startTestDaemonWithBot(t, server, bot)
+       daemonStartTestWithBot(t, server, bot)
 
        telegram.Add(TelegramUpdate{
                UpdateID: 1,
@@ -149,8 +149,8 @@ func TestBotStartsAtTheStoredOffset(t *testing.T) {
                Message:  telegramOwnerMessage("new after the restart"),
        })
 
-       socket := startTestDaemonWithBot(t, server, bot)
-       terminal := startTestClient(t, socket)
+       socket := daemonStartTestWithBot(t, server, bot)
+       terminal := terminalStartTestClient(t, socket)
        terminal.WaitFor(t, "[connected to OneChat]")
        telegramWaitForSent(t, telegram, "ok")
 
@@ -189,7 +189,7 @@ func TestBotTriesAgainAfterFailures(t *testing.T) {
        }
 
        bot, logs := telegramStartTestBot(t, telegram)
-       startTestDaemonWithBot(t, server, bot)
+       daemonStartTestWithBot(t, server, bot)
 
        telegram.Add(TelegramUpdate{
                UpdateID: 1,
@@ -236,8 +236,8 @@ func TestRepliesGoAsRichMessages(t *testing.T) {
 
        telegram := newTelegramFake(t)
        bot, _ := telegramStartTestBot(t, telegram)
-       socket := startTestDaemonWithBot(t, server, bot)
-       terminal := startTestClient(t, socket)
+       socket := daemonStartTestWithBot(t, server, bot)
+       terminal := terminalStartTestClient(t, socket)
        terminal.WaitFor(t, "[connected to OneChat]")
 
        terminal.Type("make **it** so")
@@ -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 \\<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) {
@@ -423,7 +484,7 @@ func TestBotLogsNoTokenWhenTheNetworkFails(t *testing.T) {
        telegram.Close()
 
        bot, logs := telegramStartTestBot(t, telegram)
-       startTestDaemonWithBot(t, newModelFake(t), bot)
+       daemonStartTestWithBot(t, newModelFake(t), bot)
 
        require.Eventually(t, func() bool {
                return strings.Count(logs.String(), "no valid answer") >= 2
@@ -653,10 +714,10 @@ func TestLoadBotConfig(t *testing.T) {
 // telegramStartTestBot returns a bot of telegramTestOwner on server, with its
 // offset file in a temporary directory and short waits, and the buffer of its
 // log.
-func telegramStartTestBot(t *testing.T, server *TelegramFake) (*TelegramBot, *syncBuffer) {
+func telegramStartTestBot(t *testing.T, server *TelegramFake) (*TelegramBot, *SyncBuffer) {
        t.Helper()
 
-       logs := &syncBuffer{}
+       logs := &SyncBuffer{}
        bot := newTelegramBot(newTelegramAPI(server.URL, telegramTestToken), telegramTestOwner, filepath.Join(t.TempDir(), telegramOffsetFileName), log.New(logs, "", 0))
        bot.minWait = time.Millisecond
        bot.maxWait = 10 * time.Millisecond