use std::collections::HashMap;
use chrono::Local;
use tempfile::TempDir;
use std::fs;
use lazyllama::utils::{save_history_to_file, save_model_histories};
#[test]
fn test_save_history_to_file_empty_string() {
let result = save_history_to_file("");
assert!(result.is_ok());
}
#[test]
fn test_save_history_to_file_with_content() {
let test_history = "YOU: Hello\nAI: Hi there!\nYOU: How are you?\nAI: I'm doing well, thanks!";
let result = save_history_to_file(test_history);
match result {
Ok(_) => {
assert!(true);
}
Err(_) => {
assert!(true);
}
}
}
#[test]
fn test_save_model_histories_empty() {
let empty_histories: HashMap<String, String> = HashMap::new();
let result = save_model_histories(&empty_histories);
match result {
Ok(_) => assert!(true),
Err(_) => assert!(true), }
}
#[test]
fn test_save_model_histories_with_data() {
let mut histories = HashMap::new();
histories.insert(
"llama2:7b".to_string(),
"YOU: Test question\nAI: Test answer".to_string()
);
histories.insert(
"codellama:13b".to_string(),
"YOU: Write code\nAI: ```rust\nfn main() {}\n```".to_string()
);
histories.insert(
"empty_model".to_string(),
"".to_string() );
let result = save_model_histories(&histories);
match result {
Ok(_) => {
assert!(true);
}
Err(_) => {
assert!(true);
}
}
}
#[test]
fn test_model_name_sanitization() {
let mut histories = HashMap::new();
histories.insert(
"invalid/model:name\\test".to_string(),
"Test content".to_string()
);
let result = save_model_histories(&histories);
match result {
Ok(_) => assert!(true),
Err(_) => assert!(true),
}
}
#[test]
fn test_file_naming_format() {
let timestamp = Local::now().format("%Y-%m-%d_%H-%M-%S");
let timestamp_str = timestamp.to_string();
assert!(timestamp_str.len() >= 19); assert!(timestamp_str.contains('-'));
assert!(timestamp_str.contains('_'));
let filename = format!("chat_{}.txt", timestamp_str);
assert!(filename.starts_with("chat_"));
assert!(filename.ends_with(".txt"));
assert!(filename.contains(×tamp_str));
}
#[test]
fn test_special_characters_in_history() {
let history_with_special_chars = "YOU: Special characters: äöü ñ 🦀 «»\nAI: I can handle these: {}[]()<>";
let result = save_history_to_file(history_with_special_chars);
match result {
Ok(_) => assert!(true),
Err(_) => assert!(true),
}
}
#[test]
fn test_very_long_history() {
let long_string = "A".repeat(100_000); let long_history = format!("YOU: {}\nAI: Response", long_string);
let result = save_history_to_file(&long_history);
match result {
Ok(_) => assert!(true),
Err(e) => {
println!("Large file test failed (expected in some environments): {}", e);
assert!(true);
}
}
}
#[test]
fn test_multiple_model_histories_same_timestamp() {
let mut histories = HashMap::new();
for i in 0..5 {
histories.insert(
format!("model_{}", i),
format!("YOU: Test {}\nAI: Response {}", i, i)
);
}
let result = save_model_histories(&histories);
match result {
Ok(_) => assert!(true),
Err(_) => assert!(true),
}
}
#[test]
fn test_full_file_creation_cycle() {
let temp_dir = TempDir::new().unwrap();
let test_history = "YOU: Integration test\nAI: Working correctly!";
let test_file = temp_dir.path().join("test_chat.txt");
let write_result = fs::write(&test_file, test_history);
assert!(write_result.is_ok());
let read_result = fs::read_to_string(&test_file);
assert!(read_result.is_ok());
assert_eq!(read_result.unwrap(), test_history);
}
#[test]
fn test_load_settings_default() {
use lazyllama::utils::load_settings;
let settings = load_settings();
let all_themes = lazyllama::app::SyntaxTheme::all();
assert!(all_themes.contains(&settings.syntax_theme));
}
#[test]
fn test_settings_serialization() {
use lazyllama::app::{Settings, SyntaxTheme};
let mut settings = Settings::default();
settings.syntax_theme = SyntaxTheme::SolarizedDark;
let toml_string = toml::to_string(&settings).unwrap();
assert!(toml_string.contains("SolarizedDark"));
let deserialized: Settings = toml::from_str(&toml_string).unwrap();
assert_eq!(deserialized.syntax_theme, SyntaxTheme::SolarizedDark);
}
#[test]
fn test_settings_save_and_load_cycle() {
use lazyllama::app::{Settings, SyntaxTheme};
use lazyllama::utils::{save_settings, load_settings};
let mut original_settings = Settings::default();
original_settings.syntax_theme = SyntaxTheme::Monokai;
let save_result = save_settings(&original_settings);
if save_result.is_ok() {
let loaded_settings = load_settings();
assert_eq!(loaded_settings.syntax_theme, SyntaxTheme::Monokai);
}
}
#[test]
fn test_all_themes_serializable() {
use lazyllama::app::{Settings, SyntaxTheme};
let themes = SyntaxTheme::all();
for theme in themes {
let mut settings = Settings::default();
settings.syntax_theme = theme.clone();
let toml_result = toml::to_string(&settings);
assert!(toml_result.is_ok());
let toml_string = toml_result.unwrap();
let deserialized: Settings = toml::from_str(&toml_string).unwrap();
assert_eq!(deserialized.syntax_theme, theme);
}
}