#[test]
fn test_clean_model_output_im_start_im_end_ext_cov() {
let raw = "<|im_start|>assistant\nHi there!<|im_end|>";
let cleaned = clean_model_output(raw);
assert_eq!(cleaned, "Hi there!");
}
#[test]
fn test_clean_model_output_empty_ext_cov() {
let raw = "";
let cleaned = clean_model_output(raw);
assert_eq!(cleaned, "");
}
#[test]
fn test_clean_model_output_only_markers_ext_cov() {
let raw = "<|im_start|><|im_end|><|endoftext|>";
let cleaned = clean_model_output(raw);
assert_eq!(cleaned, "");
}
#[test]
fn test_clean_model_output_multiple_endoftext_ext_cov() {
let raw = "Text<|endoftext|>More text<|endoftext|>";
let cleaned = clean_model_output(raw);
assert_eq!(cleaned, "TextMore text");
}
#[test]
fn test_prefault_mmap_large_ext_cov() {
let data = vec![0u8; 4096 * 10]; prefault_mmap(&data);
}
#[test]
fn test_prefault_mmap_not_page_aligned_ext_cov() {
let data = vec![0u8; 5000]; prefault_mmap(&data);
}
#[test]
fn test_inference_result_debug_ext_cov() {
let result = InferenceResult {
text: "test".to_string(),
tokens: vec![1],
input_token_count: 1,
generated_token_count: 0,
inference_ms: 5.0,
tok_per_sec: 0.0,
load_ms: 2.0,
format: "APR".to_string(),
used_gpu: false,
};
let debug_str = format!("{:?}", result);
assert!(debug_str.contains("text"));
assert!(debug_str.contains("tokens"));
assert!(debug_str.contains("format"));
assert!(debug_str.contains("used_gpu"));
}
#[test]
fn test_inference_result_zero_values_ext_cov() {
let result = InferenceResult {
text: String::new(),
tokens: vec![],
input_token_count: 0,
generated_token_count: 0,
inference_ms: 0.0,
tok_per_sec: 0.0,
load_ms: 0.0,
format: String::new(),
used_gpu: false,
};
assert!(result.text.is_empty());
assert!(result.tokens.is_empty());
assert_eq!(result.input_token_count, 0);
}
#[test]
fn test_inference_result_large_values_ext_cov() {
let result = InferenceResult {
text: "A".repeat(10000),
tokens: vec![1; 1000],
input_token_count: 100,
generated_token_count: 900,
inference_ms: 1000000.0,
tok_per_sec: 1000.0,
load_ms: 5000.0,
format: "GGUF".to_string(),
used_gpu: true,
};
assert_eq!(result.text.len(), 10000);
assert_eq!(result.tokens.len(), 1000);
assert_eq!(result.generated_token_count, 900);
}
#[test]
fn test_inference_result_formats_ext_cov() {
for fmt 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: fmt.to_string(),
used_gpu: false,
};
assert_eq!(result.format, fmt);
}
}
#[test]
fn test_run_inference_permission_denied_ext_cov() {
let config = InferenceConfig::new("/root/super_secret/model.gguf");
let result = run_inference(&config);
assert!(result.is_err());
}
#[test]
fn test_run_inference_empty_path_ext_cov() {
let config = InferenceConfig::new("");
let result = run_inference(&config);
assert!(result.is_err());
}
#[test]
fn test_format_detection_gguf_magic_deep_icov() {
use crate::format::{detect_format, ModelFormat};
let data = vec![0x47, 0x47, 0x55, 0x46, 0x03, 0x00, 0x00, 0x00];
let format = detect_format(&data);
assert!(matches!(format, Ok(ModelFormat::Gguf)));
}
#[test]
fn test_format_detection_apr_magic_deep_icov() {
use crate::format::{detect_format, ModelFormat};
let data = b"APR\0xxxx";
let format = detect_format(data);
assert!(matches!(format, Ok(ModelFormat::Apr)));
}
#[test]
fn test_format_detection_safetensors_deep_icov() {
use crate::format::{detect_format, ModelFormat};
let header_size: u64 = 2048;
let data = header_size.to_le_bytes();
let format = detect_format(&data);
assert!(matches!(format, Ok(ModelFormat::SafeTensors)));
}
#[test]
fn test_format_detection_unknown_magic_deep_icov() {
use crate::format::{detect_format, FormatError};
let data = b"\x00\x00\x00\x00\x00\x00\x00\x00"; let format = detect_format(data);
assert!(matches!(format, Err(FormatError::UnknownFormat)));
}
fn detect_arch_from_stem(stem: &str) -> &'static str {
let lower = stem.to_lowercase();
[("qwen", "Qwen2"), ("llama", "LLaMA"), ("mistral", "Mistral"), ("phi", "Phi")]
.iter()
.find(|(k, _)| lower.contains(k))
.map_or("Transformer", |(_, v)| v)
}
#[test]
fn test_architecture_detection_qwen_deep_icov() {
let path = PathBuf::from("/models/qwen2-7b-instruct-q4.gguf");
let arch = path.file_stem().and_then(|s| s.to_str()).map(detect_arch_from_stem);
assert_eq!(arch, Some("Qwen2"));
}
#[test]
fn test_architecture_detection_llama_deep_icov() {
let path = PathBuf::from("/models/llama-3.1-8b-instruct.gguf");
let arch = path.file_stem().and_then(|s| s.to_str()).map(detect_arch_from_stem);
assert_eq!(arch, Some("LLaMA"));
}
#[test]
fn test_architecture_detection_mistral_deep_icov() {
let path = PathBuf::from("/models/mistral-7b-v0.2-q4_k_m.gguf");
let arch = path.file_stem().and_then(|s| s.to_str()).map(detect_arch_from_stem);
assert_eq!(arch, Some("Mistral"));
}
#[test]
fn test_architecture_detection_phi_deep_icov() {
let path = PathBuf::from("/models/phi-2-q4_0.gguf");
let arch = path.file_stem().and_then(|s| s.to_str()).map(detect_arch_from_stem);
assert_eq!(arch, Some("Phi"));
}
#[test]
fn test_architecture_detection_transformer_fallback_deep_icov() {
let path = PathBuf::from("/models/custom-model-q8_0.gguf");
let arch = path.file_stem().and_then(|s| s.to_str()).map(detect_arch_from_stem);
assert_eq!(arch, Some("Transformer"));
}
#[test]
fn test_architecture_detection_case_insensitive_deep_icov() {
let cases = [
("/models/QWEN2-7B.gguf", "Qwen2"),
("/models/LLAMA-3.gguf", "LLaMA"),
("/models/MISTRAL-7B.gguf", "Mistral"),
("/models/PHI-2.gguf", "Phi"),
("/models/QwEn2-MixedCase.gguf", "Qwen2"),
];
for (path_str, expected) in cases {
let path = PathBuf::from(path_str);
let arch = path.file_stem().and_then(|s| s.to_str()).map(detect_arch_from_stem);
assert_eq!(arch, Some(expected), "Failed for path: {path_str}");
}
}
#[test]
fn test_architecture_detection_no_extension_deep_icov() {
let path = PathBuf::from("/models/qwen2-model");
let arch = path.file_stem().and_then(|s| s.to_str()).map(detect_arch_from_stem);
assert_eq!(arch, Some("Qwen2"));
}
#[test]
fn test_instruct_model_detection_deep_icov() {
let model_name = "llama-3.1-8b-instruct.gguf";
let is_instruct = model_name.to_lowercase().contains("instruct");
assert!(is_instruct);
}
#[test]
fn test_instruct_model_detection_uppercase_deep_icov() {
let model_name = "LLAMA-3.1-8B-INSTRUCT.gguf";
let is_instruct = model_name.to_lowercase().contains("instruct");
assert!(is_instruct);
}
#[test]
fn test_instruct_model_detection_mixed_case_deep_icov() {
let model_name = "Qwen2-7B-Instruct-Q4_K_M.gguf";
let is_instruct = model_name.to_lowercase().contains("instruct");
assert!(is_instruct);
}
#[test]
fn test_instruct_model_detection_not_instruct_deep_icov() {
let model_name = "llama-3.1-8b-base.gguf";
let is_instruct = model_name.to_lowercase().contains("instruct");
assert!(!is_instruct);
}
#[test]
fn test_instruct_model_detection_partial_match_deep_icov() {
let model_name = "model-instructed.gguf";
let is_instruct = model_name.to_lowercase().contains("instruct");
assert!(is_instruct);
}
#[test]
fn test_chat_message_user_creation_deep_icov() {
use crate::chat_template::ChatMessage;
let msg = ChatMessage::user("Hello, world!");
assert_eq!(msg.role, "user");
assert_eq!(msg.content, "Hello, world!");
}
#[test]
fn test_chat_message_system_creation_deep_icov() {
use crate::chat_template::ChatMessage;
let msg = ChatMessage::system("You are a helpful assistant.");
assert_eq!(msg.role, "system");
assert_eq!(msg.content, "You are a helpful assistant.");
}
#[test]
fn test_format_messages_instruct_model_deep_icov() {
use crate::chat_template::{format_messages, ChatMessage};
let messages = vec![ChatMessage::user("What is 2+2?")];
let result = format_messages(&messages, Some("qwen2-7b-instruct.gguf"));
assert!(result.is_ok());
let formatted = result.expect("operation failed");
assert!(formatted.contains("<|im_start|>") || formatted.contains("user"));
}
#[test]
fn test_format_messages_llama_template_deep_icov() {
use crate::chat_template::{format_messages, ChatMessage};
let messages = vec![ChatMessage::user("Hello!")];
let result = format_messages(&messages, Some("llama-3.1-8b-instruct.gguf"));
assert!(result.is_ok());
let formatted = result.expect("operation failed");
assert!(formatted.contains("[INST]") || formatted.contains("user"));
}
#[test]
fn test_format_messages_fallback_raw_deep_icov() {
use crate::chat_template::{format_messages, ChatMessage};
let messages = vec![ChatMessage::user("Just text")];
let result = format_messages(&messages, Some("unknown-model.gguf"));
assert!(result.is_ok());
}
#[test]
fn test_input_tokens_priority_over_prompt_deep_icov() {
let config = InferenceConfig::new("/model.gguf")
.with_prompt("Hello")
.with_input_tokens(vec![1, 2, 3, 4]);
assert!(config.input_tokens.is_some());
assert!(config.prompt.is_some());
let input_tokens = if let Some(ref tokens) = config.input_tokens {
tokens.clone()
} else if let Some(ref _prompt) = config.prompt {
vec![100, 200] } else {
vec![1u32] };
assert_eq!(input_tokens, vec![1, 2, 3, 4]);
}
#[test]
fn test_input_tokens_none_uses_prompt_deep_icov() {
let config = InferenceConfig::new("/model.gguf").with_prompt("Hello");
assert!(config.input_tokens.is_none());
assert!(config.prompt.is_some());
}
#[test]
fn test_input_tokens_none_prompt_none_uses_bos_deep_icov() {
let config = InferenceConfig::new("/model.gguf");
assert!(config.input_tokens.is_none());
assert!(config.prompt.is_none());
let input_tokens = if let Some(ref tokens) = config.input_tokens {
tokens.clone()
} else if let Some(ref _prompt) = config.prompt {
vec![100, 200]
} else {
vec![1u32] };
assert_eq!(input_tokens, vec![1u32]);
}
#[test]
fn test_verbose_flag_enabled_deep_icov() {
let config = InferenceConfig::new("/model.gguf").with_verbose(true);
assert!(config.verbose);
}
#[test]
fn test_verbose_flag_disabled_deep_icov() {
let config = InferenceConfig::new("/model.gguf").with_verbose(false);
assert!(!config.verbose);
}
#[test]
fn test_verbose_default_is_false_deep_icov() {
let config = InferenceConfig::new("/model.gguf");
assert!(!config.verbose);
}
#[test]
fn test_model_name_extraction_from_path_deep_icov() {
let path = PathBuf::from("/models/qwen2-7b-instruct.gguf");
let model_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
assert_eq!(model_name, "qwen2-7b-instruct.gguf");
}
#[test]
fn test_model_name_extraction_no_parent_deep_icov() {
let path = PathBuf::from("qwen2-7b.gguf");
let model_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
assert_eq!(model_name, "qwen2-7b.gguf");
}
#[test]
fn test_model_name_extraction_empty_path_deep_icov() {
let path = PathBuf::from("");
let model_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
assert_eq!(model_name, "");
}
#[test]
fn test_file_stem_extraction_deep_icov() {
let path = PathBuf::from("/models/llama-3.1-8b.gguf");
let stem = path.file_stem().and_then(|s| s.to_str());
assert_eq!(stem, Some("llama-3.1-8b"));
}
#[test]
fn test_gh278_base_model_no_chat_template_wrapping() {
use crate::gguf::test_factory::GGUFBuilder;
let gguf_bytes = GGUFBuilder::new()
.architecture("llama")
.hidden_dim("llama", 64)
.num_layers("llama", 1)
.num_heads("llama", 2)
.num_kv_heads("llama", 2)
.context_length("llama", 128)
.vocab_size("llama", 5)
.add_u32("tokenizer.ggml.bos_token_id", 1)
.add_u32("tokenizer.ggml.eos_token_id", 2)
.build();
let dir = tempfile::tempdir().expect("create temp dir");
let gguf_path = dir.path().join("smollm-base.gguf");
std::fs::write(&gguf_path, &gguf_bytes).expect("write test gguf");
let config = InferenceConfig::new(&gguf_path)
.with_prompt("Hello");
let result = prepare_tokens(&config, &ModelFormat::Gguf);
assert!(result.is_err(), "should fail on tokenization (no vocab), not on template");
let err_msg = format!("{:?}", result.expect_err("expected error"));
assert!(
err_msg.contains("Tokenizer encode failed"),
"error should be about encoding, not template: {err_msg}"
);
}
#[test]
fn test_gh278_instruct_model_with_chat_template_applies_wrapping() {
use crate::gguf::test_factory::GGUFBuilder;
use crate::gguf::{GGUFValue, MappedGGUFModel};
let gguf_bytes = GGUFBuilder::new()
.architecture("llama")
.hidden_dim("llama", 64)
.num_layers("llama", 1)
.num_heads("llama", 2)
.num_kv_heads("llama", 2)
.context_length("llama", 128)
.vocab_size("llama", 5)
.add_string("tokenizer.chat_template", "{% for m in messages %}{{ m.content }}{% endfor %}")
.build();
let dir = tempfile::tempdir().expect("create temp dir");
let gguf_path = dir.path().join("llama-instruct.gguf");
std::fs::write(&gguf_path, &gguf_bytes).expect("write test gguf");
let mapped = MappedGGUFModel::from_path(&gguf_path).expect("load gguf");
let has_template = mapped
.model
.metadata
.get("tokenizer.chat_template")
.is_some_and(|v| matches!(v, GGUFValue::String(s) if !s.is_empty()));
assert!(has_template, "test GGUF must have chat_template in metadata");
}
#[test]
fn test_gh278_bos_token_default_by_model_type() {
assert_ne!("llama", "gpt2", "llama model type should add BOS");
assert_eq!("gpt2", "gpt2", "gpt2 model type should NOT add BOS");
}