learning_ai_clock/web/src/lib/format.test.ts

68 lines
1.9 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { formatDuration, formatDurationCompact, formatRelativeTime } from './format';
describe('format', () => {
describe('formatDuration', () => {
it('formats zero', () => {
expect(formatDuration(0)).toBe('00:00');
});
it('formats seconds only', () => {
expect(formatDuration(30_000)).toBe('00:30');
});
it('formats minutes and seconds', () => {
expect(formatDuration(5 * 60_000 + 30_000)).toBe('05:30');
});
it('formats hours', () => {
expect(formatDuration(2 * 3600_000 + 15 * 60_000 + 45_000)).toBe('02:15:45');
});
it('handles negative values', () => {
expect(formatDuration(-1000)).toBe('00:00');
});
});
describe('formatDurationCompact', () => {
it('formats seconds', () => {
expect(formatDurationCompact(45_000)).toBe('45s');
});
it('formats minutes', () => {
expect(formatDurationCompact(15 * 60_000)).toBe('15m');
});
it('formats hours and minutes', () => {
expect(formatDurationCompact(2 * 3600_000 + 30 * 60_000)).toBe('2h 30m');
});
it('formats exact hours', () => {
expect(formatDurationCompact(3 * 3600_000)).toBe('3h');
});
it('handles zero', () => {
expect(formatDurationCompact(0)).toBe('0s');
});
});
describe('formatRelativeTime', () => {
it('returns "now" for small differences', () => {
const now = Date.now();
expect(formatRelativeTime(now + 5000, now)).toBe('now');
});
it('returns "in X" for future times', () => {
const now = Date.now();
const result = formatRelativeTime(now + 5 * 60_000, now);
expect(result).toMatch(/^in /);
});
it('returns "X ago" for past times', () => {
const now = Date.now();
const result = formatRelativeTime(now - 5 * 60_000, now);
expect(result).toMatch(/ ago$/);
});
});
});