Beispiel #1
0
        public async Task ResourceExplorer_LoadType_VerifyTokenRangeAndIdHonored()
        {
            var          path            = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\..")));
            const string resourcesFolder = "resources";
            const string resourceId      = "testWithId.dialog";

            using (var explorer = new ResourceExplorer())
            {
                explorer.AddResourceProvider(new FolderResourceProvider(explorer, path));

                // Load file using resource explorer
                var resource = explorer.GetResource(resourceId);
                var dialog   = await explorer.LoadTypeAsync <Dialog>(resource).ConfigureAwait(false);

                // Verify correct range
                var expectedRange = new SourceRange
                {
                    StartPoint = new SourcePoint(1, 1),
                    EndPoint   = new SourcePoint(14, 1),
                    Path       = Path.Join(Path.Join(path, resourcesFolder), resourceId)
                };

                Assert.Equal(expectedRange, dialog.Source);

                // Verify that the correct id was set
                Assert.Equal("explicit-id", dialog.Id);
            }
        }
Beispiel #2
0
        public async Task TestFolderSource_DeleteFiresChanged()
        {
            const string testId         = "DeleteFiresChanged.dialog";
            var          testDialogFile = Path.Combine(Environment.CurrentDirectory, testId);

            File.Delete(testDialogFile);
            File.WriteAllText(testDialogFile, "{}");

            var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\..")));

            using (var explorer = new ResourceExplorer())
            {
                explorer.AddResourceProvider(new FolderResourceProvider(explorer, path, true));

                AssertResourceFound(explorer, testId);

                var changeFired = new TaskCompletionSource <bool>();

                explorer.Changed += (e, resources) =>
                {
                    if (resources.Any(resource => resource.Id == testId))
                    {
                        changeFired.SetResult(true);
                    }
                };

                // changed file
                File.Delete(testDialogFile);

                await Task.WhenAny(changeFired.Task, Task.Delay(5000)).ConfigureAwait(false);

                AssertResourceNull(explorer, testId);
            }
        }
Beispiel #3
0
        public async Task ResourceExplorer_ReadTokenRangeAdvance_AssignId()
        {
            var          path            = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\..")));
            var          sourceContext   = new ResourceSourceContext();
            const string resourcesFolder = "resources";
            const string resourceId      = "test.dialog";

            using (var explorer = new ResourceExplorer())
            {
                explorer.AddResourceProvider(new FolderResourceProvider(explorer, path));

                // Load file using resource explorer
                var resource = explorer.GetResource(resourceId);

                // Read token range using resource explorer
                var(jToken, range) = await explorer.ReadTokenRangeAsync(resource, sourceContext, true).ConfigureAwait(false);

                // Verify correct range
                var expectedRange = new SourceRange
                {
                    StartPoint = new SourcePoint(1, 1),
                    EndPoint   = new SourcePoint(14, 1),
                    Path       = Path.Join(Path.Join(path, resourcesFolder), resourceId)
                };

                Assert.Equal(expectedRange, range);

                // Verify ID was added
                Assert.Equal(resourceId, sourceContext.DefaultIdMap[jToken]);
            }
        }
Beispiel #4
0
        public async Task AdaptiveDialogBotExceptionWhenNoResource()
        {
            // Arrange
            var logger = NullLogger <AdaptiveDialogBot> .Instance;

            var storage                    = new MemoryStorage();
            var conversationState          = new ConversationState(storage);
            var userState                  = new UserState(storage);
            var skillConversationIdFactory = new SkillConversationIdFactory(storage);
            var languagePolicy             = new LanguagePolicy("en-US");

            var resourceExplorer = new ResourceExplorer();
            var resourceProvider = new MockResourceProvider(resourceExplorer);

            resourceExplorer.AddResourceProvider(resourceProvider);

            var botFrameworkClientMock = new Mock <BotFrameworkClient>();

            var botFrameworkAuthenticationMock = new Mock <BotFrameworkAuthentication>();

            botFrameworkAuthenticationMock.Setup(bfa => bfa.CreateBotFrameworkClient()).Returns(botFrameworkClientMock.Object);

            // The test dialog being used here happens to not send anything so we only need to mock the type.
            var adapterMock = new Mock <BotAdapter>();

            // ChannelId and Conversation.Id are required for ConversationState and
            // ChannelId and From.Id are required for UserState.
            var activity = new Activity
            {
                ChannelId    = "test-channel",
                Conversation = new ConversationAccount {
                    Id = "test-conversation-id"
                },
                From = new ChannelAccount {
                    Id = "test-id"
                }
            };

            var turnContext = new TurnContext(adapterMock.Object, activity);

            // Act
            var bot = new AdaptiveDialogBot(
                "main.dialog",
                "main.lg",
                resourceExplorer,
                conversationState,
                userState,
                skillConversationIdFactory,
                languagePolicy,
                botFrameworkAuthenticationMock.Object,
                logger: logger);

            var exception = await Record.ExceptionAsync(() => ((IBot)bot).OnTurnAsync(turnContext, CancellationToken.None));

            // Assert
            Assert.NotNull(exception);
            Assert.IsType <InvalidOperationException>(exception);
        }
Beispiel #5
0
        public static ResourceExplorer BuildMemoryResourceExplorer(IEnumerable <MemoryResource> resources = null)
        {
            var resourceExplorer = new ResourceExplorer();
            var resourceProvider = new MemoryResourceProvider(
                resourceExplorer,
                resources ?? Array.Empty <MemoryResource>());

            resourceExplorer.AddResourceProvider(resourceProvider);

            return(resourceExplorer);
        }
Beispiel #6
0
        public void AdaptiveDialogBot_ShouldNotThrowOnNullLogger()
        {
            // Arrange
            var logger = NullLogger <AdaptiveDialogBot> .Instance;

            var storage                    = new MemoryStorage();
            var conversationState          = new ConversationState(storage);
            var userState                  = new UserState(storage);
            var skillConversationIdFactory = new SkillConversationIdFactory(storage);
            var languagePolicy             = new LanguagePolicy("en-US");

            var resourceExplorer = new ResourceExplorer();
            var resourceProvider = new MockResourceProvider(resourceExplorer);

            resourceExplorer.AddResourceProvider(resourceProvider);

            var botFrameworkClientMock = new Mock <BotFrameworkClient>();

            var botFrameworkAuthenticationMock = new Mock <BotFrameworkAuthentication>();

            botFrameworkAuthenticationMock.Setup(bfa => bfa.CreateBotFrameworkClient()).Returns(botFrameworkClientMock.Object);

            // The test dialog being used here happens to not send anything so we only need to mock the type.
            var adapterMock = new Mock <BotAdapter>();

            // ChannelId and Conversation.Id are required for ConversationState and
            // ChannelId and From.Id are required for UserState.
            var activity = new Activity
            {
                ChannelId    = "test-channel",
                Conversation = new ConversationAccount {
                    Id = "test-conversation-id"
                },
                From = new ChannelAccount {
                    Id = "test-id"
                }
            };

            var turnContext = new TurnContext(adapterMock.Object, activity);

            var telemetryClient = new NullBotTelemetryClient();

            new AdaptiveDialogBot(
                "main.dialog",
                "main.lg",
                resourceExplorer,
                conversationState,
                userState,
                skillConversationIdFactory,
                languagePolicy,
                botFrameworkAuthenticationMock.Object,
                telemetryClient,
                logger: null);
        }
        public static ResourceExplorer BuildMemoryResourceExplorer(IEnumerable <MemoryResource> resources = null)
        {
            var resourceExplorer = new ResourceExplorer();
            var resourceProvider = new MemoryResourceProvider(
                resourceExplorer,
                resources ?? Array.Empty <MemoryResource>());

            resourceExplorer.AddResourceProvider(resourceProvider);
            resourceExplorer.RegisterType <OnQnAMatch>(OnQnAMatch.Kind);

            return(resourceExplorer);
        }
        public async Task TestFolderSource()
        {
            var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\..")));

            using (var explorer = new ResourceExplorer())
            {
                explorer.AddResourceProvider(new FolderResourceProvider(explorer, path));

                await AssertResourceType(path, explorer, "dialog");

                var resources = explorer.GetResources("foo").ToArray();
                Assert.AreEqual(0, resources.Length);
            }
        }
Beispiel #9
0
        public void TestResourceDialogIdAssignment()
        {
            var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\..")));

            using (var explorer = new ResourceExplorer())
            {
                explorer.AddResourceProvider(new FolderResourceProvider(explorer, path));
                var dlg1 = explorer.LoadType <Dialog>("test.dialog") as AdaptiveDialog;
                Assert.Equal("test.dialog", dlg1.Id);

                Assert.Equal("1234567890", dlg1.Triggers[0].Actions[0].Id);
                Assert.Equal("test3.dialog", dlg1.Triggers[0].Actions[1].Id);

                var dlg2 = explorer.LoadType <Dialog>("test2.dialog");
                Assert.Equal("1234567890", dlg2.Id);
            }
        }
Beispiel #10
0
        public async Task ResourceExplorer_LoadType_VerifyAdaptiveDialogIdAssigned()
        {
            var          path       = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\..")));
            const string resourceId = "test.dialog";

            using (var explorer = new ResourceExplorer())
            {
                explorer.AddResourceProvider(new FolderResourceProvider(explorer, path));

                // Load file using resource explorer
                var resource = explorer.GetResource(resourceId);
                var dialog   = await explorer.LoadTypeAsync <AdaptiveDialog>(resource).ConfigureAwait(false);

                // Verify that the correct id was assigned
                Assert.Equal(resourceId, dialog.Id);
            }
        }
Beispiel #11
0
        public void TestFolderSource()
        {
            var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\..")));

            using (var explorer = new ResourceExplorer())
            {
                explorer.AddResourceProvider(new FolderResourceProvider(explorer, path));

                var resources = explorer.GetResources(".dialog").ToArray();

                Assert.Equal(4, resources.Length);
                Assert.Equal($".dialog", Path.GetExtension(resources[0].Id));

                resources = explorer.GetResources("foo").ToArray();
                Assert.Empty(resources);
            }
        }
        public async Task TestFolderSource_WriteFiresChanged()
        {
            string testId         = "WriteFiresChanged.dialog";
            string testDialogFile = Path.Combine(Environment.CurrentDirectory, testId);

            File.Delete(testDialogFile);
            string contents = "{}";

            File.WriteAllText(testDialogFile, contents);

            var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\..")));

            using (var explorer = new ResourceExplorer())
            {
                explorer.AddResourceProvider(new FolderResourceProvider(path, monitorChanges: true));

                AssertResourceFound(explorer, testId);

                await AssertResourceContents(explorer, testId, contents);

                TaskCompletionSource <bool> changeFired = new TaskCompletionSource <bool>();

                explorer.Changed += (resources) =>
                {
                    if (resources.Any(res => res.Id == testId))
                    {
                        changeFired.SetResult(true);
                    }
                };

                // changed file
                contents = "{'foo':123 }";
                File.WriteAllText(testDialogFile, contents);

                await Task.WhenAny(changeFired.Task, Task.Delay(5000)).ConfigureAwait(false);

                AssertResourceFound(explorer, testId);

                await AssertResourceContents(explorer, testId, contents);
            }
        }
Beispiel #13
0
        public void TestMissingResourceThrows()
        {
            var path = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, PathUtils.NormalizePath(@"..\..\..")));

            using (var explorer = new ResourceExplorer())
            {
                explorer.AddResourceProvider(new FolderResourceProvider(explorer, path));
                try
                {
                    explorer.GetResource("bogus.dialog");
                    throw new XunitException("should have thrown exception");
                }
                catch (ArgumentException err)
                {
                    Assert.Contains("bogus", err.Message);
                    Assert.Equal("bogus.dialog", err.ParamName);
                }
                catch (Exception err2)
                {
                    throw new XunitException($"Unknown exception {err2.GetType().Name} thrown");
                }
            }
        }
Beispiel #14
0
        public async Task AdaptiveDialogBotTurnState()
        {
            // Arrange
            var logger = NullLogger <AdaptiveDialogBot> .Instance;

            var storage                    = new MemoryStorage();
            var conversationState          = new ConversationState(storage);
            var userState                  = new UserState(storage);
            var skillConversationIdFactory = new SkillConversationIdFactory(storage);
            var languagePolicy             = new LanguagePolicy("en-US");

            var resourceExplorer = new ResourceExplorer();
            var resourceProvider = new MockResourceProvider(resourceExplorer);

            resourceProvider.Add("main.dialog", new MockResource("{ \"$kind\": \"Microsoft.AdaptiveDialog\" }"));
            resourceExplorer.AddResourceProvider(resourceProvider);

            var botFrameworkClientMock = new Mock <BotFrameworkClient>();

            var botFrameworkAuthenticationMock = new Mock <BotFrameworkAuthentication>();

            botFrameworkAuthenticationMock.Setup(bfa => bfa.CreateBotFrameworkClient()).Returns(botFrameworkClientMock.Object);

            // The test dialog being used here happens to not send anything so we only need to mock the type.
            var adapterMock = new Mock <BotAdapter>();

            // ChannelId and Conversation.Id are required for ConversationState and
            // ChannelId and From.Id are required for UserState.
            var activity = new Activity
            {
                ChannelId    = "test-channel",
                Conversation = new ConversationAccount {
                    Id = "test-conversation-id"
                },
                From = new ChannelAccount {
                    Id = "test-id"
                }
            };

            var turnContext = new TurnContext(adapterMock.Object, activity);

            var telemetryClient = new NullBotTelemetryClient();

            // Act
            var bot = new AdaptiveDialogBot(
                "main.dialog",
                "main.lg",
                resourceExplorer,
                conversationState,
                userState,
                skillConversationIdFactory,
                languagePolicy,
                botFrameworkAuthenticationMock.Object,
                telemetryClient,
                logger: logger);

            await bot.OnTurnAsync(turnContext, CancellationToken.None);

            // Assert
            Assert.NotNull(turnContext.TurnState.Get <BotFrameworkClient>());
            Assert.NotNull(turnContext.TurnState.Get <SkillConversationIdFactoryBase>());
            Assert.NotNull(turnContext.TurnState.Get <ConversationState>());
            Assert.NotNull(turnContext.TurnState.Get <UserState>());
            Assert.NotNull(turnContext.TurnState.Get <ResourceExplorer>());
            Assert.NotNull(turnContext.TurnState.Get <LanguageGenerator>());
            Assert.NotNull(turnContext.TurnState.Get <LanguageGeneratorManager>());
            Assert.NotNull(turnContext.TurnState.Get <LanguagePolicy>());
        }
Beispiel #15
0
        public async Task AdaptiveDialogBotSetTestOptions()
        {
            // Arrange
            var logger = NullLogger <AdaptiveDialogBot> .Instance;

            var storage                    = new MemoryStorage();
            var conversationState          = new ConversationState(storage);
            var userState                  = new UserState(storage);
            var skillConversationIdFactory = new SkillConversationIdFactory(storage);
            var languagePolicy             = new LanguagePolicy("en-US");

            var resourceExplorer = new ResourceExplorer();
            var resourceProvider = new MockResourceProvider(resourceExplorer);

            resourceProvider.Add("main.dialog", new MockResource("{ \"$kind\": \"Microsoft.AdaptiveDialog\" }"));
            resourceExplorer.AddResourceProvider(resourceProvider);

            var botFrameworkClientMock = new Mock <BotFrameworkClient>();

            var botFrameworkAuthenticationMock = new Mock <BotFrameworkAuthentication>();

            botFrameworkAuthenticationMock.Setup(bfa => bfa.CreateBotFrameworkClient()).Returns(botFrameworkClientMock.Object);

            // The test dialog being used here happens to not send anything so we only need to mock the type.
            var adapterMock = new Mock <BotAdapter>();

            // Type "event" and Name of "SetTestOptions" should store Value in ConversationState.
            // ChannelId and Conversation.Id are required for ConversationState and
            // ChannelId and From.Id are required for UserState.
            var activity = new Activity
            {
                Type         = "event",
                Name         = "SetTestOptions",
                ChannelId    = "test-channel",
                Conversation = new ConversationAccount {
                    Id = "test-conversation-id"
                },
                From = new ChannelAccount {
                    Id = "test-id"
                },
                Value = new JObject {
                    { "randomSeed", new JValue(123) }, { "randomValue", new JValue(456) }
                }
            };

            var turnContext = new TurnContext(adapterMock.Object, activity);

            var telemetryClient = new NullBotTelemetryClient();

            // Act
            var bot = new AdaptiveDialogBot(
                "main.dialog",
                "main.lg",
                resourceExplorer,
                conversationState,
                userState,
                skillConversationIdFactory,
                languagePolicy,
                botFrameworkAuthenticationMock.Object,
                telemetryClient,
                logger: logger);

            await bot.OnTurnAsync(turnContext, CancellationToken.None);

            // Assert TestOptions are in Conversation
            var testOptionsAccessor = conversationState.CreateProperty <JObject>("TestOptions");

            Assert.Equal(123, (await testOptionsAccessor.GetAsync(turnContext)).GetValue("randomSeed"));
            Assert.Equal(456, (await testOptionsAccessor.GetAsync(turnContext)).GetValue("randomValue"));
        }