use pretty_assertions::assert_eq;
use rho_protocol::ContentBlock;
use rho_sdk::model::{self, ImageContent};
use super::{user_input, InvalidPrompt};
const PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";
const SVG: &str = "PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciLz4=";
fn resource_link(uri: &str, description: Option<&str>) -> ContentBlock {
ContentBlock::ResourceLink {
uri: uri.into(),
name: "notes".into(),
description: description.map(str::to_owned),
}
}
#[test]
fn prompt_blocks_reach_the_model_as_text_images_or_fenced_context() {
let prompt = [
ContentBlock::Text {
text: "hello".into(),
},
ContentBlock::Image {
mime_type: "image/png".into(),
data: PNG.into(),
},
ContentBlock::Resource {
uri: "file:///workspace/main.rs".into(),
text: "fn main() {}".into(),
},
resource_link("file:///workspace/notes.md", None),
resource_link("file:///workspace/plan.md", Some("the plan")),
ContentBlock::Unknown,
];
let input = user_input(&prompt).unwrap();
assert_eq!(
input.blocks(),
[
model::ContentBlock::Text("hello".into()),
model::ContentBlock::Image(ImageContent {
data: PNG.into(),
mime_type: "image/png".into(),
}),
model::ContentBlock::Text(
"```resource file:///workspace/main.rs\nfn main() {}\n```".into()
),
model::ContentBlock::Text("```resource file:///workspace/notes.md\nnotes\n```".into()),
model::ContentBlock::Text(
"```resource file:///workspace/plan.md\nthe plan\n```".into()
),
model::ContentBlock::Text("```context unsupported\n\n```".into()),
]
);
}
#[test]
fn prompt_images_must_be_the_image_they_declare() {
let cases = [
(
"not base64",
"image/png",
"not base64!",
Err(InvalidPrompt::ImageNotBase64 { index: 1 }),
),
(
"an svg",
"image/svg+xml",
SVG,
Err(InvalidPrompt::ImageUnrecognized { index: 1 }),
),
(
"a png declared jpeg",
"image/jpeg",
PNG,
Err(InvalidPrompt::ImageMismatch {
index: 1,
declared: "image/jpeg".into(),
actual: "image/png",
}),
),
(
"a png",
"image/png",
PNG,
Ok(vec![
model::ContentBlock::Text("look".into()),
model::ContentBlock::Image(ImageContent {
data: PNG.into(),
mime_type: "image/png".into(),
}),
]),
),
];
for (name, mime_type, data, expected) in cases {
let prompt = [
ContentBlock::Text {
text: "look".into(),
},
ContentBlock::Image {
mime_type: mime_type.into(),
data: data.into(),
},
];
assert_eq!(
user_input(&prompt).map(|input| input.blocks().to_vec()),
expected,
"{name}"
);
}
}
#[test]
fn fenced_context_outgrows_backticks_in_the_body_and_header() {
for (block, expected) in [
(
ContentBlock::Resource {
uri: "file:///workspace/readme.md".into(),
text: "```rust\nfn main() {}\n```".into(),
},
"````resource file:///workspace/readme.md\n```rust\nfn main() {}\n```\n````",
),
(
resource_link("file:///workspace/notes.md\n```\ninjected", None),
"````resource file:///workspace/notes.md\n```\ninjected\nnotes\n````",
),
] {
let input = user_input(&[block]).unwrap();
assert_eq!(input.blocks(), [model::ContentBlock::Text(expected.into())]);
}
}