public async static Task UI_Culture_From_Message(string language, CultureInfo current, CultureInfo expected)
        {
            var dialog = new Mock <ILocalizedDialog>();

            CultureInfo actual = null;

            dialog
            .Setup(d => d.StartAsync(It.IsAny <IDialogContext>()))
            .Returns <IDialogContext>(async c => { c.Wait(dialog.Object.FirstMessage); });
            dialog
            .Setup(d => d.FirstMessage(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()))
            .Returns <IDialogContext, IAwaitable <IMessageActivity> >(async(c, m) => actual = CurrentCulture);

            Func <IDialog <object> > MakeRoot = () => dialog.Object;

            using (new FiberTestBase.ResolveMoqAssembly(dialog.Object))
                using (var container = Build(Options.None, dialog.Object))
                {
                    var toBot = MakeTestMessage();
                    toBot.Locale = language;

                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                        Assert.AreEqual(current, CurrentCulture);
                        await task.PostAsync(toBot, CancellationToken.None);

                        Assert.AreEqual(current, CurrentCulture);
                        Assert.AreEqual(actual, expected);
                    }
                }
        }
示例#2
0
        public async Task DialogTask_Frames()
        {
            var dialog = new Mock <IDialogFrames <object> >(MockBehavior.Loose);

            dialog
            .Setup(d => d.StartAsync(It.IsAny <IDialogContext>()))
            .Returns <IDialogContext>(async context => { PromptDialog.Text(context, dialog.Object.ItemReceived, "blah"); });

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

            using (new FiberTestBase.ResolveMoqAssembly(dialog.Object))
                using (var container = Build(Options.None, dialog.Object))
                {
                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                        Assert.AreEqual(0, stack.Frames.Count);

                        await task.PostAsync(toBot, CancellationToken.None);

                        Assert.AreEqual(3, stack.Frames.Count);
                        Assert.IsInstanceOfType(stack.Frames[0].Target, typeof(PromptDialog.PromptString));
                        Assert.IsInstanceOfType(stack.Frames[1].Target, dialog.Object.GetType());
                    }
                }
        }
示例#3
0
        public async Task LinqQuerySyntax_Without_Reflection_Surrogate()
        {
            // no environment capture in closures here
            var query = from x in new PromptDialog.PromptString("p1", "p1", 1)
                        from y in new PromptDialog.PromptString("p2", "p2", 1)
                        select string.Join(" ", x, y);

            query = query.PostToUser();

            var words = new[] { "hello", "world" };

            using (var container = Build(Options.None))
            {
                var toBot = MakeTestMessage();

                var adapter = new TestAdapter();
                foreach (var word in words)
                {
                    toBot.Text = word;
                    await adapter.ProcessActivity((Activity)toBot, async (context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, () => query);

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

                var expected = string.Join(" ", words);
                AssertQueryText(expected, adapter.ActiveQueue);
            }
        }
示例#4
0
        public async Task LinqQuerySyntax_Where_True()
        {
            var query = Chain.PostToChain().Select(m => m.Text).Where(text => text == true.ToString()).PostToUser();

            using (var container = Build(Options.Reflection))
            {
                var toBot = MakeTestMessage();
                toBot.Text = true.ToString();

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

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

                var texts = adapter.ActiveQueue.Select(m => m.Text).ToArray();
                Assert.AreEqual(1, texts.Length);
                Assert.AreEqual(true.ToString(), texts[0]);
            }
        }
示例#5
0
        public async Task LinqQuerySyntax_SelectMany()
        {
            var toBot = MakeTestMessage();

            var words = new[] { "hello", "world", "!" };

            using (var container = Build(Options.Reflection))
            {
                var adapter = new TestAdapter();

                foreach (var word in words)
                {
                    toBot.Text = word;
                    await adapter.ProcessActivity((Activity)toBot, async (context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeSelectManyQuery);

                            var task = scope.Resolve <IPostToBot>();
                            // if we inline the query from MakeQuery into this method, and we use an anonymous method to return that query as MakeRoot
                            // then because in C# all anonymous functions in the same method capture all variables in that method, query will be captured
                            // with the linq anonymous methods, and the serializer gets confused trying to deserialize it all.
                            await task.PostAsync(toBot, CancellationToken.None);
                        }
                    });
                }

                var expected = string.Join(" ", words);
                AssertQueryText(expected, adapter.ActiveQueue);
            }
        }
        public async Task PromptSuccessAsync <T>(Action <IDialogContext, ResumeAfter <T> > prompt, IMessageActivity toBot, Func <T, bool> expected)
        {
            var dialogRoot = MockDialog <T>(prompt);

            Func <IDialog <object> > MakeRoot = () => dialogRoot.Object;

            using (new FiberTestBase.ResolveMoqAssembly(dialogRoot.Object))
                using (var container = Build(Options.ScopedQueue, dialogRoot.Object))
                {
                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                        await task.PostAsync(toBot, CancellationToken.None);

                        AssertMentions(PromptText, scope);
                    }

                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                        AssertNoMessages(scope);
                        dialogRoot.Verify(d => d.PromptResult(It.IsAny <IDialogContext>(), It.Is <IAwaitable <T> >(actual => expected(actual.GetAwaiter().GetResult()))), Times.Once);
                    }
                }
        }
示例#7
0
        public async Task LinqQuerySyntax_Where_False()
        {
            var query = Chain.PostToChain().Select(m => m.Text).Where(text => text == true.ToString()).PostToUser();

            using (var container = Build(Options.Reflection))
            {
                var toBot = MakeTestMessage();
                toBot.Text = false.ToString();

                using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                {
                    DialogModule_MakeRoot.Register(scope, () => query);

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

                        Assert.Fail();
                    }
                    catch (Chain.WhereCanceledException)
                    {
                    }
                }

                var queue = container.Resolve <Queue <Message> >();
                var texts = queue.Select(m => m.Text).ToArray();
                Assert.AreEqual(0, texts.Length);
            }
        }
示例#8
0
        public static async Task <Message> SendDirectAsync(Message toBot, Func <IDialog <object> > MakeRoot, CancellationToken token = default(CancellationToken))
        {
            var builder = new ContainerBuilder();

            builder.RegisterModule(new DialogModule_MakeRoot());

            builder
            .RegisterType <AlwaysSendDirect_BotToUser>()
            .AsSelf()
            .As <IBotToUser>()
            .InstancePerLifetimeScope();

            var container = builder.Build();

            using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
            {
                DialogModule_MakeRoot.Register(scope, MakeRoot);

                using (new LocalizedScope(toBot.Language))
                {
                    var task = scope.Resolve <IPostToBot>();
                    await task.PostAsync(toBot, default(CancellationToken));

                    return(null);
                }
            }
        }
示例#9
0
        public async Task Chain_Switch_Case()
        {
            var toBot = MakeTestMessage();

            var words         = new[] { "hello", "world", "echo" };
            var expectedReply = new[] { "world!", "!", "echo" };

            using (var container = Build(Options.Reflection))
            {
                var adapter = new TestAdapter();
                foreach (var word in words)
                {
                    toBot.Text = word;
                    await adapter.ProcessActivity((Activity)toBot, async (context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeSwitchDialog);

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

                var texts = adapter.ActiveQueue.Select(m => m.Text).ToArray();
                CollectionAssert.AreEqual(expectedReply, texts);
            }
        }
示例#10
0
        private async Task SendToBot(RecognizedPhrase recognizedPhrase, Func <IDialog <object> > dialog)
        {
            var activity = new Activity
            {
                From = new ChannelAccount {
                    Id = _conversationResult.Id
                },
                Conversation = new ConversationAccount {
                    Id = _conversationResult.Id
                },
                Recipient = new ChannelAccount {
                    Id = "Bot"
                },
                ServiceUrl = "https://skype.botframework.com",
                ChannelId  = "skype",
                Locale     = "pt-Br",
                Text       = recognizedPhrase.DisplayText
            };

            using (var scope = Conversation
                               .Container.BeginLifetimeScope(DialogModule.LifetimeScopeTag, Configure))
            {
                scope.Resolve <IMessageActivity>(TypedParameter.From((IMessageActivity)activity));
                DialogModule_MakeRoot.Register(scope, dialog);
                var postToBot = scope.Resolve <IPostToBot>();
                await postToBot.PostAsync(activity, CancellationToken.None);
            }
        }
示例#11
0
        public async Task Linq_Unwrap()
        {
            var toBot = MakeTestMessage();

            var words = new[] { "hello", "world" };

            using (var container = Build(Options.Reflection))
            {
                var adapter = new TestAdapter();
                foreach (var word in words)
                {
                    toBot.Text = word;
                    await adapter.ProcessActivity((Activity)toBot, async (context) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeUnwrapQuery);

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

                var expected = words.Last();
                AssertQueryText(expected, adapter.ActiveQueue);
            }
        }
示例#12
0
        public async Task LinqQuerySyntax_Where_False()
        {
            var query = Chain.PostToChain().Select(m => m.Text).Where(text => text == true.ToString()).PostToUser();

            using (var container = Build(Options.Reflection))
            {
                var toBot = MakeTestMessage();
                toBot.Text = false.ToString();
                var adapter = new TestAdapter();
                await adapter.ProcessActivity((Activity)toBot, async (context) =>
                {
                    using (var scope = DialogModule.BeginLifetimeScope(container, context))
                    {
                        DialogModule_MakeRoot.Register(scope, () => query);

                        var task = scope.Resolve <IPostToBot>();
                        try
                        {
                            await task.PostAsync(toBot, CancellationToken.None);
                            Assert.Fail();
                        }
                        catch (Chain.WhereCanceledException)
                        {
                        }
                    }
                });

                var texts = adapter.ActiveQueue.Select(m => m.Text).ToArray();
                Assert.AreEqual(1, texts.Length);
                Func <string, string, bool> Contains = (text, q) => text.IndexOf(q, StringComparison.OrdinalIgnoreCase) >= 0;
                Assert.IsTrue(Contains(texts[0], "exception") || Contains(texts[0], "bot code is having an issue"));
            }
        }
示例#13
0
        private async void SendToBot(ConversationService conversationService, string text)
        {
            Activity activity = new Activity()
            {
                From = new ChannelAccount {
                    Id = conversationService.ParticipantId, Name = conversationService.ParticipantName
                },
                Conversation = new ConversationAccount {
                    Id = conversationService.ConversationId
                },
                Recipient = new ChannelAccount {
                    Id = "Bot"
                },
                ServiceUrl = "https://skype.botframework.com",
                ChannelId  = "skype",
            };

            activity.Text = text;

            using (var scope = Microsoft.Bot.Builder.Dialogs.Conversation
                               .Container.BeginLifetimeScope(DialogModule.LifetimeScopeTag, builder => Configure(builder, conversationService)))
            {
                scope.Resolve <IMessageActivity>
                    (TypedParameter.From((IMessageActivity)activity));
                // DialogModule_MakeRoot.Register
                //  (scope, () => new Dialogs.LyncLuisDialog(scope.Resolve<PresenceService>()));
                DialogModule_MakeRoot.Register
                    (scope, () => new Dialogs.RootDialog());
                var postToBot = scope.Resolve <IPostToBot>();
                await postToBot.PostAsync(activity, CancellationToken.None);
            }
        }
示例#14
0
        public async Task SayAsync_ShouldSendText()
        {
            var dialog = Chain.PostToChain().Do(async(context, activity) =>
            {
                await context.SayAsync("some text");
            });

            using (var container = Build(Options.None))
            {
                var toBot = MakeTestMessage();
                toBot.Text = "hi";

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

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

                        await task.PostAsync(toBot, CancellationToken.None);

                        await AssertOutgoingActivity(scope, (toUser) =>
                        {
                            Assert.AreEqual("some text", toUser.Text);
                            Assert.IsNull(toUser.Speak);
                            Assert.AreEqual(0, toUser.Attachments.Count());
                        });
                    }
                });
            };
        }
示例#15
0
        public async Task DialogTask_RememberLastWait_ReturningFromChild()
        {
            string testMessage = "foo";
            Func <IDialog <object> > MakeRoot = () => new DialogOne();
            var toBot = MakeTestMessage();

            toBot.Text = testMessage;


            using (var container = Build(Options.MockConnectorFactory))
            {
                int count   = 2;
                var adapter = new TestAdapter();
                await adapter.ProcessActivity((Activity)toBot, async (context) =>
                {
                    for (int i = 0; i < count; i++)
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeRoot);
                            var task = scope.Resolve <IPostToBot>();
                            await task.PostAsync(toBot, CancellationToken.None);
                        }
                    }
                });

                var queue = adapter.ActiveQueue;
                Assert.AreEqual(count, queue.Count);
                Assert.AreEqual(testMessage, queue.Dequeue().Text);
            }
        }
示例#16
0
        public async Task Switch_Case()
        {
            var toBot = MakeTestMessage();

            var words         = new[] { "hello", "world", "echo" };
            var expectedReply = new[] { "world!", "!", "echo" };

            using (var container = Build(Options.Reflection))
            {
                foreach (var word in words)
                {
                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        DialogModule_MakeRoot.Register(scope, MakeSwitchDialog);

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

                var queue = container.Resolve <Queue <Message> >();
                var texts = queue.Select(m => m.Text).ToArray();
                CollectionAssert.AreEqual(expectedReply, texts);
            }
        }
示例#17
0
        private async Task SendToBot(RecognizedPhrase recognizedPhrase)
        {
            Activity activity = new Activity()
            {
                From = new ChannelAccount {
                    Id = conversationResult.Id
                },
                Conversation = new ConversationAccount {
                    Id = conversationResult.Id
                },
                Recipient = new ChannelAccount {
                    Id = "Bot"
                },
                ServiceUrl = "https://skype.botframework.com",
                ChannelId  = "skype",
            };

            activity.Text = recognizedPhrase.DisplayText;

            using (var scope = Microsoft.Bot.Builder.Dialogs.Conversation
                               .Container.BeginLifetimeScope(DialogModule.LifetimeScopeTag, Configure))
            {
                scope.Resolve <IMessageActivity>
                    (TypedParameter.From((IMessageActivity)activity));
                DialogModule_MakeRoot.Register
                    (scope, () => new Dialogs.RentLuisDialog());
                var postToBot = scope.Resolve <IPostToBot>();
                await postToBot.PostAsync(activity, CancellationToken.None);
            }
        }
示例#18
0
 /// <summary>
 /// Process an incoming message within the conversation.
 /// </summary>
 /// <remarks>
 /// This method:
 /// 1. Instantiates and composes the required components.
 /// 2. Deserializes the dialog state (the dialog stack and each dialog's state) from the <paramref name="toBot"/> <see cref="IMessageActivity"/>.
 /// 3. Resumes the conversation processes where the dialog suspended to wait for a <see cref="IMessageActivity"/>.
 /// 4. Queues <see cref="IMessageActivity"/>s to be sent to the user.
 /// 5. Serializes the updated dialog state in the messages to be sent to the user.
 ///
 /// The <paramref name="MakeRoot"/> factory method is invoked for new conversations only,
 /// because existing conversations have the dialog stack and state serialized in the <see cref="IMessageActivity"/> data.
 /// </remarks>
 /// <param name="toBot">The message sent to the bot.</param>
 /// <param name="MakeRoot">The factory method to make the root dialog.</param>
 /// <param name="token">The cancellation token.</param>
 /// <returns>A task that represents the message to send inline back to the user.</returns>
 public static async Task SendAsync(Microsoft.Bot.Builder.ITurnContext v4Context, Func <IDialog <object> > MakeRoot, CancellationToken token = default(CancellationToken))
 {
     using (var scope = DialogModule.BeginLifetimeScope(Container, v4Context))
     {
         DialogModule_MakeRoot.Register(scope, MakeRoot);
         await SendAsync(scope, v4Context, token);
     }
 }
示例#19
0
 /// <summary>
 /// Process an incoming message within the conversation.
 /// </summary>
 /// <remarks>
 /// This method:
 /// 1. Instantiates and composes the required components.
 /// 2. Deserializes the dialog state (the dialog stack and each dialog's state) from the <paramref name="toBot"/> <see cref="IMessageActivity"/>.
 /// 3. Resumes the conversation processes where the dialog suspended to wait for a <see cref="IMessageActivity"/>.
 /// 4. Queues <see cref="IMessageActivity"/>s to be sent to the user.
 /// 5. Serializes the updated dialog state in the messages to be sent to the user.
 ///
 /// The <paramref name="MakeRoot"/> factory method is invoked for new conversations only,
 /// because existing conversations have the dialog stack and state serialized in the <see cref="IMessageActivity"/> data.
 /// </remarks>
 /// <param name="toBot">The message sent to the bot.</param>
 /// <param name="MakeRoot">The factory method to make the root dialog.</param>
 /// <param name="token">The cancellation token.</param>
 /// <returns>A task that represents the message to send inline back to the user.</returns>
 public static async Task SendAsync(IMessageActivity toBot, Func <IDialog <object> > MakeRoot, CancellationToken token = default(CancellationToken))
 {
     using (var scope = DialogModule.BeginLifetimeScope(Container, toBot))
     {
         DialogModule_MakeRoot.Register(scope, MakeRoot);
         await SendAsync(scope, toBot, token);
     }
 }
示例#20
0
 private async Task SendAsync(IMessageActivity toBot, Func <ILifetimeScope, IDialog <object> > makeRoot, CancellationToken token = default(CancellationToken))
 {
     using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, toBot))
     {
         DialogModule_MakeRoot.Register(scope, () => makeRoot(scope));
         var task = scope.Resolve <IPostToBot>();
         await task.PostAsync(toBot, token);
     }
 }
示例#21
0
        private IMessageActivity GetResponse(IContainer container, Func <IDialog <object> > makeRoot)
        {
            using (var scope = DialogModule.BeginLifetimeScope(container, DialogTestBase.MakeTestMessage()))
            {
                DialogModule_MakeRoot.Register(scope, makeRoot);

                return(scope.Resolve <Queue <IMessageActivity> >().Dequeue());
            }
        }
示例#22
0
        public async Task SampleChain_Joke()
        {
            var joke = Chain
                       .PostToChain()
                       .Select(m => m.Text)
                       .Switch
                       (
                Chain.Case
                (
                    new Regex("^chicken"),
                    (context, text) =>
                    Chain
                    .Return("why did the chicken cross the road?")
                    .PostToUser()
                    .WaitToBot()
                    .Select(ignoreUser => "to get to the other side")
                ),
                Chain.Default <string, IDialog <string> >(
                    (context, text) =>
                    Chain
                    .Return("why don't you like chicken jokes?")
                    )
                       )
                       .Unwrap()
                       .PostToUser().
                       Loop();

            using (var container = Build(Options.None))
            {
                var toBot = MakeTestMessage();

                var toBotTexts = new[]
                {
                    "chicken",
                    "i don't know",
                    "anything but chickens"
                };

                foreach (var word in toBotTexts)
                {
                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        DialogModule_MakeRoot.Register(scope, () => joke);

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

                var queue = container.Resolve <Queue <Message> >();
                var texts = queue.Select(m => m.Text).ToArray();
                Assert.AreEqual("why did the chicken cross the road?", texts[0]);
                Assert.AreEqual("to get to the other side", texts[1]);
                Assert.AreEqual("why don't you like chicken jokes?", texts[2]);
            }
        }
示例#23
0
        /// <summary>
        /// Resume a conversation and post the data to the dialog waiting.
        /// </summary>
        /// <param name="conversationReference"> The resumption cookie.</param>
        /// <param name="toBot"> The data sent to bot.</param>
        /// <param name="token"> The cancellation token.</param>
        /// <returns> A task that represent the message to send back to the user after resumption of the conversation.</returns>
        public static async Task ResumeAsync(ConversationReference conversationReference, Microsoft.Bot.Builder.ITurnContext v4Context, CancellationToken token = default(CancellationToken))
        {
            using (var scope = DialogModule.BeginLifetimeScope(Container, v4Context))
            {
                Func <IDialog <object> > MakeRoot = () => { throw new InvalidOperationException(); };
                DialogModule_MakeRoot.Register(scope, MakeRoot);

                await SendAsync(scope, v4Context, token);
            }
        }
示例#24
0
        public async Task DialogTask_RememberLastWait()
        {
            var          dialogOne   = new Mock <IDialogFrames <string> >(MockBehavior.Strict);
            const string testMessage = "foo";

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

            dialogOne
            .Setup(d => d.ItemReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()))
            .Returns <IDialogContext, IAwaitable <IMessageActivity> >(async(context, message) =>
            {
                var msg    = await message;
                var reply  = context.MakeMessage();
                reply.Text = msg.Text;
                await context.PostAsync(reply);
                // no need to call context.Wait(...) since frame remembers the last wait from StartAsync(...)
            });

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

            using (new FiberTestBase.ResolveMoqAssembly(dialogOne.Object))
                using (var container = Build(Options.None, dialogOne.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>();
                            toBot.Text = testMessage;
                            await task.PostAsync(toBot, CancellationToken.None);

                            dialogOne.Verify(d => d.StartAsync(It.IsAny <IDialogContext>()), Times.Once);
                            dialogOne.Verify(d => d.ItemReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()), Times.Once);
                        }
                    });

                    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>();
                            toBot.Text = testMessage;
                            await task.PostAsync(toBot, CancellationToken.None);

                            dialogOne.Verify(d => d.StartAsync(It.IsAny <IDialogContext>()), Times.Once);
                            dialogOne.Verify(d => d.ItemReceived(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <IMessageActivity> >()), Times.Exactly(2));
                        }
                    });
                }
        }
示例#25
0
        public async Task PromptFailureAsync <T>(Action <IDialogContext, ResumeAfter <T> > prompt)
        {
            var dialogRoot = MockDialog <T>();

            dialogRoot
            .Setup(d => d.StartAsync(It.IsAny <IDialogContext>()))
            .Returns <IDialogContext>(async c => { c.Wait(dialogRoot.Object.FirstMessage); });
            dialogRoot
            .Setup(d => d.FirstMessage(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <Connector.Message> >()))
            .Returns <IDialogContext, IAwaitable <object> >(async(c, a) => { prompt(c, dialogRoot.Object.PromptResult); });
            dialogRoot
            .Setup(d => d.PromptResult(It.IsAny <IDialogContext>(), It.IsAny <IAwaitable <T> >()))
            .Returns <IDialogContext, IAwaitable <T> >(async(c, a) => { c.Done(default(T)); });

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

            using (new FiberTestBase.ResolveMoqAssembly(dialogRoot.Object))
                using (var container = Build(Options.ScopedQueue, dialogRoot.Object))
                {
                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                        await task.PostAsync(toBot, CancellationToken.None);

                        AssertMentions(PromptText, scope);
                    }

                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                        await task.PostAsync(toBot, CancellationToken.None);

                        AssertMentions(RetryText, scope);
                    }

                    using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
                    {
                        DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                        await task.PostAsync(toBot, CancellationToken.None);

                        AssertMentions("too many attempts", scope);
                        dialogRoot.Verify(d => d.PromptResult(It.IsAny <IDialogContext>(), It.Is <IAwaitable <T> >(actual => actual.ToTask().IsFaulted)), Times.Once);
                    }
                }
        }
示例#26
0
        /// <summary>
        /// Handle message activity type.
        /// </summary>
        /// <param name="activity">Activity</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Awaitable task.</returns>
        private async Task SendToBot(Activity activity, CancellationToken cancellationToken)
        {
            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
            {
                DialogModule_MakeRoot.Register(scope, () => scope.Resolve <RootLuisDialog>());
                var postTask = scope.Resolve <IPostToBot>().PostAsync(activity, cancellationToken);
                await this.SendTypingUntillTaskComplete(postTask, activity);

                await postTask;
            }
        }
示例#27
0
        /// <summary>
        /// Resume a conversation and post the data to the dialog waiting.
        /// </summary>
        /// <typeparam name="T"> Type of the data. </typeparam>
        /// <param name="resumptionCookie"> The id of the bot.</param>
        /// <param name="toBot"> The data sent to bot.</param>
        /// <param name="token"> The cancellation token.</param>
        /// <returns> A task that represent the message to send back to the user after resumption of the conversation.</returns>
        public static async Task ResumeAsync<T>(ResumptionCookie resumptionCookie, T toBot, CancellationToken token = default(CancellationToken))
        {
            var continuationMessage = resumptionCookie.GetMessage(); 
            using (var scope = DialogModule.BeginLifetimeScope(Container, continuationMessage))
            {
                Func<IDialog<object>> MakeRoot = () => { throw new InvalidOperationException(); };
                DialogModule_MakeRoot.Register(scope, MakeRoot);

                await ResumeAsync(scope, continuationMessage, toBot, token);
            }
        }
示例#28
0
        /// <summary>
        /// Resume a conversation and post the data to the dialog waiting.
        /// </summary>
        /// <param name="conversationReference"> The resumption cookie.</param>
        /// <param name="toBot"> The data sent to bot.</param>
        /// <param name="token"> The cancellation token.</param>
        /// <returns> A task that represent the message to send back to the user after resumption of the conversation.</returns>
        public static async Task ResumeAsync(ConversationReference conversationReference, IActivity toBot, CancellationToken token = default(CancellationToken))
        {
            var continuationMessage = conversationReference.GetPostToBotMessage();

            using (var scope = DialogModule.BeginLifetimeScope(Container, continuationMessage))
            {
                Func <IDialog <object> > MakeRoot = () => { throw new InvalidOperationException(); };
                DialogModule_MakeRoot.Register(scope, MakeRoot);

                await SendAsync(scope, toBot, token);
            }
        }
示例#29
0
        private async Task <IMessageActivity> GetResponse(IContainer container, Func <IDialog <object> > makeRoot, IMessageActivity toBot)
        {
            using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
            {
                DialogModule_MakeRoot.Register(scope, makeRoot);

                // act: sending the message
                await Conversation.SendAsync(scope, toBot);

                return(scope.Resolve <Queue <IMessageActivity> >().Dequeue());
            }
        }
示例#30
0
        public async Task PromptFailureAsync <T>(Action <IDialogContext, ResumeAfter <T> > prompt)
        {
            var dialogRoot = MockDialog <T>(prompt);

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

            using (new FiberTestBase.ResolveMoqAssembly(dialogRoot.Object))
                using (var container = Build(Options.ScopedQueue, dialogRoot.Object))
                {
                    var adapter = new TestAdapter();
                    await adapter.ProcessActivityAsync((Activity)toBot, async (context, cancellationToken) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                            await task.PostAsync(toBot, CancellationToken.None);
                            AssertMentions(PromptText, adapter.ActiveQueue.Dequeue() as IMessageActivity);
                        }
                    });

                    await adapter.ProcessActivityAsync((Activity)toBot, async (context, cancellationToken) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                            await task.PostAsync(toBot, CancellationToken.None);
                            AssertMentions(RetryText, adapter.ActiveQueue.Dequeue() as IMessageActivity);
                        }
                    });

                    await adapter.ProcessActivityAsync((Activity)toBot, async (context, cancellationToken) =>
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, context))
                        {
                            DialogModule_MakeRoot.Register(scope, MakeRoot);

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

                            await task.PostAsync(toBot, CancellationToken.None);
                            AssertMentions("too many attempts", adapter.ActiveQueue.Dequeue() as IMessageActivity);
                            dialogRoot.Verify(d => d.PromptResult(It.IsAny <IDialogContext>(), It.Is <IAwaitable <T> >(actual => actual.ToTask().IsFaulted)), Times.Once);
                        }
                    });
                }
        }