use langchainrust::{
AgentState, StateUpdate, MessageEntry,
MessageRole, StepEntry,
};
#[test]
fn test_agent_state_creation() {
let state = AgentState::new("Hello world".to_string());
assert_eq!(state.input, "Hello world");
assert_eq!(state.messages.len(), 1);
assert_eq!(state.messages[0].role, MessageRole::Human);
assert_eq!(state.messages[0].content, "Hello world");
assert!(state.steps.is_empty());
assert!(state.output.is_none());
}
#[test]
fn test_agent_state_add_message() {
let mut state = AgentState::new("question".to_string());
state.add_message(MessageEntry::ai("answer".to_string()));
assert_eq!(state.messages.len(), 2);
assert_eq!(state.messages[0].role, MessageRole::Human);
assert_eq!(state.messages[1].role, MessageRole::AI);
assert_eq!(state.messages[1].content, "answer");
}
#[test]
fn test_agent_state_add_step() {
let mut state = AgentState::new("task".to_string());
state.add_step(StepEntry::new("tool_call".to_string(), "result".to_string()));
assert_eq!(state.steps.len(), 1);
assert_eq!(state.steps[0].action, "tool_call");
assert_eq!(state.steps[0].observation, "result");
}
#[test]
fn test_agent_state_set_output() {
let mut state = AgentState::new("input".to_string());
state.set_output("final answer".to_string());
assert_eq!(state.output, Some("final answer".to_string()));
}
#[test]
fn test_message_entry_types() {
let human = MessageEntry::human("user message".to_string());
assert_eq!(human.role, MessageRole::Human);
assert_eq!(human.content, "user message");
let ai = MessageEntry::ai("ai response".to_string());
assert_eq!(ai.role, MessageRole::AI);
assert_eq!(ai.content, "ai response");
let system = MessageEntry::system("system prompt".to_string());
assert_eq!(system.role, MessageRole::System);
assert_eq!(system.content, "system prompt");
let tool = MessageEntry::tool("tool output".to_string());
assert_eq!(tool.role, MessageRole::Tool);
assert_eq!(tool.content, "tool output");
}
#[test]
fn test_state_update_full() {
let state = AgentState::new("test".to_string());
let update = StateUpdate::full(state.clone());
assert!(update.update.is_some());
assert!(update.metadata.is_empty());
}
#[test]
fn test_state_update_with_metadata() {
let state = AgentState::new("test".to_string());
let metadata = std::collections::HashMap::from([
("node".to_string(), serde_json::json!("step1")),
("timestamp".to_string(), serde_json::json!(12345)),
]);
let update = StateUpdate::with_metadata(state, metadata.clone());
assert!(update.update.is_some());
assert_eq!(update.metadata.get("node").unwrap(), "step1");
}
#[test]
fn test_state_update_unchanged() {
let update = StateUpdate::<AgentState>::unchanged();
assert!(update.update.is_none());
assert!(update.metadata.is_empty());
}
#[test]
fn test_agent_state_clone_preserves_data() {
let original = AgentState::new("original input".to_string());
original.clone().add_message(MessageEntry::ai("response".to_string()));
let cloned = original.clone();
assert_eq!(cloned.input, "original input");
assert_eq!(cloned.messages.len(), 1);
}
#[test]
fn test_message_role_equality() {
assert_eq!(MessageRole::Human, MessageRole::Human);
assert_ne!(MessageRole::Human, MessageRole::AI);
assert_ne!(MessageRole::System, MessageRole::Tool);
}