|
| 1 | +import { numericComparator } from '../sort-utils'; |
| 2 | + |
| 3 | +describe('numericComparator', () => { |
| 4 | + it('should return -1 when a < b with positive multiplier', () => { |
| 5 | + expect(numericComparator(1, 2, 1)).toBe(-1); |
| 6 | + }); |
| 7 | + |
| 8 | + it('should return 1 when a > b with positive multiplier', () => { |
| 9 | + expect(numericComparator(2, 1, 1)).toBe(1); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should return 0 when a === b', () => { |
| 13 | + expect(numericComparator(1, 1, 1)).toBe(0); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should invert result with negative multiplier (descending sort)', () => { |
| 17 | + expect(numericComparator(1, 2, -1)).toBe(1); |
| 18 | + expect(numericComparator(2, 1, -1)).toBe(-1); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should use fallback comparison when values are equal', () => { |
| 22 | + expect(numericComparator(5, 5, 1, 3)).toBe(3); |
| 23 | + expect(numericComparator(5, 5, 1, -2)).toBe(-2); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should apply direction multiplier to fallback comparison', () => { |
| 27 | + expect(numericComparator(5, 5, -1, 3)).toBe(-3); |
| 28 | + expect(numericComparator(5, 5, -1, -2)).toBe(2); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should ignore fallback when values are not equal', () => { |
| 32 | + expect(numericComparator(1, 2, 1, 100)).toBe(-1); |
| 33 | + expect(numericComparator(2, 1, 1, 100)).toBe(1); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should work with timestamps', () => { |
| 37 | + const timestamp1 = 1679000000000; |
| 38 | + const timestamp2 = 1679000000001; |
| 39 | + expect(numericComparator(timestamp1, timestamp2, 1)).toBe(-1); |
| 40 | + expect(numericComparator(timestamp2, timestamp1, 1)).toBe(1); |
| 41 | + expect(numericComparator(timestamp1, timestamp1, 1)).toBe(0); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should use logIndex as tiebreaker for equal timestamps in ascending sort', () => { |
| 45 | + const timestamp = 1679000000000; |
| 46 | + const logs = [ |
| 47 | + { timestamp, logIndex: 0 }, |
| 48 | + { timestamp, logIndex: 1 }, |
| 49 | + { timestamp, logIndex: 2 }, |
| 50 | + ]; |
| 51 | + |
| 52 | + const sortedAsc = [...logs].sort((a, b) => |
| 53 | + numericComparator(a.timestamp, b.timestamp, 1, a.logIndex - b.logIndex), |
| 54 | + ); |
| 55 | + expect(sortedAsc.map((l) => l.logIndex)).toEqual([0, 1, 2]); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should use logIndex as tiebreaker for equal timestamps in descending sort', () => { |
| 59 | + const timestamp = 1679000000000; |
| 60 | + const logs = [ |
| 61 | + { timestamp, logIndex: 0 }, |
| 62 | + { timestamp, logIndex: 1 }, |
| 63 | + { timestamp, logIndex: 2 }, |
| 64 | + ]; |
| 65 | + |
| 66 | + const sortedDesc = [...logs].sort((a, b) => |
| 67 | + numericComparator(a.timestamp, b.timestamp, -1, a.logIndex - b.logIndex), |
| 68 | + ); |
| 69 | + // Direction multiplier is applied to fallback, so order is reversed |
| 70 | + expect(sortedDesc.map((l) => l.logIndex)).toEqual([2, 1, 0]); |
| 71 | + }); |
| 72 | +}); |
0 commit comments