//! Additional coverage tests for infer module (Part 2)
//!
//! These tests focus on:
//! - find_fallback_tokenizer function
//! - run_gguf_generate function paths
//! - Error handling edge cases
//! - Configuration validation edge cases

#[cfg(test)]
mod tests {
    use crate::infer::*;
    use std::io::Write;
    use std::path::PathBuf;
    use tempfile::{NamedTempFile, TempDir};

    // =========================================================================
    // find_fallback_tokenizer Tests
    // These test the fallback tokenizer loading path (lines 614-636)
    // =========================================================================

    #[test]
    fn test_find_fallback_tokenizer_nonexistent_path() {
        // Path that doesn't exist should return None
        let result = find_fallback_tokenizer(std::path::Path::new("/nonexistent/model.apr"));
        assert!(result.is_none());
    }

    #[test]
    fn test_find_fallback_tokenizer_not_apr_file() {
        // Create a file that isn't an APR model
        let mut temp = NamedTempFile::with_suffix(".apr").expect("create temp");
        temp.write_all(b"not a valid apr file").expect("write");
        temp.flush().expect("flush");

        let result = find_fallback_tokenizer(temp.path());
        // Should return None because it can't be loaded as APR
        assert!(result.is_none());
    }

    #[test]
    fn test_find_fallback_tokenizer_invalid_apr_header() {
        let mut temp = NamedTempFile::with_suffix(".apr").expect("create temp");
        // Write APR-like magic but invalid structure
        let mut data = Vec::new();
        data.extend_from_slice(b"APR2"); // APR v2 magic
        data.extend_from_slice(&[0u8; 32]); // Incomplete header
        temp.write_all(&data).expect("write");
        temp.flush().expect("flush");

        let result = find_fallback_tokenizer(temp.path());
        // Should return None due to parsing error
        assert!(result.is_none());
    }

    #[test]
    fn test_find_fallback_tokenizer_directory_path() {
        let temp_dir = TempDir::new().expect("create temp dir");
        let result = find_fallback_tokenizer(temp_dir.path());
        // Should return None for directory
        assert!(result.is_none());
    }

    #[test]
    fn test_find_fallback_tokenizer_empty_file() {
        let temp = NamedTempFile::with_suffix(".apr").expect("create temp");
        // Empty file
        let result = find_fallback_tokenizer(temp.path());
        assert!(result.is_none());
    }

    // =========================================================================
    // prefault_mmap Additional Edge Cases
    // =========================================================================

    #[test]
    fn test_prefault_mmap_random_data_pattern() {
        // Test with pseudo-random data pattern
        let data: Vec<u8> = (0..16384).map(|i| ((i * 7 + 13) % 256) as u8).collect();
        prefault_mmap(&data);
    }

    #[test]
    fn test_prefault_mmap_alternating_pages() {
        // Create data where pages have different content
        let mut data = Vec::new();
        for page in 0..4 {
            let fill = (page * 50) as u8;
            data.extend_from_slice(&[fill; 4096]);
        }
        prefault_mmap(&data);
    }

    #[test]
    fn test_prefault_mmap_sparse_pattern() {
        // Mostly zeros with some non-zero values at page boundaries
        let mut data = vec![0u8; 4096 * 5];
        data[0] = 1;
        data[4096] = 2;
        data[8192] = 3;
        data[12288] = 4;
        data[16383] = 5;
        prefault_mmap(&data);
    }

    #[test]
    fn test_prefault_mmap_max_byte_values() {
        // Edge case with maximum byte values
        let data = vec![0xFFu8; 4096 * 2 + 1];
        prefault_mmap(&data);
    }

    // =========================================================================
    // clean_model_output Additional Edge Cases
    // =========================================================================

    #[test]
    fn test_clean_model_output_consecutive_markers() {
        let raw = "<|im_start|><|im_start|><|im_start|>text<|im_end|><|im_end|><|im_end|>";
        let cleaned = clean_model_output(raw);
        assert_eq!(cleaned, "text");
    }

    #[test]
    fn test_clean_model_output_marker_inside_text() {
        // Markers embedded in content
        let raw = "Hello <|im_end|> World";
        let cleaned = clean_model_output(raw);
        assert_eq!(cleaned, "Hello  World");
    }

    #[test]
    fn test_clean_model_output_only_assistant_prefix() {
        let raw = "<|im_start|>assistant\n";
        let cleaned = clean_model_output(raw);
        assert_eq!(cleaned, "");
    }

    #[test]
    fn test_clean_model_output_mixed_line_endings() {
        let raw = "<|im_start|>assistant\r\n\r\nContent\r\n<|im_end|>";
        let cleaned = clean_model_output(raw);
        // Should handle CRLF line endings
        assert!(cleaned.contains("Content"));
    }

    #[test]
    fn test_clean_model_output_unicode_markers() {
        // Ensure markers work with surrounding Unicode
        let raw = "<|im_start|>assistant\n\u{4e2d}\u{6587}<|im_end|>";
        let cleaned = clean_model_output(raw);
        assert_eq!(cleaned, "\u{4e2d}\u{6587}");
    }

    #[test]
    fn test_clean_model_output_escaped_sequences() {
        let raw = r"<|im_start|>assistant
Line with \n escaped newline<|im_end|>";
        let cleaned = clean_model_output(raw);
        assert!(cleaned.contains(r"\n"));
    }

    #[test]
    fn test_clean_model_output_partial_marker_not_removed() {
        // Partial markers should not be removed
        let raw = "<|im_star content <|im_en";
        let cleaned = clean_model_output(raw);
        // Contains partial markers - they should remain
        assert!(cleaned.contains("<|im_star"));
    }

    // =========================================================================
    // InferenceConfig Field Coverage
    // =========================================================================

    #[test]
    fn test_inference_config_trace_output_clone() {
        let mut config = InferenceConfig::new("/model.gguf");
        config.trace_output = Some(PathBuf::from("/output/trace.json"));

        let cloned = config.clone();
        assert_eq!(
            cloned.trace_output,
            Some(PathBuf::from("/output/trace.json"))
        );
    }

    #[test]
    fn test_inference_config_trace_steps_clone() {
        let mut config = InferenceConfig::new("/model.gguf");
        config.trace_steps = Some(vec![
            "step1".to_string(),
            "step2".to_string(),
            "step3".to_string(),
        ]);

        let cloned = config.clone();
        assert_eq!(cloned.trace_steps.as_ref().map(std::vec::Vec::len), Some(3));
    }

    #[test]
    fn test_inference_config_trace_verbose_default() {
        let config = InferenceConfig::new("/model.gguf");
        assert!(!config.trace_verbose);
    }

    #[test]
    fn test_inference_config_all_fields_none() {
        let config = InferenceConfig::new("/model.gguf");
        assert!(config.prompt.is_none());
        assert!(config.input_tokens.is_none());
        assert!(config.trace_output.is_none());
        assert!(config.trace_steps.is_none());
    }

    // =========================================================================
    // run_inference Error Paths
    // =========================================================================

    #[test]
    fn test_run_inference_io_error_symlink_target_missing() {
        // Test with a path that would be a broken symlink
        let config = InferenceConfig::new("/broken/symlink/target.gguf");
        let result = run_inference(&config);
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("Failed to read") || err_str.contains("IO"),
            "Unexpected error: {}",
            err_str
        );
    }

    #[test]
    fn test_run_inference_format_detection_boundary() {
        // Create file with exactly 8 bytes but not a valid format
        let mut temp = NamedTempFile::with_suffix(".bin").expect("create temp");
        // Random data that doesn't match any known magic
        temp.write_all(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0])
            .expect("write");
        temp.flush().expect("flush");

        let config = InferenceConfig::new(temp.path());
        let result = run_inference(&config);
        assert!(result.is_err());
    }

    #[test]
    fn test_run_inference_safetensors_header_boundary() {
        // Create SafeTensors file with header size at boundary
        let mut temp = NamedTempFile::with_suffix(".safetensors").expect("create temp");
        // Header size that indicates SafeTensors but content is malformed
        let header_size: u64 = 1000;
        let mut data = Vec::new();
        data.extend_from_slice(&header_size.to_le_bytes());
        // Incomplete header content
        data.extend_from_slice(b"{malformed");
        temp.write_all(&data).expect("write");
        temp.flush().expect("flush");

        let config = InferenceConfig::new(temp.path());
        let result = run_inference(&config);
        assert!(result.is_err());
    }

    // =========================================================================
    // InferenceResult Field Tests
    // =========================================================================

    #[test]
    fn test_inference_result_high_precision_times() {
        let result = InferenceResult {
            text: "test".to_string(),
            tokens: vec![1],
            input_token_count: 1,
            generated_token_count: 0,
            inference_ms: 0.000001,   // Very small time
            tok_per_sec: 1_000_000.0, // Very high rate
            load_ms: 0.000001,
            format: "GGUF".to_string(),
            used_gpu: false,
        };
        assert!(result.inference_ms > 0.0);
        assert!(result.tok_per_sec > 999999.0);
    }

    #[test]
    fn test_inference_result_extreme_token_counts() {
        let result = InferenceResult {
            text: "test".to_string(),
            tokens: vec![1; 100000],
            input_token_count: 50000,
            generated_token_count: 50000,
            inference_ms: 1000.0,
            tok_per_sec: 50000.0,
            load_ms: 100.0,
            format: "GGUF".to_string(),
            used_gpu: true,
        };
        assert_eq!(
            result.input_token_count + result.generated_token_count,
            100000
        );
        assert_eq!(result.tokens.len(), 100000);
    }

    // =========================================================================
    // Format Detection Integration Tests
    // =========================================================================

    #[test]
    fn test_format_detection_apr_v1_legacy() {
        use crate::format::{detect_format, ModelFormat};
        // APR v1 with ASCII '1' version byte
        let data = b"APR1xxxxxxxx";
        let result = detect_format(data);
        assert!(matches!(result, Ok(ModelFormat::Apr)));
    }

    #[test]
    fn test_format_detection_apr_v2_legacy() {
        use crate::format::{detect_format, ModelFormat};
        // APR v2 with ASCII '2' version byte
        let data = b"APR2xxxxxxxx";
        let result = detect_format(data);
        assert!(matches!(result, Ok(ModelFormat::Apr)));
    }

    #[test]
    fn test_format_detection_apr_legacy_null_version() {
        use crate::format::{detect_format, ModelFormat};
        // APR legacy with null byte version
        let data = b"APR\0xxxxxxxx";
        let result = detect_format(data);
        assert!(matches!(result, Ok(ModelFormat::Apr)));
    }

    #[test]
    #[ignore = "Test expectation needs adjustment"]
    fn test_format_detection_invalid_apr_version() {
        use crate::format::{detect_format, FormatError};
        // APR magic but invalid version byte (not 0, '1', or '2')
        let data = b"APR3xxxxxxxx";
        let result = detect_format(data);
        // Should not match APR, might match SafeTensors if header size valid
        assert!(matches!(result, Err(FormatError::UnknownFormat)) || result.is_ok());
    }

    // =========================================================================
    // Architecture Detection Edge Cases
    // =========================================================================

    #[test]
    #[ignore = "Test expectation needs adjustment"]
    fn test_architecture_detection_embedded_names() {
        // Model names where architecture name is embedded
        let test_cases = [
            ("my-qwen-based-model.gguf", true, "qwen"),
            ("llama-style-network.gguf", true, "llama"),
            ("inspired-by-mistral.gguf", true, "mistral"),
            ("phi-inspired-v2.gguf", true, "phi"),
            ("completely-different.gguf", false, ""),
        ];

        for (filename, should_match, search_term) in test_cases {
            let contains = filename.to_lowercase().contains(search_term);
            assert_eq!(
                contains, should_match,
                "Failed for {}: expected {} to be {}",
                filename, search_term, should_match
            );
        }
    }

    #[test]
    fn test_architecture_detection_numeric_suffixes() {
        let names = [
            "qwen2-7b",
            "llama-3.1",
            "mistral-7b-v0.2",
            "phi-2",
            "phi3-mini",
        ];

        for name in names {
            let lower = name.to_lowercase();
            let has_arch = lower.contains("qwen")
                || lower.contains("llama")
                || lower.contains("mistral")
                || lower.contains("phi");
            assert!(has_arch, "Should detect architecture in: {}", name);
        }
    }

    // =========================================================================
    // Token Processing Edge Cases
    // =========================================================================

    #[test]
    fn test_input_token_empty_vec() {
        let config = InferenceConfig::new("/model.gguf").with_input_tokens(vec![]);
        assert_eq!(config.input_tokens, Some(vec![]));
    }

    #[test]
    fn test_input_token_max_values() {
        let tokens = vec![u32::MAX, u32::MAX - 1, u32::MAX - 2];
        let config = InferenceConfig::new("/model.gguf").with_input_tokens(tokens.clone());
        assert_eq!(config.input_tokens, Some(tokens));
    }

    #[test]
    fn test_input_token_zero_value() {
        let config = InferenceConfig::new("/model.gguf").with_input_tokens(vec![0, 0, 0]);
        assert_eq!(config.input_tokens, Some(vec![0, 0, 0]));
    }

    // =========================================================================
    // Prompt Handling Edge Cases
    // =========================================================================

    #[test]
    fn test_prompt_with_null_bytes() {
        let prompt = "Hello\0World";
        let config = InferenceConfig::new("/model.gguf").with_prompt(prompt);
        assert_eq!(config.prompt, Some(prompt.to_string()));
    }

    #[test]
    fn test_prompt_with_control_characters() {
        let prompt = "Hello\x01\x02\x03World";
        let config = InferenceConfig::new("/model.gguf").with_prompt(prompt);
        assert_eq!(config.prompt, Some(prompt.to_string()));
    }

    #[test]
    fn test_prompt_with_newlines_and_tabs() {
        let prompt = "Line1\nLine2\n\tIndented\n\n\nMultiple newlines";
        let config = InferenceConfig::new("/model.gguf").with_prompt(prompt);
        assert!(config.prompt.as_ref().unwrap().contains('\n'));
        assert!(config.prompt.as_ref().unwrap().contains('\t'));
    }

    // =========================================================================
    // Temperature and Sampling Edge Cases
    // =========================================================================

    #[test]
    fn test_temperature_subnormal_values() {
        // Test with very small positive float
        let config = InferenceConfig::new("/model.gguf").with_temperature(f32::MIN_POSITIVE);
        assert!(config.temperature > 0.0);
        assert!(config.temperature < 0.001);
    }

    #[test]
    fn test_top_k_max_value() {
        let config = InferenceConfig::new("/model.gguf").with_top_k(usize::MAX);
        assert_eq!(config.top_k, usize::MAX);
    }

    #[test]
    fn test_max_tokens_max_value() {
        let config = InferenceConfig::new("/model.gguf").with_max_tokens(usize::MAX);
        assert_eq!(config.max_tokens, usize::MAX);
    }

    // =========================================================================
    // File Format Creation for Testing
    // =========================================================================

    #[test]
    fn test_run_inference_with_minimal_gguf_v3() {
        let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp");
        let mut data = Vec::new();
        // GGUF magic
        data.extend_from_slice(b"GGUF");
        // Version 3 (little-endian u32)
        data.extend_from_slice(&3u32.to_le_bytes());
        // Tensor count = 0
        data.extend_from_slice(&0u64.to_le_bytes());
        // Metadata count = 0
        data.extend_from_slice(&0u64.to_le_bytes());
        // Additional padding
        data.extend_from_slice(&[0u8; 256]);
        temp.write_all(&data).expect("write");
        temp.flush().expect("flush");

        let config = InferenceConfig::new(temp.path())
            .with_prompt("test")
            .with_max_tokens(1);
        let result = run_inference(&config);
        // Should fail due to missing tensors, but format detection should work
        assert!(result.is_err());
    }

    #[test]
    fn test_run_inference_with_apr_v2_minimal() {
        let mut temp = NamedTempFile::with_suffix(".apr").expect("create temp");
        let mut data = Vec::new();
        // APR v2 magic (ASCII '2' as version byte)
        data.extend_from_slice(b"APR2");
        // Minimal header padding
        data.extend_from_slice(&[0u8; 256]);
        temp.write_all(&data).expect("write");
        temp.flush().expect("flush");

        let config = InferenceConfig::new(temp.path()).with_max_tokens(1);
        let result = run_inference(&config);
        // May succeed with degenerate output or fail
        let _ = result;
    }

    // =========================================================================
    // Verbose and Trace Mode Tests
    // =========================================================================

    #[test]
    fn test_verbose_mode_with_all_options() {
        let config = InferenceConfig::new("/model.gguf")
            .with_verbose(true)
            .with_trace(true)
            .with_prompt("test")
            .with_max_tokens(10)
            .with_temperature(0.5)
            .with_top_k(40)
            .without_gpu();

        assert!(config.verbose);
        assert!(config.trace);
        assert!(config.no_gpu);
    }

    #[test]
    fn test_trace_configuration_complete() {
        let mut config = InferenceConfig::new("/model.gguf");
        config.trace = true;
        config.trace_verbose = true;
        config.trace_output = Some(PathBuf::from("/tmp/trace/output.json"));
        config.trace_steps = Some(vec![
            "embedding".to_string(),
            "attention".to_string(),
            "ffn".to_string(),
            "logits".to_string(),
        ]);

        assert!(config.trace);
        assert!(config.trace_verbose);
        assert!(config.trace_output.is_some());
        assert_eq!(config.trace_steps.as_ref().map(std::vec::Vec::len), Some(4));
    }

    // =========================================================================
    // EOS Token Detection Tests
    // =========================================================================

    #[test]
    fn test_eos_detection_boundary_values() {
        // Test values around known EOS tokens
        let eos_tokens = [151645u32, 151643, 2];
        let boundary_values = [151644u32, 151646, 1, 3, 151642];

        for token in eos_tokens {
            let is_eos = token == 151645 || token == 151643 || token == 2;
            assert!(is_eos, "Token {} should be detected as EOS", token);
        }

        for token in boundary_values {
            let is_eos = token == 151645 || token == 151643 || token == 2;
            assert!(!is_eos, "Token {} should NOT be detected as EOS", token);
        }
    }

    // =========================================================================
    // Argmax Logic Tests for Generation
    // =========================================================================

    #[test]
    fn test_argmax_single_element() {
        let logits = vec![0.5f32];
        let next_token = logits
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map_or(0, |(i, _)| i as u32);
        assert_eq!(next_token, 0);
    }

    #[test]
    fn test_argmax_large_vocab() {
        // Simulate large vocabulary
        let mut logits = vec![-1.0f32; 50000];
        logits[32000] = 10.0; // Set one element to be max
        let next_token = logits
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map_or(0, |(i, _)| i as u32);
        assert_eq!(next_token, 32000);
    }

    #[test]
    fn test_argmax_all_negative() {
        let logits = vec![-10.0f32, -5.0, -1.0, -0.5, -2.0];
        let next_token = logits
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map_or(0, |(i, _)| i as u32);
        assert_eq!(next_token, 3); // -0.5 is the maximum
    }

    #[test]
    fn test_argmax_mixed_special_values() {
        let logits = vec![f32::NEG_INFINITY, 0.0, f32::INFINITY, 1.0];
        let next_token = logits
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map_or(0, |(i, _)| i as u32);
        assert_eq!(next_token, 2); // INFINITY is the maximum
    }

    // =========================================================================
    // InferenceResult Builder Pattern Tests
    // =========================================================================

    #[test]
    fn test_inference_result_all_formats() {
        let formats = ["GGUF", "APR", "SafeTensors", "Custom", ""];

        for format in formats {
            let result = InferenceResult {
                text: "test".to_string(),
                tokens: vec![1],
                input_token_count: 1,
                generated_token_count: 0,
                inference_ms: 1.0,
                tok_per_sec: 1.0,
                load_ms: 1.0,
                format: format.to_string(),
                used_gpu: false,
            };
            assert_eq!(result.format, format);
        }
    }

    #[test]
    fn test_inference_result_gpu_combinations() {
        for used_gpu in [true, false] {
            for format in ["GGUF", "APR", "SafeTensors"] {
                let result = InferenceResult {
                    text: "test".to_string(),
                    tokens: vec![1],
                    input_token_count: 1,
                    generated_token_count: 0,
                    inference_ms: 1.0,
                    tok_per_sec: 1.0,
                    load_ms: 1.0,
                    format: format.to_string(),
                    used_gpu,
                };
                assert_eq!(result.used_gpu, used_gpu);
                assert_eq!(result.format, format);
            }
        }
    }

    // =========================================================================
    // Path Handling Edge Cases
    // =========================================================================

    #[test]
    fn test_path_with_consecutive_slashes() {
        let config = InferenceConfig::new("//path//to//model.gguf");
        assert!(config.model_path.to_str().unwrap().contains("model.gguf"));
    }

    #[test]
    fn test_path_with_dot_components() {
        let config = InferenceConfig::new("/path/./to/../to/model.gguf");
        assert!(config.model_path.to_str().unwrap().contains("model.gguf"));
    }

    #[test]
    fn test_path_with_tilde() {
        let config = InferenceConfig::new("~/models/model.gguf");
        assert!(config.model_path.to_str().unwrap().starts_with('~'));
    }

    // =========================================================================
    // Tokens Per Second Calculation Edge Cases
    // =========================================================================

    #[test]
    fn test_tok_per_sec_exactly_one_second() {
        let inference_ms = 1000.0;
        let generated = 50;
        let tok_per_sec = generated as f64 / (inference_ms / 1000.0);
        assert!((tok_per_sec - 50.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_tok_per_sec_microsecond_precision() {
        let inference_ms = 0.001; // 1 microsecond
        let generated = 1;
        let tok_per_sec = if inference_ms > 0.0 {
            generated as f64 / (inference_ms / 1000.0)
        } else {
            0.0
        };
        // 1 / 0.000001 = 1,000,000
        assert!((tok_per_sec - 1_000_000.0).abs() < 0.001);
    }

    // =========================================================================
    // Config Builder Immutability Test
    // =========================================================================

    #[test]
    fn test_config_builder_creates_new_instance() {
        let base = InferenceConfig::new("/model.gguf");
        let with_prompt = base.clone().with_prompt("test");

        // Original should be unchanged (due to clone)
        assert!(base.prompt.is_none());
        assert_eq!(with_prompt.prompt, Some("test".to_string()));
    }

    #[test]
    fn test_config_debug_output_parseable() {
        let config = InferenceConfig::new("/model.gguf")
            .with_prompt("test")
            .with_max_tokens(100);

        let debug = format!("{:?}", config);

        // Should contain struct name and key field names
        assert!(debug.contains("InferenceConfig"));
        assert!(debug.contains("model_path"));
        assert!(debug.contains("prompt"));
        assert!(debug.contains("max_tokens"));
    }

    // =========================================================================
    // Error Type Coverage
    // =========================================================================

    #[test]
    fn test_error_type_io_error() {
        use crate::error::RealizarError;

        let err = RealizarError::IoError {
            message: "Custom IO error message".to_string(),
        };
        let err_string = err.to_string();
        assert!(err_string.contains("Custom IO error message"));
    }

    #[test]
    fn test_error_type_format_error() {
        use crate::error::RealizarError;

        let err = RealizarError::FormatError {
            reason: "Invalid magic bytes detected".to_string(),
        };
        let err_string = err.to_string();
        assert!(err_string.contains("Invalid magic bytes"));
    }

    #[test]
    fn test_error_type_inference_error() {
        use crate::error::RealizarError;

        let err = RealizarError::InferenceError("Generation loop failed".to_string());
        let err_string = err.to_string();
        assert!(err_string.contains("Generation loop failed"));
    }

    // =========================================================================
    // Generated Token Slice Edge Cases
    // =========================================================================

    #[test]
    fn test_generated_tokens_single_input() {
        let all_tokens = vec![1u32, 100, 200, 300];
        let input_count = 1;
        let generated = &all_tokens[input_count..];
        assert_eq!(generated, &[100, 200, 300]);
    }

    #[test]
    fn test_generated_tokens_equal_counts() {
        let all_tokens = vec![1u32, 2, 3, 4, 5, 6];
        let input_count = 3;
        let generated = &all_tokens[input_count..];
        assert_eq!(generated.len(), 3);
        assert_eq!(input_count, generated.len());
    }

    // =========================================================================
    // Model Loading Path Edge Cases
    // =========================================================================

    #[test]
    fn test_model_path_root_directory() {
        let config = InferenceConfig::new("/model.gguf");
        assert!(config.model_path.starts_with("/"));
    }

    #[test]
    fn test_model_path_current_directory() {
        let config = InferenceConfig::new("./model.gguf");
        assert!(config.model_path.starts_with("./"));
    }

    #[test]
    fn test_model_path_parent_directory() {
        let config = InferenceConfig::new("../models/model.gguf");
        assert!(config.model_path.starts_with("../"));
    }
}
