import { describe, it, expect } from 'vitest';
import {
  formatFileSize,
  isPreviewableFileType,
  getFileIconType,
  validateFileType,
  validateFileSize
} from '../../src/utils';

describe('工具函数', () => {
  describe('formatFileSize', () => {
    it('应该格式化文件大小', () => {
      expect(formatFileSize(0)).toBe('0 Bytes');
      expect(formatFileSize(1024)).toBe('1 KB');
      expect(formatFileSize(1048576)).toBe('1 MB');
      expect(formatFileSize(1073741824)).toBe('1 GB');
      expect(formatFileSize(1099511627776)).toBe('1 TB');
    });

    it('应该处理小数文件大小', () => {
      expect(formatFileSize(1536)).toBe('1.5 KB');
      expect(formatFileSize(1572864)).toBe('1.5 MB');
    });
  });

  describe('isPreviewableFileType', () => {
    it('应该检查文件类型是否可预览', () => {
      expect(isPreviewableFileType('image/jpeg')).toBe(true);
      expect(isPreviewableFileType('image/png')).toBe(true);
      expect(isPreviewableFileType('video/mp4')).toBe(true);
      expect(isPreviewableFileType('video/quicktime')).toBe(true);
      expect(isPreviewableFileType('application/pdf')).toBe(false);
      expect(isPreviewableFileType('text/plain')).toBe(false);
      expect(isPreviewableFileType(null)).toBe(false);
    });
  });

  describe('getFileIconType', () => {
    it('应该返回正确的文件图标类型', () => {
      expect(getFileIconType('image/jpeg')).toBe('image');
      expect(getFileIconType('video/mp4')).toBe('video');
      expect(getFileIconType('audio/mp3')).toBe('audio');
      expect(getFileIconType('application/pdf')).toBe('pdf');
      expect(getFileIconType('application/msword')).toBe('document');
      expect(getFileIconType('application/vnd.ms-excel')).toBe('spreadsheet');
      expect(getFileIconType('text/plain')).toBe('text');
      expect(getFileIconType('application/octet-stream')).toBe('other');
    });
  });

  describe('validateFileType', () => {
    it('应该验证文件类型', () => {
      const imageFile = new File([''], 'test.jpg', { type: 'image/jpeg' });
      const pdfFile = new File([''], 'test.pdf', { type: 'application/pdf' });

      // 接受所有文件类型
      expect(validateFileType(imageFile, '*/*')).toBe(true);
      expect(validateFileType(pdfFile, '*/*')).toBe(true);

      // 接受特定MIME类型
      expect(validateFileType(imageFile, 'image/*')).toBe(true);
      expect(validateFileType(pdfFile, 'image/*')).toBe(false);

      // 接受文件扩展名
      expect(validateFileType(imageFile, '.jpg,.png')).toBe(true);
      expect(validateFileType(pdfFile, '.jpg,.png')).toBe(false);

      // 混合类型
      expect(validateFileType(imageFile, 'image/*,.pdf')).toBe(true);
      expect(validateFileType(pdfFile, 'image/*,.pdf')).toBe(true);
    });

    it('应该在没有accept参数时返回true', () => {
      const file = new File([''], 'test.txt', { type: 'text/plain' });
      expect(validateFileType(file)).toBe(true);
    });
  });

  describe('validateFileSize', () => {
    it('应该验证文件大小', () => {
      const smallFile = new File([''], 'small.txt', { type: 'text/plain' });
      Object.defineProperty(smallFile, 'size', { value: 1024 * 1024 }); // 1MB

      const largeFile = new File([''], 'large.txt', { type: 'text/plain' });
      Object.defineProperty(largeFile, 'size', { value: 10 * 1024 * 1024 }); // 10MB

      expect(validateFileSize(smallFile, 5)).toBe(true); // 5MB限制
      expect(validateFileSize(largeFile, 5)).toBe(false); // 5MB限制
    });

    it('应该处理边界情况', () => {
      const exactSizeFile = new File([''], 'exact.txt', { type: 'text/plain' });
      Object.defineProperty(exactSizeFile, 'size', { value: 5 * 1024 * 1024 }); // 5MB

      expect(validateFileSize(exactSizeFile, 5)).toBe(true);
    });
  });
});