use serde_json::json; use store::MessageRecord; use teams_core::{FontSize, Span, card_content_text, html_to_spans, message_spans}; fn text(value: &str) -> Span { Span::Text(value.to_owned()) } #[test] fn plain_paragraph() { assert_eq!( html_to_spans("
Hello world
"), vec![text("Hello world")] ); } #[test] fn paragraphs_are_separated_by_one_break() { assert_eq!( html_to_spans("one
\ntwo
three
"), vec![ text("one"), Span::LineBreak, text("two"), Span::LineBreak, text("three") ] ); } #[test] fn inline_formatting_nests() { assert_eq!( html_to_spans("a bold both it s
"), vec![ text("a "), Span::Bold(vec![text("bold "), Span::Italic(vec![text("both")])]), text(" "), Span::Italic(vec![text("it")]), text(" "), Span::Bold(vec![text("s")]), ] ); } #[test] fn mention_uses_the_visible_name() { assert_eq!( html_to_spans(r#"run a < b & done
x
fn main() {\n 1 < 2\n}\n"),
vec![
text("x"),
Span::LineBreak,
Span::CodeBlock {
language: Some("rust".to_owned()),
code: "fn main() {\n 1 < 2\n}".to_owned(),
},
]
);
}
#[test]
fn teams_codeblock_wrapper() {
assert_eq!(
html_to_spans(r#"let a = 1;
a\n b"), vec![Span::CodeBlock { language: None, code: "a\n b".to_owned() }] ); } #[test] fn language_is_found_on_inner_code_element() { assert_eq!( html_to_spans(r#"
print(1)"#),
vec![Span::CodeBlock {
language: Some("python".to_owned()),
code: "print(1)".to_owned(),
}]
);
}
#[test]
fn hosted_content_image() {
let url = "https://graph.microsoft.com/v1.0/chats/19:x@thread.v2/messages/1/hostedContents/aGVsbG8/$value";
assert_eq!(
html_to_spans(&format!(
r#"look
hi 
Bob Sample12:01original text
my answer
"#; assert_eq!( html_to_spans(html), vec![ Span::Quote { message_id: Some("170".into()), children: vec![ Span::Bold(vec![text("Bob Sample")]), text("12:01"), Span::LineBreak, text("original text"), ] }, Span::LineBreak, text("my answer"), ] ); } #[test] fn list_items() { assert_eq!( html_to_spans("old new
| Area | Owner |
|---|---|
| Sync | Priya |
| a |
wise words"), vec![Span::BlockQuote(vec![text("wise words")])] ); } #[test] fn span_colors_are_parsed() { assert_eq!( html_to_spans( r#"xyz"# ), vec![ Span::Colored { color: Some(0xff0000), background: Some(0xffff00), children: vec![text("x")], }, text("y"), Span::Colored { color: None, background: Some(0xffff00), children: vec![text("z")], }, ] ); } #[test] fn line_breaks() { assert_eq!( html_to_spans("
a
b
file
body
", &attachments)), vec![text("body")] ); assert!(message_spans(&record_with("", &attachments)).is_empty()); } #[test] fn card_text_matches_the_golden_output() { #[derive(serde::Deserialize)] struct Golden { content: String, expected: Optionx2 H2O
"), vec![ text("x"), Span::Superscript(vec![text("2")]), text(" H"), Span::Subscript(vec![text("2")]), text("O"), ] ); } #[test] fn font_sizes_map_to_small_large_and_pixels() { let sized = |value: &str| html_to_spans(&format!("t")); assert_eq!( sized("xx-small"), vec![Span::Sized(FontSize::Small, vec![text("t")])] ); assert_eq!( sized("x-large"), vec![Span::Sized(FontSize::Large, vec![text("t")])] ); for (value, pixels) in [ ("x-small", 10), ("small", 13), ("large", 18), ("xx-large", 32), ] { assert_eq!( sized(value), vec![Span::Sized(FontSize::Pixels(pixels), vec![text("t")])] ); } assert_eq!( sized("12px"), vec![Span::Sized(FontSize::Pixels(12), vec![text("t")])] ); assert_eq!( sized("0"), vec![Span::Sized(FontSize::Hidden, vec![text("t")])] ); assert_eq!( sized("0px"), vec![Span::Sized(FontSize::Hidden, vec![text("t")])] ); for value in ["inherit", "medium"] { assert_eq!(sized(value), vec![text("t")]); } } #[test] fn sized_text_nests_with_bold_and_color() { assert_eq!( html_to_spans("big"), vec![Span::Sized( FontSize::Large, vec![Span::Colored { color: Some(0xff0000), background: None, children: vec![Span::Bold(vec![text("big")])], }] )] ); } #[test] fn margin_left_paragraphs_become_indent_levels() { assert_eq!( html_to_spans( "a
a
"), vec![text("a")] ); } #[test] fn teams_code_blocks_keep_their_line_breaks() { assert_eq!( html_to_spans( "a
b"
),
vec![Span::CodeBlock {
language: None,
code: "a\n\u{a0} b".to_owned(),
}]
);
}
#[test]
fn org_emoji_keep_their_image_and_name() {
let source = "https://graph.microsoft.com/v1.0/chats/19:a@thread.v2/messages/1/hostedContents/aWQ9/$value";
assert_eq!(
html_to_spans(&format!(
"hi
before
x",
);
assert_eq!(
spans,
[
Span::Text("before".into()),
Span::LineBreak,
Span::CodeBlock {
language: Some("py".into()),
code: "x".into()
}
]
);
}
#[test]
fn teams_auto_selected_code_keeps_its_language() {
let spans = html_to_spans(
"def f",
);
assert!(
matches!(&spans[..], [Span::CodeBlock { language: Some(language), .. }] if language == "python"),
"{spans:?}"
);
}