예제 #1
0
        public void DoesntPingWhenThereIsActivity()
        {
            var selfData = new SelfBotData { Id = "BOTID" };

            var mockState = new Mock<IBotState>();
            mockState.Setup(m => m.Connected).Returns(true).Verifiable();

            var mockService = new Mock<IBotServices>();
            mockService.Setup(m => m.SendRawMessage(It.Is<OutputMessage>(om => om.Type == "ping")))
                       .Verifiable();

            var handler = new PingHandler
            {
                BotState = mockState.Object,
                BotServices = mockService.Object,
                PingFrequencyMs = 200
            };

            var message = new PlainMessage
            {
                Type = EventType.Message,
                Subtype = MessageSubType.PlainMessage,
                Text = "<@BOTID>: pingdom"
            };

            Thread.Sleep(250);

            foreach (var loop in Enumerable.Range(1, 6))
            {
                handler.CanHandle(message);
                Thread.Sleep(100);
            }

            mockService.Verify(m => m.SendRawMessage(It.Is<OutputMessage>(om => om.Type == "ping")), Times.Exactly(1));
        }
예제 #2
0
        public void CanHandlePongMessages()
        {
            var selfData = new SelfBotData { Id = "BOTID" };

            var message = new Pong
            {
                Type = EventType.Pong
            };

            var handler = new PingHandler();

            var result = handler.CanHandle(message);

            Assert.True(result);
        }
예제 #3
0
        public void CanNotHandleInvalidPingMessages()
        {
            var selfData = new SelfBotData { Id = "BOTID" };

            var message = new PlainMessage
            {
                Type = EventType.Message,
                Subtype = MessageSubType.PlainMessage,
                Text = "<@BOTID>: pingdom"
            };

            var handler = new PingHandler();

            var result = handler.CanHandle(message);

            Assert.False(result);
        }
예제 #4
0
        public void HandlesValidPrefChange()
        {
            var selfBot = new SelfBotData { Prefs = new JObject() };
            selfBot.Prefs["pref1"] = "old";

            var mockState = new Mock<IBotState>();
            mockState.Setup(s => s.Self).Returns(selfBot).Verifiable();

            var message = new PrefChange
            {
                Type = EventType.PrefChange,
                Name = "pref1",
                Value = "foobar"
            };

            RunHandler(message, mockState);

            Assert.Equal("foobar", selfBot.Prefs["pref1"]);
        }
예제 #5
0
        public void CanNotHandleInvalidPingMessages()
        {
            var selfData = new SelfBotData { Id = "BOTID" };

            var mockState = new Mock<IBotState>();
            mockState.Setup(m => m.Ims).Returns(new List<DirectMessageChannel>()).Verifiable();
            mockState.Setup(m => m.Self).Returns(selfData).Verifiable();

            var message = new PlainMessage
            {
                Type = EventType.Message,
                Subtype = MessageSubType.PlainMessage,
                Text = "<@BOTID>: pingdom"
            };

            var handler = new UserPingHandler();
            handler.BotState = mockState.Object;

            var result = handler.CanHandle(message);

            Assert.False(result);
        }
예제 #6
0
        public void SendsPingAfterGivenTime()
        {
            var selfData = new SelfBotData { Id = "BOTID" };

            var mockState = new Mock<IBotState>();
            mockState.Setup(m => m.Connected).Returns(true).Verifiable();

            var mockService = new Mock<IBotServices>();
            mockService.Setup(m => m.SendRawMessage(It.Is<OutputMessage>(om => om.Type == "ping")))
                       .Verifiable();

            var handler = new PingHandler
            {
                BotState = mockState.Object,
                BotServices = mockService.Object,
                PingFrequencyMs = 200
            };

            Thread.Sleep(500);

            mockService.Verify(m => m.SendRawMessage(It.Is<OutputMessage>(om => om.Type == "ping")), Times.Exactly(2));
        }
예제 #7
0
        public async void HandlesChannelMessage()
        {
            var selfData = new SelfBotData { Id = "BOTID" };

            var mockState = new Mock<IBotState>();
            mockState.Setup(m => m.Ims).Returns(new List<DirectMessageChannel>()).Verifiable();

            string channelResult = "";
            string messageResult = "";

            var mockService = new Mock<IBotServices>();
            mockService.Setup(s => s.SendMessage(It.IsAny<string>(), It.IsAny<string>()))
                       .Callback<string, string>((c, m) => 
                       {
                           channelResult = c;
                           messageResult = m;
                       })
                       .Returns(1)
                       .Verifiable();

            var message = new PlainMessage
            {
                Type = EventType.Message,
                Subtype = MessageSubType.PlainMessage,
                Text = "<@BOTID>: ping",
                Channel = "CHANID",
                User = "******"
            };

            var handler = new UserPingHandler();
            handler.BotState = mockState.Object;
            handler.BotServices = mockService.Object;

            var result = await handler.HandleMessageAsync(message);

            mockState.Verify();
            mockService.Verify();
            Assert.Equal(ProcessingChainResult.Continue, result);
            Assert.Equal("CHANID", channelResult);
            Assert.Equal("<@USERID>: pong", messageResult);
        }
        public void IsToMeShouldNotMatchWhenNotNamed()
        {
            var selfData = new SelfBotData { Id = "BOTID" };

            var mockState = new Mock<IBotState>();
            mockState.Setup(m => m.Ims).Returns(new List<DirectMessageChannel>()).Verifiable();
            mockState.Setup(m => m.Self).Returns(selfData).Verifiable();

            var message = new PlainMessage()
            {
                Type = EventType.Message,
                Subtype = MessageSubType.PlainMessage,
                Channel = "TESTID",
                Text = "<@OTHERID>: test message"
            };

            var result = EventMessageExtensions.IsToMe(message, mockState.Object);

            mockState.Verify();
            Assert.False(result);
        }