public async void OnNewFollowersDetected_Raised_When_NewFollower() { var usersFollowsResponseFirstUserJson = JMock.Of <GetUsersFollowsResponse>(o => o.Follows == new[] { Mock.Of <Follow>(f => f.FromUserId == "FromFirstUserId") } ); var mockHandler = TwitchLibMock.HttpCallHandler(("https://api.twitch.tv/helix/users/follows", usersFollowsResponseFirstUserJson)); _api = TwitchLibMock.TwitchApi(mockHandler); var eventExecuteCount = 0; _followerService = new FollowerService(_api); _followerService.SetChannelsById(Utils.CreateListWithEmptyString()); _followerService.OnNewFollowersDetected += (sender, e) => eventExecuteCount++; await _followerService.UpdateLatestFollowersAsync(); var usersFollowsResponseSecondUserJson = JMock.Of <GetUsersFollowsResponse>(o => o.Follows == new[] { Mock.Of <Follow>(f => f.FromUserId == "FromSecondUserId") } ); TwitchLibMock.ResetHttpCallHandlerResponses(mockHandler, ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseSecondUserJson)); await _followerService.UpdateLatestFollowersAsync(); Assert.Equal(2, eventExecuteCount); }
public async void OnNewFollowersDetected_NotRaised_When_NoNewFollowers() { var usersFollowsResponseJson = JMock.Of <GetUsersFollowsResponse>(o => o.Follows == new[] { Mock.Of <Follow>(f => f.FromUserId == "FromUserId") } ); _api = TwitchLibMock.TwitchApi( ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseJson) ); var eventExcecutCount = 0; _followerService = new FollowerService(_api); _followerService.SetChannelsById(Utils.CreateListWithEmptyString()); _followerService.OnNewFollowersDetected += (sender, e) => eventExcecutCount++; await _followerService.UpdateLatestFollowersAsync(); await _followerService.UpdateLatestFollowersAsync(); Assert.Equal(1, eventExcecutCount); }
public void Start_Throws_InvalidOperationException_When_ServiceAlreadyStarted() { _followerService = new FollowerService(_api); _followerService.SetChannelsById(Utils.CreateListWithEmptyString()); _followerService.Start(); AssertException.Throws <InvalidOperationException>(AlreadyStartedExceptionMessage, () => _followerService.Start()); }
public void OnChannelsSet_Raised_When_ChannelsSet() { var eventRaised = false; _followerService = new FollowerService(_api); _followerService.OnChannelsSet += (sender, e) => eventRaised = true; _followerService.SetChannelsById(Utils.CreateListWithEmptyString()); Assert.True(eventRaised); }
public BroadcasterInfo(DrakeBot bot, string id, string accessToken) { ID = id; AccessToken = accessToken; follows = new FollowerService(bot.Service); follows.SetChannelsById(new List <string>(new [] { id })); follows.OnNewFollowersDetected += bot.Follows_OnNewFollowersDetected; follows.Start(); }
public TwitchFollowerService(ITwitchAPI twitchApi, TwitchClientSettings settings) { _twitchApi = twitchApi; _settings = settings; _followerService = new FollowerService(twitchApi); _followerService.SetChannelsById(new List <string> { settings.TwitchChannelId }); _followerService.Start(); _followerService.OnNewFollowersDetected += FollowerServiceOnOnNewFollowersDetected; }
public async void OnNewFollowersDetected_NotRaised_When_NewFollowerOlderThanLatest() { var usersFollowsResponseFirstUserJson = JMock.Of <GetUsersFollowsResponse>(o => o.Follows == new[] { Mock.Of <Follow>(f => f.FromUserId == "FromFirstUserId" && f.FollowedAt == new DateTime(1)) } ); var mockHandler = new Mock <IHttpCallHandler>(); mockHandler .Setup(x => x.GeneralRequest(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Core.Enums.ApiVersion>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(new KeyValuePair <int, string>(200, usersFollowsResponseFirstUserJson)); _api = TwitchLibMock.TwitchApi(mockHandler); var eventExecuteCount = 0; _followerService = new FollowerService(_api); _followerService.SetChannelsById(Utils.CreateListWithEmptyString()); _followerService.OnNewFollowersDetected += (sender, e) => eventExecuteCount++; await _followerService.UpdateLatestFollowersAsync(); var usersFollowsResponseSecondUserJson = JMock.Of <GetUsersFollowsResponse>(o => o.Follows == new[] { Mock.Of <Follow>(f => f.FromUserId == "FromSecondUserId" && f.FollowedAt == new DateTime(0)) } ); mockHandler.Reset(); mockHandler .Setup(x => x.GeneralRequest(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Core.Enums.ApiVersion>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(new KeyValuePair <int, string>(200, usersFollowsResponseSecondUserJson)); await _followerService.UpdateLatestFollowersAsync(); Assert.Equal(1, eventExecuteCount); }
public async void KnownFollowers_Contains_UserId_When_ServiceUpdated() { var usersFollowsResponseJson = JMock.Of <GetUsersFollowsResponse>(o => o.Follows == new[] { Mock.Of <Follow>(u => u.FromUserId == "UserId" && u.ToUserId == "Id") } ); _api = TwitchLibMock.TwitchApi( ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseJson) ); _followerService = new FollowerService(_api); _followerService.SetChannelsById(Utils.CreateListWithEmptyString()); await _followerService.UpdateLatestFollowersAsync(); Assert.NotNull(_followerService.KnownFollowers[string.Empty].FirstOrDefault(f => f.FromUserId == "UserId")); //Same check for SetChannelsByName var usersResponseJson = JMock.Of <GetUsersResponse>(o => o.Users == new[] { Mock.Of <User>(u => u.Id == "Id" && u.DisplayName == "DisplayName"), }); _api = TwitchLibMock.TwitchApi( ("https://api.twitch.tv/helix/users", usersResponseJson), ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseJson) ); _followerService = new FollowerService(_api); _followerService.SetChannelsByName(Utils.CreateListWithEmptyString()); await _followerService.UpdateLatestFollowersAsync(); Assert.NotNull(_followerService.KnownFollowers[string.Empty].FirstOrDefault(f => f.FromUserId == "UserId")); }
public void OnServiceTick_Raised_When_ServiceTicked() { var usersFollowsResponseJson = JMock.Of <GetUsersFollowsResponse>(o => o.Follows == new[] { Mock.Of <Follow>() } ); _api = TwitchLibMock.TwitchApi( ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseJson) ); var signalEvent = new ManualResetEvent(false); _followerService = new FollowerService(_api, checkIntervalInSeconds: 1); _followerService.SetChannelsById(Utils.CreateListWithEmptyString()); _followerService.OnServiceTick += (sender, e) => signalEvent.Set(); _followerService.Start(); Assert.True(signalEvent.WaitOne(1500)); }
public async void OnNewFollowersDetected_Raised_When_LatestFollowersUpdated() { var usersFollowsResponseJson = JMock.Of <GetUsersFollowsResponse>(o => o.Follows == new[] { Mock.Of <Follow>() } ); _api = TwitchLibMock.TwitchApi( ("https://api.twitch.tv/helix/users/follows", usersFollowsResponseJson) ); var eventExcecuted = false; _followerService = new FollowerService(_api); _followerService.SetChannelsById(Utils.CreateListWithEmptyString()); _followerService.OnNewFollowersDetected += (sender, e) => eventExcecuted = true; await _followerService.UpdateLatestFollowersAsync(); Assert.True(eventExcecuted); }
public void SetChannelsById_Throws_ArgumentException_When_ChannelsArgumentEmpty() { _followerService = new FollowerService(_api); AssertException.Throws <ArgumentException>(ChannelListEmptyExceptionMessage, () => _followerService.SetChannelsById(new List <string>())); }
public void SetChannelsById_Throws_ArgumentNullException_When_ChannelsArgumentNull() { _followerService = new FollowerService(_api); AssertException.Throws <ArgumentNullException>(() => _followerService.SetChannelsById(null)); }