use crate::model::{
AgentResponse, AgentResponseParseError, MAX_QUESTIONS, ReviewCommentOutcome,
ReviewCommentResolution, questions_field_description, render_field_description_template,
};
use crate::question::QuestionItem;
#[test]
fn test_questions_field_description_renders_template_limit() {
let expected_limit = format!("Emit at most {MAX_QUESTIONS} items");
let description = questions_field_description();
let normalized_description = description.split_whitespace().collect::<Vec<_>>().join(" ");
assert!(normalized_description.contains(&expected_limit));
assert!(normalized_description.contains("Emit an empty array when no input is required"));
assert!(normalized_description.contains("field defaults to an empty array when omitted"));
assert!(normalized_description.contains("genuinely ambiguous requirement"));
assert!(normalized_description.contains("Never request permission for agreed work"));
assert!(normalized_description.contains("ask for satisfaction or sign-off"));
assert!(normalized_description.contains("Execute agreed work"));
assert!(!description.contains("{{ max_questions }}"));
}
#[test]
fn test_agent_response_to_display_text_joins_answer_and_questions() {
let response = AgentResponse {
answer: "Primary answer".to_string(),
questions: vec![QuestionItem::new("Need one clarification.")],
review_comment_outcomes: Vec::new(),
};
let display_text = response.to_display_text();
assert_eq!(display_text, "Primary answer\n\nNeed one clarification.");
}
#[test]
fn test_agent_response_answers_skip_blank_answer_text() {
let answered = AgentResponse {
answer: "Primary answer".to_string(),
questions: vec![QuestionItem::new("Hidden from answer text.")],
review_comment_outcomes: Vec::new(),
};
let blank = AgentResponse::plain(" \n ");
let answered_text = answered.to_answer_display_text();
let answered_list = answered.answers();
let blank_text = blank.to_answer_display_text();
let blank_list = blank.answers();
assert_eq!(answered_text, "Primary answer");
assert_eq!(answered_list, vec!["Primary answer".to_string()]);
assert_eq!(blank_text, "");
assert_eq!(blank_list, Vec::<String>::new());
}
#[test]
fn test_agent_response_parse_error_display() {
let empty = AgentResponseParseError::Empty;
let invalid = AgentResponseParseError::InvalidFormat {
reason: "missing answer".to_string(),
};
let empty_text = empty.to_string();
let invalid_text = invalid.to_string();
assert_eq!(empty_text, "response is empty");
assert_eq!(
invalid_text,
"response is not valid protocol JSON: missing answer"
);
}
#[test]
fn test_agent_response_review_comment_outcomes_round_trip() {
let response = AgentResponse {
answer: "Addressed the comment.".to_string(),
questions: Vec::new(),
review_comment_outcomes: vec![ReviewCommentOutcome {
reply: "Added the missing validation.".to_string(),
resolution: ReviewCommentResolution::Fixed,
thread_id: "thread-42".to_string(),
}],
};
let serialized = serde_json::to_string(&response).expect("response should serialize");
let deserialized =
serde_json::from_str::<AgentResponse>(&serialized).expect("response should deserialize");
assert_eq!(deserialized, response);
assert!(serialized.contains(r#""resolution":"fixed""#));
}
#[test]
fn test_agent_response_question_items_applies_question_cap() {
let response = AgentResponse {
answer: String::new(),
questions: (0..=MAX_QUESTIONS)
.map(|index| QuestionItem::new(format!("Question {index}")))
.collect(),
review_comment_outcomes: Vec::new(),
};
let questions = response.question_items();
assert_eq!(questions.len(), MAX_QUESTIONS);
}
#[test]
fn test_field_description_template_survives_a_wrapped_placeholder() {
let template = "Emit at most {{\nmax_items }} items, and no more.\n";
let rendered = render_field_description_template(template, "{{ max_items }}", 4);
assert_eq!(rendered, "Emit at most 4 items, and no more.");
}
#[test]
fn test_field_description_template_keeps_unterminated_placeholder_text() {
let template = "Emit at most {{ max_items items.";
let rendered = render_field_description_template(template, "{{ max_items }}", 4);
assert_eq!(rendered, "Emit at most {{ max_items items.");
}