//! Integration tests for M7P4 Operational Visibility — structured error context.
//!
//! - Task 06: Structured error context
#![allow(clippy::too_many_lines, clippy::unwrap_used)]

use tidaldb::schema::EntityKind;

// ── Task 06: Structured Error Context ────────────────────────────────────────

/// `TidalError::Internal` wraps `ErrorContext` with operation + detail fields.
#[test]
fn tidal_error_internal_wraps_error_context() {
    use tidaldb::TidalError;

    let err = TidalError::internal("test_operation", "something went wrong");

    if let TidalError::Internal(ctx) = &err {
        assert_eq!(ctx.operation, "test_operation");
        assert_eq!(ctx.detail, "something went wrong");
        assert!(ctx.entity_id.is_none(), "entity_id should default to None");
        assert!(
            ctx.entity_kind.is_none(),
            "entity_kind should default to None"
        );
        assert!(
            ctx.signal_type.is_none(),
            "signal_type should default to None"
        );
    } else {
        panic!("expected TidalError::Internal, got {err:?}");
    }

    // Display must include the operation name.
    let display = format!("{err}");
    assert!(
        display.contains("test_operation"),
        "Display must include the operation name: {display}"
    );
}

/// `ErrorContext` builder pattern sets optional fields correctly.
#[test]
fn error_context_builder_sets_optional_fields() {
    use tidaldb::ErrorContext;

    let ctx = ErrorContext::new("write_signal", "WAL append failed")
        .with_entity(42)
        .with_kind(EntityKind::Item)
        .with_signal("view");

    assert_eq!(ctx.operation, "write_signal");
    assert_eq!(ctx.detail, "WAL append failed");
    assert_eq!(ctx.entity_id, Some(42));
    assert_eq!(ctx.entity_kind, Some(EntityKind::Item));
    assert_eq!(ctx.signal_type, Some("view".to_string()));
}
