]> git.giacomolli.uk - onechat/blob - whatsapp_batch_test.go
Keep retries after a rejected pair request
[onechat] / whatsapp_batch_test.go
1 package main
2
3 import (
4         "testing"
5         "time"
6
7         "github.com/stretchr/testify/require"
8 )
9
10 func TestWhatsAppBatchText(t *testing.T) {
11         at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)
12         testCases := []struct {
13                 name  string
14                 batch WhatsAppBatch
15                 text  string
16         }{
17                 {
18                         name: "a private chat with a message of another person starts a turn",
19                         batch: WhatsAppBatch{
20                                 Chat: whatsappTestChat,
21                                 Messages: []WhatsAppMessage{
22                                         {
23                                                 Sender: "Ana Souza",
24                                                 Time:   at,
25                                                 Text:   "Are you at home?",
26                                         },
27                                         {
28                                                 Time: at.Add(3 * time.Minute),
29                                                 Text: "Yes",
30                                                 Mine: true,
31                                         },
32                                 },
33                         },
34                         text: "[WhatsApp] Ana Souza, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n12:00 Ana Souza: Are you at home?\n12:03 You: Yes\n" + whatsappBatchEmptyReply,
35                 },
36                 {
37                         name: "a private chat with messages of the user only starts no turn",
38                         batch: WhatsAppBatch{
39                                 Chat: whatsappTestChat,
40                                 Messages: []WhatsAppMessage{
41                                         {
42                                                 Time: at,
43                                                 Text: "On my way.",
44                                                 Mine: true,
45                                         },
46                                 },
47                         },
48                         text: "[WhatsApp] Ana Souza, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n12:00 You: On my way.",
49                 },
50                 {
51                         name: "a group with no mention of the user starts no turn",
52                         batch: WhatsAppBatch{
53                                 Chat: whatsappTestGroup,
54                                 Messages: []WhatsAppMessage{
55                                         {
56                                                 Sender: "Bruno Lima",
57                                                 Time:   at,
58                                                 Text:   "Who brings the cake?",
59                                         },
60                                 },
61                         },
62                         text: "[WhatsApp] Family, group 120363000000000001@g.us, Thursday 2026-01-01\n12:00 Bruno Lima: Who brings the cake?",
63                 },
64                 {
65                         name: "a group with a mention of the user starts a turn",
66                         batch: WhatsAppBatch{
67                                 Chat: whatsappTestGroup,
68                                 Messages: []WhatsAppMessage{
69                                         {
70                                                 Sender:  "Bruno Lima",
71                                                 Time:    at,
72                                                 Text:    "@you can you bring the cake?",
73                                                 Mention: true,
74                                         },
75                                 },
76                         },
77                         text: "[WhatsApp] Family, group 120363000000000001@g.us, Thursday 2026-01-01\n12:00 Bruno Lima (mentions you): @you can you bring the cake?\n" + whatsappBatchEmptyReply,
78                 },
79                 {
80                         name: "a group with a reply to the user starts a turn",
81                         batch: WhatsAppBatch{
82                                 Chat: whatsappTestGroup,
83                                 Messages: []WhatsAppMessage{
84                                         {
85                                                 Sender: "Bruno Lima",
86                                                 Time:   at,
87                                                 Text:   "Thanks!",
88                                                 Reply:  true,
89                                         },
90                                 },
91                         },
92                         text: "[WhatsApp] Family, group 120363000000000001@g.us, Thursday 2026-01-01\n12:00 Bruno Lima (replies to you): Thanks!\n" + whatsappBatchEmptyReply,
93                 },
94         }
95
96         for _, tc := range testCases {
97                 t.Run(tc.name, func(t *testing.T) {
98                         require.Equal(t, tc.text, tc.batch.Text(time.UTC))
99                 })
100         }
101 }