autoeq-plot 0.5.5

Plotly visualizations and report generation for AutoEQ
Documentation
use super::create::{
    create_cea2034_combined_traces, create_cea2034_traces, create_cea2034_with_eq_combined_traces,
    create_cea2034_with_eq_traces,
};
use super::misc::shorten_curve_name;
use crate::ref_lines::make_ref_lines;
use ndarray::Array1;
use serde_json::json;
use serde_json::to_value as to_json;
use std::collections::HashMap;

#[test]
fn test_create_cea2034_traces() {
    // Create mock CEA2034 curves
    let mut curves = HashMap::new();

    // Create a simple frequency grid
    let freq = Array1::from(vec![20.0, 100.0, 1000.0, 10000.0, 20000.0]);
    let spl = Array1::from(vec![80.0, 85.0, 90.0, 85.0, 80.0]);

    // Add mock curves for the primary CEA2034 set used by create_cea2034_traces
    curves.insert(
        "On Axis".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Listening Window".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Early Reflections".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Sound Power".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl.clone(),
            phase: None,
            ..Default::default()
        },
    );

    // Test creating CEA2034 traces
    let traces = create_cea2034_traces(&curves);

    // Should have 4 traces
    assert_eq!(traces.len(), 4);

    // Test creating CEA2034 traces with EQ
    let eq_response = Array1::from(vec![1.0, 1.0, 1.0, 1.0, 1.0]);
    let eq_traces = create_cea2034_with_eq_traces(&curves, &eq_response);

    // Should have 4 traces
    assert_eq!(eq_traces.len(), 4);
}

#[test]
fn test_make_ref_lines_values() {
    let lines = make_ref_lines("x3", "y3");
    assert_eq!(lines.len(), 2);
    let v0 = to_json(&lines[0]).unwrap();
    let v1 = to_json(&lines[1]).unwrap();
    assert_eq!(v0["x"], json!([100.0, 10000.0]));
    assert_eq!(v1["x"], json!([100.0, 10000.0]));
    assert_eq!(v0["y"], json!([1.0, 1.0]));
    assert_eq!(v1["y"], json!([-1.0, -1.0]));
}

#[test]
fn test_create_cea2034_combined_traces_counts_and_axes() {
    // Build minimal curves covering names used by combined function
    let mut curves = HashMap::new();
    let freq = Array1::from(vec![100.0, 1000.0, 10000.0]);
    let spl_primary = Array1::from(vec![80.0, 85.0, 82.0]);
    let spl_di = Array1::from(vec![5.0, 6.0, 7.0]);

    // Primary curves
    curves.insert(
        "On Axis".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_primary.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Listening Window".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_primary.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Early Reflections".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_primary.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Sound Power".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_primary.clone(),
            phase: None,
            ..Default::default()
        },
    );

    // DI curves
    curves.insert(
        "Early Reflections DI".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_di.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Sound Power DI".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_di.clone(),
            phase: None,
            ..Default::default()
        },
    );

    let traces = create_cea2034_combined_traces(&curves, "x7", "y7", "y7");
    assert_eq!(traces.len(), 6);

    // Check that DI traces target the secondary axis
    let v = to_json(&traces).unwrap();
    let names: Vec<String> = v
        .as_array()
        .unwrap()
        .iter()
        .map(|t| t["name"].as_str().unwrap().to_string())
        .collect();
    assert!(names.contains(&"ERDI".to_string()));
    assert!(names.contains(&"SPDI".to_string()));

    // Find DI entries and ensure yaxis is y7 (DI shares primary axis in current implementation)
    for t in v.as_array().unwrap() {
        let n = t["name"].as_str().unwrap();
        if n.ends_with(" DI") {
            assert_eq!(t["yaxis"], json!("y7"));
        }
    }
}

#[test]
fn test_create_cea2034_with_eq_combined_traces_counts_and_names() {
    let mut curves = HashMap::new();
    let freq = Array1::from(vec![100.0, 1000.0, 10000.0]);
    let spl_primary = Array1::from(vec![80.0, 85.0, 82.0]);
    let spl_di = Array1::from(vec![5.0, 6.0, 7.0]);

    // Primary curves
    curves.insert(
        "On Axis".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_primary.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Listening Window".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_primary.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Early Reflections".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_primary.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Sound Power".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_primary.clone(),
            phase: None,
            ..Default::default()
        },
    );
    // DI
    curves.insert(
        "Early Reflections DI".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_di.clone(),
            phase: None,
            ..Default::default()
        },
    );
    curves.insert(
        "Sound Power DI".to_string(),
        crate::Curve {
            freq: freq.clone(),
            spl: spl_di.clone(),
            phase: None,
            ..Default::default()
        },
    );

    let eq = Array1::from(vec![1.0, -1.0, 0.5]);
    let traces = create_cea2034_with_eq_combined_traces(&curves, &eq, "x8", "y8", "y8");
    assert_eq!(traces.len(), 6);
    let v = to_json(&traces).unwrap();
    // Primary names should have suffix w/EQ, DI should not
    let names: Vec<String> = v
        .as_array()
        .unwrap()
        .iter()
        .map(|t| t["name"].as_str().unwrap().to_string())
        .collect();
    assert!(names.iter().any(|n| n == "ON w/EQ"));
    assert!(names.iter().any(|n| n == "LW w/EQ"));
    assert!(names.iter().any(|n| n == "ER w/EQ"));
    assert!(names.iter().any(|n| n == "SP w/EQ"));
    assert!(names.iter().any(|n| n == "ERDI"));
    assert!(names.iter().any(|n| n == "SPDI"));
    // DI yaxis should be y8 (shares primary axis in current implementation)
    for t in v.as_array().unwrap() {
        let n = t["name"].as_str().unwrap();
        if n.ends_with(" DI") {
            assert_eq!(t["yaxis"], json!("y8"));
        }
    }
}

#[test]
fn test_shorten_curve_name() {
    // Test basic curve name abbreviations
    assert_eq!(shorten_curve_name("On Axis"), "ON");
    assert_eq!(shorten_curve_name("Listening Window"), "LW");
    assert_eq!(shorten_curve_name("Early Reflections"), "ER");
    assert_eq!(shorten_curve_name("Sound Power"), "SP");
    assert_eq!(shorten_curve_name("Estimated In-Room Response"), "PIR");

    // Test DI curve abbreviations
    assert_eq!(shorten_curve_name("Early Reflections DI"), "ERDI");
    assert_eq!(shorten_curve_name("Sound Power DI"), "SPDI");

    // Test unknown curve name (should return original)
    assert_eq!(shorten_curve_name("Unknown Curve"), "Unknown Curve");
    assert_eq!(shorten_curve_name(""), "");

    // Test case sensitivity (should return original since no exact match)
    assert_eq!(shorten_curve_name("on axis"), "on axis");
    assert_eq!(shorten_curve_name("ON AXIS"), "ON AXIS");
}

#[test]
fn test_shorten_curve_name_edge_cases() {
    // Long unknown name should be returned unchanged
    let long_name = "A".repeat(200);
    assert_eq!(shorten_curve_name(&long_name), long_name.as_str());

    // Whitespace-only unknown name
    assert_eq!(shorten_curve_name("   "), "   ");

    // Names containing special characters
    assert_eq!(shorten_curve_name("On Axis @ 0°"), "On Axis @ 0°");

    // Partial match should not abbreviate
    assert_eq!(shorten_curve_name("On Axis DI"), "On Axis DI");
    assert_eq!(shorten_curve_name("Sound Power X"), "Sound Power X");
}