BlueGit

ict rusty-teams

Files

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

spans.rs

spans.rs 10.8 KiB Download Raw
1use serde_json::json;
2use store::MessageRecord;
3use teams_core::{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 },
54 text(" please look")
55 ]
56 );
57}
58
59#[test]
60fn link_keeps_url_and_label() {
61 assert_eq!(
62 html_to_spans(r#"<a href="https://example.test/x?a=1&amp;b=2">docs</a>"#),
63 vec![Span::Link {
64 url: "https://example.test/x?a=1&b=2".to_owned(),
65 children: vec![text("docs")],
66 }]
67 );
68}
69
70#[test]
71fn anchor_without_href_is_plain_text() {
72 assert_eq!(html_to_spans("<a>nothing</a>"), vec![text("nothing")]);
73}
74
75#[test]
76fn inline_code_and_entities() {
77 assert_eq!(
78 html_to_spans("<p>run <code>a &lt; b</code> &amp; done</p>"),
79 vec![
80 text("run "),
81 Span::Code("a < b".to_owned()),
82 text(" & done")
83 ]
84 );
85}
86
87#[test]
88fn codeblock_with_language_class() {
89 assert_eq!(
90 html_to_spans("<p>x</p><pre class=\"language-rust\">fn main() {\n 1 &lt; 2\n}\n</pre>"),
91 vec![
92 text("x"),
93 Span::LineBreak,
94 Span::CodeBlock {
95 language: Some("rust".to_owned()),
96 code: "fn main() {\n 1 < 2\n}".to_owned(),
97 },
98 ]
99 );
100}
101
102#[test]
103fn teams_codeblock_wrapper() {
104 assert_eq!(
105 html_to_spans(r#"<codeblock class="language-js"><pre>let a = 1;</pre></codeblock>"#),
106 vec![Span::CodeBlock {
107 language: Some("js".to_owned()),
108 code: "let a = 1;".to_owned(),
109 }]
110 );
111}
112
113#[test]
114fn pre_without_language() {
115 assert_eq!(
116 html_to_spans("<pre>a\n b</pre>"),
117 vec![Span::CodeBlock {
118 language: None,
119 code: "a\n b".to_owned()
120 }]
121 );
122}
123
124#[test]
125fn language_is_found_on_inner_code_element() {
126 assert_eq!(
127 html_to_spans(r#"<pre><code class="language-python">print(1)</code></pre>"#),
128 vec![Span::CodeBlock {
129 language: Some("python".to_owned()),
130 code: "print(1)".to_owned(),
131 }]
132 );
133}
134
135#[test]
136fn hosted_content_image() {
137 let url = "https://graph.microsoft.com/v1.0/chats/19:x@thread.v2/messages/1/hostedContents/aGVsbG8/$value";
138 assert_eq!(
139 html_to_spans(&format!(
140 r#"<p>look</p><p><img src="{url}" width="300" itemtype="http://schema.skype.com/AMSImage"></p>"#
141 )),
142 vec![
143 text("look"),
144 Span::LineBreak,
145 Span::Image {
146 hosted_content_url: url.to_owned()
147 }
148 ]
149 );
150}
151
152#[test]
153fn emoji_element_and_emoji_image_become_text() {
154 assert_eq!(
155 html_to_spans(
156 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>"#
157 ),
158 vec![text("hi 😄 and 👍")]
159 );
160}
161
162#[test]
163fn reply_quote() {
164 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>"#;
165 assert_eq!(
166 html_to_spans(html),
167 vec![
168 Span::Quote(vec![
169 Span::Bold(vec![text("Bob Sample")]),
170 text("12:01"),
171 Span::LineBreak,
172 text("original text"),
173 ]),
174 Span::LineBreak,
175 text("my answer"),
176 ]
177 );
178}
179
180#[test]
181fn list_items() {
182 assert_eq!(
183 html_to_spans("<ul>\n<li>one</li>\n<li>two <b>b</b></li>\n</ul>"),
184 vec![Span::List {
185 ordered: false,
186 start: 1,
187 items: vec![
188 vec![text("one")],
189 vec![text("two "), Span::Bold(vec![text("b")])],
190 ],
191 }]
192 );
193}
194
195#[test]
196fn ordered_list_keeps_start_and_nesting() {
197 assert_eq!(
198 html_to_spans(r#"<ol start="9"><li>a<ul><li>b</li></ul></li><li>c</li></ol>"#),
199 vec![Span::List {
200 ordered: true,
201 start: 9,
202 items: vec![
203 vec![
204 text("a"),
205 Span::LineBreak,
206 Span::List {
207 ordered: false,
208 start: 1,
209 items: vec![vec![text("b")]],
210 },
211 ],
212 vec![text("c")],
213 ],
214 }]
215 );
216}
217
218#[test]
219fn list_nested_directly_in_list_joins_the_previous_item() {
220 let spans = html_to_spans("<ul><li>a</li><ul><li>b</li></ul></ul>");
221 let [Span::List { items, .. }] = &spans[..] else {
222 panic!("one list expected")
223 };
224 assert_eq!(items.len(), 1);
225 assert!(matches!(items[0].last(), Some(Span::List { .. })));
226}
227
228#[test]
229fn headings_keep_their_level_up_to_three() {
230 assert_eq!(
231 html_to_spans("<h2>Title</h2><h5>Small</h5>"),
232 vec![
233 Span::Heading {
234 level: 2,
235 children: vec![text("Title")]
236 },
237 Span::LineBreak,
238 Span::Heading {
239 level: 3,
240 children: vec![text("Small")]
241 },
242 ]
243 );
244}
245
246#[test]
247fn strike_underline_and_rule() {
248 assert_eq!(
249 html_to_spans("<p><s>old</s> <u>new</u></p><hr>"),
250 vec![
251 Span::Strike(vec![text("old")]),
252 text(" "),
253 Span::Underline(vec![text("new")]),
254 Span::LineBreak,
255 Span::Rule,
256 ]
257 );
258}
259
260#[test]
261fn table_rows_and_header() {
262 assert_eq!(
263 html_to_spans(
264 "<table><thead><tr><th>Area</th><th>Owner</th></tr></thead><tbody><tr><td>Sync</td><td>Priya</td></tr></tbody></table>"
265 ),
266 vec![Span::Table {
267 header: true,
268 rows: vec![
269 vec![vec![text("Area")], vec![text("Owner")]],
270 vec![vec![text("Sync")], vec![text("Priya")]],
271 ],
272 }]
273 );
274}
275
276#[test]
277fn table_without_header_cells_has_no_header() {
278 let spans = html_to_spans("<table><tr><td>a</td></tr></table>");
279 assert!(matches!(&spans[..], [Span::Table { header: false, .. }]));
280}
281
282#[test]
283fn plain_blockquote_is_not_a_reply() {
284 assert_eq!(
285 html_to_spans("<blockquote>wise words</blockquote>"),
286 vec![Span::BlockQuote(vec![text("wise words")])]
287 );
288}
289
290#[test]
291fn span_colors_are_parsed() {
292 assert_eq!(
293 html_to_spans(
294 r#"<span style="color: #F00; background-color: rgb(255, 255, 0)">x</span><span>y</span><mark>z</mark>"#
295 ),
296 vec![
297 Span::Colored {
298 color: Some(0xff0000),
299 background: Some(0xffff00),
300 children: vec![text("x")],
301 },
302 text("y"),
303 Span::Colored {
304 color: None,
305 background: Some(0xffff00),
306 children: vec![text("z")],
307 },
308 ]
309 );
310}
311
312#[test]
313fn line_breaks() {
314 assert_eq!(
315 html_to_spans("<p>a<br>b</p>"),
316 vec![text("a"), Span::LineBreak, text("b")]
317 );
318}
319
320#[test]
321fn attachment_markers_and_scripts_are_dropped() {
322 assert_eq!(
323 html_to_spans(r#"<p>file</p><attachment id="abc"></attachment><script>alert(1)</script>"#),
324 vec![text("file")]
325 );
326}
327
328#[test]
329fn empty_and_system_bodies_yield_nothing() {
330 assert!(html_to_spans("").is_empty());
331 assert!(html_to_spans("<systemEventMessage/>").is_empty());
332}
333
334#[test]
335fn whitespace_collapses() {
336 assert_eq!(
337 html_to_spans("<div>\n a \n\t b </div>"),
338 vec![text("a b")]
339 );
340}
341
342#[test]
343fn card_text_follows_python_order() {
344 let card = json!({
345 "type": "AdaptiveCard",
346 "body": [
347 {"type": "TextBlock", "text": "Deploy finished"},
348 {"type": "FactSet", "facts": [{"title": "Env", "value": "prod"}]},
349 {"type": "TextBlock", "text": " "}
350 ],
351 "title": "Header"
352 });
353 assert_eq!(
354 card_content_text(&card.to_string()).unwrap(),
355 "Header\nDeploy finished\nEnv\nprod"
356 );
357}
358
359fn 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 created_at: chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap(),
367 edited_at: None,
368 deleted: false,
369 body_html: body_html.to_owned(),
370 attachments_json: attachments_json.to_owned(),
371 reactions_json: "[]".to_owned(),
372 mentions_json: "[]".to_owned(),
373 }
374}
375
376#[test]
377fn message_spans_append_card_text_only_for_cards() {
378 let attachments = r#"[
379 {"content_type":"application/vnd.microsoft.card.adaptive","name":null,"url":null,"text":"Line one\nLine two"},
380 {"content_type":"reference","name":"a.pdf","url":"https://files.example.test/a.pdf","text":"[attachment: a.pdf]"}
381 ]"#;
382 assert_eq!(
383 message_spans(&record_with("<p>body</p>", attachments)),
384 vec![
385 text("body"),
386 Span::LineBreak,
387 text("Line one"),
388 Span::LineBreak,
389 text("Line two")
390 ]
391 );
392 assert_eq!(
393 message_spans(&record_with("", attachments)),
394 vec![text("Line one"), Span::LineBreak, text("Line two")]
395 );
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}