]> git.giacomolli.uk - onechat/blob - whatsapp_send_test.go
Describe child agent directories and limits
[onechat] / whatsapp_send_test.go
1 package main
2
3 import (
4 "crypto/sha256"
5 "encoding/hex"
6 "errors"
7 "net/http"
8 "path/filepath"
9 "strings"
10 "testing"
11
12 "github.com/stretchr/testify/require"
13 "go.mau.fi/whatsmeow/types"
14 "go.mau.fi/whatsmeow/types/events"
15 )
16
17 func TestWhatsAppSendSendsOnceForEachID(t *testing.T) {
18 whatsapp, fake := whatsappStartTestLinked(t)
19 whatsappTestContacts(t, whatsapp)
20 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
21 whatsappTestConnect(t, fake)
22
23 answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "The keys are under the mat.")
24 require.NoError(t, err)
25 require.Equal(t, "Sent to Ana Souza at 12:05.", answer)
26
27 answer, err = whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "The keys are under the mat.")
28 require.NoError(t, err)
29 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)
30
31 _, err = whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "Another text.")
32 require.EqualError(t, err, "the id keys belongs to another message: give a new id for each message")
33
34 require.Equal(t, []WhatsAppFakeSend{
35 {
36 to: whatsappTestAna.String(),
37 text: "The keys are under the mat.",
38 id: "MSG1",
39 },
40 }, fake.Sent())
41
42 again, err := openWhatsAppState(filepath.Join(whatsapp.dir, whatsappStateFileName), whatsappBatchQuiet, whatsappBatchWindow)
43 require.NoError(t, err)
44
45 record, found := again.Send("keys")
46 require.True(t, found)
47 require.Equal(t, whatsappSent, record.State)
48 require.Equal(t, "MSG1", record.MessageID)
49 }
50
51 func TestWhatsAppSendToAGroup(t *testing.T) {
52 whatsapp, fake := whatsappStartTestLinked(t)
53 fake.groups = []*types.GroupInfo{
54 {
55 JID: whatsappTestFamily,
56 GroupName: types.GroupName{
57 Name: "Family",
58 },
59 },
60 }
61
62 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
63 whatsappTestConnect(t, fake)
64
65 answer, err := whatsapp.Send(t.Context(), "cake", whatsappTestFamily.String(), "I bring the cake.")
66 require.NoError(t, err)
67 require.Equal(t, "Sent to Family at 12:05.", answer)
68 }
69
70 func TestWhatsAppSendThatFails(t *testing.T) {
71 whatsapp, fake := whatsappStartTestLinked(t)
72 fake.sendErr = errors.New("server error for " + whatsappTestNumber)
73
74 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
75 whatsappTestConnect(t, fake)
76
77 for range 2 {
78 answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
79 require.NoError(t, err)
80 require.True(t, strings.HasPrefix(answer, "The send failed: server error for OWN."), answer)
81 }
82
83 require.Len(t, fake.Sent(), 1)
84 }
85
86 func TestWhatsAppSendCutByAStop(t *testing.T) {
87 whatsapp, fake := whatsappStartTestLinked(t)
88 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
89 whatsappTestConnect(t, fake)
90
91 digest := sha256.Sum256([]byte("hi"))
92 require.NoError(t, whatsapp.state.PutSend(WhatsAppSend{
93 ID: "keys",
94 Chat: whatsappTestAna.String(),
95 Digest: hex.EncodeToString(digest[:]),
96 State: whatsappSending,
97 }))
98
99 answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
100 require.NoError(t, err)
101 require.Contains(t, answer, "its result is unknown")
102 require.Empty(t, fake.Sent())
103 }
104
105 func TestWhatsAppSendRefuses(t *testing.T) {
106 testCases := []struct {
107 name string
108 id string
109 chat string
110 text string
111 err string
112 }{
113 {
114 name: "no id",
115 id: " ",
116 chat: whatsappTestAna.String(),
117 text: "hi",
118 err: "the id must have 1 to 100 bytes",
119 },
120 {
121 name: "no text",
122 id: "a",
123 chat: whatsappTestAna.String(),
124 text: "",
125 err: "the text must have 1 to 65536 bytes",
126 },
127 {
128 name: "a name for a chat",
129 id: "a",
130 chat: "Ana Souza",
131 text: "hi",
132 err: `"Ana Souza" is not the ID of a WhatsApp chat: find the ID with whatsapp_find`,
133 },
134 {
135 name: "a status broadcast",
136 id: "a",
137 chat: types.StatusBroadcastJID.String(),
138 text: "hi",
139 err: `"status@broadcast" is not a private chat or a group`,
140 },
141 {
142 name: "the chat of the user",
143 id: "a",
144 chat: whatsappTestOwn.String(),
145 text: "hi",
146 err: "this is the chat of the user with itself: send to another chat",
147 },
148 }
149
150 whatsapp, fake := whatsappStartTestLinked(t)
151 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
152 whatsappTestConnect(t, fake)
153
154 for _, tc := range testCases {
155 t.Run(tc.name, func(t *testing.T) {
156 _, err := whatsapp.Send(t.Context(), tc.id, tc.chat, tc.text)
157 require.EqualError(t, err, tc.err)
158 })
159 }
160
161 require.Empty(t, fake.Sent())
162 }
163
164 func TestWhatsAppSendWhileDisconnected(t *testing.T) {
165 whatsapp, fake := whatsappStartTestLinked(t)
166 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
167 whatsappTestConnect(t, fake)
168 require.True(t, fake.Emit(&events.Disconnected{}))
169
170 _, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
171 require.EqualError(t, err, "WhatsApp is disconnected, so nothing was sent: try again later")
172
173 require.True(t, fake.Emit(&events.Connected{}))
174
175 answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
176 require.NoError(t, err)
177 require.Equal(t, "Sent to +15550002222 at 12:05.", answer)
178 }
179
180 func TestWhatsAppSendWithoutALink(t *testing.T) {
181 whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
182
183 answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
184 require.NoError(t, err)
185 require.Equal(t, whatsappNotLinked, answer)
186 }
187
188 func TestAgentSendsAWhatsAppMessage(t *testing.T) {
189 whatsapp, fake := whatsappStartTestLinked(t)
190 whatsappTestContacts(t, whatsapp)
191
192 server := newModelFake(t)
193 server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
194 if call.number == 0 {
195 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))
196
197 return
198 }
199
200 modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("Sent.")}, 100, 0, 5))
201 }
202
203 socket, _ := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
204 client := terminalStartTestClient(t, socket)
205 whatsappTestConnect(t, fake)
206
207 client.Type("Tell Ana that the keys are under the mat.")
208 client.WaitFor(t, "[result] Sent to Ana Souza at 12:05.\nSent.\n[done]")
209 require.Len(t, fake.Sent(), 1)
210 }