//! HTTPClient widget tests
//!
//! HTTPClient 위젯의 통합 테스트 모음입니다.
//!
//! # 테스트 항목
//! - 생성자 및 빌더 메서드
//! - URL/메서드/헤더 설정
//! - 요청 상태 관리
//! - 렌더링 동작
//! - 응답 처리
//! - 이력 탐색
//! - 스크롤 기능
//! - RequestBuilder 플루언트 API

use revue::layout::Rect;
use revue::render::Buffer;
use revue::widget::traits::RenderContext;
use revue::widget::{
    ContentType, HttpClient, HttpBackend, HttpMethod, HttpRequest, HttpResponse, MockHttpBackend,
    RequestBuilder, RequestState, ResponseView, View,
};
use revue::widget::{http_delete, http_get, http_patch, http_post, http_put};
use std::collections::HashMap;
use std::time::Duration;

// HTTP helper functions for tests (위젯 내부 함수가 private이므로 테스트용)
fn test_base64_encode(data: &[u8]) -> String {
    const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    let mut result = String::new();
    let chunks = data.chunks(3);

    for chunk in chunks {
        let b0 = chunk[0] as usize;
        let b1 = chunk.get(1).copied().unwrap_or(0) as usize;
        let b2 = chunk.get(2).copied().unwrap_or(0) as usize;

        result.push(ALPHABET[b0 >> 2] as char);
        result.push(ALPHABET[((b0 & 0x03) << 4) | (b1 >> 4)] as char);

        if chunk.len() > 1 {
            result.push(ALPHABET[((b1 & 0x0F) << 2) | (b2 >> 6)] as char);
        } else {
            result.push('=');
        }

        if chunk.len() > 2 {
            result.push(ALPHABET[b2 & 0x3F] as char);
        } else {
            result.push('=');
        }
    }

    result
}

// =============================================================================
// Constructor Tests - 생성자 테스트
// =============================================================================

/// HttpClient::new() 기본 생성자 테스트
#[test]
fn test_http_client_new() {
    let client = HttpClient::new();
    assert_eq!(client.state(), RequestState::Idle);
    assert!(client.response().is_none());
    assert!(client.error().is_none());
    assert_eq!(client.request().url, "");
    assert_eq!(client.request().method, HttpMethod::default());
}

/// HttpClient::default() 테스트
#[test]
fn test_http_client_default() {
    let client = HttpClient::default();
    assert_eq!(client.state(), RequestState::Idle);
    assert!(client.request().headers.is_empty());
    assert!(client.request().params.is_empty());
    assert_eq!(client.request().body, "");
}

/// http_client() 헬퍼 함수 테스트
#[test]
fn test_http_client_helper() {
    let client = revue::widget::http_client();
    assert_eq!(client.state(), RequestState::Idle);
}

// =============================================================================
// URL Builder Tests - URL 빌더 테스트
// =============================================================================

/// URL 설정 빌더 메서드 테스트
#[test]
fn test_http_client_url_builder() {
    let client = HttpClient::new().url("https://api.example.com");
    assert_eq!(client.request().url, "https://api.example.com");
}

/// URL 빌더 체이닝 테스트
#[test]
fn test_http_client_url_chaining() {
    let client = HttpClient::new()
        .url("https://api.example.com")
        .url("https://other.com");
    assert_eq!(client.request().url, "https://other.com");
}

/// set_url 메서드 테스트
#[test]
fn test_http_client_set_url() {
    let mut client = HttpClient::new();
    client.set_url("https://api.example.com/users");
    assert_eq!(client.request().url, "https://api.example.com/users");
}

/// 빈 URL로 생성 테스트
#[test]
fn test_http_client_empty_url() {
    let client = HttpClient::new().url("");
    assert_eq!(client.request().url, "");
}

/// 빈 문자열에서 URL 설정 테스트
#[test]
fn test_http_client_set_empty_url() {
    let mut client = HttpClient::new().url("https://example.com");
    client.set_url("");
    assert_eq!(client.request().url, "");
}

// =============================================================================
// HTTP Method Tests - HTTP 메서드 테스트
// =============================================================================

/// GET 메서드 설정 테스트
#[test]
fn test_http_client_method_get() {
    let client = HttpClient::new().method(HttpMethod::GET);
    assert_eq!(client.request().method, HttpMethod::GET);
}

/// POST 메서드 설정 테스트
#[test]
fn test_http_client_method_post() {
    let client = HttpClient::new().method(HttpMethod::POST);
    assert_eq!(client.request().method, HttpMethod::POST);
}

/// PUT 메서드 설정 테스트
#[test]
fn test_http_client_method_put() {
    let client = HttpClient::new().method(HttpMethod::PUT);
    assert_eq!(client.request().method, HttpMethod::PUT);
}

/// DELETE 메서드 설정 테스트
#[test]
fn test_http_client_method_delete() {
    let client = HttpClient::new().method(HttpMethod::DELETE);
    assert_eq!(client.request().method, HttpMethod::DELETE);
}

/// PATCH 메서드 설정 테스트
#[test]
fn test_http_client_method_patch() {
    let client = HttpClient::new().method(HttpMethod::PATCH);
    assert_eq!(client.request().method, HttpMethod::PATCH);
}

/// HEAD 메서드 설정 테스트
#[test]
fn test_http_client_method_head() {
    let client = HttpClient::new().method(HttpMethod::HEAD);
    assert_eq!(client.request().method, HttpMethod::HEAD);
}

/// OPTIONS 메서드 설정 테스트
#[test]
fn test_http_client_method_options() {
    let client = HttpClient::new().method(HttpMethod::OPTIONS);
    assert_eq!(client.request().method, HttpMethod::OPTIONS);
}

/// get() 헬퍼 함수 테스트
#[test]
fn test_get_helper() {
    let client = http_get("https://api.example.com/users");
    assert_eq!(client.request().method, HttpMethod::GET);
    assert_eq!(client.request().url, "https://api.example.com/users");
}

/// post() 헬퍼 함수 테스트
#[test]
fn test_post_helper() {
    let client = http_post("https://api.example.com/users");
    assert_eq!(client.request().method, HttpMethod::POST);
    assert_eq!(client.request().url, "https://api.example.com/users");
}

/// put() 헬퍼 함수 테스트
#[test]
fn test_put_helper() {
    let client = http_put("https://api.example.com/users/1");
    assert_eq!(client.request().method, HttpMethod::PUT);
    assert_eq!(client.request().url, "https://api.example.com/users/1");
}

/// delete() 헬퍼 함수 테스트
#[test]
fn test_delete_helper() {
    let client = http_delete("https://api.example.com/users/1");
    assert_eq!(client.request().method, HttpMethod::DELETE);
    assert_eq!(client.request().url, "https://api.example.com/users/1");
}

/// patch() 헬퍼 함수 테스트
#[test]
fn test_patch_helper() {
    let client = http_patch("https://api.example.com/users/1");
    assert_eq!(client.request().method, HttpMethod::PATCH);
    assert_eq!(client.request().url, "https://api.example.com/users/1");
}

/// 메서드 순환 테스트 (cycle_method)
#[test]
fn test_cycle_method() {
    let mut client = HttpClient::new().method(HttpMethod::GET);

    client.cycle_method();
    assert_eq!(client.request().method, HttpMethod::POST);

    client.cycle_method();
    assert_eq!(client.request().method, HttpMethod::PUT);

    client.cycle_method();
    assert_eq!(client.request().method, HttpMethod::DELETE);

    client.cycle_method();
    assert_eq!(client.request().method, HttpMethod::PATCH);

    client.cycle_method();
    assert_eq!(client.request().method, HttpMethod::HEAD);

    client.cycle_method();
    assert_eq!(client.request().method, HttpMethod::OPTIONS);

    client.cycle_method();
    assert_eq!(client.request().method, HttpMethod::GET);
}

// =============================================================================
// Header Tests - 헤더 테스트
// =============================================================================

/// 단일 헤더 추가 테스트
#[test]
fn test_http_client_single_header() {
    let client = HttpClient::new().header("Authorization", "Bearer token123");
    assert_eq!(
        client.request().headers.get("Authorization"),
        Some(&"Bearer token123".to_string())
    );
}

/// 여러 헤더 추가 테스트
#[test]
fn test_http_client_multiple_headers() {
    let client = HttpClient::new()
        .header("Authorization", "Bearer token")
        .header("Content-Type", "application/json")
        .header("Accept", "application/json");

    assert_eq!(client.request().headers.len(), 3);
    assert_eq!(
        client.request().headers.get("Content-Type"),
        Some(&"application/json".to_string())
    );
}

/// 헤더 값 덮어쓰기 테스트
#[test]
fn test_http_client_header_override() {
    let client = HttpClient::new()
        .header("X-Custom", "value1")
        .header("X-Custom", "value2");

    assert_eq!(
        client.request().headers.get("X-Custom"),
        Some(&"value2".to_string())
    );
}

/// 빈 헤더 값 테스트
#[test]
fn test_http_client_empty_header_value() {
    let client = HttpClient::new().header("X-Empty", "");
    assert_eq!(
        client.request().headers.get("X-Empty"),
        Some(&"".to_string())
    );
}

/// Content-Type 헤더 테스트
#[test]
fn test_http_client_content_type_header() {
    let client = HttpClient::new().header("Content-Type", "application/json");
    assert_eq!(
        client.request().headers.get("Content-Type"),
        Some(&"application/json".to_string())
    );
}

/// Authorization 헤더 테스트
#[test]
fn test_http_client_authorization_header() {
    let client = HttpClient::new().header("Authorization", "Bearer abc123");
    assert!(client
        .request()
        .headers
        .get("Authorization")
        .unwrap()
        .starts_with("Bearer "));
}

// =============================================================================
// Body Tests - 바디 테스트
// =============================================================================

/// 바디 설정 테스트
#[test]
fn test_http_client_body() {
    let client = HttpClient::new().body(r#"{"name":"test"}"#);
    assert_eq!(client.request().body, r#"{"name":"test"}"#);
}

/// JSON 바디 테스트
#[test]
fn test_http_client_json_body() {
    let json = r#"{"user":"john","age":30}"#;
    let client = HttpClient::new()
        .header("Content-Type", "application/json")
        .body(json);
    assert_eq!(client.request().body, json);
}

/// 빈 바디 테스트
#[test]
fn test_http_client_empty_body() {
    let client = HttpClient::new().body("");
    assert_eq!(client.request().body, "");
}

/// 긴 바디 테스트
#[test]
fn test_http_client_long_body() {
    let long_body = "x".repeat(10000);
    let client = HttpClient::new().body(long_body.clone());
    assert_eq!(client.request().body.len(), 10000);
}

/// 폼 데이터 바디 테스트
#[test]
fn test_http_client_form_body() {
    let form_data = "username=john&password=secret";
    let client = HttpClient::new()
        .header("Content-Type", "application/x-www-form-urlencoded")
        .body(form_data);
    assert_eq!(client.request().body, form_data);
}

// =============================================================================
// Request State Tests - 요청 상태 테스트
// =============================================================================

/// 초기 상태 테스트
#[test]
fn test_request_state_initial() {
    let client = HttpClient::new();
    assert_eq!(client.state(), RequestState::Idle);
}

/// send() 호출 후 상태 테스트
#[test]
fn test_request_state_after_send() {
    let mut client = http_get("https://httpbin.org/get");
    client.send();
    assert_eq!(client.state(), RequestState::Success);
}

/// 에러 상태 설정 테스트
#[test]
fn test_request_state_error() {
    let mut client = HttpClient::new();
    client.set_error("Connection failed");
    assert_eq!(client.state(), RequestState::Error);
    assert_eq!(client.error(), Some("Connection failed"));
}

/// clear() 후 상태 테스트
#[test]
fn test_request_state_after_clear() {
    let mut client = http_get("https://example.com");
    client.send();
    client.clear();
    assert_eq!(client.state(), RequestState::Idle);
    assert!(client.response().is_none());
}

/// 성공 응답 설정 테스트
#[test]
fn test_set_response_success() {
    let mut client = HttpClient::new();
    let response = HttpResponse {
        status: 200,
        status_text: "OK".to_string(),
        headers: HashMap::new(),
        body: "success".to_string(),
        time: Duration::from_millis(100),
        size: 7,
    };
    client.set_response(response);
    assert_eq!(client.state(), RequestState::Success);
}

/// 실패 응답 설정 테스트
#[test]
fn test_set_response_failure() {
    let mut client = HttpClient::new();
    let response = HttpResponse {
        status: 404,
        status_text: "Not Found".to_string(),
        headers: HashMap::new(),
        body: "error".to_string(),
        time: Duration::from_millis(50),
        size: 5,
    };
    client.set_response(response);
    assert_eq!(client.state(), RequestState::Error);
}

// =============================================================================
// Response Tests - 응답 테스트
// =============================================================================

/// 응답 조회 테스트
#[test]
fn test_response_after_send() {
    let mut client = http_get("https://example.com");
    client.send();
    assert!(client.response().is_some());
    let response = client.response().unwrap();
    assert_eq!(response.status, 200);
}

/// 응답 상태 코드 테스트
#[test]
fn test_response_status_code() {
    let mut client = HttpClient::new();
    client.set_response(HttpResponse {
        status: 201,
        status_text: "Created".to_string(),
        headers: HashMap::new(),
        body: "".to_string(),
        time: Duration::from_millis(10),
        size: 0,
    });
    assert_eq!(client.response().unwrap().status, 201);
}

/// 응답 바디 테스트
#[test]
fn test_response_body() {
    let mut client = HttpClient::new();
    client.set_response(HttpResponse {
        status: 200,
        status_text: "OK".to_string(),
        headers: HashMap::new(),
        body: "Hello, World!".to_string(),
        time: Duration::from_millis(10),
        size: 13,
    });
    assert_eq!(client.response().unwrap().body, "Hello, World!");
}

/// JSON 응답 처리 테스트
#[test]
fn test_json_response() {
    let mut client = http_post("https://api.example.com/data")
        .header("Content-Type", "application/json")
        .body(r#"{"key":"value"}"#);
    client.send();
    assert!(client.response().is_some());
}

// =============================================================================
// HttpRequest Tests - 요청 객체 테스트
// =============================================================================

/// HttpRequest::new() 테스트
#[test]
fn test_http_request_new() {
    let req = HttpRequest::new("https://api.example.com");
    assert_eq!(req.url, "https://api.example.com");
    assert_eq!(req.method, HttpMethod::default());
}

/// HttpRequest 빌더 체이닝 테스트
#[test]
fn test_http_request_builder_chain() {
    let req = HttpRequest::new("https://api.example.com")
        .method(HttpMethod::POST)
        .header("Authorization", "Bearer token")
        .body(r#"{"test":true}"#)
        .param("page", "1")
        .param("limit", "10");

    assert_eq!(req.method, HttpMethod::POST);
    assert_eq!(req.headers.len(), 1);
    assert_eq!(req.params.len(), 2);
    assert!(req.body.contains("test"));
}

/// 쿼리 파라미터 테스트
#[test]
fn test_http_request_params() {
    let req = HttpRequest::new("https://api.example.com")
        .param("search", "rust")
        .param("sort", "desc");

    assert_eq!(req.params.get("search"), Some(&"rust".to_string()));
    assert_eq!(req.params.get("sort"), Some(&"desc".to_string()));
}

/// full_url() 테스트 (파라미터 없음)
#[test]
fn test_full_url_without_params() {
    let req = HttpRequest::new("https://api.example.com/users");
    assert_eq!(req.full_url(), "https://api.example.com/users");
}

/// full_url() 테스트 (파라미터 있음)
#[test]
fn test_full_url_with_params() {
    let req = HttpRequest::new("https://api.example.com/users")
        .param("page", "1")
        .param("limit", "10");

    let url = req.full_url();
    assert!(url.contains("page=1"));
    assert!(url.contains("limit=10"));
    assert!(url.contains("?"));
}

/// full_url() 단일 파라미터 테스트
#[test]
fn test_full_url_single_param() {
    let req = HttpRequest::new("https://api.example.com").param("key", "value");
    assert_eq!(req.full_url(), "https://api.example.com?key=value");
}

/// full_url() 다중 파라미터 순서 테스트
#[test]
fn test_full_url_multiple_params() {
    let req = HttpRequest::new("https://api.example.com")
        .param("a", "1")
        .param("b", "2")
        .param("c", "3");

    let url = req.full_url();
    // 파라미터 순서는 HashMap 순서에 따라 다를 수 있음
    assert!(url.contains("a=1"));
    assert!(url.contains("b=2"));
    assert!(url.contains("c=3"));
}

// =============================================================================
// HttpMethod Tests - 메서드 열거형 테스트
// =============================================================================

/// HttpMethod::name() 테스트
#[test]
fn test_http_method_name() {
    assert_eq!(HttpMethod::GET.name(), "GET");
    assert_eq!(HttpMethod::POST.name(), "POST");
    assert_eq!(HttpMethod::PUT.name(), "PUT");
    assert_eq!(HttpMethod::DELETE.name(), "DELETE");
    assert_eq!(HttpMethod::PATCH.name(), "PATCH");
    assert_eq!(HttpMethod::HEAD.name(), "HEAD");
    assert_eq!(HttpMethod::OPTIONS.name(), "OPTIONS");
}

/// HttpMethod::color() 테스트
#[test]
fn test_http_method_colors() {
    let get_color = HttpMethod::GET.color();
    let post_color = HttpMethod::POST.color();
    let delete_color = HttpMethod::DELETE.color();

    assert_ne!(get_color, post_color);
    assert_ne!(post_color, delete_color);
}

/// HttpMethod 기본값 테스트
#[test]
fn test_http_method_default() {
    let default = HttpMethod::default();
    assert_eq!(default, HttpMethod::GET);
}

/// HttpMethod 색상 값 테스트
#[test]
fn test_http_method_specific_colors() {
    // GET은 파랑, POST는 초록, DELETE는 빨강
    let get_color = HttpMethod::GET.color();
    let post_color = HttpMethod::POST.color();
    let delete_color = HttpMethod::DELETE.color();

    // 색상이 서로 다른지 확인
    assert_ne!(get_color, post_color);
    assert_ne!(post_color, delete_color);
    assert_ne!(get_color, delete_color);
}

// =============================================================================
// ContentType Tests - 콘텐츠 타입 테스트
// =============================================================================

/// JSON 콘텐츠 타입 감지 테스트
#[test]
fn test_content_type_json() {
    assert_eq!(
        ContentType::from_header(Some("application/json")),
        ContentType::Json
    );
    assert_eq!(
        ContentType::from_header(Some("application/json; charset=utf-8")),
        ContentType::Json
    );
    assert_eq!(
        ContentType::from_header(Some("text/json")),
        ContentType::Json
    );
}

/// XML 콘텐츠 타입 감지 테스트
#[test]
fn test_content_type_xml() {
    assert_eq!(
        ContentType::from_header(Some("application/xml")),
        ContentType::Xml
    );
    assert_eq!(
        ContentType::from_header(Some("text/xml")),
        ContentType::Xml
    );
}

/// HTML 콘텐츠 타입 감지 테스트
#[test]
fn test_content_type_html() {
    assert_eq!(
        ContentType::from_header(Some("text/html")),
        ContentType::Html
    );
}

/// 텍스트 콘텐츠 타입 감지 테스트
#[test]
fn test_content_type_text() {
    assert_eq!(
        ContentType::from_header(Some("text/plain")),
        ContentType::Text
    );
}

/// 바이너리 콘텐츠 타입 감지 테스트
#[test]
fn test_content_type_binary() {
    assert_eq!(
        ContentType::from_header(Some("application/octet-stream")),
        ContentType::Binary
    );
}

/// None 헤더 기본값 테스트
#[test]
fn test_content_type_none() {
    assert_eq!(ContentType::from_header(None), ContentType::Text);
}

/// 알 수 없는 콘텐츠 타입 테스트
#[test]
fn test_content_type_unknown() {
    assert_eq!(
        ContentType::from_header(Some("application/unknown")),
        ContentType::Text
    );
}

// =============================================================================
// HttpResponse Tests - 응답 객체 테스트
// =============================================================================

/// HttpResponse 성공 상태 코드 테스트 (2xx)
#[test]
fn test_response_is_success_2xx() {
    let mut response = HttpResponse::default();

    for status in 200..=299 {
        response.status = status;
        assert!(response.is_success(), "Status {} should be success", status);
    }
}

/// HttpResponse 실패 상태 코드 테스트
#[test]
fn test_response_is_success_not_2xx() {
    let mut response = HttpResponse::default();

    response.status = 404;
    assert!(!response.is_success());

    response.status = 500;
    assert!(!response.is_success());

    response.status = 301;
    assert!(!response.is_success());
}

/// HttpResponse 상태 색상 테스트
#[test]
fn test_response_status_colors() {
    let mut response = HttpResponse::default();

    // 2xx - 초록
    response.status = 200;
    let color_2xx = response.status_color();

    // 3xx - 노랑
    response.status = 301;
    let color_3xx = response.status_color();

    // 4xx - 빨강
    response.status = 404;
    let color_4xx = response.status_color();

    // 5xx - 보라
    response.status = 500;
    let color_5xx = response.status_color();

    assert_ne!(color_2xx, color_3xx);
    assert_ne!(color_3xx, color_4xx);
    assert_ne!(color_4xx, color_5xx);
}

/// HttpResponse 콘텐츠 타입 감지 테스트
#[test]
fn test_response_content_type() {
    let mut response = HttpResponse::default();
    response
        .headers
        .insert("Content-Type".to_string(), "application/json".to_string());

    assert_eq!(response.content_type(), ContentType::Json);
}

/// HttpResponse pretty_json() 테스트
#[test]
fn test_response_pretty_json() {
    let mut response = HttpResponse::default();
    response.body = r#"{"name":"test","value":123}"#.to_string();

    let pretty = response.pretty_json().unwrap();
    assert!(pretty.contains('\n'));
    assert!(pretty.contains("name"));
    assert!(pretty.contains("test"));
}

/// HttpResponse formatted_body() JSON 테스트
#[test]
fn test_response_formatted_body_json() {
    let mut response = HttpResponse::default();
    response
        .headers
        .insert("Content-Type".to_string(), "application/json".to_string());
    response.body = r#"{"key":"value"}"#.to_string();

    let formatted = response.formatted_body();
    // JSON은 포맷팅됨
    assert!(formatted.contains('\n') || formatted.contains("key"));
}

/// HttpResponse formatted_body() 텍스트 테스트
#[test]
fn test_response_formatted_body_text() {
    let mut response = HttpResponse::default();
    response
        .headers
        .insert("Content-Type".to_string(), "text/plain".to_string());
    response.body = "Plain text".to_string();

    let formatted = response.formatted_body();
    assert_eq!(formatted, "Plain text");
}

/// 빈 JSON 포맷 테스트
#[test]
fn test_response_pretty_json_empty() {
    let response = HttpResponse::default();
    let result = response.pretty_json();
    // 빈 문자열에 대한 처리
    assert!(result.is_none() || result.unwrap().is_empty());
}

// =============================================================================
// ResponseView Tests - 응답 뷰 테스트
// =============================================================================

/// ResponseView 기본값 테스트
#[test]
fn test_response_view_default() {
    let view = ResponseView::default();
    assert_eq!(view, ResponseView::Body);
}

/// set_view() 메서드 테스트
#[test]
fn test_set_view() {
    let mut client = HttpClient::new();
    client.set_view(ResponseView::Headers);
    // View 설정은 내부 상태로 저장됨
}

/// set_view() 모든 변형 테스트
#[test]
fn test_set_view_all_variants() {
    let mut client = HttpClient::new();

    client.set_view(ResponseView::Body);
    client.set_view(ResponseView::Headers);
    client.set_view(ResponseView::Raw);
}

// =============================================================================
// Scroll Tests - 스크롤 기능 테스트
// =============================================================================

/// 스크롤 다운 테스트
#[test]
fn test_scroll_down() {
    let mut client = HttpClient::new();
    client.scroll_down(10);
    client.scroll_down(5);
    // 내부 상태 검증 - 스크롤 값이 누적됨
}

/// 스크롤 업 테스트
#[test]
fn test_scroll_up() {
    let mut client = HttpClient::new();
    client.scroll_down(20);
    client.scroll_up(5);
    client.scroll_up(10);
    // 내부 상태 검증 - 스크롤이 0 아래로 내려가지 않음
}

/// 스크롤 경계 테스트 (0 미만)
#[test]
fn test_scroll_up_below_zero() {
    let mut client = HttpClient::new();
    client.scroll_up(100);
    // saturating_sub로 인해 0 이하로 내려가지 않음
}

/// clear() 후 스크롤 초기화 테스트
#[test]
fn test_scroll_reset_after_clear() {
    let mut client = HttpClient::new();
    client.scroll_down(50);
    client.clear();
    // 스크롤이 0으로 재설정됨
}

// =============================================================================
// History Tests - 요청 이력 테스트
// =============================================================================

/// 이력 저장 테스트
#[test]
fn test_history_saved_on_send() {
    let mut client = http_get("https://api.example.com/1");
    client.send();

    let mut client2 = http_post("https://api.example.com/2");
    client2.send();

    // 각 클라이언트는 자신만의 이력을 가짐
}

/// history_back() 테스트
#[test]
fn test_history_back() {
    let mut client = HttpClient::new();
    client.set_url("https://api.example.com/1");
    client.send();

    client.set_url("https://api.example.com/2");
    client.send();

    client.history_back();
    // 이전 URL로 복귀
}

/// history_forward() 테스트
#[test]
fn test_history_forward() {
    let mut client = HttpClient::new();
    client.set_url("https://api.example.com/1");
    client.send();

    client.set_url("https://api.example.com/2");
    client.send();

    client.history_back();
    client.history_forward();
    // 다음 URL로 이동
}

/// 빈 이력에서 탐색 테스트
#[test]
fn test_history_navigation_empty() {
    let mut client = HttpClient::new();
    client.history_back();
    client.history_forward();
    // 이력이 없으므로 아무 일도 일어나지 않음
}

/// 단일 항목 이력 탐색 테스트
#[test]
fn test_history_navigation_single_item() {
    let mut client = http_get("https://api.example.com");
    client.send();
    client.history_back();
    // 단일 항목에서 뒤로가기는 안전하게 처리됨
}

// =============================================================================
// HttpColors Tests - 색상 설정 테스트
// =============================================================================

/// HttpColors 기본값 테스트
#[test]
fn test_http_colors_default() {
    // HttpColors는 내부 구조체이므로 테스트에서 직접 접근할 수 없음
    // 색상 관련 기능은 렌더링 테스트에서 검증됨
}

/// HttpClient 색상 설정 테스트
#[test]
fn test_http_client_colors() {
    // 색상 설정은 httpclient 모듈 내부의 HttpColors 구조체를 사용하지만
    // 테스트에서는 colors() 메서드가 호출 가능한지만 확인
    // 실제 색상 테스트는 렌더링 테스트에서 검증됨
}

// =============================================================================
// toggle_headers Tests - 헤더 패널 토글 테스트
// =============================================================================

/// toggle_headers() 테스트
#[test]
fn test_toggle_headers() {
    let mut client = HttpClient::new();
    client.toggle_headers();
    client.toggle_headers();
    // 헤더 패널 토글 상태 변경
}

// =============================================================================
// RequestBuilder Tests - 플루언트 API 테스트
// =============================================================================

/// RequestBuilder::get() 테스트
#[test]
fn test_request_builder_get() {
    let request = RequestBuilder::get("https://api.example.com").build();
    assert_eq!(request.method, HttpMethod::GET);
    assert_eq!(request.url, "https://api.example.com");
}

/// RequestBuilder::post() 테스트
#[test]
fn test_request_builder_post() {
    let request = RequestBuilder::post("https://api.example.com").build();
    assert_eq!(request.method, HttpMethod::POST);
}

/// RequestBuilder::put() 테스트
#[test]
fn test_request_builder_put() {
    let request = RequestBuilder::put("https://api.example.com").build();
    assert_eq!(request.method, HttpMethod::PUT);
}

/// RequestBuilder::delete() 테스트
#[test]
fn test_request_builder_delete() {
    let request = RequestBuilder::delete("https://api.example.com").build();
    assert_eq!(request.method, HttpMethod::DELETE);
}

/// RequestBuilder::patch() 테스트
#[test]
fn test_request_builder_patch() {
    let request = RequestBuilder::patch("https://api.example.com").build();
    assert_eq!(request.method, HttpMethod::PATCH);
}

/// RequestBuilder 체이닝 테스트
#[test]
fn test_request_builder_chain() {
    let request = RequestBuilder::get("https://api.example.com")
        .header("Authorization", "Bearer token")
        .param("page", "1")
        .build();

    assert!(request.headers.contains_key("Authorization"));
    assert!(request.params.contains_key("page"));
}

/// RequestBuilder::json() 테스트
#[test]
fn test_request_builder_json() {
    let request = RequestBuilder::post("https://api.example.com")
        .json(r#"{"key":"value"}"#)
        .build();

    assert_eq!(
        request.headers.get("Content-Type"),
        Some(&"application/json".to_string())
    );
    assert_eq!(request.body, r#"{"key":"value"}"#);
}

/// RequestBuilder::form() 테스트
#[test]
fn test_request_builder_form() {
    let request = RequestBuilder::post("https://api.example.com")
        .form("key=value&foo=bar")
        .build();

    assert_eq!(
        request.headers.get("Content-Type"),
        Some(&"application/x-www-form-urlencoded".to_string())
    );
    assert_eq!(request.body, "key=value&foo=bar");
}

/// RequestBuilder::bearer_auth() 테스트
#[test]
fn test_request_builder_bearer_auth() {
    let request = RequestBuilder::get("https://api.example.com")
        .bearer_auth("my_secret_token")
        .build();

    assert_eq!(
        request.headers.get("Authorization"),
        Some(&"Bearer my_secret_token".to_string())
    );
}

/// RequestBuilder::basic_auth() 테스트
#[test]
fn test_request_builder_basic_auth() {
    let request = RequestBuilder::get("https://api.example.com")
        .basic_auth("username", "password")
        .build();

    let auth = request.headers.get("Authorization").unwrap();
    assert!(auth.starts_with("Basic "));
}

/// RequestBuilder 복합 체이닝 테스트
#[test]
fn test_request_builder_complex_chain() {
    let request = RequestBuilder::post("https://api.example.com/users")
        .header("X-API-Key", "secret")
        .bearer_auth("token")
        .json(r#"{"name":"John","age":30}"#)
        .build();

    assert_eq!(request.method, HttpMethod::POST);
    assert!(request.headers.contains_key("X-API-Key"));
    assert!(request.headers.contains_key("Authorization"));
    assert!(request.headers.contains_key("Content-Type"));
}

// =============================================================================
// MockHttpBackend Tests - 모 백엔드 테스트
// =============================================================================

/// MockHttpBackend 기본 응답 테스트
#[test]
fn test_mock_backend_default() {
    let backend = MockHttpBackend::new();
    let request = HttpRequest::new("https://any.url.com");

    let response = backend.send(&request).unwrap();
    assert_eq!(response.status, 200);
}

/// MockHttpBackend 사용자 정의 응답 테스트
#[test]
fn test_mock_backend_custom_response() {
    let backend = MockHttpBackend::new();

    let custom_response = HttpResponse {
        status: 201,
        status_text: "Created".to_string(),
        headers: HashMap::new(),
        body: "custom body".to_string(),
        time: Duration::from_millis(10),
        size: 11,
    };

    backend.mock_response("example.com", custom_response);

    let request = HttpRequest::new("https://example.com/test");
    let response = backend.send(&request).unwrap();

    assert_eq!(response.status, 201);
    assert_eq!(response.body, "custom body");
}

/// MockHttpBackend JSON 응답 테스트
#[test]
fn test_mock_backend_json() {
    let backend = MockHttpBackend::new();
    backend.mock_json("api", 200, r#"{"result":"success"}"#);

    let request = HttpRequest::new("https://api.example.com/data");
    let response = backend.send(&request).unwrap();

    assert_eq!(response.status, 200);
    assert_eq!(response.content_type(), ContentType::Json);
    assert!(response.body.contains("success"));
}

/// MockHttpBackend 에러 응답 테스트
#[test]
fn test_mock_backend_error() {
    let backend = MockHttpBackend::new();
    backend.mock_error("api", 404, "Not Found");

    let request = HttpRequest::new("https://api.example.com/missing");
    let response = backend.send(&request).unwrap();

    assert_eq!(response.status, 404);
    assert!(response.body.contains("Not Found"));
}

/// MockHttpBackend 와일드카드 패턴 테스트
#[test]
fn test_mock_backend_wildcard() {
    let backend = MockHttpBackend::new();

    backend.mock_json("*", 500, r#"{"error":"server error"}"#);

    let request = HttpRequest::new("https://any.url.com/anything");
    let response = backend.send(&request).unwrap();

    assert_eq!(response.status, 500);
}

/// MockHttpBackend 최신 매칭 우선 테스트
#[test]
fn test_mock_backend_most_recent_wins() {
    let backend = MockHttpBackend::new();

    backend.mock_json("test", 200, r#"{"first":true}"#);
    backend.mock_json("test", 201, r#"{"second":true}"#);

    let request = HttpRequest::new("https://test.com");
    let response = backend.send(&request).unwrap();

    // 가장 최근에 설정된 응답이 사용됨
    assert_eq!(response.status, 201);
    assert!(response.body.contains("second"));
}

// =============================================================================
// Base64 Encoding Tests - Base64 인코딩 테스트
// =============================================================================

/// 간단한 base64 인코딩 테스트
#[test]
fn test_base64_encode_simple() {
    assert_eq!(test_base64_encode(b"Hello"), "SGVsbG8=");
    assert_eq!(test_base64_encode(b"Hi"), "SGk=");
    assert_eq!(test_base64_encode(b"A"), "QQ==");
}

/// 빈 문자열 base64 인코딩 테스트
#[test]
fn test_base64_encode_empty() {
    assert_eq!(test_base64_encode(b""), "");
}

/// 짝수 길이 문자열 base64 인코딩 테스트
#[test]
fn test_base64_encode_even_length() {
    assert_eq!(test_base64_encode(b"HelloWorld"), "SGVsbG9Xb3JsZA==");
}

/// 홀수 길이 문자열 base64 인코딩 테스트
#[test]
fn test_base64_encode_odd_length() {
    assert_eq!(test_base64_encode(b"Hello!"), "SGVsbG8h");
}

/// 자격 증명 base64 인코딩 테스트
#[test]
fn test_base64_encode_credentials() {
    let encoded = test_base64_encode(b"user:pass");
    assert_eq!(encoded, "dXNlcjpwYXNz");
}

/// 특수 문자 base64 인코딩 테스트
#[test]
fn test_base64_encode_special_chars() {
    let encoded = test_base64_encode(b"test@email.com");
    assert_eq!(encoded, "dGVzdEBlbWFpbC5jb20=");
}

/// 긴 문자열 base64 인코딩 테스트
#[test]
fn test_base64_encode_long_string() {
    let input = "a".repeat(100);
    let encoded = test_base64_encode(input.as_bytes());
    // base64로 인코딩되면 길이가 증가하고 '=' 패딩이 포함됨
    assert!(encoded.len() > input.len());
}

// =============================================================================
// Rendering Tests - 렌더링 테스트
// =============================================================================

/// 기본 렌더링 테스트
#[test]
fn test_render_basic() {
    let client = http_get("https://example.com");

    let mut buffer = Buffer::new(80, 20);
    let area = Rect::new(0, 0, 80, 20);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
}

/// 작은 영역 렌더링 테스트
#[test]
fn test_render_small_area() {
    let client = http_get("https://example.com");

    let mut buffer = Buffer::new(40, 10);
    let area = Rect::new(0, 0, 40, 10);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
}

/// 너무 작은 영역 렌더링 테스트 (렌더링 건너뜀)
#[test]
fn test_render_too_small() {
    let client = http_get("https://example.com");

    let mut buffer = Buffer::new(30, 5);
    let area = Rect::new(0, 0, 30, 5);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
    // 너무 작은 영역에서는 렌더링이 건너뛰어짐
}

/// 응답 상태 렌더링 테스트
#[test]
fn test_render_with_response() {
    let mut client = http_get("https://example.com");
    client.send();

    let mut buffer = Buffer::new(80, 20);
    let area = Rect::new(0, 0, 80, 20);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
}

/// 에러 상태 렌더링 테스트
#[test]
fn test_render_with_error() {
    let mut client = HttpClient::new();
    client.set_error("Connection timeout");

    let mut buffer = Buffer::new(80, 20);
    let area = Rect::new(0, 0, 80, 20);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
}

/// POST 메서드 렌더링 테스트
#[test]
fn test_render_post_method() {
    let client = http_post("https://api.example.com/data");

    let mut buffer = Buffer::new(80, 20);
    let area = Rect::new(0, 0, 80, 20);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
}

/// DELETE 메서드 렌더링 테스트
#[test]
fn test_render_delete_method() {
    let client = http_delete("https://api.example.com/users/1");

    let mut buffer = Buffer::new(80, 20);
    let area = Rect::new(0, 0, 80, 20);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
}

/// 긴 URL 렌더링 테스트
#[test]
fn test_render_long_url() {
    let long_url = "https://api.example.com/v1/users/123/posts/456/comments/789?include=author,replies&sort=desc";
    let client = HttpClient::new().url(long_url);

    let mut buffer = Buffer::new(80, 20);
    let area = Rect::new(0, 0, 80, 20);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
}

/// 다양한 응답 뷰 렌더링 테스트
#[test]
fn test_render_all_response_views() {
    let mut client = http_get("https://example.com");
    client.send();

    let mut buffer = Buffer::new(80, 20);
    let area = Rect::new(0, 0, 80, 20);

    // Body view
    client.set_view(ResponseView::Body);
    let mut ctx = RenderContext::new(&mut buffer, area);
    client.render(&mut ctx);

    // Headers view
    client.set_view(ResponseView::Headers);
    let mut ctx = RenderContext::new(&mut buffer, area);
    client.render(&mut ctx);

    // Raw view
    client.set_view(ResponseView::Raw);
    let mut ctx = RenderContext::new(&mut buffer, area);
    client.render(&mut ctx);
}

/// 사용자 정의 색상으로 렌더링 테스트
#[test]
fn test_render_with_custom_colors() {
    // 색상 설정은 내부 구조체이므로 기본 렌더링만 테스트
    let client = HttpClient::new().url("https://example.com");

    let mut buffer = Buffer::new(80, 20);
    let area = Rect::new(0, 0, 80, 20);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
}

/// 대형 버퍼 렌더링 테스트
#[test]
fn test_render_large_buffer() {
    let client = http_get("https://example.com");

    let mut buffer = Buffer::new(120, 40);
    let area = Rect::new(0, 0, 120, 40);
    let mut ctx = RenderContext::new(&mut buffer, area);

    client.render(&mut ctx);
}

// =============================================================================
// Edge Cases - 엣지 케이스 테스트
// =============================================================================

/// 빈 URL과 빈 바디로 전송 테스트
#[test]
fn test_send_with_empty_url_and_body() {
    let mut client = HttpClient::new();
    client.send();
    // 빈 URL로도 전송은 성공 (mock response 반환)
}

/// 특수 문자가 포함된 URL 테스트
#[test]
fn test_url_with_special_chars() {
    let special_url = "https://example.com/path?query=test%20space&other=hello%2Bworld";
    let client = HttpClient::new().url(special_url);
    assert_eq!(client.request().url, special_url);
}

/// Unicode 문자가 포함된 바디 테스트
#[test]
fn test_body_with_unicode() {
    let unicode_body = r#"{"message":"안녕하세요","emoji":"😀"}"#;
    let client = HttpClient::new().body(unicode_body);
    assert_eq!(client.request().body, unicode_body);
}

/// 매우 긴 URL 테스트
#[test]
fn test_very_long_url() {
    let long_url = "https://example.com/".repeat(100);
    let client = HttpClient::new().url(long_url.clone());
    assert_eq!(client.request().url.len(), long_url.len());
}

/// 여러 헤더의 같은 키 테스트 (덮어쓰기)
#[test]
fn test_multiple_headers_same_key() {
    let client = HttpClient::new()
        .header("X-Custom", "first")
        .header("X-Custom", "second")
        .header("X-Custom", "third");

    assert_eq!(
        client.request().headers.get("X-Custom"),
        Some(&"third".to_string())
    );
}

/// 많은 쿼리 파라미터 테스트
#[test]
fn test_many_query_params() {
    let mut client = HttpClient::new();

    for i in 0..20 {
        client = client.header(&format!("X-Header-{}", i), &format!("value-{}", i));
    }

    assert_eq!(client.request().headers.len(), 20);
}

/// 많은 헤더 테스트
#[test]
fn test_many_headers() {
    let client = HttpClient::new()
        .header("H1", "v1")
        .header("H2", "v2")
        .header("H3", "v3")
        .header("H4", "v4")
        .header("H5", "v5");

    assert_eq!(client.request().headers.len(), 5);
}

/// 응답 없이 상태 조회 테스트
#[test]
fn test_state_without_response() {
    let client = HttpClient::new();
    assert_eq!(client.state(), RequestState::Idle);
    assert!(client.response().is_none());
}

/// clear() 후 재전송 테스트
#[test]
fn test_clear_and_resend() {
    let mut client = http_get("https://example.com");
    client.send();
    assert!(client.response().is_some());

    client.clear();
    assert!(client.response().is_none());
    assert_eq!(client.state(), RequestState::Idle);

    client.send();
    assert!(client.response().is_some());
}

/// 연속적인 전송 테스트
#[test]
fn test_multiple_sends() {
    let mut client = http_get("https://example.com");

    for _ in 0..5 {
        client.send();
        assert_eq!(client.state(), RequestState::Success);
        assert!(client.response().is_some());
    }
}

/// 다양한 상태 코드 응답 테스트
#[test]
fn test_various_status_codes() {
    let status_codes = [200, 201, 204, 301, 302, 400, 401, 403, 404, 500, 502, 503];

    for &status in &status_codes {
        let mut client = HttpClient::new();
        client.set_response(HttpResponse {
            status,
            status_text: "Test".to_string(),
            headers: HashMap::new(),
            body: "".to_string(),
            time: Duration::from_millis(10),
            size: 0,
        });

        assert_eq!(client.response().unwrap().status, status);
    }
}

/// 빈 헤더 맵 테스트
#[test]
fn test_empty_headers_map() {
    let client = HttpClient::new();
    assert!(client.request().headers.is_empty());
}

/// 빈 파라미터 맵 테스트
#[test]
fn test_empty_params_map() {
    let client = HttpClient::new();
    assert!(client.request().params.is_empty());
}

/// RequestState 디버그 표현 테스트
#[test]
fn test_request_state_debug() {
    let state = RequestState::Idle;
    let debug_str = format!("{:?}", state);
    assert!(debug_str.contains("Idle"));
}

/// HttpMethod 디버그 표현 테스트
#[test]
fn test_http_method_debug() {
    let method = HttpMethod::GET;
    let debug_str = format!("{:?}", method);
    assert!(debug_str.contains("GET"));
}

/// ContentType 디버그 표현 테스트
#[test]
fn test_content_type_debug() {
    let ct = ContentType::Json;
    let debug_str = format!("{:?}", ct);
    assert!(debug_str.contains("Json"));
}

/// ResponseView 디버그 표현 테스트
#[test]
fn test_response_view_debug() {
    let view = ResponseView::Body;
    let debug_str = format!("{:?}", view);
    assert!(debug_str.contains("Body"));
}

/// HttpRequest Clone 테스트
#[test]
fn test_http_request_clone() {
    let req1 = HttpRequest::new("https://example.com")
        .method(HttpMethod::POST)
        .header("X-Test", "value");

    let req2 = req1.clone();
    assert_eq!(req1.url, req2.url);
    assert_eq!(req1.method, req2.method);
    assert_eq!(req1.headers.len(), req2.headers.len());
}

/// HttpClient Clone 테스트
#[test]
fn test_http_client_clone() {
    let _client = HttpClient::new()
        .url("https://example.com")
        .method(HttpMethod::POST);

    // HttpClient는 Clone을 구현하지 않음
    // 이 테스트는 기본 생성 동작 확인을 위한 것
}

/// 응답 시간 포맷팅 테스트 (내부 함수)
#[test]
fn test_response_time_formatting() {
    let response = HttpResponse {
        status: 200,
        status_text: "OK".to_string(),
        headers: HashMap::new(),
        body: "test".to_string(),
        time: Duration::from_millis(1234),
        size: 4,
    };

    // 내부 포맷팅 함수는 private이지만 렌더링을 통해 검증 가능
    let mut client = HttpClient::new();
    client.set_response(response);

    let mut buffer = Buffer::new(80, 20);
    let area = Rect::new(0, 0, 80, 20);
    let mut ctx = RenderContext::new(&mut buffer, area);
    client.render(&mut ctx);
}

/// 응답 크기 포맷팅 테스트
#[test]
fn test_response_size_formatting() {
    let sizes = [0, 100, 1024, 1024 * 1024];

    for &size in &sizes {
        let response = HttpResponse {
            status: 200,
            status_text: "OK".to_string(),
            headers: HashMap::new(),
            body: "x".repeat(size),
            time: Duration::from_millis(10),
            size,
        };

        let mut client = HttpClient::new();
        client.set_response(response);

        let mut buffer = Buffer::new(80, 20);
        let area = Rect::new(0, 0, 80, 20);
        let mut ctx = RenderContext::new(&mut buffer, area);
        client.render(&mut ctx);
    }
}

/// request_mut() 메서드 테스트
#[test]
fn test_request_mut() {
    let mut client = HttpClient::new();
    client.request_mut().url = "https://example.com".to_string();
    client.request_mut().method = HttpMethod::POST;

    assert_eq!(client.request().url, "https://example.com");
    assert_eq!(client.request().method, HttpMethod::POST);
}

/// request() 불변 참조 테스트
#[test]
fn test_request_immutable_ref() {
    let client = HttpClient::new().url("https://example.com");
    let req_ref = client.request();

    assert_eq!(req_ref.url, "https://example.com");
    // 불변 참조이므로 수정 불가
}

/// 기본 User-Agent 헤더 없음 테스트
#[test]
fn test_no_default_user_agent() {
    let client = HttpClient::new();
    assert_eq!(client.request().headers.get("User-Agent"), None);
}

/// 호스트 이름만 있는 URL 테스트
#[test]
fn test_url_hostname_only() {
    let client = HttpClient::new().url("example.com");
    assert_eq!(client.request().url, "example.com");
}

/// 포트가 포함된 URL 테스트
#[test]
fn test_url_with_port() {
    let url = "https://localhost:8080/api";
    let client = HttpClient::new().url(url);
    assert_eq!(client.request().url, url);
}

/// HTTPS URL 테스트
#[test]
fn test_https_url() {
    let url = "https://secure.example.com";
    let client = HttpClient::new().url(url);
    assert!(client.request().url.starts_with("https://"));
}

/// HTTP URL 테스트
#[test]
fn test_http_url() {
    let url = "http://insecure.example.com";
    let client = HttpClient::new().url(url);
    assert!(client.request().url.starts_with("http://"));
}

/// URL 경계 테스트 - 빈 경로
#[test]
fn test_url_empty_path() {
    let url = "https://example.com";
    let client = HttpClient::new().url(url);
    // URL에 "/"가 없는지 확인 - "https://"에는 "/"가 있으므로
    // 경로 부분에 "/"가 없는지 확인해야 함
    assert!(!client.request().url.ends_with('/'));
    assert_eq!(client.request().url, "https://example.com");
}

/// URL 경계 테스트 - 루트 경로
#[test]
fn test_url_root_path() {
    let url = "https://example.com/";
    let client = HttpClient::new().url(url);
    assert!(client.request().url.ends_with('/'));
}

/// 여러 send() 호출 후 이력 길이 테스트
#[test]
fn test_history_length_after_multiple_sends() {
    let mut client = HttpClient::new();

    for i in 0..5 {
        client.set_url(&format!("https://example.com/{}", i));
        client.send();
    }

    // 이력이 누적됨
}

/// URL 변경 후 전송 테스트
#[test]
fn test_change_url_and_send() {
    let mut client = http_get("https://example.com/first");
    client.send();

    client.set_url("https://example.com/second");
    client.send();

    assert_eq!(client.request().url, "https://example.com/second");
}

/// 메서드 변경 후 전송 테스트
#[test]
fn test_change_method_and_send() {
    let mut client = http_get("https://example.com");
    client.send();

    client = client.method(HttpMethod::POST);
    client.send();

    assert_eq!(client.request().method, HttpMethod::POST);
}

/// 에러 후 성공으로 복구 테스트
#[test]
fn test_recover_from_error_to_success() {
    let mut client = HttpClient::new();
    client.set_error("Network error");
    assert_eq!(client.state(), RequestState::Error);

    client.send();
    assert_eq!(client.state(), RequestState::Success);
    assert!(client.error().is_none());
}

/// 성공 후 에러로 전환 테스트
#[test]
fn test_transition_from_success_to_error() {
    let mut client = http_get("https://example.com");
    client.send();
    assert_eq!(client.state(), RequestState::Success);

    client.set_error("Timeout");
    assert_eq!(client.state(), RequestState::Error);
}
