-
Notifications
You must be signed in to change notification settings - Fork 0
/
messenger_test.go
134 lines (118 loc) · 3.61 KB
/
messenger_test.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package core_test
import (
"fmt"
"testing"
"time"
"github.com/hood-chat/core"
"github.com/hood-chat/core/entity"
logging "github.com/ipfs/go-log"
"github.com/stretchr/testify/require"
)
func TestMessenger(t *testing.T) {
t.Log("start test")
err := logging.SetLogLevel("msgr-core", "DEBUG")
require.NoError(t, err)
// err = logging.SetLogLevel("*", "DEBUG")
require.NoError(t, err)
opt1 := core.DefaultOption()
opt2 := core.DefaultOption()
mr1 := core.NewMessengerAPI(t.TempDir()+"/h1", opt1, core.DefaultRoutedHost{})
t.Log("somthing wrong")
_, err = mr1.IdentityAPI().SignUp("h1")
require.NoError(t, err)
mr1.Start()
t.Log("messenger 1 created")
_, err = mr1.IdentityAPI().Get()
require.NoError(t, err)
mr2 := core.NewMessengerAPI(t.TempDir()+"/h2", opt2, core.DefaultRoutedHost{})
_, err = mr2.IdentityAPI().SignUp("h2")
mr2.Start()
require.NoError(t, err)
user2, err := mr2.IdentityAPI().Get()
require.NoError(t, err)
t.Log("messenger 2 created")
err = mr1.ContactBookAPI().Put(*user2.ToContact())
require.NoError(t, err)
time.Sleep(5 * time.Second)
t.Log("contact added")
chat1, err := mr1.ChatAPI().New(core.NewPrivateChat(*user2.ToContact()))
require.NoError(t, err)
chat1, err = mr1.ChatAPI().ChatInfo(chat1.ID)
require.NoError(t, err)
t.Log("Chat created")
_, err = mr1.ChatAPI().Send(chat1.ID, "hello")
require.NoError(t, err)
_, err = mr1.ChatAPI().Send(chat1.ID, "hello")
require.NoError(t, err)
t.Log("message sent")
time.Sleep(60 * time.Second)
chat2, err := mr2.ChatAPI().ChatInfo(chat1.ID)
require.NoError(t, err)
require.Equal(t, chat1.ID, chat2.ID)
msgs, err := mr2.ChatAPI().Messages(chat1.ID, 0, 20)
require.NoError(t, err)
t.Logf("list of messages \n %v", msgs)
// Test Event hand event handler
time.Sleep(10 * time.Second)
msgs, err = mr1.ChatAPI().Messages(chat1.ID, 0, 20)
require.Equal(t, 2, len(msgs))
require.Equal(t, int(chat2.Unread), len(msgs))
require.NoError(t, err)
for _, val := range msgs {
require.Equal(t, val.Status, entity.Sent)
}
// Test Seen
err = mr2.ChatAPI().Seen(chat1.ID)
require.NoError(t, err)
chat2, err = mr2.ChatAPI().ChatInfo(chat1.ID)
require.NoError(t, err)
require.Equal(t, 0, int(chat2.Unread))
mr1.Stop()
mr2.Stop()
}
func getMessengers(t *testing.T, n int) []core.MessengerAPI {
messengers := make([]core.MessengerAPI, 0)
for i := 0; i < n; i++ {
name := "h" + fmt.Sprint(i)
opt := core.DefaultOption()
mr := core.NewMessengerAPI(t.TempDir()+"/"+name, opt, core.DefaultRoutedHost{})
_, err := mr.IdentityAPI().SignUp(name)
if err != nil {
panic("cant create host")
}
require.NoError(t, err)
mr.Start()
messengers = append(messengers, mr)
}
return messengers
}
func TestGroup(t *testing.T) {
err := logging.SetLogLevel("msgr-core", "DEBUG")
require.NoError(t, err)
// err = logging.SetLogLevel("*", "DEBUG")
require.NoError(t, err)
gpName := "something"
msgrs := getMessengers(t, 3)
members := make([]entity.Contact, 0)
for _, v := range msgrs {
self, err := v.IdentityAPI().Get()
require.NoError(t, err)
members = append(members, *self.ToContact())
}
chat, err := msgrs[0].ChatAPI().New(core.NewGroupChat(gpName, members))
require.NoError(t, err)
err = msgrs[0].ChatAPI().Invite(chat.ID, chat.Members)
require.NoError(t, err)
time.Sleep(30 * time.Second)
msg, err := msgrs[0].ChatAPI().Send(chat.ID, "helllooooooo")
require.NoError(t, err)
time.Sleep(5 * time.Second)
for _, v := range msgrs[1:] {
ch, err := v.ChatAPI().ChatInfo(chat.ID)
require.NoError(t, err)
require.Equal(t, ch.Name, chat.Name)
t.Logf("%v = %v",chat,ch)
_, err = v.ChatAPI().Message(msg.ID)
require.NoError(t, err)
}
}