|
| 1 | +use rmcp::model::{AnnotateAble, Content, Meta, RawContent, ResourceContents}; |
| 2 | +use serde_json::json; |
| 3 | + |
| 4 | +#[test] |
| 5 | +fn serialize_embedded_text_resource_with_meta() { |
| 6 | + // Inner contents meta |
| 7 | + let mut inner_meta = Meta::new(); |
| 8 | + inner_meta.insert("inner".to_string(), json!(2)); |
| 9 | + |
| 10 | + // Top-level embedded resource meta |
| 11 | + let mut top_meta = Meta::new(); |
| 12 | + top_meta.insert("top".to_string(), json!(1)); |
| 13 | + |
| 14 | + let content: Content = RawContent::Resource(rmcp::model::RawEmbeddedResource { |
| 15 | + meta: Some(top_meta), |
| 16 | + resource: ResourceContents::TextResourceContents { |
| 17 | + uri: "str://example".to_string(), |
| 18 | + mime_type: Some("text/plain".to_string()), |
| 19 | + text: "hello".to_string(), |
| 20 | + meta: Some(inner_meta), |
| 21 | + }, |
| 22 | + }) |
| 23 | + .no_annotation(); |
| 24 | + |
| 25 | + let v = serde_json::to_value(&content).unwrap(); |
| 26 | + |
| 27 | + let expected = json!({ |
| 28 | + "type": "resource", |
| 29 | + "_meta": {"top": 1}, |
| 30 | + "resource": { |
| 31 | + "uri": "str://example", |
| 32 | + "mimeType": "text/plain", |
| 33 | + "text": "hello", |
| 34 | + "_meta": {"inner": 2} |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + assert_eq!(v, expected); |
| 39 | +} |
| 40 | + |
| 41 | +#[test] |
| 42 | +fn serialize_embedded_text_resource_without_meta_omits_fields() { |
| 43 | + let content: Content = RawContent::Resource(rmcp::model::RawEmbeddedResource { |
| 44 | + meta: None, |
| 45 | + resource: ResourceContents::TextResourceContents { |
| 46 | + uri: "str://no-meta".to_string(), |
| 47 | + mime_type: Some("text/plain".to_string()), |
| 48 | + text: "hi".to_string(), |
| 49 | + meta: None, |
| 50 | + }, |
| 51 | + }) |
| 52 | + .no_annotation(); |
| 53 | + |
| 54 | + let v = serde_json::to_value(&content).unwrap(); |
| 55 | + |
| 56 | + assert_eq!(v.get("_meta"), None); |
| 57 | + let inner = v.get("resource").and_then(|r| r.as_object()).unwrap(); |
| 58 | + assert_eq!(inner.get("_meta"), None); |
| 59 | +} |
| 60 | + |
| 61 | +#[test] |
| 62 | +fn deserialize_embedded_text_resource_with_meta() { |
| 63 | + let raw = json!({ |
| 64 | + "type": "resource", |
| 65 | + "_meta": {"x": true}, |
| 66 | + "resource": { |
| 67 | + "uri": "str://from-json", |
| 68 | + "text": "ok", |
| 69 | + "_meta": {"y": 42} |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + let content: Content = serde_json::from_value(raw).unwrap(); |
| 74 | + |
| 75 | + let raw = match &content.raw { |
| 76 | + RawContent::Resource(er) => er, |
| 77 | + _ => panic!("expected resource"), |
| 78 | + }; |
| 79 | + |
| 80 | + // top-level _meta |
| 81 | + let top = raw.meta.as_ref().expect("top-level meta missing"); |
| 82 | + assert_eq!(top.get("x").unwrap(), &json!(true)); |
| 83 | + |
| 84 | + // inner contents _meta |
| 85 | + match &raw.resource { |
| 86 | + ResourceContents::TextResourceContents { |
| 87 | + meta, uri, text, .. |
| 88 | + } => { |
| 89 | + assert_eq!(uri, "str://from-json"); |
| 90 | + assert_eq!(text, "ok"); |
| 91 | + let inner = meta.as_ref().expect("inner meta missing"); |
| 92 | + assert_eq!(inner.get("y").unwrap(), &json!(42)); |
| 93 | + } |
| 94 | + _ => panic!("expected text resource contents"), |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +fn serialize_embedded_blob_resource_with_meta() { |
| 100 | + let mut inner_meta = Meta::new(); |
| 101 | + inner_meta.insert("blob_inner".to_string(), json!(true)); |
| 102 | + |
| 103 | + let mut top_meta = Meta::new(); |
| 104 | + top_meta.insert("blob_top".to_string(), json!("t")); |
| 105 | + |
| 106 | + let content: Content = RawContent::Resource(rmcp::model::RawEmbeddedResource { |
| 107 | + meta: Some(top_meta), |
| 108 | + resource: ResourceContents::BlobResourceContents { |
| 109 | + uri: "str://blob".to_string(), |
| 110 | + mime_type: Some("application/octet-stream".to_string()), |
| 111 | + blob: "Zm9v".to_string(), |
| 112 | + meta: Some(inner_meta), |
| 113 | + }, |
| 114 | + }) |
| 115 | + .no_annotation(); |
| 116 | + |
| 117 | + let v = serde_json::to_value(&content).unwrap(); |
| 118 | + |
| 119 | + assert_eq!(v.get("_meta").unwrap(), &json!({"blob_top": "t"})); |
| 120 | + let inner = v.get("resource").unwrap(); |
| 121 | + assert_eq!(inner.get("_meta").unwrap(), &json!({"blob_inner": true})); |
| 122 | +} |
0 commit comments