| 1 | use teams_core::{ |
| 2 | Draft, DraftLine, LineKind, Mark, MarkKind, TABLE_MARK, Table, TypingStyle, html_to_spans, |
| 3 | }; |
| 4 | |
| 5 | fn typed(draft: &mut Draft, text: &str) { |
| 6 | let mut typing: Option<TypingStyle> = None; |
| 7 | for character in text.chars() { |
| 8 | let cursor = draft.text().len(); |
| 9 | let mut next = draft.text().to_owned(); |
| 10 | next.push(character); |
| 11 | let cursor = draft.apply_edit(&next, cursor + character.len_utf8(), typing.as_ref()); |
| 12 | typing = draft.convert_typed(cursor).map(|(_, style)| style); |
| 13 | draft.take_edits(); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | fn typed_draft(text: &str) -> Draft { |
| 18 | let mut draft = Draft::default(); |
| 19 | typed(&mut draft, text); |
| 20 | draft |
| 21 | } |
| 22 | |
| 23 | fn enter(draft: &mut Draft, cursor: usize, shift: bool) -> Option<usize> { |
| 24 | let result = draft.break_line(cursor, shift); |
| 25 | draft.take_edits(); |
| 26 | result |
| 27 | } |
| 28 | |
| 29 | fn table_of(rows: &[&[&str]]) -> Table { |
| 30 | Table::from_rows( |
| 31 | rows.iter() |
| 32 | .map(|row| row.iter().map(|cell| (*cell).to_owned()).collect()) |
| 33 | .collect(), |
| 34 | ) |
| 35 | } |
| 36 | |
| 37 | #[test] |
| 38 | fn enter_in_a_quote_adds_a_quote_line_and_enter_on_the_empty_one_leaves() { |
| 39 | let mut draft = typed_draft(">abc"); |
| 40 | let cursor = enter(&mut draft, 3, false).unwrap(); |
| 41 | assert_eq!(draft.text(), "abc\n"); |
| 42 | assert_eq!(draft.lines(), [LineKind::Quote, LineKind::Quote]); |
| 43 | let cursor = enter(&mut draft, cursor, false).unwrap(); |
| 44 | assert_eq!(draft.lines(), [LineKind::Quote, LineKind::Text]); |
| 45 | assert_eq!(cursor, 4); |
| 46 | assert_eq!(enter(&mut draft, cursor, false), None); |
| 47 | assert_eq!(draft.to_html(), "<blockquote>abc</blockquote>"); |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn enter_on_an_empty_quote_line_in_the_middle_splits_the_quote() { |
| 52 | let mut draft = Draft::from_markdown("> a\n> \n> b"); |
| 53 | enter(&mut draft, 2, false).unwrap(); |
| 54 | assert_eq!( |
| 55 | draft.lines(), |
| 56 | [LineKind::Quote, LineKind::Text, LineKind::Quote] |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn shift_enter_keeps_breaking_inside_the_quote() { |
| 62 | let mut draft = typed_draft(">a"); |
| 63 | let cursor = enter(&mut draft, 1, true).unwrap(); |
| 64 | let cursor = enter(&mut draft, cursor, true).unwrap(); |
| 65 | assert_eq!(cursor, 3); |
| 66 | assert_eq!( |
| 67 | draft.lines(), |
| 68 | [LineKind::Quote, LineKind::Quote, LineKind::Quote] |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn backspace_at_the_start_of_a_quote_leaves_it_or_joins_the_line_above() { |
| 74 | let mut empty = typed_draft(">"); |
| 75 | assert_eq!(empty.backspace_at_start(0), Some(0)); |
| 76 | assert_eq!(empty.lines(), [LineKind::Text]); |
| 77 | |
| 78 | let mut joined = Draft::from_markdown("x\n> abc"); |
| 79 | assert_eq!(joined.backspace_at_start(2), Some(1)); |
| 80 | assert_eq!(joined.text(), "xabc"); |
| 81 | assert_eq!(joined.lines(), [LineKind::Text]); |
| 82 | |
| 83 | let mut inner = Draft::from_markdown("> a\n> b"); |
| 84 | assert_eq!(inner.backspace_at_start(2), Some(1)); |
| 85 | assert_eq!(inner.text(), "ab"); |
| 86 | assert_eq!(inner.lines(), [LineKind::Quote]); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn down_at_the_end_of_a_closing_quote_opens_a_text_line_below() { |
| 91 | let mut draft = typed_draft(">abc"); |
| 92 | assert_eq!(draft.leave_quote_at_end(1), None); |
| 93 | assert_eq!(draft.leave_quote_at_end(3), Some(4)); |
| 94 | assert_eq!(draft.lines(), [LineKind::Quote, LineKind::Text]); |
| 95 | assert_eq!(Draft::plain("x").leave_quote_at_end(1), None); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn teams_shortcuts_make_headings_bold_italic_and_code() { |
| 100 | let heading = typed_draft("## Plan"); |
| 101 | assert_eq!(heading.text(), "Plan"); |
| 102 | assert_eq!(heading.lines(), [LineKind::Heading(2)]); |
| 103 | assert_eq!(heading.to_html(), "<h2>Plan</h2>"); |
| 104 | assert_eq!(typed_draft("#### deep").lines(), [LineKind::Heading(3)]); |
| 105 | assert_eq!(typed_draft("#nope").lines(), [LineKind::Text]); |
| 106 | |
| 107 | let inline = typed_draft("*b* _i_ `c`"); |
| 108 | assert_eq!(inline.text(), "b i c"); |
| 109 | assert_eq!( |
| 110 | inline.marks(), |
| 111 | [ |
| 112 | Mark { |
| 113 | range: 0..1, |
| 114 | kind: MarkKind::Bold |
| 115 | }, |
| 116 | Mark { |
| 117 | range: 2..3, |
| 118 | kind: MarkKind::Italic |
| 119 | }, |
| 120 | Mark { |
| 121 | range: 4..5, |
| 122 | kind: MarkKind::Code |
| 123 | }, |
| 124 | ] |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn enter_in_a_heading_is_left_to_send_and_backspace_makes_it_text() { |
| 130 | let mut draft = typed_draft("# a"); |
| 131 | assert_eq!(enter(&mut draft, 1, false), None); |
| 132 | assert_eq!(draft.backspace_at_start(0), Some(0)); |
| 133 | assert_eq!(draft.lines(), [LineKind::Text]); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn an_inserted_table_takes_an_empty_line_and_gets_a_line_after_it() { |
| 138 | let mut draft = Draft::default(); |
| 139 | let index = draft.insert_table(0, Table::new(2, 3)); |
| 140 | assert_eq!(index, 0); |
| 141 | assert_eq!(draft.text(), format!("{TABLE_MARK}\n")); |
| 142 | assert!(draft.lines()[0].is_table()); |
| 143 | assert_eq!(draft.lines()[1], LineKind::Text); |
| 144 | |
| 145 | let mut below = Draft::plain("hello"); |
| 146 | assert_eq!(below.insert_table(2, Table::new(1, 1)), 1); |
| 147 | assert_eq!(below.text(), format!("hello\n{TABLE_MARK}\n")); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn a_table_is_sent_as_teams_html_between_the_text() { |
| 152 | let mut draft = Draft::plain("before"); |
| 153 | let index = draft.insert_table(6, Table::new(1, 2)); |
| 154 | draft.set_table(index, table_of(&[&["a", "b"]])); |
| 155 | let end = draft.text().len(); |
| 156 | let mut text = draft.text().to_owned(); |
| 157 | text.push_str("after"); |
| 158 | draft.apply_edit(&text, end + 5, None); |
| 159 | assert_eq!( |
| 160 | draft.trimmed().to_html(), |
| 161 | "before<figure class=\"table\"><table class=\"copy-paste-table\"><tbody>\ |
| 162 | <tr><td>a</td><td>b</td></tr></tbody></table></figure>after" |
| 163 | ); |
| 164 | assert_eq!(draft.plain_text(), "before\na\tb\nafter"); |
| 165 | assert!( |
| 166 | !Draft::from_lines(&[DraftLine { |
| 167 | kind: LineKind::Table(Table::new(1, 1)), |
| 168 | number: None, |
| 169 | content: TABLE_MARK.to_string(), |
| 170 | marks: Vec::new(), |
| 171 | }]) |
| 172 | .plain_text() |
| 173 | .contains(TABLE_MARK) |
| 174 | ); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn text_typed_next_to_a_table_moves_to_its_own_line() { |
| 179 | let mut draft = Draft::default(); |
| 180 | draft.insert_table(0, Table::new(1, 1)); |
| 181 | let mark = TABLE_MARK.len_utf8(); |
| 182 | let mut text = draft.text().to_owned(); |
| 183 | text.insert(mark, 'x'); |
| 184 | let cursor = draft.apply_edit(&text, mark + 1, None); |
| 185 | assert_eq!(draft.text(), format!("{TABLE_MARK}\nx\n")); |
| 186 | assert_eq!(cursor, mark + 2); |
| 187 | assert!(draft.lines()[0].is_table()); |
| 188 | assert_eq!(draft.lines()[1], LineKind::Text); |
| 189 | |
| 190 | let mut text = draft.text().to_owned(); |
| 191 | text.insert(0, 'y'); |
| 192 | let cursor = draft.apply_edit(&text, 1, None); |
| 193 | assert_eq!(draft.text(), format!("y\n{TABLE_MARK}\nx\n")); |
| 194 | assert_eq!(cursor, 1); |
| 195 | assert!(draft.lines()[1].is_table()); |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn deleting_the_mark_drops_the_table() { |
| 200 | let mut draft = Draft::plain("a"); |
| 201 | draft.insert_table(1, Table::new(1, 1)); |
| 202 | draft.apply_edit("a\n\n", 2, None); |
| 203 | assert!(draft.lines().iter().all(|kind| !kind.is_table())); |
| 204 | } |
| 205 | |
| 206 | #[test] |
| 207 | fn delete_before_a_table_and_backspace_after_it_find_the_table() { |
| 208 | let mut draft = Draft::plain("a"); |
| 209 | let index = draft.insert_table(1, Table::new(1, 1)); |
| 210 | assert_eq!(draft.table_after(1), Some(index)); |
| 211 | assert_eq!(draft.delete_forward(1), Some(1)); |
| 212 | assert!(draft.lines()[index].is_table()); |
| 213 | let after = draft.text().len(); |
| 214 | assert_eq!(draft.table_before(after), Some(index)); |
| 215 | assert_eq!(draft.table_before(0), None); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn leaving_a_table_lands_beside_it_and_adds_a_line_when_needed() { |
| 220 | let mut draft = Draft::default(); |
| 221 | draft.insert_table(0, Table::new(1, 1)); |
| 222 | assert_eq!(draft.line_beside_table(0, false), 0); |
| 223 | assert_eq!(draft.lines()[0], LineKind::Text); |
| 224 | assert!(draft.lines()[1].is_table()); |
| 225 | let below = draft.line_beside_table(1, true); |
| 226 | assert_eq!(draft.line_at(below), 2); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn removing_a_table_keeps_the_lines_around_it() { |
| 231 | let mut draft = Draft::from_markdown("> a"); |
| 232 | let index = draft.insert_table(3, Table::new(1, 1)); |
| 233 | let end = draft.text().len(); |
| 234 | let mut text = draft.text().to_owned(); |
| 235 | text.push('z'); |
| 236 | draft.apply_edit(&text, end + 1, None); |
| 237 | let cursor = draft.remove_table(index); |
| 238 | assert_eq!(draft.text(), "a\nz"); |
| 239 | assert_eq!(draft.lines(), [LineKind::Quote, LineKind::Text]); |
| 240 | assert_eq!(cursor, 2); |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn quote_and_list_buttons_leave_a_table_alone() { |
| 245 | let mut draft = Draft::plain("a"); |
| 246 | let index = draft.insert_table(1, Table::new(1, 1)); |
| 247 | let end = draft.text().len(); |
| 248 | draft.toggle_lines(0..end, LineKind::Quote); |
| 249 | assert!(draft.lines()[index].is_table()); |
| 250 | } |
| 251 | |
| 252 | #[test] |
| 253 | fn a_copied_table_pastes_into_the_middle_of_a_line() { |
| 254 | let mut source = Draft::default(); |
| 255 | let index = source.insert_table(0, table_of(&[&["1", "2"]])); |
| 256 | let copied = source.slice(0..TABLE_MARK.len_utf8()); |
| 257 | let mut draft = Draft::plain("abcd"); |
| 258 | draft.insert_lines(2..2, &copied); |
| 259 | assert_eq!(draft.text(), format!("ab\n{TABLE_MARK}\ncd")); |
| 260 | assert_eq!(draft.table(1), source.table(index)); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn pasted_markdown_becomes_headings_lists_quotes_code_and_tables() { |
| 265 | let markdown = "# Title\n\nSome **bold**, *it*, ~~gone~~, `code` and [docs](https://x.de)\n\n\ |
| 266 | 1. one\n 1. nested\n2. two\n- dot\n - inner\n\n> quoted\n\n```rust\nfn x() {}\n```\n\n\ |
| 267 | | A | B |\n|---|---|\n| **1** | 2 |\n"; |
| 268 | let draft = Draft::from_markdown(markdown); |
| 269 | let kinds: Vec<LineKind> = draft |
| 270 | .lines() |
| 271 | .iter() |
| 272 | .filter(|kind| **kind != LineKind::Text) |
| 273 | .cloned() |
| 274 | .collect(); |
| 275 | assert_eq!( |
| 276 | kinds, |
| 277 | [ |
| 278 | LineKind::Heading(1), |
| 279 | LineKind::Numbered(0), |
| 280 | LineKind::Numbered(1), |
| 281 | LineKind::Numbered(0), |
| 282 | LineKind::Bullet(0), |
| 283 | LineKind::Bullet(1), |
| 284 | LineKind::Quote, |
| 285 | LineKind::Code(Some("rust".into())), |
| 286 | LineKind::Table(table_of(&[&["A", "B"], &["1", "2"]])), |
| 287 | ] |
| 288 | ); |
| 289 | let html = draft.trimmed().to_html(); |
| 290 | assert!(html.starts_with("<h1>Title</h1>"), "{html}"); |
| 291 | assert!(html.contains("<b>bold</b>, <i>it</i>, <s>gone</s>, <code>code</code>")); |
| 292 | assert!(html.contains("<a href=\"https://x.de\">docs</a>")); |
| 293 | assert!(html.contains("<blockquote>quoted</blockquote>")); |
| 294 | assert!(html.contains("<pre class=\"language-rust\">fn x() {}</pre>")); |
| 295 | assert!( |
| 296 | html.ends_with("<td>1</td><td>2</td></tr></tbody></table></figure>"), |
| 297 | "{html}" |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn text_with_pipes_but_no_delimiter_row_stays_text() { |
| 303 | assert!(!teams_core::has_markdown("a | b\nc | d")); |
| 304 | assert!(teams_core::has_markdown("| a | b |\n| - | - |")); |
| 305 | assert!(teams_core::has_markdown("## Heading")); |
| 306 | } |
| 307 | |
| 308 | #[test] |
| 309 | fn received_tables_and_headings_edit_as_tables_and_headings() { |
| 310 | let spans = html_to_spans( |
| 311 | "<h2>Notes</h2><table><tbody><tr><td><p>a</p></td><td>b<br>c</td></tr></tbody></table><p>end</p>", |
| 312 | ); |
| 313 | let draft = Draft::from_spans(&spans); |
| 314 | assert_eq!(draft.lines()[0], LineKind::Heading(2)); |
| 315 | assert_eq!( |
| 316 | draft.lines()[1], |
| 317 | LineKind::Table(table_of(&[&["a", "b\nc"]])) |
| 318 | ); |
| 319 | assert_eq!(draft.lines()[2], LineKind::Text); |
| 320 | } |
| 321 | |
| 322 | #[test] |
| 323 | fn pasting_markdown_with_a_table_leaves_the_cursor_after_the_paste() { |
| 324 | let markdown = "## Plan\n- dot\n> quoted\n\n| Area | Owner |\n|---|---|\n| Sync | Priya |\nend"; |
| 325 | let lines = Draft::from_markdown(markdown).slice(0..usize::MAX); |
| 326 | let mut draft = Draft::default(); |
| 327 | let cursor = draft.insert_lines(0..0, &lines); |
| 328 | assert_eq!(cursor, draft.text().len(), "{:?}", draft.text()); |
| 329 | } |
| 330 | |
| 331 | #[test] |
| 332 | fn a_sent_table_edits_back_into_the_same_table() { |
| 333 | let table = table_of(&[&["a", "x\ny"], &["", "b"]]); |
| 334 | let draft = Draft::from_spans(&html_to_spans(&table.to_html())); |
| 335 | let LineKind::Table(received) = &draft.lines()[0] else { |
| 336 | panic!("{:?}", draft.lines()); |
| 337 | }; |
| 338 | assert_eq!(received.rows[0], ["a", "x\ny"]); |
| 339 | assert!(received.rows[1][0].trim().is_empty()); |
| 340 | assert_eq!(received.to_html(), table.to_html()); |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn a_table_as_chatsvc_stores_it_reads_back_into_the_sent_table() { |
| 345 | let table = table_of(&[&["a", "b"], &["1", "2"], &["line one\nline two", ""]]); |
| 346 | let stored = "<table>\n<tbody>\n<tr>\n<td>a</td>\n<td>b</td>\n</tr>\n<tr>\n<td>1</td>\n<td>2</td>\n</tr>\n<tr>\n<td>\n<p>line one</p>\n<p>line two</p>\n</td>\n<td> </td>\n</tr>\n</tbody>\n</table>"; |
| 347 | let draft = Draft::from_spans(&html_to_spans(stored)); |
| 348 | let LineKind::Table(received) = &draft.lines()[0] else { |
| 349 | panic!("{:?}", draft.lines()); |
| 350 | }; |
| 351 | assert_eq!(received.rows[..2], table.rows[..2]); |
| 352 | assert_eq!(received.rows[2][0], "line one\nline two"); |
| 353 | assert!(received.rows[2][1].trim().is_empty()); |
| 354 | assert_eq!(received.to_html(), table.to_html()); |
| 355 | } |