Skip to content

Commit c53ea48

Browse files
authored
Merge pull request #1 from microsoft/benbr-fix-streaming-behavior
Clean up PR for #5609
2 parents 019ae5c + 6bd0d75 commit c53ea48

21 files changed

Lines changed: 897 additions & 757 deletions

__tests__/html2/livestream/activityOrder.html

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212
<body>
1313
<main id="webchat"></main>
1414
<script type="module">
15+
const streamingFormat = new URL(window.location.href).searchParams.get('streamingFormat') ?? 'channelData'; // can be entity or channelData
16+
17+
function addLivestreamingMetadata(activity, livestreamingMetadata) {
18+
return streamingFormat === 'entity'
19+
? {
20+
...activity,
21+
entities: [...(activity.entities ?? []), { ...livestreamingMetadata, type: 'streaminfo' }]
22+
}
23+
: {
24+
...activity,
25+
channelData: { ...activity.channelData, ...livestreamingMetadata }
26+
};
27+
}
28+
1529
run(async function () {
1630
const {
1731
React: { createElement },
@@ -21,27 +35,11 @@
2135
decorator: { WebChatDecorator },
2236
hooks: { useActivityKeys, useGetActivitiesByKey }
2337
}
24-
} = window; // Imports in UMD fashion.pkve
38+
} = window; // Imports in UMD fashion.
2539

2640
const { directLine, store } = testHelpers.createDirectLineEmulator();
2741
let currentActivityKeysWithId = [];
2842

29-
const url = new URL(window.location.href);
30-
const streamingFormat = url.searchParams.get('streamingFormat') ?? 'channelData'; // can be entity or channelData
31-
32-
const makeActivity = (input, stream) => {
33-
if (!stream) return input;
34-
if (streamingFormat == 'entity') {
35-
input.entities = [{
36-
...stream,
37-
type: 'streaminfo'
38-
}]
39-
} else {
40-
input.channelData = stream;
41-
}
42-
return input;
43-
}
44-
4543
const Monitor = () => {
4644
const [activityKeys] = useActivityKeys();
4745
const getActivitiesByKey = useGetActivitiesByKey();
@@ -65,27 +63,28 @@
6563
await pageConditions.uiConnected();
6664

6765
// SETUP: Bot sent a message.
68-
await directLine.emulateIncomingActivity(
69-
makeActivity({
70-
id: 'a-00001',
71-
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
72-
text: 'Adipisicing cupidatat eu Lorem anim ut aute magna occaecat id cillum.',
73-
type: 'message'
74-
})
75-
);
66+
await directLine.emulateIncomingActivity({
67+
id: 'a-00001',
68+
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
69+
text: 'Adipisicing cupidatat eu Lorem anim ut aute magna occaecat id cillum.',
70+
type: 'message'
71+
});
7672

7773
let firstActivityKey = currentActivityKeysWithId[0][0];
7874

7975
// WHEN: Bot is typing a message.
8076
const firstTypingActivityId = 't-00001';
8177

8278
await directLine.emulateIncomingActivity(
83-
makeActivity({
84-
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
85-
id: firstTypingActivityId,
86-
text: 'A quick',
87-
type: 'typing'
88-
}, { streamSequence: 1, streamType: 'streaming' })
79+
addLivestreamingMetadata(
80+
{
81+
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
82+
id: firstTypingActivityId,
83+
text: 'A quick',
84+
type: 'typing'
85+
},
86+
{ streamSequence: 1, streamType: 'streaming' }
87+
)
8988
);
9089

9190
let secondActivityKey = currentActivityKeysWithId[1][0];
@@ -109,14 +108,12 @@
109108
// ---
110109

111110
// WHEN: Bot send another message.
112-
await directLine.emulateIncomingActivity(
113-
makeActivity({
114-
id: 'a-00003',
115-
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
116-
text: 'Amet consequat enim incididunt excepteur aliquip magna duis et tempor.',
117-
type: 'message'
118-
})
119-
);
111+
await directLine.emulateIncomingActivity({
112+
id: 'a-00003',
113+
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
114+
text: 'Amet consequat enim incididunt excepteur aliquip magna duis et tempor.',
115+
type: 'message'
116+
});
120117

121118
let thirdActivityKey = currentActivityKeysWithId[2][0];
122119

@@ -145,12 +142,15 @@
145142

146143
// WHEN: Bot continue typing the message.
147144
await directLine.emulateIncomingActivity(
148-
makeActivity({
149-
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
150-
id: 't-00002',
151-
text: 'A quick brown fox',
152-
type: 'typing'
153-
}, { streamId: firstTypingActivityId, streamSequence: 2, streamType: 'streaming' })
145+
addLivestreamingMetadata(
146+
{
147+
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
148+
id: 't-00002',
149+
text: 'A quick brown fox',
150+
type: 'typing'
151+
},
152+
{ streamId: firstTypingActivityId, streamSequence: 2, streamType: 'streaming' }
153+
)
154154
);
155155

156156
// THEN: Should display 3 messages, the order should kept the same.
@@ -178,12 +178,15 @@
178178

179179
// WHEN: Bot continue typing the message.
180180
await directLine.emulateIncomingActivity(
181-
makeActivity({
182-
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
183-
id: 't-00003',
184-
text: 'A quick brown fox jumped over',
185-
type: 'typing'
186-
}, { streamId: firstTypingActivityId, streamSequence: 3, streamType: 'streaming' })
181+
addLivestreamingMetadata(
182+
{
183+
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
184+
id: 't-00003',
185+
text: 'A quick brown fox jumped over',
186+
type: 'typing'
187+
},
188+
{ streamId: firstTypingActivityId, streamSequence: 3, streamType: 'streaming' }
189+
)
187190
);
188191

189192
// THEN: Should display 3 messages, the order should kept the same.
@@ -211,12 +214,15 @@
211214

212215
// WHEN: Bot finished typing the message.
213216
await directLine.emulateIncomingActivity(
214-
makeActivity({
215-
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
216-
id: 'a-00002',
217-
text: 'A quick brown fox jumped over the lazy dogs.',
218-
type: 'message'
219-
}, { streamId: firstTypingActivityId, streamType: 'final' })
217+
addLivestreamingMetadata(
218+
{
219+
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
220+
id: 'a-00002',
221+
text: 'A quick brown fox jumped over the lazy dogs.',
222+
type: 'message'
223+
},
224+
{ streamId: firstTypingActivityId, streamType: 'final' }
225+
)
220226
);
221227

222228
// THEN: Should display 3 messages.

__tests__/html2/livestream/attachmentWithoutText.html

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,20 @@
2424
const attachmentLayout =
2525
new URLSearchParams(location.search).get('layout') === 'carousel' ? 'carousel' : undefined;
2626

27-
const url = new URL(window.location.href);
28-
const streamingFormat = url.searchParams.get('streamingFormat') ?? 'channelData'; // can be entity or channelData
29-
30-
const makeActivity = (input, stream) => {
31-
if (!stream) return input;
32-
if (streamingFormat == 'entity') {
33-
input.entities = [{
34-
...stream,
35-
type: 'streaminfo'
36-
}]
37-
} else {
38-
input.channelData = stream;
39-
}
40-
return input;
41-
}
42-
27+
const streamingFormat = new URL(window.location.href).searchParams.get('streamingFormat') ?? 'channelData'; // can be entity or channelData
28+
29+
function addLivestreamingMetadata(activity, livestreamingMetadata) {
30+
return streamingFormat === 'entity'
31+
? {
32+
...activity,
33+
entities: [...(activity.entities ?? []), { ...livestreamingMetadata, type: 'streaminfo' }]
34+
}
35+
: {
36+
...activity,
37+
channelData: { ...activity.channelData, ...livestreamingMetadata }
38+
};
39+
}
40+
4341
run(async function () {
4442
const {
4543
React: { createElement },
@@ -71,19 +69,22 @@
7169
const firstTypingActivityId = crypto.randomUUID();
7270

7371
await directLine.emulateIncomingActivity(
74-
makeActivity({
75-
attachmentLayout,
76-
attachments: [
77-
{
78-
content: 'Card 1',
79-
contentType: 'text/plain'
80-
}
81-
],
82-
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
83-
id: firstTypingActivityId,
84-
timestamp: 1,
85-
type: 'typing'
86-
}, { streamSequence: 1, streamType: 'streaming' })
72+
addLivestreamingMetadata(
73+
{
74+
attachmentLayout,
75+
attachments: [
76+
{
77+
content: 'Card 1',
78+
contentType: 'text/plain'
79+
}
80+
],
81+
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
82+
id: firstTypingActivityId,
83+
timestamp: 1,
84+
type: 'typing'
85+
},
86+
{ streamSequence: 1, streamType: 'streaming' }
87+
)
8788
);
8889

8990
// THEN: Should display the activity.
@@ -100,23 +101,26 @@
100101

101102
// WHEN: Receive another attachment through livestreaming.
102103
await directLine.emulateIncomingActivity(
103-
makeActivity({
104-
attachmentLayout,
105-
attachments: [
106-
{
107-
content: 'Card 1',
108-
contentType: 'text/plain'
109-
},
110-
{
111-
content: 'Card 2',
112-
contentType: 'text/plain'
113-
}
114-
],
115-
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
116-
id: crypto.randomUUID(),
117-
timestamp: 2,
118-
type: 'typing'
119-
}, { streamId: firstTypingActivityId, streamSequence: 2, streamType: 'streaming' })
104+
addLivestreamingMetadata(
105+
{
106+
attachmentLayout,
107+
attachments: [
108+
{
109+
content: 'Card 1',
110+
contentType: 'text/plain'
111+
},
112+
{
113+
content: 'Card 2',
114+
contentType: 'text/plain'
115+
}
116+
],
117+
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
118+
id: crypto.randomUUID(),
119+
timestamp: 2,
120+
type: 'typing'
121+
},
122+
{ streamId: firstTypingActivityId, streamSequence: 2, streamType: 'streaming' }
123+
)
120124
);
121125

122126
// THEN: Should display the activity.
@@ -134,23 +138,26 @@
134138

135139
// WHEN: Finalized message is received.
136140
await directLine.emulateIncomingActivity(
137-
makeActivity({
138-
attachmentLayout,
139-
attachments: [
140-
{
141-
content: 'Card A',
142-
contentType: 'text/plain'
143-
},
144-
{
145-
content: 'Card B',
146-
contentType: 'text/plain'
147-
}
148-
],
149-
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
150-
id: crypto.randomUUID(),
151-
timestamp: 3,
152-
type: 'message'
153-
}, { streamId: firstTypingActivityId, streamType: 'final' })
141+
addLivestreamingMetadata(
142+
{
143+
attachmentLayout,
144+
attachments: [
145+
{
146+
content: 'Card A',
147+
contentType: 'text/plain'
148+
},
149+
{
150+
content: 'Card B',
151+
contentType: 'text/plain'
152+
}
153+
],
154+
from: { id: 'u-00001', name: 'Bot', role: 'bot' },
155+
id: crypto.randomUUID(),
156+
timestamp: 3,
157+
type: 'message'
158+
},
159+
{ streamId: firstTypingActivityId, streamType: 'final' }
160+
)
154161
);
155162

156163
// THEN: Should display the activity.

0 commit comments

Comments
 (0)