| 1 | use chrono::FixedOffset; |
| 2 | use serde_json::json; |
| 3 | use teams_core::{ |
| 4 | AdaptiveCard, BadgeAppearance, BadgeShape, BadgeStyle, CardElement, ColumnWidth, |
| 5 | ContainerStyle, FontSize, HorizontalAlignment, IconPosition, LabelPosition, Span, TargetWidth, |
| 6 | TextColor, TextSize, VerticalAlignment, WidthClass, format_card_dates_in, split_overflow, |
| 7 | }; |
| 8 | |
| 9 | fn parse(card: serde_json::Value) -> AdaptiveCard { |
| 10 | AdaptiveCard::parse(&card.to_string()).unwrap() |
| 11 | } |
| 12 | |
| 13 | fn first_element(body: serde_json::Value) -> CardElement { |
| 14 | parse(json!({"body": [body]})).items.remove(0).element |
| 15 | } |
| 16 | |
| 17 | #[test] |
| 18 | fn text_blocks_default_to_one_truncated_line() { |
| 19 | let CardElement::Text(text) = |
| 20 | first_element(json!({"type": "TextBlock", "text": "a long line"})) |
| 21 | else { |
| 22 | panic!("text") |
| 23 | }; |
| 24 | assert!(!text.wrap); |
| 25 | assert_eq!(text.max_lines, None); |
| 26 | assert_eq!(text.alignment, None); |
| 27 | assert!(!text.monospace); |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn text_block_layout_properties_are_parsed() { |
| 32 | let CardElement::Text(text) = first_element(json!({ |
| 33 | "type": "TextBlock", "text": "x", "wrap": true, "maxLines": 2, |
| 34 | "horizontalAlignment": "Center", "fontType": "Monospace" |
| 35 | })) else { |
| 36 | panic!("text") |
| 37 | }; |
| 38 | assert!(text.wrap && text.monospace); |
| 39 | assert_eq!(text.max_lines, Some(2)); |
| 40 | assert_eq!(text.alignment, Some(HorizontalAlignment::Center)); |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn heading_style_is_bold_and_larger_unless_sized() { |
| 45 | let CardElement::Text(heading) = |
| 46 | first_element(json!({"type": "TextBlock", "text": "h", "style": "heading"})) |
| 47 | else { |
| 48 | panic!("text") |
| 49 | }; |
| 50 | assert!(heading.bold); |
| 51 | assert_eq!(heading.size, TextSize::Large); |
| 52 | let CardElement::Text(sized) = first_element( |
| 53 | json!({"type": "TextBlock", "text": "h", "style": "heading", "size": "small"}), |
| 54 | ) else { |
| 55 | panic!("text") |
| 56 | }; |
| 57 | assert_eq!(sized.size, TextSize::Small); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn text_runs_carry_highlight_color_size_and_monospace() { |
| 62 | let CardElement::Text(text) = first_element(json!({"type": "RichTextBlock", "inlines": [ |
| 63 | {"type": "TextRun", "text": "mark", "highlight": true}, |
| 64 | {"type": "TextRun", "text": "good", "color": "good", "size": "large", "fontType": "monospace"}, |
| 65 | {"type": "TextRun", "text": "quiet", "isSubtle": true} |
| 66 | ]})) else { |
| 67 | panic!("text") |
| 68 | }; |
| 69 | let Span::Colored { |
| 70 | color, background, .. |
| 71 | } = &text.spans[0] |
| 72 | else { |
| 73 | panic!("highlight is a colored span") |
| 74 | }; |
| 75 | assert!(color.is_some() && background.is_some()); |
| 76 | let Span::Colored { |
| 77 | color: Some(_), |
| 78 | background: None, |
| 79 | children, |
| 80 | } = &text.spans[1] |
| 81 | else { |
| 82 | panic!("colored run") |
| 83 | }; |
| 84 | assert_eq!( |
| 85 | children[0], |
| 86 | Span::Sized(FontSize::Pixels(18), vec![Span::Code("good".to_owned())]) |
| 87 | ); |
| 88 | assert!(matches!( |
| 89 | &text.spans[2], |
| 90 | Span::Colored { |
| 91 | color: Some(_), |
| 92 | background: None, |
| 93 | .. |
| 94 | } |
| 95 | )); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn images_carry_alt_text_alignment_and_background() { |
| 100 | let CardElement::Image(image) = first_element(json!({ |
| 101 | "type": "Image", "url": "http://img.example/a.png", "altText": "Logo", |
| 102 | "horizontalAlignment": "right", "backgroundColor": "#80102030" |
| 103 | })) else { |
| 104 | panic!("image") |
| 105 | }; |
| 106 | assert_eq!(image.url, "http://img.example/a.png"); |
| 107 | assert_eq!(image.alt_text.as_deref(), Some("Logo")); |
| 108 | assert_eq!(image.alignment, HorizontalAlignment::Right); |
| 109 | assert_eq!(image.background_color, Some(0x10203080)); |
| 110 | let CardElement::Image(opaque) = first_element( |
| 111 | json!({"type": "Image", "url": "https://img.example/a.png", "backgroundColor": "#102030"}), |
| 112 | ) else { |
| 113 | panic!("image") |
| 114 | }; |
| 115 | assert_eq!(opaque.background_color, Some(0x102030ff)); |
| 116 | assert_eq!(opaque.alignment, HorizontalAlignment::Left); |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn containers_columns_and_the_card_keep_layout_properties() { |
| 121 | let card = parse(json!({ |
| 122 | "minHeight": "120px", |
| 123 | "verticalContentAlignment": "center", |
| 124 | "backgroundImage": {"url": "https://img.example/bg.png", "fillMode": "repeat"}, |
| 125 | "body": [ |
| 126 | {"type": "Container", "style": "good", "bleed": true, "rtl": true, "minHeight": "80px", |
| 127 | "verticalContentAlignment": "bottom", "backgroundImage": "https://img.example/c.png", |
| 128 | "height": "stretch", |
| 129 | "items": [{"type": "TextBlock", "text": "x"}]}, |
| 130 | {"type": "ColumnSet", "style": "emphasis", "columns": [ |
| 131 | {"type": "Column", "style": "accent", "minHeight": "40px", "items": [{"type": "TextBlock", "text": "y"}]} |
| 132 | ]} |
| 133 | ] |
| 134 | })); |
| 135 | assert_eq!(card.layout.min_height, Some(120.)); |
| 136 | assert_eq!(card.layout.vertical_alignment, VerticalAlignment::Center); |
| 137 | assert_eq!( |
| 138 | card.layout.background_image.as_deref(), |
| 139 | Some("https://img.example/bg.png") |
| 140 | ); |
| 141 | assert!(card.items[0].stretch); |
| 142 | let CardElement::Container { layout, .. } = &card.items[0].element else { |
| 143 | panic!("container") |
| 144 | }; |
| 145 | assert_eq!(layout.style, ContainerStyle::Good); |
| 146 | assert!(layout.bleed && layout.rtl); |
| 147 | assert_eq!(layout.min_height, Some(80.)); |
| 148 | assert_eq!(layout.vertical_alignment, VerticalAlignment::Bottom); |
| 149 | assert_eq!( |
| 150 | layout.background_image.as_deref(), |
| 151 | Some("https://img.example/c.png") |
| 152 | ); |
| 153 | let CardElement::Columns { columns, layout } = &card.items[1].element else { |
| 154 | panic!("columns") |
| 155 | }; |
| 156 | assert_eq!(layout.style, ContainerStyle::Emphasis); |
| 157 | assert_eq!(columns[0].layout.style, ContainerStyle::Accent); |
| 158 | assert_eq!(columns[0].layout.min_height, Some(40.)); |
| 159 | } |
| 160 | |
| 161 | #[test] |
| 162 | fn unknown_elements_without_fallback_are_dropped() { |
| 163 | let card = parse(json!({"body": [ |
| 164 | {"type": "Hologram", "text": "x"}, |
| 165 | {"type": "TextBlock", "text": "kept"} |
| 166 | ]})); |
| 167 | assert_eq!(card.items.len(), 1); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn fallback_elements_replace_unknown_or_unmet_elements() { |
| 172 | let card = parse(json!({"body": [ |
| 173 | {"type": "Hologram", "fallback": {"type": "TextBlock", "text": "plain"}}, |
| 174 | {"type": "Hologram", "fallback": "drop", "fallbackText": "never"}, |
| 175 | {"type": "TextBlock", "text": "future", "requires": {"adaptiveCards": "1.9"}, |
| 176 | "fallback": {"type": "TextBlock", "text": "older"}}, |
| 177 | {"type": "TextBlock", "text": "custom", "requires": {"acTest": "1.0"}, "fallback": "drop"}, |
| 178 | {"type": "Hologram", "fallback": {"type": "Hologram", "fallback": {"type": "TextBlock", "text": "deep"}}} |
| 179 | ]})); |
| 180 | let texts: Vec<String> = card |
| 181 | .items |
| 182 | .iter() |
| 183 | .map(|item| match &item.element { |
| 184 | CardElement::Text(text) => match &text.spans[0] { |
| 185 | Span::Text(text) => text.clone(), |
| 186 | other => panic!("unexpected span {other:?}"), |
| 187 | }, |
| 188 | other => panic!("unexpected element {other:?}"), |
| 189 | }) |
| 190 | .collect(); |
| 191 | assert_eq!(texts, ["plain", "older", "deep"]); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn requirements_up_to_version_1_6_are_met() { |
| 196 | let card = parse(json!({"body": [ |
| 197 | {"type": "TextBlock", "text": "a", "requires": {"adaptiveCards": "1.x"}}, |
| 198 | {"type": "TextBlock", "text": "b", "requires": {"adaptiveCards": "1.6"}}, |
| 199 | {"type": "TextBlock", "text": "c", "requires": {"adaptiveCards": "*"}}, |
| 200 | {"type": "TextBlock", "text": "d", "requires": {"adaptiveCards": "1.7"}} |
| 201 | ]})); |
| 202 | assert_eq!(card.items.len(), 3); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn the_whole_card_keeps_its_fallback_text() { |
| 207 | let card = parse(json!({"body": [{"type": "Hologram", "fallbackText": "Open elsewhere"}]})); |
| 208 | let CardElement::Text(text) = &card.items[0].element else { |
| 209 | panic!("text") |
| 210 | }; |
| 211 | assert_eq!(text.spans, vec![Span::Text("Open elsewhere".to_owned())]); |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn width_classes_follow_the_breakpoints() { |
| 216 | assert_eq!(WidthClass::from_pixels(180.), WidthClass::VeryNarrow); |
| 217 | assert_eq!(WidthClass::from_pixels(300.), WidthClass::Narrow); |
| 218 | assert_eq!(WidthClass::from_pixels(480.), WidthClass::Standard); |
| 219 | assert_eq!(WidthClass::from_pixels(720.), WidthClass::Wide); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn target_width_matches_exact_and_ranged_classes() { |
| 224 | let exact = TargetWidth::parse("narrow").unwrap(); |
| 225 | assert!(exact.matches(WidthClass::Narrow)); |
| 226 | assert!(!exact.matches(WidthClass::Standard)); |
| 227 | let at_least = TargetWidth::parse("atLeast:standard").unwrap(); |
| 228 | assert!(!at_least.matches(WidthClass::Narrow)); |
| 229 | assert!(at_least.matches(WidthClass::Standard) && at_least.matches(WidthClass::Wide)); |
| 230 | let at_most = TargetWidth::parse("atMost:narrow").unwrap(); |
| 231 | assert!(at_most.matches(WidthClass::VeryNarrow) && at_most.matches(WidthClass::Narrow)); |
| 232 | assert!(!at_most.matches(WidthClass::Standard)); |
| 233 | assert_eq!(TargetWidth::parse("gigantic"), None); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn target_width_is_kept_on_items_and_columns() { |
| 238 | let card = parse(json!({"body": [ |
| 239 | {"type": "TextBlock", "text": "wide only", "targetWidth": "atLeast:wide"}, |
| 240 | {"type": "ColumnSet", "columns": [ |
| 241 | {"type": "Column", "targetWidth": "veryNarrow", "items": [{"type": "TextBlock", "text": "x"}]} |
| 242 | ]} |
| 243 | ]})); |
| 244 | assert!( |
| 245 | card.items[0] |
| 246 | .target_width |
| 247 | .unwrap() |
| 248 | .matches(WidthClass::Wide) |
| 249 | ); |
| 250 | let CardElement::Columns { columns, .. } = &card.items[1].element else { |
| 251 | panic!("columns") |
| 252 | }; |
| 253 | assert!( |
| 254 | columns[0] |
| 255 | .target_width |
| 256 | .unwrap() |
| 257 | .matches(WidthClass::VeryNarrow) |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn tables_resolve_widths_header_and_alignment() { |
| 263 | let CardElement::Table(table) = first_element(json!({ |
| 264 | "type": "Table", |
| 265 | "columns": [{"width": 2}, {"width": "80px", "horizontalCellContentAlignment": "right"}], |
| 266 | "firstRowAsHeader": false, "showGridLines": false, "gridStyle": "accent", |
| 267 | "horizontalCellContentAlignment": "center", "verticalCellContentAlignment": "bottom", |
| 268 | "rows": [ |
| 269 | {"type": "TableRow", "style": "emphasis", "cells": [ |
| 270 | {"type": "TableCell", "items": [{"type": "TextBlock", "text": "a"}]}, |
| 271 | {"type": "TableCell", "verticalContentAlignment": "top", "items": [{"type": "TextBlock", "text": "b"}]} |
| 272 | ]}, |
| 273 | {"type": "TableRow", "cells": [{"type": "TableCell", "items": [{"type": "TextBlock", "text": "c"}]}]} |
| 274 | ] |
| 275 | })) else { |
| 276 | panic!("table") |
| 277 | }; |
| 278 | assert_eq!(table.columns[0].width, ColumnWidth::Weighted(2.)); |
| 279 | assert_eq!(table.columns[1].width, ColumnWidth::Pixels(80.)); |
| 280 | assert!(!table.first_row_as_header && !table.show_grid_lines); |
| 281 | assert_eq!(table.grid_style, ContainerStyle::Accent); |
| 282 | assert_eq!(table.rows[0].style, ContainerStyle::Emphasis); |
| 283 | let row = &table.rows[0]; |
| 284 | assert_eq!( |
| 285 | table.cell_horizontal_alignment(row, 0), |
| 286 | HorizontalAlignment::Center |
| 287 | ); |
| 288 | assert_eq!( |
| 289 | table.cell_horizontal_alignment(row, 1), |
| 290 | HorizontalAlignment::Right |
| 291 | ); |
| 292 | assert_eq!( |
| 293 | table.cell_vertical_alignment(row, &row.cells[0], 0), |
| 294 | VerticalAlignment::Bottom |
| 295 | ); |
| 296 | assert_eq!( |
| 297 | table.cell_vertical_alignment(row, &row.cells[1], 1), |
| 298 | VerticalAlignment::Top |
| 299 | ); |
| 300 | } |
| 301 | |
| 302 | #[test] |
| 303 | fn tables_pad_missing_columns_and_expose_cell_text() { |
| 304 | let card = parse(json!({"body": [{ |
| 305 | "type": "Table", |
| 306 | "rows": [ |
| 307 | {"cells": [{"items": [{"type": "TextBlock", "text": "a"}]}, {"items": [{"type": "TextBlock", "text": "b"}]}]}, |
| 308 | {"cells": [{"items": [{"type": "TextBlock", "text": "c"}]}]} |
| 309 | ] |
| 310 | }]})); |
| 311 | let CardElement::Table(table) = &card.items[0].element else { |
| 312 | panic!("table") |
| 313 | }; |
| 314 | assert_eq!(table.columns.len(), 2); |
| 315 | assert!(table.first_row_as_header && table.show_grid_lines); |
| 316 | assert_eq!(card.plain_text().unwrap(), "a\nb\nc"); |
| 317 | } |
| 318 | |
| 319 | #[test] |
| 320 | fn code_blocks_keep_snippet_language_and_start_line() { |
| 321 | let CardElement::CodeBlock(block) = first_element(json!({ |
| 322 | "type": "CodeBlock", "codeSnippet": "let a = 1;\nlet b = 2;\n", "language": "Rust", "startLineNumber": 7 |
| 323 | })) else { |
| 324 | panic!("code block") |
| 325 | }; |
| 326 | assert_eq!(block.code, "let a = 1;\nlet b = 2;"); |
| 327 | assert_eq!(block.language.as_deref(), Some("Rust")); |
| 328 | assert_eq!(block.start_line, 7); |
| 329 | let CardElement::CodeBlock(plain) = |
| 330 | first_element(json!({"type": "CodeBlock", "codeSnippet": "x"})) |
| 331 | else { |
| 332 | panic!("code block") |
| 333 | }; |
| 334 | assert_eq!(plain.start_line, 1); |
| 335 | } |
| 336 | |
| 337 | #[test] |
| 338 | fn badges_parse_style_shape_and_icon() { |
| 339 | let CardElement::Badge(badge) = first_element(json!({ |
| 340 | "type": "Badge", "text": "Done", "icon": "CheckmarkCircle,Filled", "style": "Good", |
| 341 | "appearance": "tint", "shape": "rounded", "size": "large", "iconPosition": "After", |
| 342 | "tooltip": "All good" |
| 343 | })) else { |
| 344 | panic!("badge") |
| 345 | }; |
| 346 | assert_eq!(badge.style, BadgeStyle::Good); |
| 347 | assert_eq!(badge.appearance, BadgeAppearance::Tint); |
| 348 | assert_eq!(badge.shape, BadgeShape::Rounded); |
| 349 | assert_eq!(badge.icon.as_deref(), Some("CheckmarkCircle")); |
| 350 | assert_eq!(badge.icon_position, IconPosition::After); |
| 351 | assert_eq!(badge.tooltip.as_deref(), Some("All good")); |
| 352 | let CardElement::Badge(plain) = first_element(json!({"type": "Badge", "text": "New"})) else { |
| 353 | panic!("badge") |
| 354 | }; |
| 355 | assert_eq!(plain.appearance, BadgeAppearance::Filled); |
| 356 | assert_eq!(plain.shape, BadgeShape::Circular); |
| 357 | assert!(AdaptiveCard::parse(&json!({"body": [{"type": "Badge"}]}).to_string()).is_none()); |
| 358 | } |
| 359 | |
| 360 | #[test] |
| 361 | fn progress_elements_parse_value_color_and_label() { |
| 362 | let CardElement::ProgressBar(bar) = |
| 363 | first_element(json!({"type": "ProgressBar", "value": 140, "color": "good"})) |
| 364 | else { |
| 365 | panic!("bar") |
| 366 | }; |
| 367 | assert_eq!(bar.value, Some(100.)); |
| 368 | assert_eq!(bar.color, TextColor::Good); |
| 369 | let CardElement::ProgressBar(indeterminate) = first_element(json!({"type": "ProgressBar"})) |
| 370 | else { |
| 371 | panic!("bar") |
| 372 | }; |
| 373 | assert_eq!(indeterminate.value, None); |
| 374 | let CardElement::ProgressRing(ring) = first_element( |
| 375 | json!({"type": "ProgressRing", "label": "Loading", "labelPosition": "Below", "size": "large"}), |
| 376 | ) else { |
| 377 | panic!("ring") |
| 378 | }; |
| 379 | assert_eq!(ring.label.as_deref(), Some("Loading")); |
| 380 | assert_eq!(ring.label_position, LabelPosition::Below); |
| 381 | } |
| 382 | |
| 383 | #[test] |
| 384 | fn compound_buttons_need_a_title() { |
| 385 | let CardElement::CompoundButton(button) = first_element(json!({ |
| 386 | "type": "CompoundButton", "title": "Open", "description": "Opens the page", "icon": "Open", |
| 387 | "badge": "New", "selectAction": {"type": "Action.OpenUrl", "url": "https://a.example"} |
| 388 | })) else { |
| 389 | panic!("button") |
| 390 | }; |
| 391 | assert_eq!(button.title, "Open"); |
| 392 | assert_eq!(button.badge.as_deref(), Some("New")); |
| 393 | assert!(button.select_action.is_some()); |
| 394 | assert!( |
| 395 | AdaptiveCard::parse( |
| 396 | &json!({"body": [{"type": "CompoundButton", "description": "x"}]}).to_string() |
| 397 | ) |
| 398 | .is_none() |
| 399 | ); |
| 400 | } |
| 401 | |
| 402 | #[test] |
| 403 | fn carousels_hold_pages_and_ignore_the_timer() { |
| 404 | let card = parse(json!({"body": [{ |
| 405 | "type": "Carousel", "timer": 5000, "initialPageIndex": 1, |
| 406 | "pages": [ |
| 407 | {"type": "CarouselPage", "items": [{"type": "TextBlock", "text": "one"}]}, |
| 408 | {"type": "CarouselPage", "items": [{"type": "TextBlock", "text": "two"}]}, |
| 409 | {"type": "CarouselPage", "items": []} |
| 410 | ] |
| 411 | }]})); |
| 412 | let CardElement::Carousel(carousel) = &card.items[0].element else { |
| 413 | panic!("carousel") |
| 414 | }; |
| 415 | assert_eq!(carousel.pages.len(), 2); |
| 416 | assert_eq!(carousel.initial_page, 1); |
| 417 | assert_eq!(card.plain_text().unwrap(), "one\ntwo"); |
| 418 | } |
| 419 | |
| 420 | #[test] |
| 421 | fn action_mode_tooltip_and_style_are_parsed() { |
| 422 | let card = parse( |
| 423 | json!({"body": [{"type": "TextBlock", "text": "x"}], "actions": [ |
| 424 | {"type": "Action.OpenUrl", "title": "A", "url": "https://a.example", "style": "destructive", "tooltip": "Open A"}, |
| 425 | {"type": "Action.OpenUrl", "title": "B", "url": "https://b.example", "mode": "secondary"} |
| 426 | ]}), |
| 427 | ); |
| 428 | assert!(!card.actions[0].secondary); |
| 429 | assert_eq!(card.actions[0].tooltip.as_deref(), Some("Open A")); |
| 430 | assert!(card.actions[1].secondary); |
| 431 | assert!(card.actions[1].is_clickable()); |
| 432 | } |
| 433 | |
| 434 | fn numbered_actions(count: usize, secondary: &[usize]) -> Vec<teams_core::CardAction> { |
| 435 | let actions: Vec<_> = (0..count) |
| 436 | .map(|index| { |
| 437 | let mut action = json!({"type": "Action.OpenUrl", "title": format!("A{index}"), "url": "https://a.example"}); |
| 438 | if secondary.contains(&index) { |
| 439 | action["mode"] = json!("secondary"); |
| 440 | } |
| 441 | action |
| 442 | }) |
| 443 | .collect(); |
| 444 | parse(json!({"body": [{"type": "TextBlock", "text": "x"}], "actions": actions})).actions |
| 445 | } |
| 446 | |
| 447 | #[test] |
| 448 | fn secondary_actions_go_to_the_overflow() { |
| 449 | let actions = numbered_actions(4, &[1]); |
| 450 | let split = split_overflow(&actions, |_| true); |
| 451 | assert_eq!(split.primary, [0, 2, 3]); |
| 452 | assert_eq!(split.overflow, [1]); |
| 453 | } |
| 454 | |
| 455 | #[test] |
| 456 | fn primary_actions_beyond_six_go_to_the_overflow_in_order() { |
| 457 | let actions = numbered_actions(8, &[3]); |
| 458 | let split = split_overflow(&actions, |_| true); |
| 459 | assert_eq!(split.primary, [0, 1, 2, 4, 5, 6]); |
| 460 | assert_eq!(split.overflow, [3, 7]); |
| 461 | } |
| 462 | |
| 463 | #[test] |
| 464 | fn hidden_actions_are_left_out_of_the_split() { |
| 465 | let actions = numbered_actions(3, &[]); |
| 466 | let split = split_overflow(&actions, |action| action.title != "A1"); |
| 467 | assert_eq!(split.primary, [0, 2]); |
| 468 | assert!(split.overflow.is_empty()); |
| 469 | } |
| 470 | |
| 471 | fn local(text: &str) -> String { |
| 472 | format_card_dates_in(text, &FixedOffset::east_opt(2 * 3600).unwrap()) |
| 473 | } |
| 474 | |
| 475 | #[test] |
| 476 | fn dates_format_in_the_given_zone() { |
| 477 | let iso = "2026-10-09T12:05:00Z"; |
| 478 | assert_eq!( |
| 479 | local(&format!("{{{{DATE({iso}, SHORT)}}}}")), |
| 480 | "Fri, Oct 9, 2026" |
| 481 | ); |
| 482 | assert_eq!( |
| 483 | local(&format!("{{{{DATE({iso},LONG)}}}}")), |
| 484 | "Friday, October 9, 2026" |
| 485 | ); |
| 486 | assert_eq!( |
| 487 | local(&format!("{{{{DATE({iso}, COMPACT)}}}}")), |
| 488 | "2026-10-09" |
| 489 | ); |
| 490 | assert_eq!(local(&format!("{{{{DATE({iso})}}}}")), "2026-10-09"); |
| 491 | assert_eq!(local(&format!("{{{{TIME({iso})}}}}")), "14:05"); |
| 492 | } |
| 493 | |
| 494 | #[test] |
| 495 | fn the_zone_can_move_the_date() { |
| 496 | assert_eq!( |
| 497 | local("{{DATE(2026-10-09T23:30:00Z, SHORT)}}"), |
| 498 | "Sat, Oct 10, 2026" |
| 499 | ); |
| 500 | } |
| 501 | |
| 502 | #[test] |
| 503 | fn dates_work_without_braces_inside_longer_text() { |
| 504 | assert_eq!( |
| 505 | local("Due DATE(2026-10-09T12:05:00Z, SHORT) at TIME(2026-10-09T12:05:00Z)."), |
| 506 | "Due Fri, Oct 9, 2026 at 14:05." |
| 507 | ); |
| 508 | } |
| 509 | |
| 510 | #[test] |
| 511 | fn malformed_dates_stay_literal() { |
| 512 | for text in [ |
| 513 | "{{DATE(yesterday, SHORT)}}", |
| 514 | "{{DATE(2026-10-09, SHORT)}}", |
| 515 | "{{TIME()}}", |
| 516 | "{{DATE(2026-10-09T12:05:00Z, WEEKLY)}}", |
| 517 | ] { |
| 518 | assert_eq!(local(text), text); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | #[test] |
| 523 | fn text_blocks_and_facts_format_dates() { |
| 524 | let card = parse(json!({"body": [ |
| 525 | {"type": "TextBlock", "text": "On {{DATE(2026-10-09T12:05:00Z, LONG)}}"}, |
| 526 | {"type": "RichTextBlock", "inlines": [{"type": "TextRun", "text": "{{TIME(2026-10-09T12:05:00Z)}}"}]}, |
| 527 | {"type": "FactSet", "facts": [{"title": "Due", "value": "{{DATE(2026-10-09T12:05:00Z, COMPACT)}}"}]} |
| 528 | ]})); |
| 529 | let text = card.plain_text().unwrap(); |
| 530 | assert!(!text.contains("{{"), "{text}"); |
| 531 | assert!(text.contains("2026")); |
| 532 | } |