|
6 | 6 | insertEventIntoDescendingList, |
7 | 7 | binarySearch, |
8 | 8 | normalizeURL, |
| 9 | + mergeReverseSortedLists, |
9 | 10 | } from './utils.ts' |
10 | 11 |
|
11 | 12 | import type { Event } from './core.ts' |
@@ -270,6 +271,94 @@ test('binary search', () => { |
270 | 271 | expect(binarySearch(['a', 'b', 'd', 'e'], b => ('[' < b ? -1 : '[' === b ? 0 : 1))).toEqual([0, false]) |
271 | 272 | }) |
272 | 273 |
|
| 274 | +describe('mergeReverseSortedLists', () => { |
| 275 | + test('merge empty lists', () => { |
| 276 | + const list1: Event[] = [] |
| 277 | + const list2: Event[] = [] |
| 278 | + expect(mergeReverseSortedLists(list1, list2)).toHaveLength(0) |
| 279 | + }) |
| 280 | + |
| 281 | + test('merge list with empty list', () => { |
| 282 | + const list1 = [buildEvent({ id: 'a', created_at: 30 }), buildEvent({ id: 'b', created_at: 20 })] |
| 283 | + const list2: Event[] = [] |
| 284 | + const result = mergeReverseSortedLists(list1, list2) |
| 285 | + expect(result).toHaveLength(2) |
| 286 | + expect(result.map(e => e.id)).toEqual(['a', 'b']) |
| 287 | + }) |
| 288 | + |
| 289 | + test('merge two simple lists', () => { |
| 290 | + const list1 = [ |
| 291 | + buildEvent({ id: 'a', created_at: 30 }), |
| 292 | + buildEvent({ id: 'b', created_at: 10 }), |
| 293 | + buildEvent({ id: 'f', created_at: 3 }), |
| 294 | + buildEvent({ id: 'g', created_at: 2 }), |
| 295 | + ] |
| 296 | + const list2 = [ |
| 297 | + buildEvent({ id: 'c', created_at: 25 }), |
| 298 | + buildEvent({ id: 'd', created_at: 5 }), |
| 299 | + buildEvent({ id: 'e', created_at: 1 }), |
| 300 | + ] |
| 301 | + const result = mergeReverseSortedLists(list1, list2) |
| 302 | + expect(result.map(e => e.id)).toEqual(['a', 'c', 'b', 'd', 'f', 'g', 'e']) |
| 303 | + }) |
| 304 | + |
| 305 | + test('merge lists with same timestamps', () => { |
| 306 | + const list1 = [ |
| 307 | + buildEvent({ id: 'a', created_at: 30 }), |
| 308 | + buildEvent({ id: 'b', created_at: 20 }), |
| 309 | + buildEvent({ id: 'f', created_at: 10 }), |
| 310 | + ] |
| 311 | + const list2 = [ |
| 312 | + buildEvent({ id: 'c', created_at: 30 }), |
| 313 | + buildEvent({ id: 'd', created_at: 20 }), |
| 314 | + buildEvent({ id: 'e', created_at: 20 }), |
| 315 | + ] |
| 316 | + const result = mergeReverseSortedLists(list1, list2) |
| 317 | + expect(result.map(e => e.id)).toEqual(['c', 'a', 'd', 'e', 'b', 'f']) |
| 318 | + }) |
| 319 | + |
| 320 | + test('deduplicate events with same timestamp and id', () => { |
| 321 | + const list1 = [ |
| 322 | + buildEvent({ id: 'a', created_at: 30 }), |
| 323 | + buildEvent({ id: 'b', created_at: 20 }), |
| 324 | + buildEvent({ id: 'b', created_at: 20 }), |
| 325 | + buildEvent({ id: 'c', created_at: 20 }), |
| 326 | + buildEvent({ id: 'd', created_at: 10 }), |
| 327 | + ] |
| 328 | + const list2 = [ |
| 329 | + buildEvent({ id: 'a', created_at: 30 }), |
| 330 | + buildEvent({ id: 'c', created_at: 20 }), |
| 331 | + buildEvent({ id: 'b', created_at: 20 }), |
| 332 | + buildEvent({ id: 'd', created_at: 10 }), |
| 333 | + buildEvent({ id: 'e', created_at: 10 }), |
| 334 | + buildEvent({ id: 'd', created_at: 10 }), |
| 335 | + ] |
| 336 | + console.log('==================') |
| 337 | + const result = mergeReverseSortedLists(list1, list2) |
| 338 | + console.log( |
| 339 | + 'result:', |
| 340 | + result.map(e => e.id), |
| 341 | + ) |
| 342 | + expect(result.map(e => e.id)).toEqual(['a', 'c', 'b', 'd', 'e']) |
| 343 | + }) |
| 344 | + |
| 345 | + test('merge when one list is completely before the other', () => { |
| 346 | + const list1 = [buildEvent({ id: 'a', created_at: 50 }), buildEvent({ id: 'b', created_at: 40 })] |
| 347 | + const list2 = [buildEvent({ id: 'c', created_at: 30 }), buildEvent({ id: 'd', created_at: 20 })] |
| 348 | + const result = mergeReverseSortedLists(list1, list2) |
| 349 | + expect(result).toHaveLength(4) |
| 350 | + expect(result.map(e => e.id)).toEqual(['a', 'b', 'c', 'd']) |
| 351 | + }) |
| 352 | + |
| 353 | + test('merge when one list is completely after the other', () => { |
| 354 | + const list1 = [buildEvent({ id: 'a', created_at: 10 }), buildEvent({ id: 'b', created_at: 5 })] |
| 355 | + const list2 = [buildEvent({ id: 'c', created_at: 30 }), buildEvent({ id: 'd', created_at: 20 })] |
| 356 | + const result = mergeReverseSortedLists(list1, list2) |
| 357 | + expect(result).toHaveLength(4) |
| 358 | + expect(result.map(e => e.id)).toEqual(['c', 'd', 'a', 'b']) |
| 359 | + }) |
| 360 | +}) |
| 361 | + |
273 | 362 | describe('normalizeURL', () => { |
274 | 363 | test('normalizes wss:// URLs', () => { |
275 | 364 | expect(normalizeURL('wss://example.com')).toBe('wss://example.com/') |
|
0 commit comments