BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / tests / spans.rs

spans.rs

spans.rs 14.8 KiB Download Raw
1use serde_json::json;
2use store::MessageRecord;
3use teams_core::{FontSize, Span, card_content_text, html_to_spans, message_spans};
4
5fn text(value: &str) -> Span {
6 Span::Text(value.to_owned())
7}
8
9#[test]
10fn plain_paragraph() {
11 assert_eq!(
12 html_to_spans("<p>Hello world</p>"),
13 vec![text("Hello world")]
14 );
15}
16
17#[test]
18fn 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]
32fn 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]
47fn 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]
61fn link_keeps_url_and_label() {
62 assert_eq!(
63 html_to_spans(r#"<a href="https://example.test/x?a=1&amp;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]
72fn anchor_without_href_is_plain_text() {
73 assert_eq!(html_to_spans("<a>nothing</a>"), vec![text("nothing")]);
74}
75
76#[test]
77fn inline_code_and_entities() {
78 assert_eq!(
79 html_to_spans("<p>run <code>a &lt; b</code> &amp; done</p>"),
80 vec![
81 text("run "),
82 Span::Code("a < b".to_owned()),
83 text(" & done")
84 ]
85 );
86}
87
88#[test]
89fn codeblock_with_language_class() {
90 assert_eq!(
91 html_to_spans("<p>x</p><pre class=\"language-rust\">fn main() {\n 1 &lt; 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]
104fn 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]
115fn 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]
126fn 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]
137fn 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]
154fn 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]
164fn 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 {
170 message_id: Some("170".into()),
171 children: vec![
172 Span::Bold(vec![text("Bob Sample")]),
173 text("12:01"),
174 Span::LineBreak,
175 text("original text"),
176 ]
177 },
178 Span::LineBreak,
179 text("my answer"),
180 ]
181 );
182}
183
184#[test]
185fn list_items() {
186 assert_eq!(
187 html_to_spans("<ul>\n<li>one</li>\n<li>two <b>b</b></li>\n</ul>"),
188 vec![Span::List {
189 ordered: false,
190 start: 1,
191 items: vec![
192 vec![text("one")],
193 vec![text("two "), Span::Bold(vec![text("b")])],
194 ],
195 }]
196 );
197}
198
199#[test]
200fn ordered_list_keeps_start_and_nesting() {
201 assert_eq!(
202 html_to_spans(r#"<ol start="9"><li>a<ul><li>b</li></ul></li><li>c</li></ol>"#),
203 vec![Span::List {
204 ordered: true,
205 start: 9,
206 items: vec![
207 vec![
208 text("a"),
209 Span::LineBreak,
210 Span::List {
211 ordered: false,
212 start: 1,
213 items: vec![vec![text("b")]],
214 },
215 ],
216 vec![text("c")],
217 ],
218 }]
219 );
220}
221
222#[test]
223fn list_nested_directly_in_list_joins_the_previous_item() {
224 let spans = html_to_spans("<ul><li>a</li><ul><li>b</li></ul></ul>");
225 let [Span::List { items, .. }] = &spans[..] else {
226 panic!("one list expected")
227 };
228 assert_eq!(items.len(), 1);
229 assert!(matches!(items[0].last(), Some(Span::List { .. })));
230}
231
232#[test]
233fn headings_keep_their_level_up_to_three() {
234 assert_eq!(
235 html_to_spans("<h2>Title</h2><h5>Small</h5>"),
236 vec![
237 Span::Heading {
238 level: 2,
239 children: vec![text("Title")]
240 },
241 Span::LineBreak,
242 Span::Heading {
243 level: 3,
244 children: vec![text("Small")]
245 },
246 ]
247 );
248}
249
250#[test]
251fn strike_underline_and_rule() {
252 assert_eq!(
253 html_to_spans("<p><s>old</s> <u>new</u></p><hr>"),
254 vec![
255 Span::Strike(vec![text("old")]),
256 text(" "),
257 Span::Underline(vec![text("new")]),
258 Span::LineBreak,
259 Span::Rule,
260 ]
261 );
262}
263
264#[test]
265fn table_rows_and_header() {
266 assert_eq!(
267 html_to_spans(
268 "<table><thead><tr><th>Area</th><th>Owner</th></tr></thead><tbody><tr><td>Sync</td><td>Priya</td></tr></tbody></table>"
269 ),
270 vec![Span::Table {
271 header: true,
272 rows: vec![
273 vec![vec![text("Area")], vec![text("Owner")]],
274 vec![vec![text("Sync")], vec![text("Priya")]],
275 ],
276 }]
277 );
278}
279
280#[test]
281fn table_without_header_cells_has_no_header() {
282 let spans = html_to_spans("<table><tr><td>a</td></tr></table>");
283 assert!(matches!(&spans[..], [Span::Table { header: false, .. }]));
284}
285
286#[test]
287fn plain_blockquote_is_not_a_reply() {
288 assert_eq!(
289 html_to_spans("<blockquote>wise words</blockquote>"),
290 vec![Span::BlockQuote(vec![text("wise words")])]
291 );
292}
293
294#[test]
295fn span_colors_are_parsed() {
296 assert_eq!(
297 html_to_spans(
298 r#"<span style="color: #F00; background-color: rgb(255, 255, 0)">x</span><span>y</span><mark>z</mark>"#
299 ),
300 vec![
301 Span::Colored {
302 color: Some(0xff0000),
303 background: Some(0xffff00),
304 children: vec![text("x")],
305 },
306 text("y"),
307 Span::Colored {
308 color: None,
309 background: Some(0xffff00),
310 children: vec![text("z")],
311 },
312 ]
313 );
314}
315
316#[test]
317fn line_breaks() {
318 assert_eq!(
319 html_to_spans("<p>a<br>b</p>"),
320 vec![text("a"), Span::LineBreak, text("b")]
321 );
322}
323
324#[test]
325fn attachment_markers_and_scripts_are_dropped() {
326 assert_eq!(
327 html_to_spans(r#"<p>file</p><attachment id="abc"></attachment><script>alert(1)</script>"#),
328 vec![text("file")]
329 );
330}
331
332#[test]
333fn empty_and_system_bodies_yield_nothing() {
334 assert!(html_to_spans("").is_empty());
335 assert!(html_to_spans("<systemEventMessage/>").is_empty());
336}
337
338#[test]
339fn whitespace_collapses() {
340 assert_eq!(
341 html_to_spans("<div>\n a \n\t b </div>"),
342 vec![text("a b")]
343 );
344}
345
346#[test]
347fn card_text_lists_texts_and_facts_in_document_order() {
348 let card = json!({
349 "type": "AdaptiveCard",
350 "body": [
351 {"type": "TextBlock", "text": "Deploy finished"},
352 {"type": "FactSet", "facts": [{"title": "Env", "value": "prod"}]},
353 {"type": "TextBlock", "text": " "}
354 ]
355 });
356 assert_eq!(
357 card_content_text(&card.to_string()).unwrap(),
358 "Deploy finished\nEnv: prod"
359 );
360}
361
362fn record_with(body_html: &str, attachments_json: &str) -> MessageRecord {
363 MessageRecord {
364 subject: None,
365 conversation_id: "c".to_owned(),
366 message_id: "m".to_owned(),
367 reply_to_id: None,
368 sender_id: None,
369 sender_name: None,
370 sender_application_id: None,
371 links_json: "[]".to_owned(),
372 created_at: chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap(),
373 edited_at: None,
374 deleted: false,
375 body_html: body_html.to_owned(),
376 attachments_json: attachments_json.to_owned(),
377 reactions_json: "[]".to_owned(),
378 mentions_json: "[]".to_owned(),
379 call_event_json: None,
380 importance: Default::default(),
381 }
382}
383
384#[test]
385fn message_spans_leave_cards_to_the_card_renderer() {
386 let card = json!({"body": [{"type": "TextBlock", "text": "Line one"}]}).to_string();
387 let attachments = json!([
388 {"content_type": "application/vnd.microsoft.card.adaptive", "name": null, "url": null, "text": "Line one", "content": card}
389 ])
390 .to_string();
391 assert_eq!(
392 message_spans(&record_with("<p>body</p>", &attachments)),
393 vec![text("body")]
394 );
395 assert!(message_spans(&record_with("", &attachments)).is_empty());
396}
397
398#[test]
399fn card_text_matches_the_golden_output() {
400 #[derive(serde::Deserialize)]
401 struct Golden {
402 content: String,
403 expected: Option<String>,
404 }
405 let goldens: Vec<Golden> =
406 serde_json::from_str(include_str!("fixtures/card_golden.json")).unwrap();
407 for golden in goldens {
408 assert_eq!(
409 teams_core::card_content_text(&golden.content),
410 golden.expected,
411 "{}",
412 golden.content
413 );
414 }
415}
416
417#[test]
418fn superscript_and_subscript() {
419 assert_eq!(
420 html_to_spans("<p>x<sup>2</sup> H<sub>2</sub>O</p>"),
421 vec![
422 text("x"),
423 Span::Superscript(vec![text("2")]),
424 text(" H"),
425 Span::Subscript(vec![text("2")]),
426 text("O"),
427 ]
428 );
429}
430
431#[test]
432fn font_sizes_map_to_small_large_and_pixels() {
433 let sized =
434 |value: &str| html_to_spans(&format!("<span style=\"font-size:{value};\">t</span>"));
435 assert_eq!(
436 sized("xx-small"),
437 vec![Span::Sized(FontSize::Small, vec![text("t")])]
438 );
439 assert_eq!(
440 sized("x-large"),
441 vec![Span::Sized(FontSize::Large, vec![text("t")])]
442 );
443 for (value, pixels) in [
444 ("x-small", 10),
445 ("small", 13),
446 ("large", 18),
447 ("xx-large", 32),
448 ] {
449 assert_eq!(
450 sized(value),
451 vec![Span::Sized(FontSize::Pixels(pixels), vec![text("t")])]
452 );
453 }
454 assert_eq!(
455 sized("12px"),
456 vec![Span::Sized(FontSize::Pixels(12), vec![text("t")])]
457 );
458 assert_eq!(
459 sized("0"),
460 vec![Span::Sized(FontSize::Hidden, vec![text("t")])]
461 );
462 assert_eq!(
463 sized("0px"),
464 vec![Span::Sized(FontSize::Hidden, vec![text("t")])]
465 );
466 for value in ["inherit", "medium"] {
467 assert_eq!(sized(value), vec![text("t")]);
468 }
469}
470
471#[test]
472fn sized_text_nests_with_bold_and_color() {
473 assert_eq!(
474 html_to_spans("<span style=\"font-size:x-large;color:#ff0000\"><b>big</b></span>"),
475 vec![Span::Sized(
476 FontSize::Large,
477 vec![Span::Colored {
478 color: Some(0xff0000),
479 background: None,
480 children: vec![Span::Bold(vec![text("big")])],
481 }]
482 )]
483 );
484}
485
486#[test]
487fn margin_left_paragraphs_become_indent_levels() {
488 assert_eq!(
489 html_to_spans(
490 "<p style=\"margin-left:40px;\">a</p><div style=\"margin-left: 120px\">b</div>"
491 ),
492 vec![
493 Span::Indent {
494 level: 1,
495 children: vec![text("a")],
496 },
497 Span::LineBreak,
498 Span::Indent {
499 level: 3,
500 children: vec![text("b")],
501 },
502 ]
503 );
504 assert_eq!(
505 html_to_spans("<p style=\"margin-left:10px\">a</p>"),
506 vec![text("a")]
507 );
508}
509
510#[test]
511fn teams_code_blocks_keep_their_line_breaks() {
512 assert_eq!(
513 html_to_spans(
514 "<pre class=\"language-plaintext skipProofing\" spellcheck=\"false\"><code>a<br>&nbsp; b</code></pre>"
515 ),
516 vec![Span::CodeBlock {
517 language: None,
518 code: "a\n\u{a0} b".to_owned(),
519 }]
520 );
521}
522
523#[test]
524fn org_emoji_keep_their_image_and_name() {
525 let source = "https://graph.microsoft.com/v1.0/chats/19:a@thread.v2/messages/1/hostedContents/aWQ9/$value";
526 assert_eq!(
527 html_to_spans(&format!(
528 "<p>hi <customemoji id=\"MC13\" alt=\"crying_cat\" source=\"{source}\"></customemoji></p>"
529 )),
530 vec![
531 text("hi "),
532 Span::CustomEmoji {
533 name: "crying_cat".to_owned(),
534 url: source.to_owned(),
535 },
536 ]
537 );
538 assert_eq!(
539 html_to_spans("<p><customemoji alt=\"crying_cat\"></customemoji></p>"),
540 vec![text(":crying_cat:")]
541 );
542 let official = "https://de-prod.asyncgw.teams.microsoft.com/v1/objects/0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8/views/imgt2_anim";
543 assert_eq!(
544 html_to_spans(&format!(
545 r#"<p><img alt="absolute-cinema" itemid="absolute-cinema;0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8" itemtype="http://schema.skype.com/CustomEmoji" src="{official}" width="20" height="20"></p>"#
546 )),
547 vec![Span::CustomEmoji {
548 name: "absolute-cinema".to_owned(),
549 url: official.to_owned(),
550 }]
551 );
552}