use super::*;
#[test]
fn test_valid_simple_sequence() {
let msgs = vec![ChatMessage::user("hello"), ChatMessage::assistant("hi")];
assert!(validate_message_sequence(&msgs).is_ok());
}
#[test]
fn test_valid_tool_call_sequence() {
let msgs = vec![
ChatMessage::user("run command"),
ChatMessage::assistant_tool_call("call_1", "bash", r#"{"cmd":"ls"}"#),
ChatMessage::tool("call_1", "file1 file2"),
ChatMessage::assistant("done"),
];
assert!(validate_message_sequence(&msgs).is_ok());
}
#[test]
fn test_valid_multi_tool_call_sequence() {
let msgs = vec![
ChatMessage::user("run commands"),
ChatMessage::Assistant {
content: None,
reasoning_content: None,
tool_calls: Some(vec![
crate::types::ToolCallMessage {
id: "call_1".into(),
name: "bash".into(),
arguments: "{}".into(),
},
crate::types::ToolCallMessage {
id: "call_2".into(),
name: "read".into(),
arguments: "{}".into(),
},
]),
thinking_signature: None,
},
ChatMessage::tool("call_1", "result1"),
ChatMessage::tool("call_2", "result2"),
ChatMessage::assistant("done"),
];
assert!(validate_message_sequence(&msgs).is_ok());
}
#[test]
fn test_orphaned_tool_result() {
let msgs = vec![
ChatMessage::user("hello"),
ChatMessage::tool("call_1", "orphaned result"),
];
let err = validate_message_sequence(&msgs).unwrap_err();
assert!(err.contains("no preceding tool_call"));
}
#[test]
fn test_system_only_sequence_rejected() {
let msgs = vec![
ChatMessage::system("prompt"),
ChatMessage::system_ephemeral("reminder"),
ChatMessage::Custom {
role: "artifact".into(),
data: serde_json::json!({"id": "x"}),
},
];
let err = validate_message_sequence(&msgs).unwrap_err();
assert!(err.contains("no sendable message"));
}
#[test]
fn test_system_plus_user_ok() {
let msgs = vec![ChatMessage::system("prompt"), ChatMessage::user("hi")];
assert!(validate_message_sequence(&msgs).is_ok());
}
#[test]
fn test_mismatched_tool_call_id() {
let msgs = vec![
ChatMessage::user("run"),
ChatMessage::assistant_tool_call("call_1", "bash", "{}"),
ChatMessage::tool("call_2", "wrong id"),
];
let err = validate_message_sequence(&msgs).unwrap_err();
assert!(err.contains("does not match"));
}
#[test]
fn test_set_chat_messages_valid() {
let mut s = make_session();
let msgs = vec![ChatMessage::user("hello"), ChatMessage::assistant("hi")];
assert!(s.set_chat_messages(msgs.clone()).is_ok());
assert_eq!(s.chat_messages().len(), 2);
}
#[test]
fn test_set_chat_messages_invalid() {
let mut s = make_session();
let msgs = vec![ChatMessage::tool("call_1", "orphaned")];
assert!(s.set_chat_messages(msgs).is_err());
}
#[test]
fn deserialize_legacy_flat_fields() {
let json = r#"{
"id": null,
"chat_messages": [],
"always_allowed_actions": [],
"total_tool_calls": 5,
"nudge_count": 3,
"turn_tool_calls": 2,
"reasoning_only_strikes": 1,
"empty_response_strikes": 0
}"#;
let session: AgentSession = serde_json::from_str(json).unwrap();
assert_eq!(session.run_state.nudge_count, 3);
assert_eq!(session.run_state.turn_actions, 2);
assert_eq!(session.run_state.reasoning_only_strikes, 1);
assert_eq!(session.run_state.empty_response_strikes, 0);
assert!(!session.run_state.run_has_actions); }
#[test]
fn deserialize_new_run_state_format() {
let json = r#"{
"id": null,
"chat_messages": [],
"always_allowed_actions": [],
"total_tool_calls": 5,
"run_state": {
"turn_tool_calls": 4,
"run_has_tool_calls": true,
"reasoning_only_strikes": 0,
"empty_response_strikes": 1,
"nudge_count": 2
}
}"#;
let session: AgentSession = serde_json::from_str(json).unwrap();
assert_eq!(session.run_state.turn_actions, 4);
assert!(session.run_state.run_has_actions);
assert_eq!(session.run_state.empty_response_strikes, 1);
assert_eq!(session.run_state.nudge_count, 2);
}
#[test]
fn deserialize_run_state_takes_precedence_over_flat() {
let json = r#"{
"id": null,
"chat_messages": [],
"always_allowed_actions": [],
"total_tool_calls": 0,
"run_state": {
"turn_tool_calls": 10,
"run_has_tool_calls": true,
"reasoning_only_strikes": 0,
"empty_response_strikes": 0,
"nudge_count": 0
},
"nudge_count": 99,
"turn_tool_calls": 99
}"#;
let session: AgentSession = serde_json::from_str(json).unwrap();
assert_eq!(session.run_state.turn_actions, 10); assert_eq!(session.run_state.nudge_count, 0); }
#[test]
fn deserialize_legacy_missing_optional_fields() {
let json = r#"{
"id": null,
"chat_messages": [],
"always_allowed_actions": [],
"total_tool_calls": 0,
"nudge_count": 1
}"#;
let session: AgentSession = serde_json::from_str(json).unwrap();
assert_eq!(session.run_state.nudge_count, 1);
assert_eq!(session.run_state.turn_actions, 0); assert_eq!(session.run_state.reasoning_only_strikes, 0);
assert_eq!(session.run_state.empty_response_strikes, 0);
}
#[test]
fn roundtrip_preserves_run_state() {
let mut session = AgentSession::new(SessionId::new(1));
session.run_state.nudge_count = 5;
session.run_state.turn_actions = 3;
session.run_state.run_has_actions = true;
session.run_state.reasoning_only_strikes = 2;
let json = serde_json::to_string(&session).unwrap();
let restored: AgentSession = serde_json::from_str(&json).unwrap();
assert_eq!(restored.run_state.nudge_count, 5);
assert_eq!(restored.run_state.turn_actions, 3);
assert!(restored.run_state.run_has_actions);
assert_eq!(restored.run_state.reasoning_only_strikes, 2);
}
#[test]
fn deserialize_migrates_top_level_total_into_run_state() {
let json = r#"{
"id": null,
"chat_messages": [],
"always_allowed_actions": [],
"total_tool_calls": 7,
"run_state": {
"turn_tool_calls": 4,
"run_has_tool_calls": true,
"reasoning_only_strikes": 0,
"empty_response_strikes": 0,
"nudge_count": 0
}
}"#;
let session: AgentSession = serde_json::from_str(json).unwrap();
assert_eq!(session.run_state.total_actions, 7);
assert_eq!(session.run_state.turn_actions, 4);
let rewritten = serde_json::to_string(&session).unwrap();
assert!(
!rewritten.contains("total_tool_calls"),
"new writes keep the total inside run_state: {rewritten}"
);
}
#[test]
fn push_assistant_tool_calls_validates_json_args() {
let mut s = make_session();
let valid_args = r#"{"path": "src/main.rs", "content": "fn main() {}"}"#;
s.push_assistant_tool_calls(
&[("id1".into(), "write_file".into(), valid_args.into())],
None,
None,
);
if let ChatMessage::Assistant {
tool_calls: Some(ref tc),
..
} = s.chat_messages[0]
{
assert_eq!(tc[0].arguments, valid_args);
} else {
panic!("expected Assistant message with tool_calls");
}
let truncated_args = r#"{"path": "src/ui/markdown.rs", "content": "#;
s.push_assistant_tool_calls(
&[("id2".into(), "write_file".into(), truncated_args.into())],
None,
None,
);
if let ChatMessage::Assistant {
tool_calls: Some(ref tc),
..
} = s.chat_messages[1]
{
assert_eq!(tc[0].arguments, "{}");
assert!(
!tc[0].arguments.contains("tool_call_arguments_truncated"),
"assistant arguments must not carry the poison wrapper object"
);
} else {
panic!("expected Assistant message with tool_calls");
}
}
#[test]
fn push_assistant_tool_calls_truncated_multibyte_no_panic() {
let mut s = make_session();
let mut bad_args = "あ".repeat(70); bad_args.push_str("truncated");
s.push_assistant_tool_calls(&[("id1".into(), "tool".into(), bad_args)], None, None);
if let ChatMessage::Assistant {
tool_calls: Some(ref tc),
..
} = s.chat_messages[0]
{
assert_eq!(tc[0].arguments, "{}");
} else {
panic!("expected Assistant message with tool_calls");
}
}
#[test]
fn push_assistant_tool_calls_then_tool_result_matches_anthropic_protocol() {
let mut s = make_session();
let tool_calls = vec![
(
"call_00_VJtlnKha0ZZ2Yo8t5ysQ8883".to_string(),
"write_file".to_string(),
"{}".to_string(),
),
(
"call_01_abc123".to_string(),
"bash".to_string(),
r#"{"command": "ls"}"#.to_string(),
),
];
s.push_assistant_tool_calls(
&tool_calls,
Some("thinking...".to_string()),
Some("I'll help you".to_string()),
);
for (tc_id, _, _) in &tool_calls {
s.push_tool_result(
tc_id,
"Tool call was not executed: the response hit the output token limit.",
);
}
if let ChatMessage::Assistant {
tool_calls: Some(ref tc),
..
} = s.chat_messages[0]
{
assert_eq!(tc.len(), 2);
assert_eq!(tc[0].id, "call_00_VJtlnKha0ZZ2Yo8t5ysQ8883");
assert_eq!(tc[0].name, "write_file");
assert_eq!(tc[1].id, "call_01_abc123");
assert_eq!(tc[1].name, "bash");
} else {
panic!("expected Assistant message with tool_calls");
}
if let ChatMessage::Tool { tool_call_id, .. } = &s.chat_messages[1] {
assert_eq!(tool_call_id, "call_00_VJtlnKha0ZZ2Yo8t5ysQ8883");
} else {
panic!("expected Tool message for first tool call");
}
if let ChatMessage::Tool { tool_call_id, .. } = &s.chat_messages[2] {
assert_eq!(tool_call_id, "call_01_abc123");
} else {
panic!("expected Tool message for second tool call");
}
assert!(
validate_message_sequence(&s.chat_messages).is_ok(),
"message sequence should be valid with matching tool_use and tool_result"
);
}