datui-lib 0.4.4

Data Exploration in the Terminal (library)
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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
//! Rows drawn through the widget from a state.

use super::*;

/// The ORDER BY of the SQL in effect is state, so it leaves its mark: the sort
/// mark on each column it orders by, as named in the result, until the sidebar
/// sorts or another query runs (#688, item 13).
#[cfg(feature = "sql")]
#[test]
fn a_sql_order_by_marks_the_header_until_the_sidebar_sorts() {
    let df = df!("k" => [1i64, 2, 3], "v" => [3i64, 2, 1]).unwrap();
    let marks = |sql: &str| {
        let mut state =
            DataTableState::from_lazyframe(df.clone().lazy(), &OpenOptions::default()).unwrap();
        state.sql_query(sql.to_string());
        assert!(state.error.is_none(), "{sql}: {:?}", state.error);
        state.header_sort()
    };
    let owned = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::<Vec<_>>();
    assert_eq!(
        marks("SELECT v, k FROM df ORDER BY k DESC"),
        (owned(&["k"]), vec![true])
    );
    assert_eq!(
        marks("SELECT * FROM df ORDER BY k DESC, v LIMIT 2"),
        (owned(&["k", "v"]), vec![true, false])
    );
    assert_eq!(
        marks("SELECT v AS w, k FROM df ORDER BY w"),
        (owned(&["w"]), vec![false]),
        "named as in the result"
    );
    assert_eq!(
        marks("SELECT k, SUM(v) AS s FROM df GROUP BY k ORDER BY s DESC"),
        (owned(&["s"]), vec![true])
    );
    // An expression, or a column the result leaves out, leaves no mark.
    assert_eq!(marks("SELECT * FROM df ORDER BY k + 1"), (vec![], vec![]));
    assert_eq!(marks("SELECT v FROM df ORDER BY k"), (vec![], vec![]));
    assert_eq!(marks("SELECT * FROM df"), (vec![], vec![]));

    // On the header, and the sidebar's sort replaces it.
    let mut state =
        DataTableState::from_lazyframe(df.clone().lazy(), &OpenOptions::default()).unwrap();
    state.sql_query("SELECT * FROM df ORDER BY k DESC".to_string());
    state.collect();
    let area = Rect::new(0, 0, 30, 6);
    let mut buf = Buffer::empty(area);
    DataTable::default().render(area, &mut buf, &mut state);
    let header = row_string(&buf, area, 0);
    let g = crate::glyphs::get();
    assert!(header.contains(&format!("k{}", g.sort_desc)), "{header:?}");
    state.sort_by(vec!["v".to_string()], vec![false]);
    assert_eq!(state.header_sort(), (owned(&["v"]), vec![false]));
    // A new query names its own order, or none.
    state.sql_query("SELECT * FROM df".to_string());
    assert_eq!(state.header_sort(), (vec![], vec![]));
}

#[test]
fn byte_clamp_keeps_view_near_end_of_buffer() {
    // Regression: jumping to the END of a large dataset used to go blank because the
    // max_buffered_mb byte-clamp trimmed the buffer's HEAD (slice(0, max_rows)), discarding
    // exactly the tail rows the view needed. The clamp must keep the view in range.
    let lf = df!("a" => &["seed"]).unwrap().lazy();
    // 1 MB byte budget.
    let mut state = DataTableState::new(lf, None, None, None, Some(1), true).unwrap();
    state.view.num_rows = 1000;
    state.view.num_rows_valid = true;
    state.visible_rows = 40;
    state.view.start_row = 960; // jump-to-end position (num_rows - visible_rows)

    // Buffer spans [900, 1000); the view [960, 1000) sits at its tail. ~2 MB of data forces
    // a trim to roughly half the rows.
    let buffer_start = 900;
    let big: Vec<String> = (0..100).map(|_| "z".repeat(20_000)).collect();
    let df = df!("a" => big).unwrap();

    let fitted = state.fill_plan(buffer_start, 1000, 1000, true).fit(df);
    let (sliced, eff_start) = (fitted.df, fitted.start);
    let eff_end = eff_start + sliced.height();
    assert!(
        sliced.height() < 100,
        "expected a trim below the byte budget"
    );
    assert!(
        eff_start <= state.view.start_row,
        "view start {} fell before kept buffer start {}",
        state.view.start_row,
        eff_start
    );
    assert!(
        eff_end >= state.view.start_row + state.visible_rows,
        "view end {} fell after kept buffer end {}",
        state.view.start_row + state.visible_rows,
        eff_end
    );
    assert_eq!(
        sliced.height(),
        eff_end - eff_start,
        "df height must match range"
    );
}

#[test]
fn more_columns_indicator_appears_and_tracks_scroll() {
    // 4 columns that can't all fit: a right-edge marker should signal off-screen columns,
    // and after scrolling right a left-edge marker should appear too.
    let mut state =
        DataTableState::new(create_large_test_lf(), None, None, None, None, true).unwrap();
    state.visible_rows = 3;
    state.collect();

    let area = Rect::new(0, 0, 6, 4); // narrow: not all 4 columns fit
    let mut buf = Buffer::empty(area);
    DataTable::default().render(area, &mut buf, &mut state);
    let header = header_row_string(&buf, area);
    // The arrows come from the glyph set for the locale, which is ASCII on Windows CI.
    let g = crate::glyphs::get();
    assert!(
        header.contains(g.arrow_right),
        "expected right indicator, header: {header:?}"
    );
    assert!(
        !header.contains(g.arrow_left),
        "should not show left indicator at offset 0: {header:?}"
    );

    // Scroll right: now columns exist both left and right of the viewport.
    state.scroll_right();
    let mut buf2 = Buffer::empty(area);
    DataTable::default().render(area, &mut buf2, &mut state);
    let header2 = header_row_string(&buf2, area);
    assert!(
        header2.contains(g.arrow_left),
        "expected left indicator after scroll: {header2:?}"
    );
}

#[test]
fn the_header_mark_follows_the_state_sort_and_its_reverse() {
    // Through the stateful render: the marks come from the state being drawn,
    // so applying a sort shows them and `reverse` flips them, with no caller
    // wiring in between.
    let g = crate::glyphs::get();
    let mut state = DataTableState::new(create_test_lf(), None, None, None, None, true).unwrap();
    state.visible_rows = 3;
    state.sort(vec!["a".to_string()], true);

    let area = Rect::new(0, 0, 20, 5);
    let mut buf = Buffer::empty(area);
    DataTable::default().render(area, &mut buf, &mut state);
    let header = header_row_string(&buf, area);
    assert!(
        header.contains(&format!("a{}", g.sort_asc)),
        "sorted ascending: {header:?}"
    );

    state.reverse();
    let mut buf2 = Buffer::empty(area);
    DataTable::default().render(area, &mut buf2, &mut state);
    let header2 = header_row_string(&buf2, area);
    assert!(
        header2.contains(&format!("a{}", g.sort_desc)),
        "reversed: {header2:?}"
    );
    assert!(
        !header2.contains(g.sort_asc),
        "the old direction is gone: {header2:?}"
    );
}

/// A click finds the cell drawn under it, at 80×24 and on a wide screen: each
/// column where its heading is, frozen or scrolling, and each row where its cells
/// are, with row numbers on and the view scrolled down.
#[test]
fn a_click_finds_the_cell_drawn_under_it() {
    for (width, height) in [(80, 24), (200, 50)] {
        let n = 400;
        let mut columns = vec![Column::new("id".into(), (0..n).collect::<Vec<i64>>())];
        for c in 0..30 {
            columns.push(Column::new(
                format!("col_{c:02}").as_str().into(),
                (0..n).map(|i| format!("v{i}_{c}")).collect::<Vec<_>>(),
            ));
        }
        let lf = DataFrame::new_infer_height(columns).unwrap().lazy();
        let mut state = DataTableState::new(lf, None, None, None, None, true).unwrap();
        state.set_locked_columns(1);
        state.toggle_row_numbers();
        let area = Rect::new(0, 0, width, height);
        let render = |state: &mut DataTableState| {
            let mut buf = Buffer::empty(area);
            DataTable::default().render(area, &mut buf, state);
            buf
        };
        // The first frame sets how many rows fit; the rows are read for it.
        render(&mut state);
        state.collect();
        for scrolled in [false, true] {
            if scrolled {
                state.page_down();
                state.collect();
            }
            let buf = render(&mut state);
            let drawn = state.drawn.clone().expect("the table was drawn");
            let header = row_string(&buf, area, 0);
            assert!(drawn.columns.len() > 3, "{width}x{height}: {header:?}");
            assert_eq!(drawn.columns[0].2, "id", "the frozen column first");
            for (_, _, name) in &drawn.columns {
                // In cells, not bytes: the frozen separator is a wide glyph.
                let at = header.find(name.as_str()).expect("heading drawn");
                let from = header[..at].chars().count() as u16;
                for x in [from, from + name.len() as u16 - 1] {
                    let hit = state.drawn_cell(x, 0).expect("on the table");
                    assert_eq!(hit.row, None, "the header is no row");
                    assert_eq!(hit.column.as_deref(), Some(name.as_str()), "at {x}");
                }
            }
            // The rail and the row numbers are no column, but are the row.
            let y = drawn.header + 5;
            assert_eq!(
                state.drawn_cell(0, y),
                Some(CellHit {
                    row: Some(5),
                    column: None
                })
            );
            // A cell: the row and column whose value is drawn there.
            let (from, to, name) = drawn.columns[2].clone();
            let c: usize = name["col_".len()..].parse().unwrap();
            let hit = state.drawn_cell(from, y).expect("a cell");
            let row = drawn.start_row + 5;
            let text: String = (from..to).map(|x| buf[(x, y)].symbol()).collect();
            assert_eq!(text.trim(), format!("v{row}_{c}"), "{width}x{height}");
            state.point_at(&hit);
            assert_eq!(state.table_state.selected(), Some(5));
            assert_eq!(state.current_column(), Some(name.as_str()));
            // Off the table's right or bottom edge is nothing.
            assert_eq!(state.drawn_cell(width, y), None);
            assert_eq!(state.drawn_cell(0, height), None);
        }
    }
}

/// The column cursor at 80×24: its header and cells take the column tint, the
/// current cell (the cursor's row and column) the cell tint, and the rest of the
/// current row keeps the row tint. Frozen columns take it the same way.
#[test]
fn the_column_cursor_tints_its_header_and_cells() {
    let (mut state, area) = cursor_fixture();
    let row_tint = Color::Rgb(0x28, 0x34, 0x57);
    let column_tint = Color::Rgb(0x29, 0x2e, 0x42);
    let cell_tint = Color::Rgb(0x3b, 0x42, 0x61);
    let table = || {
        DataTable {
            selection_style: Style::default().bg(row_tint),
            ..DataTable::default()
        }
        .with_cursor_styles(
            crate::config::column_cursor_style(Some(column_tint)),
            crate::config::cell_cursor_style(Some(cell_tint)),
        )
    };
    state.move_cursor(CursorMove::Right);
    state.move_cursor(CursorMove::Right);
    assert_eq!(state.current_column(), Some("city"));
    let mut buf = Buffer::empty(area);
    table().render(area, &mut buf, &mut state);
    let city = column_span(&buf, area, "city");
    let name = column_span(&buf, area, "name");
    for x in city.clone() {
        assert_eq!(buf[(x, 0)].bg, cell_tint, "the header, at {x}");
        assert!(buf[(x, 0)].modifier.contains(Modifier::BOLD));
        assert_eq!(buf[(x, 1)].bg, cell_tint, "the current cell, at {x}");
        for y in 2..area.height {
            assert_eq!(buf[(x, y)].bg, column_tint, "the column, at {x},{y}");
        }
    }
    for x in name.clone() {
        assert_eq!(buf[(x, 1)].bg, row_tint, "the rest of the row");
        assert_ne!(buf[(x, 0)].bg, cell_tint, "another header");
        assert_ne!(buf[(x, 2)].bg, column_tint, "another column");
    }

    // Frozen: the same marks, left of the separator.
    state.move_cursor(CursorMove::First);
    assert_eq!(state.current_column(), Some("id"));
    let mut buf = Buffer::empty(area);
    table().render(area, &mut buf, &mut state);
    let id = column_span(&buf, area, "id");
    for x in id {
        assert_eq!(buf[(x, 0)].bg, cell_tint);
        assert_eq!(buf[(x, 1)].bg, cell_tint);
        assert_eq!(buf[(x, 5)].bg, column_tint);
    }
    for x in city {
        assert_ne!(buf[(x, 0)].bg, cell_tint, "city lets go of it");
    }
}

/// Where the tints would not show (16 colors, `NO_COLOR`), the header and the
/// current cell are reversed, so the cursor is still on screen, and nothing else is.
#[test]
fn the_column_cursor_shows_without_its_tints() {
    let (mut state, area) = cursor_fixture();
    state.move_cursor(CursorMove::Right);
    for tint in [Color::Black, Color::White, Color::Reset] {
        let mut buf = Buffer::empty(area);
        DataTable::default()
            .with_cursor_styles(
                crate::config::column_cursor_style(Some(tint)),
                crate::config::cell_cursor_style(Some(tint)),
            )
            .render(area, &mut buf, &mut state);
        let name = column_span(&buf, area, "name");
        let reversed = |x, y| buf[(x, y)].modifier.contains(Modifier::REVERSED);
        for x in name {
            assert!(reversed(x, 0), "{tint:?}: the header");
            assert!(reversed(x, 1), "{tint:?}: the current cell");
            assert!(!reversed(x, 2), "{tint:?}: not the rest of the column");
        }
        let city = column_span(&buf, area, "city");
        assert!(!reversed(city.start, 0) && !reversed(city.start, 1));
    }
}

/// Under a reversed row, the current cell is drawn upright, tinted or not, so it
/// stands out from the row; the header keeps its own mark.
#[test]
fn the_current_cell_stands_out_of_a_reversed_row() {
    let (mut state, area) = cursor_fixture();
    state.move_cursor(CursorMove::Right);
    for tint in [Color::Rgb(0x3b, 0x42, 0x61), Color::Black, Color::Reset] {
        let mut buf = Buffer::empty(area);
        DataTable {
            selection_style: Style::default().add_modifier(Modifier::REVERSED),
            ..DataTable::default()
        }
        .with_cursor_styles(
            crate::config::column_cursor_style(Some(tint)),
            crate::config::cell_cursor_style(Some(tint)),
        )
        .render(area, &mut buf, &mut state);
        let reversed = |x, y| buf[(x, y)].modifier.contains(Modifier::REVERSED);
        for x in column_span(&buf, area, "name") {
            assert!(!reversed(x, 1), "{tint:?}: the current cell is upright");
            assert!(buf[(x, 1)].modifier.contains(Modifier::BOLD));
        }
        let city = column_span(&buf, area, "city");
        assert!(reversed(city.start, 1), "{tint:?}: the rest of the row");
    }
}

/// The cursor follows its column by name when the columns are reordered or
/// frozen, and a column hidden from under it hands it to the one in its place.
#[test]
fn the_column_cursor_follows_its_column_by_name() {
    let (mut state, area) = cursor_fixture();
    state.move_cursor(CursorMove::Right);
    state.move_cursor(CursorMove::Right);
    assert_eq!(state.current_column(), Some("city"));
    let order = |names: &[&str]| names.iter().map(|n| n.to_string()).collect::<Vec<_>>();
    state.set_column_order(order(&["city", "id", "name", "amount"]));
    assert_eq!(state.current_column(), Some("city"));
    assert_eq!(state.current_column_index(), Some(0));
    state.set_locked_columns(0);
    state.set_column_order(order(&["id", "name", "city", "amount"]));
    state.set_locked_columns(3);
    assert_eq!(state.current_column(), Some("city"), "frozen now");
    // Hidden: the column now in its place takes it.
    state.set_locked_columns(0);
    state.set_column_order(order(&["id", "name", "amount"]));
    assert_eq!(state.current_column(), Some("amount"));
    // Hidden at the end: the last column.
    state.set_column_order(order(&["id", "name"]));
    assert_eq!(state.current_column(), Some("name"));
    state.set_column_order(Vec::new());
    assert_eq!(state.current_column(), None);
    DataTable::default().render(area, &mut Buffer::empty(area), &mut state);
}

/// `h` `l` cross from the frozen columns to the scrolling ones and back in the
/// shown order; the view moves only when the cursor would leave it, and a page
/// puts the cursor on the new page's first column.
#[test]
fn the_column_cursor_scrolls_only_at_the_edges() {
    let n = 5;
    let names: Vec<String> = (0..40).map(|i| format!("column_{i:02}")).collect();
    let columns: Vec<Column> = names
        .iter()
        .map(|name| Column::new(name.as_str().into(), (0..n).collect::<Vec<i64>>()))
        .collect();
    let lf = DataFrame::new_infer_height(columns).unwrap().lazy();
    let mut state = DataTableState::new(lf, None, None, None, None, true).unwrap();
    state.visible_rows = 5;
    state.set_locked_columns(2);
    let area = Rect::new(0, 0, 80, 8);
    let draw = |state: &mut DataTableState| {
        DataTable::default().render(area, &mut Buffer::empty(area), state);
        state.columns_on_screen().unwrap()
    };
    let start = draw(&mut state);
    assert_eq!((start.first, start.cursor), (3, 1));
    state.move_cursor(CursorMove::Right);
    state.move_cursor(CursorMove::Right);
    let on = draw(&mut state);
    assert_eq!((on.first, on.cursor), (3, 3), "into the scrolling side");
    // Walk to the right edge: nothing scrolls until the cursor would leave (a
    // column cut at the edge counts as leaving), then just enough.
    let mut before = on;
    let past = loop {
        state.move_cursor(CursorMove::Right);
        let now = draw(&mut state);
        assert_eq!(now.cursor, before.cursor + 1);
        if now.first != before.first {
            break now;
        }
        before = now;
    };
    assert!(past.first > 3 && past.cursor <= past.last, "{past:?}");
    assert!(past.cursor >= start.last, "not before the edge: {past:?}");
    assert!(past.first <= before.last, "no column skipped: {past:?}");
    // Back to the left edge, and one more scrolls back a column.
    for _ in past.first..past.cursor {
        state.move_cursor(CursorMove::Left);
    }
    assert_eq!(draw(&mut state).first, past.first);
    state.move_cursor(CursorMove::Left);
    assert_eq!(draw(&mut state).first, past.first - 1);
    // Pages: the cursor starts the new page; on the last, it takes the last column.
    state.move_cursor(CursorMove::PageRight);
    let page = draw(&mut state);
    assert_eq!(page.cursor, page.first);
    state.move_cursor(CursorMove::Last);
    let last = draw(&mut state);
    assert_eq!((last.cursor, last.last), (40, 40));
    state.move_cursor(CursorMove::PageRight);
    assert_eq!(draw(&mut state).cursor, 40);
    // From a frozen column, the next is the first scrolling column: the view
    // goes back to it.
    state.go_to_column("column_01");
    assert_eq!(draw(&mut state).first, last.first, "frozen: on screen");
    state.move_cursor(CursorMove::Right);
    let back = draw(&mut state);
    assert_eq!((back.first, back.cursor), (3, 3));
    // `[` on the first page: its first column, then the first of all.
    state.move_cursor(CursorMove::Right);
    state.move_cursor(CursorMove::PageLeft);
    assert_eq!(draw(&mut state).cursor, 3);
    state.move_cursor(CursorMove::PageLeft);
    assert_eq!(draw(&mut state).cursor, 1);
    state.move_cursor(CursorMove::Left);
    assert_eq!(draw(&mut state).cursor, 1, "nothing left of the first");
}

/// Every cell right of the frozen separator sits one cell off it, as the cells
/// left of it do, and the gap takes its row's tint. A right-aligned number as
/// wide as its column, a negative one most often, used to touch the line:
/// `│-9.930889` (#386).
#[test]
fn the_frozen_separator_has_a_gap_on_both_sides() {
    let lf = df!(
        "carrier" => &["AA", "UA", "9E"],
        "delay" => &[-9.930889f64, 3.5, 12.25],
        "name" => &["American", "United", "Endeavor"],
    )
    .unwrap()
    .lazy();
    let mut state = DataTableState::new(lf, None, None, None, None, true).unwrap();
    state.visible_rows = 3;
    state.set_locked_columns(1);
    state.table_state.select(Some(0));

    let area = Rect::new(0, 0, 40, 6);
    let mut buf = Buffer::empty(area);
    let table = DataTable {
        header_bg: Color::Indexed(238),
        alternate_row_bg: Some(Color::Indexed(236)),
        selection_style: Style::default().bg(Color::Indexed(24)),
        ..DataTable::default()
    };
    table.render(area, &mut buf, &mut state);

    let rows: Vec<String> = (0..area.height)
        .map(|y| row_string(&buf, area, y))
        .collect();
    assert!(
        rows[1].contains(&format!("{} -9.930889", crate::glyphs::get().rule)),
        "{rows:#?}"
    );
    let rule = crate::glyphs::get().rule;
    let sep = (0..area.width)
        .find(|&x| buf[(x, 0)].symbol() == rule)
        .expect("a separator");
    for y in 0..area.height {
        let row = &rows[y as usize];
        assert_eq!(buf[(sep - 1, y)].symbol(), " ", "row {y}: {row:?}");
        assert_eq!(buf[(sep + 1, y)].symbol(), " ", "row {y}: {row:?}");
        assert_eq!(
            buf[(sep + 1, y)].bg,
            buf[(sep + 2, y)].bg,
            "row {y}'s gap takes the row's tint: {row:?}"
        );
    }
    // The header, the highlight and the stripe, not only unstyled rows.
    assert_eq!(buf[(sep + 1, 0)].bg, Color::Indexed(238));
    assert_eq!(buf[(sep + 1, 1)].bg, Color::Indexed(24));
    assert_eq!(buf[(sep + 1, 2)].bg, Color::Indexed(236));
}

/// The frozen separator runs down the header and the rows, and stops under the
/// last: a grouped view of seven rows had it running down the empty screen.
#[test]
fn the_frozen_separator_stops_at_the_last_row() {
    let lf = df!(
        "carrier" => &["AA", "UA", "9E"],
        "delay" => &[-9.9f64, 3.5, 12.25],
    )
    .unwrap()
    .lazy();
    let mut state = DataTableState::new(lf, None, None, None, None, true).unwrap();
    state.visible_rows = 3;
    state.set_locked_columns(1);
    state.table_state.select(Some(0));
    let area = Rect::new(0, 0, 30, 10);
    let mut buf = Buffer::empty(area);
    DataTable::default().render(area, &mut buf, &mut state);
    let rule = crate::glyphs::get().rule;
    let sep = (0..area.width)
        .find(|&x| buf[(x, 0)].symbol() == rule)
        .expect("a separator");
    let ruled: Vec<u16> = (0..area.height)
        .filter(|&y| buf[(sep, y)].symbol() == rule)
        .collect();
    let last = ruled.last().copied().unwrap();
    assert_eq!(
        ruled,
        (0..=last).collect::<Vec<_>>(),
        "unbroken to the last row"
    );
    let header = DataTable::default().header_height();
    assert_eq!(last, header + 2, "under the third row, no further");
}

/// A frozen column whose type is wider than its name and values still gets its
/// whole width and the gap before the separator. The width pass left the type row
/// out, so `id` over `i64` ran onto the line (`i64│`) and a one-letter string
/// column did not fit at all.
#[test]
fn a_frozen_column_is_as_wide_as_its_type() {
    let lf = df!(
        "id" => &[1i64, 2],
        "k" => &["x", "y"],
        "v" => &[-3.5f64, 4.25],
    )
    .unwrap()
    .lazy();
    let mut state = DataTableState::new(lf, None, None, None, None, true).unwrap();
    state.visible_rows = 2;
    state.set_locked_columns(2);
    let area = Rect::new(0, 0, 30, 4);
    let mut buf = Buffer::empty(area);
    DataTable {
        dtype_row: true,
        ..DataTable::default()
    }
    .render(area, &mut buf, &mut state);

    let rows: Vec<String> = (0..area.height)
        .map(|y| row_string(&buf, area, y))
        .collect();
    let rule = crate::glyphs::get().rule;
    assert!(rows[0].contains(&format!(" id k   {rule}")), "{rows:#?}");
    assert!(rows[1].contains(&format!("i64 str {rule}")), "{rows:#?}");
    assert!(rows[2].contains(&format!("  1 x   {rule}")), "{rows:#?}");
}

/// A list cell reads as it did when the buffer held lists as text, and the type
/// row names the list once the rows have landed, not `str`.
#[test]
fn a_list_column_draws_its_items_under_its_list_type() {
    let mut state = list_state();
    let area = Rect::new(0, 0, 80, 4);
    let mut buf = Buffer::empty(area);
    DataTable {
        dtype_row: true,
        ..DataTable::default()
    }
    .render(area, &mut buf, &mut state);
    let rows: Vec<String> = (0..area.height)
        .map(|y| row_string(&buf, area, y))
        .collect();
    assert!(rows[1].contains("list[str]"), "{rows:#?}");
    assert!(!rows[1].contains(" str "), "{rows:#?}");
    assert!(rows[2].contains("[a, b]"), "{rows:#?}");
    // The column's width cap cuts the rest; `exact` tests the whole preview.
    assert!(
        rows[3].contains("[t0, t1, t2, t3, t4, t5, t6, t7"),
        "{rows:#?}"
    );
}

/// A page over columns not drawn yet waits for the draw, which measures them from
/// the rows on hand and lands it: `Last` ends the page with the last column whole,
/// and the one before that would not have fitted. Nothing moves before then.
#[test]
fn a_page_over_columns_not_drawn_lands_at_the_draw() {
    let names: Vec<String> = (0..30).map(|i| format!("col{i:02}")).collect();
    let columns: Vec<Column> = names
        .iter()
        .enumerate()
        .map(|(i, name)| {
            let value = "x".repeat(3 + i % 5);
            Series::new(name.as_str().into(), vec![value; 3]).into()
        })
        .collect();
    let lf = DataFrame::new_infer_height(columns).unwrap().lazy();
    let mut state = DataTableState::new(lf, None, None, None, None, true).unwrap();
    state.visible_rows = 3;
    state.collect();
    let area = Rect::new(0, 0, 50, 4);
    let draw = |state: &mut DataTableState| {
        let mut buf = Buffer::empty(area);
        DataTable::default().render(area, &mut buf, state);
        row_string(&buf, area, 0)
    };
    draw(&mut state);
    assert!(state.drawn_width("col29").is_none(), "not drawn yet");

    state.scroll_columns(ColumnMove::Last);
    assert_eq!(state.termcol_index, 0, "nothing moves before the draw");
    let header = draw(&mut state);
    assert!(header.trim_end().ends_with("col29"), "{header}");
    let start = state.termcol_index;
    assert!(start > 0);
    // The column before the page would not have fitted beside it.
    let room = state.scroll_room.unwrap();
    let used: u16 = names[start - 1..]
        .iter()
        .map(|n| state.shown_width(n).unwrap() + room.padding)
        .sum::<u16>()
        - room.padding;
    assert!(used > room.width, "{used} in {}", room.width);

    // Back, which lands at the draw again; then forward over columns drawn now,
    // which needs none.
    state.scroll_columns(ColumnMove::PageLeft);
    draw(&mut state);
    let back = state.termcol_index;
    assert!(back < start);
    state.scroll_columns(ColumnMove::PageRight);
    assert_eq!(state.termcol_index, start);
    state.scroll_columns(ColumnMove::First);
    assert_eq!(state.termcol_index, 0);
}

/// The hidden-columns count goes in the blank run after the last column, or not
/// at all: at no width does it cover the type under a heading. The separator's gap
/// took the one cell of slack that used to keep `+2 >` clear of `str` at 60
/// columns, and the count then wrote over the `r`.
#[test]
fn the_hidden_count_never_covers_a_type() {
    let lf = df!(
        "k" => &["x"],
        "origin" => &["JFK"],
        "dest" => &["LAX"],
        "tail" => &["N1"],
        "name" => &["Endeavor"],
    )
    .unwrap()
    .lazy();
    let mut state = DataTableState::new(lf, None, None, None, None, true).unwrap();
    state.visible_rows = 1;
    state.set_locked_columns(1);
    let table = || DataTable {
        dtype_row: true,
        ..DataTable::default()
    };
    assert_eq!(table().header_height(), 2, "the count goes on the type row");
    for width in 8..=40 {
        let area = Rect::new(0, 0, width, 3);
        let mut buf = Buffer::empty(area);
        table().render(area, &mut buf, &mut state);
        let names = row_string(&buf, area, 0);
        let types = row_string(&buf, area, 1);
        // Every heading shown whole has its whole type under it; these are all
        // strings, so each starts where its name does.
        for name in ["origin", "dest", "tail", "name"] {
            if let Some(at) = names.find(&format!(" {name}")) {
                let x = names[..at].chars().count() + 1;
                let under: String = types.chars().skip(x).take(3).collect();
                assert_eq!(under, "str", "width {width}:\n{names}\n{types}");
            }
        }
    }
}

/// The gap is paid for in the width budget: at every width, the columns right of
/// the separator are whole or absent. Left out, the last column that fits exactly
/// comes out one cell short, and a number cut short reads as a different number.
/// The one exception is a first column with no room for its value at all, which
/// shows a preview behind the clip marker rather than nothing.
#[test]
fn the_separator_gap_never_cuts_a_number_short() {
    let values = ["-987", "654", "-32", "10"];
    let lf = df!(
        "k" => &["x"],
        "a" => &[-987i64],
        "b" => &[654i64],
        "c" => &[-32i64],
        "d" => &[10i64],
    )
    .unwrap()
    .lazy();
    let mut state = DataTableState::new(lf, None, None, None, None, true).unwrap();
    state.visible_rows = 1;
    state.set_locked_columns(1);
    let data_row = DataTable::default().header_height();
    for width in 8..=30 {
        let area = Rect::new(0, 0, width, data_row + 1);
        let mut buf = Buffer::empty(area);
        DataTable::default().render(area, &mut buf, &mut state);
        let row = row_string(&buf, area, data_row);
        let g = crate::glyphs::get();
        let (_, scrolled) = row.split_once(g.rule).expect("a separator");
        for (i, token) in scrolled.split_whitespace().enumerate() {
            let previewed = i == 0 && token.ends_with(g.ellipsis);
            assert!(
                values.contains(&token) || previewed,
                "width {width}: {token:?} is cut short in {row:?}"
            );
        }
    }
}

/// A heading wider than the table is clipped, marked, over values that fit whole:
/// it never leaves the table blank, at any width, in either glyph set, with or
/// without the type row. It used to drop the column, and with it everything
/// after, leaving the rail and an off-screen hint over nothing.
#[test]
fn a_long_header_never_blanks_the_table() {
    let name = format!("numeric_header_{}", "x".repeat(90));
    let df = DataFrame::new_infer_height(vec![
        Series::new(name.as_str().into(), &[1i64, 22, 333]).into(),
        Series::new("tail".into(), &["t1", "t2", "t3"]).into(),
    ])
    .unwrap();
    for g in glyph_sets() {
        for dtype_row in [false, true] {
            for width in [12u16, 20, 60, 80, 120] {
                let table = || DataTable {
                    glyphs: g,
                    dtype_row,
                    ..DataTable::default()
                };
                let header_h = usize::from(table().header_height());
                let mut state = state_of(&df, 3);
                let rows = draw(table(), &mut state, width, header_h as u16 + 3);
                let ctx = format!(
                    "{} glyphs, type row {dtype_row}, width {width}:\n{}",
                    set_name(g),
                    rows.join("\n")
                );
                assert!(rows[0].contains("num"), "{ctx}");
                for (i, value) in ["1", "22", "333"].iter().enumerate() {
                    assert!(
                        rows[header_h + i].split_whitespace().any(|t| t == *value),
                        "{value} is whole on its row: {ctx}"
                    );
                }
                if dtype_row && width >= 20 {
                    assert!(rows[1].contains("i64"), "{ctx}");
                }
                // Wider than any cap on automatic widths: always clipped, and from
                // 60 columns on, with the column after it beside it.
                assert!(rows[0].contains(g.ellipsis), "{ctx}");
                if width >= 60 {
                    assert!(rows[0].contains("tail"), "{ctx}");
                }
            }
        }
    }
}

/// A clipped heading gives way to its marks: the name is cut, never the sort
/// direction, which is state.
#[test]
fn a_clipped_heading_keeps_its_sort_mark() {
    let name = format!("numeric_header_{}", "x".repeat(90));
    let df = DataFrame::new_infer_height(vec![Series::new(name.as_str().into(), &[1i64]).into()])
        .unwrap();
    for g in glyph_sets() {
        let table = DataTable {
            glyphs: g,
            ..DataTable::default()
        }
        .with_sort(vec![name.clone()], vec![true]);
        let area = Rect::new(0, 0, 30, 2);
        let mut buf = Buffer::empty(area);
        let mut ts = TableState::default();
        table.render_dataframe(&df, area, &mut buf, &mut ts, false);
        let header = header_row_string(&buf, area);
        assert!(
            header
                .trim_end()
                .ends_with(&format!("{}{}", g.ellipsis, g.sort_desc)),
            "{}: {header:?}",
            set_name(g)
        );
    }
}

/// A heading whose drawn width is not its string's width (`لا` draws two cells,
/// a halfwidth sound mark one) is drawn whole over right-aligned numbers, with its
/// sort mark after it. ratatui's own alignment pushed the last letter off the cell,
/// and the mark landed on it.
#[test]
fn a_heading_is_placed_by_the_cells_it_draws() {
    for name in ["الاسم", "ガギ"] {
        let df = DataFrame::new_infer_height(vec![
            Series::new(name.into(), &[1i64]).into(),
            Series::new("tail".into(), &["x"]).into(),
        ])
        .unwrap();
        let table = DataTable::default().with_sort(vec![name.to_string()], vec![false]);
        let mark = table.glyphs.sort_asc;
        let area = Rect::new(0, 0, 30, 3);
        let mut buf = Buffer::empty(area);
        let mut ts = TableState::default();
        table.render_dataframe(&df, area, &mut buf, &mut ts, false);
        let rows: Vec<String> = (0..3).map(|y| drawn_from(&buf, y, 0)).collect();
        assert!(rows[0].starts_with(&format!("{name}{mark}")), "{rows:#?}");
        let width = crate::glyphs::cell_width(name) + 1;
        assert!(
            rows[1].starts_with(&format!("{:>width$} x", "1")),
            "{rows:#?}"
        );
    }
}

/// A number too wide for the whole table shows its leading digits behind the clip
/// marker: something rather than nothing, and never its trailing digits passing
/// for a whole number, which is what ratatui's own cut of a right-aligned value
/// would show.
#[test]
fn a_number_wider_than_the_table_is_a_marked_preview() {
    let full = "-1234567890123456789";
    let df = df!("n" => &[-1234567890123456789i64]).unwrap();
    for g in glyph_sets() {
        for width in 3u16..=24 {
            let mut state = state_of(&df, 1);
            let table = DataTable {
                glyphs: g,
                ..DataTable::default()
            };
            let rows = draw(table, &mut state, width, 2);
            let shown: String = rows[1].chars().skip(1).collect::<String>();
            let shown = shown.trim();
            let ctx = format!("{} glyphs, width {width}: {rows:?}", set_name(g));
            if usize::from(width) > full.len() {
                assert_eq!(shown, full, "{ctx}");
            } else if g.ellipsis.starts_with(shown) {
                // Room for no more than the marker.
                assert!(!shown.is_empty(), "{ctx}");
            } else {
                let kept = shown
                    .strip_suffix(g.ellipsis)
                    .unwrap_or_else(|| panic!("a clipped number carries the marker: {ctx}"));
                assert!(full.starts_with(kept), "{ctx}");
            }
        }
    }
}

/// Wide characters are measured in cells: a column of four CJK characters is
/// eight cells wide, and shows whole beside the columns after it while there is
/// room. Counted in characters, it was given four and clipped with space to spare.
#[test]
fn wide_characters_are_measured_in_cells() {
    let values = ["東京大阪", "京都横浜", "名古屋市"];
    let df = df!(
        "a" => &values,
        "b" => &[1i64, 2, 3],
        "tail" => &["x", "y", "z"],
    )
    .unwrap();
    for g in glyph_sets() {
        for width in [16u16, 30, 80] {
            let mut state = state_of(&df, 3);
            let table = DataTable {
                glyphs: g,
                ..DataTable::default()
            };
            let rows = draw(table, &mut state, width, 4);
            let ctx = format!("{} glyphs, width {width}: {rows:#?}", set_name(g));
            assert!(rows[0].contains("tail"), "{ctx}");
            for (i, value) in values.iter().enumerate() {
                assert!(rows[1 + i].contains(value), "{ctx}");
            }
        }
    }
}

/// A value cut where it meets the edge keeps whole graphemes and gains the clip
/// marker: no half of a wide character, no accent split from its letter, no
/// joined emoji broken apart, at any width, in either glyph set.
#[test]
fn a_clipped_cell_keeps_whole_graphemes() {
    let values = [
        "東京大阪名古屋横浜",
        "e\u{301}e\u{301}e\u{301}e\u{301}e\u{301}e\u{301}e\u{301}",
        "👩\u{200d}👩\u{200d}👧👍🏽🇯🇵 and more",
        "plain text that runs on",
    ];
    let df = df!("id" => &[1i64, 2, 3, 4], "text" => &values).unwrap();
    for g in glyph_sets() {
        for width in 4u16..=30 {
            let mut state = state_of(&df, 4);
            let area = Rect::new(0, 0, width, 5);
            let mut buf = Buffer::empty(area);
            DataTable {
                glyphs: g,
                ..DataTable::default()
            }
            .render(area, &mut buf, &mut state);
            // The rail, `id` two cells wide, and one cell of padding.
            let text_x = 4;
            if text_x >= width {
                continue;
            }
            for (i, value) in values.iter().enumerate() {
                let y = 1 + i as u16;
                let shown = drawn_from(&buf, y, text_x);
                let shown = shown.trim_end();
                let ctx = format!("{} glyphs, width {width}, row {i}: {shown:?}", set_name(g));
                if shown.is_empty() || shown == *value {
                    continue;
                }
                let kept = shown
                    .strip_suffix(g.ellipsis)
                    .unwrap_or_else(|| panic!("a clipped value is marked: {ctx}"));
                let span = Span::raw(*value);
                let mut whole = String::new();
                for grapheme in span.styled_graphemes(Style::default()) {
                    if whole.len() >= kept.len() {
                        break;
                    }
                    whole.push_str(grapheme.symbol);
                }
                assert_eq!(whole, kept, "{ctx}");
                // And each cell holds a whole grapheme of the value or the marker.
                for x in text_x..width {
                    let symbol = buf[(x, y)].symbol();
                    assert!(
                        symbol == " "
                            || g.ellipsis.contains(symbol)
                            || span
                                .styled_graphemes(Style::default())
                                .any(|gr| gr.symbol == symbol),
                        "cell {x} holds {symbol:?}: {ctx}"
                    );
                }
            }
        }
    }
}

/// A frozen text column wider than the window is clipped, marked and still
/// frozen, with the column after it beside it. It used to vanish, leaving only
/// the column after it.
#[test]
fn a_frozen_long_text_is_clipped_not_dropped() {
    let url = format!("https://example.com/{}", "long-segment/".repeat(15));
    let df = df!("url" => &[url.as_str(), "short"], "tail" => &[7i64, 8]).unwrap();
    for g in glyph_sets() {
        for width in [40u16, 60, 80, 120] {
            let mut state = state_of(&df, 2);
            state.set_locked_columns(1);
            let table = DataTable {
                glyphs: g,
                ..DataTable::default()
            };
            let rows = draw(table, &mut state, width, 3);
            let ctx = format!("{} glyphs, width {width}: {rows:#?}", set_name(g));
            let (frozen, scrolled) = rows[0].split_once(g.rule).expect(&ctx);
            assert!(frozen.contains("url"), "{ctx}");
            assert!(scrolled.contains("tail"), "{ctx}");
            let (frozen, scrolled) = rows[1].split_once(g.rule).expect(&ctx);
            assert!(frozen.contains("https://exa"), "{ctx}");
            assert!(frozen.trim_end().ends_with(g.ellipsis), "{ctx}");
            assert!(scrolled.split_whitespace().any(|t| t == "7"), "{ctx}");
            assert_eq!(state.frozen_shown(), 1, "{ctx}");
        }
    }
}

/// The frozen columns are measured on the rows on screen, like the rest. They
/// were measured on the head of the buffer, which is not the page on screen once
/// the view has moved into it, so a longer value on screen was cut short.
#[test]
fn frozen_columns_are_measured_on_the_rows_on_screen() {
    let long = "a much longer frozen value";
    let names: Vec<String> = (0..400)
        .map(|i| {
            if i == 302 {
                long.to_string()
            } else {
                format!("n{i}")
            }
        })
        .collect();
    let df = df!("name" => names, "v" => (0..400i64).collect::<Vec<_>>()).unwrap();
    let mut state = state_of(&df, 5);
    state.set_locked_columns(1);
    state.scroll_to(300);
    state.collect();
    assert!(
        state.view.buffered_start_row < state.view.start_row,
        "the page is not the head of the buffer: {} vs {}",
        state.view.buffered_start_row,
        state.view.start_row
    );
    let rows = draw(DataTable::default(), &mut state, 80, 6);
    assert!(
        rows.iter().any(|row| row.contains(long)),
        "the value on screen is whole: {rows:#?}"
    );
}

/// Frozen columns are spaced like scrolling ones: the configured padding on both
/// sides of the separator, at every setting. The frozen side used a hardcoded
/// single space.
#[test]
fn frozen_and_scrolling_columns_share_the_padding() {
    let df = df!(
        "id" => &[1i64, 2],
        "k" => &["x", "y"],
        "v" => &[3i64, 4],
        "w" => &[5i64, 6],
    )
    .unwrap();
    for padding in [0u16, 1, 2, 3] {
        let mut state = state_of(&df, 2);
        state.set_locked_columns(2);
        let table = DataTable {
            table_cell_padding: padding,
            ..DataTable::default()
        };
        let rows = draw(table, &mut state, 40, 3);
        let gap = " ".repeat(usize::from(padding));
        let rule = crate::glyphs::get().rule;
        assert!(
            rows[0].starts_with(&format!(" id{gap}k {rule} v{gap}w")),
            "padding {padding}: {rows:#?}"
        );
        assert!(
            rows[1].contains(&format!(" 1{gap}x {rule} 3{gap}5")),
            "padding {padding}: {rows:#?}"
        );
    }
}

/// Frozen columns that cannot all fit beside a usable scrolling column: as many
/// as fit stay frozen, the broken rule says some had to scroll, those lead the
/// scrolling side, every column stays reachable, and the request stands, so a
/// wider window freezes them all again.
#[test]
fn a_frozen_prefix_too_wide_scrolls_until_there_is_room() {
    let df = phonetic_frame();
    for g in glyph_sets() {
        for row_numbers in [false, true] {
            let mut state = state_of(&df, 2);
            state.row_numbers = row_numbers;
            state.set_locked_columns(4);
            let table = || DataTable {
                glyphs: g,
                ..DataTable::default()
            };
            let rows = draw(table(), &mut state, 60, 3);
            let ctx = format!(
                "{} glyphs, row numbers {row_numbers}: {rows:#?}",
                set_name(g)
            );
            assert_eq!(state.locked_columns_count(), 4, "{ctx}");
            let shown = state.frozen_shown();
            assert!((1..4).contains(&shown), "{shown} frozen: {ctx}");
            let (frozen, scrolled) = rows[0].split_once(g.rule_broken).expect(&ctx);
            assert!(!rows[0].contains(g.rule), "{ctx}");
            assert!(frozen.contains(PHONETIC[0]), "{ctx}");
            assert!(
                scrolled.trim_start().starts_with(PHONETIC[shown]),
                "the first column left out leads the scrolling side: {ctx}"
            );

            let reached = names_reached(&mut state, g, 60);
            assert_eq!(reached.len(), PHONETIC.len(), "{reached:?}: {ctx}");

            let rows = draw(table(), &mut state, 160, 3);
            assert_eq!(state.frozen_shown(), 4, "{rows:#?}");
            let (frozen, _) = rows[0].split_once(g.rule).expect("the plain rule");
            for name in &PHONETIC[..4] {
                assert!(frozen.contains(name), "{rows:#?}");
            }
        }
    }
}

/// A rollback puts back the frozen fit its columns were sliced for: a wider
/// layout since then must re-slice them, or the columns that had to scroll show
/// twice, frozen and scrolling.
#[test]
fn a_rollback_keeps_the_frozen_fit_its_columns_were_sliced_for() {
    let df = phonetic_frame();
    let mut state = state_of(&df, 2);
    state.set_locked_columns(4);
    draw(DataTable::default(), &mut state, 60, 3);
    assert!(state.frozen_shown() < 4);
    let saved = state.rollback_point();
    draw(DataTable::default(), &mut state, 200, 3);
    assert_eq!(state.frozen_shown(), 4);
    state.roll_back(saved);
    let rows = draw(DataTable::default(), &mut state, 200, 3);
    for name in PHONETIC {
        assert_eq!(rows[0].matches(name).count(), 1, "{name}: {rows:#?}");
    }
}

/// With every column frozen there is nothing to scroll, until the window is too
/// narrow for them all: then the ones that do not fit scroll, and each is still
/// reachable.
#[test]
fn with_every_column_frozen_each_is_still_reachable() {
    let df = phonetic_frame();
    for g in glyph_sets() {
        let mut state = state_of(&df, 2);
        state.set_locked_columns(PHONETIC.len());
        let table = || DataTable {
            glyphs: g,
            ..DataTable::default()
        };
        let rows = draw(table(), &mut state, 200, 3);
        assert_eq!(state.frozen_shown(), PHONETIC.len(), "{rows:#?}");
        assert!(rows[0].contains(g.rule), "{rows:#?}");
        assert!(!rows[0].contains(g.arrow_right), "{rows:#?}");

        let rows = draw(table(), &mut state, 60, 3);
        assert!(state.frozen_shown() < PHONETIC.len(), "{rows:#?}");
        assert!(rows[0].contains(g.rule_broken), "{rows:#?}");
        let reached = names_reached(&mut state, g, 60);
        assert_eq!(reached.len(), PHONETIC.len(), "{reached:?}");
    }
}

/// The page from the issue's reproduction: one description runs to a long URL.
/// That page still shows its rows, with the description clipped and marked
/// rather than any column vanishing for it.
#[test]
fn a_page_with_one_long_value_is_not_blank() {
    let url = format!("https://example.com/{}", "long-segment/".repeat(15));
    let n = 80usize;
    let df = df!(
        "id" => (0..n as i64).collect::<Vec<_>>(),
        "description" => (0..n)
            .map(|i| if i == 24 { url.clone() } else { format!("item {i}") })
            .collect::<Vec<_>>(),
        "amount" => (0..n).map(|i| i as f64 * 1.5).collect::<Vec<_>>(),
        "status" => (0..n).map(|i| if i % 2 == 0 { "open" } else { "closed" }).collect::<Vec<_>>(),
    )
    .unwrap();
    for g in glyph_sets() {
        for width in [60u16, 80, 120] {
            let mut state = state_of(&df, 20);
            state.page_down();
            state.collect();
            let table = DataTable {
                glyphs: g,
                ..DataTable::default()
            };
            let rows = draw(table, &mut state, width, 21);
            let ctx = format!("{} glyphs, width {width}: {rows:#?}", set_name(g));
            assert!(rows[0].contains("id"), "{ctx}");
            assert!(rows[0].contains("desc"), "{ctx}");
            let long = rows
                .iter()
                .find(|row| row.contains("https://example.com/"))
                .expect(&ctx);
            assert!(long.contains(g.ellipsis), "{ctx}");
            for row in &rows[1..] {
                assert!(!row.trim().is_empty(), "{ctx}");
            }
        }
    }
}

/// Paging down past a page with one long value, and back, moves no column: the
/// heading row is the same on every page, at every width, in both glyph sets.
/// Measured per page, the long URL turned a six-column view into a two-column one.
/// On its page the URL is clipped and marked.
#[test]
fn widths_hold_still_across_pages() {
    let df = long_url_frame();
    for g in glyph_sets() {
        for width in [60u16, 80, 120] {
            let mut state = state_of(&df, 20);
            let table = || DataTable {
                glyphs: g,
                ..DataTable::default()
            };
            let first = draw(table(), &mut state, width, 21);
            let ctx = |rows: &[String]| format!("{} glyphs, width {width}: {rows:#?}", set_name(g));
            if width >= 80 {
                assert!(first[0].contains("timestamp"), "{}", ctx(&first));
            }
            let mut saw_url = false;
            for step in 0..6 {
                if step < 3 {
                    state.page_down();
                } else {
                    state.page_up();
                }
                state.collect();
                let rows = draw(table(), &mut state, width, 21);
                assert_eq!(rows[0], first[0], "page {step}: {}", ctx(&rows));
                if let Some(row) = rows.iter().find(|r| r.contains("https://")) {
                    saw_url = true;
                    let clipped = row.split_whitespace().nth(1).unwrap();
                    assert!(clipped.ends_with(g.ellipsis), "{}", ctx(&rows));
                }
            }
            assert!(saw_url, "the long URL's page was drawn");
        }
    }
}

/// A frozen column keeps its width across pages, so the number of columns that
/// stay frozen does not change with the page either.
#[test]
fn frozen_columns_hold_still_across_pages() {
    let df = long_url_frame();
    for g in glyph_sets() {
        for width in [60u16, 80, 120] {
            let mut state = state_of(&df, 20);
            state.set_locked_columns(3);
            let table = || DataTable {
                glyphs: g,
                ..DataTable::default()
            };
            let first = draw(table(), &mut state, width, 21);
            let frozen = state.frozen_shown();
            for _ in 0..3 {
                state.page_down();
                state.collect();
                let rows = draw(table(), &mut state, width, 21);
                let ctx = format!("{} glyphs, width {width}: {rows:#?}", set_name(g));
                assert_eq!(state.frozen_shown(), frozen, "{ctx}");
                assert_eq!(rows[0], first[0], "{ctx}");
            }
        }
    }
}

/// A number's column widens for a wider number on a later page, so it is never
/// cut, and does not narrow again on a page of narrower ones.
#[test]
fn a_number_column_widens_and_stays_wide() {
    let values: Vec<i64> = (0..60)
        .map(|i| if i == 30 { 123_456_789 } else { i })
        .collect();
    let df =
        df!("n" => values, "t" => (0..60).map(|i| format!("t{i}")).collect::<Vec<_>>()).unwrap();
    let mut state = state_of(&df, 20);
    draw(DataTable::default(), &mut state, 60, 21);
    assert_eq!(state.shown_width("n"), Some(2));
    state.page_down();
    state.collect();
    let rows = draw(DataTable::default(), &mut state, 60, 21);
    assert!(rows.iter().any(|r| r.contains("123456789")), "{rows:#?}");
    state.page_down();
    state.collect();
    draw(DataTable::default(), &mut state, 60, 21);
    assert_eq!(state.shown_width("n"), Some(9));
}

/// A struct is a preview like text: one long value on a later page is clipped
/// at the cap, and the columns after it stay on screen on the pages after.
#[test]
fn a_long_struct_value_does_not_widen_its_column_for_good() {
    let n = 60usize;
    let y: Vec<String> = (0..n)
        .map(|i| {
            if i == 30 {
                "a very long struct value ".repeat(6)
            } else {
                "short".to_string()
            }
        })
        .collect();
    let s = StructChunked::from_series(
        "s".into(),
        n,
        [
            Series::new("x".into(), (0..n as i64).collect::<Vec<_>>()),
            Series::new("y".into(), y),
        ]
        .iter(),
    )
    .unwrap()
    .into_series();
    let df = DataFrame::new_infer_height(vec![
        Series::new("id".into(), (0..n as i64).collect::<Vec<_>>()).into(),
        s.into(),
        Series::new("tail".into(), (0..n as i64).collect::<Vec<_>>()).into(),
    ])
    .unwrap();
    let mut state = state_of(&df, 20);
    for page in 0..3 {
        let rows = draw(DataTable::default(), &mut state, 80, 21);
        assert!(rows[0].contains("tail"), "page {page}: {rows:#?}");
        assert!(
            state.shown_width("s").is_some_and(|w| w <= 32),
            "page {page}: {rows:#?}"
        );
        if page == 1 {
            let long = rows.iter().find(|r| r.contains("{30,")).unwrap();
            assert!(long.contains(crate::glyphs::get().ellipsis), "{rows:#?}");
        }
        state.page_down();
        state.collect();
    }
}

/// Automatic text and headings stop at two fifths of the terminal, marked where
/// cut: 32 cells at 80 columns, 48 at 120.
#[test]
fn automatic_text_stops_at_the_cap_and_is_marked() {
    let long = "x".repeat(200);
    let df = df!("text" => &[long.as_str()], "tail" => &[1i64]).unwrap();
    for g in glyph_sets() {
        for (width, cap) in [(80u16, 32u16), (120, 48)] {
            let mut state = state_of(&df, 1);
            let table = DataTable {
                glyphs: g,
                ..DataTable::default()
            };
            let rows = draw(table, &mut state, width, 2);
            let ctx = format!("{} glyphs, width {width}: {rows:#?}", set_name(g));
            assert_eq!(state.shown_width("text"), Some(cap), "{ctx}");
            let row: String = rows[1].chars().skip(1).collect();
            let value = row.split_whitespace().next().unwrap();
            assert!(value.ends_with(g.ellipsis), "{ctx}");
            assert_eq!(crate::glyphs::cell_width(value), usize::from(cap), "{ctx}");
            assert!(rows[0].contains("tail"), "{ctx}");
        }
    }
}

/// The last column drawn takes the room to the table's right edge: a long text on
/// the far right runs to the edge rather than stopping at the cap. A width set by
/// hand is drawn as set, and a number stays under its heading.
#[test]
fn the_last_column_runs_to_the_right_edge() {
    let long = "x".repeat(200);
    let df = df!("id" => &[1i64], "text" => &[long.as_str()]).unwrap();
    let ellipsis = crate::glyphs::get().ellipsis;
    let mut state = state_of(&df, 1);
    let rows = draw(DataTable::default(), &mut state, 120, 2);
    let value = rows[1].trim_end();
    assert!(value.ends_with(ellipsis), "{rows:#?}");
    assert_eq!(crate::glyphs::cell_width(value), 120, "{rows:#?}");
    assert_eq!(state.shown_width("text"), Some(48), "learned at the cap");
    assert!(state.on_screen_width("text").unwrap() > 48, "drawn past it");

    state.set_width_choices([("text".to_string(), WidthChoice::Manual(20))]);
    let rows = draw(DataTable::default(), &mut state, 120, 2);
    assert_eq!(state.shown_width("text"), Some(20), "{rows:#?}");
    assert!(
        crate::glyphs::cell_width(rows[1].trim_end()) < 40,
        "{rows:#?}"
    );

    let df = df!("text" => &["ab"], "n" => &[5i64]).unwrap();
    let mut state = state_of(&df, 1);
    let rows = draw(DataTable::default(), &mut state, 120, 2);
    assert!(
        crate::glyphs::cell_width(rows[1].trim_end()) < 20,
        "{rows:#?}"
    );
}

/// The cap follows the terminal, not the table: a sidebar narrowing the table
/// moves no column.
#[test]
fn a_sidebar_moves_no_column() {
    let df = long_url_frame();
    let mut state = state_of(&df, 20);
    state.page_down();
    state.collect();
    let table = || DataTable {
        screen_width: 120,
        ..DataTable::default()
    };
    let whole = draw(table(), &mut state, 120, 21);
    let widths: Vec<_> = ["id", "description", "amount"]
        .iter()
        .map(|c| state.shown_width(c))
        .collect();
    let beside = draw(table(), &mut state, 70, 21);
    for (c, before) in ["id", "description", "amount"].iter().zip(&widths) {
        assert_eq!(state.shown_width(c), *before, "{c}: {beside:#?}");
    }
    assert!(
        whole[0].starts_with(&beside[0][..40]),
        "{whole:#?} {beside:#?}"
    );
}

/// A width set by hand: exact for text, values clipped and marked; it survives
/// paging, scrolling, reordering, hiding and resizing; fit and reset change it.
#[test]
fn a_manual_width_survives_everything_but_fit_and_reset() {
    let df = long_url_frame();
    let mut state = state_of(&df, 20);
    state.set_width_choices([("description".to_string(), WidthChoice::Manual(6))]);
    let rows = draw(DataTable::default(), &mut state, 80, 21);
    assert_eq!(state.shown_width("description"), Some(6), "{rows:#?}");
    // Six cells, the ellipsis among them: `item …`, or `ite...` in ASCII.
    let ellipsis = crate::glyphs::get().ellipsis;
    let kept = 6 - crate::glyphs::display_width(ellipsis);
    let clipped = format!("{}{ellipsis}", &"item 10"[..kept]);
    assert!(rows.iter().any(|r| r.contains(&clipped)), "{rows:#?}");

    state.page_down();
    state.collect();
    state.scroll_right();
    draw(DataTable::default(), &mut state, 80, 21);
    state.scroll_left();
    // Moved to the end, then hidden and shown again.
    let mut order = state.headers();
    order.retain(|c| c != "description");
    order.push("description".to_string());
    state.set_column_order(order.clone());
    draw(DataTable::default(), &mut state, 200, 21);
    assert_eq!(state.shown_width("description"), Some(6));
    order.pop();
    state.set_column_order(order.clone());
    draw(DataTable::default(), &mut state, 40, 21);
    order.insert(1, "description".to_string());
    state.set_column_order(order);
    draw(DataTable::default(), &mut state, 120, 21);
    assert_eq!(state.width_choice("description"), WidthChoice::Manual(6));
    assert_eq!(state.shown_width("description"), Some(6));

    // Fit takes the rows on screen: page two holds the URL.
    state.set_width_choices([("description".to_string(), WidthChoice::Fit)]);
    draw(DataTable::default(), &mut state, 120, 21);
    let url_width = 20 + 13 * 15;
    assert_eq!(
        state.width_choice("description"),
        WidthChoice::Manual(url_width)
    );
    state.reset();
    assert_eq!(state.width_choice("description"), WidthChoice::Auto);
}

/// A column scrolled out of view is fitted to the rows on screen too.
#[test]
fn a_column_out_of_view_is_fitted_to_the_page() {
    let df = long_url_frame();
    let mut state = state_of(&df, 20);
    state.page_down();
    state.collect();
    for _ in 0..3 {
        state.scroll_right();
    }
    draw(DataTable::default(), &mut state, 80, 21);
    state.set_width_choices([("description".to_string(), WidthChoice::Fit)]);
    let rows = draw(DataTable::default(), &mut state, 80, 21);
    assert!(!rows[0].contains("description"), "{rows:#?}");
    assert_eq!(
        state.width_choice("description"),
        WidthChoice::Manual(20 + 13 * 15)
    );
}

/// A number column set narrower than its numbers still shows them whole.
#[test]
fn a_number_column_set_narrow_still_shows_whole_numbers() {
    let df = df!("n" => &[1_234_567i64, 2], "t" => &["a", "b"]).unwrap();
    let mut state = state_of(&df, 2);
    state.set_width_choices([("n".to_string(), WidthChoice::Manual(4))]);
    let rows = draw(DataTable::default(), &mut state, 40, 3);
    assert!(rows[1].contains("1234567"), "{rows:#?}");
}

/// A column ending at the table's edge with more after it leaves its heading's
/// last cell to the off-screen hint, so the hint covers neither a letter nor the
/// clip marker, and the values keep the full width: a number that fits is whole,
/// never a preview.
#[test]
fn the_offscreen_hint_takes_a_heading_cell_not_a_value_cell() {
    let df = df!(
        "aaaaaaaaaa" => &["aaaaaaaaaa"],
        "bbbbbbb" => &[1_234_567i64],
        "c" => &["x"],
    )
    .unwrap();
    for g in glyph_sets() {
        for dtype_row in [false, true] {
            let table = DataTable {
                glyphs: g,
                dtype_row,
                ..DataTable::default()
            };
            let header_h = usize::from(table.header_height());
            let mut state = state_of(&df, 1);
            // The rail, ten cells, a gap and seven: the second column ends at the edge.
            let rows = draw(table, &mut state, 19, header_h as u16 + 1);
            let ctx = format!("{} glyphs, type row {dtype_row}: {rows:#?}", set_name(g));
            assert!(rows[header_h].ends_with(" 1234567"), "{ctx}");
            let hint_row = &rows[header_h - 1];
            assert!(hint_row.ends_with(g.arrow_right), "{ctx}");
            let before = hint_row.strip_suffix(g.arrow_right).unwrap();
            if dtype_row {
                assert!(before.trim_end().ends_with("i64"), "{ctx}");
            } else {
                assert!(before.ends_with(g.ellipsis), "{ctx}");
            }
        }
    }
}

/// Past the page's last row, a row down moves the page one row and formats only the
/// row it brings in; the rows still on screen keep their cells.
#[test]
fn a_row_down_moves_the_page_one_row_and_formats_only_that_row() {
    let formatted = || crate::widgets::table::tests::FORMATTED.with(std::cell::Cell::get);
    let df = df!(
        "n" => (0..100i64).collect::<Vec<_>>(),
        "s" => (0..100).map(|i| format!("v{i}")).collect::<Vec<_>>(),
    )
    .unwrap();
    let mut state = state_of(&df, 5);
    draw(DataTable::new(), &mut state, 40, 8);
    let rows = state.visible_rows;
    for _ in 0..rows - 1 {
        state.select_next();
    }
    draw(DataTable::new(), &mut state, 40, 8);
    assert_eq!(state.view.start_row, 0);
    let mut moved = 0;
    for step in 1..=20 {
        let before = formatted();
        if state.select_next() {
            state.collect();
        }
        let page = draw(DataTable::new(), &mut state, 40, 8);
        assert_eq!(state.view.start_row, step, "{page:#?}");
        assert_eq!(state.table_state.selected(), Some(rows - 1));
        assert!(page[1].contains(&format!("v{step}")), "{page:#?}");
        assert!(
            page[rows].contains(&format!("v{}", step + rows - 1)),
            "{page:#?}"
        );
        // A new buffer is a new series, formatted whole; otherwise only the new row.
        if formatted() - before == 2 {
            moved += 1;
        }
    }
    assert!(moved >= 10, "{moved} of 20 rows down reused the page");
}

/// A frame whose page did not change formats no cell; a change to the rows, their
/// order, their side of the rule or how they read draws what changed.
#[test]
fn an_unchanged_page_is_drawn_from_the_cells_kept() {
    let formatted = || crate::widgets::table::tests::FORMATTED.with(std::cell::Cell::get);
    let df = df!(
        "n" => (0..100i64).map(|i| i * 1000).collect::<Vec<_>>(),
        "s" => (0..100).map(|i| format!("v{i}")).collect::<Vec<_>>(),
    )
    .unwrap();
    let mut state = state_of(&df, 5);
    let first = draw(DataTable::new(), &mut state, 40, 8);
    assert!(first[2].contains("1000"), "{first:?}");
    let before = formatted();
    assert_eq!(draw(DataTable::new(), &mut state, 40, 8), first);
    assert_eq!(formatted(), before, "nothing formatted again");
    // The cursor moving within the page draws from the cells kept.
    state.select_next();
    draw(DataTable::new(), &mut state, 40, 8);
    assert_eq!(formatted(), before);

    let thousands = || {
        DataTable::new().with_number_format(crate::numfmt::NumberFormatSettings {
            format: crate::numfmt::NumberFormat::preset("thousands").unwrap(),
            enabled: true,
            exclude: Vec::new(),
            align_numeric_right: true,
        })
    };
    let grouped = draw(thousands(), &mut state, 40, 8);
    assert!(grouped[2].contains("1,000"), "{grouped:?}");
    assert!(formatted() > before, "a new format formats again");

    let narrow = draw(thousands(), &mut state, 12, 8);
    assert!(
        narrow.iter().all(|row| row.chars().count() <= 12),
        "{narrow:?}"
    );

    let typed = draw(thousands().with_dtype_row(true), &mut state, 40, 8);
    assert!(typed[1].contains("i64"), "{typed:?}");

    state.set_column_order(vec!["s".to_string(), "n".to_string()]);
    state.collect();
    let moved = draw(thousands(), &mut state, 40, 8);
    assert!(moved[0].find('s') < moved[0].find('n'), "{moved:?}");
    assert!(moved[2].find("v1") < moved[2].find("1,000"), "{moved:?}");

    state.set_locked_columns(1);
    state.collect();
    let frozen = draw(thousands(), &mut state, 40, 8);
    assert!(
        frozen[2].contains("v1") && frozen[2].contains("1,000"),
        "{frozen:?}"
    );

    state.scroll_to(50);
    state.collect();
    let later = draw(thousands(), &mut state, 40, 8);
    assert!(
        later[1].contains("50,000") && !later.iter().any(|r| r.contains(" 1,000")),
        "{later:?}"
    );

    state.query("select where n < 3000".to_string());
    let queried = draw(thousands(), &mut state, 40, 8);
    assert!(
        queried[1].contains("v0") && !queried.iter().any(|r| r.contains("v50")),
        "{queried:?}"
    );
}