public void WillReconnectToNextServer() { var sim = SimAnon.Value; var ws = sim.ChatWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); var url1 = ws.ConnectUrl; // Prepare new ClientWebSocket for consumption by client code, and dispose the old one sim.ChatWebSocket = new SimulatedClientWebSocket(true, false, Simulator.CHAT_WELCOME); ws.Dispose(); ws = sim.ChatWebSocket; bool reconnectSucceeded = ws.JoinedChat.Wait(Simulator.TIMEOUT); var url2 = ws.ConnectUrl; reconnectSucceeded.Should().BeTrue(); url1.Should().NotBeNull(); url2.Should().NotBeNull(); url1.Should().NotBe(url2); } }
public void TimeoutUserSendsCorrectPackets() { var sim = SimAuth.Value; var ws = sim.ChatWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); var id = ws.LastId.GetValueOrDefault() + 1; var replyJson = BuildTimeoutReply(id); var task = sut.TimeoutUserAsync("SomeTestUser", TimeSpan.FromMinutes(5)); ws.InjectPacket(replyJson); task.Wait(Simulator.TIMEOUT); // Wait for TimeoutUserAsync to complete // Validate packet sent ws.LastPacket["type"].Should().NotBeNull(); ws.LastPacket["type"].Value <string>().Should().Be("method"); ws.LastPacket["method"].Should().NotBeNull(); ws.LastPacket["method"].Value <string>().Should().Be("timeout"); ws.LastPacket["arguments"].Should().NotBeNull(); var args = ws.LastPacket["arguments"].Values <string>().ToArray(); args.Should().HaveCount(2); args[0].Should().Be("SomeTestUser"); args[1].Should().Be("5m0s"); task.Result.Should().BeTrue(); } }
public void AddCorrectHeaders() { var sim = SimAuth.Value; var ws = sim.ChatWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); ws.Headers.Should().Contain("x-is-bot", "true"); } }
public void WIllRetryFailedInitialChatConnection() { var sim = SimAuth.Value; var ws = sim.ChatWebSocket = new SimulatedClientWebSocket(true, true, failConnectCount: 4); using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { var task = sut.StartAsync(); var didRetry = ws.FailedConnectAttemptsReached.Wait(Simulator.TIMEOUT); ws.ConnectionAttempts.Should().BeGreaterOrEqualTo(4); didRetry.Should().BeTrue(); } }
public void ImplementsCorrectInterfaces() { var sim = SimAnon.Value; var packet = BuildChatMessage(sim, 56789, "connor", "Hello world!"); var ws = sim.ChatWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); var result = Assert.Raises <ChatMessageEventArgs>(x => sut.ChatMessage += x, x => sut.ChatMessage -= x, () => ws.InjectPacket(packet)); // Assert.IsAssignableFrom<IChatService>(result.Sender); } }
public void HandlesNullAvatar() { var sim = SimAnon.Value; var packet = BuildChatMessage(sim, 56789, "connor", "Hello world!", roles: new string[] { "Owner", "Mod" }, avatar: null); var ws = sim.ChatWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); using (var monitor = sut.Monitor()) { ws.InjectPacket(packet); monitor.Should().Raise(nameof(sut.ChatMessage)) .WithArgs <ChatMessageEventArgs>(a => a.Avatar == "" && a.UserName == "connor") .WithSender(sut); } } }
public void RaisesUserLeftEvent() { var sim = SimAnon.Value; var packet = BuildUserJoinOrLeave(sim, "SomeNewUser", 34103083, isJoin: false); var ws = sim.ChatWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); using (var monitor = sut.Monitor()) { ws.InjectPacket(packet); monitor.Should().Raise(nameof(sut.UserLeft)) .WithArgs <ChatUserInfoEventArgs>(a => a.UserId == 34103083 && a.UserName == "SomeNewUser" && a.ChannelId == sim.ChannelInfo.Id) .WithSender(sut); } } }
public void DetectsUserRoles() { var sim = SimAnon.Value; var packet = BuildChatMessage(sim, 56789, "connor", "Hello world!", roles: new string[] { "Owner", "Mod" }); var ws = sim.ChatWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); using (var monitor = sut.Monitor()) { ws.InjectPacket(packet); monitor.Should().Raise(nameof(sut.ChatMessage)) .WithArgs <ChatMessageEventArgs>(a => a.Message == "Hello world!" && a.UserName == "connor" && a.UserId == 56789 && a.IsModerator && a.IsOwner && !a.IsWhisper) .WithSender(sut); } } }
public void WillReconnect() { var sim = SimAnon.Value; var ws = sim.ConstellationWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); // Prepare new ClientWebSocket for consumption by client code, and dispose the old one sim.ConstellationWebSocket = new SimulatedClientWebSocket(false, false, Simulator.CONSTALLATION_WELCOME); ws.Dispose(); ws = sim.ConstellationWebSocket; var connectedAndJoined = ws.JoinedConstallation.Wait(Simulator.TIMEOUT * 5); connectedAndJoined.Should().BeTrue(); } }
public void CanHandleRealDataDump() { var sim = SimAuth.Value; var ws = sim.ConstellationWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); foreach (var line in File.ReadAllLines("Data/ChatDump.json")) { if (string.IsNullOrWhiteSpace(line)) { continue; } ws.InjectPacket(line); } } }
public void CanCombineEvent() { var packet = BuildChannelStatusEvent("channel:1234:update", followers: 22, viewers: 43, online: true); var sim = SimAnon.Value; var ws = sim.ConstellationWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); using (var monitor = sut.Monitor()) { ws.InjectPacket(packet); monitor.Should().Raise(nameof(sut.ChannelUpdate)) .WithArgs <ChannelUpdateEventArgs>(a => a.Channel.NumFollowers == 22 && a.Channel.ViewersCurrent == 43 && a.Channel.Online == true && a.ChannelId == sim.ChannelInfo.Id) .WithSender(sut); } } }
public void WillConnectAndJoinAnonymously() { var sim = SimAnon.Value; var ws = sim.ChatWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); var connectedAndJoined = ws.JoinedChat.Wait(Simulator.TIMEOUT); connectedAndJoined.Should().BeTrue(); ws.LastPacket["method"].Should().NotBeNull(); ws.LastPacket["method"].Value <string>().Should().Be("auth"); ws.LastPacket["arguments"].Should().NotBeNull(); var args = ws.LastPacket["arguments"].ToString(Formatting.None); args.Should().Be($"[{sim.ChannelInfo.Id}]"); } }
public void CantSendAnononymously() { var sim = SimAnon.Value; var ws = sim.ChatWebSocket; const string text = "Some test message"; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); var id = ws.LastId.GetValueOrDefault() + 1; var replyJson = BuildMsgReply(sim, id, text); var task = sut.SendMessageAsync(text); ws.InjectPacket(replyJson); task.Wait(Simulator.TIMEOUT); // Wait for SendMessageAsync to complete task.Result.Should().BeFalse(); } }
public void WillConnectAndJoin() { var sim = SimAnon.Value; var ws = sim.ConstellationWebSocket; using (var sut = new MixerClientInternal(ChannelName, LoggerFactory, sim)) { sut.StartAsync().Wait(Simulator.TIMEOUT); var connectedAndJoined = ws.JoinedConstallation.Wait(Simulator.TIMEOUT); connectedAndJoined.Should().BeTrue(); // {{"id": 1,"type": "method","method": "livesubscribe","params": {"events": ["channel:1234:update" ]}}} ws.LastPacket["method"].Should().NotBeNull(); ws.LastPacket["method"].Value <string>().Should().Be("livesubscribe"); var args = $"[{sim.ChannelInfo.Id},{sim.ChannelInfo.UserId},\"{sim.ChatAuthKey}\"]"; ws.LastPacket["params"].Should().NotBeNull(); ws.LastPacket["params"]["events"].Should().NotBeNull(); var events = ws.LastPacket["params"]["events"].ToString(Formatting.None); events.Should().ContainAll($"channel:{sim.ChannelInfo.Id}:update"); } }