package simplex_test import ( "encoding/json" "testing" "sneak.berlin/go/simplexcalc/internal/simplex" ) // newChatItems holds one message the bot must answer and four it must // not, each shaped as the chat client sends it. const newChatItems = `{"type":"newChatItems","user":{"userId":1},"chatItems":[ {"chatInfo":{"type":"direct","contact":{"contactId":3,"localDisplayName":"alice"}}, "chatItem":{"chatDir":{"type":"directRcv"}, "meta":{"itemId":41,"itemText":"2 + 2","itemEdited":false}, "content":{"type":"rcvMsgContent","msgContent":{"type":"text","text":"2 + 2"}}, "mentions":{},"reactions":[]}}, {"chatInfo":{"type":"direct","contact":{"contactId":3}}, "chatItem":{"chatDir":{"type":"directSnd"},"meta":{"itemId":42}, "content":{"type":"sndMsgContent","msgContent":{"type":"text","text":"4"}}}}, {"chatInfo":{"type":"group","groupInfo":{"groupId":9}}, "chatItem":{"chatDir":{"type":"groupRcv","groupMember":{"groupMemberId":5}}, "meta":{"itemId":43}, "content":{"type":"rcvMsgContent","msgContent":{"type":"text","text":"1 + 1"}}}}, {"chatInfo":{"type":"direct","contact":{"contactId":3}}, "chatItem":{"chatDir":{"type":"directRcv"},"meta":{"itemId":44}, "content":{"type":"rcvMsgContent","msgContent":{"type":"file","text":"3 * 3"}}}}, {"chatInfo":{"type":"direct","contact":{"contactId":3}}, "chatItem":{"chatDir":{"type":"directRcv"},"meta":{"itemId":45}, "content":{"type":"rcvDirectEvent","rcvDirectEvent":{"type":"contactDeleted"}}}} ]}` // TestMessage: only a text message a contact sent in a direct chat is a // message to answer. Answering the bot's own messages would loop. func TestMessage(t *testing.T) { t.Parallel() var r simplex.NewChatItems err := json.Unmarshal([]byte(newChatItems), &r) if err != nil { t.Fatalf("decoding: %v", err) } var got []simplex.Message for _, item := range r.ChatItems { if msg, ok := item.Message(); ok { got = append(got, msg) } } want := simplex.Message{ContactID: 3, ItemID: 41, Text: "2 + 2"} if len(got) != 1 || got[0] != want { t.Errorf("messages = %+v, want only %+v", got, want) } }