use pretty_assertions::assert_eq;
use rho_sdk::model::{ContentBlock, ImageContent};
use super::{label, placeholder, sanitize_title, session_input};
#[test]
fn a_prompt_shows_as_plain_text_until_titled() {
let long = format!("{} tail", "word ".repeat(20));
let long_word = "x".repeat(90);
for (input, as_placeholder, as_label) in [
(
"# rho-ui: a long questionnaire hides its Submit button when zoomed in\n\n\
**The user** zoomed in.",
Some("rho-ui: a long questionnaire hides its Submit button when zoomed in"),
Some("rho-ui: a long questionnaire hides its Submit button when zoomed in The user…"),
),
(
"Fix __the__ *parser* in `lexer.rs`, see [issue 12](https://x.test/12)",
Some("Fix the parser in lexer.rs, see issue 12"),
Some("Fix the parser in lexer.rs, see issue 12"),
),
(
"keep snake_case_names, 2 * 3 * 4, and Vec<String>",
Some("keep snake_case_names, 2 * 3 * 4, and Vec<String>"),
Some("keep snake_case_names, 2 * 3 * 4, and Vec<String>"),
),
(
"\n \n```\nlet x = 1;\n```",
Some("let x = 1;"),
Some("let x = 1;"),
),
(" spaced \t out ", Some("spaced out"), Some("spaced out")),
("\u{7}\nfix bug", Some("fix bug"), Some("fix bug")),
(
"Fix the parser\nin lexer.rs",
Some("Fix the parser"),
Some("Fix the parser in lexer.rs"),
),
("```sh\n# build\n```", Some("# build"), Some("# build")),
(
"See https://github.com/acme/widgets/pull/12345#discussion_r1234567890-and-more-of-it",
Some(
"See https://github.com/acme/widgets/pull/12345#discussion_r1234567890-and-more-…",
),
Some(
"See https://github.com/acme/widgets/pull/12345#discussion_r1234567890-and-more-…",
),
),
(
"Fix __init__.py and crates/*/Cargo.toml, not **this**",
Some("Fix __init__.py and crates/*/Cargo.toml, not this"),
Some("Fix __init__.py and crates/*/Cargo.toml, not this"),
),
(
"Fix the **parser**. See _the note_.",
Some("Fix the parser. See the note."),
Some("Fix the parser. See the note."),
),
(
"Fix \u{1b}]0;owned\u{7} the build",
Some("Fix ]0;owned the build"),
Some("Fix ]0;owned the build"),
),
(
long.as_str(),
Some(
"word word word word word word word word word word word word word word word word…",
),
Some(
"word word word word word word word word word word word word word word word word…",
),
),
(
long_word.as_str(),
Some(&*format!("{}…", "x".repeat(79))),
Some(&*format!("{}…", "x".repeat(79))),
),
("## \n\n", None, None),
] {
assert_eq!(
(placeholder(input).as_deref(), label(input).as_deref()),
(as_placeholder, as_label),
"input: {input:?}"
);
}
}
#[test]
fn sanitize_title_cleans_labels_and_rejects_prose() {
let at_character_limit = "a".repeat(80);
let over_character_limit = "a".repeat(81);
for (input, expected) in [
(" \"Review the auth path\". ", Some("Review the auth path")),
("\"Implement resume picker.\"", Some("Implement resume picker")),
("\n\n# Draft\n", Some("Draft")),
(" ", None),
("Fix one two three four five six", Some("Fix one two three four five six")),
("Fix one two three four five six seven", None),
("Unable to inspect or modify the repository because no shell or file tools are available", None),
("Implement resume picker\nI will inspect the repository first", None),
(at_character_limit.as_str(), Some(at_character_limit.as_str())),
(over_character_limit.as_str(), None),
("Fix \u{1b}]0;owned\u{7} the build", None),
] {
assert_eq!(sanitize_title(input).as_deref(), expected, "input: {input:?}");
}
}
#[test]
fn a_session_is_named_from_its_first_messages_text_and_images() {
let image = ImageContent {
data: "aW1hZ2U=".into(),
mime_type: "image/png".into(),
};
for (label, message, source) in [
(
"text and image",
vec![
ContentBlock::Text("what is \"this\"?".into()),
ContentBlock::Image(image.clone()),
],
"First message:\n\nwhat is \"this\"?",
),
(
"image alone",
vec![ContentBlock::Image(image.clone())],
"First message:\n\n",
),
] {
let input = session_input(&message);
let material: Vec<String> = input
.blocks()
.iter()
.filter_map(|block| match block {
ContentBlock::Text(text) => text.split_once('\n'),
ContentBlock::Image(_) | ContentBlock::ToolCall(_) => None,
})
.map(|(_, quoted)| serde_json::from_str(quoted).unwrap())
.collect();
let images: Vec<_> = input
.blocks()
.iter()
.filter(|block| matches!(block, ContentBlock::Image(_)))
.cloned()
.collect();
assert_eq!(
(material, images),
(
vec![source.to_owned()],
vec![ContentBlock::Image(image.clone())]
),
"{label}"
);
}
}