| 1 | use serde_json::json; |
| 2 | use teams_core::{ |
| 3 | AdaptiveCard, CardActionKind, CardElement, ColumnWidth, ImageSize, Span, TextColor, TextSize, |
| 4 | VerticalAlignment, card_content_text, |
| 5 | }; |
| 6 | |
| 7 | fn digest_card() -> String { |
| 8 | json!({ |
| 9 | "type": "AdaptiveCard", |
| 10 | "msTeams": {"width": "Full"}, |
| 11 | "body": [ |
| 12 | {"type": "ColumnSet", "columns": [ |
| 13 | {"type": "Column", "width": "auto", "verticalContentAlignment": "center", "items": [ |
| 14 | {"type": "Image", "url": "https://img.example.com/person.png", "size": "small", "style": "person", "height": "32px"} |
| 15 | ]}, |
| 16 | {"type": "Column", "width": "stretch", "items": [ |
| 17 | {"type": "TextBlock", "size": "medium", "weight": "bolder", "wrap": true, "text": "Ada Example edited your page"} |
| 18 | ]} |
| 19 | ]}, |
| 20 | {"type": "ColumnSet", "separator": true, "columns": [ |
| 21 | {"type": "Column", "width": "40px", "items": [ |
| 22 | {"type": "Image", "url": "https://img.example.com/page.png", "width": "24px", "height": "24px"} |
| 23 | ]}, |
| 24 | {"type": "Column", "width": 2, "items": [ |
| 25 | {"type": "TextBlock", "weight": "bolder", "wrap": true, "color": "accent", "text": "[**Quarterly Plan**](https://example.com/wiki/plan) in [**Product Space**](https://example.com/wiki/space)"}, |
| 26 | {"type": "TextBlock", "spacing": "small", "isSubtle": true, "text": "Owned by: Ada Example"} |
| 27 | ]} |
| 28 | ]}, |
| 29 | {"type": "ActionSet", "actions": [ |
| 30 | {"type": "Action.OpenUrl", "title": "View page", "url": "https://example.com/wiki/plan"}, |
| 31 | {"type": "Action.OpenUrl", "title": "Unsafe", "url": "javascript:alert(1)"}, |
| 32 | {"type": "Action.Submit", "title": "Watch", "data": {"action": "watch"}}, |
| 33 | {"type": "Action.Submit", "title": "Hidden", "isVisible": false} |
| 34 | ]} |
| 35 | ] |
| 36 | }) |
| 37 | .to_string() |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn digest_card_keeps_layout_text_and_actions() { |
| 42 | let card = AdaptiveCard::parse(&digest_card()).unwrap(); |
| 43 | assert!(card.full_width); |
| 44 | assert_eq!(card.items.len(), 3); |
| 45 | |
| 46 | let CardElement::Columns(columns) = &card.items[0].element else { |
| 47 | panic!("first item is a column set"); |
| 48 | }; |
| 49 | assert_eq!(columns[0].width, ColumnWidth::Auto); |
| 50 | assert_eq!(columns[0].vertical_alignment, VerticalAlignment::Center); |
| 51 | let CardElement::Image(image) = &columns[0].items[0].element else { |
| 52 | panic!("avatar column holds an image"); |
| 53 | }; |
| 54 | assert!(image.person); |
| 55 | assert_eq!(image.size, ImageSize::Small); |
| 56 | assert_eq!(image.height, Some(32.)); |
| 57 | let CardElement::Text(title) = &columns[1].items[0].element else { |
| 58 | panic!("title column holds text"); |
| 59 | }; |
| 60 | assert!(title.bold && title.wrap); |
| 61 | assert_eq!(title.size, TextSize::Medium); |
| 62 | |
| 63 | assert!(card.items[1].separator); |
| 64 | let CardElement::Columns(columns) = &card.items[1].element else { |
| 65 | panic!("second item is a column set"); |
| 66 | }; |
| 67 | assert_eq!(columns[0].width, ColumnWidth::Pixels(40.)); |
| 68 | assert_eq!(columns[1].width, ColumnWidth::Weighted(2.)); |
| 69 | let CardElement::Text(line) = &columns[1].items[0].element else { |
| 70 | panic!("text expected"); |
| 71 | }; |
| 72 | assert_eq!(line.color, TextColor::Accent); |
| 73 | assert_eq!( |
| 74 | line.spans[0], |
| 75 | Span::Link { |
| 76 | url: "https://example.com/wiki/plan".to_owned(), |
| 77 | children: vec![Span::Bold(vec![Span::Text("Quarterly Plan".to_owned())])], |
| 78 | } |
| 79 | ); |
| 80 | let CardElement::Text(owner) = &columns[1].items[1].element else { |
| 81 | panic!("text expected"); |
| 82 | }; |
| 83 | assert!(owner.subtle); |
| 84 | |
| 85 | let CardElement::Actions(actions) = &card.items[2].element else { |
| 86 | panic!("third item is an action set"); |
| 87 | }; |
| 88 | assert_eq!(actions.len(), 3); |
| 89 | assert_eq!( |
| 90 | actions[0].kind, |
| 91 | CardActionKind::OpenUrl("https://example.com/wiki/plan".to_owned()) |
| 92 | ); |
| 93 | assert_eq!(actions[1].kind, CardActionKind::Unsupported); |
| 94 | assert_eq!(actions[2].title, "Watch"); |
| 95 | assert_eq!(actions[2].kind, CardActionKind::Unsupported); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn plain_text_drops_urls_images_and_buttons() { |
| 100 | assert_eq!( |
| 101 | card_content_text(&digest_card()).unwrap(), |
| 102 | "Ada Example edited your page\nQuarterly Plan in Product Space\nOwned by: Ada Example" |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn hidden_and_unknown_elements_are_skipped_but_fallback_text_stays() { |
| 108 | let card = json!({ |
| 109 | "body": [ |
| 110 | {"type": "TextBlock", "text": "hidden", "isVisible": false}, |
| 111 | {"type": "Carousel", "fallbackText": "Open the carousel elsewhere"}, |
| 112 | {"type": "Carousel"}, |
| 113 | {"type": "Image", "url": "http://insecure.example.com/a.png"}, |
| 114 | {"type": "TextBlock", "text": "shown"} |
| 115 | ] |
| 116 | }) |
| 117 | .to_string(); |
| 118 | assert_eq!( |
| 119 | card_content_text(&card).unwrap(), |
| 120 | "Open the carousel elsewhere\nshown" |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn containers_fact_sets_and_rich_text_are_parsed() { |
| 126 | let card = json!({ |
| 127 | "body": [ |
| 128 | {"type": "Container", "style": "emphasis", "items": [ |
| 129 | {"type": "RichTextBlock", "inlines": [ |
| 130 | "plain ", |
| 131 | {"type": "TextRun", "text": "bold", "weight": "bolder"}, |
| 132 | {"type": "TextRun", "text": " link", "selectAction": {"type": "Action.OpenUrl", "url": "https://example.com/x"}} |
| 133 | ]}, |
| 134 | {"type": "FactSet", "facts": [{"title": "Env", "value": "prod"}]}, |
| 135 | {"type": "ImageSet", "imageSize": "medium", "images": [ |
| 136 | {"type": "Image", "url": "https://img.example.com/a.png"} |
| 137 | ]} |
| 138 | ]} |
| 139 | ] |
| 140 | }) |
| 141 | .to_string(); |
| 142 | assert_eq!( |
| 143 | card_content_text(&card).unwrap(), |
| 144 | "plain bold link\nEnv: prod" |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn cards_without_content_are_none() { |
| 150 | assert!(AdaptiveCard::parse("not json").is_none()); |
| 151 | assert!(AdaptiveCard::parse(r#"{"body": []}"#).is_none()); |
| 152 | assert!(AdaptiveCard::parse("[1]").is_none()); |
| 153 | } |