From ad540e268d207eb4b18447018e1a5d67fac9d45e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 22 Apr 2018 23:23:36 +0300 Subject: Add tests for Room --- matrix/rooms/room_test.go | 195 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) (limited to 'matrix/rooms') diff --git a/matrix/rooms/room_test.go b/matrix/rooms/room_test.go index c3a4cfe..2f3f402 100644 --- a/matrix/rooms/room_test.go +++ b/matrix/rooms/room_test.go @@ -15,3 +15,198 @@ // along with this program. If not, see . package rooms_test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "maunium.net/go/gomatrix" + "maunium.net/go/gomuks/matrix/rooms" +) + +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()) + assert.Nil(t, room.GetSessionOwner()) + */ +} + +func TestRoom_GetCanonicalAlias(t *testing.T) { + room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net") + room.UpdateState(&gomatrix.Event{ + Type: "m.room.canonical_alias", + Content: map[string]interface{}{ + "alias": "#foo:maunium.net", + }, + }) + assert.Equal(t, "#foo:maunium.net", room.GetCanonicalAlias()) +} + +func TestRoom_GetTopic(t *testing.T) { + room := rooms.NewRoom("!test:maunium.net", "@tulir:maunium.net") + room.UpdateState(&gomatrix.Event{ + Type: "m.room.topic", + Content: map[string]interface{}{ + "topic": "test topic", + }, + }) + assert.Equal(t, "test topic", room.GetTopic()) +} + + +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) { + room.UpdateState(&gomatrix.Event{ + Type: "m.room.name", + Content: map[string]interface{}{ + "name": "Test room", + }, + }) +} + +func addCanonicalAlias(room *rooms.Room) { + room.UpdateState(&gomatrix.Event{ + Type: "m.room.canonical_alias", + Content: map[string]interface{}{ + "alias": "#foo:maunium.net", + }, + }) +} + +func addAliases(room *rooms.Room) { + server1 := "maunium.net" + room.UpdateState(&gomatrix.Event{ + Type: "m.room.aliases", + StateKey: &server1, + Content: map[string]interface{}{ + "aliases": []interface{}{"#bar:maunium.net", "#test:maunium.net", "#foo:maunium.net"}, + }, + }) + + server2 := "matrix.org" + room.UpdateState(&gomatrix.Event{ + Type: "m.room.aliases", + StateKey: &server2, + Content: map[string]interface{}{ + "aliases": []interface{}{"#foo:matrix.org", "#test:matrix.org"}, + }, + }) +} + +func addMembers(room *rooms.Room, count int) { + user1 := "@tulir:maunium.net" + room.UpdateState(&gomatrix.Event{ + Type: "m.room.member", + StateKey: &user1, + Content: map[string]interface{}{ + "displayname": "tulir", + "membership": "join", + }, + }) + + for i := 1; i < count; i++ { + userN := fmt.Sprintf("@user_%d:matrix.org", i+1) + content := map[string]interface{}{ + "membership": "join", + } + if i % 2 == 1 { + content["displayname"] = fmt.Sprintf("User #%d", i+1) + } + if i % 5 == 0 { + content["membership"] = "invite" + } + room.UpdateState(&gomatrix.Event{ + Type: "m.room.member", + StateKey: &userN, + Content: content, + }) + } +} + +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")) + assert.Equal(t, room.GetMember("@tulir:maunium.net"), room.GetSessionOwner()) +} + +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") + room.UnreadMessages = 123 + room.Highlighted = true + room.HasNewMessages = true + room.MarkRead() + assert.Zero(t, room.UnreadMessages) + assert.False(t, room.Highlighted) + assert.False(t, room.HasNewMessages) +} -- cgit v1.2.3