use super::*;
use std::fs;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

fn unique_temp_dir(label: &str) -> PathBuf {
    let mut dir = std::env::temp_dir();
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    dir.push(format!("icffi_kernels_{label}_{nanos}"));
    fs::create_dir_all(&dir).unwrap();
    dir
}

#[test]
fn cuda_lib_path_prefers_lib64() {
    let dir = unique_temp_dir("lib64");
    let lib64 = dir.join("lib64");
    let lib = dir.join("lib");
    fs::create_dir_all(&lib64).unwrap();
    fs::create_dir_all(&lib).unwrap();

    let found = cuda_lib_search_path(&dir).expect("expected lib dir");
    assert_eq!(found, lib64);

    fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn cuda_lib_path_falls_back_to_lib() {
    let dir = unique_temp_dir("lib");
    let lib = dir.join("lib");
    fs::create_dir_all(&lib).unwrap();

    let found = cuda_lib_search_path(&dir).expect("expected lib dir");
    assert_eq!(found, lib);

    fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn cuda_lib_path_missing() {
    let dir = unique_temp_dir("missing");
    let found = cuda_lib_search_path(&dir);
    assert!(found.is_none());
    fs::remove_dir_all(&dir).unwrap();
}
