dugix 1.0.0

A local-first terminal chat app for deep research and multi-agent work
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
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
// Casts here are on terminal-bounded values (dims, byte colors, glyph counts),
// never on unbounded user data.
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss
)]
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;

use super::cards::{
    push_assistant_stored, push_assistant_streaming, push_compaction, push_compaction_pending,
    push_error, push_mode, push_recall, push_research_plan, push_research_stage, push_session_link,
    push_skill, push_survey_section, push_tool_call_with_rows, push_user_card, push_user_command,
};
use super::welcome::render_welcome;
use crate::app_view::AppView;
use crate::history_cache::HistoryCache;
use crate::ui::markdown::line_text;

#[allow(clippy::too_many_lines)] // whole conversation view: header, day dividers, cards, stats
pub(super) fn render_history(f: &mut Frame, app: &mut AppView, area: Rect) {
    // The rightmost column is the scrollbar gutter, always reserved so lines
    // don't shift when the scrollbar appears.
    let inner = Rect {
        x: area.x,
        y: area.y,
        width: area.width.saturating_sub(1).max(1),
        height: area.height,
    };
    if app.is_welcome() {
        app.max_scroll = 0;
        // Record an empty layout so a click here can't map onto the previous
        // session's stale snapshot and open one of its URLs.
        app.sel.record_render(
            Rect::default(),
            0,
            Vec::new(),
            Vec::new(),
            Vec::new(),
            Vec::new(),
        );
        render_welcome(f, app, area);
        return;
    }
    app.welcome_targets.clear();
    let width = inner.width.max(1) as usize;
    let old_image_generation = app.history_cache.image_generation;
    sync_cache(app, width);
    let images_changed = old_image_generation != app.history_cache.image_generation;

    // The in-progress reply changes every frame, so it renders fresh after the
    // cached prefix.
    let mut tail: Vec<Line<'static>> = Vec::new();
    let mut tail_code: Vec<Option<usize>> = Vec::new();
    let mut tail_blocks: Vec<String> = Vec::new();
    let mut tail_diagrams = Vec::new();
    let mut tail_widgets = Vec::new();
    if app.viewing_stream() {
        push_assistant_streaming(
            &mut tail,
            app,
            width,
            &mut tail_code,
            &mut tail_blocks,
            super::cards::Regions {
                diagrams: &mut tail_diagrams,
                widgets: &mut tail_widgets,
            },
        );
    }
    if app.is_compacting_current_session() {
        push_compaction_pending(&mut tail, width, &app.theme);
    }
    tail_code.resize(tail.len(), None);

    let estimated_top = (app.history_cache.lines.len() + tail.len())
        .saturating_sub(usize::from(inner.height))
        .saturating_sub(app.scroll);
    app.interactive
        .reflow(&mut app.history_cache, estimated_top, inner.height);

    let cache = &app.history_cache;
    let cached_lines = cache.lines.len();
    let total = cached_lines + tail.len();

    // `app.scroll` counts lines scrolled up from the bottom (0 = follow).
    let height = inner.height as usize;
    app.history_height = height;
    let max_top = total.saturating_sub(height);
    app.max_scroll = max_top; // let the event loop clamp scrolling

    // The line count can change under the viewport, and a raw delta only pins
    // when all growth landed below it. Growth above shifts the content without
    // shifting the line index, so instead re-find the previous top line by
    // prefix (only the frontier lines grow) and pin the viewport back onto it.
    let prev_tail = std::mem::take(&mut app.prev_tail);
    let prev_top = app
        .prev_total
        .saturating_sub(height)
        .saturating_sub(app.scroll);
    let anchor: Option<&str> = if app.prev_total > 0 && !prev_tail.is_empty() {
        let prev_cached = app.prev_total.saturating_sub(prev_tail.len());
        prev_top
            .checked_sub(prev_cached)
            .and_then(|offset| prev_tail.get(offset).map(String::as_str))
    } else {
        None
    };

    let explicit_toggle = app.pin_viewport_top;
    let clicked = app.tool_click_anchor.take();
    app.pin_viewport_top = false;
    // While following the bottom the viewport tracks the newest lines, even
    // across the tail→cache transition, where a stored card renders taller
    // than the streaming tail.
    let pin_top = app.prev_total > 0 && (explicit_toggle || app.scroll > 0);
    // A toggle while following the bottom keeps following it: the re-wrap's
    // growth belongs above the viewport.
    let pin = pin_top && !(explicit_toggle && app.scroll == 0);
    if pin
        && !explicit_toggle
        && let Some(anchor) = anchor
        && anchor.chars().count() >= 12
    {
        // Scrolled up inside the streaming tail: pin to the content line, not
        // the line index. A short anchor is a frontier line still being typed,
        // where the delta path below is exact and a prefix could match wrongly.
        let prefix: String = anchor.chars().take(24).collect();
        // The anchor can only have moved into lines rendered since last frame.
        let search_from = app.prev_total.saturating_sub(prev_tail.len()).min(total);
        let mut hit: Option<usize> = None;
        let mut i = total;
        while i > search_from && hit.is_none() {
            i -= 1;
            let text = if i < cached_lines {
                line_text(&cache.lines[i])
            } else {
                line_text(&tail[i - cached_lines])
            };
            if text.starts_with(&prefix) {
                hit = Some(i);
                break;
            }
        }
        if let Some(hit) = hit {
            app.scroll = max_top.saturating_sub(hit);
        } else {
            let delta = total as i64 - app.prev_total as i64;
            if delta != 0 {
                app.scroll = (app.scroll as i64 + delta).clamp(0, app.max_scroll as i64) as usize;
            }
        }
    } else if pin && explicit_toggle {
        // A display-flag toggle re-wraps in place, so pin the viewport to the
        // message the previous top line belonged to, at the same offset within
        // it; the absolute line index would land on content shifted by all the
        // growth above.
        let mut pinned = clicked.and_then(|(owner, row, old_start)| {
            let new_start = row.map_or_else(
                || cache.owner.iter().position(|o| *o == Some(owner)),
                |index| {
                    cache
                        .batch_rows
                        .iter()
                        .find(|row| row.owner == owner && row.index == index)
                        .map(|row| row.lines.start)
                },
            );
            new_start.map(|new_start| (prev_top + new_start).saturating_sub(old_start))
        });
        if pinned.is_none() && images_changed {
            // Images can grow above the answer, so prefer surviving text over
            // the old offset within the message.
            for offset in 0..6 {
                if let Some(text) = app.sel.line_at(prev_top + offset)
                    && text.chars().count() >= 12
                    && let Some(hit) = find_line(&cache.lines, &tail, |line| line == text)
                {
                    pinned = Some(hit.saturating_sub(offset));
                    break;
                }
            }
        }
        if pinned.is_none()
            && let Some(owner) = app.sel.owner_at(prev_top)
        {
            // Walk up to the previous render's block start.
            let mut old_start = prev_top;
            while old_start > 0 && app.sel.owner_at(old_start - 1) == Some(owner) {
                old_start -= 1;
            }
            let offset = prev_top - old_start;
            // The in-flight reply's owner id is the message count at record
            // time, so its block lives in the tail.
            let (new_start, block_len) = if owner == app.messages.len() {
                (Some(cached_lines), tail.len())
            } else {
                let start = cache.owner.iter().position(|o| *o == Some(owner));
                let len = start.map_or(0, |s| {
                    cache.owner[s..]
                        .iter()
                        .take_while(|o| **o == Some(owner))
                        .count()
                });
                (start, len)
            };
            if let Some(new_start) = new_start {
                pinned = Some(new_start + offset.min(block_len.saturating_sub(1)));
            }
        }
        if pinned.is_none() {
            // Fallback: re-find the top line's text nearby — the top is often a
            // blank row or a header the toggle itself rewrites.
            'candidates: for off in [0, 1, 2, 3, 4, 5, -1, -2, -3, -4, -5] {
                let li = if off >= 0 {
                    prev_top + off as usize
                } else {
                    prev_top.saturating_sub((-off) as usize)
                };
                let Some(text) = app.sel.line_at(li) else {
                    continue;
                };
                if text.chars().count() < 12 {
                    continue;
                }
                // Exact text survives the re-wrap; a frontier line kept growing.
                if let Some(hit) = find_line(&cache.lines, &tail, |l| l == text) {
                    pinned = Some((hit as i64 - off).max(0) as usize);
                    break 'candidates;
                }
                let prefix: String = text.chars().take(24).collect();
                if let Some(hit) = find_line(&cache.lines, &tail, |l| l.starts_with(&prefix)) {
                    pinned = Some((hit as i64 - off).max(0) as usize);
                    break 'candidates;
                }
            }
        }
        if let Some(pinned) = pinned {
            app.scroll = max_top.saturating_sub(pinned);
        } else {
            let delta = total as i64 - app.prev_total as i64;
            if delta != 0 {
                app.scroll = (app.scroll as i64 + delta).clamp(0, app.max_scroll as i64) as usize;
            }
        }
    } else if pin {
        let delta = total as i64 - app.prev_total as i64;
        if delta != 0 {
            app.scroll = (app.scroll as i64 + delta).clamp(0, app.max_scroll as i64) as usize;
        }
    }
    app.prev_total = total;
    app.prev_tail = tail.iter().map(line_text).collect();

    app.scroll = app.scroll.min(app.max_scroll);
    let top = max_top.saturating_sub(app.scroll);

    // Snapshot layout, plain text and per-line owner so mouse selection can
    // map screen cells to a message.
    let cache = &app.history_cache;
    let mut plain = cache.plain.clone();
    plain.extend(tail.iter().map(line_text));
    let mut owner = cache.owner.clone();
    owner.resize(total, Some(app.messages.len()));
    let base = cache.blocks.len();
    let mut code = cache.code.clone();
    code.extend(tail_code.iter().map(|c| c.map(|id| id + base)));
    let mut blocks = cache.blocks.clone();
    blocks.append(&mut tail_blocks);
    app.sel
        .record_render(inner, top, plain, owner, code, blocks);

    let cache = &app.history_cache;
    let line_at = |i: usize| {
        if i < cached_lines {
            &cache.lines[i]
        } else {
            &tail[i - cached_lines]
        }
    };
    let hl = Style::default().bg(app.theme.selection).fg(app.theme.fg);
    let visible: Vec<Line> = (top..total.min(top + height))
        .map(|li| {
            app.sel
                .highlight(li, line_at(li), hl)
                .unwrap_or_else(|| line_at(li).clone())
        })
        .collect();

    f.render_widget(Paragraph::new(visible), inner);
    if app.popup == dugix_core::app::Popup::None {
        let regions = crate::pictures::regions(
            &app.history_cache.lines,
            &app.history_cache.image_at_line,
            top..(top + height).min(cached_lines),
        );
        app.pictures
            .paint(f.buffer_mut(), inner, top, &regions, |target| {
                if target.contains("://") {
                    match crate::app_view::remote_image(target) {
                        dugix_core::remote_image::Status::Ready(path) => Some(path),
                        _ => None,
                    }
                } else {
                    Some(std::path::PathBuf::from(target))
                }
            });
    }
    app.favicons.paint(
        f.buffer_mut(),
        inner,
        crate::app_view::favicon,
        app.popup == dugix_core::app::Popup::None,
    );
    if app.popup == dugix_core::app::Popup::None {
        let mut placements = app.history_cache.diagrams.clone();
        placements.extend(tail_diagrams.into_iter().map(|mut placement| {
            placement.row += cached_lines;
            placement
        }));
        app.diagrams.paint(f, inner, top, &placements, app.theme.bg);
    }

    let mut widgets = app.history_cache.widgets.clone();
    widgets.extend(tail_widgets.into_iter().map(|mut placement| {
        placement.row += cached_lines;
        placement.owner = app.messages.len();
        // Bind mutable state only to stored replies, never to a live task's
        // shifting message index.
        placement.complete = false;
        placement
    }));
    app.interactive.paint(
        f,
        inner,
        top,
        &widgets,
        &app.theme,
        matches!(
            app.popup,
            dugix_core::app::Popup::None | dugix_core::app::Popup::InteractiveUi
        ),
    );
    // Diagrams inside panels paint after the panels, over their placeholders.
    if app.popup == dugix_core::app::Popup::None {
        let placements = app.interactive.diagram_placements(&widgets, inner.width);
        if !placements.is_empty() {
            app.diagrams.paint(f, inner, top, &placements, app.theme.bg);
        }
    }

    // Hand-drawn: ratatui's `Scrollbar` thumb jumps on every streamed line and
    // never sits flush at the track bottom.
    if total > height {
        render_scrollbar(f, area, total, top, &app.theme);
    }
}

/// Draw the history scrollbar into the 1-column gutter at `area`'s right edge;
/// `total` is the rendered line count and `top` the first visible line.
fn render_scrollbar(
    f: &mut Frame,
    area: Rect,
    total: usize,
    top: usize,
    theme: &crate::theme::Theme,
) {
    let track = area.height as usize;
    if track == 0 {
        return;
    }
    let thumb_len = (track * track / total).clamp(1, track);
    // Map `top` onto the thumb's travel range so both scroll extremes land flush.
    let room = total.saturating_sub(track);
    let thumb_top = (top * (track - thumb_len)).checked_div(room).unwrap_or(0);
    let gutter = Rect {
        x: area.x + area.width.saturating_sub(1),
        y: area.y,
        width: 1,
        height: area.height,
    };
    let thumb_style = Style::default().fg(theme.accent);
    let track_style = Style::default().fg(theme.border_dim);
    let lines: Vec<Line> = (0..track)
        .map(|y| {
            let (symbol, style) = if y >= thumb_top && y < thumb_top + thumb_len {
                ("█", thumb_style)
            } else {
                ("║", track_style)
            };
            Line::from(Span::styled(symbol, style))
        })
        .collect();
    f.render_widget(Paragraph::new(lines), gutter);
}

/// Scan the rendered lines from the bottom for the last one matching `pred`.
fn find_line(
    cache: &[Line<'static>],
    tail: &[Line<'static>],
    pred: impl Fn(&str) -> bool,
) -> Option<usize> {
    let total = cache.len() + tail.len();
    let mut i = total;
    while i > 0 {
        i -= 1;
        let text = if i < cache.len() {
            line_text(&cache[i])
        } else {
            line_text(&tail[i - cache.len()])
        };
        if pred(&text) {
            return Some(i);
        }
    }
    None
}

// Long by design (cache sync).
#[allow(clippy::too_many_lines)]
/// Bring the cached wrapped prefix up to date, wrapping only messages not yet
/// cached: stored messages are append-only, so this is O(new) per frame.
fn sync_cache(app: &mut AppView, width: usize) {
    let image_generation = app.activate_image_context();
    if app.history_cache.msg_count != app.messages.len() {
        app.stream_image_cache.borrow_mut().clear();
    }
    if app.history_cache.msg_count > 0 && app.history_cache.image_generation != image_generation {
        app.pin_viewport_top = true;
    }
    let session = app.session.as_ref().map(|s| s.id.clone());
    if app.tool_session != session {
        app.tool_session.clone_from(&session);
        app.batch_focus = None;
        if !app.tool_open.is_empty() || !app.batch_open.is_empty() {
            app.tool_open.clear();
            app.batch_open.clear();
            app.tool_open_gen = app.tool_open_gen.wrapping_add(1);
        }
        app.tool_click_anchor = None;
    }
    let key = (
        session,
        width,
        app.show_tool_detail,
        app.settings.show_reasoning,
        app.settings.hide_hints,
        app.settings.show_stats,
        // Both generations only advance; either change invalidates the key.
        app.theme_gen
            .wrapping_add(app.tool_open_gen)
            .wrapping_add(image_generation),
    );
    // Detach the cache: the wrap pass reads the domain half through the
    // `AppView` deref, which borrows the whole view.
    let mut cache = std::mem::take(&mut app.history_cache);
    let c = &mut cache;
    if c.key != key || app.messages.len() < c.msg_count {
        if c.key.0 != key.0 {
            if let Some(sid) = &c.key.0 {
                app.session_caches.insert(sid.clone(), std::mem::take(c));
            }
            if let Some(sid) = &key.0
                && let Some(cached) = app.session_caches.remove(sid)
                && cached.key == key
                && cached.msg_count == app.messages.len()
            {
                *c = cached;
                // Reattach before returning, or the view renders empty for a frame.
                app.history_cache = cache;
                return;
            }
            *c = HistoryCache {
                key,
                ..Default::default()
            };
        } else if app.messages.len() < c.msg_count || c.key.1 != key.1 || c.key.6 != key.6 {
            *c = HistoryCache {
                key,
                ..Default::default()
            };
        } else {
            // Only display flags changed, so keep the image cache.
            let ic = std::mem::take(&mut c.image_cache);
            *c = HistoryCache {
                key,
                ..Default::default()
            };
            c.image_cache = ic;
        }
    }
    let theme = app.theme;
    for (i, m) in app.messages.iter().enumerate().skip(c.msg_count) {
        let start = c.lines.len();
        let widget_start = c.widgets.len();
        if let Some(day) = m.created_at.as_deref().map(day_of)
            && c.last_day.as_deref() != Some(day)
        {
            if c.last_day.is_some() {
                rule_line(&mut c.lines, &day_label(day), width, &theme);
            }
            c.last_day = Some(day.to_string());
        }
        if m.role == "user" || m.role == "gate_reply" {
            let images_dir = app.space.files_dir(&app.active_space.name);
            push_user_card(
                &mut c.lines,
                &mut c.image_at_line,
                &m.content,
                width,
                &theme,
                m.created_at.as_deref(),
                &images_dir,
                &mut c.image_cache,
            );
        } else if m.role == "skill" {
            push_skill(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "mode" {
            push_mode(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "recall" {
            push_recall(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "user_command" {
            push_user_command(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "compaction" {
            push_compaction(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "research_stage" {
            push_research_stage(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "error" {
            push_error(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "survey" {
            push_survey_section(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "research_plan" {
            push_research_plan(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "session_link" {
            push_session_link(&mut c.lines, &m.content, width, &theme);
        } else if m.role == "tool_call" {
            push_tool_call_with_rows(
                &mut c.lines,
                &m.content,
                app.tool_open
                    .get(&i)
                    .copied()
                    .unwrap_or(app.show_tool_detail),
                &app.settings,
                width,
                &theme,
                super::cards::CardEnv {
                    files_dir: &app.space.files_dir(&app.active_space.name),
                    studio_linked: app.studio.linked(),
                    image_at_line: &mut c.image_at_line,
                    image_cache: &mut c.image_cache,
                },
                Some(super::cards::BatchRows {
                    owner: i,
                    open: &app.batch_open,
                    focus: app.batch_focus,
                    hits: &mut c.batch_rows,
                }),
            );
        } else {
            push_assistant_stored(
                &mut c.lines,
                &mut c.image_at_line,
                &m.content,
                m,
                &app.settings,
                width,
                &mut c.code,
                &mut c.blocks,
                &theme,
                &app.space.files_dir(&app.active_space.name),
                &mut c.image_cache,
                super::cards::Regions {
                    diagrams: &mut c.diagrams,
                    widgets: &mut c.widgets,
                },
            );
        }
        for widget in &mut c.widgets[widget_start..] {
            widget.owner = i;
        }
        c.owner.resize(c.lines.len(), Some(i));
        c.code.resize(c.lines.len(), None);
        c.image_at_line.resize(c.lines.len(), None);
        let new_plain: Vec<String> = c.lines[start..].iter().map(line_text).collect();
        c.plain.extend(new_plain);
    }
    c.image_generation = image_generation;
    c.msg_count = app.messages.len();
    app.history_cache = cache;
}

/// Toggle only the tool card owning a rendered line, including its preamble.
pub(crate) fn toggle_tool_at_line(app: &mut AppView, line: usize) {
    let Some(owner) = app.sel.owner_at(line) else {
        return;
    };
    if app
        .messages
        .get(owner)
        .is_none_or(|m| m.role != "tool_call")
    {
        return;
    }
    if let Some(row) = app
        .history_cache
        .batch_rows
        .iter()
        .find(|row| row.lines.contains(&line))
    {
        let key = (owner, row.index);
        app.batch_focus = Some(key);
        if !app.batch_open.insert(key) {
            app.batch_open.remove(&key);
        }
        app.tool_click_anchor = Some((owner, Some(row.index), row.lines.start));
    } else {
        app.batch_focus = None;
        let open = app
            .tool_open
            .get(&owner)
            .copied()
            .unwrap_or(app.show_tool_detail);
        app.tool_open.insert(owner, !open);
        app.tool_click_anchor = app
            .history_cache
            .owner
            .iter()
            .position(|o| *o == Some(owner))
            .map(|start| (owner, None, start));
    }
    app.tool_open_gen = app.tool_open_gen.wrapping_add(1);
    app.pin_viewport_top = true;
    // A consumed card click must not turn its next click into word selection.
    app.sel.clear();
}

/// Enter folds the focused batch row without submitting an empty composer.
pub(crate) fn toggle_focused_batch_row(app: &mut AppView) {
    if let Some((owner, index)) = app.batch_focus
        && let Some(line) = app
            .history_cache
            .batch_rows
            .iter()
            .find(|row| row.owner == owner && row.index == index)
            .map(|row| row.lines.start)
    {
        toggle_tool_at_line(app, line);
    }
}

/// Arrow keys move between the opened batch's rows after a row is focused.
pub(crate) fn move_batch_focus(app: &mut AppView, down: bool) {
    let Some((owner, index)) = app.batch_focus else {
        return;
    };
    let next = if down {
        index.saturating_add(1)
    } else {
        index.saturating_sub(1)
    };
    if app
        .history_cache
        .batch_rows
        .iter()
        .any(|row| row.owner == owner && row.index == next)
    {
        app.batch_focus = Some((owner, next));
        app.tool_open_gen = app.tool_open_gen.wrapping_add(1);
    }
}

/// A centered `── label ──` rule.
pub(super) fn rule_line(
    out: &mut Vec<Line<'static>>,
    label: &str,
    width: usize,
    theme: &crate::theme::Theme,
) {
    let label = format!(" {label} ");
    let dashes = width.saturating_sub(crate::ui::style::width(&label));
    let line = format!(
        "{}{}{}",
        "─".repeat(dashes / 2),
        label,
        "─".repeat(dashes - dashes / 2)
    );
    out.push(Line::from(Span::styled(
        line,
        Style::default().fg(theme.border_dim),
    )));
}

/// `2026-08-08T14:32:00Z` -> `2026-08-08`.
pub(super) fn day_of(rfc3339: &str) -> &str {
    rfc3339.get(0..10).unwrap_or(rfc3339)
}

/// Human label for a "YYYY-MM-DD" day: Today / Yesterday / weekday+date.
pub(super) fn day_label(day: &str) -> String {
    let today = chrono::Local::now().format("%Y-%m-%d").to_string();
    if day == today {
        return "Today".into();
    }
    let yesterday = (chrono::Local::now() - chrono::Duration::days(1))
        .format("%Y-%m-%d")
        .to_string();
    if day == yesterday {
        return "Yesterday".into();
    }
    chrono::NaiveDate::parse_from_str(day, "%Y-%m-%d")
        .map_or_else(|_| day.to_string(), |d| d.format("%A, %B %-d").to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use dugix_core::app::App;
    use dugix_core::db::{Db, Message};

    fn msg(role: &str, content: &str) -> Message {
        Message {
            role: role.into(),
            content: content.into(),
            model: None,
            reasoning: None,
            tokens: None,
            secs: None,
            cost: None,
            phrase: None,
            persona: None,
            created_at: None,
        }
    }

    fn test_app() -> AppView {
        let db = Db::open_in_memory().unwrap();
        let space = dugix_core::space::Space {
            root: std::env::temp_dir().join(format!("dugix-hist-{}", uuid::Uuid::new_v4())),
        };
        AppView::new(App::new(db, Some("k"), space))
    }

    /// Build an app with a session and a manually-inserted streaming task.
    fn streaming_app(thinking: &str, buffer: &str) -> AppView {
        let mut a = test_app();
        let space = a.active_space.id.clone();
        let s =
            a.db.create_session("stream test", "a/one", &space, "chat")
                .unwrap();
        let session = a.db.get_session(&s.id).unwrap().unwrap();
        a.session = Some(session);
        a.settings.show_reasoning = true;
        a.show_tool_detail = true;
        // Stored history so the transcript overflows the pane.
        for i in 0..8 {
            a.messages.push(msg(
                "user",
                &format!("earlier user message number {i} with a decent amount of body text"),
            ));
            a.messages.push(msg(
                "assistant",
                &format!(
                    "earlier assistant reply {i} with a longer body so it wraps over a few lines"
                ),
            ));
        }
        let abort = tokio::spawn(async {}).abort_handle();
        let task = dugix_core::app::ChatTask {
            id: 1,
            session_id: s.id.clone(),
            session_title: "stream test".into(),
            space_id: space,
            model: "a/one".into(),
            model_id: "one".into(),
            backend: dugix_core::provider::BackendTag::OpenRouter,
            incognito: false,
            buffer: buffer.into(),
            thinking: thinking.into(),
            tool_status: None,
            usage: None,
            usage_row_id: None,
            started: std::time::Instant::now(),
            thinking_idx: 0,
            spinner_color: dugix_core::app::SpinnerColor::Green,
            abort,
        };
        a.chat_tasks.insert(1, task);
        a
    }

    /// The rendered text of the first three rows of the history pane.
    fn visible_snapshot(terminal: &ratatui::Terminal<ratatui::backend::TestBackend>) -> String {
        let buf = terminal.backend().buffer();
        let mut out = String::new();
        for y in 0..3 {
            for x in 0..buf.area.width {
                let cell = &buf[(x, y)];
                out.push_str(cell.symbol());
            }
            out.push('|');
        }
        out
    }

    #[tokio::test]
    async fn diagram_preview_renders_in_place_and_preserves_copy_metadata() {
        let mut a = test_app();
        let source = "flowchart LR\nA[Start] --> B[End]";
        a.messages.push(msg(
            "assistant",
            &format!("Before\n\n```mermaid\n{source}\n```\n\nAfter"),
        ));
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 40)).unwrap();
        terminal
            .draw(|f| render_history(f, &mut a, f.area()))
            .unwrap();
        let rows = a.history_cache.lines.len();
        let preview = a.history_cache.diagrams[0].clone();
        let block = a.history_cache.code[preview.row].expect("copyable preview");
        assert_eq!(a.history_cache.blocks[block], source);
        let completion = tokio::time::timeout(
            std::time::Duration::from_secs(10),
            a.diagrams.next_completion(),
        )
        .await
        .expect("renderer completed")
        .expect("completion");
        a.diagrams.complete(completion);
        terminal
            .draw(|f| render_history(f, &mut a, f.area()))
            .unwrap();
        assert_eq!(a.history_cache.lines.len(), rows);
        assert_eq!(a.history_cache.blocks[block], source);
        assert!(
            terminal
                .backend()
                .buffer()
                .content()
                .iter()
                .any(|cell| cell.symbol() == "▀")
        );
    }

    #[tokio::test]
    async fn late_diagram_completion_above_the_viewport_does_not_jump() {
        let mut a = test_app();
        let later = (0..40)
            .map(|i| format!("Later paragraph number {i} remains in the reading viewport.\n\n"))
            .collect::<String>();
        a.messages.push(msg(
            "assistant",
            &format!("```mermaid\nflowchart LR\nA-->B\n```\n\n{later}"),
        ));
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        a.scroll = usize::MAX;
        terminal
            .draw(|f| render_history(f, &mut a, f.area()))
            .unwrap();
        a.scroll = 5;
        terminal
            .draw(|f| render_history(f, &mut a, f.area()))
            .unwrap();
        let before = visible_snapshot(&terminal);
        assert!(before.contains("Later paragraph"), "{before}");
        let completion = tokio::time::timeout(
            std::time::Duration::from_secs(10),
            a.diagrams.next_completion(),
        )
        .await
        .expect("renderer completed")
        .expect("completion");
        a.diagrams.complete(completion);
        terminal
            .draw(|f| render_history(f, &mut a, f.area()))
            .unwrap();
        assert_eq!(visible_snapshot(&terminal), before);
    }

    #[tokio::test]
    async fn closing_a_streamed_diagram_keeps_source_and_viewport_stable() {
        let partial = "```mermaid\nflowchart LR\nA[Start] --> B[End]";
        let mut a = streaming_app("", partial);
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal
            .draw(|f| render_history(f, &mut a, f.area()))
            .unwrap();
        a.scroll = 2;
        terminal
            .draw(|f| render_history(f, &mut a, f.area()))
            .unwrap();
        let before = visible_snapshot(&terminal);
        let total = a.prev_total;
        a.chat_tasks.get_mut(&1).unwrap().buffer.push_str("\n```");
        terminal
            .draw(|f| render_history(f, &mut a, f.area()))
            .unwrap();
        assert_eq!(a.prev_total, total);
        assert_eq!(visible_snapshot(&terminal), before);
    }

    #[test]
    fn cache_appends_new_messages_and_resets_on_width_or_flag_change() {
        let mut a = test_app();
        a.messages.push(msg("user", "hello"));
        a.messages.push(msg("assistant", "**bold** answer"));
        sync_cache(&mut a, 80);
        let n = a.history_cache.lines.len();
        assert!(n > 0);
        assert_eq!(a.history_cache.msg_count, 2);
        assert_eq!(a.history_cache.plain.len(), n);

        a.messages.push(msg("user", "again"));
        sync_cache(&mut a, 80);
        assert!(a.history_cache.lines.len() > n);
        assert_eq!(a.history_cache.msg_count, 3);

        sync_cache(&mut a, 20);
        assert_eq!(a.history_cache.msg_count, 3);
        assert_eq!(a.history_cache.key.1, 20);

        a.show_tool_detail = true;
        let before = a.history_cache.lines.len();
        a.messages.push(msg(
            "tool_call",
            r#"{"name":"skill","arguments":"{}","result":"ok"}"#,
        ));
        sync_cache(&mut a, 20);
        assert!(a.history_cache.lines.len() > before);

        a.messages.clear();
        sync_cache(&mut a, 20);
        assert_eq!(a.history_cache.lines.len(), 0);
        assert_eq!(a.history_cache.msg_count, 0);
    }

    #[test]
    fn survey_section_renders_marker_header_and_dimmed_footer() {
        let mut a = test_app();
        a.messages.push(msg(
            "survey",
            "For \"fine-tuning LLMs\":\n 1. Depth or breadth?\n\nAnswer in chat — then say \"I approve\".",
        ));
        sync_cache(&mut a, 80);
        let text = a
            .history_cache
            .lines
            .iter()
            .map(line_text)
            .collect::<Vec<_>>()
            .join("\n");
        assert!(text.contains("? "), "{text}");
        assert!(text.contains("fine-tuning LLMs"), "{text}");
        assert!(text.contains("1. Depth or breadth?"), "{text}");
        assert!(text.contains("Answer in chat"), "{text}");
        assert!(
            a.history_cache
                .plain
                .iter()
                .any(|l| l.contains("Depth or breadth?"))
        );
    }

    #[test]
    fn compaction_digest_renders_with_header_and_body() {
        let mut a = test_app();
        a.messages.push(msg("user", "long conversation…"));
        a.messages
            .push(msg("compaction", "digest line one\ndigest line two"));
        sync_cache(&mut a, 80);
        let text = a
            .history_cache
            .lines
            .iter()
            .map(line_text)
            .collect::<Vec<_>>()
            .join("\n");
        assert!(text.contains("≡ "), "{text}");
        assert!(text.contains("earlier messages, summarized"), "{text}");
        assert!(text.contains("digest line one"), "{text}");
        assert!(text.contains("digest line two"), "{text}");
        assert!(
            a.history_cache
                .plain
                .iter()
                .any(|l| l.contains("digest line one")),
            "{text}"
        );
    }

    #[test]
    fn skill_row_renders_name_not_body() {
        let mut a = test_app();
        a.messages.push(msg("skill", "web-search\nSECRET BODY"));
        sync_cache(&mut a, 80);
        let text = a
            .history_cache
            .lines
            .iter()
            .map(line_text)
            .collect::<Vec<_>>()
            .join("\n");
        assert!(text.contains("skill web-search"), "{text}");
        assert!(text.contains("active for this session"), "{text}");
        assert!(!text.contains("SECRET BODY"), "{text}");
    }

    #[test]
    fn user_command_card_shows_the_command_exit_code_and_at_most_twelve_lines() {
        let mut a = test_app();
        let output: String = (1..=20).map(|n| format!("line {n}\n")).collect();
        let content = serde_json::json!({
            "cmd": "cargo test",
            "cwd": "/w",
            "exit": 101,
            "output": output,
            "interactive": false,
        })
        .to_string();
        a.messages.push(msg("user_command", &content));
        sync_cache(&mut a, 80);
        let text = a
            .history_cache
            .lines
            .iter()
            .map(line_text)
            .collect::<Vec<_>>()
            .join("\n");
        assert!(text.contains("❯ $ cargo test · exit 101"), "{text}");
        assert!(text.contains("  line 12\n"), "{text}");
        assert!(!text.contains("line 13"), "{text}");
        assert!(text.contains("… 8 more lines"), "{text}");
    }

    #[test]
    fn running_compaction_is_visible_in_the_history_pane() {
        let mut a = test_app();
        let session =
            a.db.create_session("compacting", "a/one", &a.active_space.id, "chat")
                .unwrap();
        a.session = Some(session.clone());
        a.messages.push(msg("user", "old conversation"));
        let (_tx, rx) = tokio::sync::mpsc::unbounded_channel();
        a.compaction.rx = Some(rx);
        a.compaction.session_id = Some(session.id);

        let backend = ratatui::backend::TestBackend::new(100, 20);
        let mut terminal = ratatui::Terminal::new(backend).unwrap();
        terminal
            .draw(|f| render_history(f, &mut a, f.area()))
            .unwrap();
        let text = terminal
            .backend()
            .buffer()
            .content()
            .iter()
            .map(|cell| cell.symbol().to_string())
            .collect::<String>();
        assert!(text.contains("summarizing earlier messages"), "{text}");
    }

    /// A conversation tall enough to overflow the history pane, with one
    /// tool-call block whose detail toggle produces a large line delta.
    fn tall_tool_call_app() -> AppView {
        let mut a = test_app();
        for i in 0..60 {
            a.messages.push(msg(
                "user",
                &format!("message number {i} with some body text"),
            ));
        }
        let tool_content = serde_json::json!({
            "name": "search",
            "arguments": r#"{"mode":"web","query":"q"}"#,
            "result": "line one\n".repeat(40),
        })
        .to_string();
        a.messages.push(msg("tool_call", &tool_content));
        a
    }

    #[tokio::test]
    async fn streaming_reasoning_growth_keeps_viewport_on_reasoning() {
        use std::fmt::Write as _;
        let reasoning: String = (0..60)
            .map(|i| format!("reasoning line {i} with a bunch of words to wrap around"))
            .collect::<Vec<_>>()
            .join("\n");
        let mut a = streaming_app(&reasoning, "The answer body.\nSecond answer line.");
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        a.scroll = 8;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let before = visible_snapshot(&terminal);
        assert!(
            before.contains("reasoning line"),
            "viewport should be in the reasoning, got: {before}"
        );

        let task = a.chat_tasks.get_mut(&1).unwrap();
        for i in 0..20 {
            write!(
                task.thinking,
                "\nfresh reasoning token {i} some more words to wrap"
            )
            .unwrap();
        }
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();

        let after = visible_snapshot(&terminal);
        assert!(
            after.starts_with(&before[..before.len().min(24)]),
            "viewport content jumped while reading reasoning: before={before:?} after={after:?}"
        );
    }

    #[tokio::test]
    async fn streaming_reasoning_growth_keeps_viewport_on_answer() {
        use std::fmt::Write as _;
        let reasoning: String = (0..50)
            .map(|i| format!("reasoning line {i} with a bunch of words to wrap around"))
            .collect::<Vec<_>>()
            .join("\n");
        let answer: String = (0..40)
            .map(|i| format!("answer paragraph line {i} with a bunch of words to wrap around"))
            .collect::<Vec<_>>()
            .join("\n");
        let mut a = streaming_app(&reasoning, &answer);
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();

        assert!(a.max_scroll > 0, "needs an overflowing transcript");
        a.scroll = 12;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let before = visible_snapshot(&terminal);
        assert!(
            before.contains("answer"),
            "viewport should be in the answer, got: {before}"
        );

        let task = a.chat_tasks.get_mut(&1).unwrap();
        for i in 0..30 {
            write!(
                task.thinking,
                "\nmore reasoning token {i} with some words to wrap"
            )
            .unwrap();
        }
        task.buffer.push_str(" more answer tokens here.");
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();

        let after = visible_snapshot(&terminal);
        assert!(
            after.starts_with(&before[..before.len().min(24)]),
            "viewport content jumped: before={before:?} after={after:?}"
        );
    }

    #[tokio::test]
    async fn streaming_tail_to_cache_transition_does_not_jump() {
        let mut a = streaming_app(
            "some reasoning text here\nmore of it",
            &"A final answer body with enough text to wrap over several lines. ".repeat(6),
        );
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        a.scroll = 3;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let before = visible_snapshot(&terminal);

        a.on_chat_event(1, dugix_core::provider::StreamEvent::Done)
            .unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();

        let after = visible_snapshot(&terminal);
        assert!(
            after.starts_with(&before[..before.len().min(24)]),
            "tail->cache transition jumped: before={before:?} after={after:?}"
        );
    }

    #[tokio::test]
    async fn streaming_tail_to_cache_transition_pins_inside_the_tail() {
        let reasoning: String = (0..40)
            .map(|i| format!("reasoning line {i} with a bunch of words to wrap around"))
            .collect::<Vec<_>>()
            .join("\n");
        let mut a = streaming_app(&reasoning, "A final answer body line one.\nBody line two.");
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        a.scroll = 5;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let before = visible_snapshot(&terminal);
        assert!(
            before.contains("reasoning line"),
            "viewport should be in the reasoning, got: {before}"
        );

        a.on_chat_event(1, dugix_core::provider::StreamEvent::Done)
            .unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();

        let after = visible_snapshot(&terminal);
        assert!(
            after.starts_with(&before[..before.len().min(24)]),
            "tail->cache transition inside the tail jumped: before={before:?} after={after:?}"
        );
    }

    #[tokio::test]
    async fn tool_call_landing_above_viewport_keeps_position() {
        let reasoning: String = (0..60)
            .map(|i| format!("reasoning line {i} with a bunch of words to wrap around"))
            .collect::<Vec<_>>()
            .join("\n");
        let mut a = streaming_app(&reasoning, "answer body line one\nanswer body line two");
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        a.scroll = 10;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let before = visible_snapshot(&terminal);
        assert!(
            before.contains("reasoning line"),
            "viewport should be in the reasoning, got: {before}"
        );

        a.on_chat_event(
            1,
            dugix_core::provider::StreamEvent::ToolCall {
                id: "call_0".into(),
                reasoning: None,
                assistant_content: None,
                step_start: true,
                name: "web_search".into(),
                arguments: r#"{"query":"some search query text"}"#.into(),
                result: "result line one\nresult line two\nresult line three".into(),
            },
        )
        .unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();

        let after = visible_snapshot(&terminal);
        assert!(
            after.starts_with(&before[..before.len().min(24)]),
            "tool call landing above the viewport jumped: before={before:?} after={after:?}"
        );
    }

    #[tokio::test]
    async fn opened_batch_completion_above_a_streaming_viewport_does_not_jump() {
        let reasoning = (0..60)
            .map(|index| format!("reasoning line {index} with enough words to identify it"))
            .collect::<Vec<_>>()
            .join("\n");
        let mut app = streaming_app(&reasoning, "answer line one\nanswer line two");
        app.show_tool_detail = true;
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal
            .draw(|frame| crate::ui::render(frame, &mut app))
            .unwrap();
        app.scroll = 10;
        terminal
            .draw(|frame| crate::ui::render(frame, &mut app))
            .unwrap();
        let before = visible_snapshot(&terminal);
        let args = serde_json::json!({"calls": (0..6).map(|index| serde_json::json!({"tool":"files","arguments":{"action":"read","name":format!("{index}.txt")}})).collect::<Vec<_>>()}).to_string();
        let result = (0..6)
            .map(|index| {
                format!(
                    "[{}/6] files/read {index}.txt\ncontents of file {index}",
                    index + 1
                )
            })
            .collect::<Vec<_>>()
            .join("\n");
        app.on_chat_event(
            1,
            dugix_core::provider::StreamEvent::ToolCall {
                id: "batch-complete".into(),
                reasoning: None,
                assistant_content: None,
                step_start: true,
                name: "batch".into(),
                arguments: args,
                result,
            },
        )
        .unwrap();
        terminal
            .draw(|frame| crate::ui::render(frame, &mut app))
            .unwrap();
        let after = visible_snapshot(&terminal);
        assert!(
            after.starts_with(&before[..24]),
            "batch completion jumped: before={before:?}, after={after:?}"
        );
        assert_eq!(app.history_cache.batch_rows.len(), 6);
    }

    #[test]
    fn opening_a_batch_row_keeps_its_screen_position_and_other_rows_closed() {
        let mut app = test_app();
        for index in 0..20 {
            app.messages
                .push(msg("assistant", &format!("Before batch message {index}.")));
        }
        app.messages.push(msg("tool_call", &serde_json::json!({
            "name":"batch", "arguments":serde_json::json!({"calls":[
                {"tool":"search","arguments":{"query":"needle"}},
                {"tool":"fetch_url","arguments":{"url":"https://example.test"}}
            ]}).to_string(),
            "result":"[1/2] search  \"needle\"\n[1] A result\n    https://example.test\n    A result snippet.\n[2/2] fetch_url https://example.test\nA page preview"
        }).to_string()));
        for index in 0..30 {
            app.messages
                .push(msg("assistant", &format!("After batch message {index}.")));
        }
        app.show_tool_detail = true;
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal
            .draw(|frame| render_history(frame, &mut app, frame.area()))
            .unwrap();
        let row = app.history_cache.batch_rows[0].lines.start;
        app.scroll = app.max_scroll - (row - 5);
        terminal
            .draw(|frame| render_history(frame, &mut app, frame.area()))
            .unwrap();
        for open in [true, false] {
            let line = app.sel.pos_at(4, 5).unwrap().0;
            toggle_tool_at_line(&mut app, line);
            terminal
                .draw(|frame| render_history(frame, &mut app, frame.area()))
                .unwrap();
            let screen: String = (0..79)
                .map(|x| terminal.backend().buffer()[(x, 5)].symbol())
                .collect();
            assert!(screen.contains("1  ◎ search \"needle\""), "{screen}");
            assert!(screen.contains(if open { '▾' } else { '▸' }));
            assert_eq!(app.batch_open.contains(&(20, 0)), open);
            assert!(!app.batch_open.contains(&(20, 1)));
            assert!(app.scroll > 0);
        }
        app.batch_open.insert((20, 0));
        let space = app.active_space.id.clone();
        app.session = Some(app.db.create_session("other", "m", &space, "chat").unwrap());
        sync_cache(&mut app, 79);
        assert!(app.batch_open.is_empty());
        assert_eq!(app.batch_focus, None);
    }

    #[test]
    fn switching_back_to_a_session_reuses_its_wrapped_history() {
        let mut a = tall_tool_call_app();
        let first =
            a.db.create_session("first", "m", &a.active_space.id, "chat")
                .unwrap();
        let second =
            a.db.create_session("second", "m", &a.active_space.id, "chat")
                .unwrap();
        a.session = Some(first.clone());
        sync_cache(&mut a, 80);
        let wrapped = a.history_cache.lines.len();
        assert!(wrapped > 0);
        let messages = std::mem::take(&mut a.messages);
        a.session = Some(second);
        sync_cache(&mut a, 80);
        a.messages = messages;
        a.session = Some(first);
        sync_cache(&mut a, 80);
        assert_eq!(
            a.history_cache.lines.len(),
            wrapped,
            "restored, not dropped"
        );
    }

    #[test]
    fn clicked_card_header_stays_on_its_screen_row_when_scrolled_up() {
        let mut app = tall_tool_call_app();
        for n in 0..40 {
            app.messages.push(msg(
                "assistant",
                &format!("Later message {n} after the clicked card."),
            ));
        }
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal
            .draw(|f| render_history(f, &mut app, f.area()))
            .unwrap();
        let header = app
            .history_cache
            .owner
            .iter()
            .position(|o| *o == Some(60))
            .unwrap();
        app.scroll = app.max_scroll - (header - 5);
        terminal
            .draw(|f| render_history(f, &mut app, f.area()))
            .unwrap();
        assert!(app.scroll > 0);
        for open in [true, false] {
            let line = app.sel.pos_at(3, 5).unwrap().0;
            toggle_tool_at_line(&mut app, line);
            terminal
                .draw(|f| render_history(f, &mut app, f.area()))
                .unwrap();
            let row: String = (0..79)
                .map(|x| terminal.backend().buffer()[(x, 5)].symbol())
                .collect();
            assert!(row.contains("search \"q\""), "{row}");
            assert!(row.contains(if open { '▾' } else { '▸' }), "{row}");
            assert!(app.scroll > 0);
        }
    }

    #[test]
    fn per_card_cache_generation_and_session_reset_do_not_leak_overrides() {
        let mut app = tall_tool_call_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal
            .draw(|f| render_history(f, &mut app, f.area()))
            .unwrap();
        let header = app
            .history_cache
            .owner
            .iter()
            .position(|o| *o == Some(60))
            .unwrap();
        let collapsed = app.history_cache.lines.len();
        toggle_tool_at_line(&mut app, header);
        terminal
            .draw(|f| render_history(f, &mut app, f.area()))
            .unwrap();
        assert!(app.history_cache.lines.len() > collapsed);
        assert_eq!(app.scroll, 0, "clicks at the bottom keep following it");
        app.invalidate_history_cache();
        terminal
            .draw(|f| render_history(f, &mut app, f.area()))
            .unwrap();
        assert_eq!(app.tool_open.get(&60), Some(&true));
        app.apply_event(&dugix_core::app::AppEvent::ViewportReset);
        assert!(app.tool_open.is_empty());
        terminal
            .draw(|f| render_history(f, &mut app, f.area()))
            .unwrap();
        assert_eq!(app.history_cache.lines.len(), collapsed);
        toggle_tool_at_line(&mut app, header);
        let space = app.active_space.id.clone();
        let session = app
            .db
            .create_session("other", "a/one", &space, "chat")
            .unwrap();
        app.session = Some(session);
        sync_cache(&mut app, 79);
        assert!(app.tool_open.is_empty());
    }

    #[test]
    fn flag_toggle_pins_viewport_when_scrolled_up() {
        let mut a = tall_tool_call_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let total1 = a.history_cache.lines.len();
        assert!(a.max_scroll > 10, "conversation should overflow the pane");

        a.scroll = 5;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        assert_eq!(a.scroll, 5);
        a.show_tool_detail = true;
        a.pin_viewport_top = true;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let total2 = a.history_cache.lines.len();
        let delta = total2 as i64 - total1 as i64;
        assert!(delta > 20, "tool detail should add many lines");
        assert_eq!(
            a.scroll,
            5 + delta as usize,
            "viewport top must stay pinned"
        );

        a.show_tool_detail = false;
        a.pin_viewport_top = true;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        assert_eq!(a.scroll, 5, "collapse must restore the pinned position");
    }

    #[test]
    fn flag_toggle_while_following_bottom_stays_at_bottom() {
        let mut a = tall_tool_call_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let total1 = a.history_cache.lines.len();
        assert_eq!(a.scroll, 0, "starts following the bottom");

        a.show_tool_detail = true;
        a.pin_viewport_top = true;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let total2 = a.history_cache.lines.len();
        assert!(total2 > total1, "tool detail should add lines");
        assert_eq!(a.scroll, 0, "must keep following the bottom");
    }

    /// A tall conversation with reasoning traces on many assistant messages.
    fn tall_reasoning_app() -> AppView {
        let mut a = test_app();
        for i in 0..40 {
            a.messages.push(msg(
                "user",
                &format!("question number {i} with some body text"),
            ));
            let mut m = msg(
                "assistant",
                &format!("answer number {i} with a decent amount of body text"),
            );
            m.reasoning = Some(format!(
                "thinking trace {i} line one\nthinking trace {i} line two\nthinking trace {i} line three"
            ));
            m.model = Some("a/one".into());
            m.phrase = Some("working".into());
            a.messages.push(m);
        }
        a.settings.show_reasoning = false;
        a
    }

    #[test]
    fn flag_toggle_reasoning_while_scrolled_up_pins_content() {
        let mut a = tall_reasoning_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let total1 = a.history_cache.lines.len();
        assert!(a.max_scroll > 10, "conversation should overflow the pane");

        a.scroll = 25;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let before = visible_snapshot(&terminal);
        assert!(
            before.contains("answer number 34"),
            "viewport should be in the middle of the conversation, got: {before}"
        );

        a.settings.show_reasoning = true;
        a.pin_viewport_top = true;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let total2 = a.history_cache.lines.len();
        assert!(total2 > total1, "reasoning should add lines");
        let after = visible_snapshot(&terminal);
        assert!(
            after.starts_with(&before[..before.len().min(24)]),
            "viewport content jumped on reasoning toggle: before={before:?} after={after:?}"
        );

        a.settings.show_reasoning = false;
        a.pin_viewport_top = true;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let after2 = visible_snapshot(&terminal);
        assert!(
            after2.starts_with(&before[..before.len().min(24)]),
            "viewport content jumped on reasoning collapse: before={before:?} after={after2:?}"
        );
    }

    #[test]
    fn flag_toggle_reasoning_while_following_bottom_stays_at_bottom() {
        let mut a = tall_reasoning_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        assert_eq!(a.scroll, 0, "starts following the bottom");

        a.settings.show_reasoning = true;
        a.pin_viewport_top = true;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        assert_eq!(a.scroll, 0, "must keep following the bottom");
        let buf = terminal.backend().buffer();
        let all: String = (0..HISTORY_H)
            .flat_map(|y| {
                (0..buf.area.width - 1).map(move |x| buf[(x, y as u16)].symbol().to_string())
            })
            .collect();
        assert!(
            all.contains("thinking trace 39"),
            "the last message's reasoning must be visible at the bottom"
        );
    }

    #[tokio::test]
    async fn flag_toggle_while_streaming_and_scrolled_into_tail_stays_pinned() {
        let reasoning: String = (0..60)
            .map(|i| format!("reasoning line {i} with a bunch of words to wrap around"))
            .collect::<Vec<_>>()
            .join("\n");
        let mut a = streaming_app(&reasoning, "The answer body.\nSecond answer line.");
        a.messages.push(msg(
            "tool_call",
            r#"{"name":"search","arguments":"{}","result":"line one\nline two"}"#,
        ));
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        a.scroll = 8;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let before = visible_snapshot(&terminal);
        assert!(
            before.contains("reasoning line"),
            "viewport should be in the tail, got: {before}"
        );

        a.show_tool_detail = true;
        a.pin_viewport_top = true;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let after = visible_snapshot(&terminal);
        assert!(
            after.starts_with(&before[..before.len().min(24)]),
            "viewport jumped on toggle while streaming: before={before:?} after={after:?}"
        );
    }

    /// The scrollbar gutter column (x = width - 1): (symbol, fg) per row.
    fn gutter_snapshot(
        terminal: &ratatui::Terminal<ratatui::backend::TestBackend>,
    ) -> Vec<(String, ratatui::style::Color)> {
        let buf = terminal.backend().buffer();
        let x = crate::ui::reading_column(buf.area, "auto").right() - 1;
        (0..buf.area.height)
            .map(|y| (buf[(x, y)].symbol().to_string(), buf[(x, y)].fg))
            .collect()
    }

    /// Height of the history pane on an 80x24 terminal with an empty composer.
    const HISTORY_H: usize = 20;

    #[test]
    fn scrollbar_thumb_sits_flush_at_bottom_when_following() {
        let mut a = tall_tool_call_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        assert_eq!(a.scroll, 0, "starts following the bottom");
        let gutter = gutter_snapshot(&terminal);
        assert_eq!(
            gutter[HISTORY_H - 1].1,
            a.theme.accent,
            "thumb must sit flush at the track bottom — no dead gap"
        );
        assert_eq!(gutter[0].1, a.theme.border_dim);
    }

    #[test]
    fn scrollbar_thumb_sits_flush_at_top_when_scrolled_to_start() {
        let mut a = tall_tool_call_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        a.scroll = a.max_scroll;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let gutter = gutter_snapshot(&terminal);
        assert_eq!(
            gutter[0].1, a.theme.accent,
            "thumb must sit flush at the track top when scrolled to the start"
        );
    }

    #[test]
    fn scrollbar_thumb_tracks_viewport_proportionally() {
        let mut a = tall_tool_call_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let total = a.history_cache.lines.len();
        assert!(total > HISTORY_H, "conversation should overflow the pane");
        a.scroll = a.max_scroll / 2;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let top = a.max_scroll - a.scroll;
        let thumb_len = (HISTORY_H * HISTORY_H / total).clamp(1, HISTORY_H);
        let expect_top = top * (HISTORY_H - thumb_len) / (total - HISTORY_H);
        let gutter = gutter_snapshot(&terminal);
        let accent_rows: Vec<usize> = (0..HISTORY_H)
            .filter(|&y| gutter[y].1 == a.theme.accent)
            .collect();
        assert_eq!(
            accent_rows.len(),
            thumb_len,
            "thumb length is the viewport fraction"
        );
        assert_eq!(
            accent_rows.first(),
            Some(&expect_top),
            "thumb top maps to the viewport"
        );
    }

    #[tokio::test]
    async fn stream_end_while_following_bottom_stays_at_bottom() {
        let mut a = streaming_app(
            "some reasoning text here\nmore of it",
            "A short final answer.",
        );
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        assert_eq!(a.scroll, 0, "follows the bottom while streaming");

        a.on_chat_event(1, dugix_core::provider::StreamEvent::Done)
            .unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        assert_eq!(
            a.scroll, 0,
            "must keep following the bottom after the stream ends"
        );
        let buf = terminal.backend().buffer();
        let col = crate::ui::reading_column(buf.area, "auto");
        let bottom_row: String = (col.x..col.right() - 1)
            .map(|x| buf[(x, HISTORY_H as u16 - 1)].symbol().to_string())
            .collect();
        assert!(
            bottom_row.trim().is_empty(),
            "the message's trailing blank row must sit at the pane bottom, got: {bottom_row:?}"
        );
    }

    // ---- remote images in replies ----

    const PIC: &str = "https://img.example.invalid/pic.png";

    fn tiny_png(size: u32) -> Vec<u8> {
        let image = image::RgbImage::from_fn(size, size, |x, y| {
            image::Rgb([x as u8 * 6, y as u8 * 6, 80])
        });
        let mut out = Vec::new();
        image
            .write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Png)
            .unwrap();
        out
    }

    fn screen(terminal: &ratatui::Terminal<ratatui::backend::TestBackend>) -> Vec<String> {
        let buf = terminal.backend().buffer();
        (0..buf.area.height)
            .map(|y| {
                (0..buf.area.width)
                    .map(|x| buf[(x, y)].symbol())
                    .collect::<String>()
            })
            .collect()
    }

    /// 40 question/answer pairs; the answer at pair 10 carries a remote image.
    fn remote_image_app() -> AppView {
        let mut a = test_app();
        for i in 0..40 {
            a.messages.push(msg(
                "user",
                &format!("question number {i} with some body text"),
            ));
            let body = if i == 10 {
                format!(
                    "answer number {i} with a decent amount of text\n\n![the diagram]({PIC})\n\nmore words after the picture"
                )
            } else {
                format!("answer number {i} with a decent amount of body text")
            };
            let mut m = msg("assistant", &body);
            m.model = Some("a/one".into());
            m.phrase = Some("working".into());
            a.messages.push(m);
        }
        a
    }

    fn history_text(a: &AppView) -> String {
        a.history_cache
            .lines
            .iter()
            .map(line_text)
            .collect::<Vec<_>>()
            .join("\n")
    }

    #[test]
    fn remote_image_placeholder_swaps_for_pixels_without_moving_the_viewport() {
        let mut a = remote_image_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let text = history_text(&a);
        assert!(
            text.contains("\u{25a3} the diagram \u{b7} loading"),
            "{text}"
        );
        let before_lines = a.history_cache.lines.len();

        a.scroll = 25;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let before = visible_snapshot(&terminal);
        let before_screen = screen(&terminal);
        assert!(before.contains("answer number"), "{before}");

        a.remote_images.seed(PIC, &tiny_png(40)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        assert!(
            a.history_cache.lines.len() > before_lines + 5,
            "the image should add rows once it is known"
        );
        assert!(!history_text(&a).contains("loading"));
        assert_eq!(
            screen(&terminal),
            before_screen,
            "late image rows must not move the viewport"
        );

        a.scroll = 0;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let bottom = screen(&terminal);
        a.remote_images.seed_failure(PIC);
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        assert_eq!(a.scroll, 0);
        assert_eq!(screen(&terminal), bottom);
    }

    #[test]
    fn remote_image_shows_failure_and_disabled_text() {
        let mut a = remote_image_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        a.remote_images.seed_failure(PIC);
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let text = history_text(&a);
        assert!(
            text.contains("\u{25a3} the diagram \u{b7} img.example.invalid (couldn't load)"),
            "{text}"
        );

        a.core.settings.remote_images = false;
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let text = history_text(&a);
        assert!(
            text.contains("\u{25a3} the diagram \u{b7} img.example.invalid"),
            "{text}"
        );
        assert!(
            !text.contains("couldn't load") && !text.contains("loading"),
            "{text}"
        );
        assert!(!a.remote_images.enabled());
    }

    #[test]
    fn clicking_a_remote_image_row_targets_its_url() {
        let mut a = remote_image_app();
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 24)).unwrap();
        a.remote_images.seed(PIC, &tiny_png(40)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut a)).unwrap();
        let opened: Vec<_> = a.history_cache.image_at_line.iter().flatten().collect();
        assert!(opened.len() > 3);
        assert!(opened.iter().all(|target| target.as_str() == PIC));
    }
}

#[cfg(test)]
mod card_background_tests {
    use super::*;
    use dugix_core::app::App;
    use dugix_core::db::{Db, Message};
    use dugix_core::space::Space;

    #[test]
    fn user_card_uses_configured_bg_and_stays_right_aligned() {
        let db = Db::open_in_memory().unwrap();
        let space = Space {
            root: std::env::temp_dir().join(format!("dugix-tint-{}", uuid::Uuid::new_v4())),
        };
        let mut app = AppView::new(App::new(db, Some("k"), space));
        app.messages.push(Message {
            role: "user".into(),
            content: "a short note".into(),
            model: None,
            reasoning: None,
            tokens: None,
            secs: None,
            cost: None,
            phrase: None,
            persona: None,
            created_at: Some("2026-08-08T10:00:00Z".into()),
        });
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(80, 10)).unwrap();
        terminal.draw(|f| crate::ui::render(f, &mut app)).unwrap();
        let buf = terminal.backend().buffer();
        let mut body_start = None;
        for y in 0..buf.area.height {
            for x in 0..buf.area.width {
                let cell = &buf[(x, y)];
                if cell.symbol() == "a" {
                    body_start = Some((x, cell.bg));
                    break;
                }
            }
            if body_start.is_some() {
                break;
            }
        }
        let (body_start, body_bg) = body_start.expect("user message should be rendered");
        assert_eq!(
            body_bg, app.theme.raised,
            "card should use the raised shade (the terminal's own bg when transparent)"
        );
        assert!(
            body_start >= 30,
            "card should be right-aligned, got body start at {body_start}"
        );
    }
}