File: 12room-state.t

package info (click to toggle)
libnet-async-matrix-perl 0.19-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 264 kB
  • sloc: perl: 2,595; makefile: 2
file content (311 lines) | stat: -rw-r--r-- 7,894 bytes parent folder | download | duplicates (2)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Async::HTTP 0.02; # ->GET

use lib ".";
use t::Util;

use IO::Async::Loop;
use Net::Async::Matrix;
use Future;

my $matrix = Net::Async::Matrix->new(
   ua => my $ua = Test::Async::HTTP->new,
   server => "localserver.test",

   make_delay => sub { Future->new },
);

IO::Async::Loop->new->add( $matrix ); # for ->loop->new_future
matrix_login( $matrix, $ua );

use constant {
   ROOM_ID => "!room:localserver.test",
   USER_ID => '@sender:localserver.test',
};

sub _mksync
{
   my ( $event ) = @_;

   return (
      rooms => {
         join => {
            ROOM_ID() => {
               timeline => {
                  events => [
                     { %$event,
                        sender => USER_ID,
                     },
                  ]
               }
            }
         }
      }
   );
}

my $room = matrix_join_room( $matrix, $ua,
   {  type       => "m.room.member",
      room_id    => ROOM_ID,
      state_key  => USER_ID,
      content    => {
         membership => "join",
      },
      sender    => USER_ID,
   },
   {  type      => "m.room.name",
      content   => { name => "Initial name" },
      room_id   => ROOM_ID,
      state_key => "",
      sender    => USER_ID,
   },
   {  type      => "m.room.aliases",
      content   => { aliases => [ '#room1:localserver.test' ] },
      room_id   => ROOM_ID,
      state_key => "localserver.test",
      sender    => USER_ID,
   },
   {  type      => "m.room.join_rules",
      content   => { join_rule => "private" },
      room_id   => ROOM_ID,
      state_key => "",
      sender    => USER_ID,
   },
   {  type      => "m.room.topic",
      content   => { topic => "Initial topic" },
      room_id   => ROOM_ID,
      state_key => "",
      sender    => USER_ID,
   },
   {  type      => "m.room.power_levels",
      content   => {
         users => {
            USER_ID() => 100,
         },
         users_default => 50,
      },
      room_id   => ROOM_ID,
      state_key => "",
      sender    => USER_ID,
   },
);

my @state_changes;
my @member_changes;

$room->configure(
   on_state_changed => sub {
      shift;
      push @state_changes, [ @_ ];
   },
   on_membership => sub {
      shift;
      push @member_changes, [ @_ ];
   },
);

# room name
{
   is( $room->name, "Initial name", '$room->name initially' );

   send_sync( $ua, _mksync
      {
         type => "m.room.name",
         state_key => "",
         content => {
            name => "A new name",
         },
      },
   );

   is( $room->name, "A new name", '$room->name after event' );

   ok( my $ch = shift @state_changes, 'on_state_changed invoked' );
   my ( $member, $event, %changes ) = @$ch;

   is( $member->user->user_id, USER_ID, '[0] is $member' );
   is( $event->{type}, "m.room.name", '[1] is $event' );
   is_deeply( \%changes,
      { name => [ "Initial name", "A new name" ] },
      '[2..] is %changes' );
}

# aliases
{
   is_deeply( [ $room->aliases ], [ '#room1:localserver.test' ],
      '$room->aliases initially' );

   send_sync( $ua, _mksync
      {
         type => "m.room.aliases",
         state_key => "localserver.test",
         content => {
            aliases => [ '#room1:localserver.test', '#room2:localserver.test' ],
         },
      },
   );

   is_deeply( [ $room->aliases ], [ '#room1:localserver.test', '#room2:localserver.test' ],
      '$room->aliases after event' );

   ok( my $ch = shift @state_changes, 'on_state_changed invoked' );
   my ( $member, $event, %changes ) = @$ch;

   is( $member->user->user_id, USER_ID, '[0] is $member' );
   is( $event->{type}, "m.room.aliases", '[1] is $event' );
   is_deeply( \%changes,
      { aliases => [
            [ '#room1:localserver.test' ], [ '#room1:localserver.test', '#room2:localserver.test' ], []
         ]
      },
      '[2..] is %changes' );
}

# join rule
{
   is( $room->join_rule, "private", '$room->join_rule initially' );

   send_sync( $ua, _mksync
      {
         type => "m.room.join_rules", # sic
         state_key => "",
         content => {
            join_rule => "public",
         },
      },
   );

   is( $room->join_rule, "public", '$room->join_rule after event' );

   ok( my $ch = shift @state_changes, 'on_state_changed invoked' );
   my ( $member, $event, %changes ) = @$ch;

   is( $member->user->user_id, USER_ID, '[0] is $member' );
   is( $event->{type}, "m.room.join_rules", '[1] is $event' );
   is_deeply( \%changes,
      { join_rule => [ "private", "public" ] },
      '[2..] is %changes' );
}

# topic
{
   is( $room->topic, "Initial topic", '$room->topic initially' );

   send_sync( $ua, _mksync
      {
         type => "m.room.topic",
         state_key => "",
         content => {
            topic => "A new topic",
         },
      },
   );

   is( $room->topic, "A new topic", '$room->topic after event' );

   ok( my $ch = shift @state_changes, 'on_state_changed invoked' );
   my ( $member, $event, %changes ) = @$ch;

   is( $member->user->user_id, USER_ID, '[0] is $member' );
   is( $event->{type}, "m.room.topic", '[1] is $event' );
   is_deeply( \%changes,
      { topic => [ "Initial topic", "A new topic" ] },
      '[2..] is %changes' );
}

# members - joining
{
   is_deeply( [ map { $_->user->user_id } $room->members ], [ USER_ID ],
      'user ID of $room->members initially' );

   send_sync( $ua, _mksync
      {
         type => "m.room.member",
         state_key => '@new-user:localserver.test',
         content => {
            membership => "join",
         },
      },
   );

   is_deeply( [ sort map { $_->user->user_id } $room->members ],
      [ '@new-user:localserver.test', USER_ID ],
      'user ID of $room->members after event' );

   ok( my $ch = shift @member_changes, 'on_membership invoked' );
   my ( $member, $event, $subject, %changes ) = @$ch;

   is( $member->user->user_id, USER_ID, '[0] is $member' );
   is( $event->{type}, "m.room.member", '[1] is $event' );
   is( $subject->user->user_id, '@new-user:localserver.test', '[2] is $subject' );
   is_deeply( \%changes,
      { membership => [ undef, "join" ] },
      '[3..] is %changes' );
}

# members - changing name
{
   send_sync( $ua, _mksync
      {
         type => "m.room.member",
         state_key => '@new-user:localserver.test',
         content => {
            membership => "join",
            displayname => "Your Name Here",
         },
      },
   );

   is_deeply( [ grep { defined } map { $_->displayname } $room->members ],
      [ "Your Name Here" ],
      'displayname of $room->members after event' );

   ok( my $ch = shift @member_changes, 'on_membership invoked' );
   my ( $member, $event, $subject, %changes ) = @$ch;

   is( $member->user->user_id, USER_ID, '[0] is $member' );
   is( $event->{type}, "m.room.member", '[1] is $event' );
   is( $subject->user->user_id, '@new-user:localserver.test', '[2] is $subject' );
   is_deeply( \%changes,
      {
         displayname => [ undef, "Your Name Here" ],
      },
      '[3..] is %changes' );
}

# member levels
{
   is( $room->member_level( USER_ID ), 100, 'member_level initially' );

   send_sync( $ua, _mksync
      {
         type      => "m.room.power_levels",
         content   => {
            users => {
               USER_ID() => 80,
            },
            users_default => 50,
         },
         state_key => "",
      }
   );

   is( $room->member_level( USER_ID ), 80, 'member_level after event' );

   ok( my $ch = shift @member_changes, 'on_membership invoked' );
   my ( $member, $event, $subject, %changes ) = @$ch;

   is( $member->user->user_id, USER_ID, '[0] is $member' );
   is( $event->{type}, "m.room.power_levels", '[1] is $event' );
   is( $subject->user->user_id, USER_ID, '[2] is $subject' );
   is_deeply( \%changes,
      { level => [ 100, 80 ] },
      '[3..] is %changes' );
}

done_testing;