BlueGit

ict rusty-teams

Files

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

spans.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
spans.rs 7.7 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![
185 Span::ListItem(vec![text("one")]),
186 Span::ListItem(vec![text("two "), Span::Bold(vec![text("b")])]),
187 ]
188 );
189}
190
191#[test]
192fn line_breaks() {
193 assert_eq!(
194 html_to_spans("<p>a<br>b</p>"),
195 vec![text("a"), Span::LineBreak, text("b")]
196 );
197}
198
199#[test]
200fn attachment_markers_and_scripts_are_dropped() {
201 assert_eq!(
202 html_to_spans(r#"<p>file</p><attachment id="abc"></attachment><script>alert(1)</script>"#),
203 vec![text("file")]
204 );
205}
206
207#[test]
208fn empty_and_system_bodies_yield_nothing() {
209 assert!(html_to_spans("").is_empty());
210 assert!(html_to_spans("<systemEventMessage/>").is_empty());
211}
212
213#[test]
214fn whitespace_collapses() {
215 assert_eq!(
216 html_to_spans("<div>\n a \n\t b </div>"),
217 vec![text("a b")]
218 );
219}
220
221#[test]
222fn card_text_follows_python_order() {
223 let card = json!({
224 "type": "AdaptiveCard",
225 "body": [
226 {"type": "TextBlock", "text": "Deploy finished"},
227 {"type": "FactSet", "facts": [{"title": "Env", "value": "prod"}]},
228 {"type": "TextBlock", "text": " "}
229 ],
230 "title": "Header"
231 });
232 assert_eq!(
233 card_content_text(&card.to_string()).unwrap(),
234 "Header\nDeploy finished\nEnv\nprod"
235 );
236}
237
238fn record_with(body_html: &str, attachments_json: &str) -> MessageRecord {
239 MessageRecord {
240 conversation_id: "c".to_owned(),
241 message_id: "m".to_owned(),
242 reply_to_id: None,
243 sender_id: None,
244 sender_name: None,
245 created_at: chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap(),
246 edited_at: None,
247 deleted: false,
248 body_html: body_html.to_owned(),
249 attachments_json: attachments_json.to_owned(),
250 reactions_json: "[]".to_owned(),
251 mentions_json: "[]".to_owned(),
252 }
253}
254
255#[test]
256fn message_spans_append_card_text_only_for_cards() {
257 let attachments = r#"[
258 {"content_type":"application/vnd.microsoft.card.adaptive","name":null,"url":null,"text":"Line one\nLine two"},
259 {"content_type":"reference","name":"a.pdf","url":"https://files.example.test/a.pdf","text":"[attachment: a.pdf]"}
260 ]"#;
261 assert_eq!(
262 message_spans(&record_with("<p>body</p>", attachments)),
263 vec![
264 text("body"),
265 Span::LineBreak,
266 text("Line one"),
267 Span::LineBreak,
268 text("Line two")
269 ]
270 );
271 assert_eq!(
272 message_spans(&record_with("", attachments)),
273 vec![text("Line one"), Span::LineBreak, text("Line two")]
274 );
275}
276
277#[test]
278fn card_text_matches_the_golden_output() {
279 #[derive(serde::Deserialize)]
280 struct Golden {
281 content: String,
282 expected: Option<String>,
283 }
284 let goldens: Vec<Golden> =
285 serde_json::from_str(include_str!("fixtures/card_golden.json")).unwrap();
286 for golden in goldens {
287 assert_eq!(
288 teams_core::card_content_text(&golden.content),
289 golden.expected,
290 "{}",
291 golden.content
292 );
293 }
294}