diff --git a/packages/genesys-web-messaging-tester/src/genesys/message-delayer/MessageDelayer.ts b/packages/genesys-web-messaging-tester/src/genesys/message-delayer/MessageDelayer.ts index df25c9a..b6d36b8 100644 --- a/packages/genesys-web-messaging-tester/src/genesys/message-delayer/MessageDelayer.ts +++ b/packages/genesys-web-messaging-tester/src/genesys/message-delayer/MessageDelayer.ts @@ -1,6 +1,7 @@ import { EventEmitter } from 'events'; import { Response } from '../Response'; import { orderByTimestamp } from './orderByTimestamp'; +import debug from 'debug'; /** * Provides the ability to delay messages for the purpose of re-ordering them. @@ -16,6 +17,8 @@ export interface MessageDelayer extends EventEmitter { * timestamps. */ export class ReorderedMessageDelayer extends EventEmitter implements MessageDelayer { + private static readonly debugger = debug('ReorderedMessageDelayer'); + private static readonly DELAY_IN_MS = 2000; private messages: Response[] = []; @@ -58,7 +61,13 @@ export class ReorderedMessageDelayer extends EventEmitter implements MessageDela private releaseOldestMessage(): void { if (this.messages.length > 0) { - this.messages = orderByTimestamp(this.messages); + const result = orderByTimestamp(this.messages); + if (result.wasRearranged) { + ReorderedMessageDelayer.debugger('Messages out of order: %O', this.messages); + } + + this.messages = result.responses; + this.emit('message', this.messages.shift()); } } diff --git a/packages/genesys-web-messaging-tester/src/genesys/message-delayer/orderByTimestamp.spec.ts b/packages/genesys-web-messaging-tester/src/genesys/message-delayer/orderByTimestamp.spec.ts index 3eeed13..78c0b2d 100644 --- a/packages/genesys-web-messaging-tester/src/genesys/message-delayer/orderByTimestamp.spec.ts +++ b/packages/genesys-web-messaging-tester/src/genesys/message-delayer/orderByTimestamp.spec.ts @@ -1,4 +1,4 @@ -import { orderByTimestamp } from './orderByTimestamp'; +import { orderByTimestamp, orderByTimestampResult } from './orderByTimestamp'; import { Response } from '../Response'; import { StructuredMessage } from '../StructuredMessage'; import { SessionResponse, SessionResponseSuccessBody } from '../SessionResponse'; @@ -45,29 +45,35 @@ describe('orderWithTimestamps', () => { createStructuredMessage('6', '2023-07-22T21:19:25.256Z'), ]; - expect(orderByTimestamp(unordered)).toEqual([ - createStructuredMessage('7', '2023-07-22T20:13:55.256Z'), - createSessionResponse({ connected: true, newSession: false, readOnly: false }), - createStructuredMessage('6', '2023-07-22T21:19:25.256Z'), - createStructuredMessage('1', '2023-07-22T21:19:41.256Z'), - createSessionResponse({ connected: false, newSession: true, readOnly: false }), - createSessionResponse({ connected: false, newSession: false, readOnly: true }), - createStructuredMessage('3', '2023-07-22T21:19:55.256Z'), - ]); + expect(orderByTimestamp(unordered)).toStrictEqual({ + wasRearranged: true, + responses: [ + createStructuredMessage('7', '2023-07-22T20:13:55.256Z'), + createSessionResponse({ connected: true, newSession: false, readOnly: false }), + createStructuredMessage('6', '2023-07-22T21:19:25.256Z'), + createStructuredMessage('1', '2023-07-22T21:19:41.256Z'), + createSessionResponse({ connected: false, newSession: true, readOnly: false }), + createSessionResponse({ connected: false, newSession: false, readOnly: true }), + createStructuredMessage('3', '2023-07-22T21:19:55.256Z'), + ], + }); }); test('Does not change the order of non-timestamped elements', () => { const input: Response[] = [ createSessionResponse({ connected: true, newSession: false, readOnly: false }), - createSessionResponse({ connected: false, newSession: true, readOnly: false }), - createSessionResponse({ connected: false, newSession: false, readOnly: true }), + createSessionResponse({ connected: true, newSession: true, readOnly: false }), + createSessionResponse({ connected: true, newSession: true, readOnly: true }), ]; - expect(orderByTimestamp(input)).toEqual([ - createSessionResponse({ connected: true, newSession: false, readOnly: false }), - createSessionResponse({ connected: true, newSession: true, readOnly: false }), - createSessionResponse({ connected: false, newSession: false, readOnly: true }), - ]); + expect(orderByTimestamp(input)).toStrictEqual({ + wasRearranged: false, + responses: [ + createSessionResponse({ connected: true, newSession: false, readOnly: false }), + createSessionResponse({ connected: true, newSession: true, readOnly: false }), + createSessionResponse({ connected: true, newSession: true, readOnly: true }), + ], + }); }); test('Correctly sorts an array with only timestamped elements', () => { @@ -77,10 +83,13 @@ describe('orderWithTimestamps', () => { createStructuredMessage('3', '2023-07-22T21:19:25.256Z'), ]; - expect(orderByTimestamp(unordered)).toStrictEqual([ - createStructuredMessage('3', '2023-07-22T21:19:25.256Z'), - createStructuredMessage('1', '2023-07-22T21:19:41.256Z'), - createStructuredMessage('2', '2023-07-22T21:19:55.256Z'), - ]); + expect(orderByTimestamp(unordered)).toStrictEqual({ + wasRearranged: true, + responses: [ + createStructuredMessage('3', '2023-07-22T21:19:25.256Z'), + createStructuredMessage('1', '2023-07-22T21:19:41.256Z'), + createStructuredMessage('2', '2023-07-22T21:19:55.256Z'), + ], + }); }); }); diff --git a/packages/genesys-web-messaging-tester/src/genesys/message-delayer/orderByTimestamp.ts b/packages/genesys-web-messaging-tester/src/genesys/message-delayer/orderByTimestamp.ts index 9353274..0a6bc00 100644 --- a/packages/genesys-web-messaging-tester/src/genesys/message-delayer/orderByTimestamp.ts +++ b/packages/genesys-web-messaging-tester/src/genesys/message-delayer/orderByTimestamp.ts @@ -2,7 +2,12 @@ import { Response } from '../Response'; import { StructuredMessage } from '../StructuredMessage'; import { isStructuredMessage } from '../WebMessengerGuestSession'; -export function orderByTimestamp(array: Response[]): Response[] { +export interface orderByTimestampResult { + wasRearranged: boolean; + responses: Response[]; +} + +export function orderByTimestamp(array: Response[]): orderByTimestampResult { const withTimestamps: StructuredMessage[] = []; const withoutTimestamps: Response[] = []; @@ -32,5 +37,13 @@ export function orderByTimestamp(array: Response[]): Response[ } } - return result; + let wasRearranged = false; + for (let i = 0; i < array.length; i++) { + if (!Object.is(result[i], array[i])) { + wasRearranged = true; + break; + } + } + + return { wasRearranged, responses: result }; }