| 1 |
import { describe, expect, it } from 'vitest'; |
| 2 |
|
| 3 |
import { normalizeVersion, compareVersion, versionSatisfies } from '../versionSatisfies'; |
| 4 |
|
| 5 |
describe('normalizeVersion', () => { |
| 6 |
it('should return a string', () => { |
| 7 |
expect(normalizeVersion('1.2.3')).toBeTypeOf('string'); |
| 8 |
}); |
| 9 |
it('should handle empty values', () => { |
| 10 |
expect(normalizeVersion('')).toBeTypeOf('string'); |
| 11 |
expect(normalizeVersion('')).toBe('0.0.0'); |
| 12 |
}); |
| 13 |
it('should fill to 3 segments', () => { |
| 14 |
expect(normalizeVersion('1')).toBe('1.0.0'); |
| 15 |
expect(normalizeVersion('1.2')).toBe('1.2.0'); |
| 16 |
expect(normalizeVersion('1.2.3')).toBe('1.2.3'); |
| 17 |
}); |
| 18 |
it('should limit to 3 segments', () => { |
| 19 |
expect(normalizeVersion('1.2.3.4')).toBe('1.2.3'); |
| 20 |
}); |
| 21 |
it('should keep segments in place', () => { |
| 22 |
expect(normalizeVersion('0.1')).toBe('0.1.0'); |
| 23 |
expect(normalizeVersion('0.0.1')).toBe('0.0.1'); |
| 24 |
}); |
| 25 |
it('should handle empty segments', () => { |
| 26 |
expect(normalizeVersion('.')).toBe('0.0.0'); |
| 27 |
expect(normalizeVersion('1.')).toBe('1.0.0'); |
| 28 |
expect(normalizeVersion('1.2.')).toBe('1.2.0'); |
| 29 |
expect(normalizeVersion('.1.2')).toBe('0.1.2'); |
| 30 |
expect(normalizeVersion('.3.')).toBe('0.3.0'); |
| 31 |
}); |
| 32 |
it('should handle multi-number segments', () => { |
| 33 |
expect(normalizeVersion('1.222.333')).toBe('1.222.333'); |
| 34 |
expect(normalizeVersion('1.111')).toBe('1.111.0'); |
| 35 |
expect(normalizeVersion('222')).toBe('222.0.0'); |
| 36 |
}); |
| 37 |
it('should handle preids', () => { |
| 38 |
expect(normalizeVersion('1.2.3-alpha')).toBe('1.2.3'); |
| 39 |
expect(normalizeVersion('1.2.3-rc.0')).toBe('1.2.3'); |
| 40 |
}); |
| 41 |
}); |
| 42 |
|
| 43 |
describe('compareVersion', () => { |
| 44 |
it('should return a number', () => { |
| 45 |
expect(compareVersion('1.0.0', '1.0.1')).toBeTypeOf('number'); |
| 46 |
}); |
| 47 |
it('should correctly sort equal major', () => { |
| 48 |
expect(compareVersion('1.0.0', '1.0.0')).toBe(0); |
| 49 |
expect(compareVersion('1', '1.0.0')).toBe(0); |
| 50 |
expect(compareVersion('2.1.0', '2.1')).toBe(0); |
| 51 |
expect(compareVersion('.1', '0.1.0')).toBe(0); |
| 52 |
expect(compareVersion('2.0.0-rc.3', '2')).toBe(0); |
| 53 |
}); |
| 54 |
it('should correctly sort equal minor', () => { |
| 55 |
expect(compareVersion('0.0.1', '0.0.1')).toBe(0); |
| 56 |
expect(compareVersion('.2.1', '0.2.1')).toBe(0); |
| 57 |
}); |
| 58 |
it('should correctly sort equal patch', () => { |
| 59 |
expect(compareVersion('1.0.0', '1.0.0')).toBe(0); |
| 60 |
expect(compareVersion('0.0.1', '.0.1')).toBe(0); |
| 61 |
expect(compareVersion('2.0.2-rc.1', '2.0.2')).toBe(0); |
| 62 |
}); |
| 63 |
it('should correctly sort major', () => { |
| 64 |
expect(compareVersion('2.0.0', '1.0.1')).toBe(1); |
| 65 |
expect(compareVersion('3.0', '4')).toBe(-1); |
| 66 |
}); |
| 67 |
it('should correctly sort minor', () => { |
| 68 |
expect(compareVersion('1.1.0', '1.2.0')).toBe(-1); |
| 69 |
expect(compareVersion('3.1.0', '3')).toBe(1); |
| 70 |
}); |
| 71 |
it('should correctly sort patch', () => { |
| 72 |
expect(compareVersion('1.0.0', '1.0.1')).toBe(-1); |
| 73 |
expect(compareVersion('1.2.2', '1.2.3-alpha')).toBe(-1); |
| 74 |
}); |
| 75 |
}); |
| 76 |
|
| 77 |
describe('versionSatisfies', () => { |
| 78 |
it('should assume greater-than-or-equal comparator', () => { |
| 79 |
expect(versionSatisfies('2.1', ['2', '2.1'])).toEqual(versionSatisfies('2.1', ['>=2', '>=2.1'])); |
| 80 |
}); |
| 81 |
it('should correctly apply greater-than-or-equal', () => { |
| 82 |
expect(versionSatisfies('1.0.1', ['>=1'])).toBe(true); |
| 83 |
expect(versionSatisfies('1.0.0', ['>=1'])).toBe(true); |
| 84 |
expect(versionSatisfies('2', ['>=2.0.0.0'])).toBe(true); |
| 85 |
expect(versionSatisfies('2', ['>=2'])).toBe(true); |
| 86 |
expect(versionSatisfies('1', ['>=1.1'])).toBe(false); |
| 87 |
expect(versionSatisfies('1', ['>=2'])).toBe(false); |
| 88 |
}); |
| 89 |
it('should correctly apply less-than-or-equal', () => { |
| 90 |
expect(versionSatisfies('1.0.0', ['<=1'])).toBe(true); |
| 91 |
expect(versionSatisfies('1.0.1', ['<=1'])).toBe(false); |
| 92 |
expect(versionSatisfies('2', ['<=2.0.0.0'])).toBe(true); |
| 93 |
expect(versionSatisfies('1', ['<=1.1'])).toBe(true); |
| 94 |
expect(versionSatisfies('1.1.3', ['<=1.1'])).toBe(false); |
| 95 |
}); |
| 96 |
it('should correctly apply less-than', () => { |
| 97 |
expect(versionSatisfies('1.0.0', ['<1.0.1'])).toBe(true); |
| 98 |
expect(versionSatisfies('1.0.0', ['<1.0.0'])).toBe(false); |
| 99 |
expect(versionSatisfies('1.0.1', ['<1'])).toBe(false); |
| 100 |
expect(versionSatisfies('1.3', ['<2'])).toBe(true); |
| 101 |
expect(versionSatisfies('1', ['<1.1'])).toBe(true); |
| 102 |
}); |
| 103 |
it('should correctly combine constraints', () => { |
| 104 |
expect(versionSatisfies('2.0.4', ['>=2', '<3'])).toBe(true); |
| 105 |
expect(versionSatisfies('2.0.5', ['>2.0.4', '<=2.0.5'])).toBe(true); |
| 106 |
expect(versionSatisfies('2.0.5', ['>2.0.4', '<2.0.5'])).toBe(false); |
| 107 |
}); |
| 108 |
}); |