Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write unit tests for BeginSkill #4816

Merged
merged 8 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Skills;
using Microsoft.Bot.Schema;

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Testing.Mocks
{
/// <summary>
/// BotFrameworkClient mock.
/// </summary>
public class MockSkillBotFrameworkClient : BotFrameworkClient
EricDahlvang marked this conversation as resolved.
Show resolved Hide resolved
{
/// <inheritdoc/>
public override Task<InvokeResponse<T>> PostActivityAsync<T>(string fromBotId, string toBotId, Uri toUrl, Uri serviceUrl, string conversationId, Activity activity, CancellationToken cancellationToken = default)
{
var responseActivity = activity;

if (activity.Text.Contains("skill"))
{
responseActivity = new Activity()
{
Type = "message",
Text = "This is the skill talking: hello"
};
}

if (activity.Text.Contains("end"))
{
responseActivity = new Activity()
{
Type = "endOfConversation"
};
}

var response = new InvokeResponse<ExpectedReplies>()
{
Status = 200,
Body = new ExpectedReplies
{
Activities = new List<Activity>()
{
responseActivity
}
}
};

var casted = (InvokeResponse<T>)Convert.ChangeType(response, typeof(InvokeResponse<T>), new System.Globalization.CultureInfo("en-US"));
var result = Task.FromResult(casted);
return result;
}

/// <inheritdoc/>
/// <remarks>Not implemented.</remarks>
public override Task<InvokeResponse> PostActivityAsync(string fromBotId, string toBotId, Uri toUrl, Uri serviceUrl, string conversationId, Activity activity, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs.Adaptive.Testing.Mocks;
using Microsoft.Bot.Builder.Skills;
using Microsoft.Bot.Schema;

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Testing
{
/// <summary>
/// Middleware to add <see cref="MockSkillBotFrameworkClient"/> to the <see cref="ITurnContext.TurnState"/>.
/// </summary>
public class SetSkillBotFrameworkClientMiddleware : IMiddleware
EricDahlvang marked this conversation as resolved.
Show resolved Hide resolved
{
/// <inheritdoc/>
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
turnContext.TurnState.Add<BotFrameworkClient>(new MockSkillBotFrameworkClient());
}

await next(cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Skills;
using Microsoft.Bot.Schema;
using Moq;

namespace Microsoft.Bot.Builder.Dialogs.Adaptive.Testing
{
/// <summary>
/// Middleware to add a mocked <see cref="SkillConversationIdFactoryBase"/> to the <see cref="ITurnContext.TurnState"/>.
/// </summary>
public class SetSkillConversationIdFactoryBaseMiddleware : IMiddleware
{
/// <inheritdoc/>
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
var mockSkillConversationIdFactoryBase = new Mock<SkillConversationIdFactoryBase>();
mockSkillConversationIdFactoryBase.SetupAllProperties();
turnContext.TurnState.Add(mockSkillConversationIdFactoryBase.Object);
}

await next(cancellationToken).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ public TestAdapter DefaultTestAdapter(ResourceExplorer resourceExplorer, [Caller
.UseStorage(storage)
.UseBotState(userState, convoState)
.Use(new TranscriptLoggerMiddleware(new TraceTranscriptLogger(traceActivity: false)))
.Use(new SetTestOptionsMiddleware());
.Use(new SetTestOptionsMiddleware())
.Use(new SetSkillConversationIdFactoryBaseMiddleware())
EricDahlvang marked this conversation as resolved.
Show resolved Hide resolved
.Use(new SetSkillBotFrameworkClientMiddleware());

adapter.OnTurnError += (context, err) => context.SendActivityAsync(err.Message);
return adapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public async Task Action_BeginDialogWithoutActivity()
await TestUtils.RunTestScript(_resourceExplorerFixture.ResourceExplorer);
}

[Fact]
public async Task Action_BeginSkill()
{
await TestUtils.RunTestScript(_resourceExplorerFixture.ResourceExplorer);
}

[Fact]
public async Task Action_BeginSkillEndDialog()
{
await TestUtils.RunTestScript(_resourceExplorerFixture.ResourceExplorer);
}

[Fact]
public async Task Action_CancelDialog()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"$schema": "../../../tests.schema",
"$kind": "Microsoft.Test.Script",
"dialog": {
"$kind": "Microsoft.AdaptiveDialog",
"id": "planningTest",
"recognizer": {
"$kind": "Microsoft.RegexRecognizer",
"intents": [
{
"intent": "SkillIntent",
"pattern": "skill"
}
]
},
"triggers": [
{
"$kind": "Microsoft.OnIntent",
"intent": "SkillIntent",
"actions": [
{
"$kind": "Microsoft.BeginSkill",
"BotId": "test-bot-id",
"SkillHostEndpoint": "http://localhost:3978/api/skills/",
"SkillEndpoint": "http://localhost:39782/api/messages",
"SkillAppId": "test-app-id",
"ConnectionName": "test-skill-connection-name",
"activity": {
"$kind": "Microsoft.StaticActivityTemplate",
"activity": {
"type": "message",
"text": "skill",
"deliveryMode": "expectReplies"
}
}
}
]
},
{
"$kind": "Microsoft.OnUnknownIntent",
"actions": [
{
"$kind": "Microsoft.SendActivity",
"activity": "I'm a skill bot. To get started say 'skill'"
}
]
}
]
},
"script": [
{
"$kind": "Microsoft.Test.UserSays",
"text": "hi"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "I'm a skill bot. To get started say 'skill'"
},
{
"$kind": "Microsoft.Test.UserSays",
"text": "skill"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "This is the skill talking: hello"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"$schema": "../../../tests.schema",
"$kind": "Microsoft.Test.Script",
"dialog": {
"$kind": "Microsoft.AdaptiveDialog",
"autoEndDialog": false,
"id": "planningTest",
"recognizer": {
"$kind": "Microsoft.RegexRecognizer",
"intents": [
{
"intent": "SkillIntent",
"pattern": "skill"
}
]
},
"triggers": [
{
"$kind": "Microsoft.OnIntent",
"autoEndDialog": false,
"intent": "SkillIntent",
"actions": [
{
"$kind": "Microsoft.BeginSkill",
"BotId": "test-bot-id",
"SkillHostEndpoint": "http://localhost:3978/api/skills/",
"SkillEndpoint": "http://localhost:39782/api/messages",
"SkillAppId": "test-app-id",
"ConnectionName": "test-skill-connection-name",
"AllowInterruptions": false,
"activity": {
"$kind": "Microsoft.StaticActivityTemplate",
"activity": {
"type": "message",
"text": "skill",
"deliveryMode": "expectReplies"
}
}
},
{
"$kind": "Microsoft.SendActivity",
"activity": "Success"
}
]
}
]
},
"script": [
{
"$kind": "Microsoft.Test.UserSays",
"text": "skill"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "This is the skill talking: hello"
},
{
"$kind": "Microsoft.Test.UserSays",
"text": "end"
},
{
"$kind": "Microsoft.Test.AssertReply",
"text": "Success"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,17 @@ public async Task TestDialogResourcesAreValidForSchema(Resource resource)
if (!schema.StartsWith("http"))
{
Assert.True(File.Exists(Path.Combine(folder, PathUtils.NormalizePath(schema))), $"$schema {schema}");

// NOTE: Microsoft.SendActivity in this file fails validation even though it is valid.
// NOTE: Microsoft.SendActivity in the first file fails validation even though it is valid, same as Microsoft.StaticActivityTemplate on the last two.
// Bug filed with Newtonsoft: https://stackoverflow.com/questions/63493078/why-does-validation-fail-in-code-but-work-in-newtonsoft-web-validator
if (!fileResource.FullName.Contains("Action_SendActivity.test.dialog"))
var omit = new List<string>
{
"Action_SendActivity.test.dialog",
"Action_BeginSkill.test.dialog",
"Action_BeginSkillEndDialog.test.dialog"
};

if (!omit.Any(e => fileResource.FullName.Contains(e)))
{
jToken.Validate(Schema);
}
Expand Down