import { describe, it, expect } from 'vitest';
import { validateDirectStaging, canToggleGroupByStudy } from './guards';
import { initialDirectContext, type DirectContext } from './states';
import type { StagedFile } from '../types';

const sf = (p: string): StagedFile => ({ name: p.slice(p.lastIndexOf('/') + 1), relativePath: p, size: 1 });
const ctx = (over: Partial<DirectContext>): DirectContext => ({ ...initialDirectContext, ...over });

describe('validateDirectStaging', () => {
  it('errors when no type is chosen', () => {
    expect(validateDirectStaging(ctx({})).length).toBeGreaterThan(0);
  });
  it('errors when there are no files', () => {
    expect(validateDirectStaging(ctx({ dataType: 'dicom' })).some((e) => e.code === 'INVALID_STRUCTURE')).toBe(true);
  });
  it('D3: rejects a file whose format is wrong for the chosen type', () => {
    const errs = validateDirectStaging(ctx({ dataType: 'dicom', files: [sf('clip.mp4')] }));
    expect(errs.some((e) => e.code === 'UNSUPPORTED_FORMAT')).toBe(true);
  });
  it('D6: rejects an .mhd without its raw companion', () => {
    const errs = validateDirectStaging(ctx({ dataType: 'nifti', files: [sf('vol.mhd')] }));
    expect(errs.some((e) => e.code === 'MHD_COMPANION_MISSING')).toBe(true);
  });
  it('passes a clean DICOM series', () => {
    expect(validateDirectStaging(ctx({ dataType: 'dicom', files: [sf('s1/i1.dcm'), sf('s1/i2.dcm')] }))).toEqual([]);
  });
});

describe('canToggleGroupByStudy (D2)', () => {
  it('is true only for DICOM/NIfTI', () => {
    expect(canToggleGroupByStudy(ctx({ dataType: 'dicom' }))).toBe(true);
    expect(canToggleGroupByStudy(ctx({ dataType: 'nifti' }))).toBe(true);
    expect(canToggleGroupByStudy(ctx({ dataType: 'image2d' }))).toBe(false);
    expect(canToggleGroupByStudy(ctx({ dataType: null }))).toBe(false);
  });
});
