]> git.giacomolli.uk - onechat/blob - whatsapp_send_test.go
Check error handling and common Go mistakes
[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 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
74 whatsappTestConnect(t, fake)
75
76 for range 2 {
77 answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
78 require.NoError(t, err)
79 require.True(t, strings.HasPrefix(answer, "The send failed: server error for OWN."), answer)
80 }
81
82 require.Len(t, fake.Sent(), 1)
83 }
84
85 func TestWhatsAppSendCutByAStop(t *testing.T) {
86 whatsapp, fake := whatsappStartTestLinked(t)
87 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
88 whatsappTestConnect(t, fake)
89
90 digest := sha256.Sum256([]byte("hi"))
91 require.NoError(t, whatsapp.state.PutSend(WhatsAppSend{
92 ID: "keys",
93 Chat: whatsappTestAna.String(),
94 Digest: hex.EncodeToString(digest[:]),
95 State: whatsappSending,
96 }))
97
98 answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
99 require.NoError(t, err)
100 require.Contains(t, answer, "its result is unknown")
101 require.Empty(t, fake.Sent())
102 }
103
104 func TestWhatsAppSendRefuses(t *testing.T) {
105 testCases := []struct {
106 name string
107 id string
108 chat string
109 text string
110 err string
111 }{
112 {
113 name: "no id",
114 id: " ",
115 chat: whatsappTestAna.String(),
116 text: "hi",
117 err: "the id must have 1 to 100 bytes",
118 },
119 {
120 name: "no text",
121 id: "a",
122 chat: whatsappTestAna.String(),
123 text: "",
124 err: "the text must have 1 to 65536 bytes",
125 },
126 {
127 name: "a name for a chat",
128 id: "a",
129 chat: "Ana Souza",
130 text: "hi",
131 err: `"Ana Souza" is not the ID of a WhatsApp chat: find the ID with whatsapp_find`,
132 },
133 {
134 name: "a status broadcast",
135 id: "a",
136 chat: types.StatusBroadcastJID.String(),
137 text: "hi",
138 err: `"status@broadcast" is not a private chat or a group`,
139 },
140 {
141 name: "the chat of the user",
142 id: "a",
143 chat: whatsappTestOwn.String(),
144 text: "hi",
145 err: "this is the chat of the user with itself: send to another chat",
146 },
147 }
148
149 whatsapp, fake := whatsappStartTestLinked(t)
150 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
151 whatsappTestConnect(t, fake)
152
153 for _, tc := range testCases {
154 t.Run(tc.name, func(t *testing.T) {
155 _, err := whatsapp.Send(t.Context(), tc.id, tc.chat, tc.text)
156 require.EqualError(t, err, tc.err)
157 })
158 }
159
160 require.Empty(t, fake.Sent())
161 }
162
163 func TestWhatsAppSendWhileDisconnected(t *testing.T) {
164 whatsapp, fake := whatsappStartTestLinked(t)
165 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
166 whatsappTestConnect(t, fake)
167 require.True(t, fake.Emit(&events.Disconnected{}))
168
169 _, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
170 require.EqualError(t, err, "WhatsApp is disconnected, so nothing was sent: try again later")
171
172 require.True(t, fake.Emit(&events.Connected{}))
173 answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
174 require.NoError(t, err)
175 require.Equal(t, "Sent to +15550002222 at 12:05.", answer)
176 }
177
178 func TestWhatsAppSendWithoutALink(t *testing.T) {
179 whatsapp, _, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
180
181 answer, err := whatsapp.Send(t.Context(), "keys", whatsappTestAna.String(), "hi")
182 require.NoError(t, err)
183 require.Equal(t, whatsappNotLinked, answer)
184 }
185
186 func TestAgentSendsAWhatsAppMessage(t *testing.T) {
187 whatsapp, fake := whatsappStartTestLinked(t)
188 whatsappTestContacts(t, whatsapp)
189 server := newModelFake(t)
190 server.reply = func(w http.ResponseWriter, call ModelFakeCall) {
191 if call.number == 0 {
192 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))
193 return
194 }
195
196 modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("Sent.")}, 100, 0, 5))
197 }
198
199 socket, _ := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
200 client := terminalStartTestClient(t, socket)
201 whatsappTestConnect(t, fake)
202
203 client.Type("Tell Ana that the keys are under the mat.")
204 client.WaitFor(t, "[result] Sent to Ana Souza at 12:05.\nSent.\n[done]")
205 require.Len(t, fake.Sent(), 1)
206 }