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
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
//! MarkdownRenderer for streaming markdown output with ANSI formatting.
use super::*;
/// Incremental markdown renderer for streamed text output.
/// Tracks state across partial deltas to apply ANSI formatting for
/// code blocks, inline code, bold text, and headers.
///
/// Designed for LLM streaming: mid-line tokens are rendered immediately
/// with inline formatting. Only line boundaries buffer briefly to detect
/// code fences (`` ``` ``) and headers (`#`).
pub struct MarkdownRenderer {
in_code_block: bool,
code_lang: Option<String>,
line_buffer: String,
/// Whether we're at the start of a new line (need to detect fence/header).
line_start: bool,
/// When a block element prefix (list marker, blockquote `>`) has been rendered
/// early for streaming, this tracks the prefix so we don't re-render on newline.
/// Once set, subsequent tokens stream as inline text until the newline arrives.
block_prefix_rendered: bool,
}
impl MarkdownRenderer {
/// Create a new renderer with empty state.
pub fn new() -> Self {
Self {
in_code_block: false,
code_lang: None,
line_buffer: String::new(),
line_start: true,
block_prefix_rendered: false,
}
}
/// Process a delta chunk and return ANSI-formatted output.
///
/// **Streaming behavior:**
/// - At line start, buffers briefly to detect code fences/headers (typically 1–4 chars)
/// - At line start with word boundary (text + trailing space), flushes via
/// `flush_on_whitespace()` for word-by-word prose streaming
/// - Mid-line, renders immediately with inline formatting (bold, inline code)
/// - Complete lines (ending with `\n`) are always processed immediately
///
/// ## render_latency_budget
///
/// The renderer is designed for minimal token-to-display latency:
///
/// | Path | Buffering | Expected latency |
/// |-------------------------|-----------------------|------------------|
/// | Mid-line text | None (immediate) | ~0 (no alloc) |
/// | Mid-line code block | None (immediate) | ~0 (dim wrap) |
/// | Line-start, non-special | Flush after 1 char | ~0 |
/// | Line-start, word boundary | Flush on whitespace | ~1 token |
/// | Line-start, ambiguous | Buffer 1–4 chars | 1 token |
/// | Line-start, code block | Buffer until non-`\`` | 1 token |
///
/// **Flush contract:** Every call to `render_delta()` that produces output
/// expects the caller to call `io::stdout().flush()` immediately after
/// printing. This ensures tokens appear on screen without stdio batching.
/// The caller in `prompt.rs::handle_events()` does this after every delta.
///
/// **Do not regress:** Adding new buffering paths (e.g., for tables or
/// footnotes) must preserve the mid-line fast path. Any change that causes
/// mid-line tokens to return empty strings is a latency regression.
pub fn render_delta(&mut self, delta: &str) -> String {
let mut output = String::new();
// Mid-line fast paths: render tokens immediately without buffering.
// Code fences and headers only matter at line start, so mid-line is safe.
if !self.line_start {
if self.in_code_block {
// Mid-line inside a code block: emit tokens immediately with
// appropriate styling (dim or syntax-highlighted) instead of
// buffering until a complete line. This gives token-by-token
// streaming for code blocks (issue #147).
if let Some(newline_pos) = delta.find('\n') {
let mid_line_part = &delta[..newline_pos];
if !mid_line_part.is_empty() {
output.push_str(&self.render_code_inline(mid_line_part));
}
output.push('\n');
self.line_start = true;
self.block_prefix_rendered = false;
// Process the rest (after the first \n) via buffered path
// because we're now at line start and need fence detection.
let rest = &delta[newline_pos + 1..];
if !rest.is_empty() {
output.push_str(&self.render_delta_buffered(rest));
}
} else {
// No newline — pure mid-line code content, render immediately
output.push_str(&self.render_code_inline(delta));
}
return output;
}
// Mid-line outside a code block: render with inline formatting
if let Some(newline_pos) = delta.find('\n') {
// Render the mid-line portion immediately
let mid_line_part = &delta[..newline_pos];
if !mid_line_part.is_empty() {
output.push_str(&self.render_inline(mid_line_part));
}
output.push('\n');
self.line_start = true;
self.block_prefix_rendered = false;
// Process the rest (after the first \n) by buffering
let rest = &delta[newline_pos + 1..];
if !rest.is_empty() {
output.push_str(&self.render_delta_buffered(rest));
}
} else {
// No newline — pure mid-line content, render immediately
output.push_str(&self.render_inline(delta));
}
return output;
}
// We're at line start — use buffered approach (needed to detect fences, headers)
output.push_str(&self.render_delta_buffered(delta));
output
}
/// Render a code block fragment with dim styling for immediate streaming.
/// Used for mid-line token-by-token output inside code blocks.
/// Full syntax highlighting is applied to complete lines (at newline boundaries);
/// fragments get dim styling for responsiveness.
fn render_code_inline(&self, text: &str) -> String {
format!("{DIM}{text}{RESET}")
}
/// Buffered rendering: adds delta to line_buffer, processes complete lines,
/// and attempts early flush of line-start content when safe.
///
/// render_latency_budget: This path is only entered at line start. The buffer
/// holds at most 1–4 characters before resolving. The `needs_line_buffering()`
/// check and `try_resolve_block_prefix()` aim to flush as early as possible,
/// switching to the mid-line fast path for subsequent tokens.
fn render_delta_buffered(&mut self, delta: &str) -> String {
let mut output = String::new();
self.line_buffer.push_str(delta);
// Process all complete lines (those ending with \n)
while let Some(newline_pos) = self.line_buffer.find('\n') {
let line = self.line_buffer[..newline_pos].to_string();
self.line_buffer = self.line_buffer[newline_pos + 1..].to_string();
if self.block_prefix_rendered {
// The prefix (bullet, quote marker, etc.) was already rendered.
// Just render remaining content as inline text.
output.push_str(&self.render_inline(&line));
} else {
output.push_str(&self.render_line(&line));
}
output.push('\n');
self.line_start = true;
self.block_prefix_rendered = false;
}
// Try to resolve the line-start buffer early:
// If we have enough characters to determine it's NOT a fence, header,
// or other block-level construct (list, blockquote, hr), flush as inline text.
if self.line_start && !self.line_buffer.is_empty() && !self.in_code_block {
if !self.needs_line_buffering() {
// Definitely not a fence, header, or block element — flush as inline text
let buf = std::mem::take(&mut self.line_buffer);
output.push_str(&self.render_inline(&buf));
self.line_start = false;
} else {
// Check if we can confirm a block element and render its prefix early,
// switching to mid-line streaming for subsequent tokens.
let prefix_output = self.try_resolve_block_prefix();
if !prefix_output.is_empty() {
output.push_str(&prefix_output);
} else {
// Still ambiguous from needs_line_buffering(), but if we've
// accumulated a word boundary (text + trailing whitespace), the
// content can't be a fence/header prefix — flush it now.
// This gives word-by-word streaming for prose that starts with
// characters that trigger buffering (e.g., digits, dashes).
output.push_str(&self.flush_on_whitespace());
}
}
}
// Inside a code block at line start: early-resolve when content can't be a
// closing fence. Only ``` matters here (no headers, lists, etc.). Once we
// know it's not a fence, flush as code content and set line_start=false so
// subsequent tokens stream immediately via the mid-line fast path (issue #147).
//
// render_latency_budget: In CommonMark, a closing fence can have 0–3 spaces
// of indentation. Content with >3 leading spaces or any non-backtick first
// non-space char is guaranteed not to be a fence and resolves immediately.
if self.line_start && !self.line_buffer.is_empty() && self.in_code_block {
let leading_spaces = self.line_buffer.len() - self.line_buffer.trim_start().len();
let trimmed = self.line_buffer.trim_start();
let could_be_fence = if leading_spaces > 3 {
// >3 spaces of indentation — can't be a closing fence per CommonMark
false
} else {
trimmed.is_empty() || trimmed.starts_with('`') || "`".starts_with(trimmed)
};
if !could_be_fence {
// Definitely not a closing fence — flush as code content immediately
let buf = std::mem::take(&mut self.line_buffer);
output.push_str(&self.render_code_inline(&buf));
self.line_start = false;
}
}
output
}
/// Check if the current line_buffer content at line start still needs buffering
/// because it could be a markdown control sequence (fence, header, block element).
/// Returns false when the content is definitely plain text and can be flushed.
fn needs_line_buffering(&self) -> bool {
let trimmed = self.line_buffer.trim_start();
if trimmed.is_empty() {
return true;
}
let could_be_fence = trimmed.starts_with('`') || "`".starts_with(trimmed);
let could_be_header = trimmed.starts_with('#') || "#".starts_with(trimmed);
if could_be_fence || could_be_header {
return true;
}
// Check for block-level constructs
let first = trimmed.as_bytes()[0];
match first {
b'>' => true, // blockquote — always a block element
b'+' => trimmed.len() < 2 || trimmed.starts_with("+ "),
b'-' => {
// Quick disambiguation: "-" followed by a non-space, non-dash char
// can't be a list item ("- ") or horizontal rule ("---").
// "-based", "-flag" → flush immediately. "- item", "--" → keep buffering.
if trimmed.len() >= 2 {
let second = trimmed.as_bytes()[1];
if second != b' ' && second != b'-' {
return false;
}
}
trimmed.len() < 2 || trimmed.starts_with("- ") || {
let no_sp: String = trimmed.chars().filter(|c| *c != ' ').collect();
!no_sp.is_empty() && no_sp.chars().all(|c| c == '-')
}
}
b'*' => {
trimmed.len() < 2 || trimmed.starts_with("* ") || {
let no_sp: String = trimmed.chars().filter(|c| *c != ' ').collect();
!no_sp.is_empty() && no_sp.chars().all(|c| c == '*')
}
}
b'_' => {
trimmed.len() < 3 || {
let no_sp: String = trimmed.chars().filter(|c| *c != ' ').collect();
!no_sp.is_empty() && no_sp.chars().all(|c| c == '_')
}
}
b'0'..=b'9' => {
// Quick disambiguation: if we have at least 2 chars and the first
// non-digit char isn't '.' or ')', it can't be a numbered list —
// flush immediately. "2nd", "3rd", "100ms" → flush.
// "1.", "1)", "12" (all digits), "12." → keep buffering.
if trimmed.len() >= 2 {
if let Some(pos) = trimmed.bytes().position(|b| !b.is_ascii_digit()) {
let non_digit = trimmed.as_bytes()[pos];
if non_digit != b'.' && non_digit != b')' {
return false; // Not a numbered list pattern
}
// We have digits followed by '.' or ')'.
// Keep buffering until we see what follows the separator.
// "1." "12." "1)" → buffer (next char could be space → list)
// "1. " "12. " → buffer (confirmed list pattern, resolve in prefix)
// "1.x" "12.x" → flush (not a list — char after dot isn't space)
let after_sep = pos + 1;
if after_sep >= trimmed.len() {
return true; // Haven't seen char after separator yet
}
let next = trimmed.as_bytes()[after_sep];
if next == b' ' {
return true; // "12. " pattern — list item, keep buffering
}
return false; // "12.x" — not a list
}
// All digits so far, keep buffering
}
trimmed.len() < 3
}
b'|' => true, // table row
_ => false,
}
}
/// Try to resolve a confirmed block element prefix and render it immediately.
/// When successful, renders the prefix (bullet, quote marker, etc.) and sets
/// `line_start = false` so subsequent tokens stream via the mid-line fast path.
/// Returns any rendered output.
fn try_resolve_block_prefix(&mut self) -> String {
let trimmed = self.line_buffer.trim_start();
if trimmed.is_empty() {
return String::new();
}
let first = trimmed.as_bytes()[0];
// Blockquote: ">" or "> " confirmed — render prefix, stream rest
if first == b'>' {
let rest = trimmed.strip_prefix('>').unwrap_or("");
let rest = rest.strip_prefix(' ').unwrap_or(rest);
let prefix_output = format!("{DIM}│{RESET} {ITALIC}");
let rest_output = if !rest.is_empty() {
self.render_inline(rest)
} else {
String::new()
};
self.line_buffer.clear();
self.line_start = false;
self.block_prefix_rendered = true;
return format!("{prefix_output}{rest_output}");
}
// Unordered list: confirmed when we see "- X", "* X", "+ X"
// where X is NOT a continuation of a horizontal rule
if let Some(content) = self.try_confirm_unordered_list(trimmed) {
let indent = Self::leading_whitespace(&self.line_buffer);
let content_output = if !content.is_empty() {
self.render_inline(content)
} else {
String::new()
};
let prefix_output = format!("{indent}{CYAN}•{RESET} {content_output}");
self.line_buffer.clear();
self.line_start = false;
self.block_prefix_rendered = true;
return prefix_output;
}
// Ordered list: confirmed when we see "N. " with content
if let Some((num, content)) = self.try_confirm_ordered_list(trimmed) {
let indent = Self::leading_whitespace(&self.line_buffer);
let content_output = if !content.is_empty() {
self.render_inline(content)
} else {
String::new()
};
let prefix_output = format!("{indent}{CYAN}{num}.{RESET} {content_output}");
self.line_buffer.clear();
self.line_start = false;
self.block_prefix_rendered = true;
return prefix_output;
}
String::new()
}
/// Try to confirm an unordered list item and return the content after the marker.
/// Only confirms when we have enough content to rule out a horizontal rule.
/// For "- ", confirms when a non-dash non-space character follows.
/// For "* ", confirms when a non-star non-space character follows.
/// For "+ ", always a list item (no ambiguity with HR).
fn try_confirm_unordered_list<'a>(&self, trimmed: &'a str) -> Option<&'a str> {
// "+ X" — always a list item
if let Some(rest) = trimmed.strip_prefix("+ ") {
if !rest.is_empty() {
return Some(rest);
}
// "+ " alone: still ambiguous (could get more dashes), but "+ " is a list
return Some(rest);
}
// "- X" — list item if X contains a non-dash, non-space char
if let Some(rest) = trimmed.strip_prefix("- ") {
if !rest.is_empty() && rest.chars().any(|c| c != '-' && c != ' ') {
return Some(rest);
}
return None; // Could still be "- - -" horizontal rule
}
// "* X" — list item if X contains a non-star, non-space char
if let Some(rest) = trimmed.strip_prefix("* ") {
if !rest.is_empty() && rest.chars().any(|c| c != '*' && c != ' ') {
return Some(rest);
}
return None; // Could still be "* * *" horizontal rule
}
None
}
/// Try to confirm an ordered list item and return (number, content).
/// Confirms when we see "N. " followed by actual content.
fn try_confirm_ordered_list<'a>(&self, trimmed: &'a str) -> Option<(&'a str, &'a str)> {
let dot_space = trimmed.find(". ")?;
let num_part = &trimmed[..dot_space];
if num_part.is_empty() || !num_part.chars().all(|c| c.is_ascii_digit()) {
return None;
}
let content = &trimmed[dot_space + 2..];
if content.is_empty() {
return None; // Haven't seen content yet
}
Some((num_part, content))
}
/// Flush the line buffer when it contains a word boundary (whitespace after text).
///
/// This improves perceived streaming performance: when the buffer has accumulated
/// something like `"The "` or `"Hello world "`, the trailing whitespace proves it
/// can't be a fence/header prefix (those never have spaces after the control chars
/// without first being resolved by `try_resolve_block_prefix`). So we flush the
/// buffer as inline text and switch to the mid-line fast path.
///
/// **Safety:** Does NOT flush when the trimmed buffer starts with `#` or `` ` ``
/// (potential header/fence), or with block-level markers (`>`, `-`, `*`, `+`,
/// digits) — those are handled by `needs_line_buffering`/`try_resolve_block_prefix`.
///
/// Returns rendered output if flushed, empty string otherwise.
pub fn flush_on_whitespace(&mut self) -> String {
if !self.line_start || self.line_buffer.is_empty() || self.in_code_block {
return String::new();
}
// Check if the buffer ends with whitespace and has non-whitespace content.
let has_non_ws = self.line_buffer.chars().any(|c| !c.is_whitespace());
let ends_with_ws = self
.line_buffer
.chars()
.last()
.map(|c| c.is_whitespace())
.unwrap_or(false);
if !has_non_ws || !ends_with_ws {
return String::new();
}
// Don't flush if the content could still be a markdown control sequence.
// Headers (#), fences (`), block elements (>, -, *, +, digits) need to
// keep buffering — they're handled by the dedicated resolution paths.
let trimmed = self.line_buffer.trim_start();
if !trimmed.is_empty() {
let first = trimmed.as_bytes()[0];
match first {
b'#' | b'`' | b'>' | b'-' | b'*' | b'+' | b'_' | b'|' => return String::new(),
b'0'..=b'9' => return String::new(),
_ => {}
}
}
let buf = std::mem::take(&mut self.line_buffer);
let output = self.render_inline(&buf);
self.line_start = false;
output
}
/// Flush any remaining buffered content (call after stream ends).
pub fn flush(&mut self) -> String {
if self.line_buffer.is_empty() {
if self.block_prefix_rendered {
// Close any open italic from blockquote prefix
self.block_prefix_rendered = false;
return format!("{RESET}");
}
return String::new();
}
let line = std::mem::take(&mut self.line_buffer);
self.line_start = true;
if self.block_prefix_rendered {
self.block_prefix_rendered = false;
// Prefix already rendered — just render remaining inline content
let formatted = self.render_inline(&line);
return format!("{formatted}{RESET}");
}
self.render_line(&line)
}
/// Render a single complete line, updating state for code fences.
fn render_line(&mut self, line: &str) -> String {
let trimmed = line.trim();
// After rendering a complete line, next content will be at line start
self.line_start = true;
self.block_prefix_rendered = false;
// Check for code fence (``` with optional language)
if let Some(after_fence) = trimmed.strip_prefix("```") {
if self.in_code_block {
// Closing fence
self.in_code_block = false;
self.code_lang = None;
return format!("{DIM}{line}{RESET}");
} else {
// Opening fence — capture language if present
self.in_code_block = true;
let lang = after_fence.trim();
self.code_lang = if lang.is_empty() {
None
} else {
Some(lang.to_string())
};
return format!("{DIM}{line}{RESET}");
}
}
if self.in_code_block {
// Code block content: syntax highlight if language is known, else dim
return if let Some(ref lang) = self.code_lang {
highlight_code_line(lang, line)
} else {
format!("{DIM}{line}{RESET}")
};
}
// Header: # at line start → BOLD+CYAN
if trimmed.starts_with('#') {
return format!("{BOLD}{CYAN}{line}{RESET}");
}
// Horizontal rule: ---, ***, ___ (3+ of the same char, possibly with spaces)
if Self::is_horizontal_rule(trimmed) {
let width = 40;
return format!("{DIM}{}{RESET}", "─".repeat(width));
}
// Blockquote: > at line start
if let Some(rest) = trimmed.strip_prefix('>') {
let content = rest.strip_prefix(' ').unwrap_or(rest);
let formatted = self.render_inline(content);
return format!("{DIM}│{RESET} {ITALIC}{formatted}{RESET}");
}
// Unordered list: lines starting with - , * , or + (with optional leading whitespace)
if let Some(content) = Self::strip_unordered_list_marker(trimmed) {
let indent = Self::leading_whitespace(line);
let formatted = self.render_inline(content);
return format!("{indent}{CYAN}•{RESET} {formatted}");
}
// Ordered list: lines matching N. text
if let Some((num, content)) = Self::strip_ordered_list_marker(trimmed) {
let indent = Self::leading_whitespace(line);
let formatted = self.render_inline(content);
return format!("{indent}{CYAN}{num}.{RESET} {formatted}");
}
// Apply inline formatting for normal text
self.render_inline(line)
}
/// Check if a trimmed line is a horizontal rule (---, ***, ___, 3+ chars).
fn is_horizontal_rule(trimmed: &str) -> bool {
if trimmed.len() < 3 {
return false;
}
let no_spaces: String = trimmed.chars().filter(|c| *c != ' ').collect();
if no_spaces.len() < 3 {
return false;
}
let first = no_spaces.chars().next().unwrap();
(first == '-' || first == '*' || first == '_') && no_spaces.chars().all(|c| c == first)
}
/// Strip an unordered list marker (- , * , + ) and return the content after it.
fn strip_unordered_list_marker(trimmed: &str) -> Option<&str> {
// Must be "- text", "* text", or "+ text"
// Be careful: "---" is a horizontal rule, not a list item
// "* " alone at start needs to not conflict with bold/italic markers at line level
for marker in &["- ", "* ", "+ "] {
if let Some(rest) = trimmed.strip_prefix(marker) {
return Some(rest);
}
}
None
}
/// Strip an ordered list marker (N. ) and return (number_str, content).
fn strip_ordered_list_marker(trimmed: &str) -> Option<(&str, &str)> {
// Match pattern: one or more digits, then '. ', then content
let dot_pos = trimmed.find(". ")?;
let num_part = &trimmed[..dot_pos];
if !num_part.is_empty() && num_part.chars().all(|c| c.is_ascii_digit()) {
Some((num_part, &trimmed[dot_pos + 2..]))
} else {
None
}
}
/// Extract leading whitespace from a line.
fn leading_whitespace(line: &str) -> &str {
let trimmed_len = line.trim_start().len();
&line[..line.len() - trimmed_len]
}
/// Apply inline formatting (bold, italic, inline code) to a line of normal text.
fn render_inline(&self, line: &str) -> String {
let mut result = String::new();
let chars: Vec<char> = line.chars().collect();
let len = chars.len();
let mut i = 0;
while i < len {
// Check for bold italic: ***text***
if i + 2 < len && chars[i] == '*' && chars[i + 1] == '*' && chars[i + 2] == '*' {
if let Some(close) = Self::find_triple_star(&chars, i + 3) {
let inner: String = chars[i + 3..close].iter().collect();
result.push_str(&format!("{BOLD_ITALIC}{inner}{RESET}"));
i = close + 3;
continue;
}
}
// Check for bold: **text**
if i + 1 < len && chars[i] == '*' && chars[i + 1] == '*' {
// Find closing **
if let Some(close) = Self::find_double_star(&chars, i + 2) {
let inner: String = chars[i + 2..close].iter().collect();
result.push_str(&format!("{BOLD}{inner}{RESET}"));
i = close + 2;
continue;
}
}
// Check for italic: *text* (single star, not followed by another star)
if chars[i] == '*' && (i + 1 >= len || chars[i + 1] != '*') {
if let Some(close) = Self::find_single_star(&chars, i + 1) {
// Must have at least one char between markers
if close > i + 1 {
let inner: String = chars[i + 1..close].iter().collect();
result.push_str(&format!("{ITALIC}{inner}{RESET}"));
i = close + 1;
continue;
}
}
}
// Check for inline code: `text`
if chars[i] == '`' {
// Find closing backtick (not another opening fence)
if let Some(close) = Self::find_backtick(&chars, i + 1) {
let inner: String = chars[i + 1..close].iter().collect();
result.push_str(&format!("{CYAN}{inner}{RESET}"));
i = close + 1;
continue;
}
}
result.push(chars[i]);
i += 1;
}
result
}
/// Find closing *** starting from position `from` in char slice.
fn find_triple_star(chars: &[char], from: usize) -> Option<usize> {
let len = chars.len();
let mut j = from;
while j + 2 < len {
if chars[j] == '*' && chars[j + 1] == '*' && chars[j + 2] == '*' {
return Some(j);
}
j += 1;
}
None
}
/// Find closing ** starting from position `from` in char slice.
fn find_double_star(chars: &[char], from: usize) -> Option<usize> {
let len = chars.len();
let mut j = from;
while j + 1 < len {
if chars[j] == '*' && chars[j + 1] == '*' {
return Some(j);
}
j += 1;
}
None
}
/// Find closing single * starting from position `from` in char slice.
/// The closing * must NOT be followed by another * (to avoid matching inside **).
fn find_single_star(chars: &[char], from: usize) -> Option<usize> {
let len = chars.len();
for j in from..len {
if chars[j] == '*' {
// Make sure it's not part of a ** sequence
if j + 1 < len && chars[j + 1] == '*' {
continue;
}
// Also make sure the preceding char isn't * (closing side of **)
if j > from && chars[j - 1] == '*' {
continue;
}
return Some(j);
}
}
None
}
/// Find closing backtick starting from position `from` in char slice.
fn find_backtick(chars: &[char], from: usize) -> Option<usize> {
(from..chars.len()).find(|&j| chars[j] == '`')
}
}
impl Default for MarkdownRenderer {
fn default() -> Self {
Self::new()
}
}
// --- Waiting spinner for AI responses ---
/// Braille spinner frames used for the "thinking" animation.
#[cfg(test)]
mod tests {
use super::*;
/// Helper: render a full string through the renderer (not streamed).
fn render_full(input: &str) -> String {
let mut r = MarkdownRenderer::new();
let mut out = r.render_delta(input);
out.push_str(&r.flush());
out
}
#[test]
fn test_md_code_block_detection() {
let input = "before\n```\ncode line\n```\nafter\n";
let out = render_full(input);
// "code line" should be wrapped in DIM
assert!(out.contains(&format!("{DIM}code line{RESET}")));
// "before" and "after" should NOT be dim
assert!(out.contains("before"));
assert!(out.contains("after"));
}
#[test]
fn test_md_code_block_with_language() {
let input = "```rust\nlet x = 1;\n```\n";
let mut r = MarkdownRenderer::new();
let out = r.render_delta(input);
let flushed = r.flush();
let full = format!("{out}{flushed}");
// Language should be captured and fence dimmed
assert!(full.contains(&format!("{DIM}```rust{RESET}")));
// "let" should be keyword-highlighted, not just DIM
assert!(full.contains(&format!("{BOLD_CYAN}let{RESET}")));
// Number should be yellow
assert!(full.contains(&format!("{YELLOW}1{RESET}")));
}
#[test]
fn test_md_inline_code() {
let out = render_full("use `Option<T>` here\n");
assert!(out.contains(&format!("{CYAN}Option<T>{RESET}")));
}
#[test]
fn test_md_bold_text() {
let out = render_full("this is **important** stuff\n");
assert!(out.contains(&format!("{BOLD}important{RESET}")));
}
#[test]
fn test_md_header_rendering() {
let out = render_full("# Hello World\n");
assert!(out.contains(&format!("{BOLD}{CYAN}# Hello World{RESET}")));
}
#[test]
fn test_md_header_h2() {
let out = render_full("## Section Two\n");
assert!(out.contains(&format!("{BOLD}{CYAN}## Section Two{RESET}")));
}
#[test]
fn test_md_partial_delta_fence() {
// Fence marker split across multiple deltas
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("``");
// Nothing emitted yet — still buffered (no newline)
assert_eq!(out1, "");
let out2 = r.render_delta("`\n");
// Now the fence line is complete
assert!(out2.contains(&format!("{DIM}```{RESET}")));
let out3 = r.render_delta("code here\n");
assert!(out3.contains(&format!("{DIM}code here{RESET}")));
let out4 = r.render_delta("```\n");
assert!(out4.contains(&format!("{DIM}```{RESET}")));
// After closing, normal text again
let out5 = r.render_delta("normal\n");
assert!(out5.contains("normal"));
assert!(!out5.contains(&format!("{DIM}")));
}
#[test]
fn test_md_empty_delta() {
let mut r = MarkdownRenderer::new();
let out = r.render_delta("");
assert_eq!(out, "");
let flushed = r.flush();
assert_eq!(flushed, "");
}
#[test]
fn test_md_multiple_code_blocks() {
let input = "text\n```\nblock1\n```\nmiddle\n```python\nblock2\n```\nend\n";
let out = render_full(input);
// Untagged code block → DIM fallback
assert!(out.contains(&format!("{DIM}block1{RESET}")));
assert!(out.contains("middle"));
// Python-tagged code block → syntax highlighted (no keyword match, plain output)
assert!(out.contains("block2"));
assert!(out.contains("end"));
}
#[test]
fn test_md_inline_code_inside_bold() {
// Inline code backticks inside bold — bold wraps, code is separate
let out = render_full("**bold** and `code`\n");
assert!(out.contains(&format!("{BOLD}bold{RESET}")));
assert!(out.contains(&format!("{CYAN}code{RESET}")));
}
#[test]
fn test_md_unmatched_backtick() {
// Single backtick without closing — should pass through literally
let out = render_full("it's a `partial\n");
assert!(out.contains('`'));
assert!(out.contains("partial"));
}
#[test]
fn test_md_unmatched_bold() {
// Unmatched ** should pass through literally
let out = render_full("star **power\n");
assert!(out.contains("**"));
assert!(out.contains("power"));
}
#[test]
fn test_md_flush_partial_line() {
let mut r = MarkdownRenderer::new();
// "no" at line start — can't be fence/header, resolves immediately
let out = r.render_delta("no");
assert!(
out.contains("no"),
"Short non-fence/non-header text resolves immediately"
);
// Continue adding text — mid-line now, immediate output
let out2 = r.render_delta(" newline here");
assert!(out2.contains(" newline here"));
}
#[test]
fn test_md_flush_with_inline_formatting() {
let mut r = MarkdownRenderer::new();
// "hello **world**" — resolves as non-fence at line start, then renders inline
let out = r.render_delta("hello **world**");
let flushed = r.flush();
let total = format!("{out}{flushed}");
assert!(total.contains(&format!("{BOLD}world{RESET}")));
}
#[test]
fn test_md_default_trait() {
let r = MarkdownRenderer::default();
assert!(!r.in_code_block);
assert!(r.code_lang.is_none());
assert!(r.line_buffer.is_empty());
assert!(r.line_start);
assert!(!r.block_prefix_rendered);
}
// --- Streaming output tests (mid-line tokens should render immediately) ---
#[test]
fn test_md_streaming_mid_line_immediate_output() {
// Simulate LLM streaming: first token starts a line, subsequent tokens mid-line
let mut r = MarkdownRenderer::new();
// First token: "Hello " — at line start, long enough to resolve as normal text
let out1 = r.render_delta("Hello ");
// Should produce output (6 chars, clearly not a fence or header)
assert!(
out1.contains("Hello "),
"Expected immediate output for non-fence/non-header text, got: '{out1}'"
);
// Second token: "world" — mid-line, should be immediate
let out2 = r.render_delta("world");
assert!(
out2.contains("world"),
"Mid-line delta should produce immediate output, got: '{out2}'"
);
// Third token: " how" — still mid-line
let out3 = r.render_delta(" how");
assert!(
out3.contains(" how"),
"Mid-line delta should produce immediate output, got: '{out3}'"
);
}
#[test]
fn test_md_streaming_newline_resets_to_line_start() {
let mut r = MarkdownRenderer::new();
// Start with text that resolves line start
let _ = r.render_delta("Hello world");
// Now a newline — next delta should be at line start again
let _ = r.render_delta("\n");
// Short text at start of new line — should buffer briefly
let out = r.render_delta("``");
// Two backticks could be start of a fence — should buffer
assert_eq!(
out, "",
"Short ambiguous text at line start should be buffered"
);
}
#[test]
fn test_md_streaming_code_fence_detected_at_line_start() {
let mut r = MarkdownRenderer::new();
// Send a code fence at line start
let out1 = r.render_delta("```\n");
assert!(out1.contains(&format!("{DIM}```{RESET}")));
assert!(r.in_code_block);
// Content inside code block
let out2 = r.render_delta("some code\n");
assert!(out2.contains(&format!("{DIM}some code{RESET}")));
// Closing fence
let out3 = r.render_delta("```\n");
assert!(out3.contains(&format!("{DIM}```{RESET}")));
assert!(!r.in_code_block);
}
#[test]
fn test_md_streaming_header_detected_at_line_start() {
let mut r = MarkdownRenderer::new();
// Header at line start
let out = r.render_delta("# My Header\n");
assert!(out.contains(&format!("{BOLD}{CYAN}# My Header{RESET}")));
}
#[test]
fn test_md_streaming_bold_mid_line() {
let mut r = MarkdownRenderer::new();
// Start a line with enough text to resolve
let out1 = r.render_delta("This is ");
assert!(out1.contains("This is "));
// Now bold text mid-line
let out2 = r.render_delta("**important**");
assert!(
out2.contains(&format!("{BOLD}important{RESET}")),
"Bold formatting should work in mid-line streaming, got: '{out2}'"
);
}
#[test]
fn test_md_streaming_inline_code_mid_line() {
let mut r = MarkdownRenderer::new();
// Start a line
let out1 = r.render_delta("Use the ");
assert!(out1.contains("Use the "));
// Inline code mid-line
let out2 = r.render_delta("`Option`");
assert!(
out2.contains(&format!("{CYAN}Option{RESET}")),
"Inline code should work in mid-line streaming, got: '{out2}'"
);
}
#[test]
fn test_md_streaming_word_by_word_paragraph() {
// Simulate typical LLM streaming: word by word
let mut r = MarkdownRenderer::new();
let words = ["The ", "quick ", "brown ", "fox ", "jumps"];
let mut got_output = false;
for word in &words[..] {
let out = r.render_delta(word);
if !out.is_empty() {
got_output = true;
}
}
// We should have gotten SOME output before the line ends
assert!(
got_output,
"Word-by-word streaming should produce output before newline"
);
// Flush remainder
let _flushed = r.flush();
// Total output should contain all words
let mut total = String::new();
let mut r2 = MarkdownRenderer::new();
for word in &words[..] {
total.push_str(&r2.render_delta(word));
}
total.push_str(&r2.flush());
assert!(total.contains("The "));
assert!(total.contains("fox "));
}
#[test]
fn test_md_streaming_line_start_buffer_short_text() {
// At line start, very short text (1-3 chars) that could be start of fence/header
// should be buffered
let mut r = MarkdownRenderer::new();
let out = r.render_delta("#");
// Single '#' could be a header — should buffer
assert_eq!(out, "", "Single '#' at line start should be buffered");
// Now add more to reveal it's a header
let out2 = r.render_delta(" Title\n");
assert!(out2.contains(&format!("{BOLD}{CYAN}# Title{RESET}")));
}
#[test]
fn test_md_streaming_line_start_resolves_normal() {
// At line start, text that quickly resolves as not a fence/header
let mut r = MarkdownRenderer::new();
let out = r.render_delta("Normal text");
// "Normal" is 11 chars, clearly not a fence or header — should output
assert!(
out.contains("Normal text"),
"Non-fence/non-header text should be output once resolved, got: '{out}'"
);
}
#[test]
fn test_md_streaming_existing_tests_still_pass() {
// Ensure the full-line render_full helper still works exactly as before
let out = render_full("Hello **world** and `code`\n");
assert!(out.contains("Hello "));
assert!(out.contains(&format!("{BOLD}world{RESET}")));
assert!(out.contains(&format!("{CYAN}code{RESET}")));
}
#[test]
fn test_md_streaming_in_code_block_immediate() {
// Inside a code block, tokens should stream immediately once fence is ruled out.
// "let x" can't be a closing fence (doesn't start with `), so it should
// be early-resolved and emitted without needing flush().
let mut r = MarkdownRenderer::new();
let _ = r.render_delta("```rust\n");
assert!(r.in_code_block);
// Send code token — not a fence, should be emitted immediately
let out = r.render_delta("let x");
assert!(
!out.is_empty(),
"Code block content that can't be a fence should emit immediately, got empty"
);
assert!(
out.contains("let"),
"Code block content should contain the text, got: '{out}'"
);
}
#[test]
fn test_md_code_block_mid_line_emitted_immediately() {
// Issue #147: Mid-line code block content should be emitted token-by-token,
// not buffered until a newline arrives.
let mut r = MarkdownRenderer::new();
// Open a code block
let _ = r.render_delta("```\n");
assert!(r.in_code_block);
// Send a line start token that gets buffered (could be closing fence)
// Then a complete line to move past line_start
let _ = r.render_delta("let x = 1;\n");
// Now send a mid-line token — should be emitted immediately, not empty
let out = r.render_delta("println");
assert!(
!out.is_empty(),
"Mid-line code block token should be emitted immediately, got empty string"
);
assert!(
out.contains("println"),
"Mid-line code block token should contain the text, got: '{out}'"
);
}
#[test]
fn test_md_code_block_mid_line_with_newline() {
// When a newline arrives mid-line in a code block, it should transition to line_start
let mut r = MarkdownRenderer::new();
let _ = r.render_delta("```\n");
let _ = r.render_delta("first line\n");
// Send mid-line token followed by newline
let out = r.render_delta("hello\n");
assert!(
out.contains("hello"),
"Code block content before newline should be rendered, got: '{out}'"
);
// After the newline, we should be at line_start again
assert!(
r.line_start,
"After newline in code block, should be at line_start"
);
}
#[test]
fn test_md_code_block_fence_detection_still_works() {
// Closing fence detection must still work even with the mid-line fast path
let mut r = MarkdownRenderer::new();
let _ = r.render_delta("```rust\n");
assert!(r.in_code_block);
let _ = r.render_delta("let x = 42;\n");
assert!(r.in_code_block);
// Closing fence at line start — must be detected (not short-circuited)
let _ = r.render_delta("```\n");
assert!(
!r.in_code_block,
"Closing fence should still be detected and end the code block"
);
}
#[test]
fn test_md_code_block_mid_line_multiple_tokens() {
// Multiple mid-line tokens in a code block should each produce output
let mut r = MarkdownRenderer::new();
let _ = r.render_delta("```\n");
let _ = r.render_delta("start\n");
let out1 = r.render_delta("foo");
assert!(
!out1.is_empty(),
"First mid-line token should emit, got empty"
);
let out2 = r.render_delta("bar");
assert!(
!out2.is_empty(),
"Second mid-line token should emit, got empty"
);
let out3 = r.render_delta(" baz");
assert!(
!out3.is_empty(),
"Third mid-line token should emit, got empty"
);
}
#[test]
fn test_md_streaming_single_token_produces_output() {
// Issue #137: Common single-token inputs should produce non-empty output
// when used mid-line. At line start, short tokens that can't be fences/headers
// should also flush immediately.
let test_cases = vec![
// (token, description)
("Hello", "common greeting"),
("I", "single letter word"),
(" will", "space-prefixed verb"),
("The", "article"),
("Sure", "affirmative"),
("Let", "common start word"),
("Yes", "short response"),
("To", "preposition"),
];
for (token, desc) in &test_cases {
// Test mid-line: should always produce output immediately
let mut r = MarkdownRenderer::new();
// First, get past line-start by sending a resolved line-start token
let _ = r.render_delta("Start ");
let out = r.render_delta(token);
assert!(
!out.is_empty(),
"Mid-line token '{token}' ({desc}) should produce non-empty output, got empty"
);
}
// Test at line start: tokens that can't be fences (``) or headers (#)
// should flush immediately even if short
let line_start_cases = vec![
("Hello", "common greeting"),
("I", "single letter I"),
("Sure", "affirmative"),
("The", "article"),
("Yes", "short response"),
];
for (token, desc) in &line_start_cases {
let mut r = MarkdownRenderer::new();
let out = r.render_delta(token);
assert!(
!out.is_empty(),
"Line-start token '{token}' ({desc}) that can't be fence/header should produce output, got empty"
);
}
}
#[test]
fn test_md_streaming_single_char_non_special_at_line_start() {
// Single characters that are NOT '#' or '`' should flush immediately
// at line start, since they can't possibly be fences or headers
let mut r = MarkdownRenderer::new();
let out = r.render_delta("I");
assert!(
!out.is_empty(),
"'I' at line start cannot be fence or header, should flush immediately"
);
}
#[test]
fn test_md_streaming_space_prefixed_token_at_line_start() {
// " will" — space-prefixed, trimmed = "will" (4 chars), not fence/header
let mut r = MarkdownRenderer::new();
let out = r.render_delta(" will");
assert!(
!out.is_empty(),
"' will' at line start should resolve — trimmed 'will' is 4 chars, not fence/header"
);
}
// --- Streaming latency: block elements should flush content after prefix ---
#[test]
fn test_md_streaming_list_item_content_not_buffered() {
// List items should NOT buffer all content until newline.
// Once we see "- " we know it's a list item — subsequent tokens
// should stream immediately.
let mut r = MarkdownRenderer::new();
// Send list marker
let out1 = r.render_delta("- ");
// The marker itself may or may not produce output yet (prefix detection)
// but let's accumulate
let mut total = out1;
// Send content token — should produce output immediately
let out2 = r.render_delta("Hello");
total.push_str(&out2);
assert!(
!out2.is_empty(),
"List item content after '- ' should stream immediately, got empty"
);
// Another content token
let out3 = r.render_delta(" world");
total.push_str(&out3);
assert!(
!out3.is_empty(),
"Additional list item tokens should stream immediately, got empty"
);
}
#[test]
fn test_md_streaming_blockquote_content_not_buffered() {
// Blockquote content after "> " should stream immediately.
let mut r = MarkdownRenderer::new();
let _out1 = r.render_delta("> ");
let out2 = r.render_delta("Some quoted");
assert!(
!out2.is_empty(),
"Blockquote content after '> ' should stream immediately, got empty"
);
let out3 = r.render_delta(" text");
assert!(
!out3.is_empty(),
"Additional blockquote tokens should stream immediately, got empty"
);
}
#[test]
fn test_md_streaming_header_content_still_buffers() {
// Headers need to buffer until newline because the entire line
// gets BOLD+CYAN styling. But "#" alone should buffer.
let mut r = MarkdownRenderer::new();
let out = r.render_delta("#");
assert_eq!(out, "", "Single '#' should buffer (could be header)");
}
#[test]
fn test_md_streaming_code_fence_opener_still_buffers() {
// Code fence openers must buffer until complete so we detect the fence.
let mut r = MarkdownRenderer::new();
let out = r.render_delta("``");
assert_eq!(out, "", "Partial fence '``' should buffer");
let out2 = r.render_delta("`");
// Still buffering (no newline yet, could be ```lang)
// The fence might be detected only on \n
assert_eq!(
out2, "",
"Complete fence '```' without newline should buffer"
);
}
#[test]
fn test_md_streaming_inline_formatting_on_partial_lines() {
// Bold/italic/code formatting should work on partial lines (flushed mid-line)
let mut r = MarkdownRenderer::new();
// Start with resolved text
let _ = r.render_delta("Check ");
// Send bold text mid-line
let out = r.render_delta("**this**");
assert!(
out.contains(&format!("{BOLD}this{RESET}")),
"Bold formatting should work on mid-line partial text, got: '{out}'"
);
}
#[test]
fn test_md_streaming_list_renders_correctly_on_newline() {
// Even with early flushing, the full list item should render correctly
// when the newline arrives.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("- ");
let out2 = r.render_delta("item text");
let out3 = r.render_delta("\n");
let flushed = r.flush();
let total = format!("{out1}{out2}{out3}{flushed}");
// Should contain the bullet character from list rendering
assert!(
total.contains("item text"),
"List item text should appear in output, got: '{total}'"
);
}
#[test]
fn test_md_streaming_ordered_list_content_not_buffered() {
// Ordered list: "1. " detected, subsequent content should stream
let mut r = MarkdownRenderer::new();
let _out1 = r.render_delta("1. ");
let out2 = r.render_delta("First item");
assert!(
!out2.is_empty(),
"Ordered list content after '1. ' should stream immediately, got empty"
);
}
#[test]
fn test_md_streaming_no_regression_full_render() {
// Full render should still produce correct output for all line types
let out = render_full("- list item\n> quoted\n1. ordered\n# header\nplain\n");
assert!(
out.contains("list item"),
"List item missing from full render"
);
assert!(
out.contains("quoted"),
"Blockquote missing from full render"
);
assert!(
out.contains("ordered"),
"Ordered list missing from full render"
);
assert!(out.contains("header"), "Header missing from full render");
assert!(out.contains("plain"), "Plain text missing from full render");
}
// --- flush_on_whitespace tests ---
#[test]
fn test_md_flush_on_whitespace_at_line_start() {
// When buffer accumulates "word " at line start, the trailing space
// proves it's not a fence/header — flush_on_whitespace should emit it.
let mut r = MarkdownRenderer::new();
// Simulate a token that ends with whitespace at line start
// "1 " could look like the start of an ordered list ("1. "), but
// the space without a dot means it's just text with a trailing space.
// However, needs_line_buffering might still hold it. Let's use a
// clearer case: a digit followed by space that needs_line_buffering holds.
let out = r.flush_on_whitespace();
assert_eq!(out, "", "Empty buffer should not flush");
}
#[test]
fn test_md_flush_on_whitespace_with_word_boundary() {
// Direct test of flush_on_whitespace with a buffer that has
// non-special content ending in whitespace.
let mut r = MarkdownRenderer::new();
r.line_buffer = "Hello ".to_string();
r.line_start = true;
let out = r.flush_on_whitespace();
assert!(
out.contains("Hello"),
"Buffer with word boundary should flush, got: '{out}'"
);
assert!(!r.line_start, "Should switch to mid-line after flush");
assert!(
r.line_buffer.is_empty(),
"Buffer should be empty after flush"
);
}
#[test]
fn test_md_flush_on_whitespace_no_trailing_space() {
let mut r = MarkdownRenderer::new();
r.line_buffer = "Hello".to_string();
r.line_start = true;
let out = r.flush_on_whitespace();
assert_eq!(
out, "",
"Buffer without trailing whitespace should not flush"
);
}
#[test]
fn test_md_flush_on_whitespace_only_whitespace() {
let mut r = MarkdownRenderer::new();
r.line_buffer = " ".to_string();
r.line_start = true;
let out = r.flush_on_whitespace();
assert_eq!(out, "", "Buffer with only whitespace should not flush");
}
#[test]
fn test_md_flush_on_whitespace_not_at_line_start() {
let mut r = MarkdownRenderer::new();
r.line_buffer = "Hello ".to_string();
r.line_start = false; // mid-line
let out = r.flush_on_whitespace();
assert_eq!(out, "", "Should not flush when not at line start");
}
#[test]
fn test_md_flush_on_whitespace_in_code_block() {
let mut r = MarkdownRenderer::new();
r.line_buffer = "Hello ".to_string();
r.line_start = true;
r.in_code_block = true;
let out = r.flush_on_whitespace();
assert_eq!(out, "", "Should not flush inside code blocks");
}
#[test]
fn test_md_streaming_whitespace_flush_integration() {
// Full streaming simulation: tokens that arrive with trailing whitespace
// at line start should flush via the whitespace path when the normal
// needs_line_buffering check would hold them.
let mut r = MarkdownRenderer::new();
// "- " at line start triggers needs_line_buffering (could be list).
// Then "not " arrives. The buffer is now "- not " which has a word
// boundary. But try_resolve_block_prefix should handle "- not" as a
// confirmed list item before flush_on_whitespace even fires.
let out1 = r.render_delta("- ");
let out2 = r.render_delta("not");
let total = format!("{out1}{out2}");
// Should have output — either from prefix resolution or whitespace flush
assert!(
total.contains("not") || !out2.is_empty(),
"Content after list marker should stream, got out1='{out1}' out2='{out2}'"
);
}
#[test]
fn test_md_streaming_digit_with_space_stays_buffered() {
// "3 " — starts with digit, needs_line_buffering holds it (could be "3. ").
// flush_on_whitespace also guards against digits. So it stays buffered
// until the content resolves. But adding more text ("items") makes
// needs_line_buffering return false (contains ". " is false, len >= 3,
// and it's not all digits followed by ". ").
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("3 ");
// "3 " — buffered (digit start, flush_on_whitespace guards digits)
// Actually, needs_line_buffering: trimmed="3 ", first byte is digit,
// trimmed.len() >= 3? "3 " is 2 chars, so < 3, returns true (buffer).
// Then try_resolve_block_prefix: digit, tries ordered list, no ". " found. Empty.
// Then flush_on_whitespace: first byte is digit, guarded. Empty.
// So out1 should be empty.
let out2 = r.render_delta("items");
// Buffer is now "3 items". needs_line_buffering: digit start, len >= 3,
// contains ". "? No. So all(digit) on "3 items"[..?] — find(". ") returns None.
// The match arm: trimmed.len() < 3 → false. trimmed.contains(". ") is false.
// So the whole expression: false || false = false. needs_line_buffering returns false!
// So it flushes as inline text.
let total = format!("{out1}{out2}");
assert!(
total.contains("3") && total.contains("items"),
"Digit-space-text should eventually produce output, got: '{total}'"
);
}
#[test]
fn test_md_flush_on_whitespace_each_token_produces_output() {
// Simulate word-by-word streaming where each word ends with a space.
// After the first word resolves the line start, subsequent words
// should produce immediate output via the mid-line fast path.
let mut r = MarkdownRenderer::new();
let words = ["The ", "quick ", "brown ", "fox "];
let mut outputs = Vec::new();
for word in &words {
outputs.push(r.render_delta(word));
}
// First word should produce output (resolves line start)
assert!(
!outputs[0].is_empty(),
"First word 'The ' should flush immediately (not fence/header)"
);
// All subsequent words are mid-line, should produce output
for (i, out) in outputs.iter().enumerate().skip(1) {
assert!(
!out.is_empty(),
"Word {} should produce mid-line output, got empty",
i
);
}
}
#[test]
fn test_md_flush_on_whitespace_preserves_fence_detection() {
// Ensure whitespace flush doesn't break fence detection.
// "``` " could theoretically end with whitespace but should NOT flush
// as inline text — it needs to be detected as a fence.
let mut r = MarkdownRenderer::new();
let out = r.render_delta("```");
assert_eq!(out, "", "Fence should buffer, not flush on whitespace");
// Even with trailing space, the needs_line_buffering check fires first
let out2 = r.render_delta(" ");
// ``` + space = "``` " in buffer — needs_line_buffering still true (starts with `)
// flush_on_whitespace shouldn't fire because needs_line_buffering resolved first
assert_eq!(
out2, "",
"Fence with trailing space should still buffer for language detection"
);
}
#[test]
fn test_md_flush_on_whitespace_preserves_header_detection() {
// "# " should not be flushed by whitespace — it's a header marker.
// flush_on_whitespace guards against first-char '#'.
let mut r = MarkdownRenderer::new();
let out = r.render_delta("# ");
// The '#' triggers needs_line_buffering, try_resolve_block_prefix
// doesn't handle headers, and flush_on_whitespace skips '#' content.
// So "# " stays buffered.
assert_eq!(
out, "",
"'# ' should remain buffered waiting for full header line"
);
// Complete the header line — should render with header styling
let out2 = r.render_delta("Title\n");
assert!(
out2.contains("Title"),
"Header should render when line completes, got: '{out2}'"
);
}
#[test]
fn test_md_plain_text_unchanged() {
let out = render_full("just plain text\n");
assert!(out.contains("just plain text"));
}
#[test]
fn test_md_multiple_inline_codes_one_line() {
let out = render_full("use `foo` and `bar` here\n");
assert!(out.contains(&format!("{CYAN}foo{RESET}")));
assert!(out.contains(&format!("{CYAN}bar{RESET}")));
}
#[test]
fn test_md_code_block_preserves_content() {
let input = "```\nfn main() {\n println!(\"hello\");\n}\n```\n";
let out = render_full(input);
assert!(out.contains("fn main()"));
assert!(out.contains("println!"));
}
// --- Markdown rendering: italic, lists, horizontal rules, blockquotes ---
#[test]
fn test_md_italic_text() {
let out = render_full("this is *italic* text\n");
assert!(
out.contains(&format!("{ITALIC}italic{RESET}")),
"Expected italic ANSI for *italic*, got: '{out}'"
);
}
#[test]
fn test_md_bold_still_works() {
// Regression: bold must not break after adding italic support
let out = render_full("this is **bold** text\n");
assert!(
out.contains(&format!("{BOLD}bold{RESET}")),
"Expected bold ANSI for **bold**, got: '{out}'"
);
}
#[test]
fn test_md_bold_italic_text() {
let out = render_full("this is ***both*** here\n");
assert!(
out.contains(&format!("{BOLD_ITALIC}both{RESET}")),
"Expected bold+italic ANSI for ***both***, got: '{out}'"
);
}
#[test]
fn test_md_mixed_inline_formatting() {
let out = render_full("**bold** and *italic* and `code`\n");
assert!(
out.contains(&format!("{BOLD}bold{RESET}")),
"Missing bold in mixed line, got: '{out}'"
);
assert!(
out.contains(&format!("{ITALIC}italic{RESET}")),
"Missing italic in mixed line, got: '{out}'"
);
assert!(
out.contains(&format!("{CYAN}code{RESET}")),
"Missing code in mixed line, got: '{out}'"
);
}
#[test]
fn test_md_unclosed_italic_no_format() {
// A single * at end of line without closing should NOT italicize
let out = render_full("star *power\n");
assert!(
out.contains('*'),
"Unclosed italic marker should pass through literally, got: '{out}'"
);
assert!(out.contains("power"));
}
#[test]
fn test_md_unordered_list_dash() {
let out = render_full("- first item\n");
assert!(
out.contains(&format!("{CYAN}•{RESET}")),
"Expected colored bullet for '- item', got: '{out}'"
);
assert!(out.contains("first item"));
}
#[test]
fn test_md_unordered_list_star() {
let out = render_full("* second item\n");
assert!(
out.contains(&format!("{CYAN}•{RESET}")),
"Expected colored bullet for '* item', got: '{out}'"
);
assert!(out.contains("second item"));
}
#[test]
fn test_md_unordered_list_plus() {
let out = render_full("+ third item\n");
assert!(
out.contains(&format!("{CYAN}•{RESET}")),
"Expected colored bullet for '+ item', got: '{out}'"
);
assert!(out.contains("third item"));
}
#[test]
fn test_md_unordered_list_with_inline_formatting() {
let out = render_full("- a **bold** list item\n");
assert!(out.contains(&format!("{CYAN}•{RESET}")));
assert!(
out.contains(&format!("{BOLD}bold{RESET}")),
"List item content should get inline formatting, got: '{out}'"
);
}
#[test]
fn test_md_ordered_list() {
let out = render_full("1. first\n");
assert!(
out.contains(&format!("{CYAN}1.{RESET}")),
"Expected colored number for '1. first', got: '{out}'"
);
assert!(out.contains("first"));
}
#[test]
fn test_md_ordered_list_larger_number() {
let out = render_full("42. the answer\n");
assert!(
out.contains(&format!("{CYAN}42.{RESET}")),
"Expected colored number for '42. item', got: '{out}'"
);
assert!(out.contains("the answer"));
}
#[test]
fn test_md_horizontal_rule_dashes() {
let out = render_full("---\n");
assert!(
out.contains("─"),
"Expected horizontal rule rendering for '---', got: '{out}'"
);
assert!(
out.contains(&format!("{DIM}")),
"Horizontal rule should be dim, got: '{out}'"
);
}
#[test]
fn test_md_horizontal_rule_stars() {
let out = render_full("***\n");
assert!(
out.contains("─"),
"Expected horizontal rule rendering for '***', got: '{out}'"
);
}
#[test]
fn test_md_horizontal_rule_underscores() {
let out = render_full("___\n");
assert!(
out.contains("─"),
"Expected horizontal rule rendering for '___', got: '{out}'"
);
}
#[test]
fn test_md_horizontal_rule_long() {
let out = render_full("----------\n");
assert!(
out.contains("─"),
"Expected horizontal rule for long dashes, got: '{out}'"
);
}
#[test]
fn test_md_blockquote() {
let out = render_full("> quoted text\n");
assert!(
out.contains(&format!("{DIM}│{RESET}")),
"Expected dim vertical bar for blockquote, got: '{out}'"
);
assert!(
out.contains(&format!("{ITALIC}quoted text{RESET}")),
"Blockquote content should be italic, got: '{out}'"
);
}
#[test]
fn test_md_blockquote_with_inline_formatting() {
let out = render_full("> a **bold** quote\n");
assert!(out.contains(&format!("{DIM}│{RESET}")));
// The content goes through render_inline, which processes bold inside the italic context
assert!(out.contains("bold"));
}
#[test]
fn test_md_indented_list_item() {
let out = render_full(" - nested item\n");
assert!(
out.contains(&format!("{CYAN}•{RESET}")),
"Indented list item should still get bullet, got: '{out}'"
);
assert!(out.contains("nested item"));
}
#[test]
fn test_md_not_a_list_in_code_block() {
// Inside code blocks, list markers should NOT be rendered as bullets
let out = render_full("```\n- not a list\n```\n");
assert!(
!out.contains(&format!("{CYAN}•{RESET}")),
"List markers inside code blocks should not get bullets, got: '{out}'"
);
}
// --- Syntax highlighting tests ---
#[test]
fn test_md_code_block_indented_line_resolves_immediately() {
// Indented code lines like " let x = 1;" should resolve at line start
// without waiting for more tokens — a closing fence never has leading spaces
// before the backticks (in CommonMark, ≤3 spaces are allowed, but the first
// non-space char must be `\``). Content starting with spaces followed by a
// non-backtick char should early-resolve.
let mut r = MarkdownRenderer::new();
let _ = r.render_delta("```rust\n");
assert!(r.in_code_block);
// Indented code at line start — should resolve immediately
let out = r.render_delta(" let x");
assert!(
!out.is_empty(),
"Indented code block content should resolve immediately at line start, got empty"
);
assert!(
out.contains("let x"),
"Should contain the code text, got: '{out}'"
);
}
#[test]
fn test_md_code_block_space_only_token_buffers() {
// A token that is only whitespace at code block line start should buffer
// because we don't yet know what follows
let mut r = MarkdownRenderer::new();
let _ = r.render_delta("```\n");
assert!(r.in_code_block);
// Just spaces — ambiguous, should buffer
let out = r.render_delta(" ");
// This may or may not emit — it's okay either way as long as
// subsequent non-fence content resolves quickly
let _ = out; // don't assert on whitespace-only
// Follow-up with non-fence content should resolve
let out2 = r.render_delta("code");
assert!(
!out2.is_empty(),
"Content after whitespace should resolve, got empty"
);
}
#[test]
fn test_md_render_delta_every_call_produces_or_buffers_minimally() {
// Simulate a realistic streaming sequence and verify tokens aren't
// held longer than necessary. Each non-ambiguous mid-line token should
// produce output on the same call.
let mut r = MarkdownRenderer::new();
// First token resolves line start
let out1 = r.render_delta("Here is ");
assert!(!out1.is_empty(), "First token should resolve");
// Each subsequent mid-line token must produce output immediately
let tokens = ["a ", "sentence ", "with ", "multiple ", "tokens."];
for token in &tokens {
let out = r.render_delta(token);
assert!(
!out.is_empty(),
"Mid-line token '{token}' should produce immediate output"
);
}
}
#[test]
fn test_md_flush_produces_output_for_buffered_content() {
// flush() should emit any content still in the line buffer
let mut r = MarkdownRenderer::new();
// Send a partial line that gets buffered at line start
let out = r.render_delta("#");
assert_eq!(out, "", "# should buffer at line start");
// flush() should emit the buffered content
let flushed = r.flush();
assert!(
!flushed.is_empty(),
"flush() should emit buffered '#' content"
);
}
#[test]
fn test_md_code_block_backtick_start_buffers_correctly() {
// A token starting with ` at code block line start must buffer
// (could be closing fence ```)
let mut r = MarkdownRenderer::new();
let _ = r.render_delta("```\n");
let _ = r.render_delta("content\n");
// Backtick at line start — could be closing fence
let out = r.render_delta("`");
assert_eq!(
out, "",
"Single backtick at code block line start should buffer"
);
// Complete the closing fence
let out2 = r.render_delta("``\n");
assert!(!r.in_code_block, "Should have closed the code block");
assert!(!out2.is_empty(), "Closing fence should produce output");
}
// --- render_latency_budget: document the expected flush behavior ---
//
// The streaming pipeline has the following latency budget per text delta:
//
// 1. Spinner stop (first token only): ~0.1ms
// - Synchronous eprint!("\r\x1b[K") + stderr flush
// - Sends cancel signal to async spinner task
// - Aborts the spawned task handle
//
// 2. MarkdownRenderer::render_delta(): ~0 allocation for mid-line tokens
// - Mid-line fast path: no buffering, immediate String return
// - Line-start: buffers 1-4 chars for fence/header detection
// - Code block line-start: buffers until first non-backtick char
//
// 3. print!() + io::stdout().flush(): system call, ~0.01ms
// - Called after every render_delta that produces output
// - Ensures tokens are visible immediately, not batched by stdio
//
// Total per-token latency: <0.2ms for first token, <0.05ms for subsequent
// The bottleneck is always the network/API, not the renderer.
// --- Digit-word and dash-word early flush tests (issue #147) ---
#[test]
fn test_streaming_digit_nonlist_flushes_early() {
// "2n" at line start — digit followed by a letter can't be a numbered list.
// Should flush on the 2nd char since 'n' isn't '.' or ')'.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("2n");
// "2n" should flush immediately — not a numbered list pattern
assert!(
!out1.is_empty(),
"Digit followed by letter should flush immediately, got empty"
);
// Subsequent token is mid-line, should be immediate
let out2 = r.render_delta("d");
assert!(
!out2.is_empty(),
"Mid-line token after digit-word flush should be immediate, got empty"
);
}
#[test]
fn test_streaming_dash_nonlist_flushes_early() {
// "-b" at line start — dash followed by a non-space, non-dash char
// can't be a list item or horizontal rule. Should flush immediately.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("-b");
assert!(
!out1.is_empty(),
"Dash followed by letter should flush immediately, got empty"
);
// Subsequent token is mid-line
let out2 = r.render_delta("ased");
assert!(
!out2.is_empty(),
"Mid-line token after dash-word flush should be immediate, got empty"
);
}
#[test]
fn test_streaming_numbered_list_still_buffers() {
// "1." at line start — could be a numbered list, must keep buffering.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("1.");
// "1." — digit followed by '.', still ambiguous (could be "1. item")
assert!(
out1.is_empty(),
"Digit-dot should still buffer (potential numbered list), got: '{out1}'"
);
// "1. " confirms it's a list — should resolve via try_resolve_block_prefix
let out2 = r.render_delta(" item");
assert!(
!out2.is_empty(),
"Numbered list '1. item' should eventually produce output, got empty"
);
}
#[test]
fn test_streaming_dash_list_still_buffers() {
// "- " at line start is a list item — should buffer correctly.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("- ");
// "- " is a confirmed unordered list item
// try_resolve_block_prefix should handle it
// Whether it's empty or not depends on whether prefix resolves at "- "
// The key: subsequent content should stream
let out2 = r.render_delta("item");
let total = format!("{out1}{out2}");
assert!(
total.contains("item"),
"Dash list '- item' should produce output, got: '{total}'"
);
}
#[test]
fn test_streaming_dash_hr_still_buffers() {
// "---" should still buffer as a potential horizontal rule.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("-");
assert!(
out1.is_empty(),
"Single dash should buffer (ambiguous), got: '{out1}'"
);
let out2 = r.render_delta("-");
assert!(
out2.is_empty(),
"Double dash should buffer (potential HR), got: '{out2}'"
);
let out3 = r.render_delta("-");
// "---" is a horizontal rule, should still be buffered/handled correctly
assert!(
out3.is_empty(),
"Triple dash should still buffer as HR, got: '{out3}'"
);
}
#[test]
fn test_streaming_mid_line_always_immediate() {
// Once line_start is false, ALL tokens should be immediate regardless of content.
let mut r = MarkdownRenderer::new();
let _ = r.render_delta("Hello ");
assert!(!r.line_start, "Should be mid-line after 'Hello '");
// Tokens that would trigger buffering at line start should be immediate mid-line
for token in &["-", "1.", "```", "#", ">", "---"] {
let out = r.render_delta(token);
assert!(
!out.is_empty(),
"Mid-line token '{token}' should produce immediate output, got empty"
);
}
}
#[test]
fn test_streaming_fence_still_buffers() {
// "```" at line start should still buffer as a code fence.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("`");
assert!(
out1.is_empty(),
"Single backtick should buffer, got: '{out1}'"
);
let out2 = r.render_delta("``");
// Now buffer is "```" — still buffering as potential fence
assert!(
out2.is_empty(),
"Triple backtick without newline should still buffer, got: '{out2}'"
);
// A newline confirms the fence
let out3 = r.render_delta("\n");
assert!(
r.in_code_block,
"Code fence should be detected after newline"
);
assert!(
!out3.is_empty(),
"Fence line should produce output on newline"
);
}
#[test]
fn test_streaming_plain_text_immediate() {
// "Hello" at line start — first char 'H' is not special, should flush immediately.
let mut r = MarkdownRenderer::new();
let out = r.render_delta("H");
assert!(
!out.is_empty(),
"Non-special char 'H' at line start should flush immediately, got empty"
);
}
#[test]
fn test_streaming_digit_paren_still_buffers() {
// "1)" at line start — digit followed by ')', could be a numbered list variant.
let mut r = MarkdownRenderer::new();
let out = r.render_delta("1)");
assert!(
out.is_empty(),
"Digit-paren should still buffer (potential list), got: '{out}'"
);
}
#[test]
fn test_md_render_delta_latency_budget_mid_line() {
// Verify the mid-line fast path produces output without allocating
// a line buffer — this is the hot path for streaming latency.
let mut r = MarkdownRenderer::new();
let _ = r.render_delta("Start ");
assert!(!r.line_start, "Should be mid-line after first token");
// Mid-line token should not touch line_buffer
let out = r.render_delta("word");
assert!(!out.is_empty(), "Mid-line should produce output");
assert!(
r.line_buffer.is_empty(),
"Mid-line fast path should not use line_buffer"
);
}
// --- Live tool progress formatting tests ---
#[test]
fn test_streaming_contract_plain_text_no_buffering() {
// Plain text starting with a non-special character at line start
// should produce immediate output — no buffering needed.
let mut r = MarkdownRenderer::new();
assert!(r.line_start, "Renderer should start at line_start=true");
// "H" is not a special char (#, `, >, -, *, +, digit, |, _)
// so needs_line_buffering() returns false → flush as inline text
let out1 = r.render_delta("H");
assert!(
!out1.is_empty(),
"First token 'H' should produce immediate output (not special char), got empty"
);
assert!(
!r.line_start,
"After flushing 'H', line_start should be false"
);
assert!(
r.line_buffer.is_empty(),
"line_buffer should be empty after non-special first char flush"
);
// Mid-line tokens produce immediate output (mid-line fast path)
let out2 = r.render_delta("ello");
assert!(
!out2.is_empty(),
"Mid-line token 'ello' should produce immediate output"
);
assert!(
r.line_buffer.is_empty(),
"line_buffer should stay empty for mid-line tokens"
);
let out3 = r.render_delta(" world");
assert!(
!out3.is_empty(),
"Mid-line token ' world' should produce immediate output"
);
}
#[test]
fn test_streaming_contract_code_block_passthrough() {
// Tokens inside a code block should produce immediate output via
// the mid-line fast path (DIM-wrapped), not the buffered path.
let mut r = MarkdownRenderer::new();
// Open a code fence
let fence_out = r.render_delta("```rust\n");
assert!(r.in_code_block, "Should be inside code block after fence");
assert!(
fence_out.contains(&format!("{DIM}```rust{RESET}")),
"Fence line should be dim, got: '{fence_out}'"
);
// At code block line start, non-fence content resolves immediately.
// "let x" starts with 'l' (not backtick) → early-resolve as code.
let out1 = r.render_delta("let x");
assert!(
!out1.is_empty(),
"Code block content 'let x' should produce immediate output, got empty"
);
assert!(
out1.contains(&format!("{DIM}let x{RESET}")),
"Mid-line code should be DIM-wrapped (fragment styling), got: '{out1}'"
);
// Mid-line code token (line_start=false)
let out2 = r.render_delta(" = 42;");
assert!(
!out2.is_empty(),
"Code block token ' = 42;' should produce immediate output"
);
assert!(
out2.contains(&format!("{DIM} = 42;{RESET}")),
"Mid-line code token should be DIM-wrapped, got: '{out2}'"
);
}
#[test]
fn test_streaming_contract_heading_detection() {
// "#" at line start should buffer. After the line completes with "\n",
// the heading should render with BOLD+CYAN formatting.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("#");
assert_eq!(
out1, "",
"'#' at line start should buffer (could be heading)"
);
assert!(!r.line_buffer.is_empty(), "line_buffer should contain '#'");
// Complete the heading line
let out2 = r.render_delta("# Title\n");
// line_buffer was "#", now becomes "## Title" after append, then newline processes it
assert!(
out2.contains(&format!("{BOLD}{CYAN}")),
"Heading should have BOLD+CYAN formatting, got: '{out2}'"
);
assert!(
out2.contains("Title"),
"Heading output should contain 'Title', got: '{out2}'"
);
assert!(
r.line_start,
"After newline, line_start should be true again"
);
}
#[test]
fn test_streaming_contract_blockquote_detection() {
// ">" at line start triggers block-level buffering.
// Once confirmed as blockquote, renders with DIM│ and ITALIC content.
let mut r = MarkdownRenderer::new();
// ">" is a blockquote — try_resolve_block_prefix handles it
let out1 = r.render_delta("> ");
// Blockquote prefix should be resolved early by try_resolve_block_prefix
assert!(
out1.contains(&format!("{DIM}│{RESET}")),
"Blockquote should render dim vertical bar, got: '{out1}'"
);
assert!(
r.block_prefix_rendered,
"block_prefix_rendered should be true after blockquote prefix"
);
assert!(
!r.line_start,
"line_start should be false after prefix resolution"
);
// Subsequent content streams as mid-line inline text
let out2 = r.render_delta("quoted text");
assert!(
!out2.is_empty(),
"Content after blockquote prefix should stream immediately"
);
assert!(
out2.contains("quoted text"),
"Should contain the quoted text, got: '{out2}'"
);
// Complete the line
let _out3 = r.render_delta("\n");
assert!(r.line_start, "After newline, should be at line_start again");
}
#[test]
fn test_streaming_contract_inline_formatting_mid_line() {
// Mid-line **bold**, *italic*, and `code` formatting should be applied
// through the render_inline fast path.
let mut r = MarkdownRenderer::new();
// Resolve line start with plain text first
let _ = r.render_delta("This is ");
assert!(!r.line_start, "Should be mid-line");
// Bold mid-line
let out_bold = r.render_delta("**bold**");
assert!(
out_bold.contains(&format!("{BOLD}bold{RESET}")),
"Mid-line **bold** should get BOLD ANSI codes, got: '{out_bold}'"
);
// Italic mid-line
let out_italic = r.render_delta(" and *italic*");
assert!(
out_italic.contains(&format!("{ITALIC}italic{RESET}")),
"Mid-line *italic* should get ITALIC ANSI codes, got: '{out_italic}'"
);
// Inline code mid-line
let out_code = r.render_delta(" and `code`");
assert!(
out_code.contains(&format!("{CYAN}code{RESET}")),
"Mid-line `code` should get CYAN ANSI codes, got: '{out_code}'"
);
}
#[test]
fn test_streaming_contract_empty_delta() {
// render_delta("") should return empty string and not corrupt state,
// at both line_start=true and line_start=false.
// Test at line_start=true
let mut r = MarkdownRenderer::new();
assert!(r.line_start);
let out1 = r.render_delta("");
assert_eq!(out1, "", "Empty delta at line_start should return empty");
assert!(
r.line_start,
"line_start should remain true after empty delta"
);
assert!(
r.line_buffer.is_empty(),
"line_buffer should remain empty after empty delta"
);
assert!(
!r.in_code_block,
"in_code_block should remain false after empty delta"
);
// Test at line_start=false (mid-line)
let _ = r.render_delta("Hello");
assert!(!r.line_start, "Should be mid-line after 'Hello'");
let out2 = r.render_delta("");
assert_eq!(out2, "", "Empty delta at mid-line should return empty");
assert!(
!r.line_start,
"line_start should remain false after empty mid-line delta"
);
}
#[test]
fn test_streaming_contract_newline_resets_line_start() {
// After rendering mid-line content, a "\n" should set line_start=true.
let mut r = MarkdownRenderer::new();
// Get into mid-line state
let _ = r.render_delta("Hello world");
assert!(!r.line_start, "Should be mid-line after 'Hello world'");
// Newline should reset to line_start
let out = r.render_delta("\n");
assert!(
!out.is_empty() || out.contains('\n'),
"Newline delta should produce output containing newline"
);
assert!(r.line_start, "line_start should be true after newline");
assert!(
!r.block_prefix_rendered,
"block_prefix_rendered should be false after newline reset"
);
}
#[test]
fn test_streaming_contract_consecutive_code_blocks() {
// Open fence → content → close fence → open another fence.
// State should correctly track in_code_block across transitions.
let mut r = MarkdownRenderer::new();
// First code block
let _ = r.render_delta("```\n");
assert!(r.in_code_block, "Should be in code block after first fence");
assert!(
r.code_lang.is_none(),
"No language specified for first fence"
);
let _ = r.render_delta("first block\n");
assert!(r.in_code_block, "Should still be in code block");
let _ = r.render_delta("```\n");
assert!(
!r.in_code_block,
"Should exit code block after closing fence"
);
assert!(
r.code_lang.is_none(),
"code_lang should be None after closing"
);
// Normal text between code blocks
let out_normal = r.render_delta("between blocks\n");
assert!(
!r.in_code_block,
"Should not be in code block for normal text"
);
assert!(
out_normal.contains("between blocks"),
"Normal text should render, got: '{out_normal}'"
);
// Second code block with language
let _ = r.render_delta("```python\n");
assert!(
r.in_code_block,
"Should be in code block after second fence"
);
assert_eq!(
r.code_lang.as_deref(),
Some("python"),
"Should capture language 'python'"
);
let _ = r.render_delta("second block\n");
assert!(r.in_code_block, "Should still be in second code block");
let _ = r.render_delta("```\n");
assert!(
!r.in_code_block,
"Should exit second code block after closing fence"
);
assert!(
r.code_lang.is_none(),
"code_lang should be None after second close"
);
}
#[test]
fn test_streaming_contract_flush_final() {
// After feeding partial content without a trailing newline,
// flush() should emit whatever's in the line buffer.
let mut r = MarkdownRenderer::new();
// Feed content that stays buffered (# is ambiguous at line start)
let out1 = r.render_delta("# Partial heading");
// "# Partial heading" — starts with '#', needs_line_buffering=true.
// flush_on_whitespace won't fire for '#'.
// So it stays in the buffer.
assert!(
!r.line_buffer.is_empty() || !out1.is_empty(),
"Content should be either buffered or already output"
);
// Flush should emit the remaining content
let flushed = r.flush();
assert!(!flushed.is_empty(), "flush() should emit buffered content");
assert!(
flushed.contains("Partial heading"),
"flushed output should contain the text, got: '{flushed}'"
);
assert!(
r.line_buffer.is_empty(),
"line_buffer should be empty after flush"
);
// Also test flush with non-special content that was already emitted
let mut r2 = MarkdownRenderer::new();
let _ = r2.render_delta("Already emitted");
// "Already emitted" starts with 'A' — non-special → flushed immediately
let flushed2 = r2.flush();
// Nothing should be in buffer since it was already emitted
assert!(
r2.line_buffer.is_empty(),
"line_buffer should be empty after non-special text was already flushed"
);
// flushed2 might be empty (content already emitted) or contain RESET
// The key contract: no panic, no corruption
let _ = flushed2;
}
#[test]
fn test_streaming_contract_nested_formatting_in_list() {
// "- **bold item**\n" should get both list bullet formatting and bold.
let mut r = MarkdownRenderer::new();
let out = r.render_delta("- **bold item**\n");
// This is a complete line, processed by render_line.
// strip_unordered_list_marker finds "- " and returns "**bold item**".
// render_inline processes the bold markers.
assert!(
out.contains(&format!("{CYAN}•{RESET}")),
"Should have colored bullet, got: '{out}'"
);
assert!(
out.contains(&format!("{BOLD}bold item{RESET}")),
"Should have bold formatting inside list item, got: '{out}'"
);
// Also test streamed version where prefix resolves early
let mut r2 = MarkdownRenderer::new();
let out1 = r2.render_delta("- ");
// "- " — try_resolve_block_prefix tries unordered list.
// try_confirm_unordered_list: "- " has empty rest → returns Some("").
// So prefix renders with bullet.
let out2 = r2.render_delta("**bold item**");
let out3 = r2.render_delta("\n");
let total = format!("{out1}{out2}{out3}");
assert!(
total.contains(&format!("{CYAN}•{RESET}")),
"Streamed list should have colored bullet, got: '{total}'"
);
assert!(
total.contains("bold item"),
"Streamed list should contain bold item text, got: '{total}'"
);
}
#[test]
fn test_streaming_contract_digit_word_flushes() {
// Issue #147: digit-word patterns like "2nd" should flush early.
// "2" at line start buffers (could be numbered list "2. ").
// "2n" → second char is not '.' or ')' → needs_line_buffering() returns false → flush.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("2");
// "2" alone — a digit at line start with len < 2, needs_line_buffering returns true
assert!(
r.line_start,
"After single digit '2', should still be at line_start (buffering)"
);
let out2 = r.render_delta("n");
// line_buffer is now "2n". needs_line_buffering sees '2' then 'n' (not '.' or ')').
// Returns false → buffer flushes as inline text.
let combined = format!("{out1}{out2}");
assert!(
!combined.is_empty(),
"After '2n', digit-word should have flushed, got empty"
);
assert!(
combined.contains('2'),
"Flushed output should contain '2', got: '{combined}'"
);
assert!(
!r.line_start,
"After digit-word flush, line_start should be false"
);
// Subsequent tokens stream immediately via mid-line fast path
let out3 = r.render_delta("d");
assert!(
!out3.is_empty(),
"Mid-line token 'd' should produce immediate output"
);
}
#[test]
fn test_streaming_contract_dash_word_flushes() {
// Issue #147: dash-word patterns like "-based" should flush early.
// "-" at line start buffers (could be list "- " or horizontal rule "---").
// "-b" → second char is not space or dash → needs_line_buffering() returns false → flush.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("-");
// "-" alone — needs_line_buffering: trimmed.len() < 2 → true
assert!(
r.line_start,
"After single dash '-', should still be at line_start (buffering)"
);
let out2 = r.render_delta("b");
// line_buffer is now "-b". needs_line_buffering: second char 'b' != ' ' && != '-'
// → returns false → flush as inline text.
let combined = format!("{out1}{out2}");
assert!(
!combined.is_empty(),
"After '-b', dash-word should have flushed, got empty"
);
assert!(
!r.line_start,
"After dash-word flush, line_start should be false"
);
// Subsequent tokens stream immediately
let out3 = r.render_delta("ased");
assert!(
!out3.is_empty(),
"Mid-line token 'ased' should produce immediate output"
);
}
#[test]
fn test_streaming_contract_numbered_list_buffers() {
// "1." at line start should keep buffering (could be numbered list "1. item").
// needs_line_buffering: digit followed by '.' → keeps buffering.
// Once "1. item" arrives (via newline), it resolves as ordered list.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("1");
assert!(r.line_start, "After '1', should still buffer at line_start");
let out2 = r.render_delta(".");
// line_buffer is "1." — needs_line_buffering: digit then '.', trimmed.len() < 3 → true
assert!(
r.line_start,
"After '1.', should still buffer (could be numbered list)"
);
let out3 = r.render_delta(" ");
// line_buffer is "1. " — needs_line_buffering checks for ". " pattern.
// try_resolve_block_prefix tries ordered list: "1. " with empty content → returns None.
// flush_on_whitespace: starts with digit → returns empty.
// So still buffering.
let pre_content = format!("{out1}{out2}{out3}");
let out4 = r.render_delta("item");
// line_buffer is "1. item" — needs_line_buffering: contains ". " and digits before it → true.
// try_resolve_block_prefix → try_confirm_ordered_list: "1. item" → Some(("1", "item")).
// Renders prefix and sets line_start=false.
let all = format!("{pre_content}{out4}");
assert!(
all.contains(&format!("{CYAN}1.{RESET}")),
"Numbered list should render with CYAN number, got: '{all}'"
);
assert!(
all.contains("item"),
"Should contain list item content, got: '{all}'"
);
assert!(
!r.line_start,
"After ordered list prefix resolves, line_start should be false"
);
}
#[test]
fn test_streaming_contract_multi_digit_numbered_list_buffers() {
// "12." at line start should keep buffering (could be "12. item").
// The early disambiguation should NOT flush "12." as inline text —
// digits followed by '.' is a valid numbered-list prefix pattern.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("1");
assert!(r.line_start, "After '1', should still buffer");
let out2 = r.render_delta("2");
// "12" — all digits, len < 3, needs_line_buffering → true
assert!(r.line_start, "After '12', should still buffer (all digits)");
let out3 = r.render_delta(".");
// "12." — digits followed by '.', should keep buffering
// (could become "12. item" — a numbered list)
assert!(
r.line_start,
"After '12.', should still buffer (could be numbered list like '12. item')"
);
let out4 = r.render_delta(" ");
// "12. " — has ". " pattern with digits before it
let out5 = r.render_delta("item");
// "12. item" — should resolve as ordered list
let all = format!("{out1}{out2}{out3}{out4}{out5}");
assert!(
all.contains(&format!("{CYAN}12.{RESET}")),
"Multi-digit numbered list should render with CYAN number, got: '{all}'"
);
assert!(
all.contains("item"),
"Should contain list item content, got: '{all}'"
);
assert!(
!r.line_start,
"After ordered list prefix resolves, line_start should be false"
);
}
#[test]
fn test_streaming_contract_digit_dot_non_space_flushes() {
// "12.x" at line start: digits + '.' + non-space → not a numbered list.
// Should flush as inline text once the non-space char after '.' is seen.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("1");
assert!(r.line_start, "After '1', should buffer");
let out2 = r.render_delta("2");
assert!(r.line_start, "After '12', should buffer");
let out3 = r.render_delta(".");
// "12." — digits + '.', could be list, still buffering
assert!(r.line_start, "After '12.', should still buffer");
let out4 = r.render_delta("x");
// "12.x" — char after dot is 'x', not space → not a list → flush
let combined = format!("{out1}{out2}{out3}{out4}");
assert!(
!combined.is_empty(),
"After '12.x' (not a list), should flush as inline text"
);
assert!(
!r.line_start,
"After flushing '12.x', line_start should be false"
);
}
#[test]
fn test_streaming_contract_unordered_list_buffers() {
// "- " at line start triggers list detection but doesn't resolve until
// non-dash, non-space content arrives (to rule out horizontal rule "- - -").
// After "- item", try_resolve_block_prefix confirms it as a list.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("- ");
// "- " alone: try_confirm_unordered_list returns None (could be "- - -" HR).
// Still buffering.
assert!(
r.line_start,
"After '- ', should still be at line_start (not yet confirmed as list)"
);
let out2 = r.render_delta("item");
// line_buffer is "- item" — try_confirm_unordered_list: rest="item", has non-dash char → Some.
// Prefix renders with bullet, line_start=false.
let combined = format!("{out1}{out2}");
assert!(
combined.contains(&format!("{CYAN}•{RESET}")),
"Unordered list should render with CYAN bullet after '- item', got: '{combined}'"
);
assert!(
!r.line_start,
"After list prefix resolves, line_start should be false"
);
assert!(
r.block_prefix_rendered,
"block_prefix_rendered should be true after list prefix"
);
assert!(
combined.contains("item"),
"Output should contain 'item', got: '{combined}'"
);
}
#[test]
fn test_streaming_contract_code_fence_buffers() {
// Code fence "```" should buffer until fully resolved.
// No output should leak before the fence is complete.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("`");
assert_eq!(
out1, "",
"Single '`' at line start should buffer (could be fence)"
);
assert!(r.line_start, "Should still be at line_start after '`'");
let out2 = r.render_delta("`");
assert_eq!(
out2, "",
"Two backticks '``' should still buffer (could be fence)"
);
assert!(r.line_start, "Should still be at line_start after '``'");
let out3 = r.render_delta("`");
assert_eq!(
out3, "",
"Three backticks '```' should still buffer (fence, awaiting newline)"
);
let out4 = r.render_delta("rust\n");
// Now the fence line "```rust\n" is complete — should produce output
let all = format!("{out1}{out2}{out3}{out4}");
assert!(
!all.is_empty(),
"Complete fence line should produce output, got empty"
);
assert!(
r.in_code_block,
"Should be inside code block after fence resolves"
);
}
#[test]
fn test_streaming_contract_mid_line_immediate() {
// After line_start is set to false (by flushing initial content),
// subsequent tokens should produce immediate output via mid-line fast path.
let mut r = MarkdownRenderer::new();
// "Hello" starts with 'H' — not a special char, flushes immediately
let out1 = r.render_delta("Hello");
assert!(
!out1.is_empty(),
"'Hello' should flush immediately (non-special first char)"
);
assert!(!r.line_start, "After flushing 'Hello', should be mid-line");
// Now feed mid-line content
let out2 = r.render_delta(" world");
assert!(
!out2.is_empty(),
"Mid-line ' world' should produce immediate output"
);
assert!(
out2.contains("world"),
"Mid-line output should contain 'world', got: '{out2}'"
);
}
#[test]
fn test_streaming_contract_plain_text_immediate_flush() {
// Text starting with a non-special character ('H', 'T', 'A', etc.)
// should flush immediately — no buffering needed.
let mut r = MarkdownRenderer::new();
assert!(r.line_start, "Fresh renderer starts at line_start=true");
let out = r.render_delta("Hello");
assert!(
!out.is_empty(),
"'Hello' at line start should produce immediate output (not a special char)"
);
assert!(
out.contains("Hello"),
"Output should contain 'Hello', got: '{out}'"
);
assert!(
!r.line_start,
"After flushing plain text, line_start should be false"
);
assert!(
r.line_buffer.is_empty(),
"line_buffer should be empty after immediate flush"
);
}
#[test]
fn test_streaming_contract_heading_buffers_then_resolves() {
// "#" at line start should buffer. "# Title\n" resolves as heading.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("#");
assert_eq!(
out1, "",
"'#' at line start should buffer (could be heading)"
);
assert!(r.line_start, "Should still be at line_start after '#'");
assert!(!r.line_buffer.is_empty(), "line_buffer should contain '#'");
let out2 = r.render_delta(" ");
// line_buffer is "# " — still needs buffering (heading confirmed but no content yet)
let out3 = r.render_delta("Title");
let out4 = r.render_delta("\n");
let all = format!("{out1}{out2}{out3}{out4}");
// After newline, the complete heading "# Title" should render with formatting
assert!(
all.contains(&format!("{BOLD}{CYAN}")),
"Heading should have BOLD+CYAN formatting, got: '{all}'"
);
assert!(
all.contains("Title"),
"Heading output should contain 'Title', got: '{all}'"
);
assert!(r.line_start, "After newline, should be at line_start again");
}
#[test]
fn test_color_struct_display_consistency() {
// All color constants should be the same type and format without panic
let result = format!("{BOLD}{DIM}{GREEN}{YELLOW}{CYAN}{RED}{RESET}");
// Should either have all codes or be empty (if NO_COLOR is set)
assert!(result.contains('\x1b') || result.is_empty());
}
// --- MarkdownRenderer tests ---
#[test]
fn test_streaming_multi_digit_nonlist_flushes() {
// "100m" — multi-digit number followed by letter, not a list.
let mut r = MarkdownRenderer::new();
let out1 = r.render_delta("10");
// "10" — all digits, could still be "10. " — should buffer
assert!(
out1.is_empty(),
"All-digit '10' should buffer (could be list number), got: '{out1}'"
);
let out2 = r.render_delta("0m");
// "100m" — the 'm' disambiguates: not a list number
assert!(
!out2.is_empty(),
"'100m' should flush — letter after digits means not a list, got empty"
);
}
}