| 1 | use serde_json::json; |
| 2 | use teams_core::{ |
| 3 | AdaptiveCard, CardAction, CardActionIcon, CardActionKind, CardElement, ColumnWidth, |
| 4 | ExecuteTrigger, IconSize, ImageSize, Span, TextColor, TextSize, ToggleTarget, |
| 5 | VerticalAlignment, card_content_text, |
| 6 | }; |
| 7 | |
| 8 | fn digest_card() -> String { |
| 9 | json!({ |
| 10 | "type": "AdaptiveCard", |
| 11 | "msTeams": {"width": "Full"}, |
| 12 | "body": [ |
| 13 | {"type": "ColumnSet", "columns": [ |
| 14 | {"type": "Column", "width": "auto", "verticalContentAlignment": "center", "items": [ |
| 15 | {"type": "Image", "url": "https://img.example.com/person.png", "size": "small", "style": "person", "height": "32px"} |
| 16 | ]}, |
| 17 | {"type": "Column", "width": "stretch", "items": [ |
| 18 | {"type": "TextBlock", "size": "medium", "weight": "bolder", "wrap": true, "text": "Ada Example edited your page"} |
| 19 | ]} |
| 20 | ]}, |
| 21 | {"type": "ColumnSet", "separator": true, "columns": [ |
| 22 | {"type": "Column", "width": "40px", "items": [ |
| 23 | {"type": "Image", "url": "https://img.example.com/page.png", "width": "24px", "height": "24px"} |
| 24 | ]}, |
| 25 | {"type": "Column", "width": 2, "items": [ |
| 26 | {"type": "TextBlock", "weight": "bolder", "wrap": true, "color": "accent", "text": "[**Quarterly Plan**](https://example.com/wiki/plan) in [**Product Space**](https://example.com/wiki/space)"}, |
| 27 | {"type": "TextBlock", "spacing": "small", "isSubtle": true, "text": "Owned by: Ada Example"} |
| 28 | ]} |
| 29 | ]}, |
| 30 | {"type": "ActionSet", "actions": [ |
| 31 | {"type": "Action.OpenUrl", "title": "View page", "url": "https://example.com/wiki/plan"}, |
| 32 | {"type": "Action.OpenUrl", "title": "Unsafe", "url": "javascript:alert(1)"}, |
| 33 | {"type": "Action.Submit", "title": "Watch", "data": {"action": "watch"}}, |
| 34 | {"type": "Action.Submit", "title": "Hidden", "isVisible": false} |
| 35 | ]} |
| 36 | ] |
| 37 | }) |
| 38 | .to_string() |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn digest_card_keeps_layout_text_and_actions() { |
| 43 | let card = AdaptiveCard::parse(&digest_card()).unwrap(); |
| 44 | assert!(card.full_width); |
| 45 | assert_eq!(card.items.len(), 3); |
| 46 | |
| 47 | let CardElement::Columns { columns, .. } = &card.items[0].element else { |
| 48 | panic!("first item is a column set"); |
| 49 | }; |
| 50 | assert_eq!(columns[0].width, ColumnWidth::Auto); |
| 51 | assert_eq!( |
| 52 | columns[0].layout.vertical_alignment, |
| 53 | VerticalAlignment::Center |
| 54 | ); |
| 55 | let CardElement::Image(image) = &columns[0].items[0].element else { |
| 56 | panic!("avatar column holds an image"); |
| 57 | }; |
| 58 | assert!(image.person); |
| 59 | assert_eq!(image.size, ImageSize::Small); |
| 60 | assert_eq!(image.height, Some(32.)); |
| 61 | let CardElement::Text(title) = &columns[1].items[0].element else { |
| 62 | panic!("title column holds text"); |
| 63 | }; |
| 64 | assert!(title.bold && title.wrap); |
| 65 | assert_eq!(title.size, TextSize::Medium); |
| 66 | |
| 67 | assert!(card.items[1].separator); |
| 68 | let CardElement::Columns { columns, .. } = &card.items[1].element else { |
| 69 | panic!("second item is a column set"); |
| 70 | }; |
| 71 | assert_eq!(columns[0].width, ColumnWidth::Pixels(40.)); |
| 72 | assert_eq!(columns[1].width, ColumnWidth::Weighted(2.)); |
| 73 | let CardElement::Text(line) = &columns[1].items[0].element else { |
| 74 | panic!("text expected"); |
| 75 | }; |
| 76 | assert_eq!(line.color, TextColor::Accent); |
| 77 | assert_eq!( |
| 78 | line.spans[0], |
| 79 | Span::Link { |
| 80 | url: "https://example.com/wiki/plan".to_owned(), |
| 81 | children: vec![Span::Bold(vec![Span::Text("Quarterly Plan".to_owned())])], |
| 82 | } |
| 83 | ); |
| 84 | let CardElement::Text(owner) = &columns[1].items[1].element else { |
| 85 | panic!("text expected"); |
| 86 | }; |
| 87 | assert!(owner.subtle); |
| 88 | |
| 89 | let CardElement::Actions(actions) = &card.items[2].element else { |
| 90 | panic!("third item is an action set"); |
| 91 | }; |
| 92 | assert_eq!(actions.len(), 3); |
| 93 | assert_eq!( |
| 94 | actions[0].kind, |
| 95 | CardActionKind::OpenUrl("https://example.com/wiki/plan".to_owned()) |
| 96 | ); |
| 97 | assert_eq!(actions[1].kind, CardActionKind::Unsupported); |
| 98 | assert_eq!(actions[2].title, "Watch"); |
| 99 | let CardActionKind::Submit(submit) = &actions[2].kind else { |
| 100 | panic!("watch is a submit action"); |
| 101 | }; |
| 102 | assert_eq!(submit.data, json!({"action": "watch"})); |
| 103 | assert!(actions[2].is_clickable()); |
| 104 | } |
| 105 | |
| 106 | fn card_actions(actions: serde_json::Value) -> Vec<CardAction> { |
| 107 | let card = json!({"body": [{"type": "TextBlock", "text": "x"}], "actions": actions}); |
| 108 | AdaptiveCard::parse(&card.to_string()).unwrap().actions |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn plain_submit_sends_its_data_as_a_message_back() { |
| 113 | let actions = card_actions(json!([ |
| 114 | {"type": "Action.Submit", "title": "Unwatch", "data": {"type": "unwatch", "page": "p1"}} |
| 115 | ])); |
| 116 | let payload = actions[0].invoke_payload().unwrap(); |
| 117 | assert_eq!(payload.name, "messageback"); |
| 118 | assert_eq!(payload.value, json!({"type": "unwatch", "page": "p1"})); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn task_fetch_submit_wraps_data_without_the_teams_settings() { |
| 123 | let actions = card_actions(json!([ |
| 124 | {"type": "Action.Submit", "title": "Bell", "data": {"cardType": "n", "msteams": {"type": "task/fetch"}}} |
| 125 | ])); |
| 126 | let payload = actions[0].invoke_payload().unwrap(); |
| 127 | assert_eq!(payload.name, "task/fetch"); |
| 128 | assert_eq!( |
| 129 | payload.value, |
| 130 | json!({"data": {"cardType": "n", "type": "task/fetch"}, "context": {"theme": "dark"}}) |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn message_back_and_im_back_send_the_teams_value() { |
| 136 | let actions = card_actions(json!([ |
| 137 | {"type": "Action.Submit", "title": "A", "data": {"msteams": {"type": "messageBack", "value": {"k": 1}}}}, |
| 138 | {"type": "Action.Submit", "title": "B", "data": {"msteams": {"type": "imBack"}, "k": 2}} |
| 139 | ])); |
| 140 | let first = actions[0].invoke_payload().unwrap(); |
| 141 | assert_eq!( |
| 142 | (first.name.as_str(), first.value), |
| 143 | ("messageback", json!({"k": 1})) |
| 144 | ); |
| 145 | let second = actions[1].invoke_payload().unwrap(); |
| 146 | assert_eq!(second.value, json!({"msteams": {"type": "imBack"}, "k": 2})); |
| 147 | } |
| 148 | |
| 149 | #[test] |
| 150 | fn execute_action_carries_verb_id_and_data() { |
| 151 | let actions = card_actions(json!([ |
| 152 | {"type": "Action.Execute", "id": "a1", "title": "Approve", "verb": "approve", "data": {"n": 3}}, |
| 153 | {"type": "Action.Execute", "title": "No verb"} |
| 154 | ])); |
| 155 | let payload = actions[0].invoke_payload().unwrap(); |
| 156 | assert_eq!(payload.name, "adaptiveCard/action"); |
| 157 | assert_eq!( |
| 158 | payload.value, |
| 159 | json!({ |
| 160 | "action": {"type": "Action.Execute", "id": "a1", "verb": "approve", "data": {"n": 3}}, |
| 161 | "trigger": "manual" |
| 162 | }) |
| 163 | ); |
| 164 | assert_eq!(actions[1].kind, CardActionKind::Unsupported); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn show_card_and_toggle_actions_are_parsed_locally() { |
| 169 | let actions = card_actions(json!([ |
| 170 | {"type": "Action.ShowCard", "title": "More", "card": {"body": [{"type": "TextBlock", "text": "nested"}]}}, |
| 171 | {"type": "Action.ToggleVisibility", "title": "Toggle", "targetElements": ["a", {"elementId": "b", "isVisible": true}]}, |
| 172 | {"type": "Action.ToggleVisibility", "title": "Empty", "targetElements": []} |
| 173 | ])); |
| 174 | let CardActionKind::ShowCard(nested) = &actions[0].kind else { |
| 175 | panic!("show card expected"); |
| 176 | }; |
| 177 | assert_eq!(nested.plain_text().as_deref(), Some("nested")); |
| 178 | assert!(actions[0].invoke_payload().is_none()); |
| 179 | assert_eq!( |
| 180 | actions[1].kind, |
| 181 | CardActionKind::ToggleVisibility(vec![ |
| 182 | ToggleTarget { |
| 183 | element_id: "a".to_owned(), |
| 184 | visible: None |
| 185 | }, |
| 186 | ToggleTarget { |
| 187 | element_id: "b".to_owned(), |
| 188 | visible: Some(true) |
| 189 | }, |
| 190 | ]) |
| 191 | ); |
| 192 | assert_eq!(actions[2].kind, CardActionKind::Unsupported); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn disabled_actions_stay_disabled_and_hidden_ones_need_an_id() { |
| 197 | let actions = card_actions(json!([ |
| 198 | {"type": "Action.Submit", "title": "Off", "isEnabled": false, "data": {}}, |
| 199 | {"type": "Action.Submit", "title": "Gone", "isVisible": false}, |
| 200 | {"type": "Action.Submit", "title": "Later", "id": "later", "isVisible": false} |
| 201 | ])); |
| 202 | assert_eq!(actions.len(), 2); |
| 203 | assert!(!actions[0].is_clickable()); |
| 204 | assert!(!actions[1].visible); |
| 205 | assert_eq!(actions[1].id.as_deref(), Some("later")); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn toggled_items_keep_their_id_and_stay_out_of_the_text() { |
| 210 | let card = json!({"body": [ |
| 211 | {"type": "TextBlock", "text": "shown"}, |
| 212 | {"type": "TextBlock", "id": "details", "isVisible": false, "text": "secret"} |
| 213 | ]}) |
| 214 | .to_string(); |
| 215 | let parsed = AdaptiveCard::parse(&card).unwrap(); |
| 216 | assert_eq!(parsed.items.len(), 2); |
| 217 | assert_eq!(parsed.items[1].id.as_deref(), Some("details")); |
| 218 | assert!(!parsed.items[1].visible); |
| 219 | assert_eq!(parsed.plain_text().as_deref(), Some("shown")); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn plain_text_drops_urls_images_and_buttons() { |
| 224 | assert_eq!( |
| 225 | card_content_text(&digest_card()).unwrap(), |
| 226 | "Ada Example edited your page\nQuarterly Plan in Product Space\nOwned by: Ada Example" |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | #[test] |
| 231 | fn hidden_and_unknown_elements_are_skipped_but_fallback_text_stays() { |
| 232 | let card = json!({ |
| 233 | "body": [ |
| 234 | {"type": "TextBlock", "text": "hidden", "isVisible": false}, |
| 235 | {"type": "Carousel", "fallbackText": "Open the carousel elsewhere"}, |
| 236 | {"type": "Carousel"}, |
| 237 | {"type": "Image", "url": "http://insecure.example.com/a.png"}, |
| 238 | {"type": "TextBlock", "text": "shown"} |
| 239 | ] |
| 240 | }) |
| 241 | .to_string(); |
| 242 | assert_eq!( |
| 243 | card_content_text(&card).unwrap(), |
| 244 | "Open the carousel elsewhere\nshown" |
| 245 | ); |
| 246 | } |
| 247 | |
| 248 | #[test] |
| 249 | fn containers_fact_sets_and_rich_text_are_parsed() { |
| 250 | let card = json!({ |
| 251 | "body": [ |
| 252 | {"type": "Container", "style": "emphasis", "items": [ |
| 253 | {"type": "RichTextBlock", "inlines": [ |
| 254 | "plain ", |
| 255 | {"type": "TextRun", "text": "bold", "weight": "bolder"}, |
| 256 | {"type": "TextRun", "text": " link", "selectAction": {"type": "Action.OpenUrl", "url": "https://example.com/x"}} |
| 257 | ]}, |
| 258 | {"type": "FactSet", "facts": [{"title": "Env", "value": "prod"}]}, |
| 259 | {"type": "ImageSet", "imageSize": "medium", "images": [ |
| 260 | {"type": "Image", "url": "https://img.example.com/a.png"} |
| 261 | ]} |
| 262 | ]} |
| 263 | ] |
| 264 | }) |
| 265 | .to_string(); |
| 266 | assert_eq!( |
| 267 | card_content_text(&card).unwrap(), |
| 268 | "plain bold link\nEnv: prod" |
| 269 | ); |
| 270 | } |
| 271 | |
| 272 | #[test] |
| 273 | fn cards_without_content_are_none() { |
| 274 | assert!(AdaptiveCard::parse("not json").is_none()); |
| 275 | assert!(AdaptiveCard::parse(r#"{"body": []}"#).is_none()); |
| 276 | assert!(AdaptiveCard::parse("[1]").is_none()); |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | fn element_visibility_covers_items_actions_and_nested_cards() { |
| 281 | let card = json!({ |
| 282 | "body": [ |
| 283 | {"type": "TextBlock", "id": "title", "text": "t"}, |
| 284 | {"type": "Container", "items": [ |
| 285 | {"type": "TextBlock", "id": "details", "isVisible": false, "text": "d"} |
| 286 | ]}, |
| 287 | {"type": "ActionSet", "actions": [ |
| 288 | {"type": "Action.Submit", "id": "later", "isVisible": false, "title": "Later"}, |
| 289 | {"type": "Action.ShowCard", "title": "More", "card": {"body": [ |
| 290 | {"type": "TextBlock", "id": "nested", "isVisible": false, "text": "n"} |
| 291 | ]}} |
| 292 | ]} |
| 293 | ] |
| 294 | }) |
| 295 | .to_string(); |
| 296 | let visibility = AdaptiveCard::parse(&card).unwrap().element_visibility(); |
| 297 | assert_eq!(visibility.len(), 4); |
| 298 | assert!(visibility["title"]); |
| 299 | assert!(!visibility["details"]); |
| 300 | assert!(!visibility["later"]); |
| 301 | assert!(!visibility["nested"]); |
| 302 | } |
| 303 | |
| 304 | fn parse(card: serde_json::Value) -> AdaptiveCard { |
| 305 | AdaptiveCard::parse(&card.to_string()).unwrap() |
| 306 | } |
| 307 | |
| 308 | fn refresh_card(user_ids: Option<serde_json::Value>) -> AdaptiveCard { |
| 309 | let mut refresh = |
| 310 | json!({"action": {"type": "Action.Execute", "verb": "refreshView", "data": {"k": 1}}}); |
| 311 | if let Some(user_ids) = user_ids { |
| 312 | refresh["userIds"] = user_ids; |
| 313 | } |
| 314 | parse(json!({"body": [{"type": "TextBlock", "text": "x"}], "refresh": refresh})) |
| 315 | } |
| 316 | |
| 317 | #[test] |
| 318 | fn refresh_parses_the_execute_action_and_user_ids() { |
| 319 | let card = refresh_card(Some(json!(["8:orgid:ABC"]))); |
| 320 | let refresh = card.refresh.unwrap(); |
| 321 | assert_eq!(refresh.action.verb, "refreshView"); |
| 322 | assert_eq!(refresh.user_ids, Some(vec!["8:orgid:ABC".to_owned()])); |
| 323 | assert!(parse(json!({"body": [{"type": "TextBlock", "text": "x"}], "refresh": {"action": {"type": "Action.OpenUrl", "url": "https://a.example"}}})).refresh.is_none()); |
| 324 | } |
| 325 | |
| 326 | #[test] |
| 327 | fn refresh_runs_for_small_chats_without_user_ids() { |
| 328 | let refresh = refresh_card(None).refresh.unwrap(); |
| 329 | assert!(refresh.runs_automatically(Some(2), None)); |
| 330 | assert!(refresh.runs_automatically(Some(60), Some("8:orgid:me"))); |
| 331 | assert!(!refresh.runs_automatically(Some(61), Some("8:orgid:me"))); |
| 332 | assert!(!refresh.runs_automatically(None, Some("8:orgid:me"))); |
| 333 | } |
| 334 | |
| 335 | #[test] |
| 336 | fn refresh_with_user_ids_runs_only_for_listed_users() { |
| 337 | let refresh = refresh_card(Some(json!(["8:orgid:ABC-1", "8:orgid:other"]))) |
| 338 | .refresh |
| 339 | .unwrap(); |
| 340 | assert!(refresh.runs_automatically(Some(500), Some("abc-1"))); |
| 341 | assert!(refresh.runs_automatically(Some(500), Some("8:orgid:abc-1"))); |
| 342 | assert!(!refresh.runs_automatically(Some(2), Some("8:orgid:nobody"))); |
| 343 | assert!(!refresh.runs_automatically(Some(2), None)); |
| 344 | } |
| 345 | |
| 346 | #[test] |
| 347 | fn execute_payload_carries_the_trigger() { |
| 348 | let refresh = refresh_card(None).refresh.unwrap(); |
| 349 | assert_eq!(refresh.action.payload().value["trigger"], "manual"); |
| 350 | let automatic = refresh.action.payload_for(ExecuteTrigger::Automatic); |
| 351 | assert_eq!(automatic.name, "adaptiveCard/action"); |
| 352 | assert_eq!(automatic.value["trigger"], "automatic"); |
| 353 | } |
| 354 | |
| 355 | #[test] |
| 356 | fn tab_info_and_sign_in_submits_open_a_url() { |
| 357 | let actions = card_actions(json!([ |
| 358 | {"type": "Action.Submit", "title": "Tab", "data": {"msteams": {"type": "invoke", "value": {"type": "tab/tabInfoAction", "tabInfo": {"contentUrl": "https://content.example/c", "websiteUrl": "https://site.example/w", "name": "n", "entityId": "e"}}}}}, |
| 359 | {"type": "Action.Submit", "title": "Tab content", "data": {"msteams": {"type": "invoke", "value": {"type": "tab/tabInfoAction", "tabInfo": {"contentUrl": "https://content.example/c"}}}}}, |
| 360 | {"type": "Action.Submit", "title": "Tab bad", "data": {"msteams": {"type": "invoke", "value": {"type": "tab/tabInfoAction", "tabInfo": {"websiteUrl": "javascript:x"}}}}}, |
| 361 | {"type": "Action.Submit", "title": "Sign in", "data": {"msteams": {"type": "signin", "value": "https://login.example/start"}}} |
| 362 | ])); |
| 363 | assert_eq!( |
| 364 | actions[0].kind, |
| 365 | CardActionKind::OpenUrl("https://site.example/w".into()) |
| 366 | ); |
| 367 | assert_eq!( |
| 368 | actions[1].kind, |
| 369 | CardActionKind::OpenUrl("https://content.example/c".into()) |
| 370 | ); |
| 371 | assert_eq!(actions[2].kind, CardActionKind::Unsupported); |
| 372 | assert_eq!( |
| 373 | actions[3].kind, |
| 374 | CardActionKind::OpenUrl("https://login.example/start".into()) |
| 375 | ); |
| 376 | } |
| 377 | |
| 378 | #[test] |
| 379 | fn other_invoke_submits_send_the_value_named_by_its_type() { |
| 380 | let actions = card_actions(json!([ |
| 381 | {"type": "Action.Submit", "title": "Go", "data": {"msteams": {"type": "invoke", "value": {"type": "custom/thing", "payload": 1}}}} |
| 382 | ])); |
| 383 | let payload = actions[0].invoke_payload().unwrap(); |
| 384 | assert_eq!(payload.name, "custom/thing"); |
| 385 | assert_eq!(payload.value, json!({"type": "custom/thing", "payload": 1})); |
| 386 | } |
| 387 | |
| 388 | #[test] |
| 389 | fn add_app_submits_stay_as_unavailable_buttons() { |
| 390 | let actions = card_actions(json!([ |
| 391 | {"type": "Action.Submit", "title": "Add", "data": {"msteams": {"type": "invoke", "value": {"type": "appInstallToConversation"}}}}, |
| 392 | {"type": "Action.Submit", "title": "Add 2", "data": {"msteams": {"type": "addAppToConversation"}}} |
| 393 | ])); |
| 394 | for action in &actions { |
| 395 | assert_eq!( |
| 396 | action.kind, |
| 397 | CardActionKind::Unavailable("Adding apps is not supported") |
| 398 | ); |
| 399 | assert!(!action.is_clickable()); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | #[test] |
| 404 | fn select_actions_are_parsed_on_card_image_container_and_column() { |
| 405 | let select = json!({"type": "Action.OpenUrl", "url": "https://a.example"}); |
| 406 | let card = parse(json!({ |
| 407 | "selectAction": select, |
| 408 | "body": [ |
| 409 | {"type": "Image", "url": "https://img.example/a.png", "selectAction": {"type": "Action.Submit", "data": {"a": 1}}}, |
| 410 | {"type": "Container", "selectAction": {"type": "Action.ToggleVisibility", "targetElements": ["x"]}, "items": [{"type": "TextBlock", "text": "t"}]}, |
| 411 | {"type": "ColumnSet", "columns": [{"type": "Column", "selectAction": {"type": "Action.Execute", "verb": "v"}, "items": [{"type": "TextBlock", "text": "t"}]}]}, |
| 412 | {"type": "Image", "url": "https://img.example/b.png", "selectAction": {"type": "Action.ShowCard", "card": {"body": [{"type": "TextBlock", "text": "t"}]}}} |
| 413 | ] |
| 414 | })); |
| 415 | assert!(matches!( |
| 416 | card.select_action.unwrap().kind, |
| 417 | CardActionKind::OpenUrl(_) |
| 418 | )); |
| 419 | let CardElement::Image(image) = &card.items[0].element else { |
| 420 | panic!("image") |
| 421 | }; |
| 422 | assert!(matches!( |
| 423 | image.select_action.as_ref().unwrap().kind, |
| 424 | CardActionKind::Submit(_) |
| 425 | )); |
| 426 | let CardElement::Container { select_action, .. } = &card.items[1].element else { |
| 427 | panic!("container") |
| 428 | }; |
| 429 | assert!(matches!( |
| 430 | select_action.as_ref().unwrap().kind, |
| 431 | CardActionKind::ToggleVisibility(_) |
| 432 | )); |
| 433 | let CardElement::Columns { columns, .. } = &card.items[2].element else { |
| 434 | panic!("columns") |
| 435 | }; |
| 436 | assert!(matches!( |
| 437 | columns[0].select_action.as_ref().unwrap().kind, |
| 438 | CardActionKind::Execute(_) |
| 439 | )); |
| 440 | let CardElement::Image(image) = &card.items[3].element else { |
| 441 | panic!("image") |
| 442 | }; |
| 443 | assert!(image.select_action.is_none()); |
| 444 | } |
| 445 | |
| 446 | #[test] |
| 447 | fn media_takes_the_first_web_source_and_poster() { |
| 448 | let card = parse(json!({"body": [ |
| 449 | {"type": "Media", "poster": "https://img.example/p.png", "altText": "Intro", "sources": [ |
| 450 | {"mimeType": "video/mp4", "url": "ftp://x"}, {"mimeType": "video/mp4", "url": "https://v.example/a.mp4"}, {"url": "https://v.example/b.mp4"} |
| 451 | ]}, |
| 452 | {"type": "Media", "sources": []}, |
| 453 | {"type": "TextBlock", "text": "after"} |
| 454 | ]})); |
| 455 | assert_eq!(card.items.len(), 2); |
| 456 | let CardElement::Media(media) = &card.items[0].element else { |
| 457 | panic!("media") |
| 458 | }; |
| 459 | assert_eq!(media.source_url, "https://v.example/a.mp4"); |
| 460 | assert_eq!( |
| 461 | media.poster_url.as_deref(), |
| 462 | Some("https://img.example/p.png") |
| 463 | ); |
| 464 | assert_eq!(media.alt_text.as_deref(), Some("Intro")); |
| 465 | } |
| 466 | |
| 467 | #[test] |
| 468 | fn icon_elements_and_action_icons_are_parsed() { |
| 469 | let card = parse(json!({ |
| 470 | "body": [ |
| 471 | {"type": "Icon", "name": "Calendar", "size": "xxLarge", "color": "accent", "style": "Filled", "selectAction": {"type": "Action.OpenUrl", "url": "https://a.example"}}, |
| 472 | {"type": "Icon", "name": "Mail"} |
| 473 | ], |
| 474 | "actions": [ |
| 475 | {"type": "Action.OpenUrl", "title": "A", "url": "https://a.example", "iconUrl": "icon:Send,Filled"}, |
| 476 | {"type": "Action.OpenUrl", "title": "B", "url": "https://a.example", "iconUrl": "https://img.example/i.png"}, |
| 477 | {"type": "Action.OpenUrl", "title": "C", "url": "https://a.example", "iconUrl": "data:image/png;base64,AA"} |
| 478 | ] |
| 479 | })); |
| 480 | let CardElement::Icon(icon) = &card.items[0].element else { |
| 481 | panic!("icon") |
| 482 | }; |
| 483 | assert_eq!( |
| 484 | (icon.name.as_str(), icon.size, icon.color, icon.filled), |
| 485 | ( |
| 486 | "Calendar", |
| 487 | IconSize::ExtraExtraLarge, |
| 488 | TextColor::Accent, |
| 489 | true |
| 490 | ) |
| 491 | ); |
| 492 | assert!(icon.select_action.is_some()); |
| 493 | let CardElement::Icon(plain) = &card.items[1].element else { |
| 494 | panic!("icon") |
| 495 | }; |
| 496 | assert_eq!((plain.size, plain.filled), (IconSize::Standard, false)); |
| 497 | assert_eq!( |
| 498 | card.actions[0].icon, |
| 499 | Some(CardActionIcon::Named("Send".into())) |
| 500 | ); |
| 501 | assert_eq!( |
| 502 | card.actions[1].icon, |
| 503 | Some(CardActionIcon::Url("https://img.example/i.png".into())) |
| 504 | ); |
| 505 | assert_eq!(card.actions[2].icon, None); |
| 506 | } |
| 507 | |
| 508 | #[test] |
| 509 | fn mentions_and_italic_survive_in_text_fact_and_rich_text() { |
| 510 | let card = parse(json!({"body": [ |
| 511 | {"type": "TextBlock", "text": "Hi <at>Ada</at>, this is _soft_ and snake_case_x"}, |
| 512 | {"type": "FactSet", "facts": [{"title": "Owner", "value": "<at>Bob</at>"}]}, |
| 513 | {"type": "RichTextBlock", "inlines": [{"type": "TextRun", "text": "cc <at>Cy</at>", "weight": "bolder"}]} |
| 514 | ]})); |
| 515 | let CardElement::Text(text) = &card.items[0].element else { |
| 516 | panic!("text") |
| 517 | }; |
| 518 | assert!(text.spans.contains(&Span::Mention { |
| 519 | name: "Ada".into(), |
| 520 | id: None |
| 521 | })); |
| 522 | assert!( |
| 523 | text.spans |
| 524 | .contains(&Span::Italic(vec![Span::Text("soft".into())])) |
| 525 | ); |
| 526 | let CardElement::Facts(facts) = &card.items[1].element else { |
| 527 | panic!("facts") |
| 528 | }; |
| 529 | assert_eq!( |
| 530 | facts[0].value, |
| 531 | vec![Span::Mention { |
| 532 | name: "Bob".into(), |
| 533 | id: None |
| 534 | }] |
| 535 | ); |
| 536 | let CardElement::Text(rich) = &card.items[2].element else { |
| 537 | panic!("rich") |
| 538 | }; |
| 539 | assert_eq!( |
| 540 | rich.spans, |
| 541 | vec![Span::Bold(vec![ |
| 542 | Span::Text("cc ".into()), |
| 543 | Span::Mention { |
| 544 | name: "Cy".into(), |
| 545 | id: None |
| 546 | } |
| 547 | ])] |
| 548 | ); |
| 549 | } |