|
| 1 | +import '../../../env-test'; |
| 2 | +import { bucketTimestampMs } from '../src/utils/bucketTimestamp'; |
| 3 | + |
| 4 | +describe('bucketTimestampMs', () => { |
| 5 | + /** |
| 6 | + * 2026-04-14T15:37:42.500Z |
| 7 | + * minute start: 2026-04-14T15:37:00.000Z |
| 8 | + * hour start: 2026-04-14T15:00:00.000Z |
| 9 | + * day start: 2026-04-14T00:00:00.000Z |
| 10 | + */ |
| 11 | + const now = new Date('2026-04-14T15:37:42.500Z').getTime(); |
| 12 | + |
| 13 | + it('truncates to the start of the current minute', () => { |
| 14 | + const expected = new Date('2026-04-14T15:37:00.000Z').getTime(); |
| 15 | + |
| 16 | + expect(bucketTimestampMs('minutely', now)).toBe(expected); |
| 17 | + }); |
| 18 | + |
| 19 | + it('truncates to the start of the current hour', () => { |
| 20 | + const expected = new Date('2026-04-14T15:00:00.000Z').getTime(); |
| 21 | + |
| 22 | + expect(bucketTimestampMs('hourly', now)).toBe(expected); |
| 23 | + }); |
| 24 | + |
| 25 | + it('truncates to the start of the current day (UTC midnight)', () => { |
| 26 | + const expected = new Date('2026-04-14T00:00:00.000Z').getTime(); |
| 27 | + |
| 28 | + expect(bucketTimestampMs('daily', now)).toBe(expected); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns the same value for two calls within the same minute', () => { |
| 32 | + const t1 = new Date('2026-04-14T15:37:00.000Z').getTime(); |
| 33 | + const t2 = new Date('2026-04-14T15:37:59.999Z').getTime(); |
| 34 | + |
| 35 | + expect(bucketTimestampMs('minutely', t1)).toBe(bucketTimestampMs('minutely', t2)); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns different values for two calls in different minutes', () => { |
| 39 | + const t1 = new Date('2026-04-14T15:37:59.999Z').getTime(); |
| 40 | + const t2 = new Date('2026-04-14T15:38:00.000Z').getTime(); |
| 41 | + |
| 42 | + expect(bucketTimestampMs('minutely', t1)).not.toBe(bucketTimestampMs('minutely', t2)); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns the same value for two calls within the same hour', () => { |
| 46 | + const t1 = new Date('2026-04-14T15:00:00.000Z').getTime(); |
| 47 | + const t2 = new Date('2026-04-14T15:59:59.999Z').getTime(); |
| 48 | + |
| 49 | + expect(bucketTimestampMs('hourly', t1)).toBe(bucketTimestampMs('hourly', t2)); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns the same value for two calls within the same day', () => { |
| 53 | + const t1 = new Date('2026-04-14T00:00:00.000Z').getTime(); |
| 54 | + const t2 = new Date('2026-04-14T23:59:59.999Z').getTime(); |
| 55 | + |
| 56 | + expect(bucketTimestampMs('daily', t1)).toBe(bucketTimestampMs('daily', t2)); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns different values for two calls on different days', () => { |
| 60 | + const t1 = new Date('2026-04-14T23:59:59.999Z').getTime(); |
| 61 | + const t2 = new Date('2026-04-15T00:00:00.000Z').getTime(); |
| 62 | + |
| 63 | + expect(bucketTimestampMs('daily', t1)).not.toBe(bucketTimestampMs('daily', t2)); |
| 64 | + }); |
| 65 | +}); |
0 commit comments