ratkit 0.2.16

A comprehensive collection of reusable TUI components for ratatui including resizable splits, tree views, markdown rendering, toast notifications, dialogs, and terminal embedding
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
use ratatui::layout::Rect;
use ratatui_toolkit::resizable_split::{ResizableSplit, SplitDirection};

// ============================================================================
// INITIALIZATION TESTS
// ============================================================================

#[test]
fn test_new_creates_vertical_split_by_default() {
    let split = ResizableSplit::new(70);
    assert_eq!(split.direction, SplitDirection::Vertical);
    assert_eq!(split.split_percent, 70);
    assert_eq!(split.min_percent, 10);
    assert_eq!(split.max_percent, 90);
    assert!(!split.is_dragging);
    assert!(!split.is_hovering);
    assert_eq!(split.divider_pos, 0);
}

#[test]
fn test_new_with_direction_vertical() {
    let split = ResizableSplit::new_with_direction(60, SplitDirection::Vertical);
    assert_eq!(split.direction, SplitDirection::Vertical);
    assert_eq!(split.split_percent, 60);
}

#[test]
fn test_new_with_direction_horizontal() {
    let split = ResizableSplit::new_with_direction(40, SplitDirection::Horizontal);
    assert_eq!(split.direction, SplitDirection::Horizontal);
    assert_eq!(split.split_percent, 40);
}

#[test]
fn test_default_creates_70_percent_vertical_split() {
    let split = ResizableSplit::default();
    assert_eq!(split.direction, SplitDirection::Vertical);
    assert_eq!(split.split_percent, 70);
}

// ============================================================================
// PERCENTAGE CLAMPING TESTS (INITIALIZATION)
// ============================================================================

#[test]
fn test_new_clamps_percentage_to_min_5() {
    let split = ResizableSplit::new(0);
    assert_eq!(split.split_percent, 5);

    let split = ResizableSplit::new(3);
    assert_eq!(split.split_percent, 5);
}

#[test]
fn test_new_clamps_percentage_to_max_95() {
    let split = ResizableSplit::new(100);
    assert_eq!(split.split_percent, 95);

    let split = ResizableSplit::new(150);
    assert_eq!(split.split_percent, 95);

    let split = ResizableSplit::new(u16::MAX);
    assert_eq!(split.split_percent, 95);
}

#[test]
fn test_new_accepts_valid_percentages() {
    let split = ResizableSplit::new(5);
    assert_eq!(split.split_percent, 5);

    let split = ResizableSplit::new(50);
    assert_eq!(split.split_percent, 50);

    let split = ResizableSplit::new(95);
    assert_eq!(split.split_percent, 95);
}

#[test]
fn test_new_with_direction_clamps_percentage() {
    let split = ResizableSplit::new_with_direction(0, SplitDirection::Vertical);
    assert_eq!(split.split_percent, 5);

    let split = ResizableSplit::new_with_direction(200, SplitDirection::Horizontal);
    assert_eq!(split.split_percent, 95);
}

// ============================================================================
// UPDATE_DIVIDER_POSITION TESTS - VERTICAL
// ============================================================================

#[test]
fn test_update_divider_position_vertical_50_percent() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);

    split.update_divider_position(area);
    // 50% of 100 columns = 50
    assert_eq!(split.divider_pos, 50);
}

#[test]
fn test_update_divider_position_vertical_70_percent() {
    let mut split = ResizableSplit::new(70);
    let area = Rect::new(0, 0, 100, 20);

    split.update_divider_position(area);
    // 70% of 100 columns = 70
    assert_eq!(split.divider_pos, 70);
}

#[test]
fn test_update_divider_position_vertical_with_offset() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(10, 5, 100, 20);

    split.update_divider_position(area);
    // 50% of 100 columns = 50, plus offset 10 = 60
    assert_eq!(split.divider_pos, 60);
}

#[test]
fn test_update_divider_position_vertical_odd_width() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 99, 20);

    split.update_divider_position(area);
    // 50% of 99 = 49.5, which rounds down to 49
    assert_eq!(split.divider_pos, 49);
}

#[test]
fn test_update_divider_position_vertical_small_area() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 10, 5);

    split.update_divider_position(area);
    // 50% of 10 = 5
    assert_eq!(split.divider_pos, 5);
}

#[test]
fn test_update_divider_position_vertical_extreme_percentages() {
    let mut split = ResizableSplit::new(5);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);
    // 5% of 100 = 5
    assert_eq!(split.divider_pos, 5);

    let mut split = ResizableSplit::new(95);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);
    // 95% of 100 = 95
    assert_eq!(split.divider_pos, 95);
}

// ============================================================================
// UPDATE_DIVIDER_POSITION TESTS - HORIZONTAL
// ============================================================================

#[test]
fn test_update_divider_position_horizontal_50_percent() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);

    split.update_divider_position(area);
    // 50% of 20 rows = 10
    assert_eq!(split.divider_pos, 10);
}

#[test]
fn test_update_divider_position_horizontal_70_percent() {
    let mut split = ResizableSplit::new_with_direction(70, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);

    split.update_divider_position(area);
    // 70% of 20 rows = 14
    assert_eq!(split.divider_pos, 14);
}

#[test]
fn test_update_divider_position_horizontal_with_offset() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(10, 5, 100, 20);

    split.update_divider_position(area);
    // 50% of 20 rows = 10, plus offset 5 = 15
    assert_eq!(split.divider_pos, 15);
}

#[test]
fn test_update_divider_position_horizontal_odd_height() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 21);

    split.update_divider_position(area);
    // 50% of 21 = 10.5, which rounds down to 10
    assert_eq!(split.divider_pos, 10);
}

#[test]
fn test_update_divider_position_horizontal_small_area() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 10, 8);

    split.update_divider_position(area);
    // 50% of 8 = 4
    assert_eq!(split.divider_pos, 4);
}

#[test]
fn test_update_divider_position_horizontal_extreme_percentages() {
    let mut split = ResizableSplit::new_with_direction(5, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 100);
    split.update_divider_position(area);
    // 5% of 100 = 5
    assert_eq!(split.divider_pos, 5);

    let mut split = ResizableSplit::new_with_direction(95, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 100);
    split.update_divider_position(area);
    // 95% of 100 = 95
    assert_eq!(split.divider_pos, 95);
}

// ============================================================================
// IS_ON_DIVIDER TESTS - VERTICAL (3-UNIT HIT AREA)
// ============================================================================

#[test]
fn test_is_on_divider_vertical_exact_position() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Divider at column 50, should hit exactly
    assert!(split.is_on_divider(50, 10, area));
}

#[test]
fn test_is_on_divider_vertical_one_before() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Column 49 should be in hit area (divider_pos - 1)
    assert!(split.is_on_divider(49, 10, area));
}

#[test]
fn test_is_on_divider_vertical_one_after() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Column 51 should be in hit area (divider_pos + 1)
    assert!(split.is_on_divider(51, 10, area));
}

#[test]
fn test_is_on_divider_vertical_two_before() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Column 48 should be outside hit area
    assert!(!split.is_on_divider(48, 10, area));
}

#[test]
fn test_is_on_divider_vertical_two_after() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Column 52 should be outside hit area
    assert!(!split.is_on_divider(52, 10, area));
}

#[test]
fn test_is_on_divider_vertical_at_left_edge() {
    let mut split = ResizableSplit::new(5);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Divider at column 5
    assert!(split.is_on_divider(5, 10, area));
    assert!(split.is_on_divider(4, 10, area));
    assert!(split.is_on_divider(6, 10, area));
}

#[test]
fn test_is_on_divider_vertical_at_right_edge() {
    let mut split = ResizableSplit::new(95);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Divider at column 95
    assert!(split.is_on_divider(95, 10, area));
    assert!(split.is_on_divider(94, 10, area));
    // Column 96 is clamped to area boundary (99)
    assert!(split.is_on_divider(96, 10, area));
}

#[test]
fn test_is_on_divider_vertical_with_area_offset() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(10, 5, 100, 20);
    split.update_divider_position(area);

    // Divider at column 60 (50 + 10 offset)
    assert!(split.is_on_divider(60, 10, area));
    assert!(split.is_on_divider(59, 10, area));
    assert!(split.is_on_divider(61, 10, area));
    assert!(!split.is_on_divider(58, 10, area));
    assert!(!split.is_on_divider(62, 10, area));
}

#[test]
fn test_is_on_divider_vertical_row_doesnt_matter() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // For vertical split, row position doesn't matter
    assert!(split.is_on_divider(50, 0, area));
    assert!(split.is_on_divider(50, 10, area));
    assert!(split.is_on_divider(50, 19, area));
}

// ============================================================================
// IS_ON_DIVIDER TESTS - HORIZONTAL (3-UNIT HIT AREA)
// ============================================================================

#[test]
fn test_is_on_divider_horizontal_exact_position() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Divider at row 10, should hit exactly
    assert!(split.is_on_divider(50, 10, area));
}

#[test]
fn test_is_on_divider_horizontal_one_before() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Row 9 should be in hit area (divider_pos - 1)
    assert!(split.is_on_divider(50, 9, area));
}

#[test]
fn test_is_on_divider_horizontal_one_after() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Row 11 should be in hit area (divider_pos + 1)
    assert!(split.is_on_divider(50, 11, area));
}

#[test]
fn test_is_on_divider_horizontal_two_before() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Row 8 should be outside hit area
    assert!(!split.is_on_divider(50, 8, area));
}

#[test]
fn test_is_on_divider_horizontal_two_after() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Row 12 should be outside hit area
    assert!(!split.is_on_divider(50, 12, area));
}

#[test]
fn test_is_on_divider_horizontal_at_top_edge() {
    let mut split = ResizableSplit::new_with_direction(5, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 100);
    split.update_divider_position(area);

    // Divider at row 5
    assert!(split.is_on_divider(50, 5, area));
    assert!(split.is_on_divider(50, 4, area));
    assert!(split.is_on_divider(50, 6, area));
}

#[test]
fn test_is_on_divider_horizontal_at_bottom_edge() {
    let mut split = ResizableSplit::new_with_direction(95, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 100);
    split.update_divider_position(area);

    // Divider at row 95
    assert!(split.is_on_divider(50, 95, area));
    assert!(split.is_on_divider(50, 94, area));
    // Row 96 is clamped to area boundary (99)
    assert!(split.is_on_divider(50, 96, area));
}

#[test]
fn test_is_on_divider_horizontal_with_area_offset() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(10, 5, 100, 20);
    split.update_divider_position(area);

    // Divider at row 15 (10 + 5 offset)
    assert!(split.is_on_divider(50, 15, area));
    assert!(split.is_on_divider(50, 14, area));
    assert!(split.is_on_divider(50, 16, area));
    assert!(!split.is_on_divider(50, 13, area));
    assert!(!split.is_on_divider(50, 17, area));
}

#[test]
fn test_is_on_divider_horizontal_column_doesnt_matter() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // For horizontal split, column position doesn't matter
    assert!(split.is_on_divider(0, 10, area));
    assert!(split.is_on_divider(50, 10, area));
    assert!(split.is_on_divider(99, 10, area));
}

// ============================================================================
// START_DRAG / STOP_DRAG STATE MANAGEMENT TESTS
// ============================================================================

#[test]
fn test_start_drag_sets_is_dragging() {
    let mut split = ResizableSplit::new(50);
    assert!(!split.is_dragging);

    split.start_drag();
    assert!(split.is_dragging);
}

#[test]
fn test_stop_drag_clears_is_dragging() {
    let mut split = ResizableSplit::new(50);
    split.start_drag();
    assert!(split.is_dragging);

    split.stop_drag();
    assert!(!split.is_dragging);
}

#[test]
fn test_multiple_start_drag_calls() {
    let mut split = ResizableSplit::new(50);

    split.start_drag();
    assert!(split.is_dragging);

    split.start_drag();
    assert!(split.is_dragging);
}

#[test]
fn test_multiple_stop_drag_calls() {
    let mut split = ResizableSplit::new(50);

    split.stop_drag();
    assert!(!split.is_dragging);

    split.stop_drag();
    assert!(!split.is_dragging);
}

#[test]
fn test_drag_lifecycle() {
    let mut split = ResizableSplit::new(50);

    assert!(!split.is_dragging);
    split.start_drag();
    assert!(split.is_dragging);
    split.stop_drag();
    assert!(!split.is_dragging);
    split.start_drag();
    assert!(split.is_dragging);
    split.stop_drag();
    assert!(!split.is_dragging);
}

// ============================================================================
// UPDATE_FROM_MOUSE TESTS - VERTICAL
// ============================================================================

#[test]
fn test_update_from_mouse_vertical_does_not_update_when_not_dragging() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);

    split.update_from_mouse(75, 10, area);
    assert_eq!(split.split_percent, 50); // Should not change
}

#[test]
fn test_update_from_mouse_vertical_updates_when_dragging() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);

    split.start_drag();
    split.update_from_mouse(75, 10, area);
    assert_eq!(split.split_percent, 75);
}

#[test]
fn test_update_from_mouse_vertical_calculates_percentage_correctly() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();

    split.update_from_mouse(25, 10, area);
    assert_eq!(split.split_percent, 25);

    split.update_from_mouse(75, 10, area);
    assert_eq!(split.split_percent, 75);

    split.update_from_mouse(50, 10, area);
    assert_eq!(split.split_percent, 50);
}

#[test]
fn test_update_from_mouse_vertical_with_area_offset() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(10, 5, 100, 20);
    split.start_drag();

    // Mouse at column 60, area starts at 10, so relative is 50
    // 50 / 100 = 50%
    split.update_from_mouse(60, 10, area);
    assert_eq!(split.split_percent, 50);

    // Mouse at column 85, area starts at 10, so relative is 75
    // 75 / 100 = 75%
    split.update_from_mouse(85, 10, area);
    assert_eq!(split.split_percent, 75);
}

#[test]
fn test_update_from_mouse_vertical_clamps_to_min_percent() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();

    // Try to set to 5% (below min of 10%)
    split.update_from_mouse(5, 10, area);
    assert_eq!(split.split_percent, 10); // Clamped to min_percent

    // Try to set to 0%
    split.update_from_mouse(0, 10, area);
    assert_eq!(split.split_percent, 10);
}

#[test]
fn test_update_from_mouse_vertical_clamps_to_max_percent() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();

    // Try to set to 95% (above max of 90%)
    split.update_from_mouse(95, 10, area);
    assert_eq!(split.split_percent, 90); // Clamped to max_percent

    // Try to set to 100%
    split.update_from_mouse(99, 10, area);
    assert_eq!(split.split_percent, 90);
}

#[test]
fn test_update_from_mouse_vertical_mouse_before_area() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(10, 5, 100, 20);
    split.start_drag();

    // Mouse at column 5, before area starts (area.x = 10)
    // saturating_sub will give 0
    split.update_from_mouse(5, 10, area);
    assert_eq!(split.split_percent, 10); // Clamped to min_percent
}

#[test]
fn test_update_from_mouse_vertical_mouse_after_area() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();

    // Mouse at column 150, beyond area width (100)
    // relative = 150, (150 * 100) / 100 = 150%
    split.update_from_mouse(150, 10, area);
    assert_eq!(split.split_percent, 90); // Clamped to max_percent
}

#[test]
fn test_update_from_mouse_vertical_zero_width_area() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 0, 20);
    split.start_drag();

    // When area width is 0, should not update
    split.update_from_mouse(10, 10, area);
    assert_eq!(split.split_percent, 50); // Should remain unchanged
}

// ============================================================================
// UPDATE_FROM_MOUSE TESTS - HORIZONTAL
// ============================================================================

#[test]
fn test_update_from_mouse_horizontal_does_not_update_when_not_dragging() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);

    split.update_from_mouse(50, 15, area);
    assert_eq!(split.split_percent, 50); // Should not change
}

#[test]
fn test_update_from_mouse_horizontal_updates_when_dragging() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);

    split.start_drag();
    split.update_from_mouse(50, 15, area);
    // 15 / 20 = 75%
    assert_eq!(split.split_percent, 75);
}

#[test]
fn test_update_from_mouse_horizontal_calculates_percentage_correctly() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();

    split.update_from_mouse(50, 5, area);
    assert_eq!(split.split_percent, 25); // 5 / 20 = 25%

    split.update_from_mouse(50, 10, area);
    assert_eq!(split.split_percent, 50); // 10 / 20 = 50%

    split.update_from_mouse(50, 15, area);
    assert_eq!(split.split_percent, 75); // 15 / 20 = 75%
}

#[test]
fn test_update_from_mouse_horizontal_with_area_offset() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(10, 5, 100, 20);
    split.start_drag();

    // Mouse at row 15, area starts at 5, so relative is 10
    // 10 / 20 = 50%
    split.update_from_mouse(50, 15, area);
    assert_eq!(split.split_percent, 50);

    // Mouse at row 20, area starts at 5, so relative is 15
    // 15 / 20 = 75%
    split.update_from_mouse(50, 20, area);
    assert_eq!(split.split_percent, 75);
}

#[test]
fn test_update_from_mouse_horizontal_clamps_to_min_percent() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();

    // Try to set to 5% (below min of 10%)
    split.update_from_mouse(50, 1, area);
    assert_eq!(split.split_percent, 10); // Clamped to min_percent

    // Try to set to 0%
    split.update_from_mouse(50, 0, area);
    assert_eq!(split.split_percent, 10);
}

#[test]
fn test_update_from_mouse_horizontal_clamps_to_max_percent() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();

    // Try to set to 95% (above max of 90%)
    split.update_from_mouse(50, 19, area);
    assert_eq!(split.split_percent, 90); // Clamped to max_percent

    // Try to set to 100%
    split.update_from_mouse(50, 20, area);
    assert_eq!(split.split_percent, 90);
}

#[test]
fn test_update_from_mouse_horizontal_mouse_before_area() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(10, 5, 100, 20);
    split.start_drag();

    // Mouse at row 2, before area starts (area.y = 5)
    // saturating_sub will give 0
    split.update_from_mouse(50, 2, area);
    assert_eq!(split.split_percent, 10); // Clamped to min_percent
}

#[test]
fn test_update_from_mouse_horizontal_mouse_after_area() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();

    // Mouse at row 50, beyond area height (20)
    // relative = 50, (50 * 100) / 20 = 250%
    split.update_from_mouse(50, 50, area);
    assert_eq!(split.split_percent, 90); // Clamped to max_percent
}

#[test]
fn test_update_from_mouse_horizontal_zero_height_area() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 0);
    split.start_drag();

    // When area height is 0, should not update
    split.update_from_mouse(50, 10, area);
    assert_eq!(split.split_percent, 50); // Should remain unchanged
}

// ============================================================================
// BOUNDARY CONDITION TESTS
// ============================================================================

#[test]
fn test_boundary_minimum_area_size_vertical() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 1, 1);

    split.update_divider_position(area);
    // 50% of 1 = 0.5, rounds down to 0
    assert_eq!(split.divider_pos, 0);
}

#[test]
fn test_boundary_minimum_area_size_horizontal() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 1, 1);

    split.update_divider_position(area);
    // 50% of 1 = 0.5, rounds down to 0
    assert_eq!(split.divider_pos, 0);
}

#[test]
fn test_boundary_max_area_offset_vertical() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(u16::MAX - 100, 0, 100, 20);

    split.update_divider_position(area);
    let expected = (u16::MAX - 100) + 50;
    assert_eq!(split.divider_pos, expected);
}

#[test]
fn test_boundary_max_area_offset_horizontal() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, u16::MAX - 100, 100, 100);

    split.update_divider_position(area);
    let expected = (u16::MAX - 100) + 50;
    assert_eq!(split.divider_pos, expected);
}

#[test]
fn test_boundary_divider_at_position_zero() {
    let mut split = ResizableSplit::new(5);
    let area = Rect::new(0, 0, 100, 20);
    split.update_divider_position(area);

    // Divider at 5, hit area includes columns 4, 5, 6
    // But column 4 - 1 = 3 due to saturating_sub
    assert!(split.is_on_divider(4, 10, area));
    assert!(split.is_on_divider(5, 10, area));
    assert!(split.is_on_divider(6, 10, area));
}

#[test]
fn test_boundary_extreme_split_positions() {
    // Test with minimum allowed percentage
    let mut split = ResizableSplit::new(5);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();
    split.update_from_mouse(10, 10, area);
    assert_eq!(split.split_percent, 10); // min_percent

    // Test with maximum allowed percentage
    let mut split = ResizableSplit::new(95);
    let area = Rect::new(0, 0, 100, 20);
    split.start_drag();
    split.update_from_mouse(90, 10, area);
    assert_eq!(split.split_percent, 90); // max_percent
}

// ============================================================================
// RIGHT_PERCENT / BOTTOM_PERCENT TESTS
// ============================================================================

#[test]
fn test_right_percent_calculates_correctly() {
    let split = ResizableSplit::new(70);
    assert_eq!(split.right_percent(), 30);

    let split = ResizableSplit::new(50);
    assert_eq!(split.right_percent(), 50);

    let split = ResizableSplit::new(25);
    assert_eq!(split.right_percent(), 75);
}

#[test]
fn test_bottom_percent_is_alias_for_right_percent() {
    let split = ResizableSplit::new_with_direction(70, SplitDirection::Horizontal);
    assert_eq!(split.bottom_percent(), 30);
    assert_eq!(split.bottom_percent(), split.right_percent());
}

// ============================================================================
// INTEGRATION TESTS - REALISTIC SCENARIOS
// ============================================================================

#[test]
fn test_integration_complete_drag_cycle_vertical() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);

    // Initial state
    assert_eq!(split.split_percent, 50);
    assert!(!split.is_dragging);

    // Update divider position
    split.update_divider_position(area);
    assert_eq!(split.divider_pos, 50);

    // Check if mouse is on divider
    assert!(split.is_on_divider(50, 10, area));

    // Start dragging
    split.start_drag();
    assert!(split.is_dragging);

    // Drag to new position
    split.update_from_mouse(70, 10, area);
    assert_eq!(split.split_percent, 70);

    // Update divider position again
    split.update_divider_position(area);
    assert_eq!(split.divider_pos, 70);

    // Stop dragging
    split.stop_drag();
    assert!(!split.is_dragging);

    // Try to update mouse position (should not work)
    split.update_from_mouse(80, 10, area);
    assert_eq!(split.split_percent, 70); // Should not change
}

#[test]
fn test_integration_complete_drag_cycle_horizontal() {
    let mut split = ResizableSplit::new_with_direction(50, SplitDirection::Horizontal);
    let area = Rect::new(0, 0, 100, 40);

    // Initial state
    assert_eq!(split.split_percent, 50);
    assert!(!split.is_dragging);

    // Update divider position
    split.update_divider_position(area);
    assert_eq!(split.divider_pos, 20); // 50% of 40 rows

    // Check if mouse is on divider
    assert!(split.is_on_divider(50, 20, area));

    // Start dragging
    split.start_drag();
    assert!(split.is_dragging);

    // Drag to new position (row 30 = 75%)
    split.update_from_mouse(50, 30, area);
    assert_eq!(split.split_percent, 75);

    // Update divider position again
    split.update_divider_position(area);
    assert_eq!(split.divider_pos, 30); // 75% of 40 rows

    // Stop dragging
    split.stop_drag();
    assert!(!split.is_dragging);
}

#[test]
fn test_integration_dragging_respects_bounds() {
    let mut split = ResizableSplit::new(50);
    let area = Rect::new(0, 0, 100, 20);

    split.start_drag();

    // Try to drag beyond max
    split.update_from_mouse(95, 10, area);
    assert_eq!(split.split_percent, 90);

    // Try to drag below min
    split.update_from_mouse(5, 10, area);
    assert_eq!(split.split_percent, 10);

    // Drag to valid position
    split.update_from_mouse(60, 10, area);
    assert_eq!(split.split_percent, 60);
}

#[test]
fn test_integration_divider_hit_area_at_boundaries() {
    let mut split = ResizableSplit::new(5);
    let area = Rect::new(0, 0, 20, 10);
    split.update_divider_position(area);

    // Divider at column 1 (5% of 20)
    // Hit area should be columns 0, 1, 2
    assert!(split.is_on_divider(0, 5, area));
    assert!(split.is_on_divider(1, 5, area));
    assert!(split.is_on_divider(2, 5, area));
    assert!(!split.is_on_divider(3, 5, area));
}