use std::path::PathBuf;
use crate::prompt::{
TurnPrompt, TurnPromptAttachment, TurnPromptContentPart, TurnPromptTextSource,
render_prompt_text_for_agent, split_turn_prompt_content,
};
#[test]
fn test_turn_prompt_attachment_json_uses_named_fields_independent_of_order() {
let attachment = TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-1.png"),
placeholder: "[Image #1]".to_string(),
};
let alternate_order_json =
r#"{"placeholder":"[Image #1]","local_image_path":"/tmp/image-1.png"}"#;
let deserialized_attachment =
serde_json::from_str::<TurnPromptAttachment>(alternate_order_json)
.expect("attachment JSON should deserialize by field name");
let serialized_value = serde_json::to_value(&attachment).expect("attachment should serialize");
assert_eq!(deserialized_attachment, attachment);
assert_eq!(
serialized_value,
serde_json::json!({
"local_image_path": "/tmp/image-1.png",
"placeholder": "[Image #1]",
})
);
}
#[test]
fn test_turn_prompt_compares_with_str_in_both_directions() {
let prompt = TurnPrompt::from("Review this change");
let prompt_matches = prompt == "Review this change";
let text_matches = "Review this change" == prompt;
assert!(prompt_matches);
assert!(text_matches);
}
#[test]
fn test_turn_prompt_transcript_text_keeps_inline_placeholders() {
let prompt = TurnPrompt {
attachments: vec![TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-1.png"),
placeholder: "[Image #1]".to_string(),
}],
text: "Review [Image #1] carefully".to_string(),
text_source: TurnPromptTextSource::UserPrompt,
};
let transcript_text = prompt.transcript_text();
assert_eq!(transcript_text, "Review [Image #1] carefully");
}
#[test]
fn test_turn_prompt_agent_text_rewrites_user_at_lookups() {
let prompt = TurnPrompt::from("Review @src/main.rs and person@example.com");
let agent_text = prompt.agent_text();
let transcript_text = prompt.transcript_text();
assert_eq!(agent_text, "Review \"src/main.rs\" and person@example.com");
assert_eq!(
transcript_text,
"Review @src/main.rs and person@example.com"
);
}
#[test]
fn test_turn_prompt_agent_text_preserves_agent_data_at_tokens() {
let prompt = TurnPrompt::from_agent_data(
"Diff:\n```diff\n+@dataclass\n+class Config:\n+ pass\n```".to_string(),
);
let agent_text = prompt.agent_text();
assert!(agent_text.contains("+@dataclass"));
assert!(!agent_text.contains("+\"dataclass\""));
}
#[test]
fn test_turn_prompt_transcript_text_appends_missing_placeholders() {
let prompt = TurnPrompt {
attachments: vec![
TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-1.png"),
placeholder: "[Image #1]".to_string(),
},
TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-2.png"),
placeholder: "[Image #2]".to_string(),
},
],
text: "Review".to_string(),
text_source: TurnPromptTextSource::UserPrompt,
};
let transcript_text = prompt.transcript_text();
assert_eq!(transcript_text, "Review [Image #1] [Image #2]");
}
#[test]
fn test_split_turn_prompt_content_returns_text_without_attachments() {
let text = "Review this change";
let content_parts = split_turn_prompt_content(text, &[]);
assert_eq!(content_parts, vec![TurnPromptContentPart::Text(text)]);
}
#[test]
fn test_split_turn_prompt_content_orders_placeholders_and_appends_orphans() {
let attachments = vec![
TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-1.png"),
placeholder: "[Image #1]".to_string(),
},
TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-2.png"),
placeholder: "[Image #2]".to_string(),
},
TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-3.png"),
placeholder: "[Image #3]".to_string(),
},
];
let content_parts =
split_turn_prompt_content("Compare [Image #2] with [Image #1] now", &attachments);
assert_eq!(
content_parts,
vec![
TurnPromptContentPart::Text("Compare "),
TurnPromptContentPart::Attachment(&attachments[1]),
TurnPromptContentPart::Text(" with "),
TurnPromptContentPart::Attachment(&attachments[0]),
TurnPromptContentPart::Text(" now"),
TurnPromptContentPart::OrphanAttachment(&attachments[2]),
]
);
}
#[test]
fn test_split_turn_prompt_content_omits_empty_edge_text_parts() {
let attachments = vec![
TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-1.png"),
placeholder: "[Image #1]".to_string(),
},
TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-2.png"),
placeholder: "[Image #2]".to_string(),
},
];
let content_parts = split_turn_prompt_content("[Image #1] vs [Image #2]", &attachments);
assert_eq!(
content_parts,
vec![
TurnPromptContentPart::Attachment(&attachments[0]),
TurnPromptContentPart::Text(" vs "),
TurnPromptContentPart::Attachment(&attachments[1]),
]
);
}
#[test]
fn test_turn_prompt_accessors_and_conversions_expose_payload() {
let attachment = TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-1.png"),
placeholder: "[Image #1]".to_string(),
};
let prompt = TurnPrompt {
attachments: vec![attachment.clone()],
text: "Explain [Image #1]".to_string(),
text_source: TurnPromptTextSource::UserPrompt,
};
let empty_prompt = TurnPrompt::from(String::new());
let copied_prompt = TurnPrompt::from(&prompt);
let local_image_paths = prompt.local_image_paths().collect::<Vec<_>>();
assert_eq!(copied_prompt, prompt);
assert!(!prompt.is_empty());
assert!(empty_prompt.is_empty());
assert!(prompt.has_attachments());
assert!(!empty_prompt.has_attachments());
assert_eq!(local_image_paths, vec![&attachment.local_image_path]);
assert!(prompt.contains("Explain"));
assert!(prompt.ends_with("[Image #1]"));
assert!(!prompt.ends_with("Explain"));
assert_eq!(prompt.to_string(), "Explain [Image #1]");
assert_eq!(
prompt.content_parts(),
vec![
TurnPromptContentPart::Text("Explain "),
TurnPromptContentPart::Attachment(&prompt.attachments[0]),
]
);
}
#[test]
fn test_turn_prompt_transcript_text_reuses_trailing_whitespace_for_missing_placeholders() {
let attachments = vec![TurnPromptAttachment {
local_image_path: PathBuf::from("/tmp/image-1.png"),
placeholder: "[Image #1]".to_string(),
}];
let cases = [("Review ", "Review [Image #1]"), ("", "[Image #1]")];
for (text, expected_transcript_text) in cases {
let prompt = TurnPrompt {
attachments: attachments.clone(),
text: text.to_string(),
text_source: TurnPromptTextSource::UserPrompt,
};
let transcript_text = prompt.transcript_text();
assert_eq!(transcript_text, expected_transcript_text);
}
}
#[test]
fn test_render_prompt_text_for_agent_keeps_at_without_lookup_query() {
let text = "meet @ noon and @!";
let agent_text = render_prompt_text_for_agent(text);
assert_eq!(agent_text, text);
}
#[test]
fn test_turn_prompt_append_trailing_attachments_uses_unique_placeholders() {
let data = r#"{"request":"Compare [Image #1]","literal":"[Task image #1]"}"#;
let mut prompt = TurnPrompt::from_agent_data(data.to_string());
let images = [
TurnPromptAttachment {
local_image_path: PathBuf::from("first.png"),
placeholder: "[Image #1]".to_string(),
},
TurnPromptAttachment {
local_image_path: PathBuf::from("second.png"),
placeholder: "[Image #1]".to_string(),
},
];
prompt.append_trailing_attachments(&images);
assert_eq!(
prompt.text,
format!(
"{data}\n\nTask input attachment 1: [#Task image #1]\n\nTask input attachment 2: \
[Task image #2]"
)
);
assert_eq!(
prompt.content_parts(),
[
TurnPromptContentPart::Text(&format!("{data}\n\nTask input attachment 1: ")),
TurnPromptContentPart::Attachment(&prompt.attachments[0]),
TurnPromptContentPart::Text("\n\nTask input attachment 2: "),
TurnPromptContentPart::Attachment(&prompt.attachments[1]),
]
);
assert_eq!(
prompt.attachments[0].local_image_path,
images[0].local_image_path
);
assert_eq!(
prompt.attachments[1].local_image_path,
images[1].local_image_path
);
assert_eq!(prompt.text_source, TurnPromptTextSource::AgentData);
}