|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { |
| 3 | + sortFn_alphanumeric, |
| 4 | + sortFn_alphanumericCaseSensitive, |
| 5 | + sortFn_basic, |
| 6 | + sortFn_datetime, |
| 7 | + sortFn_text, |
| 8 | + sortFn_textCaseSensitive, |
| 9 | +} from '../../../src' |
| 10 | + |
| 11 | +const makeRow = (value: any): any => ({ |
| 12 | + getValue: () => value, |
| 13 | +}) |
| 14 | + |
| 15 | +const cmp = ( |
| 16 | + fn: (a: any, b: any, c: string) => number, |
| 17 | + a: any, |
| 18 | + b: any, |
| 19 | +): number => { |
| 20 | + const result = fn(makeRow(a), makeRow(b), 'col') |
| 21 | + // Normalize to -1 / 0 / 1 for stable assertions across sign-only comparators |
| 22 | + return result < 0 ? -1 : result > 0 ? 1 : 0 |
| 23 | +} |
| 24 | + |
| 25 | +describe('sortFn_alphanumeric (compareAlphanumeric)', () => { |
| 26 | + it('returns 0 for equal strings', () => { |
| 27 | + expect(cmp(sortFn_alphanumeric, 'abc', 'abc')).toBe(0) |
| 28 | + }) |
| 29 | + |
| 30 | + it('returns 0 for two empty strings', () => { |
| 31 | + expect(cmp(sortFn_alphanumeric, '', '')).toBe(0) |
| 32 | + }) |
| 33 | + |
| 34 | + it('sorts pure strings lexicographically', () => { |
| 35 | + expect(cmp(sortFn_alphanumeric, 'apple', 'banana')).toBe(-1) |
| 36 | + expect(cmp(sortFn_alphanumeric, 'banana', 'apple')).toBe(1) |
| 37 | + }) |
| 38 | + |
| 39 | + it('sorts pure numbers numerically (natural sort)', () => { |
| 40 | + // The whole point: "item2" should come before "item10" |
| 41 | + expect(cmp(sortFn_alphanumeric, 'item2', 'item10')).toBe(-1) |
| 42 | + expect(cmp(sortFn_alphanumeric, 'item10', 'item2')).toBe(1) |
| 43 | + }) |
| 44 | + |
| 45 | + it('sorts mixed alphanumeric strings naturally', () => { |
| 46 | + expect(cmp(sortFn_alphanumeric, 'a1b2', 'a1b10')).toBe(-1) |
| 47 | + expect(cmp(sortFn_alphanumeric, 'a2b', 'a10b')).toBe(-1) |
| 48 | + }) |
| 49 | + |
| 50 | + it('treats string chunk less than number chunk in mixed-type comparison', () => { |
| 51 | + // When chunk types differ at the same position, string sorts before number |
| 52 | + expect(cmp(sortFn_alphanumeric, 'abc', '123')).toBe(-1) |
| 53 | + expect(cmp(sortFn_alphanumeric, '123', 'abc')).toBe(1) |
| 54 | + }) |
| 55 | + |
| 56 | + it('returns difference in chunk count when one is a prefix of the other', () => { |
| 57 | + // After all common chunks are consumed, the longer one wins (positive) |
| 58 | + expect(cmp(sortFn_alphanumeric, 'abc', 'abc1')).toBe(-1) |
| 59 | + expect(cmp(sortFn_alphanumeric, 'abc1', 'abc')).toBe(1) |
| 60 | + }) |
| 61 | + |
| 62 | + it('returns 0 when both strings normalize to no chunks', () => { |
| 63 | + // Empty strings split+filter to [] |
| 64 | + expect(cmp(sortFn_alphanumeric, '', '')).toBe(0) |
| 65 | + }) |
| 66 | + |
| 67 | + it('lowercases input (case-insensitive)', () => { |
| 68 | + expect(cmp(sortFn_alphanumeric, 'ABC', 'abc')).toBe(0) |
| 69 | + expect(cmp(sortFn_alphanumeric, 'Apple', 'banana')).toBe(-1) |
| 70 | + }) |
| 71 | + |
| 72 | + it('coerces numbers to strings via toString', () => { |
| 73 | + expect(cmp(sortFn_alphanumeric, 2, 10)).toBe(-1) |
| 74 | + expect(cmp(sortFn_alphanumeric, 10, 2)).toBe(1) |
| 75 | + }) |
| 76 | + |
| 77 | + it('treats NaN/Infinity numbers as empty', () => { |
| 78 | + expect(cmp(sortFn_alphanumeric, NaN, '')).toBe(0) |
| 79 | + expect(cmp(sortFn_alphanumeric, Infinity, '')).toBe(0) |
| 80 | + }) |
| 81 | + |
| 82 | + it('handles long mixed strings (stress: many chunks)', () => { |
| 83 | + const a = 'abc1def2ghi3jkl4mno5' |
| 84 | + const b = 'abc1def2ghi3jkl4mno10' |
| 85 | + expect(cmp(sortFn_alphanumeric, a, b)).toBe(-1) |
| 86 | + expect(cmp(sortFn_alphanumeric, b, a)).toBe(1) |
| 87 | + }) |
| 88 | + |
| 89 | + it('sort() integration: produces natural order', () => { |
| 90 | + const items = ['item10', 'item2', 'item1', 'item20', 'item3'] |
| 91 | + items.sort((a, b) => sortFn_alphanumeric(makeRow(a), makeRow(b), 'col')) |
| 92 | + expect(items).toEqual(['item1', 'item2', 'item3', 'item10', 'item20']) |
| 93 | + }) |
| 94 | +}) |
| 95 | + |
| 96 | +describe('sortFn_alphanumericCaseSensitive', () => { |
| 97 | + it('preserves case (uppercase < lowercase by ASCII)', () => { |
| 98 | + expect(cmp(sortFn_alphanumericCaseSensitive, 'ABC', 'abc')).toBe(-1) |
| 99 | + expect(cmp(sortFn_alphanumericCaseSensitive, 'abc', 'ABC')).toBe(1) |
| 100 | + }) |
| 101 | + |
| 102 | + it('sorts naturally with case sensitivity', () => { |
| 103 | + expect(cmp(sortFn_alphanumericCaseSensitive, 'Item2', 'Item10')).toBe(-1) |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe('sortFn_text', () => { |
| 108 | + it('returns 0 for equal strings', () => { |
| 109 | + expect(cmp(sortFn_text, 'abc', 'abc')).toBe(0) |
| 110 | + }) |
| 111 | + |
| 112 | + it('sorts lexicographically (no natural sort)', () => { |
| 113 | + // text uses compareBasic — "item10" < "item2" lexicographically |
| 114 | + expect(cmp(sortFn_text, 'item10', 'item2')).toBe(-1) |
| 115 | + }) |
| 116 | + |
| 117 | + it('lowercases input', () => { |
| 118 | + expect(cmp(sortFn_text, 'ABC', 'abc')).toBe(0) |
| 119 | + }) |
| 120 | +}) |
| 121 | + |
| 122 | +describe('sortFn_textCaseSensitive', () => { |
| 123 | + it('preserves case', () => { |
| 124 | + expect(cmp(sortFn_textCaseSensitive, 'ABC', 'abc')).toBe(-1) |
| 125 | + }) |
| 126 | +}) |
| 127 | + |
| 128 | +describe('sortFn_basic', () => { |
| 129 | + it('returns 0 for equal values', () => { |
| 130 | + expect(cmp(sortFn_basic, 5, 5)).toBe(0) |
| 131 | + }) |
| 132 | + |
| 133 | + it('compares numbers', () => { |
| 134 | + expect(cmp(sortFn_basic, 1, 2)).toBe(-1) |
| 135 | + expect(cmp(sortFn_basic, 2, 1)).toBe(1) |
| 136 | + }) |
| 137 | + |
| 138 | + it('compares strings', () => { |
| 139 | + expect(cmp(sortFn_basic, 'a', 'b')).toBe(-1) |
| 140 | + }) |
| 141 | +}) |
| 142 | + |
| 143 | +describe('sortFn_datetime', () => { |
| 144 | + it('returns 0 for equal dates', () => { |
| 145 | + const d = new Date(2026, 1, 1) |
| 146 | + expect(cmp(sortFn_datetime, d, d)).toBe(0) |
| 147 | + }) |
| 148 | + |
| 149 | + it('compares dates by chronology', () => { |
| 150 | + const earlier = new Date(2026, 1, 1) |
| 151 | + const later = new Date(2026, 1, 2) |
| 152 | + expect(cmp(sortFn_datetime, earlier, later)).toBe(-1) |
| 153 | + expect(cmp(sortFn_datetime, later, earlier)).toBe(1) |
| 154 | + }) |
| 155 | + |
| 156 | + it('compares numeric timestamps', () => { |
| 157 | + expect(cmp(sortFn_datetime, 1000, 2000)).toBe(-1) |
| 158 | + }) |
| 159 | +}) |
0 commit comments