BlueGit

ict rusty-teams

Files

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

composer_blocks.rs

composer_blocks.rs 18.5 KiB Download Raw
1use teams_core::{
2 Draft, DraftLine, LineKind, Mark, MarkKind, RULE_MARK, TABLE_MARK, Table, TypingStyle,
3 html_to_spans,
4};
5
6fn typed(draft: &mut Draft, text: &str) {
7 let mut typing: Option<TypingStyle> = None;
8 for character in text.chars() {
9 let cursor = draft.text().len();
10 let mut next = draft.text().to_owned();
11 next.push(character);
12 let cursor = draft.apply_edit(&next, cursor + character.len_utf8(), typing.as_ref());
13 typing = draft.convert_typed(cursor).map(|(_, style)| style);
14 draft.take_edits();
15 }
16}
17
18fn typed_draft(text: &str) -> Draft {
19 let mut draft = Draft::default();
20 typed(&mut draft, text);
21 draft
22}
23
24fn enter(draft: &mut Draft, cursor: usize, shift: bool) -> Option<usize> {
25 let result = draft.break_line(cursor, shift);
26 draft.take_edits();
27 result
28}
29
30fn table_of(rows: &[&[&str]]) -> Table {
31 Table::from_rows(
32 rows.iter()
33 .map(|row| row.iter().map(|cell| (*cell).to_owned()).collect())
34 .collect(),
35 )
36}
37
38#[test]
39fn enter_in_a_quote_adds_a_quote_line_and_enter_on_the_empty_one_leaves() {
40 let mut draft = typed_draft(">abc");
41 let cursor = enter(&mut draft, 3, false).unwrap();
42 assert_eq!(draft.text(), "abc\n");
43 assert_eq!(draft.lines(), [LineKind::Quote, LineKind::Quote]);
44 let cursor = enter(&mut draft, cursor, false).unwrap();
45 assert_eq!(draft.lines(), [LineKind::Quote, LineKind::Text]);
46 assert_eq!(cursor, 4);
47 assert_eq!(enter(&mut draft, cursor, false), None);
48 assert_eq!(draft.to_html(), "<blockquote>abc</blockquote>");
49}
50
51#[test]
52fn enter_on_an_empty_quote_line_in_the_middle_splits_the_quote() {
53 let mut draft = Draft::from_markdown("> a\n> \n> b");
54 enter(&mut draft, 2, false).unwrap();
55 assert_eq!(
56 draft.lines(),
57 [LineKind::Quote, LineKind::Text, LineKind::Quote]
58 );
59}
60
61#[test]
62fn shift_enter_keeps_breaking_inside_the_quote() {
63 let mut draft = typed_draft(">a");
64 let cursor = enter(&mut draft, 1, true).unwrap();
65 let cursor = enter(&mut draft, cursor, true).unwrap();
66 assert_eq!(cursor, 3);
67 assert_eq!(
68 draft.lines(),
69 [LineKind::Quote, LineKind::Quote, LineKind::Quote]
70 );
71}
72
73#[test]
74fn backspace_at_the_start_of_a_quote_leaves_it_or_joins_the_line_above() {
75 let mut empty = typed_draft(">");
76 assert_eq!(empty.backspace_at_start(0), Some(0));
77 assert_eq!(empty.lines(), [LineKind::Text]);
78
79 let mut joined = Draft::from_markdown("x\n> abc");
80 assert_eq!(joined.backspace_at_start(2), Some(1));
81 assert_eq!(joined.text(), "xabc");
82 assert_eq!(joined.lines(), [LineKind::Text]);
83
84 let mut inner = Draft::from_markdown("> a\n> b");
85 assert_eq!(inner.backspace_at_start(2), Some(1));
86 assert_eq!(inner.text(), "ab");
87 assert_eq!(inner.lines(), [LineKind::Quote]);
88}
89
90#[test]
91fn down_at_the_end_of_a_closing_quote_opens_a_text_line_below() {
92 let mut draft = typed_draft(">abc");
93 assert_eq!(draft.leave_quote_at_end(1), None);
94 assert_eq!(draft.leave_quote_at_end(3), Some(4));
95 assert_eq!(draft.lines(), [LineKind::Quote, LineKind::Text]);
96 assert_eq!(Draft::plain("x").leave_quote_at_end(1), None);
97}
98
99#[test]
100fn teams_shortcuts_make_headings_bold_italic_and_code() {
101 let heading = typed_draft("## Plan");
102 assert_eq!(heading.text(), "Plan");
103 assert_eq!(heading.lines(), [LineKind::Heading(2)]);
104 assert_eq!(heading.to_html(), "<h2>Plan</h2>");
105 assert_eq!(typed_draft("#### deep").lines(), [LineKind::Heading(3)]);
106 assert_eq!(typed_draft("#nope").lines(), [LineKind::Text]);
107
108 let inline = typed_draft("*b* _i_ `c`");
109 assert_eq!(inline.text(), "b i c");
110 assert_eq!(
111 inline.marks(),
112 [
113 Mark {
114 range: 0..1,
115 kind: MarkKind::Bold
116 },
117 Mark {
118 range: 2..3,
119 kind: MarkKind::Italic
120 },
121 Mark {
122 range: 4..5,
123 kind: MarkKind::Code
124 },
125 ]
126 );
127}
128
129#[test]
130fn enter_in_a_heading_is_left_to_send_and_backspace_makes_it_text() {
131 let mut draft = typed_draft("# a");
132 assert_eq!(enter(&mut draft, 1, false), None);
133 assert_eq!(draft.backspace_at_start(0), Some(0));
134 assert_eq!(draft.lines(), [LineKind::Text]);
135}
136
137#[test]
138fn an_inserted_table_takes_an_empty_line_and_gets_a_line_after_it() {
139 let mut draft = Draft::default();
140 let index = draft.insert_table(0, Table::new(2, 3));
141 assert_eq!(index, 0);
142 assert_eq!(draft.text(), format!("{TABLE_MARK}\n"));
143 assert!(draft.lines()[0].is_table());
144 assert_eq!(draft.lines()[1], LineKind::Text);
145
146 let mut below = Draft::plain("hello");
147 assert_eq!(below.insert_table(2, Table::new(1, 1)), 1);
148 assert_eq!(below.text(), format!("hello\n{TABLE_MARK}\n"));
149}
150
151#[test]
152fn a_table_is_sent_as_teams_html_between_the_text() {
153 let mut draft = Draft::plain("before");
154 let index = draft.insert_table(6, Table::new(1, 2));
155 draft.set_table(index, table_of(&[&["a", "b"]]));
156 let end = draft.text().len();
157 let mut text = draft.text().to_owned();
158 text.push_str("after");
159 draft.apply_edit(&text, end + 5, None);
160 assert_eq!(
161 draft.trimmed().to_html(),
162 "before<figure class=\"table\"><table class=\"copy-paste-table\"><tbody>\
163 <tr><td>a</td><td>b</td></tr></tbody></table></figure>after"
164 );
165 assert_eq!(draft.plain_text(), "before\na\tb\nafter");
166 assert!(
167 !Draft::from_lines(&[DraftLine {
168 kind: LineKind::Table(Table::new(1, 1)),
169 number: None,
170 content: TABLE_MARK.to_string(),
171 marks: Vec::new(),
172 }])
173 .plain_text()
174 .contains(TABLE_MARK)
175 );
176}
177
178#[test]
179fn text_typed_next_to_a_table_moves_to_its_own_line() {
180 let mut draft = Draft::default();
181 draft.insert_table(0, Table::new(1, 1));
182 let mark = TABLE_MARK.len_utf8();
183 let mut text = draft.text().to_owned();
184 text.insert(mark, 'x');
185 let cursor = draft.apply_edit(&text, mark + 1, None);
186 assert_eq!(draft.text(), format!("{TABLE_MARK}\nx\n"));
187 assert_eq!(cursor, mark + 2);
188 assert!(draft.lines()[0].is_table());
189 assert_eq!(draft.lines()[1], LineKind::Text);
190
191 let mut text = draft.text().to_owned();
192 text.insert(0, 'y');
193 let cursor = draft.apply_edit(&text, 1, None);
194 assert_eq!(draft.text(), format!("y\n{TABLE_MARK}\nx\n"));
195 assert_eq!(cursor, 1);
196 assert!(draft.lines()[1].is_table());
197}
198
199#[test]
200fn deleting_the_mark_drops_the_table() {
201 let mut draft = Draft::plain("a");
202 draft.insert_table(1, Table::new(1, 1));
203 draft.apply_edit("a\n\n", 2, None);
204 assert!(draft.lines().iter().all(|kind| !kind.is_table()));
205}
206
207#[test]
208fn delete_before_a_table_and_backspace_after_it_find_the_table() {
209 let mut draft = Draft::plain("a");
210 let index = draft.insert_table(1, Table::new(1, 1));
211 assert_eq!(draft.table_after(1), Some(index));
212 assert_eq!(draft.delete_forward(1), Some(1));
213 assert!(draft.lines()[index].is_table());
214 let after = draft.text().len();
215 assert_eq!(draft.table_before(after), Some(index));
216 assert_eq!(draft.table_before(0), None);
217}
218
219#[test]
220fn leaving_a_table_lands_beside_it_and_adds_a_line_when_needed() {
221 let mut draft = Draft::default();
222 draft.insert_table(0, Table::new(1, 1));
223 assert_eq!(draft.line_beside_table(0, false), 0);
224 assert_eq!(draft.lines()[0], LineKind::Text);
225 assert!(draft.lines()[1].is_table());
226 let below = draft.line_beside_table(1, true);
227 assert_eq!(draft.line_at(below), 2);
228}
229
230#[test]
231fn removing_a_table_keeps_the_lines_around_it() {
232 let mut draft = Draft::from_markdown("> a");
233 let index = draft.insert_table(3, Table::new(1, 1));
234 let end = draft.text().len();
235 let mut text = draft.text().to_owned();
236 text.push('z');
237 draft.apply_edit(&text, end + 1, None);
238 let cursor = draft.remove_table(index);
239 assert_eq!(draft.text(), "a\nz");
240 assert_eq!(draft.lines(), [LineKind::Quote, LineKind::Text]);
241 assert_eq!(cursor, 2);
242}
243
244#[test]
245fn quote_and_list_buttons_leave_a_table_alone() {
246 let mut draft = Draft::plain("a");
247 let index = draft.insert_table(1, Table::new(1, 1));
248 let end = draft.text().len();
249 draft.toggle_lines(0..end, LineKind::Quote);
250 assert!(draft.lines()[index].is_table());
251}
252
253#[test]
254fn a_copied_table_pastes_into_the_middle_of_a_line() {
255 let mut source = Draft::default();
256 let index = source.insert_table(0, table_of(&[&["1", "2"]]));
257 let copied = source.slice(0..TABLE_MARK.len_utf8());
258 let mut draft = Draft::plain("abcd");
259 draft.insert_lines(2..2, &copied);
260 assert_eq!(draft.text(), format!("ab\n{TABLE_MARK}\ncd"));
261 assert_eq!(draft.table(1), source.table(index));
262}
263
264#[test]
265fn pasted_markdown_becomes_headings_lists_quotes_code_and_tables() {
266 let markdown = "# Title\n\nSome **bold**, *it*, ~~gone~~, `code` and [docs](https://x.de)\n\n\
267 1. one\n 1. nested\n2. two\n- dot\n - inner\n\n> quoted\n\n```rust\nfn x() {}\n```\n\n\
268 | A | B |\n|---|---|\n| **1** | 2 |\n";
269 let draft = Draft::from_markdown(markdown);
270 let kinds: Vec<LineKind> = draft
271 .lines()
272 .iter()
273 .filter(|kind| **kind != LineKind::Text)
274 .cloned()
275 .collect();
276 assert_eq!(
277 kinds,
278 [
279 LineKind::Heading(1),
280 LineKind::Numbered(0),
281 LineKind::Numbered(1),
282 LineKind::Numbered(0),
283 LineKind::Bullet(0),
284 LineKind::Bullet(1),
285 LineKind::Quote,
286 LineKind::Code(Some("rust".into())),
287 LineKind::Table(table_of(&[&["A", "B"], &["1", "2"]])),
288 ]
289 );
290 let html = draft.trimmed().to_html();
291 assert!(html.starts_with("<h1>Title</h1>"), "{html}");
292 assert!(html.contains("<b>bold</b>, <i>it</i>, <s>gone</s>, <code>code</code>"));
293 assert!(html.contains("<a href=\"https://x.de\">docs</a>"));
294 assert!(html.contains("<blockquote>quoted</blockquote>"));
295 assert!(html.contains("<pre class=\"language-rust skipProofing\" spellcheck=\"false\"><code>fn x() {}</code></pre>"));
296 assert!(
297 html.ends_with("<td>1</td><td>2</td></tr></tbody></table></figure>"),
298 "{html}"
299 );
300}
301
302#[test]
303fn text_with_pipes_but_no_delimiter_row_stays_text() {
304 assert!(!teams_core::has_markdown("a | b\nc | d"));
305 assert!(teams_core::has_markdown("| a | b |\n| - | - |"));
306 assert!(teams_core::has_markdown("## Heading"));
307}
308
309#[test]
310fn received_tables_and_headings_edit_as_tables_and_headings() {
311 let spans = html_to_spans(
312 "<h2>Notes</h2><table><tbody><tr><td><p>a</p></td><td>b<br>c</td></tr></tbody></table><p>end</p>",
313 );
314 let draft = Draft::from_spans(&spans);
315 assert_eq!(draft.lines()[0], LineKind::Heading(2));
316 assert_eq!(
317 draft.lines()[1],
318 LineKind::Table(table_of(&[&["a", "b\nc"]]))
319 );
320 assert_eq!(draft.lines()[2], LineKind::Text);
321}
322
323#[test]
324fn pasting_markdown_with_a_table_leaves_the_cursor_after_the_paste() {
325 let markdown = "## Plan\n- dot\n> quoted\n\n| Area | Owner |\n|---|---|\n| Sync | Priya |\nend";
326 let lines = Draft::from_markdown(markdown).slice(0..usize::MAX);
327 let mut draft = Draft::default();
328 let cursor = draft.insert_lines(0..0, &lines);
329 assert_eq!(cursor, draft.text().len(), "{:?}", draft.text());
330}
331
332#[test]
333fn a_sent_table_edits_back_into_the_same_table() {
334 let table = table_of(&[&["a", "x\ny"], &["", "b"]]);
335 let draft = Draft::from_spans(&html_to_spans(&table.to_html()));
336 let LineKind::Table(received) = &draft.lines()[0] else {
337 panic!("{:?}", draft.lines());
338 };
339 assert_eq!(received.rows[0], ["a", "x\ny"]);
340 assert!(received.rows[1][0].trim().is_empty());
341 assert_eq!(received.to_html(), table.to_html());
342}
343
344#[test]
345fn a_table_as_chatsvc_stores_it_reads_back_into_the_sent_table() {
346 let table = table_of(&[&["a", "b"], &["1", "2"], &["line one\nline two", ""]]);
347 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>&nbsp;</td>\n</tr>\n</tbody>\n</table>";
348 let draft = Draft::from_spans(&html_to_spans(stored));
349 let LineKind::Table(received) = &draft.lines()[0] else {
350 panic!("{:?}", draft.lines());
351 };
352 assert_eq!(received.rows[..2], table.rows[..2]);
353 assert_eq!(received.rows[2][0], "line one\nline two");
354 assert!(received.rows[2][1].trim().is_empty());
355 assert_eq!(received.to_html(), table.to_html());
356}
357
358#[test]
359fn font_colour_and_highlight_send_teams_spans() {
360 let mut draft = Draft::plain("red and marked");
361 draft.set_color(0..3, Some(0xB6424C));
362 draft.set_highlight(8..14, Some(0xFDD472));
363 assert_eq!(
364 draft.to_html(),
365 "<span style=\"color:#B6424C;\">red</span> and \
366 <span style=\"background-color:#FDD472;\">marked</span>"
367 );
368 draft.set_color(1..3, Some(0x1E53A3));
369 assert_eq!(draft.color_in(0..1, &MarkKind::Color(0)), Some(0xB6424C));
370 assert_eq!(draft.color_in(1..3, &MarkKind::Color(0)), Some(0x1E53A3));
371 draft.set_color(0..3, None);
372 assert_eq!(draft.color_in(0..3, &MarkKind::Color(0)), None);
373}
374
375#[test]
376fn received_colours_come_back_as_marks() {
377 let html = "<p><span style=\"color:#B6424C;\">a</span> \
378 <span style=\"background-color: rgb(253, 212, 114)\">b</span></p>";
379 let draft = Draft::from_spans(&html_to_spans(html));
380 assert_eq!(draft.text(), "a b");
381 assert!(draft.marks().contains(&Mark {
382 range: 0..1,
383 kind: MarkKind::Color(0xB6424C)
384 }));
385 assert!(draft.marks().contains(&Mark {
386 range: 2..3,
387 kind: MarkKind::Highlight(0xFDD472)
388 }));
389}
390
391#[test]
392fn typing_style_picks_and_drops_a_colour() {
393 let mut typing = TypingStyle::default();
394 typing.set_color(Some(0x2B9B62), &[MarkKind::Color(0xB6424C), MarkKind::Bold]);
395 assert_eq!(
396 typing.apply(vec![MarkKind::Color(0xB6424C), MarkKind::Bold]),
397 [MarkKind::Bold, MarkKind::Color(0x2B9B62)]
398 );
399 typing.clear(&[MarkKind::Bold, MarkKind::Link("https://x.de".into())]);
400 assert_eq!(
401 typing.apply(vec![MarkKind::Bold, MarkKind::Link("https://x.de".into())]),
402 [MarkKind::Link("https://x.de".into())]
403 );
404}
405
406#[test]
407fn clear_formatting_keeps_links_and_drops_the_indent() {
408 let mut draft = Draft::from_markdown("**bold** [l](https://x.de)");
409 draft.set_highlight(0..4, Some(0xDF9299));
410 draft.indent_blocks(0..0, false);
411 let length = draft.text().len();
412 draft.clear_formatting(0..length);
413 assert_eq!(draft.lines(), [LineKind::Text]);
414 assert_eq!(
415 draft.marks(),
416 [Mark {
417 range: 5..6,
418 kind: MarkKind::Link("https://x.de".into())
419 }]
420 );
421}
422
423#[test]
424fn indent_moves_paragraphs_by_teams_block_steps_and_nests_lists() {
425 let mut draft = Draft::plain("a\nb");
426 draft.indent_blocks(0..0, false);
427 draft.indent_blocks(0..0, false);
428 draft.indent_blocks(2..3, false);
429 assert_eq!(draft.lines(), [LineKind::Indent(2), LineKind::Indent(1)]);
430 assert_eq!(
431 draft.to_html(),
432 "<p style=\"margin-left:80px;\">a</p><p style=\"margin-left:40px;\">b</p>"
433 );
434 assert!(draft.can_outdent(0..0));
435 draft.indent_blocks(0..3, true);
436 assert_eq!(draft.lines(), [LineKind::Indent(1), LineKind::Text]);
437 assert!(!draft.can_outdent(2..3));
438 let mut list = Draft::from_markdown("- x");
439 list.indent_blocks(0..0, false);
440 assert_eq!(list.lines(), [LineKind::Bullet(1)]);
441}
442
443#[test]
444fn shift_enter_keeps_the_indent_and_enter_is_left_to_send() {
445 let mut draft = Draft::plain("a");
446 draft.indent_blocks(0..0, false);
447 assert_eq!(enter(&mut draft, 1, false), None);
448 assert_eq!(enter(&mut draft, 1, true), Some(2));
449 assert_eq!(draft.lines(), [LineKind::Indent(1), LineKind::Indent(1)]);
450}
451
452#[test]
453fn received_margins_become_indent_levels() {
454 let draft = Draft::from_spans(&html_to_spans(
455 "<p style=\"margin-left:80px\">deep</p><p>flat</p><p style=\"margin-left: 40px;\">one</p>",
456 ));
457 assert_eq!(
458 draft.lines(),
459 [LineKind::Indent(2), LineKind::Text, LineKind::Indent(1)]
460 );
461 assert_eq!(draft.text(), "deep\nflat\none");
462}
463
464#[test]
465fn a_rule_is_its_own_line_sent_as_hr() {
466 let mut draft = Draft::plain("a");
467 let cursor = draft.insert_rule(1);
468 draft.take_edits();
469 assert_eq!(draft.text(), format!("a\n{RULE_MARK}\n"));
470 assert_eq!(cursor, draft.text().len());
471 assert_eq!(
472 draft.lines(),
473 [LineKind::Text, LineKind::Rule, LineKind::Text]
474 );
475 assert_eq!(draft.trimmed().to_html(), "a<hr>");
476 assert_eq!(draft.plain_text(), "a\n\n");
477 assert_eq!(draft.backspace_at_start(cursor), Some(2));
478 assert_eq!(draft.text(), "a\n");
479 assert_eq!(draft.lines(), [LineKind::Text, LineKind::Text]);
480}
481
482#[test]
483fn text_typed_onto_a_rule_line_moves_off_it() {
484 let mut draft = Draft::default();
485 draft.insert_rule(0);
486 let mut next = draft.text().to_owned();
487 next.insert(0, 'x');
488 draft.apply_edit(&next, 1, None);
489 assert_eq!(draft.text(), format!("x\n{RULE_MARK}\n"));
490 assert_eq!(draft.lines()[1], LineKind::Rule);
491}
492
493#[test]
494fn received_rules_come_back_as_rule_lines() {
495 let draft = Draft::from_spans(&html_to_spans("<p>a</p><hr><p>b</p>"));
496 assert_eq!(
497 draft.lines(),
498 [LineKind::Text, LineKind::Rule, LineKind::Text]
499 );
500 assert_eq!(draft.to_html(), "a<hr>b");
501}
502
503#[test]
504fn paragraph_style_sets_and_clears_headings() {
505 let mut draft = Draft::plain("title\nbody");
506 draft.set_heading(0..0, Some(2));
507 assert_eq!(draft.heading_at(0), Some(2));
508 assert_eq!(draft.to_html(), "<h2>title</h2>body");
509 draft.set_heading(0..8, Some(1));
510 assert_eq!(draft.lines(), [LineKind::Heading(1), LineKind::Heading(1)]);
511 draft.set_heading(0..0, None);
512 assert_eq!(draft.lines(), [LineKind::Text, LineKind::Heading(1)]);
513}
514
515#[test]
516fn code_blocks_send_the_teams_pre_code_shape_and_read_back_line_breaks() {
517 let draft = Draft::from_lines(&[
518 DraftLine {
519 kind: LineKind::Code(None),
520 number: None,
521 content: "a < b".into(),
522 marks: Vec::new(),
523 },
524 DraftLine {
525 kind: LineKind::Code(None),
526 number: None,
527 content: " c".into(),
528 marks: Vec::new(),
529 },
530 ]);
531 let html = draft.to_html();
532 assert_eq!(
533 html,
534 "<pre class=\"language-plaintext skipProofing\" spellcheck=\"false\"><code>a &lt; b<br> c</code></pre>"
535 );
536 let back = Draft::from_spans(&html_to_spans(&html));
537 assert_eq!(back.lines(), [LineKind::Code(None), LineKind::Code(None)]);
538 assert_eq!(back.text(), "\u{FFF9}a < b\n c");
539}