2018-04-17 14:42:43 +02:00
|
|
|
// 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 rooms_test
|
2018-04-22 22:23:36 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"maunium.net/go/gomuks/matrix/rooms"
|
2018-11-14 00:11:40 +01:00
|
|
|
"maunium.net/go/mautrix"
|
2018-04-22 22:23:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewRoom_DefaultValues(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
assert.Equal(t, "!test:maunium.net", room.ID)
|
|
|
|
assert.Equal(t, "@tulir:maunium.net", room.SessionUserID)
|
|
|
|
assert.Empty(t, room.GetMembers())
|
|
|
|
assert.Equal(t, "Empty room", room.GetTitle())
|
|
|
|
assert.Empty(t, room.GetAliases())
|
|
|
|
assert.Empty(t, room.GetCanonicalAlias())
|
|
|
|
assert.Empty(t, room.GetTopic())
|
2018-11-14 00:11:40 +01:00
|
|
|
assert.Nil(t, room.GetMember(room.GetSessionOwner()))
|
2018-04-22 22:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetCanonicalAlias(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
2018-11-13 23:00:35 +01:00
|
|
|
room.UpdateState(&mautrix.Event{
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateCanonicalAlias,
|
|
|
|
Content: mautrix.Content{
|
|
|
|
Alias: "#foo:maunium.net",
|
2018-04-22 22:23:36 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Equal(t, "#foo:maunium.net", room.GetCanonicalAlias())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetTopic(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
2018-11-13 23:00:35 +01:00
|
|
|
room.UpdateState(&mautrix.Event{
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateTopic,
|
|
|
|
Content: mautrix.Content{
|
|
|
|
Topic: "test topic",
|
2018-04-22 22:23:36 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.Equal(t, "test topic", room.GetTopic())
|
|
|
|
}
|
|
|
|
|
2018-05-02 22:45:17 +02:00
|
|
|
func TestRoom_Tags_Empty(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
assert.Empty(t, room.RawTags)
|
|
|
|
tags := room.Tags()
|
|
|
|
assert.Len(t, tags, 1)
|
|
|
|
assert.Equal(t, "", tags[0].Tag)
|
2018-05-04 11:13:23 +02:00
|
|
|
assert.Equal(t, "0.5", tags[0].Order)
|
2018-05-02 22:45:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_Tags_NotEmpty(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
2018-05-04 11:13:23 +02:00
|
|
|
room.RawTags = []rooms.RoomTag{{Tag: "foo", Order: "1"}, {Tag: "bar", Order: "1"}}
|
2018-05-02 22:45:17 +02:00
|
|
|
tags := room.Tags()
|
|
|
|
assert.Equal(t, room.RawTags, tags)
|
|
|
|
}
|
|
|
|
|
2018-04-22 22:23:36 +02:00
|
|
|
func TestRoom_GetAliases(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
addAliases(room)
|
|
|
|
|
|
|
|
aliases := room.GetAliases()
|
|
|
|
assert.Contains(t, aliases, "#bar:maunium.net")
|
|
|
|
assert.Contains(t, aliases, "#test:maunium.net")
|
|
|
|
assert.Contains(t, aliases, "#foo:matrix.org")
|
|
|
|
assert.Contains(t, aliases, "#test:matrix.org")
|
|
|
|
}
|
|
|
|
|
|
|
|
func addName(room *rooms.Room) {
|
2018-11-13 23:00:35 +01:00
|
|
|
room.UpdateState(&mautrix.Event{
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateRoomName,
|
|
|
|
Content: mautrix.Content{
|
|
|
|
Name: "Test room",
|
2018-04-22 22:23:36 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func addCanonicalAlias(room *rooms.Room) {
|
2018-11-13 23:00:35 +01:00
|
|
|
room.UpdateState(&mautrix.Event{
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateCanonicalAlias,
|
|
|
|
Content: mautrix.Content{
|
|
|
|
Alias: "#foo:maunium.net",
|
2018-04-22 22:23:36 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func addAliases(room *rooms.Room) {
|
|
|
|
server1 := "maunium.net"
|
2018-11-13 23:00:35 +01:00
|
|
|
room.UpdateState(&mautrix.Event{
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateAliases,
|
2018-04-22 22:23:36 +02:00
|
|
|
StateKey: &server1,
|
2018-11-14 00:11:40 +01:00
|
|
|
Content: mautrix.Content{
|
|
|
|
Aliases: []string{"#bar:maunium.net", "#test:maunium.net", "#foo:maunium.net"},
|
2018-04-22 22:23:36 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
server2 := "matrix.org"
|
2018-11-13 23:00:35 +01:00
|
|
|
room.UpdateState(&mautrix.Event{
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateAliases,
|
2018-04-22 22:23:36 +02:00
|
|
|
StateKey: &server2,
|
2018-11-14 00:11:40 +01:00
|
|
|
Content: mautrix.Content{
|
|
|
|
Aliases: []string{"#foo:matrix.org", "#test:matrix.org"},
|
2018-04-22 22:23:36 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func addMembers(room *rooms.Room, count int) {
|
|
|
|
user1 := "@tulir:maunium.net"
|
2018-11-13 23:00:35 +01:00
|
|
|
room.UpdateState(&mautrix.Event{
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateMember,
|
2018-04-22 22:23:36 +02:00
|
|
|
StateKey: &user1,
|
2018-11-14 00:11:40 +01:00
|
|
|
Content: mautrix.Content{
|
|
|
|
Member: mautrix.Member{
|
|
|
|
Displayname: "tulir",
|
|
|
|
Membership: mautrix.MembershipJoin,
|
|
|
|
},
|
2018-04-22 22:23:36 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
for i := 1; i < count; i++ {
|
|
|
|
userN := fmt.Sprintf("@user_%d:matrix.org", i+1)
|
2018-11-14 00:11:40 +01:00
|
|
|
content := mautrix.Content{
|
|
|
|
Member: mautrix.Member{
|
|
|
|
Membership: mautrix.MembershipJoin,
|
|
|
|
},
|
2018-04-22 22:23:36 +02:00
|
|
|
}
|
2018-04-22 23:04:10 +02:00
|
|
|
if i%2 == 1 {
|
2018-11-14 00:11:40 +01:00
|
|
|
content.Displayname = fmt.Sprintf("User #%d", i+1)
|
2018-04-22 22:23:36 +02:00
|
|
|
}
|
2018-04-22 23:04:10 +02:00
|
|
|
if i%5 == 0 {
|
2018-11-14 00:11:40 +01:00
|
|
|
content.Membership = mautrix.MembershipInvite
|
2018-04-22 22:23:36 +02:00
|
|
|
}
|
2018-11-13 23:00:35 +01:00
|
|
|
room.UpdateState(&mautrix.Event{
|
2018-11-14 00:11:40 +01:00
|
|
|
Type: mautrix.StateMember,
|
2018-04-22 22:23:36 +02:00
|
|
|
StateKey: &userN,
|
2018-04-22 23:04:10 +02:00
|
|
|
Content: content,
|
2018-04-22 22:23:36 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetMembers(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
addMembers(room, 6)
|
|
|
|
|
|
|
|
members := room.GetMembers()
|
|
|
|
assert.Len(t, members, 6)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetMember(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
addMembers(room, 6)
|
|
|
|
|
|
|
|
assert.NotNil(t, room.GetMember("@user_2:matrix.org"))
|
|
|
|
assert.NotNil(t, room.GetMember("@tulir:maunium.net"))
|
2018-11-14 00:11:40 +01:00
|
|
|
assert.Equal(t, "@tulir:maunium.net", room.GetSessionOwner())
|
2018-04-22 22:23:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetTitle_ExplicitName(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
addMembers(room, 4)
|
|
|
|
addName(room)
|
|
|
|
addCanonicalAlias(room)
|
|
|
|
addAliases(room)
|
|
|
|
assert.Equal(t, "Test room", room.GetTitle())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetTitle_CanonicalAlias(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
addMembers(room, 4)
|
|
|
|
addCanonicalAlias(room)
|
|
|
|
addAliases(room)
|
|
|
|
assert.Equal(t, "#foo:maunium.net", room.GetTitle())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetTitle_FirstAlias(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
addMembers(room, 2)
|
|
|
|
addAliases(room)
|
|
|
|
assert.Equal(t, "#bar:maunium.net", room.GetTitle())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetTitle_Members_Empty(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
addMembers(room, 1)
|
|
|
|
assert.Equal(t, "Empty room", room.GetTitle())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetTitle_Members_OneToOne(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
addMembers(room, 2)
|
|
|
|
assert.Equal(t, "User #2", room.GetTitle())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_GetTitle_Members_GroupChat(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
|
|
|
addMembers(room, 76)
|
|
|
|
assert.Contains(t, room.GetTitle(), " and 74 others")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoom_MarkRead(t *testing.T) {
|
|
|
|
room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net")
|
2018-05-16 19:09:09 +02:00
|
|
|
|
|
|
|
room.AddUnread("foo", true, false)
|
|
|
|
assert.Equal(t, 1, room.UnreadCount())
|
|
|
|
assert.False(t, room.Highlighted())
|
|
|
|
|
|
|
|
room.AddUnread("bar", true, false)
|
|
|
|
assert.Equal(t, 2, room.UnreadCount())
|
|
|
|
assert.False(t, room.Highlighted())
|
|
|
|
|
|
|
|
room.AddUnread("asd", false, true)
|
|
|
|
assert.Equal(t, 2, room.UnreadCount())
|
|
|
|
assert.True(t, room.Highlighted())
|
|
|
|
|
2018-05-16 20:50:58 +02:00
|
|
|
room.MarkRead("asd")
|
2018-05-16 19:09:09 +02:00
|
|
|
assert.Empty(t, room.UnreadMessages)
|
2018-04-22 22:23:36 +02:00
|
|
|
}
|