use super::{HeldWords, text};
use crate::mcp::negotiate::Era;
use crate::mcp::tools::ToolResult;
use serde_json::json;
use std::num::NonZeroUsize;
type TestResult = Result<(), Box<dyn std::error::Error>>;
#[test]
fn held_words_are_successful_and_unchanged_in_both_eras() -> TestResult {
let held = HeldWords::new(
"draft-1",
"propose template: α\nsecond line",
bound()?,
bound()?,
)?;
for era in [Era::Legacy, Era::Modern] {
let result = held.tool_result().into_result(era);
assert_eq!(result["isError"], false);
assert_eq!(
result["structuredContent"],
json!({
"draft":"draft-1", "words":"propose template: α\nsecond line"
})
);
assert_eq!(
result["content"][0]["text"],
"held by draft draft-1: propose template: α\nsecond line"
);
}
assert!(!format!("{held:?}").contains("second line"));
assert!(!format!("{:?}", held.tool_result()).contains("second line"));
Ok(())
}
#[test]
fn ordinary_results_and_refusals_are_not_reinterpreted_as_held() {
for result in [
ToolResult::ok(json!({"words":"prepared"})),
ToolResult::failed("refused", json!({"draft":"draft-1","words":"prepared"})),
] {
assert_eq!(text(&result), None);
}
}
#[test]
fn held_boundary_refuses_missing_ids_and_oversized_utf8() -> TestResult {
let one = NonZeroUsize::MIN;
assert!(HeldWords::new("", "prepared", bound()?, bound()?).is_err());
assert!(HeldWords::new("draft\n1", "prepared", bound()?, bound()?).is_err());
assert!(HeldWords::new("draft-1", "é", bound()?, one).is_err());
Ok(())
}
fn bound() -> Result<NonZeroUsize, Box<dyn std::error::Error>> {
NonZeroUsize::new(1024).ok_or_else(|| "test bound is zero".into())
}
#[test]
fn native_held_site_pair_is_recognized_without_a_state_tag() -> TestResult {
assert!(
super::from_wire(
&json!({"state":"held","isError":true,"draft":"native-draft","words":"words"}),
bound()?,
bound()?
)
.is_err()
);
let answer = json!({"draft":"native-draft", "words":"the exact native words"});
let held =
super::from_wire(&answer, bound()?, bound()?)?.ok_or("native held pair was discarded")?;
assert_eq!(
text(&held).as_deref(),
Some("held by draft native-draft: the exact native words")
);
assert!(!held.is_error);
assert!(
super::from_wire(
&json!({"state":"refused","draft":"native-draft","words":"words"}),
bound()?,
bound()?
)?
.is_none()
);
Ok(())
}