use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_maimed_pygmy_gguf_real_inference_path() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let data = build_executable_pygmy_gguf();
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
temp.write_all(&data).expect("write pygmy data");
temp.flush().expect("flush");
let config = InferenceConfig::new(temp.path())
.with_prompt("test")
.with_max_tokens(2);
let result = run_inference(&config);
match result {
Ok(r) => {
assert_eq!(r.format, "GGUF");
assert!(r.generated_token_count <= 2);
},
Err(e) => {
let err_str = e.to_string();
assert!(
!err_str.contains("mock"),
"Should not use mock path: {}",
err_str
);
},
}
}
#[test]
fn test_maimed_pygmy_gguf_verbose_path() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let data = build_executable_pygmy_gguf();
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
temp.write_all(&data).expect("write");
temp.flush().expect("flush");
let config = InferenceConfig::new(temp.path())
.with_prompt("hello")
.with_max_tokens(1)
.with_verbose(true);
let _ = run_inference(&config);
}
#[test]
fn test_maimed_pygmy_gguf_input_tokens_path() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let data = build_executable_pygmy_gguf();
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
temp.write_all(&data).expect("write");
temp.flush().expect("flush");
let config = InferenceConfig::new(temp.path())
.with_input_tokens(vec![1, 2, 3])
.with_max_tokens(2);
let result = run_inference(&config);
if let Ok(r) = result {
assert_eq!(r.format, "GGUF");
assert_eq!(r.input_token_count, 3);
}
}
#[test]
fn test_maimed_pygmy_gguf_no_gpu_path() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let data = build_executable_pygmy_gguf();
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
temp.write_all(&data).expect("write");
temp.flush().expect("flush");
let config = InferenceConfig::new(temp.path())
.with_prompt("test")
.with_max_tokens(1)
.without_gpu();
let result = run_inference(&config);
if let Ok(r) = result {
assert!(!r.used_gpu, "Should not use GPU when disabled");
}
}
#[test]
fn test_maimed_pygmy_corrupt_magic() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let mut data = build_executable_pygmy_gguf();
data[0] = 0xDE;
data[1] = 0xAD;
data[2] = 0xBE;
data[3] = 0xEF;
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
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);
assert!(result.is_err(), "Should fail with corrupt magic");
let err_str = format!("{:?}", result.unwrap_err());
assert!(
err_str.contains("magic") || err_str.contains("format") || err_str.contains("Format"),
"Error should mention magic or format: {}",
err_str
);
}
#[test]
fn test_maimed_pygmy_corrupt_version() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let mut data = build_executable_pygmy_gguf();
data[4] = 99;
data[5] = 0;
data[6] = 0;
data[7] = 0;
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
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);
assert!(result.is_err(), "Should fail with unsupported version");
let err_str = format!("{:?}", result.unwrap_err());
assert!(
err_str.contains("version")
|| err_str.contains("Version")
|| err_str.contains("Unsupported"),
"Error should mention version: {}",
err_str
);
}
#[test]
fn test_maimed_pygmy_truncated_header() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let data = build_executable_pygmy_gguf();
let truncated = &data[..12];
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
temp.write_all(truncated).expect("write");
temp.flush().expect("flush");
let config = InferenceConfig::new(temp.path())
.with_prompt("test")
.with_max_tokens(1);
let result = run_inference(&config);
assert!(result.is_err(), "Should fail with truncated header");
}
#[test]
fn test_maimed_pygmy_corrupt_tensor_count() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let mut data = build_executable_pygmy_gguf();
let zero_count: u64 = 0;
data[8..16].copy_from_slice(&zero_count.to_le_bytes());
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
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);
assert!(result.is_err(), "Should fail with zero tensor count");
}
#[test]
fn test_maimed_pygmy_truncated_tensor_data() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let data = build_executable_pygmy_gguf();
let truncated = &data[..data.len() * 60 / 100];
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
temp.write_all(truncated).expect("write");
temp.flush().expect("flush");
let config = InferenceConfig::new(temp.path())
.with_prompt("test")
.with_max_tokens(1);
let result = run_inference(&config);
assert!(result.is_err(), "Should fail with truncated tensor data");
}
#[test]
fn test_maimed_pygmy_apr_invalid_magic() {
let mut data = Vec::new();
data.extend_from_slice(b"NOPE");
data.extend_from_slice(&[0u8; 100]);
let mut temp = NamedTempFile::with_suffix(".apr").expect("create temp file");
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);
assert!(result.is_err(), "Should fail with invalid APR magic");
}
#[test]
fn test_maimed_pygmy_apr_truncated_metadata() {
let mut data = Vec::new();
data.extend_from_slice(b"APR\x02");
let mut temp = NamedTempFile::with_suffix(".apr").expect("create temp file");
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);
assert!(result.is_err(), "Should fail with truncated APR");
}
#[test]
fn test_maimed_pygmy_safetensors_header_overflow() {
let mut data = Vec::new();
let huge_header: u64 = 1024 * 1024 * 1024;
data.extend_from_slice(&huge_header.to_le_bytes());
data.extend_from_slice(b"{}");
let mut temp = NamedTempFile::with_suffix(".safetensors").expect("create temp file");
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);
assert!(result.is_err(), "Should fail with header overflow");
}
#[test]
fn test_maimed_pygmy_safetensors_invalid_json() {
let mut data = Vec::new();
let invalid_json = b"{ not valid json {{{{";
let header_size = invalid_json.len() as u64;
data.extend_from_slice(&header_size.to_le_bytes());
data.extend_from_slice(invalid_json);
let mut temp = NamedTempFile::with_suffix(".safetensors").expect("create temp file");
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);
assert!(result.is_err(), "Should fail with invalid JSON");
}
#[test]
fn test_inference_config_temperature_edges() {
let config = InferenceConfig::new("model.gguf")
.with_prompt("test")
.with_temperature(0.0);
assert_eq!(config.temperature, 0.0);
let config = InferenceConfig::new("model.gguf")
.with_prompt("test")
.with_temperature(100.0);
assert_eq!(config.temperature, 100.0);
}
#[test]
fn test_inference_config_top_k_edges() {
let config = InferenceConfig::new("model.gguf")
.with_prompt("test")
.with_top_k(0);
assert_eq!(config.top_k, 0);
let config = InferenceConfig::new("model.gguf")
.with_prompt("test")
.with_top_k(1);
assert_eq!(config.top_k, 1);
let config = InferenceConfig::new("model.gguf")
.with_prompt("test")
.with_top_k(100000);
assert_eq!(config.top_k, 100000);
}
#[test]
fn test_inference_config_full_chain() {
let config = InferenceConfig::new("model.gguf")
.with_prompt("Hello world")
.with_max_tokens(50)
.with_temperature(0.7)
.with_top_k(40)
.with_verbose(true)
.with_trace(true)
.with_trace_output("/tmp/trace.json")
.without_gpu();
assert_eq!(config.prompt, Some("Hello world".to_string()));
assert_eq!(config.max_tokens, 50);
assert_eq!(config.temperature, 0.7);
assert_eq!(config.top_k, 40);
assert!(config.verbose);
assert!(config.trace);
assert!(config.no_gpu);
assert!(config.trace_output.is_some());
}
#[test]
fn test_inference_config_no_input() {
use crate::gguf::test_factory::build_executable_pygmy_gguf;
let data = build_executable_pygmy_gguf();
let mut temp = NamedTempFile::with_suffix(".gguf").expect("create temp file");
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);
if let Ok(r) = result {
assert!(r.input_token_count >= 1);
}
}