|
| 1 | +import * as utils from '../packages/core/src'; |
| 2 | + |
| 3 | +describe('primitives', () => { |
| 4 | + it('fnv', () => { |
| 5 | + expect(utils.fnv('123456')).toBe('9995b6aa'); |
| 6 | + expect(utils.fnv('спутник')).toBe('5e1edd8c'); |
| 7 | + }); |
| 8 | + |
| 9 | + it('fnv64', () => { |
| 10 | + expect(utils.fnv64('123456')).toBe('f6e3ed7e0e67290a'); |
| 11 | + expect(utils.fnv64('спутник')).toBe('6251be44251f6e2c'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('pick', () => { |
| 15 | + expect(utils.pick({ test: 'test', rest: 'rest' }, ['test'])).toMatchObject({ |
| 16 | + test: 'test' |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('isEqual', () => { |
| 21 | + expect(utils.isEqual({ a: 1, b: 2 }, undefined)).toBe(false); |
| 22 | + expect(utils.isEqual(undefined, undefined)).toBe(false); |
| 23 | + expect(utils.isEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true); |
| 24 | + expect(utils.isEqual({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 })).toBe(false); |
| 25 | + expect(utils.isEqual({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 }, 'c')).toBe(true); |
| 26 | + expect(utils.isEqual({ a: 1, b: 2, c: { k: 3 } }, { a: 1, b: 2, c: { k: 3 } })).toBe(true); |
| 27 | + expect(utils.isEqual({ a: 1, b: 2, c: { k: 3 } }, { a: 1, b: 2, c: { k: 4 } })).toBe(false); |
| 28 | + expect(utils.isEqual({ a: 1, b: 2 }, { b: 2, a: 1 })).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it('memoize', () => { |
| 32 | + const md5Cached = utils.memoize(utils.md5); |
| 33 | + const fnvCached = utils.memoize(utils.fnv); |
| 34 | + expect(md5Cached('123456')).toBe('e10adc3949ba59abbe56e057f20f883e'); |
| 35 | + expect(fnvCached('123456')).toBe('9995b6aa'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('extendObject', () => { |
| 39 | + expect(utils.extendObject<object>({ a: 1 }, { a: { b: 1 }, c: null })).toEqual({ |
| 40 | + a: { b: 1 }, |
| 41 | + c: null |
| 42 | + }); |
| 43 | + expect(utils.extendObject<object>({ a: 1, c: [4] }, { a: { b: 1 }, c: [1, 2, 3] })).toEqual({ |
| 44 | + a: { b: 1 }, |
| 45 | + c: [1, 2, 3] |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('Duration Conversion', () => { |
| 50 | + const durationToMs = utils.durationToMs; |
| 51 | + |
| 52 | + it('should convert ms', () => { |
| 53 | + expect(durationToMs('500')).toBe(500); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should convert single unit durations', () => { |
| 57 | + expect(durationToMs('5s')).toBe(5000); |
| 58 | + expect(durationToMs('10m')).toBe(600000); |
| 59 | + expect(durationToMs('2h')).toBe(7200000); |
| 60 | + expect(durationToMs('3d')).toBe(259200000); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should convert multiple unit durations', () => { |
| 64 | + expect(durationToMs('1h 30m')).toBe(5400000); |
| 65 | + expect(durationToMs('2d 4h 30m')).toBe(189000000); |
| 66 | + expect(durationToMs('1w 2d 3h')).toBe(788400000); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should handle full word units', () => { |
| 70 | + expect(durationToMs('5 seconds')).toBe(5000); |
| 71 | + expect(durationToMs('2 hours')).toBe(7200000); |
| 72 | + expect(durationToMs('1 day 12 hours')).toBe(129600000); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw error for invalid units', () => { |
| 76 | + expect(() => durationToMs('5x')).toThrow('Invalid time unit: x'); |
| 77 | + expect(() => durationToMs('10 invalid')).toThrow('Invalid time unit: invalid'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should throw error for invalid format', () => { |
| 81 | + expect(() => durationToMs('abc')).toThrow('Invalid duration format'); |
| 82 | + expect(() => durationToMs('')).toThrow('Invalid duration format'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should throw error for excessive input length', () => { |
| 86 | + const longInput = '1s '.repeat(17); |
| 87 | + expect(() => durationToMs(longInput)).toThrow('Input string exceeds maximum length'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should convert to milliseconds', () => { |
| 91 | + const toMilliseconds = utils.toMilliseconds; |
| 92 | + expect(toMilliseconds(0)).toBe(0); |
| 93 | + expect(toMilliseconds('0')).toBe(0); |
| 94 | + expect(toMilliseconds(1500)).toBe(1500); |
| 95 | + expect(toMilliseconds('5s')).toBe(5000); |
| 96 | + expect(toMilliseconds(undefined)).toBeUndefined(); |
| 97 | + expect(toMilliseconds('')).toBeUndefined(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should convert to seconds', () => { |
| 101 | + const toSeconds = utils.toSeconds; |
| 102 | + expect(toSeconds(0)).toBe(0); |
| 103 | + expect(toSeconds('0')).toBe(0); |
| 104 | + expect(toSeconds(1500)).toBe(1); |
| 105 | + expect(toSeconds('5s')).toBe(5); |
| 106 | + expect(toSeconds(undefined)).toBeUndefined(); |
| 107 | + expect(toSeconds('')).toBeUndefined(); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments