//! API Tests Part 04
//!
//! GPU inference tests (IMP-116+)

#[allow(unused_imports)]
use axum::{
    body::Body,
    http::{Request, StatusCode},
};
#[allow(unused_imports)]
use tower::util::ServiceExt;

#[allow(unused_imports)]
use crate::api::test_helpers::create_test_app_shared;
use crate::api::*;

#[test]
fn test_chat_delta_debug_clone_cov() {
    let delta = ChatDelta {
        role: Some("assistant".to_string()),
        content: Some("Hello".to_string()),
    };
    let debug = format!("{:?}", delta);
    assert!(debug.contains("ChatDelta"));

    let cloned = delta.clone();
    assert_eq!(cloned.role, delta.role);
}

#[test]
fn test_chat_delta_empty_cov() {
    let delta = ChatDelta {
        role: None,
        content: None,
    };
    let json = serde_json::to_string(&delta).expect("serialize");
    // Empty delta should have null fields
    assert!(json.contains("null") || !json.is_empty());
}

// =========================================================================
// Additional Coverage Tests: StreamTokenEvent
// =========================================================================

#[test]
fn test_stream_token_event_serialize_cov() {
    let event = StreamTokenEvent {
        token_id: 1234,
        text: "hello".to_string(),
    };
    let json = serde_json::to_string(&event).expect("serialize");
    assert!(json.contains("hello"));
    assert!(json.contains("1234"));
}

// =========================================================================
// Additional Coverage Tests: StreamDoneEvent
// =========================================================================

#[test]
fn test_stream_done_event_serialize_cov() {
    let event = StreamDoneEvent { num_generated: 100 };
    let json = serde_json::to_string(&event).expect("serialize");
    assert!(json.contains("100"));
}

// =========================================================================
// Additional Coverage Tests: BatchTokenizeRequest/Response
// =========================================================================

#[test]
fn test_batch_tokenize_request_serialize_cov() {
    let req = BatchTokenizeRequest {
        texts: vec!["hello".to_string(), "world".to_string()],
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("hello"));
    assert!(json.contains("world"));
}

#[test]
fn test_batch_tokenize_response_serialize_cov() {
    let resp = BatchTokenizeResponse {
        results: vec![
            TokenizeResponse {
                token_ids: vec![1, 2, 3],
                num_tokens: 3,
            },
            TokenizeResponse {
                token_ids: vec![4, 5],
                num_tokens: 2,
            },
        ],
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("1") && json.contains("4"));
}

// =========================================================================
// Additional Coverage Tests: OpenAIModel
// =========================================================================

#[test]
fn test_openai_model_debug_clone_cov() {
    let model = OpenAIModel {
        id: "text-davinci-003".to_string(),
        object: "model".to_string(),
        created: 1669599635,
        owned_by: "openai-internal".to_string(),
    };
    let debug = format!("{:?}", model);
    assert!(debug.contains("OpenAIModel"));
    assert!(debug.contains("text-davinci-003"));

    let cloned = model.clone();
    assert_eq!(cloned.id, model.id);
}

// =========================================================================
// Additional Coverage Tests: PredictionWithScore
// =========================================================================

#[test]
fn test_prediction_with_score_debug_clone_cov() {
    let pred = PredictionWithScore {
        label: "positive".to_string(),
        score: 0.95,
    };
    let debug = format!("{:?}", pred);
    assert!(debug.contains("PredictionWithScore"));
    assert!(debug.contains("positive"));

    let cloned = pred.clone();
    assert_eq!(cloned.label, pred.label);
    assert!((cloned.score - pred.score).abs() < 1e-6);
}

// =========================================================================
// Additional Coverage Tests: ChatChunkChoice
// =========================================================================

#[test]
fn test_chat_chunk_choice_debug_clone_cov() {
    let choice = ChatChunkChoice {
        index: 0,
        delta: ChatDelta {
            role: Some("assistant".to_string()),
            content: Some("Test".to_string()),
        },
        finish_reason: Some("stop".to_string()),
    };
    let debug = format!("{:?}", choice);
    assert!(debug.contains("ChatChunkChoice"));

    let cloned = choice.clone();
    assert_eq!(cloned.index, choice.index);
}

// =========================================================================
// Coverage Tests: Default functions
// =========================================================================

#[test]
fn test_default_true_cov() {
    assert!(crate::api::default_true());
}

#[test]
fn test_default_explain_method_cov() {
    let method = crate::api::default_explain_method();
    assert_eq!(method, "shap");
}

#[test]
fn test_default_top_k_features_cov() {
    let k = crate::api::default_top_k_features();
    assert_eq!(k, 5);
}

// =========================================================================
// Coverage Tests: PredictRequest
// =========================================================================

#[test]
fn test_predict_request_full_cov() {
    let req = PredictRequest {
        model: Some("sentiment".to_string()),
        features: vec![1.0, 2.0, 3.0, 4.0],
        feature_names: Some(vec![
            "a".to_string(),
            "b".to_string(),
            "c".to_string(),
            "d".to_string(),
        ]),
        top_k: Some(3),
        include_confidence: true,
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("sentiment"));
    assert!(json.contains("1.0"));
    assert!(json.contains("true"));
}

#[test]
fn test_predict_request_defaults_cov() {
    let json = r#"{"features": [0.5, 1.5]}"#;
    let req: PredictRequest = serde_json::from_str(json).expect("deserialize");
    assert!(req.model.is_none());
    assert!(!req.features.is_empty());
}

// =========================================================================
// Coverage Tests: PredictResponse
// =========================================================================

#[test]
fn test_predict_response_full_cov() {
    let resp = PredictResponse {
        request_id: "req-123".to_string(),
        model: "classifier".to_string(),
        prediction: serde_json::json!("positive"),
        confidence: Some(0.95),
        top_k_predictions: Some(vec![
            PredictionWithScore {
                label: "positive".to_string(),
                score: 0.95,
            },
            PredictionWithScore {
                label: "negative".to_string(),
                score: 0.05,
            },
        ]),
        latency_ms: 12.5,
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("req-123"));
    assert!(json.contains("positive"));
    assert!(json.contains("0.95"));
}

#[test]
fn test_predict_response_minimal_cov() {
    let resp = PredictResponse {
        request_id: "req-minimal".to_string(),
        model: "model".to_string(),
        prediction: serde_json::json!(42),
        confidence: None,
        top_k_predictions: None,
        latency_ms: 5.0,
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("req-minimal"));
    // None fields should be skipped
    assert!(!json.contains("confidence") || json.contains("null"));
}

// =========================================================================
// Coverage Tests: ExplainRequest
// =========================================================================

#[test]
fn test_explain_request_full_cov() {
    let req = ExplainRequest {
        model: Some("explainer".to_string()),
        features: vec![1.0, 2.0, 3.0],
        feature_names: vec!["a".to_string(), "b".to_string(), "c".to_string()],
        top_k_features: 2,
        method: "lime".to_string(),
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("explainer"));
    assert!(json.contains("lime"));
    assert!(json.contains("feature_names"));
}

// =========================================================================
// Coverage Tests: ExplainResponse
// =========================================================================

#[test]
fn test_explain_response_full_cov() {
    let resp = ExplainResponse {
        request_id: "explain-req-1".to_string(),
        model: "model".to_string(),
        prediction: serde_json::json!("class_a"),
        confidence: Some(0.9),
        explanation: ShapExplanation {
            base_value: 0.5,
            shap_values: vec![0.4],
            feature_names: vec!["x".to_string()],
            prediction: 0.9,
        },
        summary: "Feature x was most important".to_string(),
        latency_ms: 15.0,
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("explain-req-1"));
    assert!(json.contains("base_value"));
}

// =========================================================================
// Coverage Tests: AuditResponse
// =========================================================================

#[test]
fn test_audit_response_debug_cov() {
    // AuditResponse wraps AuditRecord - test the response type exists
    // Skip constructing the full AuditRecord (too many fields) and just test type exists
    let _check_type_exists = |r: AuditResponse| {
        let _ = format!("{:?}", r.record);
    };
}

// =========================================================================
// Coverage Tests: DispatchMetricsResponse
// =========================================================================

#[test]
fn test_dispatch_metrics_response_cov() {
    let resp = DispatchMetricsResponse {
        cpu_dispatches: 100,
        gpu_dispatches: 200,
        total_dispatches: 300,
        gpu_ratio: 0.666,
        cpu_latency_p50_us: 1000.0,
        cpu_latency_p95_us: 2000.0,
        cpu_latency_p99_us: 3000.0,
        gpu_latency_p50_us: 500.0,
        gpu_latency_p95_us: 800.0,
        gpu_latency_p99_us: 1200.0,
        cpu_latency_mean_us: 1100.0,
        gpu_latency_mean_us: 600.0,
        cpu_latency_min_us: 500,
        cpu_latency_max_us: 5000,
        gpu_latency_min_us: 200,
        gpu_latency_max_us: 2000,
        cpu_latency_variance_us: 250000.0,
        cpu_latency_stddev_us: 500.0,
        gpu_latency_variance_us: 100000.0,
        gpu_latency_stddev_us: 316.23,
        bucket_boundaries_us: vec!["0-100".to_string(), "100-500".to_string()],
        cpu_latency_bucket_counts: vec![10, 50, 40],
        gpu_latency_bucket_counts: vec![50, 100, 50],
        throughput_rps: 1000.0,
        elapsed_seconds: 60.0,
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("cpu_dispatches"));
    assert!(json.contains("gpu_ratio"));
    assert!(json.contains("throughput_rps"));
}

// =========================================================================
// Coverage Tests: ServerMetricsResponse
// =========================================================================

#[test]
fn test_server_metrics_response_deserialize_cov() {
    let json = r#"{
        "throughput_tok_per_sec": 100.5,
        "latency_p50_ms": 50.0,
        "latency_p95_ms": 100.0,
        "latency_p99_ms": 200.0,
        "gpu_memory_used_bytes": 1000000,
        "gpu_memory_total_bytes": 8000000,
        "gpu_utilization_percent": 75,
        "cuda_path_active": true,
        "batch_size": 8,
        "queue_depth": 5,
        "total_tokens": 50000,
        "total_requests": 1000,
        "uptime_secs": 3600,
        "model_name": "phi-2"
    }"#;
    let resp: ServerMetricsResponse = serde_json::from_str(json).expect("deserialize");
    assert!((resp.throughput_tok_per_sec - 100.5).abs() < 0.01);
    assert_eq!(resp.batch_size, 8);
    assert!(resp.cuda_path_active);
    assert_eq!(resp.model_name, "phi-2");
}

// =========================================================================
// Coverage Tests: DispatchMetricsQuery
// =========================================================================

#[test]
fn test_dispatch_metrics_query_cov() {
    let query = DispatchMetricsQuery {
        format: Some("prometheus".to_string()),
    };
    assert_eq!(query.format.expect("operation failed"), "prometheus");

    let query_none = DispatchMetricsQuery { format: None };
    assert!(query_none.format.is_none());
}

// =========================================================================
// Coverage Tests: DispatchResetResponse
// =========================================================================

#[test]
fn test_dispatch_reset_response_deserialize_cov() {
    let json = r#"{"success": true, "message": "Reset complete"}"#;
    let resp: DispatchResetResponse = serde_json::from_str(json).expect("deserialize");
    assert!(resp.success);
    assert_eq!(resp.message, "Reset complete");
}

// =========================================================================
// Coverage Tests: BatchGenerateRequest/Response
// =========================================================================

#[test]
fn test_batch_generate_request_full_cov() {
    let req = BatchGenerateRequest {
        prompts: vec!["Hello".to_string(), "World".to_string()],
        max_tokens: 100,
        temperature: 0.7,
        strategy: "top_k".to_string(),
        top_k: 40,
        top_p: 0.95,
        seed: Some(42),
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("Hello"));
    assert!(json.contains("top_k"));
    assert!(json.contains("42"));
}

#[test]
fn test_batch_generate_response_cov() {
    let resp = BatchGenerateResponse {
        results: vec![
            GenerateResponse {
                token_ids: vec![1, 2, 3],
                text: "output1".to_string(),
                num_generated: 3,
            },
            GenerateResponse {
                token_ids: vec![4, 5],
                text: "output2".to_string(),
                num_generated: 2,
            },
        ],
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("output1"));
    assert!(json.contains("output2"));
}

// =========================================================================
// Coverage Tests: ModelsResponse
// =========================================================================

#[test]
fn test_models_response_serialize_cov() {
    use crate::registry::ModelInfo;

    let resp = ModelsResponse {
        models: vec![
            ModelInfo {
                id: "model-1".to_string(),
                name: "LLaMA 7B".to_string(),
                description: "A large language model".to_string(),
                format: "gguf".to_string(),
                loaded: true,
            },
            ModelInfo {
                id: "model-2".to_string(),
                name: "Phi-2".to_string(),
                description: "Small but capable".to_string(),
                format: "safetensors".to_string(),
                loaded: false,
            },
        ],
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("model-1"));
    assert!(json.contains("LLaMA"));
    assert!(json.contains("gguf"));
}

// =========================================================================
// Coverage Tests: ChatCompletionRequest deserialization
// =========================================================================

#[test]
fn test_chat_completion_request_full_cov() {
    let req = ChatCompletionRequest {
        model: "gpt-4".to_string(),
        messages: vec![
            ChatMessage {
                role: "system".to_string(),
                content: "You are helpful".to_string(),
                name: None,
            },
            ChatMessage {
                role: "user".to_string(),
                content: "Hello".to_string(),
                name: Some("Alice".to_string()),
            },
        ],
        max_tokens: Some(100),
        temperature: Some(0.8),
        top_p: Some(0.95),
        n: 2,
        stream: true,
        stop: Some(vec!["###".to_string()]),
        user: Some("user-123".to_string()),
    };
    let json = serde_json::to_string(&req).expect("serialize");
    let parsed: ChatCompletionRequest = serde_json::from_str(&json).expect("deserialize");
    assert_eq!(parsed.model, "gpt-4");
    assert_eq!(parsed.n, 2);
    assert!(parsed.stream);
}

// =========================================================================
// Coverage Tests: ChatChoice
// =========================================================================

#[test]
fn test_chat_choice_serialize_cov() {
    let choice = ChatChoice {
        index: 1,
        message: ChatMessage {
            role: "assistant".to_string(),
            content: "Hello there!".to_string(),
            name: None,
        },
        finish_reason: "length".to_string(),
    };
    let json = serde_json::to_string(&choice).expect("serialize");
    assert!(json.contains("assistant"));
    assert!(json.contains("length"));
    assert!(json.contains("Hello there!"));
}

// =========================================================================
// Coverage Tests: TokenizeRequest/Response
// =========================================================================

#[test]
fn test_tokenize_request_with_model_id_cov() {
    let req = TokenizeRequest {
        text: "test text".to_string(),
        model_id: Some("custom-tokenizer".to_string()),
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("test text"));
    assert!(json.contains("custom-tokenizer"));
}

#[test]
fn test_tokenize_response_serialize_cov() {
    let resp = TokenizeResponse {
        token_ids: vec![101, 102, 103, 104],
        num_tokens: 4,
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("101"));
    assert!(json.contains("num_tokens"));
}

// =========================================================================
// Coverage Tests: GenerateRequest
// =========================================================================

#[test]
fn test_generate_request_top_p_cov() {
    let req = GenerateRequest {
        prompt: "Once upon a time".to_string(),
        max_tokens: 200,
        temperature: 0.5,
        strategy: "top_p".to_string(),
        top_k: 50,
        top_p: 0.85,
        seed: None,
        model_id: Some("story-model".to_string()),
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("top_p"));
    assert!(json.contains("0.85"));
    assert!(json.contains("story-model"));
}

// =========================================================================
// Coverage Tests: OpenAIModelsResponse
// =========================================================================

#[test]
fn test_openai_models_response_deserialize_cov() {
    let json = r#"{
        "object": "list",
        "data": [
            {
                "id": "gpt-4-turbo",
                "object": "model",
                "created": 1699000000,
                "owned_by": "openai"
            }
        ]
    }"#;
    let resp: OpenAIModelsResponse = serde_json::from_str(json).expect("deserialize");
    assert_eq!(resp.object, "list");
    assert_eq!(resp.data.len(), 1);
    assert_eq!(resp.data[0].id, "gpt-4-turbo");
}

// =========================================================================
// Coverage Tests: InMemorySinkWrapper
// =========================================================================

#[test]
fn test_in_memory_sink_wrapper_flush_cov() {
    use crate::audit::AuditSink;

    let sink = Arc::new(InMemoryAuditSink::new());
    let wrapper = InMemorySinkWrapper(sink.clone());

    // Test flush returns Ok
    let result = wrapper.flush();
    assert!(result.is_ok());

    // Test write_batch returns Ok
    let result = wrapper.write_batch(&[]);
    assert!(result.is_ok());
}

// =========================================================================
// Coverage Tests: AppState methods
// =========================================================================

#[test]
fn test_app_state_has_quantized_model_cov() {
    let state = AppState::demo().expect("test");
    // Demo state doesn't have quantized model
    assert!(!state.has_quantized_model());
    assert!(state.quantized_model().is_none());
}

#[test]
fn test_app_state_with_cache_cov() {
    let state = AppState::with_cache(10);
    // Should have model and tokenizer
    assert!(state.model.is_some());
    assert!(state.tokenizer.is_some());
    // Should have cache
    assert!(state.cache.is_some());
}

#[test]
fn test_app_state_get_model_no_registry_cov() {
    let state = AppState::demo().expect("test");
    // Single model mode - should return model
    let result = state.get_model(None);
    assert!(result.is_ok());
}

// =========================================================================
// Coverage Tests: create_demo_apr_model
// =========================================================================

#[test]
fn test_create_demo_apr_model_cov() {
    let result = crate::api::create_demo_apr_model(8);
    assert!(result.is_ok());
    let model = result.expect("operation failed");
    assert_eq!(model.tensor_count(), 1);
}

// =========================================================================
// Coverage Tests: OpenAI completions types
// =========================================================================

#[test]
fn test_completion_request_cov() {
    let json = r#"{
        "model": "gpt-3.5-turbo-instruct",
        "prompt": "Say hello",
        "max_tokens": 50
    }"#;
    let req: CompletionRequest = serde_json::from_str(json).expect("deserialize");
    assert_eq!(req.model, "gpt-3.5-turbo-instruct");
    assert_eq!(req.prompt, "Say hello");
}

#[test]
fn test_completion_response_cov() {
    let resp = CompletionResponse {
        id: "cmpl-123".to_string(),
        object: "text_completion".to_string(),
        created: 1700000000,
        model: "text-davinci-003".to_string(),
        choices: vec![CompletionChoice {
            text: "Hello!".to_string(),
            index: 0,
            logprobs: None,
            finish_reason: "stop".to_string(),
        }],
        usage: Usage {
            prompt_tokens: 5,
            completion_tokens: 2,
            total_tokens: 7,
        },
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("cmpl-123"));
    assert!(json.contains("text_completion"));
}

#[test]
fn test_completion_choice_cov() {
    let choice = CompletionChoice {
        text: "Generated text".to_string(),
        index: 2,
        logprobs: Some(serde_json::json!({"tokens": [], "token_logprobs": []})),
        finish_reason: "length".to_string(),
    };
    let json = serde_json::to_string(&choice).expect("serialize");
    assert!(json.contains("Generated text"));
    assert!(json.contains("logprobs"));
}

// =========================================================================
// Coverage Tests: Embedding types
// =========================================================================

#[test]
fn test_embedding_response_cov() {
    let resp = EmbeddingResponse {
        object: "list".to_string(),
        data: vec![EmbeddingData {
            object: "embedding".to_string(),
            index: 0,
            embedding: vec![0.1, 0.2, 0.3, 0.4],
        }],
        model: "text-embedding-ada-002".to_string(),
        usage: EmbeddingUsage {
            prompt_tokens: 4,
            total_tokens: 4,
        },
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("text-embedding-ada-002"));
    assert!(json.contains("0.1"));
}

#[test]
fn test_embedding_data_cov() {
    let data = EmbeddingData {
        object: "embedding".to_string(),
        index: 5,
        embedding: vec![0.5; 768],
    };
    assert_eq!(data.index, 5);
    assert_eq!(data.embedding.len(), 768);
}

#[test]
fn test_embedding_usage_cov() {
    let usage = EmbeddingUsage {
        prompt_tokens: 100,
        total_tokens: 100,
    };
    let json = serde_json::to_string(&usage).expect("serialize");
    assert!(json.contains("100"));
}

// =========================================================================
// Coverage Tests: ErrorResponse variations
// =========================================================================

#[test]
fn test_error_response_long_message_ext_cov() {
    let resp = ErrorResponse {
        error: "A".repeat(1000),
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.len() > 1000);
}

#[test]
fn test_error_response_special_chars_ext_cov() {
    let resp = ErrorResponse {
        error: "Error with \"quotes\" and \\backslashes\\".to_string(),
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("\\\""));
}

// =========================================================================
// Coverage Tests: Usage struct
// =========================================================================

#[test]
fn test_usage_serialize_ext_cov() {
    let usage = Usage {
        prompt_tokens: 150,
        completion_tokens: 75,
        total_tokens: 225,
    };
    let json = serde_json::to_string(&usage).expect("serialize");
    assert!(json.contains("225"));
}

#[test]
fn test_usage_zero_values_ext_cov() {
    let usage = Usage {
        prompt_tokens: 0,
        completion_tokens: 0,
        total_tokens: 0,
    };
    assert_eq!(usage.total_tokens, 0);
}

// =========================================================================
// Coverage Tests: TokenizeResponse
// =========================================================================

#[test]
fn test_tokenize_response_empty_ext_cov() {
    let resp = TokenizeResponse {
        token_ids: vec![],
        num_tokens: 0,
    };
    assert!(resp.token_ids.is_empty());
    assert_eq!(resp.num_tokens, 0);
}

#[test]
fn test_tokenize_response_large_ext_cov() {
    let resp = TokenizeResponse {
        token_ids: vec![1; 10000],
        num_tokens: 10000,
    };
    assert_eq!(resp.token_ids.len(), 10000);
}

// =========================================================================
// Coverage Tests: EmbeddingRequest
// =========================================================================

#[test]
fn test_embedding_request_serialize_ext_cov() {
    let req = EmbeddingRequest {
        input: "Embed this text".to_string(),
        model: Some("text-embedding-ada-002".to_string()),
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("Embed this text"));
}

#[test]
fn test_embedding_request_deserialize_ext_cov() {
    let json = r#"{"input":"Hello","model":"test-model"}"#;
    let req: EmbeddingRequest = serde_json::from_str(json).expect("deserialize");
    assert_eq!(req.input, "Hello");
    assert_eq!(req.model, Some("test-model".to_string()));
}

// =========================================================================
// Coverage Tests: BatchGenerateRequest
// =========================================================================

#[test]
fn test_batch_generate_request_serialize_ext_cov() {
    let req = BatchGenerateRequest {
        prompts: vec!["Hello".to_string(), "World".to_string()],
        max_tokens: 50,
        temperature: 0.8,
        strategy: "greedy".to_string(),
        top_k: 40,
        top_p: 0.9,
        seed: None,
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("Hello"));
    assert!(json.contains("World"));
}

#[test]
fn test_batch_generate_request_single_prompt_ext_cov() {
    let req = BatchGenerateRequest {
        prompts: vec!["Single prompt".to_string()],
        max_tokens: 100,
        temperature: 1.0,
        strategy: "top_k".to_string(),
        top_k: 50,
        top_p: 1.0,
        seed: Some(42),
    };
    assert_eq!(req.prompts.len(), 1);
}

// =========================================================================
// Coverage Tests: ModelLineage struct
// =========================================================================

#[test]
fn test_model_lineage_serialize_cov() {
    let lineage = ModelLineage {
        uri: "pacha://org/model".to_string(),
        version: "1.0.0".to_string(),
        recipe: Some("training-recipe-v1".to_string()),
        parent: Some("parent-model".to_string()),
        content_hash: "abc123def456".to_string(),
    };
    let json = serde_json::to_string(&lineage).expect("serialize");
    assert!(json.contains("pacha://org/model"));
    assert!(json.contains("1.0.0"));
    assert!(json.contains("abc123def456"));
}

#[test]
fn test_model_lineage_deserialize_cov() {
    let json = r#"{
        "uri": "pacha://test/model",
        "version": "2.0.0",
        "content_hash": "hash123"
    }"#;
    let lineage: ModelLineage = serde_json::from_str(json).expect("deserialize");
    assert_eq!(lineage.uri, "pacha://test/model");
    assert_eq!(lineage.version, "2.0.0");
    assert!(lineage.recipe.is_none());
    assert!(lineage.parent.is_none());
}

#[test]
fn test_model_lineage_clone_debug_cov() {
    let lineage = ModelLineage {
        uri: "pacha://x/y".to_string(),
        version: "0.1.0".to_string(),
        recipe: None,
        parent: None,
        content_hash: "xyz".to_string(),
    };
    let cloned = lineage.clone();
    assert_eq!(lineage.uri, cloned.uri);
    let debug_str = format!("{:?}", lineage);
    assert!(debug_str.contains("uri"));
    assert!(debug_str.contains("version"));
}

// =========================================================================
// Coverage Tests: ReloadRequest/ReloadResponse structs
// =========================================================================

#[test]
fn test_reload_request_full_cov() {
    let req = ReloadRequest {
        model: Some("my-model".to_string()),
        path: Some("/path/to/model.gguf".to_string()),
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("my-model"));
    assert!(json.contains("/path/to/model.gguf"));
}

#[test]
fn test_reload_request_empty_cov() {
    let req = ReloadRequest {
        model: None,
        path: None,
    };
    let json = serde_json::to_string(&req).expect("serialize");
    // Should be minimal since both are None with skip_serializing_if
    assert_eq!(json, "{}");
}

#[test]
fn test_reload_request_deserialize_cov() {
    let json = r#"{"model": "test-model"}"#;
    let req: ReloadRequest = serde_json::from_str(json).expect("deserialize");
    assert_eq!(req.model, Some("test-model".to_string()));
    assert!(req.path.is_none());
}

#[test]
fn test_reload_response_success_cov() {
    let resp = ReloadResponse {
        success: true,
        message: "Model reloaded successfully".to_string(),
        reload_time_ms: 150,
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("true"));
    assert!(json.contains("150"));
}

#[test]
fn test_reload_response_failure_cov() {
    let resp = ReloadResponse {
        success: false,
        message: "Model file not found".to_string(),
        reload_time_ms: 0,
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("false"));
    assert!(json.contains("not found"));
}

#[test]
fn test_reload_response_clone_debug_cov() {
    let resp = ReloadResponse {
        success: true,
        message: "OK".to_string(),
        reload_time_ms: 50,
    };
    let cloned = resp.clone();
    assert_eq!(resp.success, cloned.success);
    let debug_str = format!("{:?}", resp);
    assert!(debug_str.contains("success"));
}

// =========================================================================
// Coverage Tests: CompletionRequest struct
// =========================================================================

#[test]
fn test_completion_request_full_cov2() {
    let req = CompletionRequest {
        model: "gpt-3.5-turbo".to_string(),
        prompt: "Once upon a time".to_string(),
        max_tokens: Some(100),
        temperature: Some(0.7),
        top_p: Some(0.9),
        stop: Some(vec!["\n".to_string(), "END".to_string()]),
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("gpt-3.5-turbo"));
    assert!(json.contains("Once upon a time"));
    assert!(json.contains("100"));
}

#[test]
fn test_completion_request_minimal_cov() {
    let req = CompletionRequest {
        model: "test".to_string(),
        prompt: "Hello".to_string(),
        max_tokens: None,
        temperature: None,
        top_p: None,
        stop: None,
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("test"));
    // Optional fields should be omitted
    assert!(!json.contains("max_tokens"));
}

#[test]
fn test_completion_request_deserialize_cov() {
    let json = r#"{"model":"llama","prompt":"Test prompt","max_tokens":50}"#;
    let req: CompletionRequest = serde_json::from_str(json).expect("deserialize");
    assert_eq!(req.model, "llama");
    assert_eq!(req.prompt, "Test prompt");
    assert_eq!(req.max_tokens, Some(50));
}

// =========================================================================
// Coverage Tests: CompletionResponse struct
// =========================================================================

#[test]
fn test_completion_response_full_cov2() {
    let resp = CompletionResponse {
        id: "cmpl-123".to_string(),
        object: "text_completion".to_string(),
        created: 1700000000,
        model: "gpt-3.5-turbo".to_string(),
        choices: vec![CompletionChoice {
            text: "Generated text here".to_string(),
            index: 0,
            logprobs: None,
            finish_reason: "stop".to_string(),
        }],
        usage: Usage {
            prompt_tokens: 10,
            completion_tokens: 20,
            total_tokens: 30,
        },
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("cmpl-123"));
    assert!(json.contains("text_completion"));
    assert!(json.contains("Generated text here"));
}

#[test]
fn test_completion_response_multiple_choices_cov() {
    let resp = CompletionResponse {
        id: "cmpl-456".to_string(),
        object: "text_completion".to_string(),
        created: 1700000001,
        model: "llama".to_string(),
        choices: vec![
            CompletionChoice {
                text: "Choice A".to_string(),
                index: 0,
                logprobs: Some(serde_json::json!({"tokens": []})),
                finish_reason: "length".to_string(),
            },
            CompletionChoice {
                text: "Choice B".to_string(),
                index: 1,
                logprobs: None,
                finish_reason: "stop".to_string(),
            },
        ],
        usage: Usage {
            prompt_tokens: 5,
            completion_tokens: 10,
            total_tokens: 15,
        },
    };
    assert_eq!(resp.choices.len(), 2);
    assert!(resp.choices[0].logprobs.is_some());
    assert!(resp.choices[1].logprobs.is_none());
}

// =========================================================================
// Coverage Tests: CompletionChoice struct
// =========================================================================

#[test]
fn test_completion_choice_with_logprobs_cov() {
    let choice = CompletionChoice {
        text: "Hello world".to_string(),
        index: 0,
        logprobs: Some(serde_json::json!({
            "tokens": ["Hello", " world"],
            "token_logprobs": [-0.5, -0.3]
        })),
        finish_reason: "stop".to_string(),
    };
    let json = serde_json::to_string(&choice).expect("serialize");
    assert!(json.contains("Hello world"));
    assert!(json.contains("logprobs"));
}

#[test]
fn test_completion_choice_finish_reasons_cov() {
    for reason in ["stop", "length", "content_filter"] {
        let choice = CompletionChoice {
            text: "test".to_string(),
            index: 0,
            logprobs: None,
            finish_reason: reason.to_string(),
        };
        assert_eq!(choice.finish_reason, reason);
    }
}

#[test]
fn test_completion_choice_clone_debug_cov() {
    let choice = CompletionChoice {
        text: "Output".to_string(),
        index: 5,
        logprobs: None,
        finish_reason: "length".to_string(),
    };
    let cloned = choice.clone();
    assert_eq!(choice.index, cloned.index);
    let debug_str = format!("{:?}", choice);
    assert!(debug_str.contains("index"));
    assert!(debug_str.contains("finish_reason"));
}

// =========================================================================
// Coverage Tests: GenerateRequest variations
// =========================================================================

#[test]
fn test_generate_request_all_strategies_cov() {
    for strategy in ["greedy", "top_k", "top_p", "nucleus"] {
        let req = GenerateRequest {
            prompt: "Test".to_string(),
            max_tokens: 10,
            temperature: 0.5,
            strategy: strategy.to_string(),
            top_k: 40,
            top_p: 0.95,
            seed: Some(123),
            model_id: None,
        };
        assert_eq!(req.strategy, strategy);
    }
}

#[test]
fn test_generate_request_with_model_id_cov() {
    let req = GenerateRequest {
        prompt: "Hello".to_string(),
        max_tokens: 50,
        temperature: 1.0,
        strategy: "greedy".to_string(),
        top_k: 50,
        top_p: 1.0,
        seed: None,
        model_id: Some("custom-model-v2".to_string()),
    };
    assert_eq!(req.model_id, Some("custom-model-v2".to_string()));
}

#[test]
fn test_generate_request_temperature_extremes_cov() {
    let cold = GenerateRequest {
        prompt: "Cold".to_string(),
        max_tokens: 5,
        temperature: 0.0,
        strategy: "greedy".to_string(),
        top_k: 1,
        top_p: 1.0,
        seed: Some(1),
        model_id: None,
    };
    let hot = GenerateRequest {
        prompt: "Hot".to_string(),
        max_tokens: 5,
        temperature: 2.0,
        strategy: "top_p".to_string(),
        top_k: 100,
        top_p: 0.99,
        seed: Some(2),
        model_id: None,
    };
    assert!(cold.temperature < 0.1);
    assert!(hot.temperature > 1.5);
}

// =========================================================================
// Coverage Tests: GenerateResponse struct
// =========================================================================

#[test]
fn test_generate_response_serialize_cov() {
    let resp = GenerateResponse {
        token_ids: vec![1, 2, 3, 4],
        text: "The quick brown fox".to_string(),
        num_generated: 4,
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("quick brown fox"));
    assert!(json.contains("4"));
}

#[test]
fn test_generate_response_multiple_tokens_cov() {
    let resp = GenerateResponse {
        token_ids: vec![100, 200, 300],
        text: "Hello world".to_string(),
        num_generated: 3,
    };
    assert_eq!(resp.token_ids.len(), 3);
    assert_eq!(resp.num_generated, 3);
}

#[test]
fn test_generate_response_empty_cov() {
    let resp = GenerateResponse {
        token_ids: vec![],
        text: String::new(),
        num_generated: 0,
    };
    assert!(resp.text.is_empty());
    assert_eq!(resp.num_generated, 0);
    assert!(resp.token_ids.is_empty());
}

#[test]
fn test_generate_response_deserialize_cov() {
    let json = r#"{"token_ids":[1,2,3],"text":"abc","num_generated":3}"#;
    let resp: GenerateResponse = serde_json::from_str(json).expect("deserialize");
    assert_eq!(resp.token_ids, vec![1, 2, 3]);
    assert_eq!(resp.text, "abc");
}

// =========================================================================
// Coverage Tests: ChatMessage struct
// =========================================================================

#[test]
fn test_chat_message_all_roles_cov() {
    for role in ["system", "user", "assistant", "function"] {
        let msg = ChatMessage {
            role: role.to_string(),
            content: format!("Content for {}", role),
            name: None,
        };
        assert_eq!(msg.role, role);
    }
}

#[test]
fn test_chat_message_long_content_cov() {
    let msg = ChatMessage {
        role: "user".to_string(),
        content: "X".repeat(10000),
        name: None,
    };
    assert_eq!(msg.content.len(), 10000);
}

#[test]
fn test_chat_message_empty_content_cov() {
    let msg = ChatMessage {
        role: "assistant".to_string(),
        content: String::new(),
        name: None,
    };
    assert!(msg.content.is_empty());
}

#[test]
fn test_chat_message_serialize_roundtrip_cov() {
    let msg = ChatMessage {
        role: "user".to_string(),
        content: "Hello with \"quotes\" and \\backslash\\".to_string(),
        name: Some("test_user".to_string()),
    };
    let json = serde_json::to_string(&msg).expect("serialize");
    let deserialized: ChatMessage = serde_json::from_str(&json).expect("deserialize");
    assert_eq!(msg.role, deserialized.role);
    assert_eq!(msg.content, deserialized.content);
    assert_eq!(msg.name, deserialized.name);
}

#[test]
fn test_chat_message_with_name_cov() {
    let msg = ChatMessage {
        role: "function".to_string(),
        content: "Function result".to_string(),
        name: Some("my_function".to_string()),
    };
    assert_eq!(msg.name, Some("my_function".to_string()));
    let json = serde_json::to_string(&msg).expect("serialize");
    assert!(json.contains("my_function"));
}

// =========================================================================
// Coverage Tests: ModelMetadataResponse struct
// =========================================================================

#[test]
fn test_model_metadata_response_full_cov() {
    let metadata = ModelMetadataResponse {
        id: "my-model-v1".to_string(),
        name: "My Model V1".to_string(),
        format: "GGUF".to_string(),
        size_bytes: 4_000_000_000,
        quantization: Some("Q4_K_M".to_string()),
        context_length: 4096,
        lineage: Some(ModelLineage {
            uri: "pacha://org/model".to_string(),
            version: "1.0.0".to_string(),
            recipe: None,
            parent: None,
            content_hash: "abc".to_string(),
        }),
        loaded: true,
    };
    let json = serde_json::to_string(&metadata).expect("serialize");
    assert!(json.contains("my-model-v1"));
    assert!(json.contains("My Model V1"));
    assert!(json.contains("4096"));
    assert!(json.contains("Q4_K_M"));
}

#[test]
fn test_model_metadata_response_minimal_cov() {
    let metadata = ModelMetadataResponse {
        id: "basic".to_string(),
        name: "Basic Model".to_string(),
        format: "APR".to_string(),
        size_bytes: 100_000_000,
        quantization: None,
        context_length: 1024,
        lineage: None,
        loaded: false,
    };
    let json = serde_json::to_string(&metadata).expect("serialize");
    assert!(json.contains("basic"));
    assert!(!json.contains("quantization"));
}

// =========================================================================
// Coverage Tests: OpenAIModel struct
// =========================================================================

#[test]
fn test_openai_model_serialize_cov() {
    let model = OpenAIModel {
        id: "gpt-4".to_string(),
        object: "model".to_string(),
        created: 1700000000,
        owned_by: "openai".to_string(),
    };
    let json = serde_json::to_string(&model).expect("serialize");
    assert!(json.contains("gpt-4"));
    assert!(json.contains("openai"));
}

#[test]
fn test_openai_model_deserialize_cov() {
    let json = r#"{"id":"llama","object":"model","created":0,"owned_by":"meta"}"#;
    let model: OpenAIModel = serde_json::from_str(json).expect("deserialize");
    assert_eq!(model.id, "llama");
    assert_eq!(model.owned_by, "meta");
}

// =========================================================================
// Coverage Tests: format_chat_messages function via integration
// =========================================================================

#[test]
fn test_chat_message_formatting_multiline_cov() {
    let messages = vec![
        ChatMessage {
            role: "system".to_string(),
            content: "You are helpful.".to_string(),
            name: None,
        },
        ChatMessage {
            role: "user".to_string(),
            content: "Hello\nWorld".to_string(),
            name: None,
        },
    ];
    // Ensure multiline content is preserved
    assert!(messages[1].content.contains('\n'));
}

// =========================================================================
// Coverage Tests: ContextWindowManager edge cases
// =========================================================================

#[test]
fn test_context_window_manager_preserve_system_false_cov() {
    let config = ContextWindowConfig {
        max_tokens: 100,
        reserved_output_tokens: 20,
        preserve_system: false,
    };
    let manager = ContextWindowManager::new(config);
    let messages = vec![
        ChatMessage {
            role: "system".to_string(),
            content: "System message".to_string(),
            name: None,
        },
        ChatMessage {
            role: "user".to_string(),
            content: "User message".to_string(),
            name: None,
        },
    ];
    let (truncated, _) = manager.truncate_messages(&messages);
    // With preserve_system=false, system messages are treated like others
    assert!(!truncated.is_empty());
}

#[test]
fn test_context_window_config_available_tokens_cov() {
    let config = ContextWindowConfig::new(1000).with_reserved_output(200);
    // Internal method - test via needs_truncation
    let manager = ContextWindowManager::new(config);
    // Short messages should not need truncation
    let messages = vec![ChatMessage {
        role: "user".to_string(),
        content: "Short".to_string(),
        name: None,
    }];
    assert!(!manager.needs_truncation(&messages));
}

// =========================================================================
// Coverage Tests: BatchQueueStats struct (GPU feature)
// =========================================================================

#[cfg(feature = "gpu")]
#[test]
fn test_batch_queue_stats_default_cov() {
    let stats = BatchQueueStats::default();
    assert_eq!(stats.total_queued, 0);
    assert_eq!(stats.total_batches, 0);
    assert_eq!(stats.total_single, 0);
    assert!((stats.avg_batch_size - 0.0).abs() < f64::EPSILON);
}

#[cfg(feature = "gpu")]
#[test]
fn test_batch_queue_stats_clone_debug_cov() {
    let stats = BatchQueueStats {
        total_queued: 100,
        total_batches: 10,
        total_single: 5,
        avg_batch_size: 10.0,
        avg_wait_ms: 5.5,
    };
    let cloned = stats.clone();
    assert_eq!(stats.total_queued, cloned.total_queued);
    let debug_str = format!("{:?}", stats);
    assert!(debug_str.contains("total_queued"));
}

// =========================================================================
// Coverage Tests: BatchConfig struct (GPU feature)
// =========================================================================

#[cfg(feature = "gpu")]
#[test]
fn test_batch_config_default_cov() {
    let config = BatchConfig::default();
    assert_eq!(config.window_ms, 50);
    assert_eq!(config.min_batch, 4);
    assert_eq!(config.optimal_batch, 32);
    assert_eq!(config.max_batch, 64);
}

#[cfg(feature = "gpu")]
#[test]
fn test_batch_config_low_latency_cov() {
    let config = BatchConfig::low_latency();
    assert_eq!(config.window_ms, 5);
    assert_eq!(config.min_batch, 2);
    assert_eq!(config.optimal_batch, 8);
    assert_eq!(config.max_batch, 16);
}

#[cfg(feature = "gpu")]
#[test]
fn test_batch_config_high_throughput_cov() {
    let config = BatchConfig::high_throughput();
    assert_eq!(config.window_ms, 100);
    assert_eq!(config.min_batch, 8);
    assert_eq!(config.optimal_batch, 32);
    assert_eq!(config.max_batch, 128);
}

#[cfg(feature = "gpu")]
#[test]
fn test_batch_config_should_process_cov() {
    let config = BatchConfig::default();
    assert!(!config.should_process(10)); // Below optimal
    assert!(config.should_process(32)); // At optimal
    assert!(config.should_process(64)); // Above optimal
}

#[cfg(feature = "gpu")]
#[test]
fn test_batch_config_meets_minimum_cov() {
    let config = BatchConfig::default();
    assert!(!config.meets_minimum(2)); // Below min
    assert!(config.meets_minimum(4)); // At min
    assert!(config.meets_minimum(10)); // Above min
}

// =========================================================================
// Coverage Tests: ContinuousBatchResponse struct (GPU feature)
// =========================================================================

#[cfg(feature = "gpu")]
#[test]
fn test_continuous_batch_response_single_cov() {
    let resp = ContinuousBatchResponse::single(
        vec![1, 2, 3, 4, 5], // token_ids
        3,                   // prompt_len
        10.5,                // latency_ms
    );
    assert_eq!(resp.token_ids, vec![1, 2, 3, 4, 5]);
    assert_eq!(resp.prompt_len, 3);
    assert!(!resp.batched);
    assert_eq!(resp.batch_size, 1);
}

#[cfg(feature = "gpu")]
#[test]
fn test_continuous_batch_response_batched_cov() {
    let resp = ContinuousBatchResponse::batched(
        vec![10, 20, 30, 40, 50, 60], // token_ids
        4,                            // prompt_len
        8,                            // batch_size
        25.0,                         // latency_ms
    );
    assert!(resp.batched);
    assert_eq!(resp.batch_size, 8);
}

#[cfg(feature = "gpu")]
#[test]
fn test_continuous_batch_response_generated_tokens_cov() {
    let resp = ContinuousBatchResponse::single(vec![1, 2, 3, 4, 5], 2, 5.0);
    let generated = resp.generated_tokens();
    assert_eq!(generated, &[3, 4, 5]);
}

#[cfg(feature = "gpu")]
#[test]
fn test_continuous_batch_response_generated_tokens_empty_cov() {
    // Edge case: prompt_len >= token_ids.len()
    let resp = ContinuousBatchResponse::single(vec![1, 2], 5, 1.0);
    let generated = resp.generated_tokens();
    assert!(generated.is_empty());
}

// =========================================================================
// Coverage Tests: BatchProcessResult struct (GPU feature)
// =========================================================================

#[cfg(feature = "gpu")]
#[test]
fn test_batch_process_result_debug_cov() {
    let result = BatchProcessResult {
        requests_processed: 10,
        was_batched: true,
        total_time_ms: 100.0,
        avg_latency_ms: 10.0,
    };
    let debug_str = format!("{:?}", result);
    assert!(debug_str.contains("requests_processed"));
    assert!(debug_str.contains("was_batched"));
}

// =========================================================================
// Extended Coverage Tests Phase 2: Unique API tests
// =========================================================================

#[test]
fn test_health_response_roundtrip_ext_cov() {
    let json = r#"{"status":"ok","version":"2.0.0","compute_mode":"gpu"}"#;
    let resp: HealthResponse = serde_json::from_str(json).expect("parse failed");
    assert_eq!(resp.status, "ok");
    assert_eq!(resp.version, "2.0.0");
    assert_eq!(resp.compute_mode, "gpu");
}

#[test]
fn test_tokenize_request_without_model_ext_cov() {
    let json = r#"{"text":"test input"}"#;
    let req: TokenizeRequest = serde_json::from_str(json).expect("parse failed");
    assert_eq!(req.text, "test input");
    assert!(req.model_id.is_none());
}

#[test]
fn test_generate_request_all_fields_ext_cov() {
    let json = r#"{
        "prompt": "Hello",
        "max_tokens": 100,
        "temperature": 0.7,
        "strategy": "top_k",
        "top_k": 40,
        "top_p": 0.95,
        "seed": 42,
        "model_id": "gpt-test"
    }"#;
    let req: GenerateRequest = serde_json::from_str(json).expect("parse failed");
    assert_eq!(req.prompt, "Hello");
    assert_eq!(req.max_tokens, 100);
    assert_eq!(req.seed, Some(42));
    assert_eq!(req.model_id, Some("gpt-test".to_string()));
}

#[test]
fn test_batch_generate_request_with_seed_ext_cov() {
    let json = r#"{"prompts":["test"],"seed":12345}"#;
    let req: BatchGenerateRequest = serde_json::from_str(json).expect("parse failed");
    assert_eq!(req.seed, Some(12345));
}

#[test]
fn test_chat_message_with_name_ext_cov() {
    let msg = ChatMessage {
        role: "system".to_string(),
        content: "You are helpful".to_string(),
        name: Some("assistant_v2".to_string()),
    };
    let json = serde_json::to_string(&msg).expect("invalid UTF-8");
    assert!(json.contains("assistant_v2"));
}

#[test]
fn test_chat_completion_request_stream_ext_cov() {
    let json = r#"{"model":"x","messages":[],"stream":true}"#;
    let req: ChatCompletionRequest = serde_json::from_str(json).expect("parse failed");
    assert!(req.stream);
}

#[test]
fn test_chat_completion_chunk_methods_ext_cov() {
    // Test all chunk creation methods
    let chunk1 = ChatCompletionChunk::new("id1", "m", Some("text".to_string()), None);
    assert_eq!(chunk1.choices[0].delta.content, Some("text".to_string()));

    let chunk2 = ChatCompletionChunk::initial("id2", "m");
    assert_eq!(chunk2.choices[0].delta.role, Some("assistant".to_string()));

    let chunk3 = ChatCompletionChunk::content("id3", "m", "hello");
    assert_eq!(chunk3.choices[0].delta.content, Some("hello".to_string()));

    let chunk4 = ChatCompletionChunk::done("id4", "m");
    assert_eq!(chunk4.choices[0].finish_reason, Some("stop".to_string()));
}

#[test]
fn test_predict_response_skip_serialization_ext_cov() {
    let resp = PredictResponse {
        request_id: "req-456".to_string(),
        model: "model".to_string(),
        prediction: serde_json::json!(42.5),
        confidence: None,
        top_k_predictions: None,
        latency_ms: 0.5,
    };
    let json = serde_json::to_string(&resp).expect("invalid UTF-8");
    // confidence and top_k should be skipped with skip_serializing_if
    assert!(!json.contains("\"confidence\""));
    assert!(!json.contains("\"top_k_predictions\""));
}

#[test]
fn test_default_functions_ext_cov() {
    assert_eq!(default_max_tokens(), 50);
    assert_eq!(default_temperature(), 1.0);
    assert_eq!(default_strategy(), "greedy");
    assert_eq!(default_top_k(), 50);
    assert_eq!(default_top_p(), 0.9);
    assert!(default_true());
}

// =========================================================================
// More Coverage Tests (_more_cov suffix) - 56 new tests
// =========================================================================

#[test]
fn test_app_state_new_creates_valid_state_more_cov() {
    let config = ModelConfig {
        vocab_size: 100,
        hidden_dim: 32,
        num_heads: 1,
        num_layers: 1,
        intermediate_dim: 64,
        eps: 1e-5,
    };
    let model = Model::new(config).expect("create model");
    let vocab: Vec<String> = (0..100).map(|i| format!("tok{i}")).collect();
    let tokenizer = BPETokenizer::new(vocab, vec![], "tok0").expect("create tokenizer");
    let state = AppState::new(model, tokenizer);
    assert!(state.model.is_some());
    assert!(state.tokenizer.is_some());
}

#[test]
fn test_app_state_with_cache_creates_model_cache_more_cov() {
    let state = AppState::with_cache(5);
    assert!(state.cache.is_some());
    assert!(state.cache_key.is_some());
}

#[test]
fn test_error_response_serialize_more_cov() {
    let err = ErrorResponse {
        error: "Test error message".to_string(),
    };
    let json = serde_json::to_string(&err).expect("serialize");
    assert!(json.contains("Test error message"));
}

#[test]
fn test_batch_tokenize_request_serialize_more_cov() {
    let req = BatchTokenizeRequest {
        texts: vec!["hello".to_string(), "world".to_string(), "test".to_string()],
    };
    let json = serde_json::to_string(&req).expect("serialize");
    assert!(json.contains("hello"));
    assert!(json.contains("world"));
}

#[test]
fn test_batch_tokenize_response_serialize_more_cov() {
    let resp = BatchTokenizeResponse {
        results: vec![
            TokenizeResponse {
                token_ids: vec![1, 2],
                num_tokens: 2,
            },
            TokenizeResponse {
                token_ids: vec![3, 4, 5],
                num_tokens: 3,
            },
        ],
    };
    let json = serde_json::to_string(&resp).expect("serialize");
    assert!(json.contains("[1,2]"));
    assert!(json.contains("num_tokens"));
}

#[test]
fn test_stream_token_event_serialize_more_cov() {
    let event = StreamTokenEvent {
        token_id: 42,
        text: "hello".to_string(),
    };
    let json = serde_json::to_string(&event).expect("serialize");
    assert!(json.contains("42"));
    assert!(json.contains("hello"));
}

#[test]
fn test_stream_done_event_serialize_more_cov() {
    let event = StreamDoneEvent { num_generated: 15 };
    let json = serde_json::to_string(&event).expect("serialize");
    assert!(json.contains("15"));
}
