2018-04-17 14:42:43 +02:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2019-01-17 13:13:25 +01:00
|
|
|
// Copyright (C) 2019 Tulir Asokan
|
2018-04-17 14:42:43 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-04-17 14:42:43 +02:00
|
|
|
// 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
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-04-17 14:42:43 +02:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-04-17 14:42:43 +02:00
|
|
|
|
|
|
|
package matrix_test
|
2018-04-22 22:26:56 +02:00
|
|
|
|
|
|
|
import (
|
2018-04-24 22:52:31 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"maunium.net/go/gomuks/matrix"
|
2018-04-22 22:26:56 +02:00
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
2018-11-14 00:11:40 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2018-04-22 22:26:56 +02:00
|
|
|
)
|
|
|
|
|
2018-04-24 22:52:31 +02:00
|
|
|
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": {
|
2018-11-13 23:00:35 +01:00
|
|
|
Room: mautrix.NewRoom("!foo:maunium.net"),
|
2018-04-24 22:52:31 +02:00
|
|
|
},
|
|
|
|
"!bar:maunium.net": {
|
2018-11-13 23:00:35 +01:00
|
|
|
Room: mautrix.NewRoom("!bar:maunium.net"),
|
2018-04-24 22:52:31 +02:00
|
|
|
},
|
|
|
|
"!test:maunium.net": {
|
2018-11-13 23:00:35 +01:00
|
|
|
Room: mautrix.NewRoom("!test:maunium.net"),
|
2018-04-24 22:52:31 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ml := &mockListener{}
|
|
|
|
syncer := matrix.NewGomuksSyncer(mss)
|
2018-11-14 00:11:40 +01:00
|
|
|
syncer.OnEventType(mautrix.EventMessage, ml.receive)
|
|
|
|
syncer.OnEventType(mautrix.StateMember, ml.receive)
|
2018-05-10 19:56:46 +02:00
|
|
|
syncer.GetFilterJSON("@tulir:maunium.net")
|
2018-04-24 22:52:31 +02:00
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
joinEvt := &mautrix.Event{
|
2018-04-24 22:52:31 +02:00
|
|
|
ID: "!join:maunium.net",
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateMember,
|
2018-04-24 22:52:31 +02:00
|
|
|
Sender: "@tulir:maunium.net",
|
|
|
|
StateKey: ptr("̣@tulir:maunium.net"),
|
2018-11-14 00:11:40 +01:00
|
|
|
Content: mautrix.Content{
|
|
|
|
Membership: mautrix.MembershipJoin,
|
2018-04-24 22:52:31 +02:00
|
|
|
},
|
|
|
|
}
|
2018-11-13 23:00:35 +01:00
|
|
|
messageEvt := &mautrix.Event{
|
2018-04-24 22:52:31 +02:00
|
|
|
ID: "!msg:maunium.net",
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.EventMessage,
|
|
|
|
Content: mautrix.Content{
|
|
|
|
Body: "foo",
|
|
|
|
MsgType: mautrix.MsgText,
|
2018-04-24 22:52:31 +02:00
|
|
|
},
|
|
|
|
}
|
2018-11-13 23:00:35 +01:00
|
|
|
unhandledEvt := &mautrix.Event{
|
2018-04-24 22:52:31 +02:00
|
|
|
ID: "!unhandled:maunium.net",
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.EventType{Type: "m.room.unhandled_event"},
|
2018-04-24 22:52:31 +02:00
|
|
|
}
|
2018-11-13 23:00:35 +01:00
|
|
|
inviteEvt := &mautrix.Event{
|
2018-04-24 22:52:31 +02:00
|
|
|
ID: "!invite:matrix.org",
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateMember,
|
2018-04-24 22:52:31 +02:00
|
|
|
Sender: "@you:matrix.org",
|
|
|
|
StateKey: ptr("̣@tulir:maunium.net"),
|
2018-11-14 00:11:40 +01:00
|
|
|
Content: mautrix.Content{
|
|
|
|
Membership: mautrix.MembershipInvite,
|
2018-04-24 22:52:31 +02:00
|
|
|
},
|
|
|
|
}
|
2018-11-13 23:00:35 +01:00
|
|
|
leaveEvt := &mautrix.Event{
|
2018-04-24 22:52:31 +02:00
|
|
|
ID: "!leave:matrix.org",
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateMember,
|
2018-04-24 22:52:31 +02:00
|
|
|
Sender: "@you:matrix.org",
|
|
|
|
StateKey: ptr("̣@tulir:maunium.net"),
|
2018-11-14 00:11:40 +01:00
|
|
|
Content: mautrix.Content{
|
|
|
|
Membership: mautrix.MembershipLeave,
|
2018-04-24 22:52:31 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := newRespSync()
|
|
|
|
resp.Rooms.Join["!foo:maunium.net"] = join{
|
2018-11-13 23:00:35 +01:00
|
|
|
State: events{Events: []*mautrix.Event{joinEvt}},
|
|
|
|
Timeline: timeline{Events: []*mautrix.Event{messageEvt, unhandledEvt}},
|
2018-04-24 22:52:31 +02:00
|
|
|
}
|
|
|
|
resp.Rooms.Invite["!bar:maunium.net"] = struct {
|
|
|
|
State struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"invite_state"`
|
|
|
|
}{
|
2018-11-13 23:00:35 +01:00
|
|
|
State: events{Events: []*mautrix.Event{inviteEvt}},
|
2018-04-24 22:52:31 +02:00
|
|
|
}
|
|
|
|
resp.Rooms.Leave["!test:maunium.net"] = struct {
|
|
|
|
State struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"state"`
|
|
|
|
Timeline struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-11-14 00:11:40 +01:00
|
|
|
Limited bool `json:"limited"`
|
|
|
|
PrevBatch string `json:"prev_batch"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"timeline"`
|
|
|
|
}{
|
2018-11-13 23:00:35 +01:00
|
|
|
State: events{Events: []*mautrix.Event{leaveEvt}},
|
2018-04-24 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-04-22 22:26:56 +02:00
|
|
|
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
|
|
|
|
}
|
2018-04-24 22:52:31 +02:00
|
|
|
|
|
|
|
type events struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type timeline struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-11-14 00:11:40 +01:00
|
|
|
Limited bool `json:"limited"`
|
|
|
|
PrevBatch string `json:"prev_batch"`
|
2018-04-24 22:52:31 +02:00
|
|
|
}
|
|
|
|
type join struct {
|
|
|
|
State struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"state"`
|
|
|
|
Timeline struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-11-14 00:11:40 +01:00
|
|
|
Limited bool `json:"limited"`
|
|
|
|
PrevBatch string `json:"prev_batch"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"timeline"`
|
|
|
|
Ephemeral struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"ephemeral"`
|
|
|
|
AccountData struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"account_data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ptr(text string) *string {
|
|
|
|
return &text
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockListener struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
received []*mautrix.Event
|
2018-04-24 22:52:31 +02:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
func (ml *mockListener) receive(source matrix.EventSource, evt *mautrix.Event) {
|
2018-04-24 22:52:31 +02:00
|
|
|
ml.received = append(ml.received, evt)
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:00:35 +01:00
|
|
|
func newRespSync() *mautrix.RespSync {
|
|
|
|
resp := &mautrix.RespSync{NextBatch: "123"}
|
2018-04-24 22:52:31 +02:00
|
|
|
resp.Rooms.Join = make(map[string]struct {
|
|
|
|
State struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"state"`
|
|
|
|
Timeline struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-11-14 00:11:40 +01:00
|
|
|
Limited bool `json:"limited"`
|
|
|
|
PrevBatch string `json:"prev_batch"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"timeline"`
|
|
|
|
Ephemeral struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"ephemeral"`
|
|
|
|
AccountData struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"account_data"`
|
|
|
|
})
|
|
|
|
resp.Rooms.Invite = make(map[string]struct {
|
|
|
|
State struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"invite_state"`
|
|
|
|
})
|
|
|
|
resp.Rooms.Leave = make(map[string]struct {
|
|
|
|
State struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"state"`
|
|
|
|
Timeline struct {
|
2018-11-13 23:00:35 +01:00
|
|
|
Events []*mautrix.Event `json:"events"`
|
2018-11-14 00:11:40 +01:00
|
|
|
Limited bool `json:"limited"`
|
|
|
|
PrevBatch string `json:"prev_batch"`
|
2018-04-24 22:52:31 +02:00
|
|
|
} `json:"timeline"`
|
|
|
|
})
|
|
|
|
return resp
|
|
|
|
}
|