Exemplo n.º 1
0
        public async Task SendResumeAsyncTest()
        {
            var chain = Chain.PostToChain().Select(m => m.Text).Switch(
                new RegexCase <IDialog <string> >(new Regex("^resume"), (context, data) =>
            {
                context.UserData.SetValue("resume", true);
                return(Chain.Return("resumed!"));
            }),
                new DefaultCase <string, IDialog <string> >((context, data) => Chain.Return(data))).Unwrap().PostToUser();

            using (new FiberTestBase.ResolveMoqAssembly(chain))
                using (var container = Build(Options.InMemoryBotDataStore, chain))
                {
                    var msg = DialogTestBase.MakeTestMessage();
                    msg.Text = "testMsg";

                    using (var scope = DialogModule.BeginLifetimeScope(container, msg))
                    {
                        IDialog <object> Root() => chain;

                        scope.Resolve <Func <IDialog <object> > >(TypedParameter.From((Func <IDialog <object> >)Root));

                        await SendAsync(scope, msg);

                        scope.Resolve <Queue <IMessageActivity> >().Dequeue();

                        var botData = scope.Resolve <IBotData>();
                        await botData.LoadAsync(default(CancellationToken));

                        var dataBag = scope.Resolve <Func <IBotDataBag> >()();
                        Assert.IsTrue(dataBag.ContainsKey(ResumptionContext.RESUMPTION_CONTEXT_KEY));
                        Assert.IsNotNull(scope.Resolve <ConversationReference>());
                    }

                    var conversationReference = msg.ToConversationReference();
                    var continuationMessage   = conversationReference.GetPostToBotMessage();
                    using (var scope = DialogModule.BeginLifetimeScope(container, continuationMessage))
                    {
                        Func <IDialog <object> > MakeRoot = () => { throw new InvalidOperationException(); };
                        scope.Resolve <Func <IDialog <object> > >(TypedParameter.From(MakeRoot));

                        await scope.Resolve <IPostToBot>().PostAsync(new Activity {
                            Text = "resume"
                        }, CancellationToken.None);

                        var reply = scope.Resolve <Queue <IMessageActivity> >().Dequeue();
                        Assert.AreEqual("resumed!", reply.Text);

                        var botData = scope.Resolve <IBotData>();
                        await botData.LoadAsync(default(CancellationToken));

                        Assert.IsTrue(botData.UserData.GetValue <bool>("resume"));
                    }
                }
        }
Exemplo n.º 2
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";

                    using (var scope = DialogModule.BeginLifetimeScope(container, msg))
                    {
                        scope.Resolve <Func <IDialog <object> > >(TypedParameter.From(MakeRoot));
                        await ConversationTestBase.SendAsync(scope, msg);

                        var queue = scope.Resolve <Queue <IMessageActivity> >();
                        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";
                    using (var scope = DialogModule.BeginLifetimeScope(container, msg))
                    {
                        scope.Resolve <Func <IDialog <object> > >(TypedParameter.From(MakeRoot));
                        await ConversationTestBase.SendAsync(scope, msg);

                        var queue = scope.Resolve <Queue <IMessageActivity> >();
                        Assert.IsTrue(queue.Count == 2);
                        var toUser = queue.Dequeue();
                        Assert.AreEqual("reply", toUser.Text);
                        Assert.IsTrue(toUser.InputHint == InputHints.ExpectingInput);
                    }

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

                        var queue = scope.Resolve <Queue <IMessageActivity> >();
                        Assert.IsTrue(queue.Count == 1);
                        var toUser = queue.Dequeue();
                        Assert.IsTrue(toUser.InputHint == InputHints.ExpectingInput);
                        Assert.IsNotNull(toUser.LocalTimestamp);
                    }
                }
        }
Exemplo n.º 3
0
        public async Task InMemoryBotDataStoreTest()
        {
            var chain = Chain.PostToChain().Select(m => m.Text).ContinueWith <string, string>(async(context, result) =>
            {
                int t = 0;
                context.UserData.TryGetValue("count", out t);
                if (t > 0)
                {
                    int value;
                    Assert.IsTrue(context.ConversationData.TryGetValue("conversation", out value));
                    Assert.AreEqual(t - 1, value);
                    Assert.IsTrue(context.UserData.TryGetValue("user", out value));
                    Assert.AreEqual(t + 1, value);
                    Assert.IsTrue(context.PrivateConversationData.TryGetValue("PrivateConversationData", out value));
                    Assert.AreEqual(t + 2, value);
                }

                context.ConversationData.SetValue("conversation", t);
                context.UserData.SetValue("user", t + 2);
                context.PrivateConversationData.SetValue("PrivateConversationData", t + 3);
                context.UserData.SetValue("count", ++t);
                return(Chain.Return($"{t}:{await result}"));
            }).PostToUser();
            Func <IDialog <object> > MakeRoot = () => chain;

            using (new FiberTestBase.ResolveMoqAssembly(chain))
                using (var container = Build(Options.InMemoryBotDataStore, chain))
                {
                    var msg = DialogTestBase.MakeTestMessage();
                    msg.Text = "test";
                    using (var scope = DialogModule.BeginLifetimeScope(container, msg))
                    {
                        scope.Resolve <Func <IDialog <object> > >(TypedParameter.From(MakeRoot));

                        await ConversationTestBase.SendAsync(scope, msg);

                        var reply = scope.Resolve <Queue <IMessageActivity> >().Dequeue();
                        Assert.AreEqual("1:test", reply.Text);
                        var store = scope.Resolve <CachingBotDataStore>();
                        //Assert.AreEqual(0, store.cache.Count);
                        var dataStore = scope.Resolve <InMemoryDataStore>();
                        //Assert.AreEqual(3, dataStore.store.Count);
                    }

                    for (int i = 0; i < 10; i++)
                    {
                        using (var scope = DialogModule.BeginLifetimeScope(container, msg))
                        {
                            scope.Resolve <Func <IDialog <object> > >(TypedParameter.From(MakeRoot));
                            await ConversationTestBase.SendAsync(scope, msg);

                            var reply = scope.Resolve <Queue <IMessageActivity> >().Dequeue();
                            Assert.AreEqual($"{i + 2}:test", reply.Text);
                            var store = scope.Resolve <CachingBotDataStore>();
                            //Assert.AreEqual(0, store.cache.Count);
                            var dataStore = scope.Resolve <InMemoryDataStore>();
                            //Assert.AreEqual(3, dataStore.store.Count);
                            string val = string.Empty;
                            Assert.IsTrue(scope.Resolve <IBotData>().PrivateConversationData
                                          .TryGetValue(DialogModule.BlobKey, out val));
                            Assert.AreNotEqual(string.Empty, val);
                        }
                    }
                }
        }