| 1 | use serde_json::json; |
| 2 | use store::MessageRecord; |
| 3 | use teams_core::{Span, card_content_text, html_to_spans, message_spans}; |
| 4 | |
| 5 | fn text(value: &str) -> Span { |
| 6 | Span::Text(value.to_owned()) |
| 7 | } |
| 8 | |
| 9 | #[test] |
| 10 | fn plain_paragraph() { |
| 11 | assert_eq!( |
| 12 | html_to_spans("<p>Hello world</p>"), |
| 13 | vec![text("Hello world")] |
| 14 | ); |
| 15 | } |
| 16 | |
| 17 | #[test] |
| 18 | fn paragraphs_are_separated_by_one_break() { |
| 19 | assert_eq!( |
| 20 | html_to_spans("<p>one</p>\n<p>two</p><p>three</p>"), |
| 21 | vec![ |
| 22 | text("one"), |
| 23 | Span::LineBreak, |
| 24 | text("two"), |
| 25 | Span::LineBreak, |
| 26 | text("three") |
| 27 | ] |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn inline_formatting_nests() { |
| 33 | assert_eq!( |
| 34 | html_to_spans("<p>a <b>bold <i>both</i></b> <em>it</em> <strong>s</strong></p>"), |
| 35 | vec![ |
| 36 | text("a "), |
| 37 | Span::Bold(vec![text("bold "), Span::Italic(vec![text("both")])]), |
| 38 | text(" "), |
| 39 | Span::Italic(vec![text("it")]), |
| 40 | text(" "), |
| 41 | Span::Bold(vec![text("s")]), |
| 42 | ] |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | #[test] |
| 47 | fn mention_uses_the_visible_name() { |
| 48 | assert_eq!( |
| 49 | html_to_spans(r#"<p><at id="0">Ada Example</at> please look</p>"#), |
| 50 | vec![ |
| 51 | Span::Mention { |
| 52 | name: "Ada Example".to_owned(), |
| 53 | id: Some("0".to_owned()) |
| 54 | }, |
| 55 | text(" please look") |
| 56 | ] |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn link_keeps_url_and_label() { |
| 62 | assert_eq!( |
| 63 | html_to_spans(r#"<a href="https://example.test/x?a=1&b=2">docs</a>"#), |
| 64 | vec![Span::Link { |
| 65 | url: "https://example.test/x?a=1&b=2".to_owned(), |
| 66 | children: vec![text("docs")], |
| 67 | }] |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn anchor_without_href_is_plain_text() { |
| 73 | assert_eq!(html_to_spans("<a>nothing</a>"), vec![text("nothing")]); |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn inline_code_and_entities() { |
| 78 | assert_eq!( |
| 79 | html_to_spans("<p>run <code>a < b</code> & done</p>"), |
| 80 | vec![ |
| 81 | text("run "), |
| 82 | Span::Code("a < b".to_owned()), |
| 83 | text(" & done") |
| 84 | ] |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn codeblock_with_language_class() { |
| 90 | assert_eq!( |
| 91 | html_to_spans("<p>x</p><pre class=\"language-rust\">fn main() {\n 1 < 2\n}\n</pre>"), |
| 92 | vec![ |
| 93 | text("x"), |
| 94 | Span::LineBreak, |
| 95 | Span::CodeBlock { |
| 96 | language: Some("rust".to_owned()), |
| 97 | code: "fn main() {\n 1 < 2\n}".to_owned(), |
| 98 | }, |
| 99 | ] |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn teams_codeblock_wrapper() { |
| 105 | assert_eq!( |
| 106 | html_to_spans(r#"<codeblock class="language-js"><pre>let a = 1;</pre></codeblock>"#), |
| 107 | vec![Span::CodeBlock { |
| 108 | language: Some("js".to_owned()), |
| 109 | code: "let a = 1;".to_owned(), |
| 110 | }] |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn pre_without_language() { |
| 116 | assert_eq!( |
| 117 | html_to_spans("<pre>a\n b</pre>"), |
| 118 | vec![Span::CodeBlock { |
| 119 | language: None, |
| 120 | code: "a\n b".to_owned() |
| 121 | }] |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn language_is_found_on_inner_code_element() { |
| 127 | assert_eq!( |
| 128 | html_to_spans(r#"<pre><code class="language-python">print(1)</code></pre>"#), |
| 129 | vec![Span::CodeBlock { |
| 130 | language: Some("python".to_owned()), |
| 131 | code: "print(1)".to_owned(), |
| 132 | }] |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn hosted_content_image() { |
| 138 | let url = "https://graph.microsoft.com/v1.0/chats/19:x@thread.v2/messages/1/hostedContents/aGVsbG8/$value"; |
| 139 | assert_eq!( |
| 140 | html_to_spans(&format!( |
| 141 | r#"<p>look</p><p><img src="{url}" width="300" itemtype="http://schema.skype.com/AMSImage"></p>"# |
| 142 | )), |
| 143 | vec![ |
| 144 | text("look"), |
| 145 | Span::LineBreak, |
| 146 | Span::Image { |
| 147 | hosted_content_url: url.to_owned() |
| 148 | } |
| 149 | ] |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn emoji_element_and_emoji_image_become_text() { |
| 155 | assert_eq!( |
| 156 | html_to_spans( |
| 157 | r#"<p>hi <emoji id="smile" alt="😄" title="Smile"></emoji> and <img itemtype="http://schema.skype.com/Emoji" alt="👍" src="https://statics.example.test/e.png"></p>"# |
| 158 | ), |
| 159 | vec![text("hi 😄 and 👍")] |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn reply_quote() { |
| 165 | let html = r#"<blockquote itemscope="" itemtype="http://schema.skype.com/Reply" itemid="170"><strong itemprop="mri">Bob Sample</strong><span itemprop="time">12:01</span><p itemprop="preview">original text</p></blockquote><p>my answer</p>"#; |
| 166 | assert_eq!( |
| 167 | html_to_spans(html), |
| 168 | vec![ |
| 169 | Span::Quote(vec![ |
| 170 | Span::Bold(vec![text("Bob Sample")]), |
| 171 | text("12:01"), |
| 172 | Span::LineBreak, |
| 173 | text("original text"), |
| 174 | ]), |
| 175 | Span::LineBreak, |
| 176 | text("my answer"), |
| 177 | ] |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn list_items() { |
| 183 | assert_eq!( |
| 184 | html_to_spans("<ul>\n<li>one</li>\n<li>two <b>b</b></li>\n</ul>"), |
| 185 | vec![Span::List { |
| 186 | ordered: false, |
| 187 | start: 1, |
| 188 | items: vec![ |
| 189 | vec![text("one")], |
| 190 | vec![text("two "), Span::Bold(vec![text("b")])], |
| 191 | ], |
| 192 | }] |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn ordered_list_keeps_start_and_nesting() { |
| 198 | assert_eq!( |
| 199 | html_to_spans(r#"<ol start="9"><li>a<ul><li>b</li></ul></li><li>c</li></ol>"#), |
| 200 | vec![Span::List { |
| 201 | ordered: true, |
| 202 | start: 9, |
| 203 | items: vec![ |
| 204 | vec![ |
| 205 | text("a"), |
| 206 | Span::LineBreak, |
| 207 | Span::List { |
| 208 | ordered: false, |
| 209 | start: 1, |
| 210 | items: vec![vec![text("b")]], |
| 211 | }, |
| 212 | ], |
| 213 | vec![text("c")], |
| 214 | ], |
| 215 | }] |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn list_nested_directly_in_list_joins_the_previous_item() { |
| 221 | let spans = html_to_spans("<ul><li>a</li><ul><li>b</li></ul></ul>"); |
| 222 | let [Span::List { items, .. }] = &spans[..] else { |
| 223 | panic!("one list expected") |
| 224 | }; |
| 225 | assert_eq!(items.len(), 1); |
| 226 | assert!(matches!(items[0].last(), Some(Span::List { .. }))); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn headings_keep_their_level_up_to_three() { |
| 231 | assert_eq!( |
| 232 | html_to_spans("<h2>Title</h2><h5>Small</h5>"), |
| 233 | vec![ |
| 234 | Span::Heading { |
| 235 | level: 2, |
| 236 | children: vec![text("Title")] |
| 237 | }, |
| 238 | Span::LineBreak, |
| 239 | Span::Heading { |
| 240 | level: 3, |
| 241 | children: vec![text("Small")] |
| 242 | }, |
| 243 | ] |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn strike_underline_and_rule() { |
| 249 | assert_eq!( |
| 250 | html_to_spans("<p><s>old</s> <u>new</u></p><hr>"), |
| 251 | vec![ |
| 252 | Span::Strike(vec![text("old")]), |
| 253 | text(" "), |
| 254 | Span::Underline(vec![text("new")]), |
| 255 | Span::LineBreak, |
| 256 | Span::Rule, |
| 257 | ] |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn table_rows_and_header() { |
| 263 | assert_eq!( |
| 264 | html_to_spans( |
| 265 | "<table><thead><tr><th>Area</th><th>Owner</th></tr></thead><tbody><tr><td>Sync</td><td>Priya</td></tr></tbody></table>" |
| 266 | ), |
| 267 | vec![Span::Table { |
| 268 | header: true, |
| 269 | rows: vec![ |
| 270 | vec![vec![text("Area")], vec![text("Owner")]], |
| 271 | vec![vec![text("Sync")], vec![text("Priya")]], |
| 272 | ], |
| 273 | }] |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | #[test] |
| 278 | fn table_without_header_cells_has_no_header() { |
| 279 | let spans = html_to_spans("<table><tr><td>a</td></tr></table>"); |
| 280 | assert!(matches!(&spans[..], [Span::Table { header: false, .. }])); |
| 281 | } |
| 282 | |
| 283 | #[test] |
| 284 | fn plain_blockquote_is_not_a_reply() { |
| 285 | assert_eq!( |
| 286 | html_to_spans("<blockquote>wise words</blockquote>"), |
| 287 | vec![Span::BlockQuote(vec![text("wise words")])] |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | #[test] |
| 292 | fn span_colors_are_parsed() { |
| 293 | assert_eq!( |
| 294 | html_to_spans( |
| 295 | r#"<span style="color: #F00; background-color: rgb(255, 255, 0)">x</span><span>y</span><mark>z</mark>"# |
| 296 | ), |
| 297 | vec![ |
| 298 | Span::Colored { |
| 299 | color: Some(0xff0000), |
| 300 | background: Some(0xffff00), |
| 301 | children: vec![text("x")], |
| 302 | }, |
| 303 | text("y"), |
| 304 | Span::Colored { |
| 305 | color: None, |
| 306 | background: Some(0xffff00), |
| 307 | children: vec![text("z")], |
| 308 | }, |
| 309 | ] |
| 310 | ); |
| 311 | } |
| 312 | |
| 313 | #[test] |
| 314 | fn line_breaks() { |
| 315 | assert_eq!( |
| 316 | html_to_spans("<p>a<br>b</p>"), |
| 317 | vec![text("a"), Span::LineBreak, text("b")] |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | #[test] |
| 322 | fn attachment_markers_and_scripts_are_dropped() { |
| 323 | assert_eq!( |
| 324 | html_to_spans(r#"<p>file</p><attachment id="abc"></attachment><script>alert(1)</script>"#), |
| 325 | vec![text("file")] |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | #[test] |
| 330 | fn empty_and_system_bodies_yield_nothing() { |
| 331 | assert!(html_to_spans("").is_empty()); |
| 332 | assert!(html_to_spans("<systemEventMessage/>").is_empty()); |
| 333 | } |
| 334 | |
| 335 | #[test] |
| 336 | fn whitespace_collapses() { |
| 337 | assert_eq!( |
| 338 | html_to_spans("<div>\n a \n\t b </div>"), |
| 339 | vec![text("a b")] |
| 340 | ); |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn card_text_lists_texts_and_facts_in_document_order() { |
| 345 | let card = json!({ |
| 346 | "type": "AdaptiveCard", |
| 347 | "body": [ |
| 348 | {"type": "TextBlock", "text": "Deploy finished"}, |
| 349 | {"type": "FactSet", "facts": [{"title": "Env", "value": "prod"}]}, |
| 350 | {"type": "TextBlock", "text": " "} |
| 351 | ] |
| 352 | }); |
| 353 | assert_eq!( |
| 354 | card_content_text(&card.to_string()).unwrap(), |
| 355 | "Deploy finished\nEnv: prod" |
| 356 | ); |
| 357 | } |
| 358 | |
| 359 | fn record_with(body_html: &str, attachments_json: &str) -> MessageRecord { |
| 360 | MessageRecord { |
| 361 | conversation_id: "c".to_owned(), |
| 362 | message_id: "m".to_owned(), |
| 363 | reply_to_id: None, |
| 364 | sender_id: None, |
| 365 | sender_name: None, |
| 366 | sender_application_id: None, |
| 367 | created_at: chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap(), |
| 368 | edited_at: None, |
| 369 | deleted: false, |
| 370 | body_html: body_html.to_owned(), |
| 371 | attachments_json: attachments_json.to_owned(), |
| 372 | reactions_json: "[]".to_owned(), |
| 373 | mentions_json: "[]".to_owned(), |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | #[test] |
| 378 | fn message_spans_leave_cards_to_the_card_renderer() { |
| 379 | let card = json!({"body": [{"type": "TextBlock", "text": "Line one"}]}).to_string(); |
| 380 | let attachments = json!([ |
| 381 | {"content_type": "application/vnd.microsoft.card.adaptive", "name": null, "url": null, "text": "Line one", "content": card} |
| 382 | ]) |
| 383 | .to_string(); |
| 384 | assert_eq!( |
| 385 | message_spans(&record_with("<p>body</p>", &attachments)), |
| 386 | vec![text("body")] |
| 387 | ); |
| 388 | assert!(message_spans(&record_with("", &attachments)).is_empty()); |
| 389 | } |
| 390 | |
| 391 | #[test] |
| 392 | fn card_text_matches_the_golden_output() { |
| 393 | #[derive(serde::Deserialize)] |
| 394 | struct Golden { |
| 395 | content: String, |
| 396 | expected: Option<String>, |
| 397 | } |
| 398 | let goldens: Vec<Golden> = |
| 399 | serde_json::from_str(include_str!("fixtures/card_golden.json")).unwrap(); |
| 400 | for golden in goldens { |
| 401 | assert_eq!( |
| 402 | teams_core::card_content_text(&golden.content), |
| 403 | golden.expected, |
| 404 | "{}", |
| 405 | golden.content |
| 406 | ); |
| 407 | } |
| 408 | } |