Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(mcp-ui): cover widget cache round-trips and eviction (#7202)

oxoxDevdef24e76eca1
diff --git a/crates/openhuman-core/src/mcp/ui/cache.rs b/crates/openhuman-core/src/mcp/ui/cache.rs
index 306e31751e..31d09377ff 100644
--- a/crates/openhuman-core/src/mcp/ui/cache.rs
+++ b/crates/openhuman-core/src/mcp/ui/cache.rs
@@ -101,3 +101,7 @@ pub fn put_read(server_id: &str, uri: &str, resource: UiResource) {
},
);
}
+
+#[cfg(test)]
+#[path = "cache_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/mcp/ui/cache_tests.rs b/crates/openhuman-core/src/mcp/ui/cache_tests.rs
new file mode 100644
index 0000000000..5a5295781f
--- /dev/null
+++ b/crates/openhuman-core/src/mcp/ui/cache_tests.rs
@@ -0,0 +1,64 @@
+use super::*;
+use crate::mcp::ui::types::UiCsp;
+
+fn resource(html: &str) -> UiResource {
+ UiResource {
+ html: html.into(),
+ mime_type: "text/html".into(),
+ csp: UiCsp::default(),
+ permissions: None,
+ prefers_border: false,
+ }
+}
+
+#[test]
+fn inline_documents_round_trip_by_id() {
+ let id = put_inline(InlineEntry {
+ server_id: Some("cache-roundtrip".into()),
+ resource: resource("<b/>"),
+ });
+ let entry = get_inline(&id).expect("stored");
+ assert_eq!(entry.server_id.as_deref(), Some("cache-roundtrip"));
+ assert_eq!(entry.resource.html, "<b/>");
+ assert!(get_inline("absent").is_none());
+}
+
+#[test]
+fn reads_are_keyed_by_server_and_uri() {
+ put_read("cache-reads", "ui://a", resource("<a/>"));
+ assert_eq!(get_read("cache-reads", "ui://a").unwrap().html, "<a/>");
+ assert!(get_read("cache-reads", "ui://b").is_none());
+ assert!(get_read("other", "ui://a").is_none());
+}
+
+#[test]
+fn eviction_drops_the_oldest_entries_at_capacity() {
+ let mut map: HashMap<u32, Timed<u32>> = HashMap::new();
+ for key in 0..MAX_ENTRIES as u32 {
+ map.insert(
+ key,
+ Timed {
+ at: Instant::now(),
+ value: key,
+ },
+ );
+ }
+ evict(&mut map, Duration::from_secs(60));
+ assert!(map.len() < MAX_ENTRIES);
+ assert!(map.contains_key(&(MAX_ENTRIES as u32 - 1)));
+ assert!(!map.contains_key(&0));
+}
+
+#[test]
+fn eviction_drops_expired_entries() {
+ let mut map: HashMap<u32, Timed<u32>> = HashMap::new();
+ map.insert(
+ 1,
+ Timed {
+ at: Instant::now() - Duration::from_secs(10),
+ value: 1,
+ },
+ );
+ evict(&mut map, Duration::from_secs(1));
+ assert!(map.is_empty());
+}