import { describe, it, expect } from 'vitest';
import { extensionOf, isSupportedFormat, isAcceptedForType } from './formats';

describe('extensionOf', () => {
  it('detects double extensions before the last dot', () => {
    expect(extensionOf('brain.nii.gz')).toBe('.nii.gz');
    expect(extensionOf('a/b/brain.NII.GZ')).toBe('.nii.gz');
  });
  it('lower-cases and handles single extensions', () => {
    expect(extensionOf('IMG001.DCM')).toBe('.dcm');
    expect(extensionOf('photo.PNG')).toBe('.png');
  });
  it('returns empty string when there is no extension', () => {
    expect(extensionOf('README')).toBe('');
  });
});

describe('isSupportedFormat (per-pipeline, conflict #1)', () => {
  it('accepts MHA/MHD on Direct but not Cloud', () => {
    expect(isSupportedFormat('direct', 'vol.mha')).toBe(true);
    expect(isSupportedFormat('direct', 'vol.mhd')).toBe(true);
    expect(isSupportedFormat('cloud', 'vol.mha')).toBe(false);
    expect(isSupportedFormat('cloud', 'vol.mhd')).toBe(false);
  });
  it('accepts the common imaging formats on both pipelines', () => {
    for (const p of ['direct', 'cloud'] as const) {
      expect(isSupportedFormat(p, 'a.dcm')).toBe(true);
      expect(isSupportedFormat(p, 'b.nii.gz')).toBe(true);
      expect(isSupportedFormat(p, 'c.png')).toBe(true);
    }
  });
  it('rejects unsupported formats', () => {
    expect(isSupportedFormat('direct', 'notes.txt')).toBe(false);
    expect(isSupportedFormat('cloud', 'archive.zip')).toBe(false);
  });
});

describe('isAcceptedForType (D3 staging gate)', () => {
  it('accepts only the chosen type’s formats', () => {
    expect(isAcceptedForType('dicom', 'series.dcm')).toBe(true);
    expect(isAcceptedForType('dicom', 'clip.mp4')).toBe(false);
    expect(isAcceptedForType('nifti', 'brain.nii.gz')).toBe(true);
    expect(isAcceptedForType('image2d', 'slide.png')).toBe(true);
    expect(isAcceptedForType('video', 'clip.mp4')).toBe(true);
  });
  it('allows MHD/MHA + raw companions alongside volume types', () => {
    expect(isAcceptedForType('nifti', 'vol.mhd')).toBe(true);
    expect(isAcceptedForType('nifti', 'vol.raw')).toBe(true);
    expect(isAcceptedForType('image2d', 'vol.mhd')).toBe(false);
  });
});
