]> git.giacomolli.uk - onechat/blobdiff - whatsapp_test.go
Log each change of the WhatsApp connection
[onechat] / whatsapp_test.go
index 0ba27146bfb62d35c1991c33c58a82cc6dbef6de..5c38d59123714b757bea6d4d330d1672fed0ab60 100644 (file)
@@ -1,11 +1,7 @@
 package main
 
 import (
 package main
 
 import (
-       "crypto/sha256"
-       "encoding/hex"
        "errors"
        "errors"
-       "fmt"
-       "net/http"
        "os"
        "path/filepath"
        "strings"
        "os"
        "path/filepath"
        "strings"
@@ -14,8 +10,6 @@ import (
 
        "github.com/stretchr/testify/require"
        "go.mau.fi/whatsmeow"
 
        "github.com/stretchr/testify/require"
        "go.mau.fi/whatsmeow"
-       "go.mau.fi/whatsmeow/proto/waE2E"
-       "go.mau.fi/whatsmeow/proto/waWeb"
        "go.mau.fi/whatsmeow/types"
        "go.mau.fi/whatsmeow/types/events"
 )
        "go.mau.fi/whatsmeow/types"
        "go.mau.fi/whatsmeow/types/events"
 )
@@ -43,12 +37,38 @@ func TestWhatsAppStatusFollowsTheConnection(t *testing.T) {
 func TestWhatsAppStatusOfAFirstConnectionThatFailed(t *testing.T) {
        dir := t.TempDir()
        whatsappTestSession(t, dir, true)
 func TestWhatsAppStatusOfAFirstConnectionThatFailed(t *testing.T) {
        dir := t.TempDir()
        whatsappTestSession(t, dir, true)
-       whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
+       whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
        daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
 
        <-fake.connects
        require.True(t, fake.Emit(&events.Disconnected{}))
        require.Equal(t, "WhatsApp is linked and disconnected since 2026-01-01 12:00.", whatsapp.Status())
        daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
 
        <-fake.connects
        require.True(t, fake.Emit(&events.Disconnected{}))
        require.Equal(t, "WhatsApp is linked and disconnected since 2026-01-01 12:00.", whatsapp.Status())
+       require.Equal(t, "new WhatsApp messages wait, because the first connection failed (the next try is automatic)\n", logs.String())
+}
+
+func TestWhatsAppLogsEachChangeOfTheConnectionOnce(t *testing.T) {
+       dir := t.TempDir()
+       whatsappTestSession(t, dir, true)
+       clock := newTestClock()
+       whatsapp, fake, logs := whatsappStartTest(t, dir, clock)
+       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
+
+       <-fake.connects
+       require.True(t, fake.Emit(&events.Connected{}))
+       clock.now = clock.now.Add(time.Minute)
+       require.True(t, fake.Emit(&events.Disconnected{}))
+       clock.now = clock.now.Add(time.Minute)
+       require.True(t, fake.Emit(&events.Disconnected{}))
+       clock.now = clock.now.Add(25 * time.Second)
+       require.True(t, fake.Emit(&events.Connected{}))
+
+       require.Equal(t, []string{
+               "WhatsApp is connected, and new messages come in",
+               "new WhatsApp messages wait, because the connection is lost (the next try is automatic)",
+               "WhatsApp is connected again, after 1m25s",
+       }, strings.Split(strings.TrimSuffix(logs.String(), "\n"), "\n"))
+       require.NotContains(t, logs.String(), whatsappTestNumber)
+       require.NotContains(t, logs.String(), whatsappTestLID)
 }
 
 func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) {
 }
 
 func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) {
@@ -140,6 +160,14 @@ func TestWhatsAppPairsWithACode(t *testing.T) {
        items <- whatsmeow.QRChannelSuccess
        asking.WaitFor(t, "WhatsApp is linked to +15550001111.")
 
        items <- whatsmeow.QRChannelSuccess
        asking.WaitFor(t, "WhatsApp is linked to +15550001111.")
 
+       // The library connects again after the link with the context of the
+       // connection, so the end of the pairing must not end it.
+       connections := fake.Connections()
+       require.Len(t, connections, 1)
+       require.Never(t, func() bool {
+               return connections[0].Err() != nil
+       }, 200*time.Millisecond, time.Millisecond)
+
        require.Equal(t, []string{whatsappTestNumber}, fake.Phones())
        require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status())
        require.Equal(t, "[connected to OneChat]\n", other.Output())
        require.Equal(t, []string{whatsappTestNumber}, fake.Phones())
        require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status())
        require.Equal(t, "[connected to OneChat]\n", other.Output())
@@ -368,7 +396,7 @@ func TestWhatsAppLogsNoOwnNumber(t *testing.T) {
        require.Eventually(t, func() bool {
                return logs.String() != ""
        }, 10*time.Second, time.Millisecond)
        require.Eventually(t, func() bool {
                return logs.String() != ""
        }, 10*time.Second, time.Millisecond)
-       require.Equal(t, "connect: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
+       require.Equal(t, "WhatsApp messages do not come, because the connection failed, and it is tried again only at a restart of the daemon: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
 }
 
 func TestWhatsAppPhone(t *testing.T) {
 }
 
 func TestWhatsAppPhone(t *testing.T) {
@@ -454,631 +482,8 @@ func TestWhatsAppCommandArgs(t *testing.T) {
        }
 }
 
        }
 }
 
-// whatsappTestLogTexts returns the texts of the messages in the log of memory,
-// after the log holds at least one message.
-func whatsappTestLogTexts(t *testing.T, memory *Memory) []string {
-       t.Helper()
-
-       require.Eventually(t, func() bool {
-               return memory.Len() > 0
-       }, 10*time.Second, time.Millisecond)
-
-       var texts []string
-       for id := range memory.Len() {
-               message, err := memory.Message(id)
-               require.NoError(t, err)
-
-               texts = append(texts, message.Text)
-       }
-
-       return texts
-}
-
-func TestWhatsAppPrivateBatchStartsATurnWithAnEmptyReply(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       require.NoError(t, whatsapp.device.Contacts.PutContactName(t.Context(), whatsappTestAna, "Ana", "Ana Souza"))
-
-       server := newModelFake(t)
-       server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
-               modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("")}, 100, 0, 5))
-       }
-
-       telegram := newTelegramFake(t)
-       bot, _ := telegramStartTestBot(t, telegram)
-       socket, memory := daemonStartTestWithWhatsApp(t, server, bot, whatsapp)
-       client := terminalStartTestClient(t, socket)
-       client.WaitFor(t, "[connected to OneChat]")
-       <-fake.connects
-
-       // The catch-up holds the batch open until its end, so the two messages
-       // form one batch also on a slow machine.
-       require.True(t, fake.Emit(&events.Connected{}))
-       at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
-       require.True(t, fake.Emit(whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m1", "Are you at home?", at)))
-       require.True(t, fake.Emit(whatsappTestMessage(whatsappTestAna, whatsappTestOwn, "m2", "Yes", at)))
-       require.True(t, fake.Emit(&events.OfflineSyncCompleted{}))
-
-       text := "[WhatsApp] Ana Souza, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n12:00 Ana Souza: Are you at home?\n12:00 You: Yes\n" + whatsappBatchEmptyReply
-       client.WaitFor(t, text+"\n[working]\n[done]\n")
-       require.Equal(t, 1, server.Calls())
-       require.Contains(t, string(server.Call(0).raw), "Are you at home?")
-       require.Equal(t, []string{text}, whatsappTestLogTexts(t, memory))
-       require.Empty(t, telegram.Sent())
-       require.Eventually(t, func() bool {
-               return len(whatsapp.state.Closed()) == 0
-       }, 10*time.Second, time.Millisecond)
-}
-
-func TestWhatsAppGroupTurns(t *testing.T) {
-       testCases := []struct {
-               name    string
-               message func(event *events.Message)
-               line    string
-               turn    bool
-       }{
-               {
-                       name: "plain",
-                       message: func(event *events.Message) {
-                       },
-                       line: "12:00 Bruno Lima: Who brings the cake?",
-               },
-               {
-                       name: "mention",
-                       message: func(event *events.Message) {
-                               event.Message = &waE2E.Message{
-                                       ExtendedTextMessage: &waE2E.ExtendedTextMessage{
-                                               Text: new("@" + whatsappTestLID + " can you bring the cake? Call " + whatsappTestNumber + "."),
-                                               ContextInfo: &waE2E.ContextInfo{
-                                                       MentionedJID: []string{whatsappTestLID + "@lid"},
-                                               },
-                                       },
-                               }
-                       },
-                       line: "12:00 Bruno Lima (mentions you): @you can you bring the cake? Call OWN.",
-                       turn: true,
-               },
-               {
-                       name: "reply",
-                       message: func(event *events.Message) {
-                               event.Message = &waE2E.Message{
-                                       ExtendedTextMessage: &waE2E.ExtendedTextMessage{
-                                               Text: new("Who brings the cake?"),
-                                               ContextInfo: &waE2E.ContextInfo{
-                                                       Participant: new(whatsappTestOwn.String()),
-                                               },
-                                       },
-                               }
-                       },
-                       line: "12:00 Bruno Lima (replies to you): Who brings the cake?",
-                       turn: true,
-               },
-       }
-
-       for _, tc := range testCases {
-               t.Run(tc.name, func(t *testing.T) {
-                       whatsapp, fake := whatsappStartTestLinked(t)
-                       fake.groups = []*types.GroupInfo{
-                               {
-                                       JID: whatsappTestFamily,
-                                       GroupName: types.GroupName{
-                                               Name: "Family",
-                                       },
-                               },
-                       }
-
-                       server := newModelFake(t)
-                       socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
-                       client := terminalStartTestClient(t, socket)
-                       client.WaitFor(t, "[connected to OneChat]")
-                       whatsappTestConnect(t, fake)
-
-                       event := whatsappTestMessage(whatsappTestFamily, whatsappTestBruno, "m1", "Who brings the cake?", time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC))
-                       event.Info.PushName = "Bruno Lima"
-                       tc.message(event)
-                       require.True(t, fake.Emit(event))
-
-                       text := "[WhatsApp] Family, group 120363000000000001@g.us, Thursday 2026-01-01\n" + tc.line
-                       if tc.turn {
-                               text += "\n" + whatsappBatchEmptyReply
-                               client.WaitFor(t, "[done]")
-                               require.Equal(t, 1, server.Calls())
-                       } else {
-                               client.WaitFor(t, text)
-                               time.Sleep(100 * time.Millisecond)
-                               require.Zero(t, server.Calls())
-                       }
-
-                       require.Equal(t, text, whatsappTestLogTexts(t, memory)[0])
-               })
-       }
-}
-
-func TestWhatsAppTakesOnlyText(t *testing.T) {
-       at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
-       testCases := []struct {
-               name   string
-               change func(event *events.Message)
-       }{
-               {
-                       name: "an edit",
-                       change: func(event *events.Message) {
-                               event.IsEdit = true
-                       },
-               },
-               {
-                       name: "old history",
-                       change: func(event *events.Message) {
-                               event.SourceWebMsg = &waWeb.WebMessageInfo{}
-                       },
-               },
-               {
-                       name: "a view-once message",
-                       change: func(event *events.Message) {
-                               event.IsViewOnce = true
-                       },
-               },
-               {
-                       name: "a disappearing message",
-                       change: func(event *events.Message) {
-                               event.IsEphemeral = true
-                       },
-               },
-               {
-                       name: "the chat of the user with itself",
-                       change: func(event *events.Message) {
-                               event.Info.Chat = whatsappTestOwn
-                       },
-               },
-               {
-                       name: "a status broadcast",
-                       change: func(event *events.Message) {
-                               event.Info.Chat = types.StatusBroadcastJID
-                       },
-               },
-               {
-                       name: "a photo with a caption",
-                       change: func(event *events.Message) {
-                               event.Message = &waE2E.Message{
-                                       ImageMessage: &waE2E.ImageMessage{
-                                               Caption: new("look"),
-                                       },
-                               }
-                       },
-               },
-               {
-                       name: "a text of spaces",
-                       change: func(event *events.Message) {
-                               event.Message.Conversation = new("  ")
-                       },
-               },
-       }
-
-       for _, tc := range testCases {
-               t.Run(tc.name, func(t *testing.T) {
-                       whatsapp, _ := whatsappStartTestLinked(t)
-                       event := whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m1", "hi", at)
-                       tc.change(event)
-                       require.True(t, whatsapp.handle(event))
-                       require.NoError(t, whatsapp.state.CloseAll())
-                       require.Empty(t, whatsapp.state.Closed())
-               })
-       }
-}
-
-// whatsappStartTestLinked opens a WhatsApp with a linked device in a temporary
-// directory, with a quiet time of 50 ms for its batches.
-func whatsappStartTestLinked(t *testing.T) (*WhatsApp, *WhatsAppFake) {
-       t.Helper()
-
-       dir := t.TempDir()
-       whatsappTestSession(t, dir, true)
-       whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
-       whatsapp.state.quiet = 50 * time.Millisecond
-
-       return whatsapp, fake
-}
-
-func TestWhatsAppCatchupGivesOneTurn(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       server := newModelFake(t)
-       socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
-       client := terminalStartTestClient(t, socket)
-       client.WaitFor(t, "[connected to OneChat]")
-       <-fake.connects
-
-       require.True(t, fake.Emit(&events.Connected{}))
-       at := time.Date(2026, 1, 1, 8, 0, 0, 0, time.UTC)
-       for index, event := range []*events.Message{
-               whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m1", "Good morning", at),
-               whatsappTestMessage(whatsappTestBruno, whatsappTestBruno, "m2", "Call me", at.Add(time.Hour)),
-               whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m3", "Are you there?", at.Add(2*time.Hour)),
-       } {
-               event.Info.PushName = []string{"Ana", "Bruno", "Ana"}[index]
-               require.True(t, fake.Emit(event))
-       }
-
-       time.Sleep(100 * time.Millisecond)
-       require.Zero(t, memory.Len())
-
-       require.True(t, fake.Emit(&events.OfflineSyncCompleted{}))
-       client.WaitFor(t, "[done]")
-       require.Equal(t, []string{
-               "[WhatsApp] Ana, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n08:00 Ana: Good morning\n" + whatsappBatchEmptyReply,
-               "[WhatsApp] Bruno, private chat 15550003333@s.whatsapp.net, Thursday 2026-01-01\n09:00 Bruno: Call me\n" + whatsappBatchEmptyReply,
-               "[WhatsApp] Ana, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n10:00 Ana: Are you there?\n" + whatsappBatchEmptyReply,
-       }, whatsappTestLogTexts(t, memory)[:3])
-
-       time.Sleep(100 * time.Millisecond)
-       require.Equal(t, 1, server.Calls())
-       for _, text := range []string{"Good morning", "Call me", "Are you there?"} {
-               require.Contains(t, string(server.Call(0).raw), text)
-       }
-}
-
-func TestWhatsAppCatchupEndsAfterItsLimit(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       whatsapp.catchupLimit = 100 * time.Millisecond
-       socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       client := terminalStartTestClient(t, socket)
-       client.WaitFor(t, "[connected to OneChat]")
-       <-fake.connects
-
-       require.True(t, fake.Emit(&events.Connected{}))
-       require.True(t, fake.Emit(whatsappTestMessage(whatsappTestAna, whatsappTestOwn, "m1", "On my way.", time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC))))
-       client.WaitFor(t, "12:00 You: On my way.")
-       require.Len(t, whatsappTestLogTexts(t, memory), 1)
-}
-
-func TestWhatsAppStateOfTheStateFileWaitForTheCatchup(t *testing.T) {
-       dir := t.TempDir()
-       whatsappTestSession(t, dir, true)
-       before, _, _ := whatsappStartTest(t, dir, newTestClock())
-       _, err := before.state.Add(whatsappTestChat, WhatsAppMessage{
-               ID:     "m1",
-               Sender: "Ana Souza",
-               Time:   time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC),
-               Text:   "Did you see my message?",
-       }, time.Now().Add(-time.Hour))
-       require.NoError(t, err)
-
-       whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
-       socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       client := terminalStartTestClient(t, socket)
-       client.WaitFor(t, "[connected to OneChat]")
-       <-fake.connects
-
-       time.Sleep(100 * time.Millisecond)
-       require.Zero(t, memory.Len())
-
-       require.True(t, fake.Emit(&events.Connected{}))
-       require.True(t, fake.Emit(&events.OfflineSyncCompleted{}))
-       client.WaitFor(t, "12:00 Ana Souza: Did you see my message?")
-       client.WaitFor(t, "[done]")
-}
-
-// whatsappTestConnect waits for the connection of the daemon and gives it the
-// events of a connection with an empty catch-up.
-func whatsappTestConnect(t *testing.T, fake *WhatsAppFake) {
-       t.Helper()
-
-       <-fake.connects
-       require.True(t, fake.Emit(&events.Connected{}))
-       require.True(t, fake.Emit(&events.OfflineSyncCompleted{}))
-}
-
-func TestWhatsAppFindMatchesNamesWithNoRegardToCaseAndAccents(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       fake.groups = []*types.GroupInfo{
-               {
-                       JID: whatsappTestFamily,
-                       GroupName: types.GroupName{
-                               Name: "Família Souza",
-                       },
-               },
-       }
-
-       whatsappTestContacts(t, whatsapp)
-       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       whatsappTestConnect(t, fake)
-
-       testCases := []struct {
-               name   string
-               query  string
-               answer string
-       }{
-               {
-                       name:   "no accent finds an accent",
-                       query:  "joao",
-                       answer: "15550004444@s.whatsapp.net | João Silva | contact",
-               },
-               {
-                       name:   "a part of a name finds contacts and groups",
-                       query:  "SOUZA",
-                       answer: "15550002222@s.whatsapp.net | Ana Souza | contact\n120363000000000001@g.us | Família Souza | group",
-               },
-               {
-                       name:   "a business name",
-                       query:  "bakery",
-                       answer: "15550005555@s.whatsapp.net | Corner Bakery | contact",
-               },
-               {
-                       name:   "no match",
-                       query:  "Zed",
-                       answer: "No contact or group has a name with \"Zed\".",
-               },
-       }
-
-       for _, tc := range testCases {
-               t.Run(tc.name, func(t *testing.T) {
-                       answer, err := whatsapp.Find(t.Context(), tc.query)
-                       require.NoError(t, err)
-                       require.Equal(t, tc.answer, answer)
-               })
-       }
-}
-
-func TestWhatsAppFindGivesAtMostTwentyMatches(t *testing.T) {
-       whatsapp, _ := whatsappStartTestLinked(t)
-       for index := range 25 {
-               jid := types.NewJID(fmt.Sprintf("1555001%04d", index), types.DefaultUserServer)
-               require.NoError(t, whatsapp.device.Contacts.PutContactName(t.Context(), jid, "", fmt.Sprintf("Member %02d", index)))
-       }
-
-       answer, err := whatsapp.Find(t.Context(), "member")
-       require.NoError(t, err)
-
-       lines := strings.Split(answer, "\n")
-       require.Len(t, lines, 22)
-       require.Equal(t, "15550010000@s.whatsapp.net | Member 00 | contact", lines[0])
-       require.Equal(t, "5 more: give a longer part of the name.", lines[20])
-       require.Equal(t, "The groups are not in this answer, because WhatsApp is disconnected.", lines[21])
-}
-
-func TestWhatsAppFindBeforeTheNamesArrive(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       whatsappTestConnect(t, fake)
-
-       answer, err := whatsapp.Find(t.Context(), "ana")
-       require.NoError(t, err)
-       require.Equal(t, whatsappNamesNotReady, answer)
-}
-
-func TestWhatsAppFindWithoutALink(t *testing.T) {
-       whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
-
-       answer, err := whatsapp.Find(t.Context(), "ana")
-       require.NoError(t, err)
-       require.Equal(t, whatsappNotLinked, answer)
-}
-
-func TestAgentFindsAWhatsAppContact(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       whatsappTestContacts(t, whatsapp)
-       server := newModelFake(t)
-       server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
-               if call.number == 0 {
-                       modelWriteEvents(w, modelCompletedEvent([]any{modelFunctionCallItem("fc_1", "call_1", "whatsapp_find", `{"name":"ana"}`)}, 100, 0, 5))
-                       return
-               }
-
-               modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("Found Ana.")}, 100, 0, 5))
-       }
-
-       socket, _ := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
-       client := terminalStartTestClient(t, socket)
-       whatsappTestConnect(t, fake)
-
-       client.Type("Find Ana on WhatsApp.")
-       client.WaitFor(t, "[result] 15550002222@s.whatsapp.net | Ana Souza | contact\nFound Ana.\n[done]")
-}
-
 func TestSubagentHasNoWhatsAppTools(t *testing.T) {
        box := &Toolbox{}
        require.Equal(t, `error: unknown tool "whatsapp_find"`, box.run(t.Context(), "whatsapp_find", `{"name":"ana"}`))
        require.Equal(t, `error: unknown tool "whatsapp_send"`, box.run(t.Context(), "whatsapp_send", `{"id":"a","chat":"15550002222@s.whatsapp.net","text":"hi"}`))
 }
 func TestSubagentHasNoWhatsAppTools(t *testing.T) {
        box := &Toolbox{}
        require.Equal(t, `error: unknown tool "whatsapp_find"`, box.run(t.Context(), "whatsapp_find", `{"name":"ana"}`))
        require.Equal(t, `error: unknown tool "whatsapp_send"`, box.run(t.Context(), "whatsapp_send", `{"id":"a","chat":"15550002222@s.whatsapp.net","text":"hi"}`))
 }
-
-// whatsappTestContacts puts the contacts of the tests into the session of
-// whatsapp: Ana Souza, Joao Silva with an accent, a business, and the user.
-func whatsappTestContacts(t *testing.T, whatsapp *WhatsApp) {
-       t.Helper()
-
-       contacts := whatsapp.device.Contacts
-       require.NoError(t, contacts.PutContactName(t.Context(), whatsappTestAna, "Ana", "Ana Souza"))
-       require.NoError(t, contacts.PutContactName(t.Context(), types.NewJID("15550004444", types.DefaultUserServer), "João", "João Silva"))
-       _, _, err := contacts.PutBusinessName(t.Context(), types.NewJID("15550005555", types.DefaultUserServer), "Corner Bakery")
-       require.NoError(t, err)
-       require.NoError(t, contacts.PutContactName(t.Context(), whatsappTestOwn, "Me", "Me Souza"))
-}
-
-func TestWhatsAppSendSendsOnceForEachID(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       whatsappTestContacts(t, whatsapp)
-       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       whatsappTestConnect(t, fake)
-
-       answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "The keys are under the mat.")
-       require.NoError(t, err)
-       require.Equal(t, "Sent to Ana Souza at 12:05.", answer)
-
-       answer, err = whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "The keys are under the mat.")
-       require.NoError(t, err)
-       require.Equal(t, "Sent to Ana Souza at 12:05. This is the answer of the first call with this id: nothing was sent again.", answer)
-
-       _, err = whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "Another text.")
-       require.EqualError(t, err, "the id keys belongs to another message: give a new id for each message")
-
-       require.Equal(t, []WhatsAppFakeSend{
-               {
-                       to:   whatsappTestAna.String(),
-                       text: "The keys are under the mat.",
-                       id:   "MSG1",
-               },
-       }, fake.Sent())
-
-       again, err := openWhatsAppState(filepath.Join(whatsapp.dir, whatsappStateFileName), whatsappBatchQuiet, whatsappBatchWindow)
-       require.NoError(t, err)
-
-       record, found := again.Send("keys")
-       require.True(t, found)
-       require.Equal(t, whatsappSent, record.State)
-       require.Equal(t, "MSG1", record.MessageID)
-}
-
-func TestWhatsAppSendToAGroup(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       fake.groups = []*types.GroupInfo{
-               {
-                       JID: whatsappTestFamily,
-                       GroupName: types.GroupName{
-                               Name: "Family",
-                       },
-               },
-       }
-
-       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       whatsappTestConnect(t, fake)
-
-       answer, err := whatsapp.Send(t.Context(), "cake", whatsappTestFamily.String(), "I bring the cake.")
-       require.NoError(t, err)
-       require.Equal(t, "Sent to Family at 12:05.", answer)
-}
-
-func TestWhatsAppSendThatFails(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       fake.sendErr = errors.New("server error for " + whatsappTestNumber)
-       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       whatsappTestConnect(t, fake)
-
-       for range 2 {
-               answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
-               require.NoError(t, err)
-               require.True(t, strings.HasPrefix(answer, "The send failed: server error for OWN."), answer)
-       }
-
-       require.Len(t, fake.Sent(), 1)
-}
-
-func TestWhatsAppSendCutByAStop(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       whatsappTestConnect(t, fake)
-
-       digest := sha256.Sum256([]byte("hi"))
-       require.NoError(t, whatsapp.state.PutSend(WhatsAppSend{
-               ID:     "keys",
-               Chat:   whatsappTestAna.String(),
-               Digest: hex.EncodeToString(digest[:]),
-               State:  whatsappSending,
-       }))
-
-       answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
-       require.NoError(t, err)
-       require.Contains(t, answer, "its result is unknown")
-       require.Empty(t, fake.Sent())
-}
-
-func TestWhatsAppSendRefuses(t *testing.T) {
-       testCases := []struct {
-               name string
-               id   string
-               chat string
-               text string
-               err  string
-       }{
-               {
-                       name: "no id",
-                       id:   " ",
-                       chat: whatsappTestAna.String(),
-                       text: "hi",
-                       err:  "the id must have 1 to 100 bytes",
-               },
-               {
-                       name: "no text",
-                       id:   "a",
-                       chat: whatsappTestAna.String(),
-                       text: "",
-                       err:  "the text must have 1 to 65536 bytes",
-               },
-               {
-                       name: "a name for a chat",
-                       id:   "a",
-                       chat: "Ana Souza",
-                       text: "hi",
-                       err:  `"Ana Souza" is not the ID of a WhatsApp chat: find the ID with whatsapp_find`,
-               },
-               {
-                       name: "a status broadcast",
-                       id:   "a",
-                       chat: types.StatusBroadcastJID.String(),
-                       text: "hi",
-                       err:  `"status@broadcast" is not a private chat or a group`,
-               },
-               {
-                       name: "the chat of the user",
-                       id:   "a",
-                       chat: whatsappTestOwn.String(),
-                       text: "hi",
-                       err:  "this is the chat of the user with itself: send to another chat",
-               },
-       }
-
-       whatsapp, fake := whatsappStartTestLinked(t)
-       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       whatsappTestConnect(t, fake)
-
-       for _, tc := range testCases {
-               t.Run(tc.name, func(t *testing.T) {
-                       _, err := whatsapp.Send(t.Context(), tc.id, tc.chat, tc.text)
-                       require.EqualError(t, err, tc.err)
-               })
-       }
-
-       require.Empty(t, fake.Sent())
-}
-
-func TestWhatsAppSendWhileDisconnected(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
-       whatsappTestConnect(t, fake)
-       require.True(t, fake.Emit(&events.Disconnected{}))
-
-       _, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
-       require.EqualError(t, err, "WhatsApp is disconnected, so nothing was sent: try again later")
-
-       require.True(t, fake.Emit(&events.Connected{}))
-       answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
-       require.NoError(t, err)
-       require.Equal(t, "Sent to +15550002222 at 12:05.", answer)
-}
-
-func TestWhatsAppSendWithoutALink(t *testing.T) {
-       whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
-
-       answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
-       require.NoError(t, err)
-       require.Equal(t, whatsappNotLinked, answer)
-}
-
-func TestAgentSendsAWhatsAppMessage(t *testing.T) {
-       whatsapp, fake := whatsappStartTestLinked(t)
-       whatsappTestContacts(t, whatsapp)
-       server := newModelFake(t)
-       server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
-               if call.number == 0 {
-                       modelWriteEvents(w, modelCompletedEvent([]any{modelFunctionCallItem("fc_1", "call_1", "whatsapp_send", `{"id":"keys","chat":"15550002222@s.whatsapp.net","text":"The keys are under the mat."}`)}, 100, 0, 5))
-                       return
-               }
-
-               modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("Sent.")}, 100, 0, 5))
-       }
-
-       socket, _ := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
-       client := terminalStartTestClient(t, socket)
-       whatsappTestConnect(t, fake)
-
-       client.Type("Tell Ana that the keys are under the mat.")
-       client.WaitFor(t, "[result] Sent to Ana Souza at 12:05.\nSent.\n[done]")
-       require.Len(t, fake.Sent(), 1)
-}