import { describe, it, expect } from 'vitest'; import { formatMinutesAfter, toLocalDatetimeInputValue, formatElapsedTime, clampToDay, parseTime, formatMinutes, getDaysAgo, getToday, } from '../../src/utils/time'; describe('formatMinutesAfter', () => { it('converts positive minutes after reference', () => { expect(formatMinutesAfter(75, '04:00')).toBe('05:15'); }); it('handles zero minutes', () => { expect(formatMinutesAfter(0, '04:00')).toBe('04:00'); }); it('wraps around midnight for large values', () => { expect(formatMinutesAfter(480, '04:00')).toBe('12:00'); }); it('handles null input', () => { expect(formatMinutesAfter(null, '04:00')).toBe('N/A'); }); it('handles negative minutes', () => { expect(formatMinutesAfter(-30, '04:00')).toBe('03:30'); }); }); describe('toLocalDatetimeInputValue', () => { it('converts Date to datetime-local format', () => { const date = new Date('2024-01-15T21:30:00Z'); const result = toLocalDatetimeInputValue(date); expect(result).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/); }); }); describe('formatElapsedTime', () => { it('formats hours and minutes', () => { const twoHoursAgo = new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(); expect(formatElapsedTime(twoHoursAgo)).toBe('2h 0m'); }); it('formats minutes only', () => { const thirtyMinsAgo = new Date(Date.now() - 30 * 60 * 1000).toISOString(); expect(formatElapsedTime(thirtyMinsAgo)).toBe('30m'); }); }); describe('clampToDay', () => { it('clamps to start of day', () => { const date = new Date('2024-01-15T15:30:00'); const result = clampToDay(date, 'start'); expect(result.getHours()).toBe(0); expect(result.getMinutes()).toBe(0); }); it('clamps to end of day', () => { const date = new Date('2024-01-15T15:30:00'); const result = clampToDay(date, 'end'); expect(result.getHours()).toBe(23); expect(result.getMinutes()).toBe(59); }); }); describe('parseTime', () => { it('parses HH:MM string to Date', () => { const result = parseTime('07:30'); expect(result.getHours()).toBe(7); expect(result.getMinutes()).toBe(30); }); }); describe('formatMinutes', () => { it('formats hours and minutes', () => { expect(formatMinutes(90)).toBe('1h 30m'); }); it('formats hours only', () => { expect(formatMinutes(120)).toBe('2h'); }); it('formats minutes only', () => { expect(formatMinutes(45)).toBe('45m'); }); }); describe('getDaysAgo', () => { it('returns date string for N days ago', () => { const result = getDaysAgo(7); expect(result).toMatch(/^\d{4}-\d{2}-\d{2}$/); }); }); describe('getToday', () => { it('returns today date string', () => { const result = getToday(); const today = new Date().toISOString().split('T')[0]; expect(result).toBe(today); }); });