+ 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))