Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit 0e59d8c

Browse files
committed
updates va templates
1 parent 324d2be commit 0e59d8c

File tree

17 files changed

+265
-74
lines changed

17 files changed

+265
-74
lines changed

templates/csharp/VA/VA.Tests/BotTestBase.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Bot.Builder;
99
using Microsoft.Bot.Builder.Adapters;
1010
using Microsoft.Bot.Builder.AI.Luis;
11+
using Microsoft.Bot.Builder.AI.QnA;
1112
using Microsoft.Bot.Builder.Dialogs;
1213
using Microsoft.Bot.Builder.LanguageGeneration;
1314
using Microsoft.Bot.Connector.Authentication;
@@ -22,12 +23,17 @@
2223
using $ext_safeprojectname$.Dialogs;
2324
using $ext_safeprojectname$.Models;
2425
using $ext_safeprojectname$.Services;
26+
using $safeprojectname$.Mocks;
2527
using $safeprojectname$.Utilities;
2628

2729
namespace $safeprojectname$
2830
{
2931
public class BotTestBase
3032
{
33+
private const string _knowledgeBaseId = "Chitchat";
34+
private const string _endpointKey = "dummy-key";
35+
private const string _hostname = "https://dummy-hostname.azurewebsites.net/qnamaker";
36+
3137
public IServiceCollection Services { get; set; }
3238

3339
public LocaleTemplateManager TestLocaleTemplateManager { get; set; }
@@ -63,6 +69,17 @@ public virtual void Initialize()
6369
{
6470
{ "General", GeneralTestUtil.CreateRecognizer() }
6571
},
72+
QnAConfiguration = new Dictionary<string, Microsoft.Bot.Builder.AI.QnA.QnAMakerEndpoint>
73+
{
74+
{
75+
"Chitchat", new QnAMakerEndpoint
76+
{
77+
KnowledgeBaseId = _knowledgeBaseId,
78+
EndpointKey = _endpointKey,
79+
Host = _hostname
80+
}
81+
}
82+
}
6683
}
6784
},
6885
{
@@ -87,12 +104,6 @@ public virtual void Initialize()
87104
Services.AddSingleton(new MicrosoftAppCredentials("appId", "password"));
88105
Services.AddSingleton(new UserState(new MemoryStorage()));
89106
Services.AddSingleton(new ConversationState(new MemoryStorage()));
90-
Services.AddSingleton(sp =>
91-
{
92-
var userState = sp.GetService<UserState>();
93-
var conversationState = sp.GetService<ConversationState>();
94-
return new BotStateSet(userState, conversationState);
95-
});
96107

97108
// For localization testing
98109
CultureInfo.CurrentUICulture = new CultureInfo("en-us");
@@ -114,12 +125,12 @@ public virtual void Initialize()
114125
TestLocaleTemplateManager = new LocaleTemplateManager(localizedTemplates, "en-us");
115126
Services.AddSingleton(TestLocaleTemplateManager);
116127

117-
Services.AddTransient<MainDialog>();
128+
Services.AddTransient<MockMainDialog>();
118129
Services.AddTransient<OnboardingDialog>();
119130
Services.AddTransient<SwitchSkillDialog>();
120131
Services.AddTransient<List<SkillDialog>>();
121132
Services.AddSingleton<TestAdapter, DefaultTestAdapter>();
122-
Services.AddTransient<IBot, DefaultActivityHandler<MainDialog>>();
133+
Services.AddTransient<IBot, DefaultActivityHandler<MockMainDialog>>();
123134

124135
TestUserProfileState = new UserProfileState();
125136
TestUserProfileState.Name = "Bot";
@@ -130,6 +141,7 @@ public TestFlow GetTestFlow(bool includeUserProfile = true)
130141
var sp = Services.BuildServiceProvider();
131142
var adapter = sp.GetService<TestAdapter>()
132143
.Use(new FeedbackMiddleware(sp.GetService<ConversationState>(), sp.GetService<IBotTelemetryClient>()));
144+
133145
var userState = sp.GetService<UserState>();
134146
var userProfileState = userState.CreateProperty<UserProfileState>(nameof(UserProfileState));
135147

templates/csharp/VA/VA.Tests/InterruptionTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public async Task Test_Help_Interruption()
1919

2020
await GetTestFlow()
2121
.Send(string.Empty)
22-
.AssertReplyOneOf(allFirstPromptVariations.ToArray())
22+
.AssertReplyOneOf(allFirstPromptVariations.Cast<string>().ToArray())
2323
.Send(GeneralUtterances.Help)
2424
.AssertReply(activity => Assert.AreEqual(1, activity.AsMessageActivity().Attachments.Count))
2525
.StartTestAsync();
@@ -32,10 +32,10 @@ public async Task Test_Help_Interruption_In_Dialog()
3232

3333
await GetTestFlow(includeUserProfile: false)
3434
.Send(string.Empty)
35-
.AssertReplyOneOf(allNamePromptVariations.ToArray())
35+
.AssertReplyOneOf(allNamePromptVariations.Cast<string>().ToArray())
3636
.Send(GeneralUtterances.Help)
3737
.AssertReply(activity => Assert.AreEqual(1, activity.AsMessageActivity().Attachments.Count))
38-
.AssertReplyOneOf(allNamePromptVariations.ToArray())
38+
.AssertReplyOneOf(allNamePromptVariations.Cast<string>().ToArray())
3939
.StartTestAsync();
4040
}
4141

@@ -48,9 +48,9 @@ public async Task Test_Cancel_Interruption()
4848

4949
await GetTestFlow()
5050
.Send(string.Empty)
51-
.AssertReplyOneOf(allFirstPromptVariations.ToArray())
51+
.AssertReplyOneOf(allFirstPromptVariations.Cast<string>().ToArray())
5252
.Send(GeneralUtterances.Cancel)
53-
.AssertReplyOneOf(allResponseVariations.ToArray())
53+
.AssertReplyOneOf(allResponseVariations.Cast<string>().ToArray())
5454
.StartTestAsync();
5555
}
5656

@@ -61,9 +61,9 @@ public async Task Test_Repeat_Interruption()
6161

6262
await GetTestFlow(includeUserProfile: false)
6363
.Send(string.Empty)
64-
.AssertReplyOneOf(allNamePromptVariations.ToArray())
64+
.AssertReplyOneOf(allNamePromptVariations.Cast<string>().ToArray())
6565
.Send(GeneralUtterances.Repeat)
66-
.AssertReplyOneOf(allNamePromptVariations.ToArray())
66+
.AssertReplyOneOf(allNamePromptVariations.Cast<string>().ToArray())
6767
.StartTestAsync();
6868
}
6969
}

templates/csharp/VA/VA.Tests/MainDialogTests.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Threading.Tasks;
@@ -34,7 +35,7 @@ public async Task Test_Help_Intent()
3435

3536
await GetTestFlow()
3637
.Send(string.Empty)
37-
.AssertReplyOneOf(allFirstPromptVariations.ToArray())
38+
.AssertReplyOneOf(allFirstPromptVariations.Cast<string>().ToArray())
3839
.Send(GeneralUtterances.Help)
3940
.AssertReply(activity => Assert.AreEqual(1, activity.AsMessageActivity().Attachments.Count))
4041
.StartTestAsync();
@@ -47,24 +48,28 @@ public async Task Test_Escalate_Intent()
4748

4849
await GetTestFlow()
4950
.Send(string.Empty)
50-
.AssertReplyOneOf(allFirstPromptVariations.ToArray())
51+
.AssertReplyOneOf(allFirstPromptVariations.Cast<string>().ToArray())
5152
.Send(GeneralUtterances.Escalate)
5253
.AssertReply(activity => Assert.AreEqual(1, activity.AsMessageActivity().Attachments.Count))
5354
.StartTestAsync();
5455
}
5556

57+
/// <summary>
58+
/// ChitChat is the default fallback which will not be configured at functional test time so a mock ensures QnAMaker returns no answer
59+
/// enabling the unsupported message to be returned.
60+
/// </summary>
61+
/// <returns>Task.</returns>
5662
[TestMethod]
57-
[Ignore("the LG template 'UnsupportedMessage' has randomly generated response which makes this test unreliable")]
5863
public async Task Test_Unhandled_Message()
5964
{
6065
var allFirstPromptVariations = AllResponsesTemplates.ExpandTemplate("FirstPromptMessage");
6166
var allResponseVariations = AllResponsesTemplates.ExpandTemplate("UnsupportedMessage", TestUserProfileState);
6267

6368
await GetTestFlow()
6469
.Send(string.Empty)
65-
.AssertReplyOneOf(allFirstPromptVariations.ToArray())
70+
.AssertReplyOneOf(allFirstPromptVariations.Cast<string>().ToArray())
6671
.Send("Unhandled message")
67-
.AssertReplyOneOf(allResponseVariations.ToArray())
72+
.AssertReplyOneOf(allResponseVariations.Cast<string>().ToArray())
6873
.StartTestAsync();
6974
}
7075
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.IO;
3+
using System.Net.Http;
4+
using Microsoft.Bot.Builder.AI.QnA;
5+
using Microsoft.Bot.Builder.AI.QnA.Dialogs;
6+
using Microsoft.Bot.Solutions;
7+
using Microsoft.Bot.Solutions.Responses;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using RichardSzalay.MockHttp;
10+
using $ext_safeprojectname$.Dialogs;
11+
12+
namespace $safeprojectname$.Mocks
13+
{
14+
public class MockMainDialog : MainDialog
15+
{
16+
private MockHttpMessageHandler _mockHttpHandler = new MockHttpMessageHandler();
17+
private LocaleTemplateManager _templateManager;
18+
19+
public MockMainDialog(IServiceProvider serviceProvider)
20+
: base(serviceProvider)
21+
{
22+
// All calls to Generate Answer regardless of host or knowledgebaseId are captured
23+
_mockHttpHandler.When(HttpMethod.Post, "*/knowledgebases/*/generateanswer")
24+
.Respond("application/json", GetResponse("QnAMaker_NoAnswer.json"));
25+
26+
_templateManager = serviceProvider.GetService<LocaleTemplateManager>();
27+
}
28+
29+
protected override QnAMakerDialog TryCreateQnADialog(string knowledgebaseId, CognitiveModelSet cognitiveModels)
30+
{
31+
if (!cognitiveModels.QnAConfiguration.TryGetValue(knowledgebaseId, out QnAMakerEndpoint qnaEndpoint)
32+
|| qnaEndpoint == null)
33+
{
34+
throw new Exception($"Could not find QnA Maker knowledge base configuration with id: {knowledgebaseId}.");
35+
}
36+
37+
// QnAMaker dialog already present on the stack?
38+
if (Dialogs.Find(knowledgebaseId) == null)
39+
{
40+
// Return a QnAMaker dialog using our Http Mock
41+
return new QnAMakerDialog(
42+
knowledgeBaseId: qnaEndpoint.KnowledgeBaseId,
43+
endpointKey: qnaEndpoint.EndpointKey,
44+
hostName: qnaEndpoint.Host,
45+
noAnswer: _templateManager.GenerateActivityForLocale("UnsupportedMessage"),
46+
httpClient: new HttpClient(_mockHttpHandler))
47+
{
48+
Id = knowledgebaseId
49+
};
50+
}
51+
else
52+
{
53+
return null;
54+
}
55+
}
56+
57+
private Stream GetResponse(string fileName)
58+
{
59+
var path = GetFilePath(fileName);
60+
return File.OpenRead(path);
61+
}
62+
63+
private string GetFilePath(string fileName)
64+
{
65+
return Path.Combine(Environment.CurrentDirectory, "TestData", fileName);
66+
}
67+
}
68+
}

templates/csharp/VA/VA.Tests/OnboardingDialogTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ await GetTestFlow(includeUserProfile: false)
3636
MembersAdded = new List<ChannelAccount>() { new ChannelAccount("user") }
3737
})
3838
.AssertReply(activity => Assert.AreEqual(1, activity.AsMessageActivity().Attachments.Count))
39-
.AssertReplyOneOf(allNamePromptVariations.ToArray())
39+
.AssertReplyOneOf(allNamePromptVariations.Cast<string>().ToArray())
4040
.Send(testName)
41-
.AssertReplyOneOf(allHaveMessageVariations.ToArray())
41+
.AssertReplyOneOf(allHaveMessageVariations.Cast<string>().ToArray())
4242
.StartTestAsync();
4343
}
4444
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"answers": [
3+
]
4+
}

templates/csharp/VA/VA.Tests/VA.Tests.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
<WarningsAsErrors />
1212
</PropertyGroup>
1313

14+
<ItemGroup>
15+
<None Remove="TestData\QnAMaker_NoAnswer.json" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Content Include="TestData\QnAMaker_NoAnswer.json">
20+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
</Content>
22+
</ItemGroup>
23+
1424
<ItemGroup>
1525
<PackageReference Include="coverlet.msbuild" Version="2.8.0">
1626
<PrivateAssets>all</PrivateAssets>
@@ -21,6 +31,7 @@
2131
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
2232
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
2333
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
34+
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
2435
</ItemGroup>
2536

2637
<ItemGroup>

templates/csharp/VA/VA.Tests/VATestTemplate.vstemplate

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
<Project TargetFileName="VA.Tests.csproj" File="VA.Tests.csproj" ReplaceParameters="true">
1616
<Folder Name="Mocks" TargetFolderName="Mocks">
1717
<ProjectItem ReplaceParameters="true" TargetFileName="MockLuisRecognizer.cs">MockLuisRecognizer.cs</ProjectItem>
18+
<ProjectItem ReplaceParameters="true" TargetFileName="MockMainDialog.cs">MockMainDialog.cs</ProjectItem>
1819
</Folder>
1920
<Folder Name="Resources" TargetFolderName="Resources">
2021
<ProjectItem ReplaceParameters="true" TargetFileName="chitchat_default.json">chitchat_default.json</ProjectItem>
2122
<ProjectItem ReplaceParameters="true" TargetFileName="chitchat_greeting.json">chitchat_greeting.json</ProjectItem>
2223
<ProjectItem ReplaceParameters="true" TargetFileName="faq_default.json">faq_default.json</ProjectItem>
2324
<ProjectItem ReplaceParameters="true" TargetFileName="faq_overview.json">faq_overview.json</ProjectItem>
2425
</Folder>
26+
<Folder Name="TestData" TargetFolderName="TestData">
27+
<ProjectItem ReplaceParameters="true" TargetFileName="QnAMaker_NoAnswer.json">QnAMaker_NoAnswer.json</ProjectItem>
28+
</Folder>
2529
<Folder Name="Utilities" TargetFolderName="Utilities">
2630
<ProjectItem ReplaceParameters="true" TargetFileName="DispatchTestUtil.cs">DispatchTestUtil.cs</ProjectItem>
2731
<ProjectItem ReplaceParameters="true" TargetFileName="GeneralTestUtil.cs">GeneralTestUtil.cs</ProjectItem>

templates/csharp/VA/VA/Adapters/DefaultAdapter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public DefaultAdapter(
6363
Use(new TranscriptLoggerMiddleware(new AzureBlobTranscriptStore(settings.BlobStorage.ConnectionString, settings.BlobStorage.Container)));
6464
Use(new ShowTypingMiddleware());
6565
Use(new SetLocaleMiddleware(settings.DefaultLocale ?? "en-us"));
66-
Use(new EventDebuggerMiddleware()); Use(new SetSpeakMiddleware());
66+
Use(new EventDebuggerMiddleware());
67+
Use(new SetSpeakMiddleware());
6768
}
6869

6970
private async Task HandleTurnErrorAsync(ITurnContext turnContext, Exception exception)

templates/csharp/VA/VA/Bots/DefaultActivityHandler.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using System.Threading.Tasks;
88
using Microsoft.Bot.Builder;
99
using Microsoft.Bot.Builder.Dialogs;
10+
using Microsoft.Bot.Builder.Dialogs.Choices;
1011
using Microsoft.Bot.Builder.Teams;
12+
using Microsoft.Bot.Connector;
1113
using Microsoft.Bot.Schema;
1214
using Microsoft.Bot.Solutions;
1315
using Microsoft.Bot.Solutions.Responses;
@@ -66,6 +68,13 @@ protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersA
6668

6769
protected override Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
6870
{
71+
// directline speech occasionally sends empty message activities that should be ignored
72+
var activity = turnContext.Activity;
73+
if (activity.ChannelId == Channels.DirectlineSpeech && activity.Type == ActivityTypes.Message && string.IsNullOrEmpty(activity.Text))
74+
{
75+
return Task.CompletedTask;
76+
}
77+
6978
return _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken);
7079
}
7180

0 commit comments

Comments
 (0)