Пример #1
0
        public async Task ShowsMessageIfLuisNotConfiguredAndCallsBookDialogDirectly()
        {
            // Arrange
            var mockRecognizer = SimpleMockFactory.CreateMockLuisRecognizer <FlightBookingRecognizer>(null, constructorParams: new Mock <IConfiguration>().Object);

            mockRecognizer.Setup(x => x.IsConfigured).Returns(false);

            // Create a specialized mock for BookingDialog that displays a dummy TextPrompt.
            // The dummy prompt is used to prevent the MainDialog waterfall from moving to the next step
            // and assert that the dialog was called.
            var mockDialog = new Mock <BookingDialog>();

            mockDialog
            .Setup(x => x.BeginDialogAsync(It.IsAny <DialogContext>(), It.IsAny <object>(), It.IsAny <CancellationToken>()))
            .Returns(async(DialogContext dialogContext, object options, CancellationToken cancellationToken) =>
            {
                dialogContext.Dialogs.Add(new TextPrompt("MockDialog"));
                return(await dialogContext.PromptAsync("MockDialog", new PromptOptions()
                {
                    Prompt = MessageFactory.Text($"{nameof(BookingDialog)} mock invoked")
                }, cancellationToken));
            });

            var sut        = new MainDialog(mockRecognizer.Object, mockDialog.Object, _mockLogger.Object);
            var testClient = new DialogTestClient(Channels.Test, sut, middlewares: new[] { new XUnitDialogTestLogger(Output) });

            // Act/Assert
            var reply = await testClient.SendActivityAsync <IMessageActivity>("hi");

            Assert.Equal("NOTE: LUIS is not configured. To enable all capabilities, add 'LuisAppId', 'LuisAPIKey' and 'LuisAPIHostName' to the appsettings.json file.", reply.Text);

            reply = testClient.GetNextReply <IMessageActivity>();
            Assert.Equal("BookingDialog mock invoked", reply.Text);
        }
        public async Task DialogFlowUseCases(TestDataObject testData)
        {
            // Arrange
            var bookingTestData = testData.GetObject <BookingDialogTestCase>();
            var sut             = new BookingDialog();
            var testClient      = new DialogTestClient(Channels.Test, sut, bookingTestData.InitialBookingDetails, _middlewares);

            // Execute the test case
            Output.WriteLine($"Test Case: {bookingTestData.Name}");
            for (var i = 0; i < bookingTestData.UtterancesAndReplies.GetLength(0); i++)
            {
                IMessageActivity reply;
                if (bookingTestData.UtterancesAndReplies[i, 0] != "")
                {
                    reply = await testClient.SendActivityAsync <IMessageActivity>(bookingTestData.UtterancesAndReplies[i, 0]);
                }
                else
                {
                    reply = testClient.GetNextReply <IMessageActivity>();
                }
                Assert.Equal(bookingTestData.UtterancesAndReplies[i, 1], reply?.Text);
            }

            var bookingResults = (BookingDetails)testClient.DialogTurnResult.Result;

            Assert.Equal(bookingTestData.ExpectedBookingDetails?.Origin, bookingResults?.Origin);
            Assert.Equal(bookingTestData.ExpectedBookingDetails?.Destination, bookingResults?.Destination);
            Assert.Equal(bookingTestData.ExpectedBookingDetails?.TravelDate, bookingResults?.TravelDate);
        }
        public async Task TaskSelector(string utterance, string intent, string invokedDialogResponse)
        {
            var mockLuisRecognizer = SimpleMockFactory.CreateMockLuisRecognizer <IRecognizer, FlightBooking>(
                new FlightBooking
            {
                Intents = new Dictionary <FlightBooking.Intent, IntentScore>
                {
                    { Enum.Parse <FlightBooking.Intent>(intent), new IntentScore()
                      {
                          Score = 1
                      } },
                },
                Entities = new FlightBooking._Entities(),
            });

            var sut        = new MainDialog(_mockLogger.Object, mockLuisRecognizer.Object, _mockBookingDialog);
            var testClient = new DialogTestClient(Channels.Test, sut, middlewares: new[] { new XUnitOutputMiddleware(Output) });

            var reply = await testClient.SendActivityAsync <IMessageActivity>("hi");

            Assert.Equal("What can I help you with today?", reply.Text);

            reply = await testClient.SendActivityAsync <IMessageActivity>(utterance);

            Assert.Equal(invokedDialogResponse, reply.Text);

            reply = testClient.GetNextReply <IMessageActivity>();
            Assert.Equal("What else can I do for you?", reply.Text);
        }
        public async Task DialogFlowUseCases(TestDataObject testData)
        {
            // Arrange
            var bookingTestData          = testData.GetObject <BookingDialogTestCase>();
            var mockFlightBookingService = new Mock <IFlightBookingService>();

            mockFlightBookingService
            .Setup(x => x.BookFlight(It.IsAny <BookingDetails>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(bookingTestData.FlightBookingServiceResult));

            var mockGetBookingDetailsDialog = SimpleMockFactory.CreateMockDialog <GetBookingDetailsDialog>(bookingTestData.GetBookingDetailsDialogResult).Object;

            var sut        = new BookingDialog(mockGetBookingDetailsDialog, mockFlightBookingService.Object);
            var testClient = new DialogTestClient(Channels.Test, sut, middlewares: _middlewares);

            // Act/Assert
            Output.WriteLine($"Test Case: {bookingTestData.Name}");
            for (var i = 0; i < bookingTestData.UtterancesAndReplies.GetLength(0); i++)
            {
                var utterance = bookingTestData.UtterancesAndReplies[i, 0];

                // Send the activity and receive the first reply or just pull the next
                // activity from the queue if there's nothing to send
                var reply = !string.IsNullOrEmpty(utterance)
                    ? await testClient.SendActivityAsync <IMessageActivity>(utterance)
                    : testClient.GetNextReply <IMessageActivity>();

                Assert.Equal(bookingTestData.UtterancesAndReplies[i, 1], reply.Text);
            }

            Assert.Equal(bookingTestData.ExpectedDialogResult.Status, testClient.DialogTurnResult.Status);
        }
Пример #5
0
        public async Task TaskSelector(string utterance, string intent, string invokedDialogResponse, string taskConfirmationMessage)
        {
            // Create a mock recognizer that returns the expected intent.
            var mockLuisRecognizer = SimpleMockFactory.CreateMockLuisRecognizer <FlightBookingRecognizer, FlightBooking>(
                new FlightBooking
            {
                Intents = new Dictionary <FlightBooking.Intent, IntentScore>
                {
                    { Enum.Parse <FlightBooking.Intent>(intent), new IntentScore()
                      {
                          Score = 1
                      } },
                },
                Entities = new FlightBooking._Entities(),
            },
                new Mock <IConfiguration>().Object);

            mockLuisRecognizer.Setup(x => x.IsConfigured).Returns(true);
            var sut        = new MainDialog(mockLuisRecognizer.Object, _mockBookingDialog, _mockLogger.Object);
            var testClient = new DialogTestClient(Channels.Test, sut, middlewares: new[] { new XUnitDialogTestLogger(Output) });

            // Execute the test case
            Output.WriteLine($"Test Case: {intent}");
            var reply = await testClient.SendActivityAsync <IMessageActivity>("hi");

            var weekLaterDate = DateTime.Now.AddDays(7).ToString("MMMM d, yyyy");

            Assert.Equal($"What can I help you with today?\nSay something like \"Book a flight from Paris to Berlin on {weekLaterDate}\"", reply.Text);

            reply = await testClient.SendActivityAsync <IMessageActivity>(utterance);

            Assert.Equal(invokedDialogResponse, reply.Text);

            // The Booking dialog displays an additional confirmation message, assert that it is what we expect.
            if (!string.IsNullOrEmpty(taskConfirmationMessage))
            {
                reply = testClient.GetNextReply <IMessageActivity>();
                Assert.StartsWith(taskConfirmationMessage, reply.Text);
            }

            // Validate that the MainDialog starts over once the task is completed.
            reply = testClient.GetNextReply <IMessageActivity>();
            Assert.Equal("What else can I do for you?", reply.Text);
        }
        public async Task ShowsMessageIfLuisNotConfigured()
        {
            // Arrange
            var sut        = new MainDialog(_mockLogger.Object, null, _mockBookingDialog);
            var testClient = new DialogTestClient(Channels.Test, sut, middlewares: new[] { new XUnitOutputMiddleware(Output) });

            // Act/Assert
            var reply = await testClient.SendActivityAsync <IMessageActivity>("hi");

            Assert.Equal("NOTE: LUIS is not configured. To enable all capabilities, add 'LuisAppId', 'LuisAPIKey' and 'LuisAPIHostName' to the appsettings.json file.", reply.Text);

            reply = testClient.GetNextReply <IMessageActivity>();
            Assert.Equal("What can I help you with today?", reply.Text);
        }
        public async Task Test_CheckoutWithDiscountDialog()
        {
            userState = new UserState(new MemoryStorage());

            var checkoutDialog = new CheckoutDialog(userState);
            //var mainDialog= new MainDialog(checkoutDialog);
            var testClient = new DialogTestClient(Channels.Msteams, checkoutDialog);

            var reply = await testClient.SendActivityAsync <IMessageActivity>("hi");

            Assert.AreEqual("Do you have membership with us?", ((HeroCard)reply.Attachments[0].Content).Text);

            reply = await testClient.SendActivityAsync <IMessageActivity>("yes");

            Assert.AreEqual("Great! we have special offers for you.", reply.Text);
            reply = testClient.GetNextReply <IMessageActivity>();
            Assert.AreEqual("Do you want to shop and check some offers today?", ((HeroCard)reply.Attachments[0].Content).Text);

            reply = await testClient.SendActivityAsync <IMessageActivity>("Yes");

            Assert.AreEqual("You will receive a coupon shortly.", ((HeroCard)reply.Attachments[0].Content).Text);
        }
        public async Task TaskSelector(string utterance, string intent, string invokedDialogResponse, string taskConfirmationMessage)
        {
            // Create a mock recognizer that returns the expected intent.
            var mockLuisRecognizer = SimpleMockFactory.CreateMockLuisRecognizer <FlightBookingRecognizer, FlightBooking>(
                new FlightBooking
            {
                Intents = new Dictionary <FlightBooking.Intent, IntentScore>
                {
                    { Enum.Parse <FlightBooking.Intent>(intent), new IntentScore()
                      {
                          Score = 1
                      } },
                },
                Entities = new FlightBooking._Entities(),
            },
                new Mock <IConfiguration>().Object);

            mockLuisRecognizer.Setup(x => x.IsConfigured).Returns(true);
            var sut        = new MainDialog(mockLuisRecognizer.Object, _mockBookingDialog, _mockLogger.Object);
            var testClient = new DialogTestClient(Channels.Test, sut, middlewares: new[] { new XUnitDialogTestLogger(Output) });

            // Execute the test case
            Output.WriteLine($"Test Case: {intent}");
            var reply = await testClient.SendActivityAsync <IMessageActivity>("hi");

            Assert.Equal("Are you experiencing any of the following: severe difficulty breathing, chest pain, very hard time waking up, confusion, lost consciousness?", reply.Text);



            reply = await testClient.SendActivityAsync <IMessageActivity>(utterance);

            Assert.Equal("Please call 911 or go directly to your nearest emergency department.", reply.Text);



            // Validate that the MainDialog starts over once the task is completed.
            reply = testClient.GetNextReply <IMessageActivity>();
            //Assert.Equal("What else can I do for you?", reply.Text);
        }