-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Script.cs
225 lines (220 loc) · 9.12 KB
/
Script.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using Autofac;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Connector;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Bot.Builder.Tests
{
class Script : DialogTestBase
{
public static async Task RecordScript(ILifetimeScope container,
bool proactive,
StreamWriter stream,
Func<string> extraInfo,
params string[] inputs)
{
var toBot = MakeTestMessage();
using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
{
var task = scope.Resolve<IPostToBot>();
var queue = scope.Resolve<Queue<IMessageActivity>>();
Action drain = () =>
{
stream.WriteLine($"{queue.Count()}");
while (queue.Count > 0)
{
var toUser = queue.Dequeue();
if (!string.IsNullOrEmpty(toUser.Text))
{
stream.WriteLine($"ToUserText:{JsonConvert.SerializeObject(toUser.Text)}");
}
else
{
stream.WriteLine($"ToUserButtons:{JsonConvert.SerializeObject(toUser.Attachments)}");
}
}
};
string result = null;
var root = scope.Resolve<IDialog<object>>().Do(async (context, value) =>
result = JsonConvert.SerializeObject(await value));
if (proactive)
{
var loop = root.Loop();
var data = scope.Resolve<IBotData>();
await data.LoadAsync(CancellationToken.None);
var stack = scope.Resolve<IDialogStack>();
stack.Call(loop, null);
await stack.PollAsync(CancellationToken.None);
drain();
}
else
{
var builder = new ContainerBuilder();
builder
.RegisterInstance(root)
.AsSelf()
.As<IDialog<object>>();
builder.Update((IContainer)container);
}
foreach (var input in inputs)
{
stream.WriteLine($"FromUser:{JsonConvert.SerializeObject(input)}");
toBot.Text = input;
try
{
await task.PostAsync(toBot, CancellationToken.None);
drain();
if (extraInfo != null)
{
var extra = extraInfo();
stream.WriteLine(extra);
}
}
catch (Exception e)
{
stream.WriteLine($"Exception:{e.Message}");
}
}
if (result != null)
{
stream.WriteLine($"Result: {result}");
}
}
}
public static string ReadLine(StreamReader stream, out string label)
{
string line = stream.ReadLine();
label = null;
if (line != null)
{
int pos = line.IndexOf(':');
if (pos != -1)
{
label = line.Substring(0, pos);
line = line.Substring(pos + 1);
}
}
return line;
}
public static async Task VerifyScript(ILifetimeScope container, bool proactive, StreamReader stream, Action<string> extraCheck, string[] expected)
{
var toBot = DialogTestBase.MakeTestMessage();
using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
{
var task = scope.Resolve<IPostToBot>();
var queue = scope.Resolve<Queue<IMessageActivity>>();
string input, label;
Action check = () =>
{
var count = int.Parse(stream.ReadLine());
Assert.AreEqual(count, queue.Count);
for (var i = 0; i < count; ++i)
{
var toUser = queue.Dequeue();
var expectedOut = ReadLine(stream, out label);
if (label == "ToUserText")
{
Assert.AreEqual(expectedOut, JsonConvert.SerializeObject(toUser.Text));
}
else
{
Assert.AreEqual(expectedOut, JsonConvert.SerializeObject(toUser.Attachments));
}
}
extraCheck?.Invoke(ReadLine(stream, out label));
};
string result = null;
var root = scope.Resolve<IDialog<object>>().Do(async (context, value) => result = JsonConvert.SerializeObject(await value));
if (proactive)
{
var loop = root.Loop();
var data = scope.Resolve<IBotData>();
await data.LoadAsync(CancellationToken.None);
var stack = scope.Resolve<IDialogStack>();
stack.Call(loop, null);
await stack.PollAsync(CancellationToken.None);
check();
}
else
{
var builder = new ContainerBuilder();
builder
.RegisterInstance(root)
.AsSelf()
.As<IDialog<object>>();
builder.Update((IContainer)container);
}
int current = 0;
while ((input = ReadLine(stream, out label)) != null)
{
if (input.StartsWith("\""))
{
input = input.Substring(1, input.Length - 2);
Assert.IsTrue(current < expected.Length && input == expected[current++]);
toBot.Text = input;
try
{
await task.PostAsync(toBot, CancellationToken.None);
check();
}
catch (Exception e)
{
Assert.AreEqual(ReadLine(stream, out label), e.Message);
}
}
else if (input.StartsWith("Result:"))
{
Assert.AreEqual(input.Substring(7), result);
}
}
}
}
public static async Task RecordDialogScript<T>(string filePath, IDialog<T> dialog, bool proactive, params string[] inputs)
{
using (var stream = new StreamWriter(filePath))
using (var container = Build(Options.ResolveDialogFromContainer | Options.Reflection))
{
var builder = new ContainerBuilder();
builder
.RegisterInstance(dialog)
.AsSelf()
.As<IDialog<object>>();
builder.Update(container);
await RecordScript(container, proactive, stream, null, inputs);
}
}
public static async Task VerifyDialogScript<T>(string filePath, IDialog<T> dialog, bool proactive, params string[] inputs)
{
var newPath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + "-new" + Path.GetExtension(filePath));
File.Delete(newPath);
try
{
using (var stream = new StreamReader(filePath))
using (var container = Build(Options.ResolveDialogFromContainer | Options.Reflection))
{
var builder = new ContainerBuilder();
builder
.RegisterInstance(dialog)
.AsSelf()
.As<IDialog<object>>();
builder.Update(container);
await VerifyScript(container, proactive, stream, null, inputs);
}
}
catch (Exception)
{
// There was an error, so record new script and pass on error
await RecordDialogScript(newPath, dialog, proactive, inputs);
throw;
}
}
}
}