use intent_classifier::*;
use chrono;
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_empty_inputs() {
let classifier = IntentClassifier::new().await.unwrap();
let result = classifier.predict_intent("").await;
assert!(result.is_ok());
let result = classifier.predict_intent(" \t\n ").await;
assert!(result.is_ok());
let result = classifier.predict_intent("a").await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_very_long_inputs() {
let classifier = IntentClassifier::new().await.unwrap();
let long_input = "word ".repeat(1000);
let result = classifier.predict_intent(&long_input).await;
assert!(result.is_ok());
let long_word = "a".repeat(10000);
let result = classifier.predict_intent(&long_word).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_special_characters() {
let classifier = IntentClassifier::new().await.unwrap();
let special_inputs = vec![
"¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿",
"αβγδεζηθικλμνξοπρστυφχψω",
"你好世界",
"🚀🌟💫⭐️✨🎯",
"\\n\\t\\r\\0",
"\"quoted text\"",
"'single quotes'",
"`backticks`",
"line1\nline2\nline3",
"tab\tseparated\tvalues",
];
for input in special_inputs {
let result = classifier.predict_intent(input).await;
assert!(result.is_ok(), "Failed on input: {}", input);
}
}
#[tokio::test]
async fn test_edge_case_training_examples() {
let classifier = IntentClassifier::new().await.unwrap();
let example = TrainingExample {
intent: IntentId("".to_string()),
text: "some text".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
let _result = classifier.add_training_example(example).await;
let long_intent = "a".repeat(1000);
let example = TrainingExample {
intent: IntentId(long_intent),
text: "some text".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
let result = classifier.add_training_example(example).await;
assert!(result.is_ok());
let example = TrainingExample {
intent: IntentId("test_intent".to_string()),
text: "".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
let result = classifier.add_training_example(example).await;
assert!(result.is_err());
let example = TrainingExample {
intent: IntentId("test_intent".to_string()),
text: "test text".to_string(),
confidence: 0.1,
source: TrainingSource::UserFeedback,
};
let result = classifier.add_training_example(example).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_extreme_confidence_values() {
let classifier = IntentClassifier::new().await.unwrap();
let example = TrainingExample {
intent: IntentId("test_intent".to_string()),
text: "test example".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
classifier.add_training_example(example).await.unwrap();
let result = classifier.predict_intent("test example").await.unwrap();
assert!(result.confidence.value() > 0.5);
let result = classifier.predict_intent("completely different unrelated text").await.unwrap();
assert!(result.confidence.value() >= 0.0);
assert!(result.confidence.value() <= 1.0);
}
#[tokio::test]
async fn test_massive_vocabulary() {
let classifier = IntentClassifier::new().await.unwrap();
for i in 0..100 {
let example = TrainingExample {
intent: IntentId(format!("intent_{}", i % 10)),
text: format!("unique_word_{} action_{} object_{}", i, i*2, i*3),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
classifier.add_training_example(example).await.unwrap();
}
let result = classifier.predict_intent("unique_word_50 action_100 object_150").await;
assert!(result.is_ok());
let stats = classifier.get_stats().await;
assert!(stats.training_examples >= 100);
}
#[tokio::test]
async fn test_concurrent_operations() {
let classifier = IntentClassifier::new().await.unwrap();
let example = TrainingExample {
intent: IntentId("test_intent".to_string()),
text: "test example".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
classifier.add_training_example(example).await.unwrap();
let mut handles = vec![];
for i in 0..10 {
let classifier_clone = classifier.clone();
let handle = tokio::spawn(async move {
let result = classifier_clone.predict_intent(&format!("test query {}", i)).await;
assert!(result.is_ok());
});
handles.push(handle);
}
for handle in handles {
handle.await.unwrap();
}
}
#[tokio::test]
async fn test_malformed_json_import() {
let classifier = IntentClassifier::new().await.unwrap();
let malformed_json_cases = vec![
"",
"not json",
"{}",
"[]",
"{\"invalid\": \"structure\"}",
"null",
"123",
"\"just a string\"",
];
for malformed_json in malformed_json_cases {
let result = classifier.import_training_data(malformed_json).await;
match result {
Ok(_) => {
}
Err(_) => {
}
}
}
}
#[tokio::test]
async fn test_feedback_with_edge_cases() {
let classifier = IntentClassifier::new().await.unwrap();
let example = TrainingExample {
intent: IntentId("test_intent".to_string()),
text: "test example".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
classifier.add_training_example(example).await.unwrap();
let feedback = IntentFeedback {
text: "test text".to_string(),
predicted_intent: IntentId("some_intent".to_string()),
actual_intent: IntentId("".to_string()),
satisfaction_score: 3.0,
notes: None,
timestamp: chrono::Utc::now(),
};
let result = classifier.add_feedback(feedback).await;
assert!(result.is_ok());
let long_text = "word ".repeat(500);
let feedback = IntentFeedback {
text: long_text,
predicted_intent: IntentId("some_intent".to_string()),
actual_intent: IntentId("test_intent".to_string()),
satisfaction_score: 4.0,
notes: None,
timestamp: chrono::Utc::now(),
};
let result = classifier.add_feedback(feedback).await;
assert!(result.is_ok());
let feedback = IntentFeedback {
text: "🚀 test 你好".to_string(),
predicted_intent: IntentId("some_intent".to_string()),
actual_intent: IntentId("test_intent".to_string()),
satisfaction_score: 5.0,
notes: Some("Great work!".to_string()),
timestamp: chrono::Utc::now(),
};
let result = classifier.add_feedback(feedback).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_statistics_edge_cases() {
let classifier = IntentClassifier::new().await.unwrap();
let initial_stats = classifier.get_stats().await;
let initial_count = initial_stats.training_examples;
classifier.clear_training_data().await.unwrap();
let stats = classifier.get_stats().await;
assert!(stats.training_examples <= initial_count);
let example = TrainingExample {
intent: IntentId("test_intent".to_string()),
text: "test example".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
classifier.add_training_example(example).await.unwrap();
let stats = classifier.get_stats().await;
assert!(stats.training_examples > 0);
assert!(stats.intent_count >= 1);
assert!(stats.vocabulary_size >= 1);
let example = TrainingExample {
intent: IntentId("test_intent".to_string()),
text: "test example".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
classifier.add_training_example(example).await.unwrap();
let new_stats = classifier.get_stats().await;
assert!(new_stats.training_examples > stats.training_examples);
assert_eq!(new_stats.intent_count, stats.intent_count);
}
#[tokio::test]
async fn test_resource_limits() {
let classifier = IntentClassifier::new().await.unwrap();
let config = ClassifierConfig {
feature_dimensions: 10000,
..Default::default()
};
let high_dim_classifier = IntentClassifier::with_config(config).await.unwrap();
let example = TrainingExample {
intent: IntentId("test_intent".to_string()),
text: "test example".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
high_dim_classifier.add_training_example(example).await.unwrap();
let result = high_dim_classifier.predict_intent("test query").await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_configuration_edge_cases() {
let config = ClassifierConfig {
feature_dimensions: 1,
min_confidence_threshold: 0.0,
debug_mode: true,
..Default::default()
};
let classifier = IntentClassifier::with_config(config).await.unwrap();
let example = TrainingExample {
intent: IntentId("test_intent".to_string()),
text: "test example".to_string(),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
classifier.add_training_example(example).await.unwrap();
let result = classifier.predict_intent("test query").await;
assert!(result.is_ok());
let config = ClassifierConfig {
max_vocabulary_size: 100000,
..Default::default()
};
let classifier = IntentClassifier::with_config(config).await.unwrap();
for i in 0..10 {
let example = TrainingExample {
intent: IntentId(format!("intent_{}", i)),
text: format!("example text {}", i),
confidence: 1.0,
source: TrainingSource::UserFeedback,
};
classifier.add_training_example(example).await.unwrap();
}
let result = classifier.predict_intent("example text").await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_unusual_input_patterns() {
let classifier = IntentClassifier::new().await.unwrap();
let unusual_inputs = vec![
"test test test test test",
"a a a a a a a a a a",
"tEsT TeXt",
"UPPER lower MixeD",
"123 456 789",
"test 123 example 456",
"https://example.com/path?query=value",
"/home/user/file.txt",
"C:\\Windows\\System32\\file.exe",
"supercalifragilisticexpialidocious",
"pneumonoultramicroscopicsilicovolcanoconiosis",
"!@#$%^&*()_+-=[]{}|;':\",./<>?",
"test... with... dots...",
"text--with--dashes",
"function(arg1, arg2)",
"if (condition) { return true; }",
"SELECT * FROM table WHERE id = 1",
];
for input in unusual_inputs {
let result = classifier.predict_intent(input).await;
assert!(result.is_ok(), "Failed on input: {}", input);
}
}
}