import { formatTimestamp } from '../formatTimestamp';

describe('formatTimestamp', () => {
  test('returns "Just now" for messages within last minute', () => {
    const now = new Date();
    expect(formatTimestamp(now.toISOString())).toBe('Just now');
  });

  test('returns "X minutes ago" for messages within last hour', () => {
    const fiveMinutesAgo = new Date(Date.now() - 5 * 60 * 1000);
    expect(formatTimestamp(fiveMinutesAgo.toISOString())).toBe('5 minutes ago');
  });

  test('returns "X hours ago" for messages within last day', () => {
    const twoHoursAgo = new Date(Date.now() - 2 * 60 * 60 * 1000);
    expect(formatTimestamp(twoHoursAgo.toISOString())).toBe('2 hours ago');
  });

  test('returns "Yesterday" for messages from previous day', () => {
    const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000);
    expect(formatTimestamp(yesterday.toISOString())).toMatch(/Yesterday/);
  });

  test('returns date for older messages', () => {
    const lastWeek = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
    const result = formatTimestamp(lastWeek.toISOString());
    expect(result).toMatch(/\d{1,2}\/\d{1,2}\/\d{4}/); // MM/DD/YYYY format
  });

  test('handles invalid dates gracefully', () => {
    expect(formatTimestamp(null)).toBe('');
    expect(formatTimestamp(undefined)).toBe('');
    expect(formatTimestamp('invalid')).toBe('');
  });
});
