//! Unit tests for Disk profiling module

#[cfg(test)]
mod tests {
    use super::super::*;
    use tempfile::TempDir;

    #[test]
    fn test_disk_profiler_creation() {
        let temp_dir = TempDir::new().unwrap();
        let profiler = DiskProfiler::new(temp_dir.path());
        assert!(profiler.is_ok());
    }

    #[test]
    fn test_disk_profiler_with_size() {
        let temp_dir = TempDir::new().unwrap();
        let profiler = DiskProfiler::new(temp_dir.path())
            .unwrap()
            .with_test_file_size(10 * 1024 * 1024); // 10MB

        let result = profiler.run_benchmark();
        assert!(result.is_ok());
    }

    #[test]
    fn test_sequential_read() {
        let temp_dir = TempDir::new().unwrap();
        let result = measure_sequential_read(temp_dir.path(), 1024 * 1024); // 1MB
        assert!(result.is_ok());

        let seq = result.unwrap();
        assert!(seq.throughput_mbps >= 0.0);
        assert!(seq.total_bytes > 0);
    }

    #[test]
    fn test_sequential_write() {
        let temp_dir = TempDir::new().unwrap();
        let result = measure_sequential_write(temp_dir.path(), 1024 * 1024); // 1MB
        assert!(result.is_ok());

        let seq = result.unwrap();
        assert!(seq.throughput_mbps >= 0.0);
        assert!(seq.total_bytes > 0);
    }

    #[test]
    fn test_random_read() {
        let temp_dir = TempDir::new().unwrap();
        let result = measure_random_read(
            temp_dir.path(),
            1024 * 1024,
            crate::profiling::types::BlockSize::Bytes4K,
        );
        assert!(result.is_ok());

        let random = result.unwrap();
        assert!(random.throughput_mbps >= 0.0);
        assert!(random.iops >= 0.0);
    }

    #[test]
    fn test_random_write() {
        let temp_dir = TempDir::new().unwrap();
        let result = measure_random_write(
            temp_dir.path(),
            1024 * 1024,
            crate::profiling::types::BlockSize::Bytes4K,
        );
        assert!(result.is_ok());

        let random = result.unwrap();
        assert!(random.throughput_mbps >= 0.0);
        assert!(random.iops >= 0.0);
    }

    #[test]
    fn test_iops_measurement() {
        let temp_dir = TempDir::new().unwrap();
        let result = measure_iops(temp_dir.path());
        assert!(result.is_ok());

        let iops = result.unwrap();
        assert!(iops.read_iops >= 0.0);
        assert!(iops.write_iops >= 0.0);
        assert!(iops.mixed_iops >= 0.0);
    }

    #[test]
    fn test_disk_latency() {
        let temp_dir = TempDir::new().unwrap();
        let result = crate::profiling::disk::latency::test_disk_latency(temp_dir.path());
        assert!(result.is_ok());

        let latency = result.unwrap();
        assert!(latency.read_latency_p50_us >= 0.0);
        assert!(latency.write_latency_p50_us >= 0.0);
    }

    #[test]
    fn test_run_disk_benchmark_convenience() {
        let temp_dir = TempDir::new().unwrap();
        let result = run_disk_benchmark(temp_dir.path());
        assert!(result.is_ok());

        let benchmark = result.unwrap();
        assert!(benchmark.sequential_read.throughput_mbps >= 0.0);
        assert!(benchmark.sequential_write.throughput_mbps >= 0.0);
    }

    #[test]
    fn test_run_disk_benchmark_with_size() {
        let temp_dir = TempDir::new().unwrap();
        let result = run_disk_benchmark_with_size(temp_dir.path(), 5 * 1024 * 1024);
        assert!(result.is_ok());
    }
}
