#[ cfg( all( test, feature = "streaming" ) ) ]
mod tests
{
use api_ollama::{ OllamaClient, ChatRequest, ChatMessage, MessageRole };
#[ tokio::test ]
async fn test_streaming_chat_can_initialize()
{
let _client = OllamaClient::new( "http://localhost:11434".to_string(), OllamaClient::recommended_timeout_fast() );
let mut conversation_history = vec![
ChatMessage
{
role : MessageRole::System,
content : "You are a helpful assistant. Provide engaging, informative responses. \\
When appropriate, use examples and ask follow-up questions to keep \\
the conversation flowing.".to_string(),
images : None,
#[ cfg( feature = "tool_calling" ) ]
tool_calls : None,
}
];
assert_eq!( conversation_history.len(), 1 );
conversation_history.push( ChatMessage
{
role : MessageRole::User,
content : "Hello!".to_string(),
images : None,
#[ cfg( feature = "tool_calling" ) ]
tool_calls : None,
} );
let request = ChatRequest
{
model : "test-model".to_string(),
messages : conversation_history.clone(),
stream : Some( true ), options : None,
#[ cfg( feature = "tool_calling" ) ]
tools : None,
#[ cfg( feature = "tool_calling" ) ]
tool_messages : None,
};
assert_eq!( request.model, "test-model" );
assert_eq!( request.stream, Some( true ) );
assert_eq!( request.messages.len(), 2 );
if conversation_history.len() > 21
{
conversation_history.drain( 1..conversation_history.len() - 20 );
}
assert!( conversation_history.len() <= 21 );
}
#[ tokio::test ]
async fn test_demo_scenarios_structure()
{
let demo_scenarios = [
(
"Creative Writing",
"Write a short, dramatic story about a detective discovering a mysterious letter. \\
Make it engaging and suspenseful.",
),
(
"Technical Explanation",
"Explain how neural networks work, using analogies that a non-technical person \\
could understand. Include real-world examples.",
),
(
"Problem Solving",
"I have a small apartment and want to create a home office space. \\
What are some creative, space-efficient solutions?",
)];
assert_eq!( demo_scenarios.len(), 3 );
let ( scenario_name, prompt ) = demo_scenarios[ 0 ];
let request = ChatRequest
{
model : "test-model".to_string(),
messages : vec![ ChatMessage
{
role : MessageRole::User,
content : prompt.to_string(),
images : None,
#[ cfg( feature = "tool_calling" ) ]
tool_calls : None,
} ],
stream : Some( true ),
options : None,
#[ cfg( feature = "tool_calling" ) ]
tools : None,
#[ cfg( feature = "tool_calling" ) ]
tool_messages : None,
};
assert!( request.messages[ 0 ].content.contains( "detective" ) );
assert_eq!( scenario_name, "Creative Writing" );
}
}
#[ cfg( not( feature = "streaming" ) ) ]
#[ tokio::test ]
async fn test_streaming_feature_not_enabled()
{
assert!( true ); }