mathtex-editor-core 0.3.0

Headless core of the mathtex structural math editor: model, operations, navigation, selection, IR matching
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//! Behavior and regression tests through the public editor API.

use crate::model::Kind;
use crate::*;

fn sym(c: &str) -> Symbol {
    Symbol { latex: c.into(), class: MathClass::Ord }
}

fn atom(c: &str) -> NodeDoc {
    NodeDoc::Atom(sym(c))
}

fn text(s: &str) -> Command {
    Command::InsertText(s.into())
}

fn editor(cmds: impl IntoIterator<Item = Command>) -> Editor {
    let mut ed = Editor::new();
    for c in cmds {
        let _ = ed.exec(c);
    }
    ed
}

fn run(ed: &mut Editor, cmds: impl IntoIterator<Item = Command>) {
    for c in cmds {
        let _ = ed.exec(c);
    }
}

fn tex(ed: &Editor) -> String {
    ed.document().to_tex()
}

fn doc_tex(nodes: Vec<NodeDoc>) -> String {
    Document::new(nodes).to_tex()
}

fn step(node: usize, slot: Slot) -> Step {
    Step { node, slot }
}

fn at(steps: Vec<Step>, index: usize) -> CaretPath {
    CaretPath { steps, index }
}

fn delim(open: char, close: char) -> Command {
    Command::InsertDelimiters { open, close }
}

fn x_squared() -> Editor {
    editor([text("x"), Command::InsertScript(ScriptSlot::Sup), text("2")])
}

#[test]
fn revision_moves_only_on_content_changes() {
    let mut ed = Editor::new();
    let o = ed.exec(text("a"));
    assert!(o.changed && o.moved);
    assert_eq!((o.revision, ed.revision()), (1, 1));
    let o = ed.exec(Command::Move(Dir::Left));
    assert!(!o.changed && o.moved);
    assert_eq!(o.revision, 1);
    let o = ed.exec(Command::Move(Dir::Left));
    assert_eq!(o.exit, Some(ExitDir::Left));
    assert!(!o.changed && !o.moved);
}

#[test]
fn no_op_commands_report_no_change() {
    let mut ed = editor([text("ab"), Command::Move(Dir::Left), Command::Move(Dir::Left)]);
    for cmd in [Command::DeleteBackward, Command::MatrixDeleteRow, Command::MatrixInsertCol(Side::After), Command::Collapse] {
        let o = ed.exec(cmd.clone());
        assert!(!o.changed, "{cmd:?}");
        assert_eq!(o.revision, 1, "{cmd:?}");
    }
}

#[test]
fn backspace_next_to_structure_selects_then_deletes() {
    let mut ed = editor([Command::InsertFraction(FracStyle::Bar), text("a"), Command::Move(Dir::Down), text("b")]);
    ed.place_at(Edge::End);
    let o = ed.exec(Command::DeleteBackward);
    assert!(!o.changed && o.moved);
    assert_eq!(ed.selection(), Some(Selection { anchor: CaretPath::root(0), focus: CaretPath::root(1) }));
    assert!(ed.exec(Command::DeleteBackward).changed);
    assert!(ed.document().is_empty());
}

#[test]
fn delete_forward_next_to_structure_selects_then_deletes() {
    let mut ed = editor([Command::InsertFraction(FracStyle::Bar)]);
    ed.place_at(Edge::Start);
    let _ = ed.exec(Command::DeleteForward);
    assert!(ed.selection().is_some());
    let _ = ed.exec(Command::DeleteForward);
    assert!(ed.document().is_empty());
}

fn delim_with_menu() -> Editor {
    let mut ed = editor([delim('(', ')'), text("x")]);
    ed.place_at(Edge::End);
    let o = ed.exec(Command::DeleteBackward);
    assert!(!o.changed && !o.moved);
    ed
}

#[test]
fn backspace_next_to_delim_opens_swap_menu() {
    let ed = delim_with_menu();
    let menu = ed.menu().expect("swap menu should be open");
    assert_eq!(menu.items[0], MenuItem { label: "Delete".into(), kind: MenuItemKind::Delete });
    assert!(menu.items[1..].iter().all(|i| i.kind == MenuItemKind::Swap));
    assert!(ed.input_context().menu_open);
    assert_eq!(ed.document().len(), 1);
}

#[test]
fn menu_filters_by_typed_query_but_keeps_delete_pinned() {
    let mut ed = delim_with_menu();
    run(&mut ed, [text("brace")]);
    let menu = ed.menu().unwrap();
    assert_eq!(menu.query, "brace");
    assert_eq!(menu.items[0].kind, MenuItemKind::Delete);
    assert!(menu.items[1..].iter().all(|i| i.label.contains("brace")));
    assert!(!menu.items.iter().any(|i| i.label.contains('[')));
}

/// Regression: Backspace on an empty filter deletes even when another row is highlighted.
#[test]
fn menu_backspace_with_empty_query_confirms_delete() {
    let mut ed = delim_with_menu();
    run(&mut ed, [Command::Move(Dir::Down), Command::Move(Dir::Down)]);
    assert_eq!(ed.menu().unwrap().selected, 2);
    let o = ed.exec(Command::DeleteBackward);
    assert!(o.changed);
    assert!(ed.document().is_empty());
    assert!(ed.menu().is_none());
}

/// Regression: Delete never deleted a swappable structure, it reopened the menu.
#[test]
fn forward_delete_removes_a_swappable_structure() {
    let mut ed = editor([delim('(', ')'), text("x")]);
    ed.place_at(Edge::Start);
    let _ = ed.exec(Command::DeleteForward);
    assert!(ed.menu().is_some());
    let o = ed.exec(Command::DeleteForward);
    assert!(o.changed);
    assert!(ed.document().is_empty());
}

#[test]
fn menu_select_swaps_the_bracket_in_place() {
    let mut ed = delim_with_menu();
    let idx = ed.menu().unwrap().items.iter().position(|i| i.label.starts_with("[ ]")).unwrap();
    assert!(ed.exec(Command::MenuSelect(idx)).changed);
    assert!(ed.menu().is_none());
    assert_eq!(tex(&ed), "\\left[x\\right]");
}

#[test]
fn menu_escape_cancels_without_changes() {
    let mut ed = delim_with_menu();
    let o = ed.exec(Command::Collapse);
    assert!(!o.changed);
    assert!(ed.menu().is_none());
    assert_eq!(tex(&ed), "\\left(x\\right)");
}

/// Regression: under over labels named only the decoration when both slots offered one.
#[test]
fn under_over_labels_name_their_slot_when_both_exist() {
    let spec = UnderOverSpec { over: true, under: true, over_deco: Deco::Brace, under_deco: Deco::None };
    let mut ed = editor([Command::InsertUnderOver(spec), text("x")]);
    ed.place_at(Edge::End);
    let _ = ed.exec(Command::DeleteBackward);
    let labels: Vec<String> = ed.menu().unwrap().items.into_iter().map(|i| i.label).collect();
    assert!(labels.contains(&"over: plain".to_string()), "{labels:?}");
    assert!(labels.contains(&"under: brace".to_string()), "{labels:?}");
    assert!(!labels.contains(&"brace".to_string()), "{labels:?}");
}

#[test]
fn backspace_next_to_text_block_enters_it() {
    let mut ed = editor([Command::InsertStyled(Variant::Text), text("x")]);
    ed.place_at(Edge::End);
    let _ = ed.exec(Command::DeleteBackward);
    assert!(ed.selection().is_none());
    assert!(ed.in_text_slot());
    assert_eq!(ed.cursor(), at(vec![step(0, Slot::Body)], 1));
}

#[test]
fn typing_while_structure_selected_replaces_it() {
    let mut ed = editor([Command::InsertFraction(FracStyle::Bar)]);
    ed.place_at(Edge::End);
    let _ = ed.exec(Command::DeleteBackward);
    let _ = ed.exec(text("z"));
    assert_eq!(tex(&ed), "z");
}

#[test]
fn select_all_then_delete_clears() {
    let mut ed = editor([text("abc"), Command::SelectAll, Command::DeleteBackward]);
    assert!(ed.document().is_empty());
    let o = ed.exec(Command::DeleteBackward);
    assert!(o.close && !o.changed);
}

#[test]
fn drag_keeps_its_anchor_and_promotes_across_structures() {
    let mut ed = editor([text("ab"), Command::InsertFraction(FracStyle::Bar), text("c")]);
    ed.set_cursor(&CaretPath::root(1)).unwrap();
    let _ = ed.exec(Command::ExtendTo(CaretPath::root(0)));
    assert_eq!(ed.selection().unwrap().anchor, CaretPath::root(1));
    let _ = ed.exec(Command::ExtendTo(at(vec![step(2, Slot::Numerator)], 1)));
    let s = ed.selection().unwrap();
    assert_eq!((s.anchor, s.focus), (CaretPath::root(1), CaretPath::root(3)));
}

#[test]
fn arrows_exit_at_the_formula_edges() {
    let mut ed = editor([text("ab")]);
    assert!(ed.at_end() && ed.at_slot_end());
    assert_eq!(ed.exec(Command::Move(Dir::Right)).exit, Some(ExitDir::Right));
    assert_eq!(ed.exec(Command::Move(Dir::Up)).exit, Some(ExitDir::Up));
    assert_eq!(ed.exec(Command::Move(Dir::Down)).exit, Some(ExitDir::Down));
    ed.place_at(Edge::Start);
    assert!(ed.at_start());
    assert_eq!(ed.exec(Command::Move(Dir::Left)).exit, Some(ExitDir::Left));
    // With a selection the edge collapses it instead of exiting.
    let _ = ed.exec(Command::SelectAll);
    let o = ed.exec(Command::Move(Dir::Right));
    assert_eq!(o.exit, None);
    assert!(ed.selection().is_none());
}

#[test]
fn down_in_numerator_enters_denominator() {
    let mut ed = editor([Command::InsertFraction(FracStyle::Bar)]);
    let o = ed.exec(Command::Move(Dir::Down));
    assert_eq!(o.exit, None);
    assert_eq!(ed.cursor(), at(vec![step(0, Slot::Denominator)], 0));
}

/// Regression: Up or Down in a slot without vertical neighbors ignored a movable ancestor.
#[test]
fn vertical_motion_uses_the_nearest_movable_ancestor() {
    let mut ed = editor([Command::InsertFraction(FracStyle::Bar), delim('(', ')'), text("x")]);
    let o = ed.exec(Command::Move(Dir::Down));
    assert!(o.moved);
    assert_eq!(ed.cursor(), at(vec![step(0, Slot::Denominator)], 0));
}

#[test]
fn place_at_clears_selection_and_menu() {
    let mut ed = delim_with_menu();
    ed.place_at(Edge::Start);
    assert!(ed.menu().is_none());
    assert_eq!(ed.cursor(), CaretPath::root(0));
    let _ = ed.exec(Command::SelectAll);
    ed.place_at(Edge::End);
    assert!(ed.selection().is_none());
    assert_eq!(ed.cursor(), CaretPath::root(1));
}

#[test]
fn host_box_policy_enter_stops_and_reports() {
    let mut ed = editor([text("a"), Command::InsertHostBox(17), text("b")]);
    ed.set_cursor(&CaretPath::root(1)).unwrap();
    let o = ed.exec(Command::Move(Dir::Right));
    assert_eq!((o.entered_host_box, ed.cursor()), (None, CaretPath::root(2)));
    ed.set_host_box_policy(HostBoxPolicy::Enter);
    let o = ed.exec(Command::Move(Dir::Left));
    assert_eq!(o.entered_host_box, Some(HostBoxEntry { token: 17, side: Side::After }));
    assert!(!o.moved);
    ed.set_cursor(&CaretPath::root(1)).unwrap();
    let o = ed.exec(Command::Move(Dir::Right));
    assert_eq!(o.entered_host_box, Some(HostBoxEntry { token: 17, side: Side::Before }));
    assert_eq!(ed.cursor(), CaretPath::root(1));
}

#[test]
fn host_box_deletes_whole_and_restore_brings_it_back() {
    let mut ed = editor([Command::InsertHostBox(17)]);
    let before = ed.snapshot();
    assert!(ed.exec(Command::DeleteBackward).changed);
    assert!(ed.document().is_empty());
    let rev = ed.revision();
    ed.restore(&before).unwrap();
    assert_eq!(ed.revision(), rev + 1);
    assert_eq!(ed.document().host_tokens().into_iter().collect::<Vec<_>>(), vec![17]);
    assert_eq!(ed.cursor(), CaretPath::root(1));
}

#[test]
fn snapshots_round_trip_through_json() {
    let mut ed = editor([Command::InsertFraction(FracStyle::Bar), text("ab")]);
    let _ = ed.exec(Command::Extend(Dir::Left));
    let s = ed.snapshot();
    let json = serde_json::to_string(&s).unwrap();
    let back: Snapshot = serde_json::from_str(&json).unwrap();
    let mut other = Editor::new();
    other.restore(&back).unwrap();
    assert_eq!(other.snapshot(), s);
}

#[test]
fn restore_rejects_bad_paths_without_touching_state() {
    let mut ed = editor([text("ab")]);
    let mut s = ed.snapshot();
    s.cursor = CaretPath::root(9);
    assert_eq!(ed.restore(&s), Err(RestoreError::Path(PathError::GapOutOfRange { len: 2, index: 9 })));
    assert_eq!((ed.revision(), ed.cursor()), (1, CaretPath::root(2)));
    let err = ed.set_selection(&CaretPath::root(0), &at(vec![step(0, Slot::Numerator)], 0));
    assert_eq!(err, Err(PathError::NoSlot { depth: 0 }));
}

#[test]
fn set_selection_requires_one_sequence() {
    let mut ed = editor([text("a"), Command::InsertFraction(FracStyle::Bar)]);
    let err = ed.set_selection(&CaretPath::root(0), &at(vec![step(1, Slot::Numerator)], 0));
    assert_eq!(err, Err(PathError::SplitSelection));
    ed.set_selection(&CaretPath::root(0), &CaretPath::root(2)).unwrap();
    assert_eq!(ed.selection_tex().as_deref(), Some("a\\frac{}{}"));
}

/// Regression: the clipboard fragment sliced the root even for a selection inside a slot.
#[test]
fn selection_document_is_slot_aware() {
    let mut ed = editor([text("q"), Command::InsertFraction(FracStyle::Bar), text("ab")]);
    let _ = ed.exec(Command::Extend(Dir::Left));
    let doc = ed.selection_document().unwrap();
    assert_eq!(doc, Document::new(vec![atom("b")]));
    let _ = ed.exec(Command::Collapse);
    assert_eq!(ed.selection_document(), None);
}

#[test]
fn documents_paste_and_carry_host_boxes() {
    let mut ed = editor([text("a"), Command::InsertHostBox(17), text("b"), Command::SelectAll]);
    let doc = ed.selection_document().unwrap();
    assert!(matches!(doc.root()[1], NodeDoc::HostBox { token: 17 }));
    assert_eq!(ed.selection_tex().as_deref(), Some("a\\hostbox{17}b"));
    let _ = ed.exec(Command::Collapse);
    assert!(ed.exec(Command::InsertDocument(doc)).changed);
    assert_eq!(tex(&ed), "a\\hostbox{17}ba\\hostbox{17}b");
}

/// Regression: Right into `x^2` then `^3` nested a Script in the base and exported `x^3^2`.
#[test]
fn script_from_a_script_base_targets_the_owning_script() {
    let mut ed = x_squared();
    ed.place_at(Edge::Start);
    run(&mut ed, [Command::Move(Dir::Right), Command::InsertScript(ScriptSlot::Sup), text("3")]);
    assert_eq!(tex(&ed), "x^{23}");
    run(&mut ed, [Command::Move(Dir::Left), Command::Move(Dir::Left), Command::Move(Dir::Left)]);
    run(&mut ed, [Command::InsertScript(ScriptSlot::Sub), text("i")]);
    assert_eq!(tex(&ed), "x_i^{23}");
}

/// Regression: Backspace in an empty radicand or under over base discarded the other slots.
#[test]
fn backspace_in_empty_radicand_or_base_steps_into_the_filled_slot() {
    let mut ed = editor([Command::InsertSqrt, Command::Move(Dir::Up), text("3"), Command::Move(Dir::Down)]);
    let o = ed.exec(Command::DeleteBackward);
    assert!(!o.changed);
    assert_eq!(ed.cursor(), at(vec![step(0, Slot::Index)], 1));
    assert_eq!(tex(&ed), "\\sqrt[{3}]{}");
    let spec = UnderOverSpec { over: true, under: false, over_deco: Deco::None, under_deco: Deco::None };
    let mut ed = editor([Command::InsertUnderOver(spec), Command::Move(Dir::Up), text("a"), Command::Move(Dir::Down)]);
    let o = ed.exec(Command::DeleteBackward);
    assert!(!o.changed);
    assert_eq!(ed.cursor(), at(vec![step(0, Slot::Over)], 1));
}

/// Regression: MoveLineStart, Extend, and set_cursor reached the illegal script base start.
#[test]
fn illegal_script_base_start_is_never_a_resting_position() {
    let mut ed = x_squared();
    ed.set_cursor(&at(vec![step(0, Slot::Base)], 1)).unwrap();
    let _ = ed.exec(Command::MoveLineStart);
    assert_eq!(ed.cursor(), CaretPath::root(0));
    ed.set_cursor(&at(vec![step(0, Slot::Base)], 0)).unwrap();
    assert_eq!(ed.cursor(), CaretPath::root(0));
    ed.set_cursor(&at(vec![step(0, Slot::Base)], 1)).unwrap();
    let _ = ed.exec(Command::Extend(Dir::Left));
    assert_eq!(ed.selection(), Some(Selection { anchor: CaretPath::root(1), focus: CaretPath::root(0) }));
}

/// Regression: typed characters with TeX meaning leaked into the output raw.
#[test]
fn typed_characters_are_escaped_or_mapped() {
    let latex = |c| Symbol::from_char(c).map(|s| s.latex);
    assert_eq!(latex('^').as_deref(), Some("\\text{\\textasciicircum}"));
    assert_eq!(latex('\'').as_deref(), Some("\\prime"));
    assert_eq!((latex('\n'), latex('\t'), latex('\u{7}')), (None, None, None));
    assert_eq!(latex('𝑥').as_deref(), Some("𝑥"));
    assert_eq!(latex('ℝ').as_deref(), Some("ℝ"));
    assert_eq!(latex('é').as_deref(), Some("\\text{é}"));
    assert_eq!(Symbol::from_char('\u{2212}').unwrap().class, MathClass::Bin);
    let ed = editor([text("50%&$^'\n")]);
    assert_eq!(tex(&ed), "50\\%\\&\\$\\text{\\textasciicircum}\\prime");
}

/// Regression: big operators and scripts ignored an active selection.
#[test]
fn big_operators_replace_and_scripts_wrap_the_selection() {
    let op = Symbol { latex: "\\sum".into(), class: MathClass::Op };
    let ed = editor([text("ab"), Command::SelectAll, Command::InsertBigOp(op)]);
    assert_eq!(tex(&ed), "\\sum");
    let mut ed = editor([text("ab"), Command::SelectAll, Command::InsertScript(ScriptSlot::Sup), text("2")]);
    assert_eq!(tex(&ed), "{ab}^2");
    assert_eq!(ed.cursor(), at(vec![step(0, Slot::Sup)], 1));
    let _ = ed.exec(Command::SelectAll);
    assert!(ed.selection().is_some());
}

/// Regression: matrix, big operator, and document inserts left several items in a script base.
#[test]
fn inserts_into_a_script_base_keep_one_base_item() {
    let base_end = at(vec![step(0, Slot::Base)], 1);
    let op = Symbol { latex: "\\int".into(), class: MathClass::Op };
    let paste = Document::new(vec![atom("a"), atom("b")]);
    let cases = [
        (Command::InsertMatrix { env: MatrixEnv::Matrix, rows: 1, cols: 1 }, "x{\\begin{matrix}\\end{matrix}}^2"),
        (Command::InsertBigOp(op), "x{\\int}^2"),
        (Command::InsertDocument(paste), "xab^2"),
    ];
    for (cmd, expected) in cases {
        let mut ed = x_squared();
        ed.set_cursor(&base_end).unwrap();
        assert!(ed.exec(cmd.clone()).changed, "{cmd:?}");
        let root = ed.tree().root();
        let script = *ed.tree().items(root).last().unwrap();
        assert_eq!(ed.tree().len(ed.tree().child_seqs(script)[0]), 1, "{cmd:?}");
        assert_eq!(tex(&ed), expected, "{cmd:?}");
    }
}

/// Regression: Tab gave up after 100000 steps and deleting a selection was quadratic.
#[test]
fn tab_and_selection_delete_scale_linearly() {
    let mut nodes: Vec<NodeDoc> = (0..100_001).map(|_| atom("a")).collect();
    nodes.push(NodeDoc::Frac { num: vec![], den: vec![], style: FracStyle::Bar });
    let mut ed = Editor::from_document(&Document::new(nodes)).unwrap();
    let _ = ed.exec(Command::Tab);
    assert_eq!(ed.cursor(), at(vec![step(100_001, Slot::Numerator)], 0));
    let _ = ed.exec(Command::Tab);
    assert_eq!(ed.cursor(), at(vec![step(100_001, Slot::Denominator)], 0));
    assert_eq!(ed.exec(Command::Tab).exit, Some(ExitDir::Right));
    let _ = ed.exec(Command::SelectAll);
    let _ = ed.exec(Command::DeleteBackward);
    assert!(ed.document().is_empty());
}

#[test]
fn tab_fills_big_operator_limits_lower_first() {
    let op = Symbol { latex: "\\sum".into(), class: MathClass::Op };
    let mut ed = editor([Command::InsertBigOp(op), Command::Tab]);
    assert_eq!(ed.cursor(), at(vec![step(0, Slot::Lower)], 0));
    let _ = ed.exec(Command::Tab);
    assert_eq!(ed.cursor(), at(vec![step(0, Slot::Upper)], 0));
}

/// Regression: display export of an empty base gave `x^2^3` or `a^3`, and bases were never braced.
#[test]
fn script_bases_are_braced_when_needed() {
    let script = |base, sup| NodeDoc::Script { base, sub: None, sup: Some(sup) };
    assert_eq!(doc_tex(vec![script(vec![], vec![atom("2")])]), "{}^2");
    assert_eq!(doc_tex(vec![script(vec![script(vec![atom("x")], vec![atom("2")])], vec![atom("3")])]), "{x^2}^3");
    assert_eq!(doc_tex(vec![script(vec![atom("a"), atom("b")], vec![atom("3")])]), "{ab}^3");
    let ed = editor([Command::InsertScript(ScriptSlot::Sup)]);
    assert_eq!(ed.source().tex, "{\\phantom{x}}^{\\phantom{x}}");
    assert_eq!(ed.display_source().tex, "{}");
}

fn under_over(over: Option<&str>, under: Option<&str>, over_deco: Deco, under_deco: Deco) -> NodeDoc {
    let label = |l: Option<&str>| l.map(|s| if s.is_empty() { vec![] } else { vec![atom(s)] });
    NodeDoc::UnderOver { base: vec![atom("x")], over: label(over), under: label(under), over_deco, under_deco }
}

/// Regression: arrow and line decorations exported as a plain `\overset`.
#[test]
fn under_over_decorations_export_their_commands() {
    let cases = [
        (under_over(Some("a"), None, Deco::None, Deco::None), "\\overset{a}{x}"),
        (under_over(Some("a"), None, Deco::Arrow, Deco::None), "\\overset{a}{\\overrightarrow{x}}"),
        (under_over(None, Some("b"), Deco::None, Deco::Line), "\\underset{b}{\\underline{x}}"),
        (under_over(Some("a"), Some("b"), Deco::Brace, Deco::Brace), "\\overbrace{\\underbrace{x}_{b}}^{a}"),
        (under_over(Some("a"), Some("b"), Deco::Line, Deco::Arrow), "\\overset{a}{\\overline{\\underset{b}{\\underrightarrow{x}}}}"),
        (under_over(Some(""), None, Deco::Arrow, Deco::None), "\\overrightarrow{x}"),
        (under_over(Some(""), None, Deco::Brace, Deco::None), "\\overbrace{x}"),
        (under_over(Some(""), None, Deco::None, Deco::None), "x"),
    ];
    for (node, expected) in cases {
        assert_eq!(doc_tex(vec![node]), expected);
    }
}

/// Regression: a script on an overbrace base doubled the superscript.
#[test]
fn script_on_an_overbrace_braces_the_base() {
    let base = under_over(Some("a"), None, Deco::Brace, Deco::None);
    let node = NodeDoc::Script { base: vec![base], sub: None, sup: Some(vec![atom("2")]) };
    assert_eq!(doc_tex(vec![node]), "{\\overbrace{x}^{a}}^2");
}

/// Regression: `\text{}` used math escapes, dropped spaces, and accepted structures.
#[test]
fn text_slots_use_text_mode() {
    let mut ed = editor([Command::InsertStyled(Variant::Text), text("a b%~\\")]);
    assert!(ed.input_context().in_text_slot);
    assert_eq!(tex(&ed), "\\text{a\\ b\\%\\textasciitilde{}\\textbackslash{}}");
    for cmd in [
        Command::InsertFraction(FracStyle::Bar),
        Command::InsertScript(ScriptSlot::Sup),
        Command::InsertMatrix { env: MatrixEnv::Matrix, rows: 1, cols: 1 },
        Command::InsertStyled(Variant::Bold),
        Command::InsertDocument(Document::new(vec![NodeDoc::Sqrt { index: vec![], radicand: vec![] }])),
    ] {
        assert!(!ed.exec(cmd.clone()).changed, "{cmd:?}");
    }
    let _ = ed.exec(Command::InsertAtom(sym("\\alpha")));
    assert!(tex(&ed).ends_with("\\ensuremath{\\alpha}}"));
    assert_eq!(tex(&editor([text("a b")])), "ab");
    let mut ed = editor([Command::InsertStyled(Variant::Text), text("ab"), Command::Extend(Dir::Left)]);
    assert_eq!(ed.selection_tex().as_deref(), Some("\\text{b}"));
    let _ = ed.exec(Command::SelectAll);
    assert_eq!(ed.selection_tex().as_deref(), Some("\\text{ab}"));
}

/// Regression: separators were decided per node kind, so some control words ran into letters.
#[test]
fn one_separator_rule_for_every_export() {
    let alpha = || atom("\\alpha");
    assert_eq!(doc_tex(vec![alpha(), atom("x")]), "\\alpha x");
    let script = NodeDoc::Script { base: vec![alpha()], sub: None, sup: Some(vec![]) };
    assert_eq!(doc_tex(vec![script, atom("x")]), "\\alpha x");
    let delims = NodeDoc::Delim { open: '⟨', close: '⟩', body: vec![atom("x")] };
    assert_eq!(doc_tex(vec![delims, atom("y")]), "\\left\\langle x\\right\\rangle y");
    let mut ed = editor([Command::InsertAtom(sym("\\alpha")), text("x"), Command::SelectAll]);
    assert_eq!(ed.selection_tex().as_deref(), Some("\\alpha x"));
    let _ = ed.exec(Command::Collapse);
    let src = ed.source();
    // Spans start after the separator, so the gap before `x` is the `x` itself.
    assert_eq!(src.caret_offset(&CaretPath::root(1)), Some(7));
    assert_eq!(&src.tex[7..], "x");
}

/// Regressions: unbraced degree, clean mode keeping `^{}`, and the amsmath `\atop` warning.
#[test]
fn radical_fraction_and_attachment_export() {
    let sqrt = NodeDoc::Sqrt { index: vec![atom("3")], radicand: vec![atom("x")] };
    assert_eq!(doc_tex(vec![sqrt]), "\\sqrt[{3}]{x}");
    let empty_sup = NodeDoc::Script { base: vec![atom("x")], sub: Some(vec![atom("i")]), sup: Some(vec![]) };
    assert_eq!(doc_tex(vec![empty_sup]), "x_i");
    let atop = NodeDoc::Frac { num: vec![atom("a")], den: vec![atom("b")], style: FracStyle::Atop };
    assert_eq!(doc_tex(vec![atop]), "\\genfrac{}{}{0pt}{}{a}{b}");
    let op = Symbol { latex: "\\sum".into(), class: MathClass::Op };
    assert_eq!(doc_tex(vec![NodeDoc::BigOp { op, lower: vec![], upper: vec![] }, atom("x")]), "\\sum x");
}

#[test]
fn caret_offsets_index_into_the_source() {
    let ed = editor([Command::InsertFraction(FracStyle::Bar), text("a"), Command::Move(Dir::Down), text("b")]);
    let src = ed.source();
    assert_eq!(src.tex, "\\frac{a}{b}");
    assert_eq!(src.caret_offset(&at(vec![step(0, Slot::Numerator)], 1)), Some(7));
    assert_eq!(src.caret_offset(&ed.cursor()), Some(10));
    assert_eq!(src.caret_offset(&CaretPath::root(1)), Some(11));
    assert_eq!(src.caret_offset(&CaretPath::root(2)), None);
    let clean = editor([Command::InsertFraction(FracStyle::Bar)]).display_source();
    assert_eq!(clean.tex, "\\frac{}{}");
    assert_eq!(clean.caret_offset(&at(vec![step(0, Slot::Denominator)], 0)), Some(8));
}

/// Regression: a failed typeset moved the caret to the document start.
#[test]
fn geometry_needs_a_fresh_source_and_real_geometry() {
    let mut ed = editor([text("ab")]);
    let src = ed.source();
    let empty = mathtex_ir::Fragment::default();
    assert_eq!(ed.hit_test(&src, &empty, Point { x: 1.0, y: 1.0 }), Ok(None));
    let out = ed.render(&src, &empty).unwrap();
    assert!(out.selection.is_empty() && out.menu.is_none());
    let _ = ed.exec(text("c"));
    assert_eq!(ed.render(&src, &empty).unwrap_err(), StaleSource { source: 1, editor: 2 });
    let other = editor([text("a"), text("bc")]);
    assert!(other.render(&ed.source(), &empty).is_err());
}

#[test]
fn replace_typed_swaps_a_matching_run() {
    let pi = Symbol { latex: "\\pi".into(), class: MathClass::Ord };
    let mut ed = editor([text("api")]);
    let cmd = |typed: &str| Command::ReplaceTyped { typed: typed.into(), with: vec![Command::InsertAtom(pi.clone())] };
    assert!(!ed.exec(cmd("xi")).changed);
    assert!(!ed.exec(cmd("zapi")).changed);
    assert!(ed.exec(cmd("pi")).changed);
    assert_eq!(tex(&ed), "a\\pi");
    let mut ed = editor([text("5%")]);
    let o = ed.exec(Command::ReplaceTyped { typed: "%".into(), with: vec![text("x")] });
    assert!(o.changed);
    assert_eq!(tex(&ed), "5x");
}

#[test]
fn close_delimiter_leaves_matching_delimiters_at_their_end() {
    let mut ed = editor([delim('(', ')'), text("x"), Command::InsertScript(ScriptSlot::Sup), text("2")]);
    assert_eq!(ed.input_context().closing_delimiter, Some(')'));
    let o = ed.exec(Command::CloseDelimiter(')'));
    assert!(o.moved && !o.changed);
    assert_eq!(ed.cursor(), CaretPath::root(1));
    assert_eq!(ed.input_context().closing_delimiter, None);
    let _ = ed.exec(Command::CloseDelimiter(')'));
    assert_eq!(tex(&ed), "\\left(x^2\\right))");
    let mut ed = editor([delim('[', ']'), text("ab"), Command::Move(Dir::Left)]);
    assert_eq!(ed.input_context().closing_delimiter, None);
    let _ = ed.exec(Command::CloseDelimiter(']'));
    assert_eq!(tex(&ed), "\\left[a]b\\right]");
}

#[test]
fn nesting_stops_at_the_depth_cap() {
    let mut ed = Editor::new();
    for _ in 0..MAX_DEPTH {
        assert!(ed.exec(delim('(', ')')).changed);
    }
    for cmd in [delim('(', ')'), Command::InsertFraction(FracStyle::Bar), Command::InsertScript(ScriptSlot::Sub)] {
        assert!(!ed.exec(cmd.clone()).changed, "{cmd:?}");
    }
    assert!(ed.exec(text("x")).changed);
    let _ = ed.exec(Command::InsertScript(ScriptSlot::Sub));
    assert_eq!(ed.document().validate(), Ok(()));
    let mut wrapped = Editor::from_document(&ed.document()).unwrap();
    let _ = wrapped.exec(Command::SelectAll);
    assert!(!wrapped.exec(Command::InsertSqrt).changed);
}

#[test]
fn a_repaired_ragged_matrix_loads_and_row_ops_stay_live() {
    let ragged = NodeDoc::Matrix { env: MatrixEnv::Bmatrix, rows: vec![vec![vec![atom("a")], vec![]], vec![]] };
    let mut doc = Document::new(vec![ragged]);
    assert_eq!(Editor::from_document(&doc).err(), Some(DocumentError::RaggedMatrix));
    assert_eq!(doc.repair(), [Repair::PaddedMatrix]);
    let mut ed = Editor::from_document(&doc).unwrap();
    assert_eq!(ed.document(), doc);
    ed.set_cursor(&at(vec![step(0, Slot::Cell { row: 1, col: 1 })], 0)).unwrap();
    assert_eq!(ed.matrix_shape(), Some((2, 2)));
    for cmd in [Command::MatrixDeleteRow, Command::MatrixDeleteCol, Command::MatrixDeleteRow, Command::MatrixDeleteCol] {
        let _ = ed.exec(cmd);
        assert!(ed.tree().seqs.contains_key(ed.raw_cursor().seq));
    }
    assert_eq!(ed.matrix_shape(), Some((1, 1)));
    let _ = ed.exec(Command::MatrixInsertCol(Side::Before));
    assert_eq!(ed.cursor(), at(vec![step(0, Slot::Cell { row: 0, col: 0 })], 0));
    assert_eq!(tex(&ed), "\\begin{bmatrix} & a\\end{bmatrix}");
}

#[test]
fn documents_from_json_must_validate() {
    let good = r#"{"version":1,"root":[{"type":"atom","data":{"latex":"x","class":"ord"}}]}"#;
    let doc: Document = serde_json::from_str(good).unwrap();
    assert_eq!(Editor::from_document(&doc).unwrap().document(), doc);
    let bad = r#"{"version":1,"root":[{"type":"atom","data":{"latex":"}","class":"ord"}}]}"#;
    assert!(serde_json::from_str::<Document>(bad).is_err());
}

#[test]
fn invalid_symbols_and_delimiters_are_refused() {
    let mut ed = Editor::new();
    assert!(!ed.exec(Command::InsertAtom(sym("%"))).changed);
    assert!(!ed.exec(Command::InsertBigOp(sym("{"))).changed);
    assert!(!ed.exec(delim('x', ')')).changed);
    assert!(ed.document().is_empty());
}

#[test]
fn menu_anchor_follows_render() {
    let ed = delim_with_menu();
    let src = ed.source();
    let out = ed.render(&src, &mathtex_ir::Fragment::default()).unwrap();
    assert!(out.menu.is_some());
    assert!(ed.menu_anchor().is_some());
    assert_eq!(ed.raw_anchor(), None);
    assert!(matches!(ed.tree().kind(ed.menu_anchor().unwrap()), Some(Kind::Delim { .. })));
}

#[test]
fn input_context_serial_counts_state_changing_calls_only() {
    let mut ed = Editor::new();
    let s0 = ed.input_context().serial;
    let _ = ed.exec(text("ab"));
    let _ = ed.exec(Command::Move(Dir::Right));
    assert_eq!(ed.input_context().serial, s0 + 2);
    let snap = ed.snapshot();
    let _ = (ed.source(), ed.cursor(), ed.menu(), ed.at_start());
    assert_eq!(ed.input_context().serial, s0 + 2);
    ed.set_cursor(&CaretPath::root(1)).unwrap();
    ed.restore(&snap).unwrap();
    ed.place_at(Edge::Start);
    ed.set_selection(&CaretPath::root(0), &CaretPath::root(2)).unwrap();
    assert_eq!(ed.input_context().serial, s0 + 6);
    assert!(ed.set_cursor(&CaretPath::root(9)).is_err());
    assert_eq!(ed.input_context().serial, s0 + 6);
}

#[test]
fn typed_characters_and_named_commands_share_one_class_table() {
    let pairs = [('~', "\\sim"), ('{', "\\{"), ('}', "\\}"), ('⟨', "\\langle"), ('∘', "\\circ"), ('→', "\\rightarrow")];
    for (c, latex) in pairs {
        assert_eq!(Symbol::from_char(c).unwrap().class, Symbol::from_latex(latex).class, "{c} and {latex}");
    }
    assert_eq!(Symbol::from_latex("\\sin").class, MathClass::Op);
    assert_eq!(Symbol::from_latex("\\ldots").class, MathClass::Inner);
    assert_eq!(Symbol::from_latex("\\land").class, MathClass::Bin);
    assert_eq!(Symbol::from_latex("\\pi").class, MathClass::Ord);
}

#[test]
fn invalid_documents_and_tokens_are_refused_not_repaired() {
    let mut ed = editor([text("a")]);
    let bad = Document::new(vec![NodeDoc::Script { base: vec![], sub: None, sup: None }]);
    assert!(!ed.exec(Command::InsertDocument(bad.clone())).changed);
    assert!(!ed.exec(Command::InsertHostBox(MAX_HOST_TOKEN + 1)).changed);
    assert!(ed.exec(Command::InsertHostBox(MAX_HOST_TOKEN)).changed);
    assert_eq!(tex(&ed), "a\\hostbox{2147483647}");
    let snapshot = Snapshot { document: bad, cursor: CaretPath::root(0), selection: None };
    assert_eq!(ed.restore(&snapshot), Err(RestoreError::Document(DocumentError::EmptyScript)));
    assert_eq!(tex(&ed), "a\\hostbox{2147483647}");
}