Repository: block/goose
File: crates/goose/src/mcp_utils.rs (Rust)

Code before:
        let text = "Hello from GitHub!";
        let blob = base64::engine::general_purpose::STANDARD.encode(text.as_bytes());

        let resource = ResourceContents::BlobResourceContents {
            uri: "github://repo/file.md".to_string(),
            mime_type: Some("text/markdown".to_string()),
            blob,
            meta: None,
        };
        assert_eq!(extract_text_from_resource(&resource), "Hello from GitHub!");
    }

    #[test]
    fn test_extract_text_from_resource_blob_contents_binary() {
        let binary_data: Vec<u8> = vec![0xFF, 0xFE, 0x00, 0x01, 0x89, 0x50, 0x4E, 0x47];
        let blob = base64::engine::general_purpose::STANDARD.encode(&binary_data);

        let resource = ResourceContents::BlobResourceContents {
            uri: "file:///image.png".to_string(),
            mime_type: Some("image/png".to_string()),
            blob: blob.clone(),
            meta: None,
        };

        let result = extract_text_from_resource(&resource);
        assert!(result.contains("Binary content"));
        assert!(result.contains("image/png"));
    }

    #[test]
    fn test_extract_text_from_resource_blob_invalid_base64() {
        let resource = ResourceContents::BlobResourceContents {
            uri: "file:///test.txt".to_string(),
            mime_type: Some("text/plain".to_string()),
            blob: "not valid base64!!!".to_string(),
            meta: None,
        };
        assert_eq!(extract_text_from_resource(&resource), "not valid base64!!!");
    }

    #[test]
    fn test_extract_text_from_resource_blob_no_mime_type() {
        let binary_data: Vec<u8> = vec![0xFF, 0xFE];
        let blob = base64::engine::general_purpose::STANDARD.encode(&binary_data);

        let resource = ResourceContents::BlobResourceContents {
            uri: "file:///unknown".to_string(),
            mime_type: None,
            blob,
            meta: None,
        };

Review comment (bug):
since this is predictable and also doesn't include the actual raw bytes, just hte type and length, I think we can assert thew whole result right? this would also allow us to catch any incorrect length

Code after:
        };
        assert_eq!(extract_text_from_resource(&resource), expected);
    }

    #[test_case("Hello from GitHub!", "Hello from GitHub!" ; "utf8 markdown")]
    #[test_case("Simple text", "Simple text" ; "utf8 plain")]
    fn test_extract_text_from_blob_utf8(input: &str, expected: &str) {
        let blob = base64::engine::general_purpose::STANDARD.encode(input.as_bytes());
        let resource = ResourceContents::BlobResourceContents {
            uri: "github://repo/file.md".to_string(),
            mime_type: Some("text/markdown".to_string()),
            blob,
            meta: None,
        };
        assert_eq!(extract_text_from_resource(&resource), expected);
    }

    #[test]
    fn test_extract_text_from_blob_binary() {
        let binary_data: Vec<u8> = vec![0xFF, 0xFE, 0x00, 0x01, 0x89, 0x50, 0x4E, 0x47];
        let blob = base64::engine::general_purpose::STANDARD.encode(&binary_data);

        let resource = ResourceContents::BlobResourceContents {
            uri: "file:///image.png".to_string(),
            mime_type: Some("image/png".to_string()),
            blob,
            meta: None,
        };

        assert_eq!(
            extract_text_from_resource(&resource),
            "[Binary content (image/png) - 8 bytes]"
        );
    }

    #[test]
    fn test_extract_text_from_blob_binary_no_mime_type() {
        let binary_data: Vec<u8> = vec![0xFF, 0xFE];
        let blob = base64::engine::general_purpose::STANDARD.encode(&binary_data);

        let resource = ResourceContents::BlobResourceContents {
            uri: "file:///unknown".to_string(),
            mime_type: None,
            blob,
            meta: None,
        };

        assert_eq!(
            extract_text_from_resource(&resource),
            "[Binary content (application/octet-stream) - 2 bytes]"
        );