ag-protocol 0.20.0

Agentty is an ADE (Agentic Development Environment) for structured, controllable AI-assisted software development.
Documentation
//! Turn prompt tests for attachment placeholders, content splitting, and
//! agent-bound `@` lookup rewriting.

use std::path::PathBuf;

use crate::prompt::{
    TurnPrompt, TurnPromptAttachment, TurnPromptContentPart, TurnPromptTextSource,
    render_prompt_text_for_agent, split_turn_prompt_content,
};

#[test]
/// Ensures attachment JSON accepts either object key order while
/// preserving the complete named-field wire shape.
fn test_turn_prompt_attachment_json_uses_named_fields_independent_of_order() {
    // Arrange
    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"}"#;

    // Act
    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
    assert_eq!(deserialized_attachment, attachment);
    assert_eq!(
        serialized_value,
        serde_json::json!({
            "local_image_path": "/tmp/image-1.png",
            "placeholder": "[Image #1]",
        })
    );
}

#[test]
/// Ensures prompt text comparisons work with string slices on either side.
fn test_turn_prompt_compares_with_str_in_both_directions() {
    // Arrange
    let prompt = TurnPrompt::from("Review this change");

    // Act
    let prompt_matches = prompt == "Review this change";
    let text_matches = "Review this change" == prompt;

    // Assert
    assert!(prompt_matches);
    assert!(text_matches);
}

#[test]
/// Ensures transcript text keeps inline image placeholders unchanged.
fn test_turn_prompt_transcript_text_keeps_inline_placeholders() {
    // Arrange
    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,
    };

    // Act
    let transcript_text = prompt.transcript_text();

    // Assert
    assert_eq!(transcript_text, "Review [Image #1] carefully");
}

#[test]
/// Ensures agent-bound prompt text rewrites raw `@` lookups without
/// mutating transcript-facing text.
fn test_turn_prompt_agent_text_rewrites_user_at_lookups() {
    // Arrange
    let prompt = TurnPrompt::from("Review @src/main.rs and person@example.com");

    // Act
    let agent_text = prompt.agent_text();
    let transcript_text = prompt.transcript_text();

    // Assert
    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]
/// Ensures generated agent data bypasses user prompt `@` lookup rewriting.
fn test_turn_prompt_agent_text_preserves_agent_data_at_tokens() {
    // Arrange
    let prompt = TurnPrompt::from_agent_data(
        "Diff:\n```diff\n+@dataclass\n+class Config:\n+    pass\n```".to_string(),
    );

    // Act
    let agent_text = prompt.agent_text();

    // Assert
    assert!(agent_text.contains("+@dataclass"));
    assert!(!agent_text.contains("+\"dataclass\""));
}

#[test]
/// Ensures transcript text appends any attachment markers missing from the
/// text payload.
fn test_turn_prompt_transcript_text_appends_missing_placeholders() {
    // Arrange
    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,
    };

    // Act
    let transcript_text = prompt.transcript_text();

    // Assert
    assert_eq!(transcript_text, "Review [Image #1] [Image #2]");
}

#[test]
/// Ensures prompt content without attachments remains one text part.
fn test_split_turn_prompt_content_returns_text_without_attachments() {
    // Arrange
    let text = "Review this change";

    // Act
    let content_parts = split_turn_prompt_content(text, &[]);

    // Assert
    assert_eq!(content_parts, vec![TurnPromptContentPart::Text(text)]);
}

#[test]
/// Ensures prompt content parts follow placeholder order and keep orphaned
/// attachments at the end.
fn test_split_turn_prompt_content_orders_placeholders_and_appends_orphans() {
    // Arrange
    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(),
        },
    ];

    // Act
    let content_parts =
        split_turn_prompt_content("Compare [Image #2] with [Image #1] now", &attachments);

    // Assert
    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]
/// Ensures placeholders at the start and end of the text produce no empty
/// text parts.
fn test_split_turn_prompt_content_omits_empty_edge_text_parts() {
    // Arrange
    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(),
        },
    ];

    // Act
    let content_parts = split_turn_prompt_content("[Image #1] vs [Image #2]", &attachments);

    // Assert
    assert_eq!(
        content_parts,
        vec![
            TurnPromptContentPart::Attachment(&attachments[0]),
            TurnPromptContentPart::Text(" vs "),
            TurnPromptContentPart::Attachment(&attachments[1]),
        ]
    );
}

#[test]
/// Ensures prompt accessors and conversions expose text and attachment
/// state.
fn test_turn_prompt_accessors_and_conversions_expose_payload() {
    // Arrange
    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());

    // Act
    let copied_prompt = TurnPrompt::from(&prompt);
    let local_image_paths = prompt.local_image_paths().collect::<Vec<_>>();

    // Assert
    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]
/// Ensures missing placeholders are appended without an extra separator
/// after trailing whitespace or empty text.
fn test_turn_prompt_transcript_text_reuses_trailing_whitespace_for_missing_placeholders() {
    // Arrange
    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,
        };

        // Act
        let transcript_text = prompt.transcript_text();

        // Assert
        assert_eq!(transcript_text, expected_transcript_text);
    }
}

#[test]
/// Ensures an `@` followed by a non-lookup character stays unchanged.
fn test_render_prompt_text_for_agent_keeps_at_without_lookup_query() {
    // Arrange
    let text = "meet @ noon and @!";

    // Act
    let agent_text = render_prompt_text_for_agent(text);

    // Assert
    assert_eq!(agent_text, text);
}

#[test]
/// Ensures structured prompts reference each image after their data under a
/// placeholder absent from that data, even when composer markers repeat.
fn test_turn_prompt_append_trailing_attachments_uses_unique_placeholders() {
    // Arrange
    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(),
        },
    ];

    // Act
    prompt.append_trailing_attachments(&images);

    // Assert
    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);
}