forked from matrix-org/sliding-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom_names_test.go
200 lines (190 loc) · 6.39 KB
/
room_names_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
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
package syncv3
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/matrix-org/sliding-sync/sync2"
"github.com/matrix-org/sliding-sync/sync3"
"github.com/matrix-org/sliding-sync/testutils"
"github.com/matrix-org/sliding-sync/testutils/m"
)
// Test that room names come through sanely. Additional testing to ensure we copy hero slices correctly.
func TestRoomNames(t *testing.T) {
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()
defer v3.close()
bob := "@TestRoomNames_bob:localhost"
// make 4 rooms, last room is most recent, and send A,B,C into each room
latestTimestamp := time.Now()
allRooms := []roomEvents{
{
roomID: "!TestRoomNames_dm:localhost",
name: "Bob",
events: append(createRoomState(t, alice, latestTimestamp), []json.RawMessage{
testutils.NewStateEvent(t, "m.room.member", bob, bob, map[string]interface{}{"membership": "join", "displayname": "Bob"}, testutils.WithTimestamp(latestTimestamp.Add(3*time.Second))),
}...),
},
{
roomID: "!TestRoomNames_named:localhost",
name: "My Room Name",
events: append(createRoomState(t, alice, latestTimestamp), []json.RawMessage{
testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{"name": "My Room Name"}, testutils.WithTimestamp(latestTimestamp.Add(2*time.Second))),
}...),
},
{
roomID: "!TestRoomNames_empty:localhost",
name: "Empty Room",
events: createRoomState(t, alice, latestTimestamp.Add(time.Second)),
},
{
roomID: "!TestRoomNames_dm_name_set_after_join:localhost",
name: "Bob",
state: append(createRoomState(t, alice, latestTimestamp), []json.RawMessage{
testutils.NewJoinEvent(t, bob, testutils.WithTimestamp(latestTimestamp)),
}...),
events: []json.RawMessage{
testutils.NewStateEvent(
t, "m.room.member", bob, bob, map[string]interface{}{"membership": "join", "displayname": "Bob"}, testutils.WithTimestamp(latestTimestamp),
testutils.WithUnsigned(map[string]interface{}{
"prev_content": map[string]interface{}{
"membership": "join",
},
})),
},
},
}
v2.addAccount(t, alice, aliceToken)
v2.queueResponse(alice, sync2.SyncResponse{
Rooms: sync2.SyncRoomsResponse{
Join: v2JoinTimeline(allRooms...),
},
})
checkRoomNames := func(sessionID string) {
t.Helper()
// do a sync, make sure room names are sensible
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, int64(len(allRooms) - 1)}, // all rooms
},
RoomSubscription: sync3.RoomSubscription{
TimelineLimit: int64(100),
},
}},
})
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(len(allRooms)), m.MatchV3Ops(
m.MatchV3SyncOpFn(func(op *sync3.ResponseOpRange) error {
if len(op.RoomIDs) != len(allRooms) {
return fmt.Errorf("want %d rooms, got %d", len(allRooms), len(op.RoomIDs))
}
for i := range allRooms {
err := allRooms[i].MatchRoom(op.RoomIDs[i],
res.Rooms[op.RoomIDs[i]],
m.MatchRoomName(allRooms[i].name),
)
if err != nil {
return err
}
}
return nil
}),
)))
}
checkRoomNames("a")
// restart the server and repeat the tests, should still be the same when reading from the database
v3.restart(t, v2, pqString)
checkRoomNames("b")
// now check that we can filter the rooms by name
checkRoomNameFilter := func(searchTerm string, wantRooms []roomEvents) {
t.Helper()
// do a sync, make sure room names are sensible
res := v3.mustDoV3Request(t, aliceToken, sync3.Request{
Lists: map[string]sync3.RequestList{
"a": {
Ranges: sync3.SliceRanges{
[2]int64{0, (int64(len(allRooms)) - 1)}, // all rooms
},
Filters: &sync3.RequestFilters{
RoomNameFilter: searchTerm,
},
}},
})
matchers := make(map[string][]m.RoomMatcher, len(wantRooms))
wantRoomIDs := make([]string, len(wantRooms))
for i := range wantRooms {
wantRoomIDs[i] = wantRooms[i].roomID
matchers[wantRooms[i].roomID] = []m.RoomMatcher{
m.MatchRoomName(wantRooms[i].name),
}
}
m.MatchResponse(t, res, m.MatchList("a", m.MatchV3Count(len(wantRooms)), m.MatchV3Ops(
m.MatchV3SyncOp(0, int64(len(wantRooms)-1), wantRoomIDs),
)), m.MatchRoomSubscriptions(matchers))
}
// case-insensitive matching
checkRoomNameFilter("my room name", []roomEvents{allRooms[1]})
// partial matching
checkRoomNameFilter("room na", []roomEvents{allRooms[1]})
// multiple matches
checkRoomNameFilter("bob", []roomEvents{allRooms[0], allRooms[3]})
}
// Tests that rooms have their names updated when events come in
func TestRoomNameUpdates(t *testing.T) {
rig := NewTestRig(t)
defer rig.Finish()
roomID := "!a:localhost"
rig.SetupV2RoomsForUser(t, alice, NoFlush, map[string]RoomDescriptor{
roomID: {},
})
aliceToken := rig.Token(alice)
res := rig.V3.mustDoV3Request(t, aliceToken, sync3.Request{
RoomSubscriptions: map[string]sync3.RoomSubscription{
roomID: {
TimelineLimit: 0,
},
},
})
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomID: {
m.MatchJoinCount(1),
m.MatchRoomName("Empty Room"),
},
}))
// Test hero calculation
rig.FlushEvent(t, alice, roomID, testutils.NewStateEvent(t, "m.room.member", bob, bob, map[string]interface{}{
"membership": "join",
"displayname": "Bob",
}))
res = rig.V3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{})
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomID: {
m.MatchJoinCount(2),
m.MatchRoomName("Bob"),
},
}))
// Test canonical alias
rig.FlushEvent(t, alice, roomID, testutils.NewStateEvent(t, "m.room.canonical_alias", "", alice, map[string]interface{}{
"alias": "#alias:example.com",
}))
res = rig.V3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{})
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomID: {
m.MatchRoomName("#alias:example.com"),
},
}))
// Test room name
rig.FlushEvent(t, alice, roomID, testutils.NewStateEvent(t, "m.room.name", "", alice, map[string]interface{}{
"name": "My Room Name",
}))
res = rig.V3.mustDoV3RequestWithPos(t, aliceToken, res.Pos, sync3.Request{})
m.MatchResponse(t, res, m.MatchRoomSubscriptionsStrict(map[string][]m.RoomMatcher{
roomID: {
m.MatchRoomName("My Room Name"),
},
}))
}