#[cfg(test)]
mod tests {
use crate::embedding::*;
#[test]
fn test_count_tokens() {
let text = "Hello, world!";
let count = count_tokens(text);
assert!(count > 0);
assert!(count < 10); }
#[test]
fn test_split_texts_into_batches() {
let texts = vec![
"Short text".to_string(),
"Another short text".to_string(),
"Yet another text".to_string(),
];
let batches = split_texts_into_token_limited_batches(texts, 2, 1000);
assert_eq!(batches.len(), 2);
assert_eq!(batches[0].len(), 2);
assert_eq!(batches[1].len(), 1);
}
#[test]
fn test_truncate_output() {
let long_text = "This is a test. ".repeat(100);
let truncated = truncate_output(&long_text, 50);
assert!(truncated.contains("[Output truncated"));
assert!(truncated.len() < long_text.len());
}
#[test]
fn test_truncate_output_no_limit() {
let text = "This is a test.";
let result = truncate_output(text, 0);
assert_eq!(result, text);
}
#[tokio::test]
async fn test_basic_embedding_generation() {
let result = generate_embeddings("Hello, world!", "jina", "jina-embeddings-v4").await;
match result {
Ok(embeddings) => {
assert!(!embeddings.is_empty());
println!("Generated embedding with {} dimensions", embeddings.len());
}
Err(e) => {
println!(
"Embedding generation failed (expected without API key): {}",
e
);
}
}
}
#[tokio::test]
async fn test_batch_embedding_generation() {
let texts = vec!["First document".to_string(), "Second document".to_string()];
let result = generate_embeddings_batch(
texts,
"jina",
"jina-embeddings-v4",
InputType::Document,
16,
100_000,
)
.await;
match result {
Ok(embeddings) => {
assert_eq!(embeddings.len(), 2);
println!("Generated {} embeddings", embeddings.len());
}
Err(e) => {
println!(
"Batch embedding generation failed (expected without API key): {}",
e
);
}
}
}
}