//! Unit tests for the `crate::common::error` module.

use rstest::*;

use super::*;
use crate::common::types::ErrorSource;

#[test]
fn test_debug_formatting() {
    let error = WebTransportError::Protocol(None, "debug check".to_owned());
    let debug_str = format!("{error:?}");

    assert!(debug_str.contains("Protocol"));
    assert!(debug_str.contains("debug check"));
}

#[rstest]
#[case(
    WebTransportError::Protocol(None, "proto".into()),
    ErrorSource::Protocol
)]
#[case(
    WebTransportError::Unknown(None, "unknown".into()),
    ErrorSource::Unspecified
)]
fn test_error_source_mapping(#[case] error: WebTransportError, #[case] expected: ErrorSource) {
    assert_eq!(error.source(), expected);
}

#[test]
fn test_protocol_error_formatting() {
    let error = WebTransportError::Protocol(Some(1), "frame error".to_owned());

    assert_eq!(
        error.to_string(),
        "Protocol error: frame error (code: Some(1))"
    );
}

#[test]
fn test_unknown_error_formatting() {
    let error = WebTransportError::Unknown(Some(999), "mystery".to_owned());

    assert_eq!(
        error.to_string(),
        "Unknown error: mystery (code: Some(999))"
    );
}
