<commit_before>use List::Util qw( first );
use List::UtilsBy qw( partition_by );

my $creator_fixture = local_user_fixture(
   # Some of these tests depend on the user having a displayname
   displayname => "My name here",
   avatar_url  => "mxc://foo/bar",
   with_events => 1,
);

my $remote_user_fixture = remote_user_fixture(
   displayname => "My remote name here",
   avatar_url  => "mxc://foo/remote",
   with_events => 1,
);

my $room_fixture = fixture(
   requires => [ $creator_fixture, room_alias_name_fixture() ],

   setup => sub {
      my ( $user, $room_alias_name ) = @_;

      matrix_create_room( $user,
         room_alias_name => $room_alias_name,
      );
   },
);

test "Remote users can join room by alias",
   requires => [ $remote_user_fixture, $room_fixture,
                 qw( can_join_room_by_alias can_get_room_membership )],

   proves => [qw( can_join_remote_room_by_alias )],

   do => sub {
      my ( $user, $room_id, $room_alias ) = @_;

      flush_events_for( $user )->then( sub {
         do_request_json_for( $user,
            method => "POST",
            uri    => "/r0/join/$room_alias",

            content => {},
         );
      });
   },

   check => sub {
      my ( $user, $room_id, $room_alias ) = @_;

      matrix_get_room_state( $user, $room_id,
         type      => "m.room.member",
         state_key => $user->user_id,
      )->then( sub {
         my ( $body ) = @_;

         $body->{membership} eq "join" or
            die "Expected membership to be 'join'";

         assert_json_keys( $body, qw( displayname avatar_url ) );

         Future->done(1);
      });
   };

test "New room members see their own join event",
   requires => [ $remote_user_fixture, $room_fixture,
                 qw( can_join_remote_room_by_alias )],

   do => sub {
      my ( $user, $room_id, $room_alias ) = @_;

      await_sync_timeline_contains( $user, $room_id, check => sub {
         my ( $event ) = @_;
         return unless $event->{type} eq "m.room.member";

         assert_json_keys( $event, qw( type sender ));
         return unless $event->{sender} eq $user->user_id;

         assert_json_keys( my $content = $event->{content}, qw( membership displayname avatar_url ));

         $content->{membership} eq "join" or
            die "Expected user membership as 'join'";

         return 1;
      });
   };

test "New room members see existing members' presence in room initialSync",
   deprecated_endpoints => 1,
   requires => [ $creator_fixture, $remote_user_fixture, $room_fixture,
                 qw( can_join_remote_room_by_alias can_room_initial_sync )],

   do => sub {
      my ( $first_user, $user, $room_id, $room_alias ) = @_;

      ( repeat_until_true {
         matrix_initialsync_room( $user, $room_id )->then( sub {
            my ( $body ) = @_;

            my %presence = map { $_->{content}{user_id} => $_ } @{ $body->{presence} };

            $presence{$first_user->user_id} or
               return Future->done( undef );  # try again

            return Future->done( \%presence );
         })
      })->then( sub {
         my ( $presencemap ) = @_;

         assert_json_keys( $presencemap->{ $first_user->user_id },
            qw( type content ));
         assert_json_keys( $presencemap->{ $first_user->user_id }{content},
            qw( presence last_active_ago ));

         Future->done(1);
      });
   };

test "Existing members see new members' join events",
   requires => [ $creator_fixture, $remote_user_fixture, $room_fixture,
                 qw( can_join_remote_room_by_alias )],

   do => sub {
      my ( $first_user, $user, $room_id, $room_alias ) = @_;

      await_sync_timeline_contains( $first_user, $room_id, check => sub {
         my ( $event ) = @_;
         return unless $event->{type} eq "m.room.member";
         assert_json_keys( $event, qw( type sender ));
         return unless $event->{sender} eq $user->user_id;<commit_msg>Deflake "presence in initialSync" test (#965)<commit_after>use List::Util qw( first );
use List::UtilsBy qw( partition_by );

my $creator_fixture = local_user_fixture(
   # Some of these tests depend on the user having a displayname
   displayname => "My name here",
   avatar_url  => "mxc://foo/bar",
   with_events => 1,
);

my $remote_user_fixture = remote_user_fixture(
   displayname => "My remote name here",
   avatar_url  => "mxc://foo/remote",
   with_events => 1,
);

my $room_fixture = fixture(
   requires => [ $creator_fixture, room_alias_name_fixture() ],

   setup => sub {
      my ( $user, $room_alias_name ) = @_;

      matrix_create_room( $user,
         room_alias_name => $room_alias_name,
      );
   },
);

test "Remote users can join room by alias",
   requires => [ $remote_user_fixture, $room_fixture,
                 qw( can_join_room_by_alias can_get_room_membership )],

   proves => [qw( can_join_remote_room_by_alias )],

   do => sub {
      my ( $user, $room_id, $room_alias ) = @_;

      flush_events_for( $user )->then( sub {
         do_request_json_for( $user,
            method => "POST",
            uri    => "/r0/join/$room_alias",

            content => {},
         );
      });
   },

   check => sub {
      my ( $user, $room_id, $room_alias ) = @_;

      matrix_get_room_state( $user, $room_id,
         type      => "m.room.member",
         state_key => $user->user_id,
      )->then( sub {
         my ( $body ) = @_;

         $body->{membership} eq "join" or
            die "Expected membership to be 'join'";

         assert_json_keys( $body, qw( displayname avatar_url ) );

         Future->done(1);
      });
   };

test "New room members see their own join event",
   requires => [ $remote_user_fixture, $room_fixture,
                 qw( can_join_remote_room_by_alias )],

   do => sub {
      my ( $user, $room_id, $room_alias ) = @_;

      await_sync_timeline_contains( $user, $room_id, check => sub {
         my ( $event ) = @_;
         return unless $event->{type} eq "m.room.member";

         assert_json_keys( $event, qw( type sender ));
         return unless $event->{sender} eq $user->user_id;

         assert_json_keys( my $content = $event->{content}, qw( membership displayname avatar_url ));

         $content->{membership} eq "join" or
            die "Expected user membership as 'join'";

         return 1;
      });
   };

test "New room members see existing members' presence in room initialSync",
   deprecated_endpoints => 1,
   requires => [ $creator_fixture, $remote_user_fixture, $room_fixture,
                 qw( can_join_remote_room_by_alias can_room_initial_sync )],

   do => sub {
      my ( $first_user, $user, $room_id, $room_alias ) = @_;

      ( repeat_until_true {
         matrix_initialsync_room( $user, $room_id )->then( sub {
            my ( $body ) = @_;

            log_if_fail "initialSync result", $body;

            my %presence = map { $_->{content}{user_id} => $_ } @{ $body->{presence} };

            if( not $presence{$first_user->user_id} ) {
               log_if_fail "No presence for user " . $first_user->user_id . ": retrying";
               return Future->done( undef );  # try again
            }

            if( $presence{$first_user->user_id}{content}{presence} ne 'online' ) {
               log_if_fail "User " . $first_user->user_id . "not yet online: retrying";
               return Future->done( undef );  # try again
            }

            return Future->done( \%presence );
         })
      })->then( sub {
         my ( $presencemap ) = @_;

         log_if_fail "presence map: ", $presencemap;

         assert_json_keys( $presencemap->{ $first_user->user_id },
            qw( type content ));
         assert_json_keys( $presencemap->{ $first_user->user_id }{content},
            qw( presence last_active_ago ));

         Future->done(1);
      });
   };

test "Existing members see new members' join events",
   requires => [ $creator_fixture, $remote_user_fixture, $room_fixture,
                 qw( can_join_remote_room_by_alias )],

   do => sub {
      my ( $first_user, $user, $room_id, $room_alias ) = @_;

      await_sync_timeline_contains( $first_user, $room_id, check => sub {
         my ( $event ) = @_;
         return unless $event->{type} eq "m.room.member";
         assert_json_keys( $event, qw( type sender ));
         return unless $event->{sender} eq $user->user_id;
{'fraction_non_alphanumeric': 0.13936867182846932, 'fraction_numerical': 0.0020250148898153662, 'mean_word_length': 2.3854838709677417, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2743611', 'n_tokens_mistral': 2811, 'n_tokens_neox': 2739, 'n_words': 630}