public void GivenValidHandlers_WhenSendingNullMessage_ReceiveValidResponse()
        {
            var client = GetValidMockTcpClient();

            var sendMessageRequest = new SendMessageRequest
            {
                UserCookieId = new Guid("96532257-a18b-4ccc-979b-c2c90fd80cfe").ToString(),
                BotName      = "testbot",
                Message      = null
            };

            var handler = new ChatScriptHandler(client.Object);

            client.Verify(client => client.Connected);
            client.Verify(client => client.GetStream());

            var response = handler.SendMessage(sendMessageRequest, null);

            string prefix  = _sendMessageRequest.UserCookieId + char.MinValue + sendMessageRequest.BotName + char.MinValue;
            string message = prefix + sendMessageRequest.Message + char.MinValue;

            byte[] sendMessageRequestBytes = Encoding.ASCII.GetBytes(message);

            byte[] responseMessageBytes = Encoding.ASCII.GetBytes(_responseMessage.Messages[0]);

            client.Verify(client => client.GetStream().Write(sendMessageRequestBytes, 0, sendMessageRequestBytes.Length));
            client.Verify(client => client.GetStream().Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()));

            Assert.AreEqual(_responseMessage.Messages[0], response.Messages[0]);
        }
Ejemplo n.º 2
0
        private SendMessageResponse SendMessageToChatScript(string text)
        {
            try
            {
                var map = GetNode <Map>("/root/Map");

                var request = new SendMessageRequest
                {
                    UserCookieId = map.User.CookieId.ToString(),
                    BotName      = BotName,
                    Message      = text,
                    InputData    = JsonConvert.SerializeObject(map.User?.Facts?.Split(',').ToList() ?? new List <string>())
                };

                SendMessageResponse response;

                if (OS.HasFeature("JavaScript"))
                {
                    string javaScript = @"
						var sendMessageRequest = "                         + JsonConvert.SerializeObject(request) + @";

						parent.SendMessageToChatScript(sendMessageRequest);
						"                        ;
                    string jsResponse = JavaScript.Eval(javaScript).ToString();

                    response = JsonConvert.DeserializeObject <SendMessageResponse>(jsResponse);
                }
                else
                {
                    var tcpClient = new TcpClient("localhost", 1024);
                    using (ITcpClient client = new TcpClientHandler(tcpClient))
                    {
                        var chatScript = new ChatScriptHandler(client);

                        response = chatScript.SendMessage(request, GetNode <Map>("/root/Map").Context);
                    }
                }

                if (response.NewFacts != null && response.NewFacts.Length > 0)
                {
                    EmitSignal(nameof(NewFacts), new[] { response.NewFacts });
                    _oldFacts = GetNode <Map>("/root/Map").User?.Facts?.Split(',').ToList() ?? new List <string>();
                }

                _lastResponse = response;

                return(response);
            }
            catch (Exception ex)
            {
                throw;
                return(new SendMessageResponse()
                {
                    Messages = new string[] { $"Failed to send to ChatScript: {ex.Message}" }
                });
            }
        }
        public void GivenMissingSendMessageRequest_WhenSendingMessage_FailOnMissingRequest()
        {
            var client = GetValidMockTcpClient();

            var handler = new ChatScriptHandler(client.Object);

            var ex = Assert.Throws <ArgumentNullException>(() => handler.SendMessage(null, null));

            Assert.AreEqual("request", ex.ParamName);
        }
        public void GivenMissingUser_WhenSendingMessage_FailOnMissingUser()
        {
            var client = GetValidMockTcpClient();

            var handler = new ChatScriptHandler(client.Object);

            var request = new SendMessageRequest
            {
                BotName = "testbot",
                Message = "test message"
            };

            var ex = Assert.Throws <ArgumentNullException>(() => handler.SendMessage(request, null));

            Assert.AreEqual("UserCookieId", ex.ParamName);
        }
        public void GivenMessageContainsNullTerminator_WhenSendingMessage_FailOnInvalidMessage()
        {
            var client = GetValidMockTcpClient();

            var handler = new ChatScriptHandler(client.Object);

            var request = new SendMessageRequest
            {
                UserCookieId = new Guid("96532257-a18b-4ccc-979b-c2c90fd80cfe").ToString(),
                BotName      = "testbot",
                Message      = "test " + char.MinValue + " message"
            };

            var ex = Assert.Throws <InvalidOperationException>(() => handler.SendMessage(request, null));

            Assert.AreEqual("Message contains null terminator", ex.Message);
        }
        public void GivenValidHandlers_WhenSendingValidMessageRequest_ReceiveValidMessageResponse()
        {
            var client = GetValidMockTcpClient();

            var handler = new ChatScriptHandler(client.Object);

            client.Verify(client => client.Connected);
            client.Verify(client => client.GetStream());

            var response = handler.SendMessage(_sendMessageRequest, null);

            string prefix  = _sendMessageRequest.UserCookieId + char.MinValue + _sendMessageRequest.BotName + char.MinValue;
            string message = prefix + _sendMessageRequest.Message + char.MinValue;

            byte[] sendMessageRequestBytes = Encoding.ASCII.GetBytes(message);

            byte[] responseMessageBytes = Encoding.ASCII.GetBytes(_responseMessage.Messages[0]);

            client.Verify(client => client.GetStream().Write(sendMessageRequestBytes, 0, sendMessageRequestBytes.Length));
            client.Verify(client => client.GetStream().Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>()));

            Assert.AreEqual(_responseMessage.Messages[0], response.Messages[0]);
        }
Ejemplo n.º 7
0
        public ActionResult OnPostSendMessageToChatScript(SendMessageRequest request)
        {
            var response = new SendMessageResponse();

            try
            {
                var tcpClient = new TcpClient("localhost", 1024);
                using (ITcpClient client = new TcpClientHandler(tcpClient))
                {
                    var chatScript = new ChatScriptHandler(client);

                    response = chatScript.SendMessage(request, _context);
                }
            }
            catch (Exception ex)
            {
                response.Messages = new string[] { ex.Message };
            }

            string responseJson = JsonConvert.SerializeObject(response);

            return(Content(responseJson));
        }