pub mod constants;
pub mod mxe_key_recovery_finalize;
pub mod mxe_key_recovery_init;
pub mod mxe_keygen;

#[cfg(test)]
mod tests {
    use crate::{
        core::circuits::pre_compiled::constants::ARTIFACTS_DIR,
        profile_info::{PerformanceStore, PerformanceTracker},
        ArcisInstruction,
        AsyncMPCCircuit,
    };
    use std::fs;
    #[test]
    fn track_pre_compiled_performance() {
        let strict_mode = std::env::var("STRICT_PERF_TRACKING").is_ok();
        let perf_path = format!("{ARTIFACTS_DIR}/perf.csv");
        let mut perf_store = PerformanceStore::from_csv(strict_mode, perf_path.as_str())
            .expect("CSV reading failed");
        let mut n_circuits = 0;
        for entry in fs::read_dir(ARTIFACTS_DIR).expect("Failed to read ARTIFACTS_DIR") {
            let entry = entry.expect("Failed to entry");
            let mut path = entry.path();
            if !path.is_dir() {
                continue;
            }
            let suffix = "circuit.arcis";
            path.push(suffix);
            if !path.is_file() {
                continue;
            }
            let Some(path_str) = path.as_os_str().to_str() else {
                continue;
            };
            let circuit_name =
                &path_str[(ARTIFACTS_DIR.len() + 1)..(path_str.len() - suffix.len() - 1)];

            let circuit = fs::read(path_str).expect("Failed to read stored circuit");
            let circuit: AsyncMPCCircuit =
                bincode::deserialize(&circuit).expect("Deserialization failed");
            let circuit = ArcisInstruction {
                circuit,
                metadata: Default::default(),
            };
            perf_store
                .track(circuit_name, circuit.profile_info())
                .expect("Performance issue:");
            n_circuits += 1;
        }
        perf_store.to_csv(&perf_path).expect("CSV writing failed");
        assert_eq!(
            n_circuits,
            perf_store.get_instructions().len(),
            "An entry in perf.csv was not used."
        );
    }
}
