use crate::camera::update_camera_config;
    use super::{voice_sink_buffer_time_us, voice_sink_latency_time_us};
    use super::{default_voice_sink_compensation_us, voice_sink_compensation_us};
    use super::{
        voice_appsrc_leaky_type, voice_appsrc_max_buffers, voice_appsrc_max_bytes,
        voice_appsrc_max_time_ns,
    };

    #[test]
    /// Keeps `voice_sink_timing_defaults_stay_live_call_friendly` explicit because it sits on this module contract, where hidden behavior would make regressions difficult to diagnose.
    /// Inputs are the typed parameters; output is the return value or side effect.
    fn voice_sink_timing_defaults_stay_live_call_friendly() {
        temp_env::with_var_unset("LESAVKA_UAC_BUFFER_TIME_US", || {
            temp_env::with_var_unset("LESAVKA_UAC_LATENCY_TIME_US", || {
                temp_env::with_var_unset("LESAVKA_UAC_COMPENSATION_US", || {
                    temp_env::with_var_unset("LESAVKA_UAC_HDMI_COMPENSATION_US", || {
                        temp_env::with_var("LESAVKA_CAM_OUTPUT", Some("uvc"), || {
                            update_camera_config();
                            assert_eq!(voice_sink_buffer_time_us(), 120_000);
                            assert_eq!(voice_sink_latency_time_us(), 40_000);
                            assert_eq!(voice_sink_compensation_us(), 0);
                        });
                    });
                });
            });
        });
    }

    #[test]
    fn voice_appsrc_limits_default_to_a_call_stability_window() {
        temp_env::with_var_unset("LESAVKA_UAC_APP_MAX_BUFFERS", || {
            temp_env::with_var_unset("LESAVKA_UAC_APP_MAX_BYTES", || {
                temp_env::with_var_unset("LESAVKA_UAC_APP_MAX_TIME_NS", || {
                    assert_eq!(voice_appsrc_max_buffers(), 16);
                    assert_eq!(voice_appsrc_max_bytes(), 65_536);
                    assert_eq!(voice_appsrc_max_time_ns(), 200_000_000);
                });
            });
        });
    }

    #[test]
    /// Keeps `voice_appsrc_limits_accept_positive_overrides_only` explicit because it sits on this module contract, where hidden behavior would make regressions difficult to diagnose.
    /// Inputs are the typed parameters; output is the return value or side effect.
    fn voice_appsrc_limits_accept_positive_overrides_only() {
        temp_env::with_var("LESAVKA_UAC_APP_MAX_BUFFERS", Some("12"), || {
            temp_env::with_var("LESAVKA_UAC_APP_MAX_BYTES", Some("65536"), || {
                temp_env::with_var("LESAVKA_UAC_APP_MAX_TIME_NS", Some("10000000"), || {
                    assert_eq!(voice_appsrc_max_buffers(), 12);
                    assert_eq!(voice_appsrc_max_bytes(), 65_536);
                    assert_eq!(voice_appsrc_max_time_ns(), 10_000_000);
                });
            });
        });

        temp_env::with_var("LESAVKA_UAC_APP_MAX_BUFFERS", Some("0"), || {
            temp_env::with_var("LESAVKA_UAC_APP_MAX_BYTES", Some("nope"), || {
                temp_env::with_var("LESAVKA_UAC_APP_MAX_TIME_NS", Some("0"), || {
                    assert_eq!(voice_appsrc_max_buffers(), 16);
                    assert_eq!(voice_appsrc_max_bytes(), 65_536);
                    assert_eq!(voice_appsrc_max_time_ns(), 200_000_000);
                });
            });
        });
    }

    #[test]
    fn voice_appsrc_defaults_to_leaky_live_audio_queue() {
        temp_env::with_var_unset("LESAVKA_UAC_APP_LEAKY_TYPE", || {
            assert_eq!(voice_appsrc_leaky_type(), "downstream");
        });

        temp_env::with_var("LESAVKA_UAC_APP_LEAKY_TYPE", Some("upstream"), || {
            assert_eq!(voice_appsrc_leaky_type(), "upstream");
        });

        temp_env::with_var("LESAVKA_UAC_APP_LEAKY_TYPE", Some("none"), || {
            assert_eq!(voice_appsrc_leaky_type(), "none");
        });

        temp_env::with_var("LESAVKA_UAC_APP_LEAKY_TYPE", Some("bogus"), || {
            assert_eq!(voice_appsrc_leaky_type(), "downstream");
        });
    }

    #[test]
    /// Keeps `voice_sink_timing_env_accepts_positive_overrides_only` explicit because it sits on this module contract, where hidden behavior would make regressions difficult to diagnose.
    /// Inputs are the typed parameters; output is the return value or side effect.
    fn voice_sink_timing_env_accepts_positive_overrides_only() {
        temp_env::with_var("LESAVKA_CAM_OUTPUT", Some("uvc"), || {
            update_camera_config();
            temp_env::with_var("LESAVKA_UAC_BUFFER_TIME_US", Some("42000"), || {
                temp_env::with_var("LESAVKA_UAC_LATENCY_TIME_US", Some("7000"), || {
                    assert_eq!(voice_sink_buffer_time_us(), 42_000);
                    assert_eq!(voice_sink_latency_time_us(), 7_000);
                    assert_eq!(voice_sink_compensation_us(), 0);
                });
            });
        });

        temp_env::with_var("LESAVKA_CAM_OUTPUT", Some("uvc"), || {
            update_camera_config();
            temp_env::with_var("LESAVKA_UAC_BUFFER_TIME_US", Some("0"), || {
                temp_env::with_var("LESAVKA_UAC_LATENCY_TIME_US", Some("-5"), || {
                    temp_env::with_var("LESAVKA_UAC_COMPENSATION_US", Some("166667"), || {
                        assert_eq!(voice_sink_buffer_time_us(), 120_000);
                        assert_eq!(voice_sink_latency_time_us(), 40_000);
                        assert_eq!(voice_sink_compensation_us(), 166_667);
                    });
                });
            });
        });

        temp_env::with_var("LESAVKA_CAM_OUTPUT", Some("uvc"), || {
            update_camera_config();
            temp_env::with_var("LESAVKA_UAC_COMPENSATION_US", Some("-5"), || {
                temp_env::with_var("LESAVKA_UAC_BUFFER_TIME_US", Some("0"), || {
                    temp_env::with_var("LESAVKA_UAC_LATENCY_TIME_US", Some("-5"), || {
                        assert_eq!(voice_sink_buffer_time_us(), 120_000);
                        assert_eq!(voice_sink_latency_time_us(), 40_000);
                        assert_eq!(voice_sink_compensation_us(), 0);
                    });
                });
            });
        });
    }

    #[test]
    fn hdmi_sink_compensation_defaults_to_hdmi_specific_delay() {
        temp_env::with_var_unset("LESAVKA_UAC_COMPENSATION_US", || {
            temp_env::with_var_unset("LESAVKA_UAC_HDMI_COMPENSATION_US", || {
                temp_env::with_var("LESAVKA_CAM_OUTPUT", Some("hdmi"), || {
                    update_camera_config();
                    assert_eq!(default_voice_sink_compensation_us(), 205_000);
                    assert_eq!(voice_sink_compensation_us(), 205_000);
                });
            });
        });
    }

    #[test]
    fn explicit_compensation_override_wins_over_hdmi_default() {
        temp_env::with_var("LESAVKA_CAM_OUTPUT", Some("hdmi"), || {
            update_camera_config();
            temp_env::with_var("LESAVKA_UAC_HDMI_COMPENSATION_US", Some("120000"), || {
                temp_env::with_var("LESAVKA_UAC_COMPENSATION_US", Some("90000"), || {
                    assert_eq!(default_voice_sink_compensation_us(), 120_000);
                    assert_eq!(voice_sink_compensation_us(), 90_000);
                });
            });
        });
    }

    #[test]
    /// Keeps `session_clock_alignment_defaults_on_and_accepts_disable_overrides` explicit because it sits on this module contract, where hidden behavior would make regressions difficult to diagnose.
    /// Inputs are the typed parameters; output is the return value or side effect.
    fn session_clock_alignment_defaults_on_and_accepts_disable_overrides() {
        temp_env::with_var_unset("LESAVKA_UAC_SESSION_CLOCK_ALIGN", || {
            assert!(super::voice_sink_session_clock_align_enabled());
        });

        for disabled in ["0", "false", "no", "off"] {
            temp_env::with_var("LESAVKA_UAC_SESSION_CLOCK_ALIGN", Some(disabled), || {
                assert!(!super::voice_sink_session_clock_align_enabled());
            });
        }

        temp_env::with_var("LESAVKA_UAC_SESSION_CLOCK_ALIGN", Some("1"), || {
            assert!(super::voice_sink_session_clock_align_enabled());
        });
    }

    #[test]
    fn delay_queue_turns_on_only_for_positive_compensation() {
        assert!(!super::voice_sink_delay_queue_enabled(-1));
        assert!(!super::voice_sink_delay_queue_enabled(0));
        assert!(super::voice_sink_delay_queue_enabled(1));
        assert!(super::voice_sink_delay_queue_enabled(90_000));
    }
