when-am-i / files / src / utils / dateRange.test.ts
dateRange.test.ts
8fa8f3cb 2026-06-21 Pavel Safronov | 1 | import { describe, it, expect } from 'vitest'; |
| 2 | import { deriveMinYear } from './dateRange'; | |
| 3 | import type { CalendarRegion, Era } from '../types'; | |
| 4 | ||
| 5 | const WORLD_GEO = { type: 'Polygon' as const, coordinates: [[[0, 0], [1, 0], [1, 1], [0, 0]] as [number, number][]] }; | |
| 6 | ||
| 7 | function regionWith(eras: Era[]): CalendarRegion { | |
| 8 | return { region: { name: 'Test', geo: WORLD_GEO }, eras }; | |
| 9 | } | |
| 10 | ||
| 11 | describe('deriveMinYear', () => { | |
| 12 | it('falls back to 2000 BCE when no era has a dated start', () => { | |
| 13 | const regions = [regionWith([{ name: 'Open', start: null, end: null, calendar: 'Gregorian' }])]; | |
| 14 | expect(deriveMinYear(regions, 2026)).toBe(-1999); | |
| 15 | }); | |
| 16 | ||
| 17 | it('uses the oldest dated start across all regions, minus a 10% margin', () => { | |
| 18 | const regions = [ | |
| 19 | regionWith([{ name: 'A', start: '1700-01-01', end: null, calendar: 'Gregorian' }]), | |
| 20 | regionWith([{ name: 'B', start: '-0036-01-01', end: null, calendar: 'Maya Long Count' }]), | |
| 21 | ]; | |
| 22 | // oldest = -36; span to 2026 = 2062; margin = 206.2 → floor(-36 - 206.2) = -243 | |
| 23 | expect(deriveMinYear(regions, 2026)).toBe(-243); | |
| 24 | }); | |
| 25 | ||
| 26 | it('ignores null-start eras even when other eras in the same region are dated', () => { | |
| 27 | const regions = [ | |
| 28 | regionWith([ | |
| 29 | { name: 'Open', start: null, end: '1000-01-01', calendar: 'Hebrew' }, | |
| 30 | { name: 'Dated', start: '500-01-01', end: null, calendar: 'Gregorian' }, | |
| 31 | ]), | |
| 32 | ]; | |
| 33 | // oldest dated = 500; span to 2026 = 1526; margin = 152.6 → floor(500 - 152.6) = 347 | |
| 34 | expect(deriveMinYear(regions, 2026)).toBe(347); | |
| 35 | }); | |
| 36 | }); |