import { test } from 'node:test';
import assert from 'node:assert/strict';
import { formatFileSize, isCompressible } from './imageCompression';

test('formatFileSize formats bytes below 1KB as B', () => {
  assert.equal(formatFileSize(0), '0 B');
  assert.equal(formatFileSize(1), '1 B');
  assert.equal(formatFileSize(512), '512 B');
  assert.equal(formatFileSize(1023), '1023 B');
});

test('formatFileSize formats values in the KB range', () => {
  assert.equal(formatFileSize(1024), '1.0 KB');
  assert.equal(formatFileSize(1536), '1.5 KB');
  // 1024 * 1024 - 1 is still under the MB boundary
  assert.equal(formatFileSize(1024 * 1024 - 1), '1024.0 KB');
});

test('formatFileSize formats values in the MB range', () => {
  assert.equal(formatFileSize(1024 * 1024), '1.0 MB');
  assert.equal(formatFileSize(1024 * 1024 * 1.5), '1.5 MB');
  assert.equal(formatFileSize(1024 * 1024 * 10), '10.0 MB');
});

test('isCompressible accepts raster image types', () => {
  assert.equal(isCompressible({ type: 'image/png' } as Blob), true);
  assert.equal(isCompressible({ type: 'image/jpeg' } as Blob), true);
  assert.equal(isCompressible({ type: 'image/gif' } as Blob), true);
  assert.equal(isCompressible({ type: 'image/webp' } as Blob), true);
});

test('isCompressible rejects svg, empty type, and non-images', () => {
  assert.equal(isCompressible({ type: 'image/svg+xml' } as Blob), false);
  assert.equal(isCompressible({ type: '' } as Blob), false);
  assert.equal(isCompressible({ type: 'application/pdf' } as Blob), false);
  assert.equal(isCompressible({ type: 'text/plain' } as Blob), false);
  assert.equal(isCompressible({ type: 'video/mp4' } as Blob), false);
});
