@@ -3,12 +3,12 @@ import { v4 as uuidv4 } from 'uuid';
3
3
4
4
import { generateMsg } from './test-utils/generateMessage' ;
5
5
6
- import { addToMessageList , formatMessage } from '../../src/utils' ;
6
+ import { addToMessageList , findIndexInSortedArray , formatMessage } from '../../src/utils' ;
7
7
8
8
import type { FormatMessageResponse , MessageResponse } from '../../src' ;
9
9
10
10
describe ( 'addToMessageList' , ( ) => {
11
- const timestamp = new Date ( '2024-09-18T15:30 :00.000Z' ) . getTime ( ) ;
11
+ const timestamp = new Date ( '2024-01-01T00:00 :00.000Z' ) . getTime ( ) ;
12
12
// messages with each created_at 10 seconds apart
13
13
let messagesBefore : FormatMessageResponse [ ] ;
14
14
@@ -93,15 +93,91 @@ describe('addToMessageList', () => {
93
93
} ) ;
94
94
95
95
it ( "updates an existing message that wasn't filtered due to changed timestamp (timestampChanged)" , ( ) => {
96
- const newMessage = getNewFormattedMessage ( { timeOffset : 30 * 1000 , id : messagesBefore [ 4 ] . id } ) ;
96
+ const newMessage = getNewFormattedMessage ( { timeOffset : 30 * 1000 , id : messagesBefore [ 3 ] . id } ) ;
97
97
98
- expect ( messagesBefore [ 4 ] . id ) . to . equal ( newMessage . id ) ;
99
- expect ( messagesBefore [ 4 ] . text ) . to . not . equal ( newMessage . text ) ;
100
- expect ( messagesBefore [ 4 ] ) . to . not . equal ( newMessage ) ;
98
+ expect ( messagesBefore [ 3 ] . id ) . to . equal ( newMessage . id ) ;
99
+ expect ( messagesBefore [ 3 ] . text ) . to . not . equal ( newMessage . text ) ;
100
+ expect ( messagesBefore [ 3 ] ) . to . not . equal ( newMessage ) ;
101
101
102
- const messagesAfter = addToMessageList ( messagesBefore , newMessage , false , 'created_at' , false ) ;
102
+ const messagesAfter = addToMessageList ( messagesBefore , newMessage , false ) ;
103
103
104
104
expect ( messagesAfter ) . to . have . length ( 5 ) ;
105
- expect ( messagesAfter [ 4 ] ) . to . equal ( newMessage ) ;
105
+ expect ( messagesAfter [ 3 ] ) . to . equal ( newMessage ) ;
106
+ } ) ;
107
+ } ) ;
108
+
109
+ describe ( 'findIndexInSortedArray' , ( ) => {
110
+ const timestamp = new Date ( '2024-01-01T00:00:00.000Z' ) . getTime ( ) ;
111
+
112
+ const generateMessages = ( { count = 10 , sort = 'desc' } : { count ?: number ; sort ?: 'asc' | 'desc' } = { } ) => {
113
+ const messages = Array . from ( { length : count } , ( _ , index ) =>
114
+ generateMsg ( { created_at : new Date ( timestamp + index * 10 * 1000 ) . toISOString ( ) } ) ,
115
+ ) ;
116
+
117
+ if ( sort === 'desc' ) {
118
+ messages . reverse ( ) ;
119
+ }
120
+
121
+ return messages as MessageResponse [ ] ;
122
+ } ;
123
+
124
+ describe ( 'ascending order' , ( ) => {
125
+ const messages = generateMessages ( { sort : 'asc' } ) . map ( formatMessage ) ;
126
+
127
+ it ( 'finds index of the message with closest matching created_at' , ( ) => {
128
+ const newMessage = formatMessage ( generateMsg ( { created_at : new Date ( timestamp + 22 * 1000 ) } ) as MessageResponse ) ;
129
+
130
+ const index = findIndexInSortedArray ( {
131
+ needle : newMessage ,
132
+ sortedArray : messages ,
133
+ sortDirection : 'ascending' ,
134
+ selectValueToCompare : ( v ) => v . created_at . getTime ( ) ,
135
+ } ) ;
136
+
137
+ expect ( index ) . to . equal ( 3 ) ;
138
+ } ) ;
139
+
140
+ it ( 'finds exact index' , ( ) => {
141
+ const newMessage = formatMessage ( generateMsg ( { created_at : new Date ( timestamp + 20 * 1000 ) } ) as MessageResponse ) ;
142
+
143
+ const index = findIndexInSortedArray ( {
144
+ needle : newMessage ,
145
+ sortedArray : messages ,
146
+ sortDirection : 'ascending' ,
147
+ selectValueToCompare : ( v ) => v . created_at . getTime ( ) ,
148
+ } ) ;
149
+
150
+ expect ( index ) . to . equal ( 2 ) ;
151
+ } ) ;
152
+ } ) ;
153
+
154
+ describe ( 'descending order' , ( ) => {
155
+ const messages = generateMessages ( { sort : 'desc' } ) . map ( formatMessage ) ;
156
+
157
+ it ( 'finds index of the message with closest matching created_at' , ( ) => {
158
+ const newMessage = formatMessage ( generateMsg ( { created_at : new Date ( timestamp + 22 * 1000 ) } ) as MessageResponse ) ;
159
+
160
+ const index = findIndexInSortedArray ( {
161
+ needle : newMessage ,
162
+ sortedArray : messages ,
163
+ sortDirection : 'descending' ,
164
+ selectValueToCompare : ( v ) => v . created_at . getTime ( ) ,
165
+ } ) ;
166
+
167
+ expect ( index ) . to . equal ( 7 ) ;
168
+ } ) ;
169
+
170
+ it ( 'finds exact index' , ( ) => {
171
+ const newMessage = formatMessage ( generateMsg ( { created_at : new Date ( timestamp + 10 * 1000 ) } ) as MessageResponse ) ;
172
+
173
+ const index = findIndexInSortedArray ( {
174
+ needle : newMessage ,
175
+ sortedArray : messages ,
176
+ sortDirection : 'descending' ,
177
+ selectValueToCompare : ( v ) => v . created_at . getTime ( ) ,
178
+ } ) ;
179
+
180
+ expect ( index ) . to . equal ( 8 ) ;
181
+ } ) ;
106
182
} ) ;
107
183
} ) ;
0 commit comments