Repository navigation
Expand file tree
/
Copy pathmsgkit_test.go
More file actions
51 lines (45 loc) · 1.71 KB
/
Copy pathmsgkit_test.go
File metadata and controls
51 lines (45 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package msgkit
import (
"testing"
"time"
"github.com/raucheacho/msgkit/whatsapp"
)
// TestMapWhatsAppEvent covers the whatsapp.Event -> msgkit.Event mapping: an
// inbound message (with media metadata + bytes), each receipt type, session
// events, and a dropped unknown type.
func TestMapWhatsAppEvent(t *testing.T) {
now := time.Now().UTC()
inbound, ok := mapWhatsAppEvent(whatsapp.Event{
Type: whatsapp.EventMessageReceived, Phone: "212600000000", MessageID: "wamid.1", At: now,
Data: map[string]any{
"text": "hello", "pushName": "Alice",
"mediaKind": "image", "mediaMimeType": "image/jpeg", "mediaBytes": []byte{1, 2, 3},
},
})
if !ok || inbound.Type != EventMessage {
t.Fatalf("received -> %+v ok=%v, want EventMessage", inbound, ok)
}
if inbound.From != "212600000000" || inbound.Text != "hello" || inbound.PushName != "Alice" {
t.Fatalf("inbound fields = %+v", inbound)
}
if inbound.MediaKind != "image" || inbound.MediaMime != "image/jpeg" || len(inbound.MediaData) != 3 {
t.Fatalf("inbound media = %+v", inbound)
}
cases := map[string]EventType{
whatsapp.EventMessageSent: EventSent,
whatsapp.EventMessageDelivered: EventDelivered,
whatsapp.EventMessageRead: EventRead,
whatsapp.EventMessageFailed: EventFailed,
whatsapp.EventSessionReconnect: EventConnected,
whatsapp.EventSessionDisconnect: EventDisconnected,
}
for wire, want := range cases {
got, ok := mapWhatsAppEvent(whatsapp.Event{Type: wire, MessageID: "m"})
if !ok || got.Type != want {
t.Errorf("%q -> %v ok=%v, want %v", wire, got.Type, ok, want)
}
}
if _, ok := mapWhatsAppEvent(whatsapp.Event{Type: "something.else"}); ok {
t.Fatal("expected an unknown event type to be dropped (ok=false)")
}
}