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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package matrix_test
import (
"testing"
"github.com/stretchr/testify/assert"
"maunium.net/go/gomatrix"
"maunium.net/go/gomuks/matrix"
"maunium.net/go/gomuks/matrix/rooms"
)
func TestGomuksSyncer_ProcessResponse_Initial(t *testing.T) {
syncer := matrix.NewGomuksSyncer(&mockSyncerSession{})
var initDoneCalled = false
syncer.InitDoneCallback = func() {
initDoneCalled = true
}
syncer.ProcessResponse(newRespSync(), "")
assert.True(t, syncer.FirstSyncDone)
assert.True(t, initDoneCalled)
}
func TestGomuksSyncer_ProcessResponse(t *testing.T) {
mss := &mockSyncerSession{
userID: "@tulir:maunium.net",
rooms: map[string]*rooms.Room{
"!foo:maunium.net": {
Room: gomatrix.NewRoom("!foo:maunium.net"),
},
"!bar:maunium.net": {
Room: gomatrix.NewRoom("!bar:maunium.net"),
},
"!test:maunium.net": {
Room: gomatrix.NewRoom("!test:maunium.net"),
},
},
}
ml := &mockListener{}
syncer := matrix.NewGomuksSyncer(mss)
syncer.OnEventType("m.room.member", ml.receive)
syncer.OnEventType("m.room.message", ml.receive)
syncer.GetFilterJSON("@tulir:maunium.net")
joinEvt := &gomatrix.Event{
ID: "!join:maunium.net",
Type: "m.room.member",
Sender: "@tulir:maunium.net",
StateKey: ptr("̣@tulir:maunium.net"),
Content: map[string]interface{}{
"membership": "join",
},
}
messageEvt := &gomatrix.Event{
ID: "!msg:maunium.net",
Type: "m.room.message",
Content: map[string]interface{}{
"body": "foo",
"msgtype": "m.text",
},
}
unhandledEvt := &gomatrix.Event{
ID: "!unhandled:maunium.net",
Type: "m.room.unhandled_event",
}
inviteEvt := &gomatrix.Event{
ID: "!invite:matrix.org",
Type: "m.room.member",
Sender: "@you:matrix.org",
StateKey: ptr("̣@tulir:maunium.net"),
Content: map[string]interface{}{
"membership": "invite",
},
}
leaveEvt := &gomatrix.Event{
ID: "!leave:matrix.org",
Type: "m.room.member",
Sender: "@you:matrix.org",
StateKey: ptr("̣@tulir:maunium.net"),
Content: map[string]interface{}{
"membership": "leave",
},
}
resp := newRespSync()
resp.Rooms.Join["!foo:maunium.net"] = join{
State: events{Events: []*gomatrix.Event{joinEvt}},
Timeline: timeline{Events: []*gomatrix.Event{messageEvt, unhandledEvt}},
}
resp.Rooms.Invite["!bar:maunium.net"] = struct {
State struct {
Events []*gomatrix.Event `json:"events"`
} `json:"invite_state"`
}{
State: events{Events: []*gomatrix.Event{inviteEvt}},
}
resp.Rooms.Leave["!test:maunium.net"] = struct {
State struct {
Events []*gomatrix.Event `json:"events"`
} `json:"state"`
Timeline struct {
Events []*gomatrix.Event `json:"events"`
Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"`
} `json:"timeline"`
}{
State: events{Events: []*gomatrix.Event{leaveEvt}},
}
syncer.ProcessResponse(resp, "since")
assert.Contains(t, ml.received, joinEvt, joinEvt.ID)
assert.Contains(t, ml.received, messageEvt, messageEvt.ID)
assert.NotContains(t, ml.received, unhandledEvt, unhandledEvt.ID)
assert.Contains(t, ml.received, inviteEvt, inviteEvt.ID)
assert.Contains(t, ml.received, leaveEvt, leaveEvt.ID)
}
type mockSyncerSession struct {
rooms map[string]*rooms.Room
userID string
}
func (mss *mockSyncerSession) GetRoom(id string) *rooms.Room {
return mss.rooms[id]
}
func (mss *mockSyncerSession) GetUserID() string {
return mss.userID
}
type events struct {
Events []*gomatrix.Event `json:"events"`
}
type timeline struct {
Events []*gomatrix.Event `json:"events"`
Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"`
}
type join struct {
State struct {
Events []*gomatrix.Event `json:"events"`
} `json:"state"`
Timeline struct {
Events []*gomatrix.Event `json:"events"`
Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"`
} `json:"timeline"`
Ephemeral struct {
Events []*gomatrix.Event `json:"events"`
} `json:"ephemeral"`
AccountData struct {
Events []*gomatrix.Event `json:"events"`
} `json:"account_data"`
}
func ptr(text string) *string {
return &text
}
type mockListener struct {
received []*gomatrix.Event
}
func (ml *mockListener) receive(source matrix.EventSource, evt *gomatrix.Event) {
ml.received = append(ml.received, evt)
}
func newRespSync() *gomatrix.RespSync {
resp := &gomatrix.RespSync{NextBatch: "123"}
resp.Rooms.Join = make(map[string]struct {
State struct {
Events []*gomatrix.Event `json:"events"`
} `json:"state"`
Timeline struct {
Events []*gomatrix.Event `json:"events"`
Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"`
} `json:"timeline"`
Ephemeral struct {
Events []*gomatrix.Event `json:"events"`
} `json:"ephemeral"`
AccountData struct {
Events []*gomatrix.Event `json:"events"`
} `json:"account_data"`
})
resp.Rooms.Invite = make(map[string]struct {
State struct {
Events []*gomatrix.Event `json:"events"`
} `json:"invite_state"`
})
resp.Rooms.Leave = make(map[string]struct {
State struct {
Events []*gomatrix.Event `json:"events"`
} `json:"state"`
Timeline struct {
Events []*gomatrix.Event `json:"events"`
Limited bool `json:"limited"`
PrevBatch string `json:"prev_batch"`
} `json:"timeline"`
})
return resp
}
|