import { describe, expect, test } from 'vitest';
import {
  fileMatchesAccept,
  formatFileSize,
  validateFile,
  validateFiles,
} from '../file-validation';

describe('formatFileSize', () => {
  test('formats bytes', () => {
    expect(formatFileSize(0)).toBe('0 B');
    expect(formatFileSize(1024)).toBe('1.0 KB');
    expect(formatFileSize(500 * 1024 * 1024)).toMatch(/MB/);
  });
});

describe('fileMatchesAccept', () => {
  test('matches extension', () => {
    const f = new File([''], 'x.PDF', { type: 'application/pdf' });
    expect(fileMatchesAccept(f, '.pdf')).toBe(true);
    expect(fileMatchesAccept(f, '.docx')).toBe(false);
  });

  test('matches mime wildcard', () => {
    const f = new File([''], 'x.png', { type: 'image/png' });
    expect(fileMatchesAccept(f, 'image/*')).toBe(true);
    expect(fileMatchesAccept(f, 'application/pdf')).toBe(false);
  });
});

describe('validateFile', () => {
  test('rejects oversize', () => {
    const f = new File(['xx'], 'a.txt', { type: 'text/plain' });
    expect(validateFile(f, { maxBytes: 1 })).toMatch(/maximum size/i);
  });

  test('rejects wrong type', () => {
    const f = new File(['x'], 'a.exe', { type: 'application/octet-stream' });
    expect(validateFile(f, { accept: '.pdf' })).toBe('Unsupported file type');
  });
});

describe('validateFiles', () => {
  test('splits valid and errors', () => {
    const good = new File([''], 'a.pdf', { type: 'application/pdf' });
    const bad = new File([''], 'a.exe', { type: 'application/octet-stream' });
    const { valid, errors } = validateFiles([good, bad], { accept: '.pdf' });
    expect(valid).toHaveLength(1);
    expect(errors).toHaveLength(1);
  });
});
