< Summary

Class:DCL.Social.Friends.NewFriendRequestsApiBridgeMock
Assembly:FriendsController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FriendsController/NewFriendRequestsApiBridgeMock.cs
Covered lines:0
Uncovered lines:87
Coverable lines:87
Total lines:296
Line coverage:0% (0 of 87)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NewFriendRequestsApiBridgeMock(...)0%2100%
add_OnInitialized(...)0%2100%
remove_OnInitialized(...)0%2100%
add_OnFriendNotFound(...)0%2100%
remove_OnFriendNotFound(...)0%2100%
add_OnFriendsAdded(...)0%2100%
remove_OnFriendsAdded(...)0%2100%
add_OnFriendWithDirectMessagesAdded(...)0%2100%
remove_OnFriendWithDirectMessagesAdded(...)0%2100%
add_OnUserPresenceUpdated(...)0%2100%
remove_OnUserPresenceUpdated(...)0%2100%
add_OnTotalFriendRequestCountUpdated(...)0%2100%
remove_OnTotalFriendRequestCountUpdated(...)0%2100%
add_OnTotalFriendCountUpdated(...)0%2100%
remove_OnTotalFriendCountUpdated(...)0%2100%
add_OnFriendRequestsAdded(...)0%2100%
remove_OnFriendRequestsAdded(...)0%2100%
RejectFriendship(...)0%2100%
RemoveFriend(...)0%2100%
GetFriends(...)0%2100%
GetFriends(...)0%2100%
GetFriendRequests(...)0%2100%
GetFriendRequestsAsync()0%90900%
GetFriendsWithDirectMessages(...)0%2100%
RequestFriendship(...)0%2100%
RequestFriendshipAsync()0%20400%
CancelRequestAsync()0%20400%
CancelRequestByUserIdAsync(...)0%2100%
CancelRequestByUserId(...)0%2100%
AcceptFriendship(...)0%2100%
Dispose()0%2100%
AddFriendRequestByUserInputAsync()0%72800%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/FriendsController/NewFriendRequestsApiBridgeMock.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Threading;
 4using Cysharp.Threading.Tasks;
 5using DCl.Social.Friends;
 6using UnityEngine;
 7using Random = UnityEngine.Random;
 8
 9namespace DCL.Social.Friends
 10{
 11    public class NewFriendRequestsApiBridgeMock : IFriendsApiBridge
 12    {
 13        private readonly WebInterfaceFriendsApiBridge apiBridge;
 14        private readonly IUserProfileBridge userProfileBridge;
 015        private readonly CancellationTokenSource addFriendRequestByUserInputCancellationToken = new CancellationTokenSou
 016        private readonly Dictionary<string, FriendRequestPayload> friendRequests = new ();
 17
 18        public event Action<FriendshipInitializationMessage> OnInitialized
 19        {
 020            add => apiBridge.OnInitialized += value;
 021            remove => apiBridge.OnInitialized -= value;
 22        }
 23
 24        public event Action<string> OnFriendNotFound
 25        {
 026            add => apiBridge.OnFriendNotFound += value;
 027            remove => apiBridge.OnFriendNotFound -= value;
 28        }
 29
 30        public event Action<AddFriendsPayload> OnFriendsAdded
 31        {
 032            add => apiBridge.OnFriendsAdded += value;
 033            remove => apiBridge.OnFriendsAdded -= value;
 34        }
 35
 36        public event Action<AddFriendsWithDirectMessagesPayload> OnFriendWithDirectMessagesAdded
 37        {
 038            add => apiBridge.OnFriendWithDirectMessagesAdded += value;
 039            remove => apiBridge.OnFriendWithDirectMessagesAdded -= value;
 40        }
 41
 42        public event Action<UserStatus> OnUserPresenceUpdated
 43        {
 044            add => apiBridge.OnUserPresenceUpdated += value;
 045            remove => apiBridge.OnUserPresenceUpdated -= value;
 46        }
 47
 48        public event Action<FriendshipUpdateStatusMessage> OnFriendshipStatusUpdated;
 49
 50        public event Action<UpdateTotalFriendRequestsPayload> OnTotalFriendRequestCountUpdated
 51        {
 052            add => apiBridge.OnTotalFriendRequestCountUpdated += value;
 053            remove => apiBridge.OnTotalFriendRequestCountUpdated -= value;
 54        }
 55
 56        public event Action<UpdateTotalFriendsPayload> OnTotalFriendCountUpdated
 57        {
 058            add => apiBridge.OnTotalFriendCountUpdated += value;
 059            remove => apiBridge.OnTotalFriendCountUpdated -= value;
 60        }
 61
 62        public event Action<FriendRequestPayload> OnFriendRequestAdded;
 63
 64        public event Action<AddFriendRequestsPayload> OnFriendRequestsAdded
 65        {
 066            add => apiBridge.OnFriendRequestsAdded += value;
 067            remove => apiBridge.OnFriendRequestsAdded -= value;
 68        }
 69
 070        public NewFriendRequestsApiBridgeMock(WebInterfaceFriendsApiBridge apiBridge,
 71            IUserProfileBridge userProfileBridge)
 72        {
 073            this.apiBridge = apiBridge;
 074            this.userProfileBridge = userProfileBridge;
 75
 076            apiBridge.OnFriendshipStatusUpdated += message => OnFriendshipStatusUpdated?.Invoke(message);
 077            apiBridge.OnFriendRequestAdded += message => OnFriendRequestAdded?.Invoke(message);
 78
 079            AddFriendRequestByUserInputAsync(addFriendRequestByUserInputCancellationToken.Token).Forget();
 080        }
 81
 82        public void RejectFriendship(string userId)
 83        {
 084            apiBridge.RejectFriendship(userId);
 085        }
 86
 87        public void RemoveFriend(string userId)
 88        {
 089            apiBridge.RemoveFriend(userId);
 090        }
 91
 92        public void GetFriends(int limit, int skip)
 93        {
 094            apiBridge.GetFriends(limit, skip);
 095        }
 96
 97        public void GetFriends(string usernameOrId, int limit)
 98        {
 099            apiBridge.GetFriends(usernameOrId, limit);
 0100        }
 101
 102        public void GetFriendRequests(int sentLimit, int sentSkip, int receivedLimit, int receivedSkip) =>
 0103            apiBridge.GetFriendRequests(sentLimit, sentSkip, receivedLimit, receivedSkip);
 104
 105        public async UniTask<AddFriendRequestsV2Payload> GetFriendRequestsAsync(int sentLimit, int sentSkip, int receive
 106        {
 0107            await UniTask.Delay(Random.Range(100, 1000));
 108
 109            // FAKE RECEIVED REQUESTS
 0110            int amountOfReceivedRequests = Random.Range(1, 5);
 0111            List<FriendRequestPayload> requestedFromList = new List<FriendRequestPayload>();
 112
 0113            for (var i = 0; i < amountOfReceivedRequests; i++)
 114            {
 0115                var fakeUserId = $"fake_from_user_{i + 1}";
 116
 0117                userProfileBridge.AddUserProfileToCatalog(new UserProfileModel
 118                {
 119                    userId = fakeUserId,
 120                    name = $"fake from user {i + 1}",
 121                    snapshots = new UserProfileModel.Snapshots { face256 = $"https://picsum.photos/50?{i}" }
 122                });
 123
 0124                var friendRequestId = Guid.NewGuid().ToString("N");
 125
 0126                var friendRequest = new FriendRequestPayload
 127                {
 128                    from = fakeUserId,
 129                    to = userProfileBridge.GetOwn().userId,
 130                    friendRequestId = friendRequestId,
 131                    messageBody = Random.Range(0, 2) == 0 ? $"Test message from {fakeUserId}..." : string.Empty,
 132                    timestamp = DateTimeOffset.UtcNow.AddDays(-i).ToUnixTimeMilliseconds()
 133                };
 134
 0135                friendRequests[friendRequestId] = friendRequest;
 136
 0137                requestedFromList.Add(friendRequest);
 138            }
 139
 140            // FAKE SENT REQUESTS
 0141            int amountOfSentRequests = Random.Range(1, 5);
 0142            List<FriendRequestPayload> requestedToList = new List<FriendRequestPayload>();
 143
 0144            for (var i = 0; i < amountOfSentRequests; i++)
 145            {
 0146                var fakeUserId = $"fake_to_user_{i + 1}";
 147
 0148                userProfileBridge.AddUserProfileToCatalog(new UserProfileModel
 149                {
 150                    userId = fakeUserId,
 151                    name = $"fake to user {i + 1}",
 152                    snapshots = new UserProfileModel.Snapshots { face256 = $"https://picsum.photos/50?{i + 100}" }
 153                });
 154
 0155                var friendRequestId = Guid.NewGuid().ToString("N");
 156
 0157                var friendRequest = new FriendRequestPayload
 158                {
 159                    from = userProfileBridge.GetOwn().userId,
 160                    to = fakeUserId,
 161                    friendRequestId = friendRequestId,
 162                    messageBody = Random.Range(0, 2) == 0 ? $"Test message to {fakeUserId}..." : string.Empty,
 163                    timestamp = DateTimeOffset.UtcNow.AddDays(-i).ToUnixTimeMilliseconds()
 164                };
 165
 0166                friendRequests[friendRequestId] = friendRequest;
 167
 0168                requestedToList.Add(friendRequest);
 169            }
 170
 0171            var response = new AddFriendRequestsV2Payload
 172            {
 173                requestedFrom = requestedFromList.ToArray(),
 174                requestedTo = requestedToList.ToArray(),
 175                totalReceivedFriendRequests = amountOfReceivedRequests,
 176                totalSentFriendRequests = amountOfSentRequests
 177            };
 178
 0179            return response;
 0180        }
 181
 182        public void GetFriendsWithDirectMessages(string usernameOrId, int limit, int skip)
 183        {
 0184            apiBridge.GetFriendsWithDirectMessages(usernameOrId, limit, skip);
 0185        }
 186
 187        public void RequestFriendship(string friendUserId) =>
 0188            apiBridge.RequestFriendship(friendUserId);
 189
 190        public async UniTask<RequestFriendshipConfirmationPayload> RequestFriendshipAsync(string userId, string messageB
 191        {
 0192            await UniTask.Delay(Random.Range(100, 16000));
 193
 194            // TODO: add user profile to catalog if necessary
 195
 196            // if (Random.Range(0, 2) == 0)
 197            //     throw new FriendshipException(FriendRequestErrorCodes.Unknown);
 198
 0199            var friendRequestId = Guid.NewGuid().ToString("N");
 200
 0201            var friendRequest = new FriendRequestPayload
 202            {
 203                from = userProfileBridge.GetOwn().userId,
 204                friendRequestId = friendRequestId,
 205                messageBody = messageBody,
 206                timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
 207                to = userId
 208            };
 209
 0210            friendRequests[friendRequestId] = friendRequest;
 211
 0212            var response = new RequestFriendshipConfirmationPayload
 213            {
 214                friendRequest = friendRequest,
 215                messageId = Guid.NewGuid().ToString("N")
 216            };
 217
 0218            OnFriendshipStatusUpdated?.Invoke(new FriendshipUpdateStatusMessage
 219            {
 220                action = FriendshipAction.REQUESTED_TO,
 221                userId = userId
 222            });
 223
 0224            return response;
 0225        }
 226
 227        public async UniTask<CancelFriendshipConfirmationPayload> CancelRequestAsync(string friendRequestId)
 228        {
 0229            await UniTask.Delay(Random.Range(100, 16000));
 230
 231            // if (Random.Range(0, 2) == 0)
 232            //     throw new FriendshipException(FriendRequestErrorCodes.Unknown);
 233
 0234            var response = new CancelFriendshipConfirmationPayload
 235            {
 236                messageId = Guid.NewGuid().ToString("N"),
 237                friendRequest = friendRequests[friendRequestId],
 238            };
 239
 0240            OnFriendshipStatusUpdated?.Invoke(new FriendshipUpdateStatusMessage
 241            {
 242                action = FriendshipAction.CANCELLED,
 243                userId = friendRequests[friendRequestId].to
 244            });
 245
 0246            return response;
 0247        }
 248
 249        public UniTask CancelRequestByUserIdAsync(string userId) =>
 0250            apiBridge.CancelRequestByUserIdAsync(userId);
 251
 252        public void CancelRequestByUserId(string userId) =>
 0253            apiBridge.CancelRequestByUserId(userId);
 254
 255        public void AcceptFriendship(string userId)
 256        {
 0257            apiBridge.AcceptFriendship(userId);
 0258        }
 259
 260        public void Dispose()
 261        {
 0262            addFriendRequestByUserInputCancellationToken.Cancel();
 0263            addFriendRequestByUserInputCancellationToken.Dispose();
 0264        }
 265
 266        private async UniTaskVoid AddFriendRequestByUserInputAsync(CancellationToken ct = default)
 267        {
 0268            while (!ct.IsCancellationRequested)
 269            {
 0270                await UniTask.NextFrame(ct);
 271
 0272                if (Input.GetKeyDown(KeyCode.R))
 273                {
 0274                    long currentTicks = DateTimeOffset.UtcNow.Ticks;
 0275                    string fakeUserId = $"new_user_{currentTicks.ToString().Substring(currentTicks.ToString().Length - 5
 276
 0277                    userProfileBridge.AddUserProfileToCatalog(new UserProfileModel
 278                    {
 279                        userId = fakeUserId,
 280                        name = fakeUserId.Replace("_", " "),
 281                        snapshots = new UserProfileModel.Snapshots { face256 = $"https://picsum.photos/50?{DateTimeOffse
 282                    });
 283
 0284                    OnFriendRequestAdded?.Invoke(new FriendRequestPayload
 285                    {
 286                        friendRequestId = Guid.NewGuid().ToString("N"),
 287                        from = fakeUserId,
 288                        to = userProfileBridge.GetOwn().userId,
 289                        messageBody = Random.Range(0, 2) == 0 ? $"Test message from {fakeUserId}..." : string.Empty,
 290                        timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
 291                    });
 292                }
 293            }
 0294        }
 295    }
 296}

Methods/Properties

NewFriendRequestsApiBridgeMock(DCL.Social.Friends.WebInterfaceFriendsApiBridge, IUserProfileBridge)
add_OnInitialized(System.Action[FriendshipInitializationMessage])
remove_OnInitialized(System.Action[FriendshipInitializationMessage])
add_OnFriendNotFound(System.Action[String])
remove_OnFriendNotFound(System.Action[String])
add_OnFriendsAdded(System.Action[AddFriendsPayload])
remove_OnFriendsAdded(System.Action[AddFriendsPayload])
add_OnFriendWithDirectMessagesAdded(System.Action[AddFriendsWithDirectMessagesPayload])
remove_OnFriendWithDirectMessagesAdded(System.Action[AddFriendsWithDirectMessagesPayload])
add_OnUserPresenceUpdated(System.Action[UserStatus])
remove_OnUserPresenceUpdated(System.Action[UserStatus])
add_OnTotalFriendRequestCountUpdated(System.Action[UpdateTotalFriendRequestsPayload])
remove_OnTotalFriendRequestCountUpdated(System.Action[UpdateTotalFriendRequestsPayload])
add_OnTotalFriendCountUpdated(System.Action[UpdateTotalFriendsPayload])
remove_OnTotalFriendCountUpdated(System.Action[UpdateTotalFriendsPayload])
add_OnFriendRequestsAdded(System.Action[AddFriendRequestsPayload])
remove_OnFriendRequestsAdded(System.Action[AddFriendRequestsPayload])
RejectFriendship(System.String)
RemoveFriend(System.String)
GetFriends(System.Int32, System.Int32)
GetFriends(System.String, System.Int32)
GetFriendRequests(System.Int32, System.Int32, System.Int32, System.Int32)
GetFriendRequestsAsync()
GetFriendsWithDirectMessages(System.String, System.Int32, System.Int32)
RequestFriendship(System.String)
RequestFriendshipAsync()
CancelRequestAsync()
CancelRequestByUserIdAsync(System.String)
CancelRequestByUserId(System.String)
AcceptFriendship(System.String)
Dispose()
AddFriendRequestByUserInputAsync()