import { describe, expect, it } from 'vitest'; import { formatClock, formatDate, formatLogLineTimestamp, formatMoment, formatShortDate, formatTimestampPrecise, toDate, } from './date-format'; const NOW = new Date('2026-09-24T18:00:00.000Z'); const TODAY = new Date('2026-09-24T14:32:00.000Z'); const THIS_YEAR = new Date('2026-03-05T09:07:00.000Z'); const LAST_YEAR = new Date('2025-09-24T14:32:00.000Z'); describe('toDate', () => { it('normalises dates, ISO strings and epoch milliseconds', () => { expect(toDate(TODAY)?.getTime()).toBe(TODAY.getTime()); expect(toDate('2026-09-24T14:32:00.000Z')?.getTime()).toBe(TODAY.getTime()); expect(toDate(TODAY.getTime())?.getTime()).toBe(TODAY.getTime()); }); it('returns undefined for missing or invalid values', () => { expect(toDate(undefined)).toBeUndefined(); expect(toDate(null)).toBeUndefined(); expect(toDate('not-a-date')).toBeUndefined(); }); }); describe('formatDate', () => { describe('when a date-only preset is requested', () => { it('never includes time, including today', () => { expect(formatDate(TODAY, 'date', { locale: 'en-US', timeZone: 'UTC', now: NOW })).toBe('Sep 24'); }); }); describe('when a historical timestamp needs a date and time', () => { it('keeps the date and time regardless of the reference day', () => { expect(formatDate(THIS_YEAR, 'date-time', { locale: 'en-US', timeZone: 'UTC', now: NOW })).toBe( 'Mar 5, 2026, 9:07 AM', ); }); }); describe('when relative time reaches seven days', () => { it('uses the date preset with the requested timezone and reference year', () => { const options = { locale: 'en-US', timeZone: 'America/Los_Angeles', now: new Date('2026-01-08T07:30:00Z') }; const value = '2026-01-01T07:30:00Z'; expect(formatDate(value, 'relative-time', options)).toBe('Dec 31, 2025'); expect(formatDate(value, 'relative-time', options)).toBe(formatDate(value, 'date', options)); }); }); describe('when a time-only timestamp requires second precision', () => { it.each([ ['en-US', 'UTC', '2:32:05 PM'], ['en-GB', 'UTC', '14:32:05'], ['en-US', 'America/Los_Angeles', '7:32:05 AM'], ])('preserves seconds in %s and %s', (locale, timeZone, expected) => { expect(formatDate('2026-09-24T14:32:05Z', 'time-seconds', { locale, timeZone })).toBe(expected); }); it('returns no label for an invalid timestamp', () => { expect(formatDate('invalid', 'time-seconds')).toBeUndefined(); }); }); describe('when timestamps require second precision', () => { it('keeps the time of a historical score', () => { expect( formatDate('2026-09-23T10:14:12Z', 'date-time-seconds', { locale: 'en-US', timeZone: 'UTC', now: NOW }), ).toBe('Sep 23, 2026, 10:14:12 AM'); }); it('drops the year for compact list timestamps', () => { expect(formatDate('2026-09-23T10:14:12Z', 'day-time-seconds', { locale: 'en-US', timeZone: 'UTC' })).toBe( 'Sep 23, 10:14:12 AM', ); }); it('distinguishes timeline events within one minute', () => { const options = { locale: 'en-GB', timeZone: 'UTC' }; expect(formatDate('2026-09-24T10:00:00Z', 'date-time-seconds', options)).toBe('24 Sept 2026, 10:00:00'); expect(formatDate('2026-09-24T10:00:15Z', 'date-time-seconds', options)).toBe('24 Sept 2026, 10:00:15'); }); }); describe('when the requested time zone crosses a calendar boundary', () => { it('uses the requested calendar day', () => { expect( formatDate('2026-09-24T06:30:00Z', 'date', { locale: 'en-US', timeZone: 'America/Los_Angeles', now: new Date('2026-09-24T07:30:00Z'), }), ).toBe('Sep 23'); }); it('keeps the same local date across UTC midnight', () => { expect( formatDate('2026-09-24T23:30:00Z', 'date', { locale: 'en-US', timeZone: 'America/Los_Angeles', now: new Date('2026-09-25T00:30:00Z'), }), ).toBe('Sep 24'); }); it('includes the year when local years differ', () => { expect( formatShortDate('2026-01-01T07:30:00Z', { locale: 'en-US', timeZone: 'America/Los_Angeles', now: new Date('2026-01-01T08:30:00Z'), }), ).toBe('Dec 31, 2025'); }); it('omits the year when local years match across UTC New Year', () => { expect( formatShortDate('2025-12-31T23:30:00Z', { locale: 'en-US', timeZone: 'America/Los_Angeles', now: new Date('2026-01-01T00:30:00Z'), }), ).toBe('Dec 31'); }); }); describe('when the locale is en-US', () => { const locale = 'en-US'; it('formats the time preset with a 12-hour clock', () => { expect(formatDate(TODAY, 'time', { locale })).toBe('2:32 PM'); }); it('formats the date-time preset', () => { expect(formatDate(TODAY, 'date-time', { locale })).toBe('Sep 24, 2026, 2:32 PM'); }); it('formats date-only values in the reference year', () => { expect(formatDate(TODAY, 'date', { locale, now: NOW })).toBe('Sep 24'); expect(formatDate(THIS_YEAR, 'date', { locale, now: NOW })).toBe('Mar 5'); expect(formatDate(LAST_YEAR, 'date', { locale, now: NOW })).toBe('Sep 24, 2025'); }); it('formats short dates without time', () => { expect(formatShortDate(THIS_YEAR, { locale, now: NOW })).toBe('Mar 5'); expect(formatShortDate(LAST_YEAR, { locale, now: NOW })).toBe('Sep 24, 2025'); }); }); describe('when the locale is fr-FR', () => { const locale = 'fr-FR'; it('follows the locale 24-hour clock', () => { expect(formatDate(TODAY, 'time', { locale })).toBe('14:32'); expect(formatDate(TODAY, 'date-time', { locale })).toContain('14:32'); }); }); describe('when the value is unusable', () => { it('returns undefined', () => { expect(formatDate(undefined, 'date-time')).toBeUndefined(); expect(formatDate('not-a-date', 'date')).toBeUndefined(); }); }); }); describe('formatDate hours', () => { it.each([ ['en-US', '9:05 AM', 'Sep 24, 9:05 AM'], ['en-GB', '09:05', '24 Sept, 09:05'], ['es-ES', '9:05', '24 sept, 9:05'], ])('pads them only where %s does', (locale, time, dayTime) => { const nineOhFive = new Date(2026, 8, 24, 9, 5); expect(formatDate(nineOhFive, 'time', { locale })).toBe(time); expect(formatDate(nineOhFive, 'day-time', { locale })).toBe(dayTime); }); }); describe('formatClock', () => { it.each([ ['en-US', '2 PM', '2:05 PM', '12 AM'], ['en-GB', '14:00', '14:05', '00:00'], ['de-DE', '14:00', '14:05', '00:00'], ])('keeps %s times short but readable as times', (locale, onTheHour, withMinutes, midnight) => { const label = (hours: number, minutes: number) => formatClock(new Date(2026, 8, 24, hours, minutes), { locale }); expect([label(14, 0), label(14, 5), label(0, 0)]).toEqual([onTheHour, withMinutes, midnight]); }); it('returns undefined when unusable', () => { expect(formatClock('not-a-date')).toBeUndefined(); }); }); describe('formatTimestampPrecise', () => { it('keeps milliseconds', () => { expect(formatTimestampPrecise(new Date('2026-01-05T14:03:07.250Z'), { locale: 'en-US' })).toBe( 'Jan 5, 2026, 2:03:07.250 PM', ); }); it('returns undefined when unusable', () => { expect(formatTimestampPrecise(null)).toBeUndefined(); }); }); describe('formatMoment', () => { const now = new Date(2026, 9, 28, 9, 0, 0); const at = (month: number, day: number, year = 2026) => new Date(year, month, day, 22, 44, 44); it.each([ ['earlier today', at(9, 28), 'Today at 10:44:44 PM'], ['just before midnight yesterday', at(9, 27), 'Yesterday at 10:44:44 PM'], ['earlier this year', at(9, 21), 'Oct 21 at 10:44:44 PM'], ['in another year', at(11, 31, 2025), 'Dec 31, 2025 at 10:44:44 PM'], ])('names a moment %s by its day and clock', (_, time, expected) => { expect(formatMoment(time, { now, locale: 'en-US' })?.replace(/\u202f/g, ' ')).toBe(expected); }); }); describe('formatLogLineTimestamp', () => { const at = '2026-11-24T23:25:51.256Z'; it.each([ ['splits a log time into its day, its 24-hour time and its hundredths of a second', 'en-US', 'Nov 24', '.25'], ['keeps the locale order and separators', 'de-DE', '24. Nov.', ',25'], ] as const)('%s', (_title, locale, day, fraction) => { expect(formatLogLineTimestamp(at, { locale, timeZone: 'UTC' })).toEqual({ day, time: '23:25:51', fraction, }); }); it('pads the hour even where the locale does not', () => { const early = '2026-11-24T09:25:51.256Z'; expect(formatLogLineTimestamp(early, { locale: 'es-ES', timeZone: 'UTC' })?.time).toBe('09:25:51'); }); it('returns undefined for a value that is not a date', () => { expect(formatLogLineTimestamp('not-a-date')).toBeUndefined(); }); it('shows as many digits after the seconds as asked', () => { expect(formatLogLineTimestamp(at, { locale: 'en-US', timeZone: 'UTC', fractionDigits: 3 })?.fraction).toBe('.256'); expect(formatLogLineTimestamp(at, { locale: 'en-US', timeZone: 'UTC', fractionDigits: 1 })?.fraction).toBe('.2'); expect(formatTimestampPrecise(at, { locale: 'en-US', timeZone: 'UTC', withDate: false, fractionDigits: 2 })).toBe( '11:25:51.25 PM', ); }); });