Repository navigation
Expand file tree
/
Copy pathsdk.test.ts
More file actions
492 lines (434 loc) · 14.1 KB
/
Copy pathsdk.test.ts
File metadata and controls
492 lines (434 loc) · 14.1 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals';
import type { AppState } from './config';
import { appState } from './config';
import { createAngieInstance, resetInstancesForTests } from './instance-registry';
import {
bindAngieTriggerChannel,
flushPendingSdkMessages,
registerSdkInstance,
resetTriggerChannelsForTests,
startSdkMessageRouting,
triggerAngieOnInstance,
unregisterSdkInstance,
} from './sdk';
import { MessageEventType } from './types';
const ANGIE_ORIGIN = 'https://angie.elementor.com';
const registeredInstances: AppState[] = [];
const registerForRouting = ( instance: AppState ): void => {
registerSdkInstance( instance );
registeredInstances.push( instance );
startSdkMessageRouting();
};
const attachIframe = ( instance: AppState ) => {
const postMessage = jest.fn();
instance.iframeUrlObject = new URL( `${ ANGIE_ORIGIN }/angie/embedded` );
instance.iframe = { contentWindow: { postMessage } } as unknown as HTMLIFrameElement;
return postMessage;
};
const latestTriggerPort = (): { postMessage: jest.Mock; onmessage: ( ( event: MessageEvent ) => void ) | null } => {
const results = ( global.MessageChannel as jest.Mock ).mock.results;
const channel = results[ results.length - 1 ]?.value as { port1: { postMessage: jest.Mock; onmessage: ( ( event: MessageEvent ) => void ) | null } };
return channel.port1;
};
const replyToTrigger = ( requestId: string ): void => {
latestTriggerPort().onmessage?.( {
data: {
type: MessageEventType.SDK_TRIGGER_ANGIE_RESPONSE,
payload: {
success: true,
requestId,
response: 'Angie triggered successfully',
},
},
} as MessageEvent );
};
const emitHostMessage = ( data: unknown ): void => {
window.dispatchEvent( Object.assign( new Event( 'message' ), {
origin: window.location.origin,
source: window,
data,
ports: [ { postMessage: jest.fn() } ],
} ) );
};
describe( 'sdk', () => {
beforeEach( () => {
resetInstancesForTests();
jest.clearAllMocks();
} );
afterEach( () => {
for ( const instance of registeredInstances.splice( 0 ) ) {
unregisterSdkInstance( instance );
}
resetTriggerChannelsForTests();
} );
it( 'should forward a window trigger to the matching iframe', () => {
const first = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
const second = createAngieInstance( {
containerId: 'container-b',
instanceId: 'bbbbbb',
layout: 'floatingChat',
} );
const firstPostMessage = attachIframe( first );
const secondPostMessage = attachIframe( second );
registerForRouting( first );
registerForRouting( second );
emitHostMessage( {
type: MessageEventType.SDK_TRIGGER_ANGIE,
payload: { instanceId: 'aaaaaa', requestId: 'req-2', prompt: 'hello' },
} );
expect( firstPostMessage ).toHaveBeenCalledWith(
expect.objectContaining( { type: MessageEventType.SDK_TRIGGER_ANGIE } ),
ANGIE_ORIGIN,
);
expect( secondPostMessage ).not.toHaveBeenCalled();
} );
it( 'should send a trigger only on the private port of the addressed instance', async () => {
const first = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
const second = createAngieInstance( {
containerId: 'container-b',
instanceId: 'bbbbbb',
layout: 'floatingChat',
} );
const firstPostMessage = attachIframe( first );
const secondPostMessage = attachIframe( second );
bindAngieTriggerChannel( first );
const pending = triggerAngieOnInstance( first, {
requestId: 'req-2',
instanceId: 'aaaaaa',
prompt: 'hello',
} );
replyToTrigger( 'req-2' );
await expect( pending ).resolves.toEqual( {
success: true,
requestId: 'req-2',
response: 'Angie triggered successfully',
} );
expect( firstPostMessage ).toHaveBeenCalledTimes( 1 );
expect( firstPostMessage ).toHaveBeenCalledWith(
expect.objectContaining( { type: MessageEventType.SDK_INIT_TRIGGER_ANGIE } ),
ANGIE_ORIGIN,
expect.any( Array ),
);
expect( secondPostMessage ).not.toHaveBeenCalled();
expect( latestTriggerPort().postMessage ).toHaveBeenCalledWith(
expect.objectContaining( {
type: MessageEventType.SDK_TRIGGER_ANGIE,
payload: expect.objectContaining( { prompt: 'hello', instanceId: 'aaaaaa' } ),
} ),
);
} );
it( 'should relay a context attachment unchanged on the private port', async () => {
const instance = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
attachIframe( instance );
bindAngieTriggerChannel( instance );
const contextAttachment = {
label: 'Selected error',
content: 'Checkout failed with error code PAYMENT_DECLINED.',
};
const pending = triggerAngieOnInstance( instance, {
requestId: 'request-123',
instanceId: 'aaaaaa',
contextAttachment,
} );
replyToTrigger( 'request-123' );
await pending;
const relayedMessage = latestTriggerPort().postMessage.mock.calls[ 0 ][ 0 ] as {
payload: {
contextAttachment?: typeof contextAttachment;
prompt?: string;
};
};
expect( relayedMessage.payload.contextAttachment ).toBe( contextAttachment );
expect( relayedMessage.payload.prompt ).toBeUndefined();
} );
it( 'should relay conversation-starter suggestions on the private port', async () => {
const instance = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
attachIframe( instance );
bindAngieTriggerChannel( instance );
const suggestions = { items: [ { label: 'Make it longer', value: 'Make it longer' } ] };
const pending = triggerAngieOnInstance( instance, {
requestId: 'request-123',
instanceId: 'aaaaaa',
suggestions,
} );
replyToTrigger( 'request-123' );
await pending;
const relayedMessage = latestTriggerPort().postMessage.mock.calls[ 0 ][ 0 ] as {
payload: { suggestions?: typeof suggestions };
};
expect( relayedMessage.payload.suggestions ).toBe( suggestions );
} );
it( 'should reuse the same port for a second trigger', async () => {
const instance = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
const postMessage = attachIframe( instance );
bindAngieTriggerChannel( instance );
const firstTrigger = triggerAngieOnInstance( instance, { requestId: 'req-1', instanceId: 'aaaaaa', prompt: 'one' } );
const secondTrigger = triggerAngieOnInstance( instance, { requestId: 'req-2', instanceId: 'aaaaaa', prompt: 'two' } );
replyToTrigger( 'req-1' );
replyToTrigger( 'req-2' );
await Promise.all( [ firstTrigger, secondTrigger ] );
expect( postMessage ).toHaveBeenCalledTimes( 1 );
expect( latestTriggerPort().postMessage ).toHaveBeenCalledTimes( 2 );
} );
it( 'should report failure when the iframe is missing', async () => {
const instance = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
const response = await triggerAngieOnInstance( instance, {
requestId: 'req-missing',
instanceId: 'aaaaaa',
prompt: 'hello',
} );
expect( response ).toEqual( {
success: false,
requestId: 'req-missing',
error: 'Iframe not found',
} );
} );
it( 'should not forward client creation to a sibling while the addressed instance is still booting', () => {
const first = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
const second = createAngieInstance( {
containerId: 'container-b',
instanceId: 'bbbbbb',
layout: 'floatingChat',
} );
const firstPostMessage = attachIframe( first );
registerForRouting( first );
registerForRouting( second );
emitHostMessage( {
type: MessageEventType.SDK_REQUEST_CLIENT_CREATION,
payload: {
instanceId: 'bbbbbb',
serverId: 'server-1',
serverName: 'Test Server',
description: 'A test server',
serverVersion: '1.0.0',
transport: 'postMessage',
requestId: 'req-1',
},
} );
expect( firstPostMessage ).not.toHaveBeenCalled();
} );
it( 'should forward buffered client creation after the iframe appears', () => {
const instance = createAngieInstance( {
containerId: 'container-b',
instanceId: 'bbbbbb',
layout: 'floatingChat',
} );
registerForRouting( instance );
emitHostMessage( {
type: MessageEventType.SDK_REQUEST_CLIENT_CREATION,
payload: {
instanceId: 'bbbbbb',
serverId: 'server-1',
serverName: 'Test Server',
description: 'A test server',
serverVersion: '1.0.0',
transport: 'postMessage',
requestId: 'req-1',
},
} );
const postMessage = attachIframe( instance );
flushPendingSdkMessages( instance );
expect( postMessage ).toHaveBeenCalledWith(
expect.objectContaining( { type: MessageEventType.SDK_REQUEST_CLIENT_CREATION } ),
ANGIE_ORIGIN,
expect.any( Array ),
);
} );
it( 'should not double-forward when registerSdkInstance is called twice for the same instance', () => {
const instance = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
const postMessage = attachIframe( instance );
registerForRouting( instance );
registerSdkInstance( instance );
emitHostMessage( {
type: MessageEventType.SDK_REQUEST_CLIENT_CREATION,
payload: {
instanceId: 'aaaaaa',
serverId: 'server-1',
serverName: 'Test Server',
description: 'A test server',
serverVersion: '1.0.0',
transport: 'postMessage',
requestId: 'req-2',
},
} );
expect( postMessage ).toHaveBeenCalledTimes( 1 );
} );
it( 'should forward unaddressed host messages for V1 loadSidebar when the registry is empty', () => {
appState.instanceId = 'v1-id';
const postMessage = attachIframe( appState );
registerForRouting( appState );
emitHostMessage( {
type: MessageEventType.SDK_REQUEST_CLIENT_CREATION,
payload: {
serverId: 'server-1',
serverName: 'Test Server',
description: 'A test server',
serverVersion: '1.0.0',
transport: 'postMessage',
requestId: 'req-v1',
},
} );
expect( postMessage ).toHaveBeenCalledWith(
expect.objectContaining( { type: MessageEventType.SDK_REQUEST_CLIENT_CREATION } ),
ANGIE_ORIGIN,
expect.any( Array ),
);
} );
it( 'should stop routing to an unregistered instance while siblings keep listening', () => {
const first = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
const second = createAngieInstance( {
containerId: 'container-b',
instanceId: 'bbbbbb',
layout: 'floatingChat',
} );
const firstPostMessage = attachIframe( first );
const secondPostMessage = attachIframe( second );
registerForRouting( first );
registerForRouting( second );
unregisterSdkInstance( first );
registeredInstances.splice( registeredInstances.indexOf( first ), 1 );
emitHostMessage( {
type: MessageEventType.SDK_REQUEST_CLIENT_CREATION,
payload: {
instanceId: 'aaaaaa',
serverId: 'server-1',
serverName: 'Test Server',
description: 'A test server',
serverVersion: '1.0.0',
transport: 'postMessage',
requestId: 'req-2',
},
} );
emitHostMessage( {
type: MessageEventType.SDK_REQUEST_CLIENT_CREATION,
payload: {
instanceId: 'bbbbbb',
serverId: 'server-1',
serverName: 'Test Server',
description: 'A test server',
serverVersion: '1.0.0',
transport: 'postMessage',
requestId: 'req-3',
},
} );
expect( firstPostMessage ).not.toHaveBeenCalled();
expect( secondPostMessage ).toHaveBeenCalledWith(
expect.objectContaining( { type: MessageEventType.SDK_REQUEST_CLIENT_CREATION } ),
ANGIE_ORIGIN,
expect.any( Array ),
);
} );
it( 'should stop routing after unregisterSdkInstance removes the last instance', () => {
const instance = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
const postMessage = attachIframe( instance );
registerForRouting( instance );
unregisterSdkInstance( instance );
registeredInstances.length = 0;
emitHostMessage( {
type: MessageEventType.SDK_REQUEST_CLIENT_CREATION,
payload: {
instanceId: 'aaaaaa',
serverId: 'server-1',
serverName: 'Test Server',
description: 'A test server',
serverVersion: '1.0.0',
transport: 'postMessage',
requestId: 'req-2',
},
} );
expect( postMessage ).not.toHaveBeenCalled();
} );
it( 'should hand a replaced iframe a fresh port', async () => {
const instance = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
const firstPostMessage = attachIframe( instance );
bindAngieTriggerChannel( instance );
const firstTrigger = triggerAngieOnInstance( instance, { requestId: 'req-1', instanceId: 'aaaaaa', prompt: 'one' } );
replyToTrigger( 'req-1' );
await firstTrigger;
const secondPostMessage = attachIframe( instance );
bindAngieTriggerChannel( instance );
const secondTrigger = triggerAngieOnInstance( instance, { requestId: 'req-2', instanceId: 'aaaaaa', prompt: 'two' } );
replyToTrigger( 'req-2' );
await secondTrigger;
expect( firstPostMessage ).toHaveBeenCalledTimes( 1 );
expect( secondPostMessage ).toHaveBeenCalledTimes( 1 );
expect( secondPostMessage ).toHaveBeenCalledWith(
expect.objectContaining( { type: MessageEventType.SDK_INIT_TRIGGER_ANGIE } ),
ANGIE_ORIGIN,
expect.any( Array ),
);
} );
it( 'should report failure when the private port send throws', async () => {
const instance = createAngieInstance( {
containerId: 'container-a',
instanceId: 'aaaaaa',
layout: 'sidebar',
} );
attachIframe( instance );
( global.MessageChannel as jest.Mock ).mockImplementationOnce( () => ( {
port1: {
postMessage: () => {
throw new Error( 'port failed' );
},
close: jest.fn(),
start: jest.fn(),
},
port2: { postMessage: jest.fn(), close: jest.fn(), start: jest.fn() },
} ) );
bindAngieTriggerChannel( instance );
const response = await triggerAngieOnInstance( instance, {
requestId: 'req-throw',
instanceId: 'aaaaaa',
prompt: 'hello',
} );
expect( response ).toEqual( {
success: false,
requestId: 'req-throw',
error: 'port failed',
} );
} );
} );