fuzzyworld

when-am-i / files / src / utils / eras.test.ts

eras.test.ts

2.0 KB · history · blame

1import { describe, it, expect } from 'vitest';
2import { findEras } from './eras';
3import type { Era } from '../types';
4
5const colonial: Era = { name: 'Colonial', start: '1750-01-01', end: null, calendar: 'Gregorian' };
6const preColonial: Era = { name: 'Pre-Colonial', start: null, end: '1750-01-01', calendar: 'Algonquian lunar calendar' };
7// Deliberately overlaps both of the above around the 1750 boundary.
8const transitional: Era = { name: 'Transitional', start: '1700-01-01', end: '1800-01-01', calendar: 'Julian' };
9
10const eras = [colonial, preColonial, transitional];
11const names = (date: { year: number; month: number; day: number }) =>
12 findEras(date, eras).map((e) => e.name);
13
14describe('findEras', () => {
15 it('matches an open-past era before the boundary', () => {
16 expect(names({ year: 1600, month: 6, day: 1 })).toEqual(['Pre-Colonial']);
17 });
18
19 it('matches an open-present era after the boundary', () => {
20 expect(names({ year: 1900, month: 1, day: 1 })).toEqual(['Colonial']);
21 });
22
23 it('is half-open: the end date belongs to the next era, not the ending one', () => {
24 // 1750-01-01 is excluded from Pre-Colonial (its exclusive end) but included
25 // in Colonial (its inclusive start). Transitional (1700..1800) also covers it.
26 expect(names({ year: 1750, month: 1, day: 1 })).toEqual(['Colonial', 'Transitional']);
27 });
28
29 it('returns all overlapping eras at once', () => {
30 // 1740 is in both Pre-Colonial (< 1750) and Transitional (1700..1800).
31 expect(names({ year: 1740, month: 1, day: 1 })).toEqual(['Pre-Colonial', 'Transitional']);
32 // 1760 is in both Colonial (>= 1750) and Transitional.
33 expect(names({ year: 1760, month: 1, day: 1 })).toEqual(['Colonial', 'Transitional']);
34 });
35
36 it('returns nothing when no era covers the date', () => {
37 const bounded: Era[] = [{ name: 'Only', start: '1000-01-01', end: '1100-01-01', calendar: 'Julian' }];
38 expect(findEras({ year: 1200, month: 1, day: 1 }, bounded)).toEqual([]);
39 });
40});