Ejemplo n.º 1
0
        public void GetMentions_Test()
        {
            IMessageActivity activity = DialogTestBase.MakeTestMessage();

            Assert.IsFalse(activity.GetMentions().Any());
            activity.Entities = new List <Microsoft.Bot.Schema.Entity> {
                new Mention()
                {
                    Text = "testMention"
                }
            };
            // Cloning activity to resemble the incoming activity to bot
            var clonedActivity = JsonConvert.DeserializeObject <Activity>(JsonConvert.SerializeObject(activity));

            Assert.IsTrue(clonedActivity.GetMentions().Any());
            Assert.AreEqual("testMention", clonedActivity.GetMentions()[0].Text);
        }
Ejemplo n.º 2
0
        public static async Task VerifyScript(ILifetimeScope container, Func <IDialog <object> > makeRoot, bool proactive, StreamReader stream, Action <IDialogStack, string> extraCheck, string[] expected, string locale)
        {
            var toBot = DialogTestBase.MakeTestMessage();

            if (!string.IsNullOrEmpty(locale))
            {
                toBot.Locale = locale;
            }

            string input, label;
            int    current = 0;

            while ((input = ReadLine(stream, out label)) != null)
            {
                var adapter = new TestAdapter();
                await adapter.ProcessActivityAsync((Activity)toBot, async (ctx) =>
                {
                    using (var scope = DialogModule.BeginLifetimeScope(container, ctx))
                    {
                        var task  = scope.Resolve <IPostToBot>();
                        var queue = adapter.ActiveQueue;

                        Action <IDialogStack> check = (stack) =>
                        {
                            var count = int.Parse((proactive && current == 0) ? input : stream.ReadLine());
                            Assert.AreEqual(count, queue.Count);
                            for (var i = 0; i < count; ++i)
                            {
                                var toUser      = queue.Dequeue();
                                var expectedOut = ReadLine(stream, out label);
                                if (label == "ToUserText")
                                {
                                    Assert.AreEqual(expectedOut, JsonConvert.SerializeObject(toUser.Text));
                                }
                                else
                                {
                                    Assert.AreEqual(expectedOut, JsonConvert.SerializeObject(toUser.Attachments));
                                }
                            }

                            extraCheck?.Invoke(stack, ReadLine(stream, out label));
                        };

                        Func <IDialog <object> > scriptMakeRoot = () =>
                        {
                            return(makeRoot().Do(async(context, value) => context.PrivateConversationData.SetValue("result", JsonConvert.SerializeObject(await value))));
                        };
                        scope.Resolve <Func <IDialog <object> > >(TypedParameter.From(scriptMakeRoot));

                        if (proactive && current == 0)
                        {
                            var loop = scriptMakeRoot().Loop();
                            var data = scope.Resolve <IBotData>();
                            await data.LoadAsync(CancellationToken.None);
                            var stack = scope.Resolve <IDialogTask>();
                            stack.Call(loop, null);
                            await stack.PollAsync(CancellationToken.None);
                            check(stack);
                            input = ReadLine(stream, out label);
                        }

                        if (input.StartsWith("\""))
                        {
                            try
                            {
                                toBot.Text = input.Substring(1, input.Length - 2);
                                Assert.IsTrue(current < expected.Length && toBot.Text == expected[current++]);
                                await task.PostAsync(toBot, CancellationToken.None);
                                var data = scope.Resolve <IBotData>();
                                await data.LoadAsync(CancellationToken.None);
                                var stack = scope.Resolve <IDialogStack>();
                                check(stack);
                            }
                            catch (Exception e)
                            {
                                Assert.AreEqual(ReadLine(stream, out label), e.Message);
                            }
                        }
                        else if (label.ToLower().StartsWith("result"))
                        {
                            var data = scope.Resolve <IBotData>();
                            await data.LoadAsync(CancellationToken.None);
                            string result;
                            Assert.IsTrue(data.PrivateConversationData.TryGetValue("result", out result));
                            Assert.AreEqual(input.Trim(), result);
                        }
                    }
                });
            }
        }
Ejemplo n.º 3
0
        public async Task If_Root_Dialog_Throws_Propagate_Exception_Reset_Store()
        {
            var dialog = new Mock <IDialogThatFails>(MockBehavior.Loose);

            dialog
            .Setup(d => d.StartAsync(It.IsAny <IDialogContext>()))
            .Returns <IDialogContext>(async context => { context.Wait(dialog.Object.MessageReceived); });

            dialog
            .Setup(d => d.MessageReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()))
            .Returns <IDialogContext, IAwaitable <IMessageActivity> >(async(context, result) => { context.Wait(dialog.Object.Throw); });

            dialog
            .Setup(d => d.Throw(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()))
            .Throws <ApplicationException>();

            Func <IDialog <object> > MakeRoot = () => dialog.Object;
            var toBot = DialogTestBase.MakeTestMessage();

            using (new FiberTestBase.ResolveMoqAssembly(dialog.Object))
                using (var container = Build(Options.MockConnectorFactory, dialog.Object))
                {
                    await new TestAdapter().ProcessActivity((Activity)toBot, async(context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeRoot);

                            var task = scope.Resolve <IPostToBot>();
                            await task.PostAsync(toBot, CancellationToken.None);
                        }
                    });

                    dialog.Verify(d => d.StartAsync(It.IsAny <IDialogContext>()), Times.Once);
                    dialog.Verify(d => d.MessageReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()), Times.Once);
                    dialog.Verify(d => d.Throw(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()), Times.Never);

                    await new TestAdapter().ProcessActivity((Activity)toBot, async(context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeRoot);

                            await scope.Resolve <IBotData>().LoadAsync(default(CancellationToken));
                            var task = scope.Resolve <IDialogStack>();
                            Assert.AreNotEqual(0, task.Frames.Count);
                        }
                    });

                    await new TestAdapter().ProcessActivity((Activity)toBot, async(context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeRoot);

                            var task = scope.Resolve <IPostToBot>();

                            try
                            {
                                await task.PostAsync(toBot, CancellationToken.None);
                                Assert.Fail();
                            }
                            catch (ApplicationException)
                            {
                            }
                            catch
                            {
                                Assert.Fail();
                            }
                        }
                    });

                    dialog.Verify(d => d.StartAsync(It.IsAny <IDialogContext>()), Times.Once);
                    dialog.Verify(d => d.MessageReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()), Times.Once);
                    dialog.Verify(d => d.Throw(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()), Times.Once);

                    //make sure that data is persisted with connector
                    await new TestAdapter().ProcessActivity((Activity)toBot, async(context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            var connectorFactory = scope.Resolve <IConnectorClientFactory>();
                            var botDataStore     = scope.Resolve <IBotDataStore <BotData> >();
                            var botData          = scope.Resolve <IBotData>();
                            await botData.LoadAsync(default(CancellationToken));
                            // stack + resumption context
                            Assert.AreEqual(2, botData.PrivateConversationData.Count);
                        }
                    });

                    await new TestAdapter().ProcessActivity((Activity)toBot, async(context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeRoot);

                            await scope.Resolve <IBotData>().LoadAsync(default(CancellationToken));
                            var stack = scope.Resolve <IDialogStack>();
                            Assert.AreEqual(0, stack.Frames.Count);
                        }
                    });

                    await new TestAdapter().ProcessActivity((Activity)toBot, async(context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeRoot);

                            var task = scope.Resolve <IPostToBot>();
                            await task.PostAsync(toBot, CancellationToken.None);
                        }
                    });

                    dialog.Verify(d => d.StartAsync(It.IsAny <IDialogContext>()), Times.Exactly(2));
                    dialog.Verify(d => d.MessageReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()), Times.Exactly(2));
                    dialog.Verify(d => d.Throw(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()), Times.Once);
                }
        }
Ejemplo n.º 4
0
        public async Task InputHintTest()
        {
            var chain = Chain.PostToChain().Select(m => m.Text).ContinueWith <string, string>(async(context, result) =>
            {
                var text = await result;
                if (text.ToLower().StartsWith("inputhint"))
                {
                    var reply       = context.MakeMessage();
                    reply.Text      = "reply";
                    reply.InputHint = InputHints.ExpectingInput;
                    await context.PostAsync(reply);
                    return(Chain.Return($"{text}"));
                }
                else if (!text.ToLower().StartsWith("reset"))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        await context.PostAsync($"message:{i}");
                    }
                    return(Chain.Return($"{text}"));
                }
                else
                {
                    return(Chain.From(() => new PromptDialog.PromptConfirm("Are you sure you want to reset the count?",
                                                                           "Didn't get that!", 3, PromptStyle.Keyboard)).ContinueWith <bool, string>(async(ctx, res) =>
                    {
                        string reply;
                        if (await res)
                        {
                            ctx.UserData.SetValue("count", 0);
                            reply = "Reset count.";
                        }
                        else
                        {
                            reply = "Did not reset count.";
                        }
                        return Chain.Return(reply);
                    }));
                }
            }).PostToUser();
            Func <IDialog <object> > MakeRoot = () => chain;

            using (new FiberTestBase.ResolveMoqAssembly(chain))
                using (var container = Build(Options.InMemoryBotDataStore | Options.NeedsInputHint, chain))
                {
                    var msg = DialogTestBase.MakeTestMessage();
                    msg.Text = "test";
                    await new TestAdapter().ProcessActivityAsync((Activity)msg, async(context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            scope.Resolve <Func <IDialog <object> > >(TypedParameter.From(MakeRoot));
                            await Conversation.SendAsync(scope, context);

                            var queue = ((TestAdapter)context.Adapter).ActiveQueue;
                            Assert.IsTrue(queue.Count > 0);
                            while (queue.Count > 0)
                            {
                                var toUser = queue.Dequeue();
                                if (queue.Count > 0)
                                {
                                    Assert.IsTrue(toUser.InputHint == InputHints.IgnoringInput);
                                }
                                else
                                {
                                    Assert.IsTrue(toUser.InputHint == InputHints.AcceptingInput);
                                }
                            }
                        }
                    });


                    msg.Text = "inputhint";
                    await new TestAdapter().ProcessActivityAsync((Activity)msg, async(context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            scope.Resolve <Func <IDialog <object> > >(TypedParameter.From(MakeRoot));
                            await Conversation.SendAsync(scope, context);

                            var queue = ((TestAdapter)context.Adapter).ActiveQueue;
                            Assert.IsTrue(queue.Count == 2);
                            var toUser = queue.Dequeue();
                            Assert.AreEqual("reply", toUser.Text);
                            Assert.IsTrue(toUser.InputHint == InputHints.ExpectingInput);
                        }
                    });

                    msg.Text = "reset";
                    await new TestAdapter().ProcessActivityAsync((Activity)msg, async(context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            scope.Resolve <Func <IDialog <object> > >(TypedParameter.From(MakeRoot));
                            await Conversation.SendAsync(scope, context);

                            var queue = ((TestAdapter)context.Adapter).ActiveQueue;
                            Assert.IsTrue(queue.Count == 1);
                            var toUser = queue.Dequeue();
                            Assert.IsTrue(toUser.InputHint == InputHints.ExpectingInput);
                            Assert.IsNotNull(toUser.LocalTimestamp);
                        }
                    });
                }
        }