-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathbacktrackToEmpty.html
More file actions
302 lines (248 loc) · 10.7 KB
/
Copy pathbacktrackToEmpty.html
File metadata and controls
302 lines (248 loc) · 10.7 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!doctype html>
<html lang="en-US">
<head>
<link href="/assets/index.css" rel="stylesheet" type="text/css" />
<link href="./css/pauseAnimation.css" rel="stylesheet" type="text/css" />
<script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
<script crossorigin="anonymous" src="/test-harness.js"></script>
<script crossorigin="anonymous" src="/test-page-object.js"></script>
<script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
</head>
<body>
<main id="webchat"></main>
<script type="module">
const streamingFormat = new URL(window.location.href).searchParams.get('streamingFormat') ?? 'channelData'; // can be entity or channelData
function addLivestreamingMetadata(activity, livestreamingMetadata) {
return streamingFormat === 'entity'
? {
...activity,
entities: [...(activity.entities ?? []), { ...livestreamingMetadata, type: 'streaminfo' }]
}
: {
...activity,
channelData: { ...activity.channelData, ...livestreamingMetadata }
};
}
run(async function () {
const {
React: { createElement },
ReactDOM: { render },
WebChat: {
Components: { BasicWebChat, Composer },
decorator: { WebChatDecorator },
hooks: { useActiveTyping, useActivityKeys, useGetActivitiesByKey }
}
} = window; // Imports in UMD fashion.
const { directLine, store } = testHelpers.createDirectLineEmulator();
let currentActivityKeysWithId = [];
let currentActiveTyping = {};
const Monitor = () => {
const [activityKeys] = useActivityKeys();
const [activeTyping] = useActiveTyping();
const getActivitiesByKey = useGetActivitiesByKey();
currentActivityKeysWithId = Object.freeze(
activityKeys.map(key => [key, getActivitiesByKey(key).map(({ id }) => id)])
);
currentActiveTyping = activeTyping;
return false;
};
const styleOptions = {
// Non-animated typing indicator.
typingAnimationBackgroundImage: `url('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAUACgDASIAAhEBAxEB/8QAGgABAQACAwAAAAAAAAAAAAAAAAYCBwMFCP/EACsQAAECBQIEBQUAAAAAAAAAAAECAwAEBQYRBxITIjFBMlFhccFScoGh8f/EABQBAQAAAAAAAAAAAAAAAAAAAAD/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwD0lctx023JVD9UeKOIcNoSNylkdcCMbauSmXHLOPUx8r4ZAcQtO1SM9Mj5iO1gtWo1syc7S2zMKYSptbIPNgnII8/5HBpRZ9RpaKjNVVCpUzLPAQ1nmA7qPl6fmAondRrcaqhkVTiiQrYXgglsH7vnpHc3DcNNoEimaqT4Q2s4bCRuUs+gEaLd05uNFVMmiS3o3YEwFDhlP1Z7e3WLzUuzahUKHRk0zM07TmeApvOFLGEjcM9+Xp6wFnbN0Uu5GnF0x4qW1je2tO1Sc9Djy9oRD6QWlU6PPzVSqjRlgtksttKPMcqBKiO3h/cIDacIQgEIQgEIQgP/2Q==')`
};
render(
createElement(
WebChatDecorator,
{},
createElement(
Composer,
{ directLine, store, styleOptions },
createElement(BasicWebChat),
createElement(Monitor)
)
),
document.getElementById('webchat')
);
await pageConditions.uiConnected();
// WHEN: Bot send a normal message.
await directLine.emulateIncomingActivity({
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
id: 'a-00001',
timestamp: 1,
text: 'Hello, World!',
type: 'message'
});
// THEN: Should display the message.
await pageConditions.numActivitiesShown(1);
// THEN: Should not display typing indicator.
expect(pageElements.typingIndicator()).toBeFalsy();
// THEN: Should match snapshot.
await host.snapshot('local');
// ---
// WHEN: Bot is sending a typing indicator in a livestream.
const firstTypingActivityId = 't-00001';
await directLine.emulateIncomingActivity(
addLivestreamingMetadata(
{
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
id: firstTypingActivityId,
timestamp: 2,
type: 'typing'
},
{ streamSequence: 1, streamType: 'streaming' }
)
);
let firstTypingActivityKey = currentActivityKeysWithId[1][0];
// ---
// THEN: Should not display the livestream activity.
await pageConditions.numActivitiesShown(1);
// THEN: Should display typing indicator.
expect(pageElements.typingIndicator()).toBeTruthy();
// THEN: Should match snapshot.
await host.snapshot('local');
// THEN: Should have 2 activity keys.
expect(currentActivityKeysWithId).toEqual([
[expect.any(String), ['a-00001']],
[firstTypingActivityKey, ['t-00001']]
]);
// THEN: Should have active typing.
expect(currentActiveTyping).toEqual({
'u-00001': {
at: expect.any(Number),
expireAt: expect.any(Number),
name: 'Bot',
role: 'bot',
type: 'busy'
}
});
// ---
// THEN: Should display typing indicator.
expect(pageElements.typingIndicator()).toBeTruthy();
// TBD: [DONE] Add more tests. Having a normal typing indicator, while having a livestream-based typing indicator, the typing indicator must be kept
// TBD: [DONE] Add more tests. Concluding a livestream with "message" activity with empty "text" field.
// TBD: [DONE] Add more tests. 2 simultaneous livestream is ongoing. One is empty, one is contentful.
// - While both is going, it should not show typing indicator
// - When the contentful livestream concluded, it should show the typing indicator
// - In other words, typing indicator is the "last resort" to show the bot is progressing
// ---
// WHEN: Bot is typing a message.
await directLine.emulateIncomingActivity(
addLivestreamingMetadata(
{
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
id: 't-00002',
text: 'I will delete this line later.',
timestamp: 3,
type: 'typing'
},
{ streamId: 't-00001', streamSequence: 2, streamType: 'streaming' }
)
);
// THEN: Should display the livestream activity.
await pageConditions.numActivitiesShown(2);
expect(pageElements.activityContents()[1]).toHaveProperty('textContent', 'I will delete this line later.');
// THEN: Should not display typing indicator.
expect(pageElements.typingIndicator()).toBeFalsy();
// THEN: Should match snapshot.
await host.snapshot('local');
// THEN: Should have 2 activity keys only.
expect(currentActivityKeysWithId).toEqual([
[expect.any(String), ['a-00001']],
[firstTypingActivityKey, ['t-00001', 't-00002']]
]);
// THEN: Should have active typing.
expect(currentActiveTyping).toEqual({
'u-00001': {
at: expect.any(Number),
expireAt: expect.any(Number),
name: 'Bot',
role: 'bot',
type: 'livestream'
}
});
// ---
// WHEN: Bot send interim message without content.
await directLine.emulateIncomingActivity(
addLivestreamingMetadata(
{
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
id: 't-00003',
timestamp: 4,
type: 'typing'
},
{ streamId: firstTypingActivityId, streamSequence: 3, streamType: 'streaming' }
)
);
// THEN: Should not display the livestream activity.
await pageConditions.numActivitiesShown(1);
// THEN: Should display typing indicator because the livestream is empty.
expect(pageElements.typingIndicator()).toBeTruthy();
// THEN: Should match snapshot.
await host.snapshot('local');
// THEN: Should have 2 activity keys only.
expect(currentActivityKeysWithId).toEqual([
[expect.any(String), ['a-00001']],
[firstTypingActivityKey, ['t-00001', 't-00002', 't-00003']]
]);
// THEN: Should have active typing.
expect(currentActiveTyping).toEqual({
'u-00001': {
at: expect.any(Number),
expireAt: expect.any(Number),
name: 'Bot',
role: 'bot',
type: 'busy'
}
});
// ---
// WHEN: Bot send final message as typing, without content.
await directLine.emulateIncomingActivity(
addLivestreamingMetadata(
{
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
id: 't-00004',
timestamp: 5,
type: 'typing'
},
{ streamId: firstTypingActivityId, streamType: 'final' }
)
);
// THEN: Should display no messages.
await pageConditions.numActivitiesShown(1);
// THEN: Should not display typing indicator as the livestream is finalized.
expect(pageElements.typingIndicator()).toBeFalsy();
// THEN: Should match snapshot.
await host.snapshot('local');
// THEN: Should have 2 activity keys only.
expect(currentActivityKeysWithId).toEqual([
[expect.any(String), ['a-00001']],
[firstTypingActivityKey, ['t-00004']]
]);
// THEN: Should have no active typing.
expect(currentActiveTyping).toHaveProperty('u-00001', undefined);
// ---
// WHEN: Bot is sending another message.
await directLine.emulateIncomingActivity({
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
id: 'a-00002',
text: 'Aloha!',
timestamp: 6,
type: 'message'
});
// THEN: Should display 2 messages.
await pageConditions.numActivitiesShown(2);
// THEN: Should have 2 activity keys only.
expect(currentActivityKeysWithId).toEqual([
[expect.any(String), ['a-00001']],
[firstTypingActivityKey, ['t-00004']],
[expect.any(String), ['a-00002']]
]);
// THEN: Should not display typing indicator.
expect(pageElements.typingIndicator()).toBeFalsy();
// THEN: Should match snapshot.
await host.snapshot('local');
});
</script>
</body>
</html>