use crate::messages::{CacheType, MessageRole, UnifiedMessage};
#[test]
fn test_cache_type_default_is_ephemeral() {
let cache_type = CacheType::default();
assert_eq!(cache_type, CacheType::Ephemeral);
}
#[test]
fn test_cache_type_variants_exist() {
let ephemeral = CacheType::Ephemeral;
let extended = CacheType::Extended;
assert_ne!(ephemeral, extended, "Cache types should be distinct");
}
#[test]
fn test_with_ephemeral_cache_sets_cache_type() {
let message = UnifiedMessage::user("Test content");
let cached_message = message.with_ephemeral_cache();
assert_eq!(
cached_message.attributes.cache_type,
Some(CacheType::Ephemeral),
"cache_type should be set to Ephemeral"
);
}
#[test]
fn test_with_ephemeral_cache_sets_cacheable_true() {
let message = UnifiedMessage::user("Test content");
assert!(!message.attributes.cacheable, "Should start non-cacheable");
let cached_message = message.with_ephemeral_cache();
assert!(
cached_message.attributes.cacheable,
"cacheable flag should be set to true"
);
}
#[test]
fn test_with_ephemeral_cache_preserves_message_content() {
let original_content = "Important message content";
let message = UnifiedMessage::user(original_content);
let cached_message = message.with_ephemeral_cache();
match cached_message.content {
crate::messages::MessageContent::Text(ref text) => {
assert_eq!(text, original_content, "Content should be preserved");
}
_ => panic!("Expected text content"),
}
}
#[test]
fn test_with_ephemeral_cache_preserves_role() {
let message = UnifiedMessage::system("System prompt");
let cached_message = message.with_ephemeral_cache();
assert_eq!(
cached_message.role,
MessageRole::System,
"Role should be preserved"
);
}
#[test]
fn test_with_extended_cache_sets_cache_type() {
let message = UnifiedMessage::user("Test content");
let cached_message = message.with_extended_cache();
assert_eq!(
cached_message.attributes.cache_type,
Some(CacheType::Extended),
"cache_type should be set to Extended"
);
}
#[test]
fn test_with_extended_cache_sets_cacheable_true() {
let message = UnifiedMessage::user("Test content");
assert!(!message.attributes.cacheable, "Should start non-cacheable");
let cached_message = message.with_extended_cache();
assert!(
cached_message.attributes.cacheable,
"cacheable flag should be set to true"
);
}
#[test]
fn test_with_extended_cache_preserves_message_content() {
let original_content = "Important message content";
let message = UnifiedMessage::user(original_content);
let cached_message = message.with_extended_cache();
match cached_message.content {
crate::messages::MessageContent::Text(ref text) => {
assert_eq!(text, original_content, "Content should be preserved");
}
_ => panic!("Expected text content"),
}
}
#[test]
fn test_cache_builders_are_distinct() {
let message = UnifiedMessage::user("Test content");
let ephemeral_cached = message.clone().with_ephemeral_cache();
let extended_cached = message.with_extended_cache();
assert_ne!(
ephemeral_cached.attributes.cache_type, extended_cached.attributes.cache_type,
"Different builders should produce different cache types"
);
}
#[test]
fn test_cache_builder_on_system_message() {
let message = UnifiedMessage::system("You are a helpful assistant");
let cached_message = message.with_extended_cache();
assert_eq!(cached_message.role, MessageRole::System);
assert_eq!(
cached_message.attributes.cache_type,
Some(CacheType::Extended)
);
assert!(cached_message.attributes.cacheable);
}
#[test]
fn test_cache_builder_on_assistant_message() {
let message = UnifiedMessage::assistant("Previous response");
let cached_message = message.with_ephemeral_cache();
assert_eq!(cached_message.role, MessageRole::Assistant);
assert_eq!(
cached_message.attributes.cache_type,
Some(CacheType::Ephemeral)
);
assert!(cached_message.attributes.cacheable);
}
#[test]
fn test_overwriting_cache_type() {
let message = UnifiedMessage::user("Test content");
let cached_message = message.with_ephemeral_cache().with_extended_cache();
assert_eq!(
cached_message.attributes.cache_type,
Some(CacheType::Extended),
"Second builder should overwrite first"
);
}
#[test]
fn test_message_role_display_system() {
assert_eq!(format!("{}", MessageRole::System), "system");
}
#[test]
fn test_message_role_display_user() {
assert_eq!(format!("{}", MessageRole::User), "user");
}
#[test]
fn test_message_role_display_assistant() {
assert_eq!(format!("{}", MessageRole::Assistant), "assistant");
}
#[test]
fn test_message_role_display_tool() {
assert_eq!(format!("{}", MessageRole::Tool), "tool");
}
use crate::messages::MessageContent;
#[test]
fn test_message_content_display_text() {
let content = MessageContent::Text("Hello, world!".to_string());
assert_eq!(format!("{}", content), "Hello, world!");
}
#[test]
fn test_message_content_display_json() {
let content = MessageContent::Json(serde_json::json!({"key": "value"}));
let display = format!("{}", content);
assert!(display.contains("key"));
assert!(display.contains("value"));
}
#[test]
fn test_message_content_display_tool_call() {
let content = MessageContent::ToolCall {
id: "call_123".to_string(),
name: "get_weather".to_string(),
arguments: serde_json::json!({"city": "London"}),
};
let display = format!("{}", content);
assert!(display.contains("get_weather"));
assert!(display.contains("London"));
}
#[test]
fn test_message_content_display_tool_result_success() {
let content = MessageContent::ToolResult {
tool_call_id: "call_123".to_string(),
content: "Sunny, 22°C".to_string(),
is_error: false,
};
assert_eq!(format!("{}", content), "Sunny, 22°C");
}
#[test]
fn test_message_content_display_tool_result_error() {
let content = MessageContent::ToolResult {
tool_call_id: "call_123".to_string(),
content: "API timeout".to_string(),
is_error: true,
};
let display = format!("{}", content);
assert!(display.starts_with("Error:"));
assert!(display.contains("API timeout"));
}
use crate::messages::{MessageAttributes, MessageCategory};
#[test]
fn test_unified_message_with_attributes() {
let attrs = MessageAttributes {
priority: 10,
cacheable: true,
cache_type: Some(CacheType::Extended),
cache_key: Some("custom-key".to_string()),
category: MessageCategory::Context,
metadata: std::collections::HashMap::new(),
};
let message = UnifiedMessage::with_attributes(
MessageRole::System,
MessageContent::Text("Test content".to_string()),
attrs,
);
assert_eq!(message.role, MessageRole::System);
assert_eq!(message.attributes.priority, 10);
assert!(message.attributes.cacheable);
assert_eq!(message.attributes.cache_type, Some(CacheType::Extended));
assert_eq!(message.attributes.cache_key, Some("custom-key".to_string()));
assert_eq!(message.attributes.category, MessageCategory::Context);
}
#[test]
fn test_system_instruction_constructor() {
let message =
UnifiedMessage::system_instruction("You are helpful.".to_string(), Some("v1".to_string()));
assert_eq!(message.role, MessageRole::System);
assert_eq!(message.attributes.priority, 0);
assert!(message.attributes.cacheable);
assert_eq!(message.attributes.cache_key, Some("v1".to_string()));
assert_eq!(
message.attributes.category,
MessageCategory::SystemInstruction
);
}
#[test]
fn test_system_instruction_without_cache_key() {
let message = UnifiedMessage::system_instruction("You are helpful.".to_string(), None);
assert_eq!(message.attributes.cache_key, None);
assert!(message.attributes.cacheable);
}
#[test]
fn test_tool_definition_constructor() {
let message =
UnifiedMessage::tool_definition("Tool schema".to_string(), Some("tools-v1".to_string()));
assert_eq!(message.role, MessageRole::System);
assert_eq!(message.attributes.priority, 1);
assert!(message.attributes.cacheable);
assert_eq!(message.attributes.category, MessageCategory::ToolDefinition);
}
#[test]
fn test_context_constructor() {
let message = UnifiedMessage::context("User preferences".to_string(), None);
assert_eq!(message.role, MessageRole::System);
assert_eq!(message.attributes.priority, 5);
assert!(message.attributes.cacheable);
assert_eq!(message.attributes.category, MessageCategory::Context);
}
#[test]
fn test_history_constructor() {
let message = UnifiedMessage::history(MessageRole::User, "Previous question".to_string());
assert_eq!(message.role, MessageRole::User);
assert_eq!(message.attributes.priority, 20);
assert!(message.attributes.cacheable);
assert_eq!(message.attributes.category, MessageCategory::History);
}
#[test]
fn test_current_user_constructor() {
let message = UnifiedMessage::current_user("What's the weather?".to_string());
assert_eq!(message.role, MessageRole::User);
assert_eq!(message.attributes.priority, 30);
assert!(!message.attributes.cacheable);
assert_eq!(message.attributes.category, MessageCategory::Current);
}
#[test]
fn test_tool_call_constructor() {
let message = UnifiedMessage::tool_call(
"call_abc".to_string(),
"get_weather".to_string(),
serde_json::json!({"city": "London"}),
);
assert_eq!(message.role, MessageRole::Assistant);
assert_eq!(message.attributes.priority, 25);
assert!(!message.attributes.cacheable);
match message.content {
MessageContent::ToolCall {
id,
name,
arguments,
} => {
assert_eq!(id, "call_abc");
assert_eq!(name, "get_weather");
assert_eq!(arguments["city"], "London");
}
_ => panic!("Expected ToolCall content"),
}
}
#[test]
fn test_tool_result_constructor() {
let message =
UnifiedMessage::tool_result("call_abc".to_string(), "Sunny, 22°C".to_string(), false);
assert_eq!(message.role, MessageRole::Tool);
assert_eq!(message.attributes.priority, 26);
assert!(!message.attributes.cacheable);
match message.content {
MessageContent::ToolResult {
tool_call_id,
content,
is_error,
} => {
assert_eq!(tool_call_id, "call_abc");
assert_eq!(content, "Sunny, 22°C");
assert!(!is_error);
}
_ => panic!("Expected ToolResult content"),
}
}
#[test]
fn test_tool_result_constructor_with_error() {
let message = UnifiedMessage::tool_result(
"call_abc".to_string(),
"Connection failed".to_string(),
true,
);
match message.content {
MessageContent::ToolResult { is_error, .. } => {
assert!(is_error);
}
_ => panic!("Expected ToolResult content"),
}
}
use crate::messages::UnifiedLLMRequest;
use crate::provider::RequestConfig;
#[test]
fn test_unified_llm_request_new() {
let messages = vec![
UnifiedMessage::system("System prompt"),
UnifiedMessage::user("User input"),
];
let request = UnifiedLLMRequest::new(messages);
assert_eq!(request.messages.len(), 2);
assert!(request.response_schema.is_none());
assert!(request.config.is_none());
}
#[test]
fn test_unified_llm_request_with_schema() {
let messages = vec![UnifiedMessage::user("Extract data")];
let schema = serde_json::json!({
"type": "object",
"properties": {
"name": {"type": "string"}
}
});
let request = UnifiedLLMRequest::with_schema(messages, schema.clone());
assert_eq!(request.messages.len(), 1);
assert_eq!(request.response_schema, Some(schema));
assert!(request.config.is_none());
}
#[test]
fn test_unified_llm_request_with_config() {
let messages = vec![UnifiedMessage::user("Hello")];
let config = RequestConfig {
temperature: Some(0.7),
max_tokens: Some(1000),
..Default::default()
};
let request = UnifiedLLMRequest::with_config(messages, config);
assert_eq!(request.messages.len(), 1);
assert!(request.response_schema.is_none());
assert!(request.config.is_some());
assert_eq!(request.config.as_ref().unwrap().temperature, Some(0.7));
}
#[test]
fn test_sort_messages_by_priority() {
let messages = vec![
UnifiedMessage::current_user("User input".to_string()), UnifiedMessage::system_instruction("System".to_string(), None), UnifiedMessage::context("Context".to_string(), None), ];
let mut request = UnifiedLLMRequest::new(messages);
request.sort_messages();
assert_eq!(request.messages[0].attributes.priority, 0);
assert_eq!(request.messages[1].attributes.priority, 5);
assert_eq!(request.messages[2].attributes.priority, 30);
}
#[test]
fn test_get_sorted_messages_does_not_modify_original() {
let messages = vec![
UnifiedMessage::current_user("User input".to_string()), UnifiedMessage::system_instruction("System".to_string(), None), ];
let request = UnifiedLLMRequest::new(messages);
let sorted = request.get_sorted_messages();
assert_eq!(request.messages[0].attributes.priority, 30);
assert_eq!(sorted[0].attributes.priority, 0);
assert_eq!(sorted[1].attributes.priority, 30);
}
#[test]
fn test_get_system_messages() {
let messages = vec![
UnifiedMessage::system("System 1"),
UnifiedMessage::user("User"),
UnifiedMessage::system("System 2"),
UnifiedMessage::assistant("Assistant"),
];
let request = UnifiedLLMRequest::new(messages);
let system_msgs = request.get_system_messages();
assert_eq!(system_msgs.len(), 2);
assert!(system_msgs.iter().all(|m| m.role == MessageRole::System));
}
#[test]
fn test_get_conversation_messages() {
let messages = vec![
UnifiedMessage::system("System"),
UnifiedMessage::user("User"),
UnifiedMessage::assistant("Assistant"),
UnifiedMessage::tool_result("id".to_string(), "result".to_string(), false),
];
let request = UnifiedLLMRequest::new(messages);
let conv_msgs = request.get_conversation_messages();
assert_eq!(conv_msgs.len(), 3);
assert!(conv_msgs.iter().all(|m| m.role != MessageRole::System));
}
#[test]
fn test_get_cacheable_messages() {
let messages = vec![
UnifiedMessage::system_instruction("Cacheable system".to_string(), None), UnifiedMessage::current_user("Not cached".to_string()), UnifiedMessage::context("Cacheable context".to_string(), None), ];
let request = UnifiedLLMRequest::new(messages);
let cacheable = request.get_cacheable_messages();
assert_eq!(cacheable.len(), 2);
assert!(cacheable.iter().all(|m| m.attributes.cacheable));
}