/
AttachmentPromptTests.cs
120 lines (102 loc) · 5.17 KB
/
AttachmentPromptTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Adapters;
using Microsoft.Bot.Schema;
using Xunit;
namespace Microsoft.Bot.Builder.Dialogs.Tests
{
public class AttachmentPromptTests
{
[Fact]
public void AttachmentPromptWithEmptyIdShouldFail()
{
Assert.Throws<ArgumentNullException>(() => new AttachmentPrompt(string.Empty));
}
[Fact]
public void AttachmentPromptWithNullIdShouldFail()
{
Assert.Throws<ArgumentNullException>(() => new AttachmentPrompt(null));
}
[Fact]
public async Task BasicAttachmentPrompt()
{
var convoState = new ConversationState(new MemoryStorage());
var dialogState = convoState.CreateProperty<DialogState>("dialogState");
var adapter = new TestAdapter(TestAdapter.CreateConversation(nameof(BasicAttachmentPrompt)))
.Use(new AutoSaveStateMiddleware(convoState))
.Use(new TranscriptLoggerMiddleware(new TraceTranscriptLogger(traceActivity: false)));
// Create new DialogSet.
var dialogs = new DialogSet(dialogState);
// Create and add attachment prompt to DialogSet.
var attachmentPrompt = new AttachmentPrompt("AttachmentPrompt");
dialogs.Add(attachmentPrompt);
// Create mock attachment for testing.
var attachment = new Attachment { Content = "some content", ContentType = "text/plain" };
// Create incoming activity with attachment.
var activityWithAttachment = new Activity { Type = ActivityTypes.Message, Attachments = new List<Attachment> { attachment } };
await new TestFlow(adapter, async (turnContext, cancellationToken) =>
{
var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);
var results = await dc.ContinueDialogAsync();
if (results.Status == DialogTurnStatus.Empty)
{
var options = new PromptOptions { Prompt = new Activity { Type = ActivityTypes.Message, Text = "please add an attachment." } };
await dc.PromptAsync("AttachmentPrompt", options, cancellationToken);
}
else if (results.Status == DialogTurnStatus.Complete)
{
var attachments = results.Result as List<Attachment>;
var content = MessageFactory.Text((string)attachments[0].Content);
await turnContext.SendActivityAsync(content, cancellationToken);
}
})
.Send("hello")
.AssertReply("please add an attachment.")
.Send(activityWithAttachment)
.AssertReply("some content")
.StartTestAsync();
}
[Fact]
public async Task RetryAttachmentPrompt()
{
var convoState = new ConversationState(new MemoryStorage());
var dialogState = convoState.CreateProperty<DialogState>("dialogState");
var adapter = new TestAdapter(TestAdapter.CreateConversation(nameof(RetryAttachmentPrompt)))
.Use(new AutoSaveStateMiddleware(convoState))
.Use(new TranscriptLoggerMiddleware(new TraceTranscriptLogger(traceActivity: false)));
// Create new DialogSet.
var dialogs = new DialogSet(dialogState);
dialogs.Add(new AttachmentPrompt("AttachmentPrompt"));
// Create mock attachment for testing.
var attachment = new Attachment { Content = "some content", ContentType = "text/plain" };
// Create incoming activity with attachment.
var activityWithAttachment = new Activity { Type = ActivityTypes.Message, Attachments = new List<Attachment> { attachment } };
await new TestFlow(adapter, async (turnContext, cancellationToken) =>
{
var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);
var results = await dc.ContinueDialogAsync(cancellationToken);
if (results.Status == DialogTurnStatus.Empty)
{
var options = new PromptOptions { Prompt = new Activity { Type = ActivityTypes.Message, Text = "please add an attachment." } };
await dc.PromptAsync("AttachmentPrompt", options, cancellationToken);
}
else if (results.Status == DialogTurnStatus.Complete)
{
var attachments = results.Result as List<Attachment>;
var content = MessageFactory.Text((string)attachments[0].Content);
await turnContext.SendActivityAsync(content, cancellationToken);
}
})
.Send("hello")
.AssertReply("please add an attachment.")
.Send("hello again")
.AssertReply("please add an attachment.")
.Send(activityWithAttachment)
.AssertReply("some content")
.StartTestAsync();
}
}
}