public async Task Intents_ValidateFilterOrder()
        {
            IntentRecognizerMiddleware m = new IntentRecognizerMiddleware();
            string shouldRun             = null;

            /*
             *  Filters are required to run in reverse order. This code validates that by registering 3 filters and
             *  running a simple state machine across them.
             */
            m.OnFilter(async(context, intentList) =>
            {
                Assert.IsTrue(shouldRun == "third", "1st filter did not run last");
                shouldRun = "done";
            });

            m.OnFilter(async(context, intentList) =>
            {
                Assert.IsTrue(shouldRun == "second", "2nd filter did not run second");
                shouldRun = "third";
            });

            m.OnFilter(async(context, intentList) =>
            {
                Assert.IsTrue(shouldRun == "first", "last filter did not run first");
                shouldRun = "second";
            });

            TurnContext bc = TestUtilities.CreateEmptyContext();

            shouldRun = "first";

            var resultingIntents = await m.Recognize(bc);

            Assert.IsTrue(shouldRun == "done", "Final filter did not run");
        }
        public async Task Intents_MutateIntentResult()
        {
            string targetName   = Guid.NewGuid().ToString();
            string replacedName = Guid.NewGuid().ToString();

            IntentRecognizerMiddleware m = new IntentRecognizerMiddleware();

            m.OnRecognize(async(context) =>
            {
                return(new List <Intent>
                {
                    new Intent()
                    {
                        Name = targetName
                    },
                });
            });

            m.OnFilter(async(context, intentList) =>
            {
                // When this code is called, the intent should already have been recognized. This code, as "filter code"
                // has the oppertunity to manipulate that intent.

                Assert.IsTrue(intentList.Count == 1, "Expecting exactly 1 intent");
                Assert.IsTrue(intentList.First().Name == targetName, $"Unexpected Intent Name. Expected {targetName}");

                // replace the name of the intent. Do this via the Context to vette paremeter passing
                intentList[0].Name = context.ToBotContext()["replacedName"];
            });

            BotContext bc = TestUtilities.CreateEmptyContext();

            // Test that the Intent comes back has been "filtered" to have the revised name

            bc["replacedName"] = replacedName; // put the "revised" intent name into the context to vette parameter passing
            var resultingIntents = await m.Recognize(bc);

            Assert.IsTrue(resultingIntents.Count == 1, "Expected exactly 1 intent");
            Assert.IsTrue(resultingIntents.First().Name == replacedName, $"Unexpected Intent Name. Expected {replacedName}");
        }
        public async Task Intents_RemoveIntentViaFilter()
        {
            string intentToKeep   = Guid.NewGuid().ToString();
            string intentToRemove = Guid.NewGuid().ToString();

            IntentRecognizerMiddleware m = new IntentRecognizerMiddleware();

            m.OnRecognize(async(context) =>
            {
                return(new List <Intent>
                {
                    new Intent()
                    {
                        Name = intentToKeep
                    },
                    new Intent()
                    {
                        Name = intentToRemove
                    }
                });
            });

            m.OnFilter(async(context, intentList) =>
            {
                Assert.IsTrue(intentList.Count == 2, "Expecting exactly 2 intents");
                Assert.IsTrue(intentList[0].Name == intentToKeep, $"Unexpected Intent Name. Expected {intentToKeep}");
                Assert.IsTrue(intentList[1].Name == intentToRemove, $"Unexpected Intent Name. Expected {intentToRemove}");

                intentList.RemoveAt(1);
            });

            TurnContext bc = TestUtilities.CreateEmptyContext();
            var         resultingIntents = await m.Recognize(bc);

            Assert.IsTrue(resultingIntents.Count == 1, "Expected exactly 1 intent");
            Assert.IsTrue(resultingIntents.First().Name == intentToKeep, $"Unexpected Intent Name. Expected {intentToKeep}");
        }