1+ import type { ArraySlice } from 'type-fest' ;
12import { type WebChatActivity } from '../types/WebChatActivity' ;
23import getActivityLivestreamingMetadata from './getActivityLivestreamingMetadata' ;
34
5+ function injectInto < T > (
6+ where : 'channelData' | 'entities' ,
7+ metadata : T ,
8+ activity : Partial < WebChatActivity >
9+ ) : WebChatActivity {
10+ return {
11+ ...( where === 'entities' ? { entities : [ { ...metadata , type : 'streaminfo' } ] } : { channelData : metadata } ) ,
12+ ...activity
13+ } as WebChatActivity ;
14+ }
15+
416describe . each ( [ [ 'channelData' as const ] , [ 'entities' as const ] ] ) ( 'using %s' , where => {
17+ let inject : ( ...args : ArraySlice < Parameters < typeof injectInto > , 1 > ) => ReturnType < typeof injectInto > ;
18+
19+ beforeEach ( ( ) => {
20+ inject = injectInto . bind ( undefined , where ) ;
21+ } ) ;
22+
523 describe . each ( [ [ 'with "streamId"' as const ] , [ 'without "streamId"' as const ] ] ) ( 'activity %s' , variant => {
624 describe ( 'activity with "streamType" of "streaming"' , ( ) => {
725 let activity : WebChatActivity ;
826
927 beforeEach ( ( ) => {
10- const metadata = {
11- ... ( variant === 'with "streamId"' ? { streamId : 'a-00001' } : { } ) ,
12- streamSequence : 1 ,
13- streamType : 'streaming'
14- } ;
15-
16- activity = {
17- ... ( where === 'entities' ? { entities : [ { ... metadata , type : 'streaminfo' } ] } : { channelData : metadata } ) ,
18- id : 'a-00002 ' ,
19- text : 'Hello, World!' ,
20- type : 'typing'
21- } as any ;
28+ activity = inject (
29+ {
30+ ... ( variant === 'with "streamId"' ? { streamId : 'a-00001' } : { } ) ,
31+ streamSequence : 1 ,
32+ streamType : 'streaming'
33+ } ,
34+ {
35+ id : 'a-00002' ,
36+ text : 'Hello, World! ' ,
37+ type : 'typing'
38+ }
39+ ) ;
2240 } ) ;
2341
2442 test ( 'should return type of "interim activity"' , ( ) =>
@@ -39,18 +57,18 @@ describe.each([['channelData' as const], ['entities' as const]])('using %s', whe
3957 let activity : WebChatActivity ;
4058
4159 beforeEach ( ( ) => {
42- const metadata = {
43- ... ( variant === 'with "streamId"' ? { streamId : 'a-00001' } : { } ) ,
44- streamSequence : 1 ,
45- streamType : 'informative'
46- } ;
47-
48- activity = {
49- ... ( where === 'entities' ? { entities : [ { ... metadata , type : 'streaminfo' } ] } : { channelData : metadata } ) ,
50- id : 'a-00002 ' ,
51- text : 'Hello, World!' ,
52- type : 'typing'
53- } as any ;
60+ activity = inject (
61+ {
62+ ... ( variant === 'with "streamId"' ? { streamId : 'a-00001' } : { } ) ,
63+ streamSequence : 1 ,
64+ streamType : 'informative'
65+ } ,
66+ {
67+ id : 'a-00002' ,
68+ text : 'Hello, World! ' ,
69+ type : 'typing'
70+ }
71+ ) ;
5472 } ) ;
5573
5674 test ( 'should return type of "informative message"' , ( ) =>
@@ -71,17 +89,17 @@ describe.each([['channelData' as const], ['entities' as const]])('using %s', whe
7189 let activity : WebChatActivity ;
7290
7391 beforeEach ( ( ) => {
74- const metadata = {
75- ... ( variant === 'with "streamId"' ? { streamId : 'a-00001' } : { } ) ,
76- streamType : 'final'
77- } ;
78-
79- activity = {
80- ... ( where === 'entities' ? { entities : [ { ... metadata , type : 'streaminfo' } ] } : { channelData : metadata } ) ,
81- id : 'a-00002 ' ,
82- text : 'Hello, World!' ,
83- type : 'message'
84- } as any ;
92+ activity = inject (
93+ {
94+ ... ( variant === 'with "streamId"' ? { streamId : 'a-00001' } : { } ) ,
95+ streamType : 'final'
96+ } ,
97+ {
98+ id : 'a-00002' ,
99+ text : 'Hello, World! ' ,
100+ type : 'message'
101+ }
102+ ) ;
85103 } ) ;
86104
87105 if ( variant === 'with "streamId"' ) {
@@ -114,14 +132,18 @@ describe.each([['channelData' as const], ['entities' as const]])('using %s', whe
114132 [ 'zero' , 0 , false ] ,
115133 [ 'decimal' , 1.234 , false ]
116134 ] ) ( 'activity with %s "streamSequence" should return undefined' , ( _ , streamSequence , isValid ) => {
117- const metadata = { streamId : 'a-00001' , streamSequence, streamType : 'streaming' } ;
118-
119- const activity = {
120- ...( where === 'entities' ? { entities : [ { ...metadata , type : 'streaminfo' } ] } : { channelData : metadata } ) ,
121- id : 'a-00002' ,
122- text : '' ,
123- type : 'typing'
124- } as any ;
135+ const activity = inject (
136+ {
137+ streamId : 'a-00001' ,
138+ streamSequence,
139+ streamType : 'streaming'
140+ } ,
141+ {
142+ id : 'a-00002' ,
143+ text : '' ,
144+ type : 'typing'
145+ }
146+ ) ;
125147
126148 if ( isValid ) {
127149 expect ( getActivityLivestreamingMetadata ( activity ) ) . toBeTruthy ( ) ;
@@ -132,41 +154,56 @@ describe.each([['channelData' as const], ['entities' as const]])('using %s', whe
132154
133155 describe ( '"typing" activity with "streamType" of "final"' , ( ) => {
134156 test ( 'should return undefined if "text" field is defined' , ( ) => {
135- const metadata = { streamId : 'a-00001' , streamType : 'final' } ;
136-
137157 expect (
138- getActivityLivestreamingMetadata ( {
139- ...( where === 'entities' ? { entities : [ { ...metadata , type : 'streaminfo' } ] } : { channelData : metadata } ) ,
140- id : 'a-00002' ,
141- text : 'Final "typing" activity, must not have "text".' ,
142- type : 'typing'
143- } as any )
158+ getActivityLivestreamingMetadata (
159+ inject (
160+ {
161+ streamId : 'a-00001' ,
162+ streamType : 'final'
163+ } ,
164+ {
165+ id : 'a-00002' ,
166+ text : 'Final "typing" activity, must not have "text".' ,
167+ type : 'typing'
168+ }
169+ )
170+ )
144171 ) . toBeUndefined ( ) ;
145172 } ) ;
146173
147174 test ( 'should return truthy if "text" field is not defined' , ( ) => {
148- const metadata = { streamId : 'a-00001' , streamType : 'final' } ;
149-
150175 expect (
151- getActivityLivestreamingMetadata ( {
152- ...( where === 'entities' ? { entities : [ { ...metadata , type : 'streaminfo' } ] } : { channelData : metadata } ) ,
153- id : 'a-00002' ,
154- // Final activity can be "typing" if it does not have "text".
155- type : 'typing'
156- } as any )
176+ getActivityLivestreamingMetadata (
177+ inject (
178+ {
179+ streamId : 'a-00001' ,
180+ streamType : 'final'
181+ } ,
182+ {
183+ id : 'a-00002' ,
184+ // Final activity can be "typing" if it does not have "text".
185+ type : 'typing'
186+ }
187+ )
188+ )
157189 ) . toHaveProperty ( 'type' , 'final activity' ) ;
158190 } ) ;
159191 } ) ;
160192
161193 test ( 'activity with "streamType" of "streaming" without "content" should return type of "contentless"' , ( ) => {
162- const metadata = { streamSequence : 1 , streamType : 'streaming' } ;
163-
164194 expect (
165- getActivityLivestreamingMetadata ( {
166- ...( where === 'entities' ? { entities : [ { ...metadata , type : 'streaminfo' } ] } : { channelData : metadata } ) ,
167- id : 'a-00001' ,
168- type : 'typing'
169- } as any )
195+ getActivityLivestreamingMetadata (
196+ inject (
197+ {
198+ streamSequence : 1 ,
199+ streamType : 'streaming'
200+ } ,
201+ {
202+ id : 'a-00001' ,
203+ type : 'typing'
204+ }
205+ )
206+ )
170207 ) . toHaveProperty ( 'type' , 'contentless' ) ;
171208 } ) ;
172209} ) ;
0 commit comments