use crate::model::{
AgentResponse, MAX_QUESTIONS, MAX_SUBTASKS, ReviewCommentOutcome, ReviewCommentResolution,
questions_field_description, render_field_description_template, subtasks_field_description,
};
use crate::question::QuestionItem;
use crate::subtask::SubtaskItem;
use crate::verification::VerificationVerdictItem;
#[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(),
subtasks: Vec::new(),
verification_verdicts: Vec::new(),
};
let display_text = response.to_display_text();
assert_eq!(display_text, "Primary answer\n\nNeed one clarification.");
}
#[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(),
}],
subtasks: Vec::new(),
verification_verdicts: Vec::new(),
};
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(),
subtasks: Vec::new(),
verification_verdicts: Vec::new(),
};
let questions = response.question_items();
assert_eq!(questions.len(), MAX_QUESTIONS);
}
#[test]
fn test_agent_response_subtask_items_applies_subtask_cap() {
let response = AgentResponse {
answer: String::new(),
questions: Vec::new(),
review_comment_outcomes: Vec::new(),
subtasks: (0..=MAX_SUBTASKS).map(test_subtask).collect(),
verification_verdicts: Vec::new(),
};
let subtasks = response.subtask_items();
assert_eq!(subtasks.len(), MAX_SUBTASKS);
assert_eq!(subtasks[0].task_key, "task-0");
}
#[test]
fn test_agent_response_subtasks_round_trip() {
let response = AgentResponse {
answer: "Proposed a plan.".to_string(),
questions: Vec::new(),
review_comment_outcomes: Vec::new(),
subtasks: vec![test_subtask(1)],
verification_verdicts: Vec::new(),
};
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#""task_key":"task-1""#));
assert_eq!(
AgentResponse::plain("no plan").subtask_items(),
[] as [crate::subtask::SubtaskItem; 0]
);
}
#[test]
fn test_agent_response_verification_verdicts_round_trip_and_cap() {
let response = AgentResponse {
answer: "Verified the settled tasks.".to_string(),
questions: Vec::new(),
review_comment_outcomes: Vec::new(),
subtasks: Vec::new(),
verification_verdicts: (0..=MAX_SUBTASKS)
.map(|index| VerificationVerdictItem {
reason: format!("Evidence {index}"),
task_key: format!("task-{index}"),
verdict: crate::VerificationVerdict::Pass,
})
.collect(),
};
let serialized = serde_json::to_string(&response).expect("response should serialize");
let deserialized =
serde_json::from_str::<AgentResponse>(&serialized).expect("response should deserialize");
let verdicts = deserialized.verification_verdict_items();
assert_eq!(verdicts.len(), MAX_SUBTASKS);
assert_eq!(verdicts[0].task_key, "task-0");
assert!(serialized.contains(r#""verdict":"pass""#));
}
#[test]
fn test_subtask_item_defaults_touched_areas() {
let raw = r#"{"prompt":"Do the work","task_key":"task-1","title":"Work"}"#;
let subtask =
serde_json::from_str::<SubtaskItem>(raw).expect("subtask should parse without areas");
assert_eq!(subtask.kind, crate::SubtaskKind::Implementation);
assert_eq!(subtask.touched_areas, [] as [std::string::String; 0]);
}
#[test]
fn test_subtasks_field_description_reports_the_subtask_cap() {
let expected_limit = format!("at most {MAX_SUBTASKS} items");
let description = subtasks_field_description();
let normalized_description = description.split_whitespace().collect::<Vec<_>>().join(" ");
assert!(description.contains(&expected_limit));
assert!(
normalized_description.contains("Emit an empty array when no decomposition was requested")
);
assert!(normalized_description.contains("field defaults to an empty array when omitted"));
assert!(normalized_description.contains("Ordinary session and utility turns"));
assert!(normalized_description.contains("unattended in its own worktree"));
assert!(normalized_description.contains("independently completable"));
assert!(normalized_description.contains("without wildcards"));
assert!(description.contains("Areas may overlap"));
assert!(normalized_description.contains("fewer than two independent subtasks"));
assert!(!description.contains("{{"));
}
#[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.");
}
fn test_subtask(index: usize) -> SubtaskItem {
SubtaskItem {
acceptance_criteria: vec![format!("Work item {index} is complete")],
kind: crate::SubtaskKind::Implementation,
prompt: format!("Complete work item {index}"),
task_key: format!("task-{index}"),
title: format!("Work item {index}"),
touched_areas: vec![format!("crates/area-{index}/")],
}
}