/**
 * Date Utils 单元测试
 */
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { formatDate, formatDateTime, formatRelativeTime } from '../../../src/utils/date';

describe('Date Utils', () => {
  beforeEach(() => {
    // 设置固定的当前时间用于测试
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2024-01-15T12:00:00Z'));
  });

  afterEach(() => {
    vi.useRealTimers();
  });

  describe('formatDate', () => {
    it('should format Date object correctly', () => {
      const date = new Date('2024-01-15T10:30:00Z');
      const result = formatDate(date);
      expect(result).toMatch(/2024-01-15/);
    });

    it('should format date string correctly', () => {
      const dateStr = '2024-01-15T10:30:00Z';
      const result = formatDate(dateStr);
      expect(result).toMatch(/2024-01-15/);
    });

    it('should pad single digit month and day with zero', () => {
      const date = new Date('2024-03-05T10:30:00Z');
      const result = formatDate(date);
      expect(result).toMatch(/2024-03-05/);
    });
  });

  describe('formatDateTime', () => {
    it('should format Date object with time', () => {
      const date = new Date('2024-01-15T10:30:45Z');
      const result = formatDateTime(date);
      expect(result).toMatch(/2024-01-15 \d{2}:\d{2}:\d{2}/);
    });

    it('should format date string with time', () => {
      const dateStr = '2024-01-15T10:30:45Z';
      const result = formatDateTime(dateStr);
      expect(result).toMatch(/2024-01-15 \d{2}:\d{2}:\d{2}/);
    });

    it('should pad single digit hours, minutes, and seconds with zero', () => {
      const date = new Date('2024-01-15T01:05:09Z');
      const result = formatDateTime(date);
      expect(result).toMatch(/\d{2}:\d{2}:\d{2}/);
    });
  });

  describe('formatRelativeTime', () => {
    it('should return "刚刚" for very recent time', () => {
      const date = new Date('2024-01-15T11:59:50Z'); // 10 seconds ago
      const result = formatRelativeTime(date);
      expect(result).toBe('刚刚');
    });

    it('should return minutes for recent time', () => {
      const date = new Date('2024-01-15T11:45:00Z'); // 15 minutes ago
      const result = formatRelativeTime(date);
      expect(result).toBe('15分钟前');
    });

    it('should return hours for time within a day', () => {
      const date = new Date('2024-01-15T09:00:00Z'); // 3 hours ago
      const result = formatRelativeTime(date);
      expect(result).toBe('3小时前');
    });

    it('should return days for time within a week', () => {
      const date = new Date('2024-01-13T12:00:00Z'); // 2 days ago
      const result = formatRelativeTime(date);
      expect(result).toBe('2天前');
    });

    it('should return formatted date for time over a week', () => {
      const date = new Date('2024-01-01T12:00:00Z'); // 14 days ago
      const result = formatRelativeTime(date);
      expect(result).toMatch(/2024-01-01/);
    });

    it('should handle string date input', () => {
      const dateStr = '2024-01-15T11:45:00Z';
      const result = formatRelativeTime(dateStr);
      expect(result).toBe('15分钟前');
    });
  });
});
