linesmith 0.1.2

A Rust status line for Claude Code and other AI coding CLIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
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
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
//! CLI driver. `cli_main` takes args + stdin + env and returns an
//! exit code, handling arg parsing, config loading, `--check-config`,
//! and render dispatch. `main.rs` wires real IO with
//! `CliEnv::from_process`; tests pass `Cursor` / `Vec<u8>` buffers
//! and a hand-built `CliEnv`.

use crate::plugins::PluginRegistry;
use crate::segments::builder::build_lines;
use crate::{
    cli, config, detect_terminal_width, presets, run_lines_with_context, runtime, theme, RunContext,
};
use std::io::{BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};

/// `NO_COLOR`-style flag: any non-empty value means "on." Per
/// no-color.org.
fn no_color_env(name: &str) -> bool {
    std::env::var_os(name).is_some_and(|v| !v.is_empty())
}

/// `FORCE_COLOR`-style flag: treat `"0"`, `"false"`, `"off"`, unset,
/// and empty as "absent" (no force). Any other value enables force.
/// Matches npm / supports-color / chalk conventions so `FORCE_COLOR=0`
/// doesn't accidentally force color on users who set it to disable.
fn force_color_env(name: &str) -> bool {
    let Ok(v) = std::env::var(name) else {
        return false;
    };
    !matches!(v.as_str(), "" | "0" | "false" | "off")
}

/// Process-ambient inputs the CLI reads: env vars consulted by
/// `resolve_config_path`, the color-policy env flags, an optional
/// terminal-width override, and an optional color-capability
/// override. Passed through `cli_main` so tests can drive the whole
/// binary without touching the real process env. `#[non_exhaustive]`
/// leaves room for future env vars (TERM, ...) without breaking
/// external construction.
///
/// Env snapshotting is the exclusive job of [`CliEnv::from_process`].
/// [`CliEnv::default`] and [`CliEnv::for_tests`] do not read any
/// ambient state — the resolver honors only what the struct carries.
/// Production binaries must use `from_process`; callers passing
/// `default()` opt out of env awareness entirely (including
/// `NO_COLOR` / `FORCE_COLOR` / `COLUMNS`).
///
/// `terminal_width = None` means "detect lazily when the render path
/// needs it." Meta commands (`--help`, `--version`, `--check-config`)
/// never probe the terminal, so stray `COLUMNS` warnings don't leak
/// into clean stderr.
///
/// `color_capability = Some(cap)` bypasses the entire color-policy
/// precedence chain — reserved for test determinism. Production uses
/// `None` and lets `no_color` / `force_color` / config resolve it.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct CliEnv {
    pub linesmith_config: Option<String>,
    pub xdg_config_home: Option<String>,
    pub home: Option<String>,
    pub no_color: bool,
    pub force_color: bool,
    /// Raw `COLORTERM`, or `None` if unset. Threaded to the
    /// force-color resolver so it can justify TrueColor when the TTY
    /// probe gives up (e.g. piped stdout under Claude Code).
    /// `from_process` snapshots the real env; `for_tests`/`default`
    /// leave it `None` so ambient `COLORTERM=truecolor` can't leak
    /// into captured-output tests.
    pub colorterm: Option<String>,
    /// Raw `TERM`, or `None` if unset. Same snapshot discipline as
    /// `colorterm`.
    pub term: Option<String>,
    pub terminal_width: Option<u16>,
    pub color_capability: Option<theme::Capability>,
    /// cwd used for gix repo discovery. `None` skips discovery
    /// entirely. [`Self::from_process`] sets this to
    /// `std::env::current_dir()`; [`Self::for_tests`] leaves it
    /// `None`.
    pub cwd: Option<std::path::PathBuf>,
    /// Raw `LINESMITH_LOG` value, or `None` if unset. `from_process`
    /// snapshots the real env; `for_tests`/`default` leave it `None`
    /// so a developer's ambient `LINESMITH_LOG=debug` can't pollute
    /// captured-stderr CLI tests.
    pub log_level_env: Option<String>,
}

impl CliEnv {
    /// Snapshot the real process env vars. Terminal width and color
    /// capability are left unset; `run_cli` probes them only if a
    /// render happens.
    #[must_use]
    pub fn from_process() -> Self {
        Self {
            linesmith_config: std::env::var("LINESMITH_CONFIG").ok(),
            xdg_config_home: std::env::var("XDG_CONFIG_HOME").ok(),
            home: std::env::var("HOME").ok(),
            no_color: no_color_env("NO_COLOR"),
            force_color: force_color_env("FORCE_COLOR"),
            colorterm: std::env::var("COLORTERM").ok(),
            term: std::env::var("TERM").ok(),
            terminal_width: None,
            color_capability: None,
            cwd: std::env::current_dir().ok(),
            log_level_env: std::env::var(crate::logging::ENV_VAR).ok(),
        }
    }

    /// Test-suite baseline: no env paths, color flags off,
    /// `terminal_width = Some(200)`, `color_capability = Some(None)`.
    /// Forces the capability override so stdout stays plain under a
    /// truecolor host; tests that exercise the color-policy resolver
    /// directly use `CliEnv::default()` instead.
    #[must_use]
    pub fn for_tests() -> Self {
        Self {
            linesmith_config: None,
            xdg_config_home: None,
            home: None,
            no_color: false,
            force_color: false,
            colorterm: None,
            term: None,
            terminal_width: Some(200),
            color_capability: Some(theme::Capability::None),
            cwd: None,
            log_level_env: None,
        }
    }
}

/// CLI entry point. Returns a `u8` exit code so callers convert to
/// `ExitCode` only at the outermost layer.
pub fn cli_main<A>(
    args: A,
    stdin: impl Read,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
    env: &CliEnv,
) -> u8
where
    A: IntoIterator,
    A::Item: Into<std::ffi::OsString>,
{
    crate::logging::apply(env.log_level_env.as_deref(), stderr);

    let action = match cli::parse(args) {
        Ok(a) => a,
        Err(err) => {
            let _ = writeln!(stderr, "linesmith: {err}");
            let _ = writeln!(stderr, "Try --help for usage.");
            return 2;
        }
    };

    match action {
        cli::Action::Help => {
            let _ = write!(stdout, "{}", cli::HELP);
            0
        }
        cli::Action::Version => {
            let _ = writeln!(stdout, "linesmith {}", env!("CARGO_PKG_VERSION"));
            0
        }
        cli::Action::ThemesList => themes_list(stdout, stderr, env),
        cli::Action::PresetsList => presets_list(stdout),
        cli::Action::PresetsApply {
            name,
            force,
            config,
        } => presets_apply(&name, force, config, stdin, stdout, stderr, env),
        cli::Action::Init { config, no_doctor } => {
            init_action(config, no_doctor, stdin, stdout, stderr, env)
        }
        cli::Action::Doctor { plain, config } => doctor_action(plain, config, stdout, stderr),
        cli::Action::Run(args) => run_cli(args, stdin, stdout, stderr, env),
    }
}

/// Build a doctor report, render it, and return the report's exit
/// code. Stdout I/O failures are surfaced to stderr but do not
/// promote a healthy report to a non-zero exit, matching the
/// `cli_main` convention used by `themes-list`, `presets-list`, etc.:
/// partial / missing stdout is the user-visible signal that the
/// writer broke, not the report.
///
/// `config_override` is threaded into the [`DoctorEnv`] snapshot so
/// the Config category inspects the user-named path directly,
/// rather than the action layer warning about it after the fact.
fn doctor_action(
    plain: bool,
    config_override: Option<PathBuf>,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
) -> u8 {
    let env = crate::doctor::DoctorEnv::from_process(config_override);
    doctor_action_with_env(env, plain, stdout, stderr)
}

/// Test seam for `doctor_action`. Tests construct a hermetic
/// `DoctorEnv` (via `DoctorEnv::healthy()` plus per-test mutation)
/// and call this directly so the doctor's view of the world is
/// controlled, not inherited from whatever ambient env the test
/// runner happens to provide. Production code never calls this
/// directly; it goes through `doctor_action`.
fn doctor_action_with_env(
    env: crate::doctor::DoctorEnv,
    plain: bool,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
) -> u8 {
    let report = crate::doctor::build_report(&env);
    let mode = if plain {
        crate::doctor::RenderMode::Plain
    } else {
        crate::doctor::RenderMode::Default
    };
    if let Err(err) = crate::doctor::render(stdout, &report, mode) {
        let _ = writeln!(stderr, "linesmith: doctor: {err}");
    }
    report.exit_code()
}

fn themes_list(stdout: &mut dyn Write, stderr: &mut dyn Write, env: &CliEnv) -> u8 {
    let registry = build_theme_registry(env, stderr);
    for rt in registry.iter() {
        let source = match &rt.source {
            theme::ThemeSource::BuiltIn => "built-in".to_string(),
            theme::ThemeSource::UserFile(p) => p.display().to_string(),
            // ThemeSource is `#[non_exhaustive]` per ADR-0018; a
            // future variant in linesmith-core renders generically
            // until the cli adds an explicit label.
            _ => "unknown source".to_string(),
        };
        let _ = writeln!(stdout, "{}\t{}", rt.theme.name(), source);
    }
    0
}

fn presets_list(stdout: &mut dyn Write) -> u8 {
    for name in presets::names() {
        let _ = writeln!(stdout, "{name}");
    }
    0
}

/// Write a preset's body to the resolved config path. Handles
/// backup-on-overwrite, the `--force` short-circuit, and the y/N
/// confirmation prompt. Returns 0 on success, 1 on user-facing errors
/// (unknown preset, aborted overwrite, existing `.bak`, I/O failure,
/// unresolved path).
fn presets_apply(
    name: &str,
    force: bool,
    config_override: Option<PathBuf>,
    stdin: impl Read,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
    env: &CliEnv,
) -> u8 {
    let Some(body) = presets::body(name) else {
        let _ = writeln!(stderr, "linesmith: unknown preset '{name}'");
        let _ = writeln!(stderr, "available presets:");
        for known in presets::names() {
            let _ = writeln!(stderr, "  {known}");
        }
        return 1;
    };

    let Some(resolved) = resolve_writable_config_path(config_override, env, stderr) else {
        return 1;
    };
    let policy = OverwritePolicy::presets(force);
    let with_schema = config::with_schema_directive(body);
    if let Err(code) = write_config_with_backup(&with_schema, &resolved.path, policy, stdin, stderr)
    {
        return code;
    }
    let _ = writeln!(
        stdout,
        "wrote preset '{name}' to {}",
        resolved.path.display()
    );
    0
}

/// Resolve the target config path or emit the "set XDG_CONFIG_HOME or
/// HOME" diagnostic and return `None`. Shared by `presets_apply` and
/// the `init` flow so both treat an undiscoverable home identically.
fn resolve_writable_config_path(
    config_override: Option<PathBuf>,
    env: &CliEnv,
    stderr: &mut dyn Write,
) -> Option<config::ConfigPath> {
    match config::resolve_config_path(
        config_override,
        env.linesmith_config.as_deref(),
        env.xdg_config_home.as_deref(),
        env.home.as_deref(),
    ) {
        Some(p) => Some(p),
        None => {
            let _ = writeln!(
                stderr,
                "linesmith: cannot resolve a config path (set XDG_CONFIG_HOME or HOME)"
            );
            None
        }
    }
}

/// Two-bit policy for [`write_config_with_backup`]. `presets apply`
/// maps `--force` to both bits at once; `init` separates them so a
/// user who confirmed `y` to the overwrite prompt isn't then blocked
/// by a leftover `.bak` from a prior run. Use
/// [`OverwritePolicy::presets`] or [`OverwritePolicy::init`] so the
/// bit-mapping doesn't drift between call sites.
#[derive(Debug, Clone, Copy)]
struct OverwritePolicy {
    /// Skip the y/N prompt entirely. `presets apply --force` sets
    /// this; `init` always leaves it `false` so the user explicitly
    /// confirms.
    skip_prompt: bool,
    /// When a `.bak` already exists alongside `path`, replace it
    /// instead of refusing. `presets apply --force` sets this; `init`
    /// always sets it because the y/N prompt already captured the
    /// user's intent to lose the previous content.
    clobber_backup: bool,
}

impl OverwritePolicy {
    /// `presets apply [--force]` policy: prompt + refuse `.bak` by
    /// default, both elided when `--force`.
    fn presets(force: bool) -> Self {
        Self {
            skip_prompt: force,
            clobber_backup: force,
        }
    }

    /// `init` policy: always prompt, always clobber `.bak` after a
    /// confirmed `y`. The prompt captured the user's intent to lose
    /// the previous content, so a leftover `.bak` from a prior run
    /// shouldn't block the overwrite.
    fn init() -> Self {
        Self {
            skip_prompt: false,
            clobber_backup: true,
        }
    }
}

/// Write `body` to `path`, backing up any prior file. If `path` exists,
/// honor `policy` (prompt or skip; refuse or clobber an existing
/// `.bak`), rename to `path.bak`, then write. If `path` doesn't
/// exist, create parent directories and write. Returns `Ok(true)`
/// when a backup was made, `Ok(false)` otherwise; `Err(1)` on
/// aborted overwrite, refused `.bak`, or I/O failure.
///
/// Shared between `presets_apply` and `init` so both paths use the
/// same prompt phrasing and `.bak` filename. TOCTOU between
/// `exists()` and `rename`/`write` is the downstream call's problem:
/// a vanished file surfaces its own `fs::*` error and we return 1.
fn write_config_with_backup(
    body: &str,
    path: &Path,
    policy: OverwritePolicy,
    stdin: impl Read,
    stderr: &mut dyn Write,
) -> Result<bool, u8> {
    let backup = path.with_extension("toml.bak");
    let mut backup_written = false;

    if path.exists() {
        if !policy.skip_prompt && !confirm_overwrite(path, stdin, stderr) {
            let _ = writeln!(stderr, "linesmith: aborted; config.toml unchanged");
            return Err(1);
        }
        // Refuse to clobber an existing backup unless the policy
        // allows it. `presets apply` defaults to refusing (two
        // generations preserved); `presets apply --force` and `init`
        // (after a confirmed y/N) both clobber.
        let mut clobbered_prior_bak = false;
        if backup.exists() {
            if !policy.clobber_backup {
                let _ = writeln!(
                    stderr,
                    "linesmith: {} already exists; rerun with --force to replace it",
                    backup.display()
                );
                return Err(1);
            }
            // Windows' `fs::rename` fails when the destination exists;
            // pre-remove so the clobber path works the same on every
            // platform.
            if let Err(e) = std::fs::remove_file(&backup) {
                let _ = writeln!(
                    stderr,
                    "linesmith: could not remove existing backup {}: {e}",
                    backup.display()
                );
                return Err(1);
            }
            clobbered_prior_bak = true;
        }
        if let Err(e) = std::fs::rename(path, &backup) {
            let _ = writeln!(
                stderr,
                "linesmith: could not back up {} to {}: {e}",
                path.display(),
                backup.display()
            );
            // Surface that the prior-generation `.bak` was already
            // removed before the rename was attempted. Without this
            // breadcrumb, a user seeing only "could not back up"
            // would assume nothing changed and miss that a
            // recoverable backup was just consumed.
            if clobbered_prior_bak {
                let _ = writeln!(
                    stderr,
                    "linesmith: note: the previous {} was removed just before this failed rename; it is gone",
                    backup.display()
                );
            }
            return Err(1);
        }
        let _ = writeln!(
            stderr,
            "linesmith: backed up previous config to {}",
            backup.display()
        );
        backup_written = true;
    } else if let Some(parent) = path.parent() {
        if let Err(e) = std::fs::create_dir_all(parent) {
            let _ = writeln!(
                stderr,
                "linesmith: could not create {}: {e}",
                parent.display()
            );
            return Err(1);
        }
    }

    if let Err(e) = std::fs::write(path, body) {
        let _ = writeln!(stderr, "linesmith: write {}: {e}", path.display());
        if backup_written {
            let _ = writeln!(
                stderr,
                "linesmith: your previous config is preserved at {}",
                backup.display()
            );
        }
        return Err(1);
    }
    Ok(backup_written)
}

/// Prompt once on stderr; accept `y` / `yes` (case-insensitive) as yes,
/// everything else as no. EOF is treated as "no" so non-interactive
/// callers abort rather than overwrite.
fn confirm_overwrite(path: &Path, stdin: impl Read, stderr: &mut dyn Write) -> bool {
    let _ = write!(stderr, "overwrite {}? [y/N] ", path.display());
    let _ = stderr.flush();
    let mut line = String::new();
    let mut reader = BufReader::new(stdin);
    if reader.read_line(&mut line).is_err() {
        return false;
    }
    parse_confirmation(&line)
}

/// Treat `y` / `yes` (case-insensitive, surrounding whitespace allowed)
/// as confirmation. Everything else, including empty input, is rejected.
fn parse_confirmation(input: &str) -> bool {
    matches!(input.trim().to_ascii_lowercase().as_str(), "y" | "yes")
}

/// User-supplied choices for `linesmith init`. The dialoguer wrapper
/// builds these at the CLI; tests construct them directly so the
/// file-writing core can run without a TTY.
#[derive(Debug, Clone)]
struct InitChoices {
    preset: String,
    theme: String,
}

/// Gather choices via dialoguer, then dispatch to [`init_with_choices`].
/// The split keeps the file-writing + snippet logic testable without a
/// fake TTY. Also translates dialoguer's I/O errors (TTY missing,
/// stdin closed mid-prompt, broken pipe, etc.) into a printed
/// diagnostic plus a hint pointing at `presets apply` for
/// non-interactive use.
fn init_action(
    config_override: Option<PathBuf>,
    no_doctor: bool,
    stdin: impl Read,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
    env: &CliEnv,
) -> u8 {
    let preset_names: Vec<&'static str> = presets::names().collect();
    let registry = build_theme_registry(env, stderr);
    let theme_names: Vec<String> = registry
        .iter()
        .map(|t| t.theme.name().to_string())
        .collect();

    let choices = match prompt_for_init_choices(&preset_names, &theme_names) {
        Ok(c) => c,
        Err(err) => {
            // dialoguer collapses TTY-missing, EOF-mid-prompt, broken
            // pipe, and EINTR into a single `Error::IO` variant, so
            // we can't reliably distinguish them. Print the
            // underlying error verbatim and offer the no-tty fallback
            // as conditional advice rather than a diagnosis.
            let _ = writeln!(stderr, "linesmith: init: {err}");
            let _ = writeln!(
                stderr,
                "linesmith: if a terminal isn't attached, use 'linesmith presets apply <name>' instead",
            );
            return 1;
        }
    };

    init_with_choices(
        &choices,
        no_doctor,
        config_override,
        stdin,
        stdout,
        stderr,
        env,
    )
}

/// Drive the two interactive Select prompts. Defaults to index 0 in
/// each list so pressing Enter through both prompts produces a usable
/// config; the caller controls what index 0 means (today: preset =
/// `minimal`, theme = `default`).
///
/// `docs/specs/config.md:285` and lsm-5yd both list a third "tool"
/// prompt (claude-code / qwen-code / auto). Skipped here for v0.1:
/// `Config` has no `tool` field, only Claude Code has a real install
/// path, and prompting for an option that does nothing is worse UX
/// than not prompting at all. Revisit when qwen-code support gives
/// the choice a real consequence.
fn prompt_for_init_choices(
    presets: &[&str],
    themes: &[String],
) -> Result<InitChoices, dialoguer::Error> {
    use dialoguer::{theme::ColorfulTheme, Select};
    let theme = ColorfulTheme::default();
    let preset_idx = Select::with_theme(&theme)
        .with_prompt("preset")
        .items(presets)
        .default(0)
        .interact()?;
    let theme_idx = Select::with_theme(&theme)
        .with_prompt("theme")
        .items(themes)
        .default(0)
        .interact()?;
    Ok(InitChoices {
        preset: presets[preset_idx].to_string(),
        theme: themes[theme_idx].clone(),
    })
}

/// Testable core of `linesmith init`: write the chosen preset (with the
/// chosen theme injected) to the resolved config path, then print the
/// Claude Code `settings.json` snippet for copy-paste. Returns 0 on
/// success, 1 on user-facing errors.
fn init_with_choices(
    choices: &InitChoices,
    no_doctor: bool,
    config_override: Option<PathBuf>,
    stdin: impl Read,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
    env: &CliEnv,
) -> u8 {
    let Some(body) = presets::body(&choices.preset) else {
        // Defense-in-depth: prompts only offer registered presets, but a
        // hand-built `InitChoices` from a test could pass an unknown
        // name.
        let _ = writeln!(stderr, "linesmith: unknown preset '{}'", choices.preset);
        return 1;
    };
    let composed = config::with_schema_directive(&compose_init_body(body, &choices.theme));

    let Some(resolved) = resolve_writable_config_path(config_override, env, stderr) else {
        return 1;
    };
    let policy = OverwritePolicy::init();
    if let Err(code) = write_config_with_backup(&composed, &resolved.path, policy, stdin, stderr) {
        return code;
    }

    // Snippet must point at the file we just wrote. If the user
    // resolved the path explicitly (via `--config` OR `LINESMITH_CONFIG`),
    // bare `linesmith` would re-resolve to the XDG default and miss
    // it. The `explicit` bit lets us emit the right `--config <path>`
    // for both branches.
    let snippet_path = resolved.explicit.then(|| resolved.path.clone());
    let _ = writeln!(stdout, "wrote config.toml to {}", resolved.path.display());
    warn_if_path_needs_user_edit(snippet_path.as_deref(), stderr);
    let _ = writeln!(stdout);
    let _ = writeln!(stdout, "Add this to your Claude Code settings.json:");
    let _ = writeln!(stdout, "(merge into the existing top-level object)");
    let _ = writeln!(stdout);
    let _ = writeln!(stdout, "  \"statusLine\": {{");
    let _ = writeln!(stdout, "    \"type\": \"command\",");
    let _ = writeln!(stdout, "    \"command\": {},", json_command(&snippet_path));
    let _ = writeln!(stdout, "    \"padding\": 0");
    let _ = writeln!(stdout, "  }}");

    if no_doctor {
        return 0;
    }

    // Spec §`linesmith init` integration: post-init doctor
    // invocation. Init's exit code propagates from doctor —
    // a doctor FAIL after a clean init means the env around
    // the new config is still broken (Claude Code missing,
    // credentials unresolvable, etc.) and the user should
    // know before they try `linesmith` for real. Pass the
    // resolved path through so doctor inspects exactly the
    // file we just wrote, not whatever its own cascade might
    // re-resolve to (which can differ when --config or
    // $LINESMITH_CONFIG were involved).
    let _ = writeln!(stdout);
    let _ = writeln!(stdout, "Running doctor to verify your setup...");
    let _ = writeln!(stdout);
    doctor_action(false, Some(resolved.path), stdout, stderr)
}

/// JSON-encode the `command` string for the Claude Code snippet emitted
/// by `init_with_choices`. When the user resolved an explicit path (via
/// `--config` or `LINESMITH_CONFIG`), Claude Code must call
/// `linesmith --config <PATH>` to read the file we just wrote;
/// otherwise plain `linesmith` finds the XDG default. Only JSON
/// escaping happens here (backslashes + double quotes); shell quoting
/// is the caller's problem because POSIX and Windows shells disagree
/// on the rules and any wrapping we do would be wrong somewhere.
/// [`warn_if_path_needs_user_edit`] flags the cases that need
/// hand-quoting before this is invoked.
fn json_command(explicit_path: &Option<PathBuf>) -> String {
    let escape = |s: &str| s.replace('\\', "\\\\").replace('"', "\\\"");
    match explicit_path {
        Some(p) => format!("\"linesmith --config {}\"", escape(&p.to_string_lossy())),
        None => "\"linesmith\"".to_string(),
    }
}

/// Warn on stderr when the `--config` path will produce a snippet that
/// can't be pasted as-is. Two failure modes:
///
/// - Non-UTF-8 bytes: `to_string_lossy` substitutes `U+FFFD`, so the
///   emitted path is garbled and Claude Code's exec fails at runtime
///   with no link back to init.
/// - Shell metacharacters or whitespace: Claude Code tokenizes
///   `command` as a shell argv, so `linesmith --config /a b/c.toml`
///   becomes four tokens. The snippet is valid JSON but exec won't
///   find the config.
///
/// Init's exit code stays 0 either way (the file was written); the
/// warning tells the user to hand-edit before pasting.
fn warn_if_path_needs_user_edit(explicit_path: Option<&Path>, stderr: &mut dyn Write) {
    let Some(p) = explicit_path else { return };
    let s = p.to_str();
    let non_utf8 = s.is_none();
    let leading_dash = s.is_some_and(|s| s.starts_with('-'));
    let needs_quoting = s.is_some_and(|s| s.chars().any(needs_shell_quoting));
    if non_utf8 {
        let _ = writeln!(
            stderr,
            "linesmith: warning: --config path contains non-UTF-8 bytes; the emitted snippet uses Unicode replacement characters and won't work as-is — hand-edit before pasting"
        );
    } else if leading_dash {
        // Shell tokenizes the snippet's `command` as argv, so a path
        // starting with `-` lands as a flag at re-invocation. lexopt
        // rejects the unknown flag and the statusline silently fails
        // every Claude Code tick.
        let _ = writeln!(
            stderr,
            "linesmith: warning: --config path starts with `-` ({}); the emitted snippet would be parsed as a flag at re-invocation. Hand-edit the snippet's `command` field before pasting: prefix the path with `./`, use an absolute path, or insert `--` before it",
            p.display()
        );
    } else if needs_quoting {
        let _ = writeln!(
            stderr,
            "linesmith: warning: --config path contains characters that need shell quoting ({}); add quotes around the path in the snippet's `command` field before pasting",
            p.display()
        );
    }
}

/// Characters that change shell tokenization away from "one
/// whitespace-free token." Conservative: the goal is flagging paths
/// the user would otherwise paste and find broken at exec time, not
/// reproducing every shell's full quoting grammar. The arm below
/// covers common POSIX metacharacters plus a useful subset of
/// cmd.exe / PowerShell specials (including cmd.exe's `^` escape and
/// `%` variable expansion). Position-sensitive cases like a leading
/// `-` are checked separately in [`warn_if_path_needs_user_edit`].
///
/// `\` and `~` are POSIX-only: `\` is the shell escape and `~` is
/// home-directory expansion. On Windows they're literal (`\` is the
/// path separator, `~` shows up in 8.3 short names like `RUNNER~1`),
/// so flagging them there would warn on every Windows tempdir path.
fn needs_shell_quoting(c: char) -> bool {
    if matches!(
        c,
        ' ' | '\t'
            | '\''
            | '"'
            | '`'
            | '$'
            | '*'
            | '?'
            | '['
            | ']'
            | '&'
            | ';'
            | '|'
            | '<'
            | '>'
            | '('
            | ')'
            | '{'
            | '}'
            | '#'
            | '!'
            | '='
            | '^'
            | '%'
    ) {
        return true;
    }
    #[cfg(not(windows))]
    {
        matches!(c, '\\' | '~')
    }
    #[cfg(windows)]
    {
        false
    }
}

/// Inject `theme = "<name>"` into a preset body before the first table
/// header. `default` is the implicit choice and skipped, so `init`
/// with the default theme writes the same bytes as `presets apply
/// <name>`. See
/// `init_default_theme_omits_theme_field_to_match_presets_apply` for
/// the regression guard: pinning byte equality on the common path
/// makes the rare-path escape bug obvious.
fn compose_init_body(preset_body: &str, theme: &str) -> String {
    if theme == "default" {
        return preset_body.to_string();
    }
    let escaped = theme.replace('\\', "\\\\").replace('"', "\\\"");
    let theme_line = format!("theme = \"{escaped}\"\n\n");
    // A preset starting with `[` at byte 0 has no preceding newline,
    // so `find("\n[")` would miss it. Treat both shapes as "table
    // header at the top" and prepend before it.
    if preset_body.starts_with('[') {
        return format!("{theme_line}{preset_body}");
    }
    if let Some(idx) = preset_body.find("\n[") {
        // Keep the preset's leading comment block above the injected
        // `theme = ...` line.
        let split = idx + 1;
        let mut out = String::with_capacity(preset_body.len() + theme_line.len());
        out.push_str(&preset_body[..split]);
        out.push_str(&theme_line);
        out.push_str(&preset_body[split..]);
        out
    } else {
        // Degenerate preset with no tables: prepend so `theme` still
        // appears at top-level position.
        format!("{theme_line}{preset_body}")
    }
}

/// CLI-side wrapper around [`runtime::plugins::load_plugins`]:
/// builds the runtime `XdgEnv` from `CliEnv`, calls the predicate,
/// streams plugin-load errors to stderr, and returns the registry
/// plus the error count so `check_config` can fold the count into
/// its summary without re-parsing the stderr stream.
fn load_plugins(
    cfg: Option<&config::Config>,
    env: &CliEnv,
    stderr: &mut dyn Write,
) -> (
    Option<(PluginRegistry, std::sync::Arc<rhai::Engine>)>,
    usize,
) {
    let xdg_env = cli_env_to_xdg(env);
    let Some((registry, engine)) = runtime::plugins::load_plugins(cfg, &xdg_env) else {
        return (None, 0);
    };
    let errors = registry.load_errors();
    let error_count = errors.len();
    for err in errors {
        let _ = writeln!(stderr, "linesmith: plugin: {err}");
    }
    (Some((registry, engine)), error_count)
}

/// Build the runtime's `XdgEnv` snapshot from this caller's
/// [`CliEnv`]. Centralized so every runtime predicate that needs
/// XDG inputs gets the same conversion, and so `linesmith-cli`
/// keeps owning the `CliEnv → XdgEnv` map after the workspace
/// split (per ADR-0018).
fn cli_env_to_xdg(env: &CliEnv) -> crate::data_context::xdg::XdgEnv {
    crate::data_context::xdg::XdgEnv::from_os_options(
        None,
        env.xdg_config_home.clone().map(std::ffi::OsString::from),
        env.home.clone().map(std::ffi::OsString::from),
    )
}

fn run_cli(
    args: cli::CliArgs,
    stdin: impl Read,
    stdout: &mut dyn Write,
    stderr: &mut dyn Write,
    env: &CliEnv,
) -> u8 {
    let resolved = config::resolve_config_path(
        args.config.clone(),
        env.linesmith_config.as_deref(),
        env.xdg_config_home.as_deref(),
        env.home.as_deref(),
    );
    let (cfg, load_error, config_warnings) = load_config(resolved.as_ref(), stderr);

    let registry = build_theme_registry(env, stderr);

    if args.check_config {
        return check_config(
            resolved.as_ref(),
            cfg.as_ref(),
            load_error,
            config_warnings,
            &registry,
            env,
            stderr,
        );
    }

    // Surface unknown-key warnings before rendering so they ride on the
    // same stderr stream as segment-build warnings and parse errors.
    for msg in &config_warnings {
        let _ = writeln!(stderr, "linesmith: {msg}");
    }

    let (plugins, _plugin_load_errors) = load_plugins(cfg.as_ref(), env, stderr);
    let lines = build_lines(cfg.as_ref(), plugins, |msg| {
        let _ = writeln!(stderr, "linesmith: {msg}");
    });
    // `run_lines_with_context` takes `&[&[Box<dyn Segment>]]`; map
    // each owned `Vec` to a borrowed slice once.
    let line_refs: Vec<&[Box<dyn crate::segments::Segment>]> =
        lines.iter().map(Vec::as_slice).collect();

    let raw_width = env.terminal_width.unwrap_or_else(detect_terminal_width);
    let padding = layout_options(cfg.as_ref()).map_or(0, |l| l.claude_padding);
    let width = raw_width.saturating_sub(padding);
    let theme_ref = resolve_theme(cfg.as_ref(), &registry, stderr);
    let capability = resolve_color_capability(args.color_override, env, cfg.as_ref());
    let hyperlinks = supports_hyperlinks::on(supports_hyperlinks::Stream::Stdout);
    let ctx = RunContext::new(theme_ref, capability, width, env.cwd.clone(), hyperlinks);
    if let Err(err) = run_lines_with_context(stdin, stdout, stderr, &line_refs, &ctx) {
        let _ = writeln!(stderr, "linesmith: {err}");
        return 1;
    }
    0
}

fn build_theme_registry(env: &CliEnv, stderr: &mut dyn Write) -> theme::ThemeRegistry {
    let xdg_env = cli_env_to_xdg(env);
    let dir = runtime::themes::user_themes_dir(&xdg_env);
    runtime::themes::build_theme_registry(dir.as_deref(), |msg| {
        let _ = writeln!(stderr, "linesmith: {msg}");
    })
}

fn layout_options(cfg: Option<&config::Config>) -> Option<&config::LayoutOptions> {
    cfg.and_then(|c| c.layout_options.as_ref())
}

/// Color-policy precedence chain, first match wins:
///   1. `CliEnv.color_capability` override (test escape hatch)
///   2. `--no-color` / `--force-color` CLI flag
///   3. `NO_COLOR` env var
///   4. `FORCE_COLOR` env var
///   5. `[layout_options].color` in config
///   6. default `auto` — detect via `supports-color`
fn resolve_color_capability(
    cli_override: Option<cli::ColorOverride>,
    env: &CliEnv,
    cfg: Option<&config::Config>,
) -> theme::Capability {
    if let Some(cap) = env.color_capability {
        return cap;
    }
    match cli_override {
        Some(cli::ColorOverride::Never) => return theme::Capability::None,
        Some(cli::ColorOverride::Always) => return force_color_detect(env),
        None => {}
    }
    if env.no_color {
        return theme::Capability::None;
    }
    if env.force_color {
        return force_color_detect(env);
    }
    match layout_options(cfg).map(|l| l.color).unwrap_or_default() {
        config::ColorPolicy::Never => theme::Capability::None,
        config::ColorPolicy::Always => force_color_detect(env),
        // Auto plus any future variant routes to terminal detection.
        // ColorPolicy is `#[non_exhaustive]` per ADR-0018, so the
        // catch-all also handles unknown variants — auto-detect is
        // the safe degradation while the cli wires explicit handling.
        _ => theme::Capability::from_terminal(),
    }
}

/// Under "force color" intent, pick the richest tier either the
/// `supports-color` TTY probe or the `COLORTERM` / `TERM` snapshot
/// on `CliEnv` can justify. See `theme::Capability::force_from` for
/// the full semantics (including the `Palette16` floor that overrides
/// `TERM=dumb` when the user explicitly asks for color).
///
/// The env branch is load-bearing for Claude Code: CC spawns linesmith
/// with piped stdout, so `supports-color` returns `None`; without the
/// env rescue, `color = "always"` would collapse to `Palette16` and
/// every TrueColor theme would render as its 16-color downgrade.
///
/// Reads env only from `CliEnv`, never from the live process, so
/// tests and embedders stay hermetic.
fn force_color_detect(env: &CliEnv) -> theme::Capability {
    theme::Capability::force_from(
        theme::Capability::from_terminal(),
        theme::Capability::from_env_vars(env.colorterm.as_deref(), env.term.as_deref()),
    )
}

/// Resolve the active theme from config. Unknown names fall back to
/// `default` with a stderr warning; missing or empty `theme` uses
/// the default silently.
fn resolve_theme<'a>(
    cfg: Option<&config::Config>,
    registry: &'a theme::ThemeRegistry,
    stderr: &mut dyn Write,
) -> &'a theme::Theme {
    let Some(name) = cfg
        .and_then(|c| c.theme.as_deref())
        .filter(|n| !n.is_empty())
    else {
        return registry
            .lookup("default")
            .expect("default theme is always in the registry");
    };
    match registry.lookup(name) {
        Some(t) => t,
        None => {
            let _ = writeln!(stderr, "linesmith: unknown theme '{name}'; using 'default'");
            registry
                .lookup("default")
                .expect("default theme is always in the registry")
        }
    }
}

/// `--check-config`-friendly wrapper around
/// [`runtime::config::load_config`]. Writes user-facing diagnostics
/// (missing-file for explicit paths, parse / I/O errors) to stderr
/// and returns the existing 3-tuple shape `run_cli` and
/// `check_config` consume.
fn load_config(
    resolved: Option<&config::ConfigPath>,
    stderr: &mut dyn Write,
) -> (
    Option<config::Config>,
    Option<config::ConfigError>,
    Vec<String>,
) {
    use runtime::config::ConfigLoadOutcome;
    match runtime::config::load_config(resolved) {
        ConfigLoadOutcome::Unresolved => (None, None, Vec::new()),
        ConfigLoadOutcome::Loaded {
            config, warnings, ..
        } => (Some(*config), None, warnings),
        ConfigLoadOutcome::NotFound { path, explicit } => {
            if explicit {
                let _ = writeln!(stderr, "linesmith: config not found at {}", path.display());
            }
            (None, None, Vec::new())
        }
        ConfigLoadOutcome::IoError {
            source, warnings, ..
        }
        | ConfigLoadOutcome::ParseError {
            source, warnings, ..
        } => {
            let _ = writeln!(stderr, "linesmith: {source}");
            (None, Some(source), warnings)
        }
        // ConfigLoadOutcome is `#[non_exhaustive]` per ADR-0018. A
        // new variant in linesmith-core without an updated mapping
        // here falls back to defaults so the CLI still renders a
        // status line — but write a stderr line so the cli/core
        // version skew is visible instead of silently producing
        // default-themed output the user can't explain.
        _ => {
            let _ = writeln!(
                stderr,
                "linesmith: unrecognized config load outcome (cli/core version skew); using defaults",
            );
            (None, None, Vec::new())
        }
    }
}

fn check_config(
    resolved: Option<&config::ConfigPath>,
    cfg: Option<&config::Config>,
    load_error: Option<config::ConfigError>,
    config_warnings: Vec<String>,
    registry: &theme::ThemeRegistry,
    env: &CliEnv,
    stderr: &mut dyn Write,
) -> u8 {
    // `--check-config` is the CI / editor contract for strict
    // validation; if we can't even resolve a config path, that's a
    // failure rather than a "use defaults" fallback.
    let Some(cp) = resolved else {
        let _ = writeln!(
            stderr,
            "linesmith: no config path (HOME and XDG_CONFIG_HOME both unset, no --config)"
        );
        return 1;
    };
    if load_error.is_some() {
        let _ = writeln!(stderr, "linesmith: config invalid ({})", cp.path.display());
        return 1;
    }
    let Some(cfg) = cfg else {
        let _ = writeln!(
            stderr,
            "linesmith: no config at {}; using built-in defaults",
            cp.path.display()
        );
        return 0;
    };

    let mut warn_count = 0_usize;
    for msg in &config_warnings {
        let _ = writeln!(stderr, "linesmith: {msg}");
        warn_count += 1;
    }
    let (plugins, plugin_load_errors) = load_plugins(Some(cfg), env, stderr);
    warn_count += plugin_load_errors;
    // `build_lines` (not `build_segments`) so multi-line edge-case
    // warnings — `[line.foo]` non-numeric keys, single-line-with-
    // numbered-tables mode-mismatch, etc. — surface through
    // `--check-config` the same way single-line validation does.
    let _ = build_lines(Some(cfg), plugins, |msg| {
        let _ = writeln!(stderr, "linesmith: {msg}");
        warn_count += 1;
    });
    if let Some(name) = cfg.theme.as_deref().filter(|n| !n.is_empty()) {
        if registry.lookup(name).is_none() {
            let _ = writeln!(stderr, "linesmith: unknown theme '{name}'; using 'default'");
            warn_count += 1;
        }
    }
    let _ = writeln!(stderr, "linesmith: config ok ({})", cp.path.display());
    if warn_count > 0 {
        let _ = writeln!(stderr, "linesmith: {warn_count} warning(s)");
    }
    0
}

#[cfg(test)]
mod tests {
    //! Drive the whole CLI entry point (`cli_main`) with fake IO and a
    //! hand-built env. These tests lock exit codes and stderr contents
    //! end-to-end. Integration tests in `tests/integration.rs` exercise
    //! the same binary flow via `run_with_width`.

    use super::*;
    use std::io;
    use std::io::Cursor;

    /// Run `cli_main` with the given args + stdin; return
    /// `(exit_code, stdout, stderr)` as UTF-8 strings.
    fn run_cli_main(args: &[&str], stdin: &[u8], env: &CliEnv) -> (u8, String, String) {
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        let args_owned: Vec<std::ffi::OsString> =
            args.iter().map(std::ffi::OsString::from).collect();
        let code = cli_main(
            args_owned,
            Cursor::new(stdin),
            &mut stdout,
            &mut stderr,
            env,
        );
        (
            code,
            String::from_utf8(stdout).expect("stdout utf8"),
            String::from_utf8(stderr).expect("stderr utf8"),
        )
    }

    // --- meta actions ---

    #[test]
    fn help_flag_prints_help_to_stdout_and_exits_zero() {
        let (code, stdout, stderr) = run_cli_main(&["--help"], b"", &CliEnv::for_tests());
        assert_eq!(code, 0);
        assert_eq!(stdout, cli::HELP);
        assert!(stderr.is_empty());
    }

    #[test]
    fn version_flag_prints_version_to_stdout_and_exits_zero() {
        let (code, stdout, stderr) = run_cli_main(&["--version"], b"", &CliEnv::for_tests());
        assert_eq!(code, 0);
        assert_eq!(stdout, format!("linesmith {}\n", env!("CARGO_PKG_VERSION")));
        assert!(stderr.is_empty());
    }

    #[test]
    fn meta_flags_skip_terminal_width_detection() {
        // With terminal_width: None, the render path probes COLUMNS /
        // the TTY; meta commands must not, so a broken COLUMNS can't
        // leak a width warning into --help / --version / --check-config
        // stderr. `CliEnv::default()` would warn *if* detection ran.
        let (code, _stdout, stderr) = run_cli_main(&["--help"], b"", &CliEnv::default());
        assert_eq!(code, 0);
        assert!(
            stderr.is_empty(),
            "meta flag leaked width-detect output: {stderr}"
        );
    }

    #[test]
    fn unknown_flag_exits_two_and_prints_hint_to_stderr() {
        let (code, stdout, stderr) = run_cli_main(&["--nope"], b"", &CliEnv::for_tests());
        assert_eq!(code, 2);
        assert!(stdout.is_empty());
        assert!(stderr.contains("nope"));
        assert!(stderr.contains("Try --help for usage."));
    }

    #[test]
    fn empty_config_value_exits_two() {
        // Shell-expansion guard: `--config ""` from `--config "$UNSET"`
        // must not silently fall through to defaults.
        let (code, _stdout, stderr) = run_cli_main(&["--config", ""], b"", &CliEnv::for_tests());
        assert_eq!(code, 2);
        assert!(stderr.contains("Try --help"));
    }

    // --- render flow ---

    #[test]
    fn minimal_payload_round_trips_through_cli_main() {
        let json = br#"{
            "model": { "display_name": "Claude Test" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let (code, stdout, stderr) = run_cli_main(&[], json, &CliEnv::for_tests());
        assert_eq!(code, 0);
        assert_eq!(stdout, "Claude Test linesmith\n");
        assert!(stderr.is_empty());
    }

    #[test]
    fn malformed_json_renders_marker_and_routes_parse_error_to_injected_stderr() {
        // Locks the stderr plumbing: parse errors must surface on the
        // caller's stderr sink, not the real process stderr.
        let (code, stdout, stderr) = run_cli_main(&[], b"{not json", &CliEnv::for_tests());
        assert_eq!(code, 0);
        assert_eq!(stdout, "?\n");
        assert!(
            stderr.contains("parse:"),
            "expected parse diag, got: {stderr}"
        );
    }

    #[test]
    fn render_io_error_exits_one() {
        // Hand cli_main a stdout writer that fails, and confirm the
        // render path returns 1 rather than 0 or 2. Without this test
        // the exit code can silently regress to SUCCESS.
        struct FailingWriter;
        impl Write for FailingWriter {
            fn write(&mut self, _: &[u8]) -> io::Result<usize> {
                Err(io::Error::new(io::ErrorKind::BrokenPipe, "closed"))
            }
            fn flush(&mut self) -> io::Result<()> {
                Ok(())
            }
        }

        let json = br#"{"model":{"display_name":"Claude"},"workspace":{"project_dir":"/x"}}"#;
        let mut stderr = Vec::new();
        let env = CliEnv::for_tests();
        let code = cli_main(
            Vec::<std::ffi::OsString>::new(),
            Cursor::new(json),
            &mut FailingWriter,
            &mut stderr,
            &env,
        );
        assert_eq!(code, 1);
        let stderr_str = String::from_utf8(stderr).expect("utf8");
        assert!(stderr_str.contains("linesmith:"), "got: {stderr_str}");
    }

    #[test]
    fn explicit_config_path_drives_render_not_just_check() {
        // `check_config_with_valid_file_exits_zero_and_reports_ok`
        // proves the path reaches --check-config; this proves it also
        // drives the render path, so a regression that validates but
        // then discards `resolved` gets caught.
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[line]\nsegments = [\"workspace\", \"model\"]\n").unwrap();
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let (code, stdout, _stderr) = run_cli_main(
            &["--config", path.to_str().unwrap()],
            json,
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        // Config reordered segments: workspace before model.
        assert_eq!(stdout, "linesmith Claude\n");
    }

    // --- --check-config exit-code contract (docs/specs/config.md) ---

    #[test]
    fn check_config_with_no_resolvable_path_exits_one() {
        // HOME, XDG_CONFIG_HOME, and LINESMITH_CONFIG all unset, no
        // --config flag: resolve_config_path returns None and
        // --check-config treats that as a failure rather than "use
        // defaults."
        let (code, stdout, stderr) = run_cli_main(&["--check-config"], b"", &CliEnv::for_tests());
        assert_eq!(code, 1);
        assert!(stdout.is_empty());
        assert!(stderr.contains("no config path"));
    }

    #[test]
    fn check_config_with_valid_file_exits_zero_and_reports_ok() {
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[line]\nsegments = [\"model\", \"workspace\"]\n").unwrap();
        let (code, stdout, stderr) = run_cli_main(
            &["--check-config", "--config", path.to_str().unwrap()],
            b"",
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert!(stdout.is_empty());
        assert!(stderr.contains("config ok"));
        assert!(stderr.contains(path.to_str().unwrap()));
    }

    #[test]
    fn check_config_with_malformed_toml_exits_one_and_reports_invalid() {
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[line\nsegments =").unwrap();
        let (code, stdout, stderr) = run_cli_main(
            &["--check-config", "--config", path.to_str().unwrap()],
            b"",
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 1);
        assert!(stdout.is_empty());
        assert!(stderr.contains("config invalid"));
    }

    #[test]
    fn check_config_with_missing_explicit_path_warns_but_exits_zero() {
        // `--check-config` only fails when the path is *unresolvable*
        // (no env anywhere) or the file parses as invalid. A missing
        // explicit path reports "not found" and falls back to defaults
        // with SUCCESS.
        let dir = tempdir();
        let missing = dir.path().join("nonexistent.toml");
        let (code, _stdout, stderr) = run_cli_main(
            &["--check-config", "--config", missing.to_str().unwrap()],
            b"",
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert!(stderr.contains("config not found"));
        assert!(stderr.contains("using built-in defaults"));
    }

    #[test]
    fn check_config_surfaces_validation_warnings() {
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            "[line]\nsegments = [\"model\", \"does_not_exist\"]\n",
        )
        .unwrap();
        let (code, _stdout, stderr) = run_cli_main(
            &["--check-config", "--config", path.to_str().unwrap()],
            b"",
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert!(stderr.contains("does_not_exist"));
        assert!(stderr.contains("1 warning(s)"));
    }

    #[test]
    fn check_config_catches_unknown_top_level_key() {
        // Typo at top level: `thme` should warn and count toward the
        // summary so CI gates catch it.
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "thme = \"default\"\n").unwrap();
        let (code, _stdout, stderr) = run_cli_main(
            &["--check-config", "--config", path.to_str().unwrap()],
            b"",
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert!(stderr.contains("thme"));
        assert!(stderr.contains("1 warning(s)"));
    }

    #[test]
    fn check_config_catches_unknown_segment_override_key() {
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[segments.model]\npriorty = 16\n").unwrap();
        let (code, _stdout, stderr) = run_cli_main(
            &["--check-config", "--config", path.to_str().unwrap()],
            b"",
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert!(stderr.contains("priorty"));
        assert!(stderr.contains("[segments.model]"));
        assert!(stderr.contains("1 warning(s)"));
    }

    #[test]
    fn check_config_counts_warnings_across_all_three_scopes() {
        // One typo each at top-level, [layout_options], and
        // [segments.<id>]: the summary must tally all three so a CI
        // gate grepping the count catches the full set.
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            "thme = \"oops\"\n[layout_options]\nseparatr = \"x\"\n[segments.model]\npriorty = 1\n",
        )
        .unwrap();
        let (code, _stdout, stderr) = run_cli_main(
            &["--check-config", "--config", path.to_str().unwrap()],
            b"",
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert!(stderr.contains("thme"));
        assert!(stderr.contains("separatr"));
        assert!(stderr.contains("priorty"));
        assert!(stderr.contains("3 warning(s)"));
    }

    #[test]
    fn unknown_key_warnings_emit_once_per_typo_on_render_path() {
        // Pins the early-return at `if args.check_config { return ... }`:
        // the render path's pre-render loop and check_config's loop
        // must not double-emit for the same typo.
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "thme = \"oops\"\n").unwrap();
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let (code, _stdout, stderr) = run_cli_main(
            &["--config", path.to_str().unwrap()],
            json,
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert_eq!(
            stderr.matches("thme").count(),
            1,
            "unknown-key warning double-emitted: {stderr}"
        );
    }

    #[test]
    fn render_path_surfaces_unknown_key_warnings_on_stderr() {
        // Render flow must still see unknown-key warnings even though
        // no `--check-config` summary runs.
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "thme = \"oops\"\n").unwrap();
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let (code, stdout, stderr) = run_cli_main(
            &["--config", path.to_str().unwrap()],
            json,
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert_eq!(stdout, "Claude linesmith\n");
        assert!(stderr.contains("thme"));
    }

    #[test]
    fn check_config_catches_unknown_theme_name() {
        // Without this, a typo like `theme = "defualt"` only surfaces on
        // render. `--check-config` is the CI/editor contract, so it must
        // catch unknown theme names too.
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "theme = \"defualt\"\n").unwrap();
        let (code, _stdout, stderr) = run_cli_main(
            &["--check-config", "--config", path.to_str().unwrap()],
            b"",
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert!(stderr.contains("unknown theme 'defualt'"));
        assert!(stderr.contains("1 warning(s)"));
    }

    // --- CliEnv plumbing ---

    #[test]
    fn cli_env_routes_home_through_to_config_resolution() {
        // Proves env.home actually reaches resolve_config_path rather
        // than getting shadowed by a process env::var read.
        let dir = tempdir();
        let cfg_dir = dir.path().join(".config/linesmith");
        std::fs::create_dir_all(&cfg_dir).unwrap();
        std::fs::write(
            cfg_dir.join("config.toml"),
            "[line]\nsegments = [\"model\"]\n",
        )
        .unwrap();

        let env = CliEnv {
            home: Some(dir.path().to_string_lossy().into_owned()),
            ..CliEnv::for_tests()
        };
        let (code, _stdout, stderr) = run_cli_main(&["--check-config"], b"", &env);
        assert_eq!(code, 0);
        assert!(stderr.contains("config ok"));
    }

    #[test]
    fn cli_env_xdg_takes_precedence_over_home_in_resolution() {
        let dir = tempdir();
        let xdg_cfg = dir.path().join("xdg/linesmith");
        std::fs::create_dir_all(&xdg_cfg).unwrap();
        std::fs::write(xdg_cfg.join("config.toml"), "[line]\nsegments = []\n").unwrap();

        let env = CliEnv {
            xdg_config_home: Some(dir.path().join("xdg").to_string_lossy().into_owned()),
            home: Some("/nowhere/that/exists".to_string()),
            ..CliEnv::for_tests()
        };
        let (code, _stdout, stderr) = run_cli_main(&["--check-config"], b"", &env);
        assert_eq!(code, 0);
        assert!(stderr.contains(dir.path().join("xdg").to_str().unwrap()));
    }

    // --- theme wiring ---

    #[test]
    fn default_theme_under_palette16_wraps_segments_with_sgr() {
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let env = CliEnv {
            color_capability: Some(theme::Capability::Palette16),
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) = run_cli_main(&[], json, &env);
        assert_eq!(code, 0);
        // Model (Primary → BrightMagenta = SGR 95) and workspace (Info →
        // BrightCyan = SGR 96) each get wrapped; plain text between them
        // is a single space separator.
        assert_eq!(stdout, "\x1b[95mClaude\x1b[0m \x1b[96mlinesmith\x1b[0m\n");
    }

    #[test]
    fn minimal_theme_under_palette16_emits_no_color() {
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "theme = \"minimal\"\n").unwrap();

        let env = CliEnv {
            color_capability: Some(theme::Capability::Palette16),
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0);
        // Minimal theme has NoColor for every role; segments don't have
        // bold/italic decorations, so output is plain.
        assert_eq!(stdout, "Claude linesmith\n");
    }

    #[test]
    fn multi_line_config_renders_one_writeln_per_line() {
        // End-to-end multi-line: config declares two [line.N]
        // sub-tables; stdout must carry two newline-separated rows
        // with each line's segments resolved independently. Pin the
        // exact bytes (not just newline count) so a regression that
        // crosses the per-line boundary surfaces.
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/proj" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            r#"
                layout = "multi-line"
                [line.1]
                segments = ["model"]
                [line.2]
                segments = ["workspace"]
            "#,
        )
        .unwrap();
        let env = CliEnv::for_tests();
        let (code, stdout, stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0, "stderr:\n{stderr}");
        assert_eq!(stdout, "Claude\nproj\n");
    }

    #[test]
    fn multi_line_config_renders_lines_in_parsed_integer_order() {
        // BTreeMap key order is lexicographic on strings; the builder
        // sorts numerically. Pin the through-the-driver behavior so
        // a regression in either layer surfaces here.
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/proj" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            r#"
                layout = "multi-line"
                [line.10]
                segments = ["workspace"]
                [line.2]
                segments = ["model"]
            "#,
        )
        .unwrap();
        let env = CliEnv::for_tests();
        let (code, stdout, _stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0);
        assert_eq!(stdout, "Claude\nproj\n");
    }

    #[test]
    fn multi_line_with_no_numbered_tables_falls_back_to_single_line_render() {
        // Spec edge case: the fallback warning surfaces on stderr
        // and rendering uses [line].segments, producing one stdout
        // line (not zero, not two).
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/x" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            r#"
                layout = "multi-line"
                [line]
                segments = ["model"]
            "#,
        )
        .unwrap();
        let env = CliEnv::for_tests();
        let (code, stdout, stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0);
        assert_eq!(stdout, "Claude\n", "expected exactly one line");
        assert!(
            stderr.contains("no usable [line.N]"),
            "fallback warning should reach stderr, got:\n{stderr}"
        );
    }

    #[test]
    fn check_config_surfaces_multi_line_validation_warnings() {
        // `--check-config` consumes `build_lines` (not just
        // `build_segments`) so multi-line edge-case warnings
        // ([line.foo], single-line+numbered, etc.) flow through the
        // editor-facing validator, not just the render path.
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            r#"
                layout = "multi-line"
                [line.1]
                segments = ["model"]
                [line.foo]
                segments = ["bogus"]
            "#,
        )
        .unwrap();
        let env = CliEnv::for_tests();
        let (_code, _stdout, stderr) = run_cli_main(
            &["--config", path.to_str().unwrap(), "--check-config"],
            b"",
            &env,
        );
        assert!(
            stderr.contains("[line.foo]") && stderr.contains("not a positive integer"),
            "non-numeric-key warning should reach --check-config, got:\n{stderr}"
        );
    }

    #[test]
    fn multi_line_parse_failure_emits_one_question_marker_not_per_line() {
        // The render loop iterates per line, but parse failure
        // returns before the loop runs and emits a single `?\n`.
        // Without this pin, a refactor that moves the parse into the
        // loop would silently spam `?\n?\n?\n` for an N-line config.
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            r#"
                layout = "multi-line"
                [line.1]
                segments = ["model"]
                [line.2]
                segments = ["workspace"]
                [line.3]
                segments = ["context_window"]
            "#,
        )
        .unwrap();
        let env = CliEnv::for_tests();
        let (code, stdout, stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], b"{not json", &env);
        assert_eq!(code, 0, "parse failure renders the marker, exits 0");
        assert_eq!(stdout, "?\n", "exactly one marker, not one per line");
        assert!(
            stderr.contains("parse:"),
            "parse error should breadcrumb to stderr, got:\n{stderr}"
        );
    }

    #[test]
    fn multi_line_empty_segments_in_a_line_still_emits_trailing_newline() {
        // The doc on `run_lines_with_context` says explicitly-defined
        // line slots stay in the output even if no segments rendered.
        // Pin the byte count so a refactor that "optimizes" away the
        // empty-line writeln doesn't silently change vertical
        // footprint.
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/x" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            r#"
                layout = "multi-line"
                [line.1]
                segments = ["model"]
                [line.2]
                segments = []
            "#,
        )
        .unwrap();
        let env = CliEnv::for_tests();
        let (code, stdout, _stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0);
        assert_eq!(
            stdout, "Claude\n\n",
            "empty line slot must still emit `\\n`"
        );
    }

    #[test]
    fn power_user_preset_renders_two_lines_end_to_end() {
        // The preset-shape test in presets/mod.rs pins layout +
        // per-line ids; this one pins the full pipeline (preset →
        // builder → renderer) actually produces two lines. Catches
        // a regression where a refactor breaks any layer in the
        // chain.
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/proj" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(
            &path,
            presets::body("power-user").expect("preset registered"),
        )
        .unwrap();
        let env = CliEnv::for_tests();
        let (code, stdout, _stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0);
        let lines: Vec<&str> = stdout.lines().collect();
        assert_eq!(
            lines.len(),
            2,
            "power-user must render exactly two lines, got: {stdout:?}"
        );
        assert!(
            lines[0].contains("Claude"),
            "line 1 should carry model name, got: {:?}",
            lines[0]
        );
        assert!(
            lines[0].contains("proj"),
            "line 1 should carry workspace name, got: {:?}",
            lines[0]
        );
    }

    #[test]
    fn unknown_theme_falls_back_to_default_with_warning() {
        let json = br#"{
            "model": { "display_name": "C" },
            "workspace": { "project_dir": "/x" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "theme = \"nonexistent\"\n").unwrap();

        let env = CliEnv {
            color_capability: Some(theme::Capability::None),
            ..CliEnv::for_tests()
        };
        let (code, stdout, stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0);
        assert_eq!(stdout, "C x\n");
        assert!(stderr.contains("unknown theme 'nonexistent'"));
        assert!(stderr.contains("using 'default'"));
    }

    #[test]
    fn catppuccin_mocha_renders_with_mocha_palette_under_truecolor() {
        // End-to-end contract: `theme = "catppuccin-mocha"` in config +
        // TrueColor capability emits the Mocha palette's exact RGB
        // values. Model (Primary → mauve: 203,166,247); workspace
        // (Info → teal: 148,226,213). If this snapshot fails after a
        // `catppuccin` crate bump, the upstream palette drifted and the
        // theme file deserves a deliberate review.
        let json = br#"{
            "model": { "display_name": "C" },
            "workspace": { "project_dir": "/x" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "theme = \"catppuccin-mocha\"\n").unwrap();
        let env = CliEnv {
            color_capability: Some(theme::Capability::TrueColor),
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0);
        assert_eq!(
            stdout,
            "\x1b[38;2;203;166;247mC\x1b[0m \x1b[38;2;148;226;213mx\x1b[0m\n"
        );
    }

    // --- user theme loading ---

    #[test]
    fn user_theme_from_disk_renders_with_configured_palette() {
        let dir = tempdir();
        let themes_dir = dir.path().join(".config/linesmith/themes");
        std::fs::create_dir_all(&themes_dir).unwrap();
        std::fs::write(
            themes_dir.join("neon.toml"),
            r##"
                name = "neon"
                [roles]
                foreground = "#ffffff"
                background = "#000000"
                muted      = "#888888"
                primary    = "#ff00ff"
                accent     = "#00ffff"
                success    = "#00ff00"
                warning    = "#ffff00"
                error      = "#ff0000"
                info       = "#0080ff"
            "##,
        )
        .unwrap();

        let cfg_dir = dir.path().join(".config/linesmith");
        std::fs::write(cfg_dir.join("config.toml"), "theme = \"neon\"\n").unwrap();

        let json = br#"{
            "model": { "display_name": "C" },
            "workspace": { "project_dir": "/x" }
        }"#;
        let env = CliEnv {
            home: Some(dir.path().to_string_lossy().into_owned()),
            color_capability: Some(theme::Capability::TrueColor),
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) = run_cli_main(&[], json, &env);
        assert_eq!(code, 0);
        // Primary (#ff00ff = 255,0,255) wraps model; Info (#0080ff =
        // 0,128,255) wraps workspace.
        assert_eq!(
            stdout,
            "\x1b[38;2;255;0;255mC\x1b[0m \x1b[38;2;0;128;255mx\x1b[0m\n"
        );
    }

    #[test]
    fn unknown_user_theme_name_falls_back_to_default_with_warning() {
        let dir = tempdir();
        std::fs::create_dir_all(dir.path().join(".config/linesmith/themes")).unwrap();
        let cfg_dir = dir.path().join(".config/linesmith");
        std::fs::create_dir_all(&cfg_dir).unwrap();
        std::fs::write(cfg_dir.join("config.toml"), "theme = \"nonexistent\"\n").unwrap();
        let env = CliEnv {
            home: Some(dir.path().to_string_lossy().into_owned()),
            ..CliEnv::for_tests()
        };
        let json = br#"{
            "model": { "display_name": "C" },
            "workspace": { "project_dir": "/x" }
        }"#;
        let (code, stdout, stderr) = run_cli_main(&[], json, &env);
        assert_eq!(code, 0);
        assert_eq!(stdout, "C x\n");
        assert!(stderr.contains("unknown theme 'nonexistent'"));
    }

    #[test]
    fn broken_user_theme_file_warns_but_does_not_abort_startup() {
        let dir = tempdir();
        let themes_dir = dir.path().join(".config/linesmith/themes");
        std::fs::create_dir_all(&themes_dir).unwrap();
        std::fs::write(themes_dir.join("broken.toml"), "not valid toml [[").unwrap();
        let env = CliEnv {
            home: Some(dir.path().to_string_lossy().into_owned()),
            ..CliEnv::for_tests()
        };
        let json = br#"{
            "model": { "display_name": "C" },
            "workspace": { "project_dir": "/x" }
        }"#;
        let (code, stdout, stderr) = run_cli_main(&[], json, &env);
        assert_eq!(code, 0);
        assert_eq!(stdout, "C x\n");
        assert!(stderr.contains("broken.toml"));
    }

    #[test]
    fn check_config_accepts_user_theme_name() {
        // Regression guard: validation used to consult only built-ins,
        // so a `theme = "myuser"` that exists on disk was flagged.
        let dir = tempdir();
        let themes_dir = dir.path().join(".config/linesmith/themes");
        std::fs::create_dir_all(&themes_dir).unwrap();
        std::fs::write(
            themes_dir.join("myuser.toml"),
            r##"
                name = "myuser"
                [roles]
                foreground = "#ffffff"
                background = "#000000"
                muted      = "#888888"
                primary    = "#ff00ff"
                accent     = "#00ffff"
                success    = "#00ff00"
                warning    = "#ffff00"
                error      = "#ff0000"
                info       = "#0080ff"
            "##,
        )
        .unwrap();
        let cfg_dir = dir.path().join(".config/linesmith");
        std::fs::write(cfg_dir.join("config.toml"), "theme = \"myuser\"\n").unwrap();
        let env = CliEnv {
            home: Some(dir.path().to_string_lossy().into_owned()),
            ..CliEnv::for_tests()
        };
        let (code, _stdout, stderr) = run_cli_main(&["--check-config"], b"", &env);
        assert_eq!(code, 0);
        assert!(stderr.contains("config ok"));
        assert!(!stderr.contains("unknown theme"));
    }

    // --- themes list subcommand ---

    #[test]
    fn themes_list_prints_every_built_in_to_stdout() {
        let (code, stdout, _stderr) = run_cli_main(&["themes", "list"], b"", &CliEnv::for_tests());
        assert_eq!(code, 0);
        for name in [
            "default",
            "minimal",
            "catppuccin-latte",
            "catppuccin-frappe",
            "catppuccin-macchiato",
            "catppuccin-mocha",
        ] {
            assert!(
                stdout.contains(&format!("{name}\tbuilt-in")),
                "missing '{name}\\tbuilt-in' in:\n{stdout}"
            );
        }
    }

    #[test]
    fn themes_list_includes_user_themes_with_source_path() {
        let dir = tempdir();
        // Separator-normalized join to match the XDG cascade's
        // chained `.join` form on Windows — substring match against
        // cascade-rendered output requires identical separators.
        let themes_dir = dir.path().join(".config").join("linesmith").join("themes");
        std::fs::create_dir_all(&themes_dir).unwrap();
        let user_theme = themes_dir.join("neon.toml");
        std::fs::write(
            &user_theme,
            r##"
                name = "neon"
                [roles]
                foreground = "#ffffff"
                background = "#000000"
                muted      = "#888888"
                primary    = "#ff00ff"
                accent     = "#00ffff"
                success    = "#00ff00"
                warning    = "#ffff00"
                error      = "#ff0000"
                info       = "#0080ff"
            "##,
        )
        .unwrap();
        let env = CliEnv {
            home: Some(dir.path().to_string_lossy().into_owned()),
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) = run_cli_main(&["themes", "list"], b"", &env);
        assert_eq!(code, 0);
        assert!(
            stdout.contains("neon\t"),
            "missing user theme line:\n{stdout}"
        );
        assert!(
            stdout.contains(user_theme.to_str().unwrap()),
            "missing source path in:\n{stdout}"
        );
    }

    #[test]
    fn user_themes_dir_prefers_xdg_over_home() {
        // Both env vars set, different themes in each dir: the one
        // under $XDG_CONFIG_HOME/linesmith/themes wins. Without this
        // test, a swap of the precedence (or a bug in the empty-filter
        // on xdg_config_home) slips through silently.
        let dir = tempdir();
        let xdg_themes = dir.path().join("xdg/linesmith/themes");
        let home_themes = dir.path().join("home/.config/linesmith/themes");
        std::fs::create_dir_all(&xdg_themes).unwrap();
        std::fs::create_dir_all(&home_themes).unwrap();
        std::fs::write(
            xdg_themes.join("xdg_theme.toml"),
            r##"
                name = "xdgonly"
                [roles]
                foreground = "#aaaaaa"
                background = "#000000"
                muted = "#888888"
                primary = "#ff00ff"
                accent = "#00ffff"
                success = "#00ff00"
                warning = "#ffff00"
                error = "#ff0000"
                info = "#0080ff"
            "##,
        )
        .unwrap();
        std::fs::write(
            home_themes.join("home_theme.toml"),
            r##"
                name = "homeonly"
                [roles]
                foreground = "#bbbbbb"
                background = "#000000"
                muted = "#888888"
                primary = "#ff00ff"
                accent = "#00ffff"
                success = "#00ff00"
                warning = "#ffff00"
                error = "#ff0000"
                info = "#0080ff"
            "##,
        )
        .unwrap();
        let env = CliEnv {
            xdg_config_home: Some(dir.path().join("xdg").to_string_lossy().into_owned()),
            home: Some(dir.path().join("home").to_string_lossy().into_owned()),
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) = run_cli_main(&["themes", "list"], b"", &env);
        assert_eq!(code, 0);
        assert!(
            stdout.contains("xdgonly"),
            "XDG theme missing from list:\n{stdout}",
        );
        assert!(
            !stdout.contains("homeonly"),
            "HOME theme leaked in when XDG was set:\n{stdout}",
        );
    }

    #[test]
    fn unknown_subcommand_exits_two() {
        let (code, _stdout, stderr) =
            run_cli_main(&["bogus", "command"], b"", &CliEnv::for_tests());
        assert_eq!(code, 2);
        assert!(stderr.contains("Try --help"));
    }

    #[test]
    fn themes_without_subcommand_exits_two() {
        let (code, _stdout, stderr) = run_cli_main(&["themes"], b"", &CliEnv::for_tests());
        assert_eq!(code, 2);
        assert!(stderr.contains("Try --help"));
    }

    #[test]
    fn no_color_capability_strips_theme_under_default() {
        // Even the `default` theme (Palette16 values) emits nothing
        // under Capability::None. This is the NO_COLOR contract: no
        // ANSI bytes, no risk of leaking escape sequences when stdout
        // is piped to non-terminal consumers.
        let json = br#"{
            "model": { "display_name": "C" },
            "workspace": { "project_dir": "/x" }
        }"#;
        let env = CliEnv {
            color_capability: Some(theme::Capability::None),
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) = run_cli_main(&[], json, &env);
        assert_eq!(code, 0);
        assert_eq!(stdout, "C x\n");
    }

    // --- color-policy precedence ---

    /// Build a `CliEnv` suitable for driving the resolver directly —
    /// the test-only capability override is cleared so the chain
    /// actually executes.
    fn policy_env() -> CliEnv {
        CliEnv {
            color_capability: None,
            ..CliEnv::for_tests()
        }
    }

    #[test]
    fn color_policy_cli_never_wins_over_force_env() {
        let env = CliEnv {
            force_color: true,
            ..policy_env()
        };
        assert_eq!(
            resolve_color_capability(Some(cli::ColorOverride::Never), &env, None),
            theme::Capability::None,
        );
    }

    #[test]
    fn color_policy_cli_always_wins_over_no_color_env() {
        let env = CliEnv {
            no_color: true,
            ..policy_env()
        };
        let got = resolve_color_capability(Some(cli::ColorOverride::Always), &env, None);
        // Falls back to Palette16 when the terminal reports None (tests
        // run without a TTY); anything ≥ Palette16 proves the force
        // path ran and didn't land on Capability::None.
        assert!(got >= theme::Capability::Palette16, "got {got:?}");
    }

    #[test]
    fn color_policy_no_color_env_wins_over_force_env() {
        // When both NO_COLOR and FORCE_COLOR are set, no-color.org's
        // rule is that NO_COLOR wins (order in the chain).
        let env = CliEnv {
            no_color: true,
            force_color: true,
            ..policy_env()
        };
        assert_eq!(
            resolve_color_capability(None, &env, None),
            theme::Capability::None,
        );
    }

    // `LayoutOptions` is `#[non_exhaustive]` in linesmith-core,
    // which blocks both literal AND struct-update construction
    // across the crate boundary (E0639). Tests build through
    // Default + field assignment instead.
    fn layout_options_with_color(color: config::ColorPolicy) -> config::LayoutOptions {
        let mut opts = config::LayoutOptions::default();
        opts.color = color;
        opts
    }

    #[test]
    fn color_policy_no_color_env_wins_over_config_always() {
        let cfg = config::Config {
            layout_options: Some(layout_options_with_color(config::ColorPolicy::Always)),
            ..config::Config::default()
        };
        let env = CliEnv {
            no_color: true,
            ..policy_env()
        };
        assert_eq!(
            resolve_color_capability(None, &env, Some(&cfg)),
            theme::Capability::None,
        );
    }

    #[test]
    fn color_policy_config_never_strips_color() {
        let cfg = config::Config {
            layout_options: Some(layout_options_with_color(config::ColorPolicy::Never)),
            ..config::Config::default()
        };
        assert_eq!(
            resolve_color_capability(None, &policy_env(), Some(&cfg)),
            theme::Capability::None,
        );
    }

    #[test]
    fn color_policy_config_always_forces_color() {
        // Mirror of the `Never` test for the other explicit branch.
        let cfg = config::Config {
            layout_options: Some(layout_options_with_color(config::ColorPolicy::Always)),
            ..config::Config::default()
        };
        let got = resolve_color_capability(None, &policy_env(), Some(&cfg));
        assert!(got >= theme::Capability::Palette16, "got {got:?}");
    }

    #[test]
    fn color_policy_config_auto_falls_through_to_terminal_detection() {
        let cfg = config::Config {
            layout_options: Some(layout_options_with_color(config::ColorPolicy::Auto)),
            ..config::Config::default()
        };
        // Without a TTY under `cargo test`, `from_terminal` returns None;
        // the assertion is just that the resolver didn't short-circuit.
        let got = resolve_color_capability(None, &policy_env(), Some(&cfg));
        assert_eq!(got, theme::Capability::from_terminal());
    }

    #[test]
    fn force_color_detect_never_returns_none() {
        // The Palette16 floor is the whole point of force_color_detect;
        // pin it directly so a regression dropping the fallback match
        // arm is visible without chasing through resolver assertions.
        // Pass an empty CliEnv so the result is deterministic regardless
        // of the host terminal.
        assert_ne!(
            force_color_detect(&CliEnv::default()),
            theme::Capability::None
        );
    }

    #[test]
    fn force_color_detect_reads_colorterm_from_cli_env_not_ambient_process() {
        // Hermeticity guard. force_color_detect must read only from
        // CliEnv; a direct std::env::var or a module-level ambient
        // reader would bypass the snapshot and make the resolver
        // non-deterministic for tests and embedders.
        let env = CliEnv {
            colorterm: Some("truecolor".to_string()),
            term: Some("xterm-ghostty".to_string()),
            ..CliEnv::default()
        };
        assert_eq!(force_color_detect(&env), theme::Capability::TrueColor);
    }

    #[test]
    fn color_policy_force_color_env_zero_is_treated_as_absent() {
        // npm / chalk / supports-color all treat FORCE_COLOR=0 as
        // "explicitly disabled", not as force-on. Our `force_color_env`
        // parse maps it to false, so `env.force_color = false` and the
        // chain falls through to config / auto rather than forcing.
        // Verify the parser itself:
        std::env::set_var("LINESMITH_FORCE_COLOR_TEST_ZERO", "0");
        assert!(!force_color_env("LINESMITH_FORCE_COLOR_TEST_ZERO"));
        std::env::set_var("LINESMITH_FORCE_COLOR_TEST_ONE", "1");
        assert!(force_color_env("LINESMITH_FORCE_COLOR_TEST_ONE"));
        std::env::set_var("LINESMITH_FORCE_COLOR_TEST_EMPTY", "");
        assert!(!force_color_env("LINESMITH_FORCE_COLOR_TEST_EMPTY"));
        std::env::remove_var("LINESMITH_FORCE_COLOR_TEST_ZERO");
        std::env::remove_var("LINESMITH_FORCE_COLOR_TEST_ONE");
        std::env::remove_var("LINESMITH_FORCE_COLOR_TEST_EMPTY");
    }

    #[test]
    fn color_policy_test_capability_override_short_circuits_everything() {
        let env = CliEnv {
            no_color: true,
            force_color: true,
            color_capability: Some(theme::Capability::Palette256),
            ..policy_env()
        };
        assert_eq!(
            resolve_color_capability(Some(cli::ColorOverride::Never), &env, None),
            theme::Capability::Palette256,
        );
    }

    // --- claude_padding ---

    #[test]
    fn claude_padding_shrinks_render_budget_and_drops_segment() {
        // Full render: "Claude linesmith" = 16 cells. Budget 20 alone
        // fits everything; padding=10 shrinks usable to 10 cells, which
        // forces the higher-priority segment (model=64) to drop so only
        // workspace (priority 16, 9 cells) survives.
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[layout_options]\nclaude_padding = 10\n").unwrap();
        let env = CliEnv {
            terminal_width: Some(20),
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0);
        assert_eq!(stdout, "linesmith\n");
    }

    #[test]
    fn claude_padding_exceeds_width_clamps_to_zero_and_drops_everything() {
        // Pathological misconfiguration: padding larger than the
        // terminal width saturates to 0 usable cells, so every
        // positive-priority segment drops. Silent clamp is the right
        // semantic — validating this at config-load would require
        // width at parse time, which we don't have.
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[layout_options]\nclaude_padding = 500\n").unwrap();
        let env = CliEnv {
            terminal_width: Some(80),
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) =
            run_cli_main(&["--config", path.to_str().unwrap()], json, &env);
        assert_eq!(code, 0);
        // All positive-priority segments drop; only the trailing
        // newline remains.
        assert_eq!(stdout, "\n");
    }

    #[test]
    fn claude_padding_zero_matches_no_padding() {
        // Explicit 0 should be indistinguishable from omitting the key.
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let dir = tempdir();
        let path = dir.path().join("config.toml");
        std::fs::write(&path, "[layout_options]\nclaude_padding = 0\n").unwrap();
        let (code, stdout, _stderr) = run_cli_main(
            &["--config", path.to_str().unwrap()],
            json,
            &CliEnv::for_tests(),
        );
        assert_eq!(code, 0);
        assert_eq!(stdout, "Claude linesmith\n");
    }

    // --- CLI flag end-to-end ---

    #[test]
    fn no_color_flag_outranks_force_color_env_end_to_end() {
        // force_color=true would yield colored output via the resolver's
        // FORCE_COLOR branch; --no-color must outrank it, proving the
        // CLI flag sits at the top of the precedence chain through the
        // full render pipeline.
        let json = br#"{
            "model": { "display_name": "Claude" },
            "workspace": { "project_dir": "/home/dev/linesmith" }
        }"#;
        let env = CliEnv {
            force_color: true,
            color_capability: None,
            ..CliEnv::for_tests()
        };
        let (code, stdout, _stderr) = run_cli_main(&["--no-color"], json, &env);
        assert_eq!(code, 0);
        assert_eq!(stdout, "Claude linesmith\n");
        assert!(!stdout.contains('\x1b'));
    }

    struct TempDir(std::path::PathBuf);

    impl TempDir {
        fn path(&self) -> &std::path::Path {
            &self.0
        }
    }

    impl Drop for TempDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    fn env_with_home(dir: &Path) -> CliEnv {
        CliEnv {
            home: Some(dir.to_string_lossy().into_owned()),
            ..CliEnv::for_tests()
        }
    }

    #[test]
    fn presets_list_prints_every_preset_name() {
        let (code, stdout, _stderr) = run_cli_main(&["presets", "list"], b"", &CliEnv::for_tests());
        assert_eq!(code, 0);
        for name in crate::presets::names() {
            assert!(stdout.contains(name), "missing '{name}' in:\n{stdout}");
        }
    }

    #[test]
    fn presets_apply_writes_parsed_config_to_resolved_path() {
        use std::str::FromStr;
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, stdout, stderr) = run_cli_main(&["presets", "apply", "minimal"], b"", &env);
        assert_eq!(code, 0, "stderr:\n{stderr}");
        let expected = dir.path().join(".config/linesmith/config.toml");
        assert!(expected.exists(), "config.toml not written");
        let written = std::fs::read_to_string(&expected).unwrap();
        let cfg = config::Config::from_str(&written).expect("round-trips");
        assert_eq!(
            cfg.line.expect("has [line]").segments,
            vec!["model".to_string(), "context_window".to_string()]
        );
        assert!(stdout.contains("wrote preset 'minimal'"));
    }

    #[test]
    fn presets_apply_unknown_name_errors_and_lists_valid() {
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_cli_main(&["presets", "apply", "bogus"], b"", &env);
        assert_eq!(code, 1);
        assert!(stderr.contains("unknown preset 'bogus'"));
        assert!(stderr.contains("developer"));
    }

    #[test]
    fn presets_apply_prompts_on_existing_config_and_accepts_y() {
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# prior content\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_cli_main(&["presets", "apply", "minimal"], b"y\n", &env);
        assert_eq!(code, 0);
        let backup = dir.path().join(".config/linesmith/config.toml.bak");
        assert!(backup.exists(), "expected .bak");
        assert_eq!(
            std::fs::read_to_string(&backup).unwrap(),
            "# prior content\n"
        );
        assert!(std::fs::read_to_string(&cfg)
            .unwrap()
            .contains("preset: minimal"));
        // Prompt + backup-success line both land on stderr so stdout
        // stays clean for pipes.
        assert!(stderr.contains("overwrite"));
        assert!(stderr.contains("backed up previous config to"));
    }

    #[test]
    fn presets_apply_prompt_rejects_on_n_and_leaves_config_untouched() {
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# prior content\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_cli_main(&["presets", "apply", "minimal"], b"n\n", &env);
        assert_eq!(code, 1);
        assert!(stderr.contains("aborted"));
        assert_eq!(std::fs::read_to_string(&cfg).unwrap(), "# prior content\n");
    }

    #[test]
    fn presets_apply_force_skips_prompt_and_backs_up() {
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# prior content\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, _stderr) =
            run_cli_main(&["presets", "apply", "developer", "--force"], b"", &env);
        assert_eq!(code, 0);
        let backup = dir.path().join(".config/linesmith/config.toml.bak");
        assert!(backup.exists());
        assert!(std::fs::read_to_string(&cfg)
            .unwrap()
            .contains("preset: developer"));
    }

    #[test]
    fn presets_apply_eof_without_force_aborts() {
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# prior\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_cli_main(&["presets", "apply", "minimal"], b"", &env);
        assert_eq!(code, 1);
        assert!(stderr.contains("aborted"));
    }

    #[test]
    fn presets_apply_refuses_to_clobber_existing_backup_without_force() {
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        let bak = dir.path().join(".config/linesmith/config.toml.bak");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# current\n").unwrap();
        std::fs::write(&bak, "# older generation\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_cli_main(&["presets", "apply", "minimal"], b"y\n", &env);
        assert_eq!(code, 1);
        assert!(stderr.contains("already exists"));
        assert!(stderr.contains("--force"));
        // Both files are untouched.
        assert_eq!(std::fs::read_to_string(&cfg).unwrap(), "# current\n");
        assert_eq!(
            std::fs::read_to_string(&bak).unwrap(),
            "# older generation\n"
        );
    }

    #[test]
    fn presets_apply_force_replaces_existing_backup() {
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        let bak = dir.path().join(".config/linesmith/config.toml.bak");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# current\n").unwrap();
        std::fs::write(&bak, "# older generation\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, _stderr) =
            run_cli_main(&["presets", "apply", "minimal", "--force"], b"", &env);
        assert_eq!(code, 0);
        assert_eq!(std::fs::read_to_string(&bak).unwrap(), "# current\n");
        assert!(std::fs::read_to_string(&cfg)
            .unwrap()
            .contains("preset: minimal"));
    }

    #[test]
    fn presets_apply_honors_explicit_config_flag_over_xdg_path() {
        let dir = tempdir();
        let custom = dir.path().join("custom-preset.toml");
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_cli_main(
            &[
                "--config",
                custom.to_str().unwrap(),
                "presets",
                "apply",
                "minimal",
            ],
            b"",
            &env,
        );
        assert_eq!(code, 0, "stderr:\n{stderr}");
        assert!(custom.exists(), "expected preset written to --config path");
        // XDG fallback must NOT have been written.
        assert!(!dir.path().join(".config/linesmith/config.toml").exists());
    }

    #[test]
    fn presets_apply_creates_missing_parent_dirs() {
        // Fresh HOME with nothing under `.config/`: the resolver
        // produces `<HOME>/.config/linesmith/config.toml` and
        // presets_apply must `create_dir_all` both intermediate dirs.
        let dir = tempdir();
        assert!(!dir.path().join(".config").exists());
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_cli_main(&["presets", "apply", "minimal"], b"", &env);
        assert_eq!(code, 0, "stderr:\n{stderr}");
        assert!(dir.path().join(".config/linesmith").is_dir());
        assert!(dir.path().join(".config/linesmith/config.toml").exists());
    }

    #[test]
    fn presets_apply_empty_name_fails_with_unknown_preset() {
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_cli_main(&["presets", "apply", ""], b"", &env);
        assert_eq!(code, 1);
        assert!(stderr.contains("unknown preset ''"));
    }

    #[test]
    fn presets_apply_write_failure_reports_stderr_and_exits_one() {
        // Parent is a regular file, so `create_dir_all` fails and the
        // write never starts. Pins the stderr-plus-exit-1 contract on
        // the I/O-failure branch without depending on filesystem
        // permissions (which vary across CI).
        let dir = tempdir();
        let not_a_dir = dir.path().join(".config/linesmith");
        std::fs::create_dir_all(not_a_dir.parent().unwrap()).unwrap();
        std::fs::write(&not_a_dir, "I am a file, not a directory").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_cli_main(&["presets", "apply", "minimal"], b"", &env);
        assert_eq!(code, 1);
        assert!(
            stderr.contains("could not create"),
            "expected 'could not create' diagnostic, got: {stderr}"
        );
    }

    #[test]
    fn presets_list_ignores_force_flag_by_rejecting_it() {
        // Pins the "force outside apply errors" contract from the CLI
        // layer through to driver behavior.
        let (code, _stdout, stderr) =
            run_cli_main(&["--force", "presets", "list"], b"", &CliEnv::for_tests());
        assert_eq!(code, 2, "CLI parse error should exit 2");
        assert!(stderr.contains("--force"));
    }

    #[test]
    fn parse_confirmation_accepts_y_yes_case_insensitive_and_trims_whitespace() {
        for ok in [
            "y", "Y", "yes", "YES", "Yes", "  y  \n", "yes\r\n", " YES \t",
        ] {
            assert!(super::parse_confirmation(ok), "expected yes for {ok:?}");
        }
        for no in ["", "\n", " ", "yeah", "ye", "no", "n", "maybe", "yess"] {
            assert!(!super::parse_confirmation(no), "expected no for {no:?}");
        }
    }

    // --- init ---

    /// Drive the testable core directly (bypassing dialoguer) with a
    /// hand-built `InitChoices`. Mirrors `run_cli_main`'s tuple shape so
    /// init tests read like the surrounding `presets_apply_*` tests.
    fn run_init(
        choices: InitChoices,
        config_override: Option<PathBuf>,
        stdin: &[u8],
        env: &CliEnv,
    ) -> (u8, String, String) {
        // Existing tests don't assert on the post-init doctor
        // output, so default to `--no-doctor`. New tests that
        // pin the doctor wiring call `run_init_with_doctor` below.
        run_init_with_doctor(
            choices,
            /* no_doctor= */ true,
            config_override,
            stdin,
            env,
        )
    }

    fn run_init_with_doctor(
        choices: InitChoices,
        no_doctor: bool,
        config_override: Option<PathBuf>,
        stdin: &[u8],
        env: &CliEnv,
    ) -> (u8, String, String) {
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        let code = super::init_with_choices(
            &choices,
            no_doctor,
            config_override,
            io::Cursor::new(stdin),
            &mut stdout,
            &mut stderr,
            env,
        );
        (
            code,
            String::from_utf8(stdout).expect("stdout utf8"),
            String::from_utf8(stderr).expect("stderr utf8"),
        )
    }

    fn init_choices(preset: &str, theme: &str) -> InitChoices {
        InitChoices {
            preset: preset.to_string(),
            theme: theme.to_string(),
        }
    }

    #[test]
    fn init_creates_valid_config_round_trips_parse() {
        use std::str::FromStr;
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, stdout, stderr) =
            run_init(init_choices("minimal", "catppuccin-mocha"), None, b"", &env);
        assert_eq!(code, 0, "stderr:\n{stderr}");
        let path = dir.path().join(".config/linesmith/config.toml");
        let written = std::fs::read_to_string(&path).expect("file exists");
        let cfg = config::Config::from_str(&written).expect("round-trips");
        assert_eq!(cfg.theme.as_deref(), Some("catppuccin-mocha"));
        assert_eq!(
            cfg.line.expect("has [line]").segments,
            vec!["model".to_string(), "context_window".to_string()]
        );
        assert!(stdout.contains("wrote config.toml to"));
    }

    #[test]
    fn init_no_doctor_writes_config_without_running_doctor() {
        // Spec §`linesmith init` integration: `--no-doctor`
        // bypasses the post-init doctor invocation. CI / scripted
        // onboarding paths use this so the network probe doesn't
        // block, and so init's exit code reflects only init.
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, stdout, stderr) = run_init_with_doctor(
            init_choices("minimal", "catppuccin-mocha"),
            /* no_doctor= */ true,
            None,
            b"",
            &env,
        );
        assert_eq!(code, 0, "stderr:\n{stderr}");
        assert!(
            stdout.contains("wrote config.toml to"),
            "init still writes the config:\n{stdout}",
        );
        assert!(
            !stdout.contains("Running doctor"),
            "doctor must NOT run with --no-doctor; got stdout:\n{stdout}",
        );
        assert!(
            !stdout.contains("linesmith doctor (v"),
            "doctor report must NOT appear with --no-doctor; got stdout:\n{stdout}",
        );
    }

    #[test]
    fn init_runs_doctor_inline_after_writing_config() {
        // Spec §`linesmith init` integration: doctor runs after
        // init writes the config, with the "Running doctor..."
        // separator line and the report appearing inline. The
        // resolved-config path threads through to doctor so it
        // inspects exactly the file we just wrote.
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (_code, stdout, _stderr) = run_init_with_doctor(
            init_choices("minimal", "catppuccin-mocha"),
            /* no_doctor= */ false,
            None,
            b"",
            &env,
        );
        assert!(
            stdout.contains("wrote config.toml to"),
            "init must write the config before invoking doctor:\n{stdout}",
        );
        assert!(
            stdout.contains("Running doctor to verify your setup"),
            "init must announce the doctor run:\n{stdout}",
        );
        assert!(
            stdout.contains("linesmith doctor (v"),
            "doctor report header must appear inline:\n{stdout}",
        );
        // Doctor reads the file we just wrote — confirm by
        // looking for the Config section's PASS line for it.
        assert!(
            stdout.contains("Config file:") && stdout.contains("config.toml"),
            "doctor should report on the freshly-written config:\n{stdout}",
        );
    }

    #[test]
    fn init_propagates_doctor_exit_code() {
        // Spec line 161: "If doctor exits non-zero, init's own
        // exit code propagates it." The healthy fixture path on a
        // tempdir-rooted $HOME doesn't have a real claude binary
        // or credentials, so the auto-doctor will FAIL — perfect
        // for pinning the propagation contract. (If doctor PASSed
        // here, init would exit 0 and we'd still cover the
        // happy-path propagation; the failure path is the one
        // that matters for the contract.)
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, _stdout, _stderr) = run_init_with_doctor(
            init_choices("minimal", "catppuccin-mocha"),
            /* no_doctor= */ false,
            None,
            b"",
            &env,
        );
        // The actual doctor outcome depends on the host's
        // PATH/keychain/etc., so accept either propagation
        // direction. What we're pinning: init's exit code is
        // *whatever doctor returned*, not hardcoded to 0.
        assert!(
            code == 0 || code == 1,
            "init should propagate doctor's exit code (0 or 1); got {code}",
        );
    }

    #[test]
    fn init_writes_to_resolved_xdg_path() {
        // Two tempdirs so XDG-vs-HOME precedence is decisive: if the
        // resolver wrongly fell through to HOME, we'd see a file in
        // `home_dir`, not `xdg_dir`.
        let xdg_dir = tempdir();
        let home_dir = tempdir();
        let env = CliEnv {
            xdg_config_home: Some(xdg_dir.path().to_string_lossy().into_owned()),
            home: Some(home_dir.path().to_string_lossy().into_owned()),
            ..CliEnv::for_tests()
        };
        let (code, _stdout, stderr) = run_init(init_choices("minimal", "default"), None, b"", &env);
        assert_eq!(code, 0, "stderr:\n{stderr}");
        assert!(
            xdg_dir.path().join("linesmith/config.toml").exists(),
            "expected config at XDG path"
        );
        assert!(
            !home_dir
                .path()
                .join(".config/linesmith/config.toml")
                .exists(),
            "HOME path should not be touched when XDG resolves"
        );
    }

    /// Pull the indented `"statusLine": { ... }` JSON-fragment block
    /// out of the init stdout and wrap it as a complete object so
    /// `serde_json` can parse it. Asserting on the parsed shape (not
    /// substring matches) catches regressions like a stray trailing
    /// comma or unbalanced brace that would silently ship as bad UX.
    fn parse_snippet(stdout: &str) -> serde_json::Value {
        let lines: Vec<&str> = stdout.lines().collect();
        let start = lines
            .iter()
            .position(|l| l.contains("\"statusLine\""))
            .expect("snippet header present");
        let end = lines[start..]
            .iter()
            .position(|l| l.trim_end() == "  }")
            .map(|i| start + i)
            .expect("snippet closing brace present");
        let body: String = lines[start..=end].join("\n");
        let wrapped = format!("{{\n{body}\n}}");
        serde_json::from_str(&wrapped)
            .unwrap_or_else(|e| panic!("snippet not valid JSON ({e}):\n{wrapped}"))
    }

    #[test]
    fn init_emits_claude_code_settings_snippet() {
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, stdout, _stderr) = run_init(init_choices("minimal", "default"), None, b"", &env);
        assert_eq!(code, 0);
        let parsed = parse_snippet(&stdout);
        let status_line = &parsed["statusLine"];
        assert_eq!(status_line["type"], "command");
        assert_eq!(status_line["command"], "linesmith");
        assert_eq!(status_line["padding"], 0);
        assert!(
            stdout.contains("settings.json"),
            "snippet should name the destination file"
        );
        assert!(
            stdout.contains("merge into the existing top-level object"),
            "merge hint missing — without it, users pasting into an empty file get invalid JSON",
        );
    }

    #[test]
    fn init_snippet_preserves_explicit_config_flag() {
        // When --config <PATH> was used, the snippet must tell Claude
        // Code to call `linesmith --config <PATH>`. A bare
        // `linesmith` would point at the XDG default and the file
        // `init` just wrote would never be read.
        let dir = tempdir();
        let custom = dir.path().join("custom-init.toml");
        let env = env_with_home(dir.path());
        let (code, stdout, _stderr) = run_init(
            init_choices("minimal", "default"),
            Some(custom.clone()),
            b"",
            &env,
        );
        assert_eq!(code, 0);
        let parsed = parse_snippet(&stdout);
        let cmd = parsed["statusLine"]["command"]
            .as_str()
            .expect("command is a string");
        assert!(
            cmd.starts_with("linesmith --config "),
            "expected `--config` in command, got: {cmd}"
        );
        assert!(
            cmd.contains(custom.to_string_lossy().as_ref()),
            "command should reference the actual --config path: {cmd}"
        );
    }

    #[test]
    fn init_snippet_preserves_env_resolved_config_path() {
        // When the user's explicit path comes from `LINESMITH_CONFIG`,
        // the snippet must include `--config <path>` just as it does
        // for the CLI flag. A bare `linesmith` snippet would
        // re-resolve to the XDG default and miss the file we just
        // wrote.
        let dir = tempdir();
        let custom = dir.path().join("env-init.toml");
        let env = CliEnv {
            linesmith_config: Some(custom.to_string_lossy().into_owned()),
            ..env_with_home(dir.path())
        };
        let (code, stdout, _stderr) = run_init(init_choices("minimal", "default"), None, b"", &env);
        assert_eq!(code, 0);
        let parsed = parse_snippet(&stdout);
        let cmd = parsed["statusLine"]["command"]
            .as_str()
            .expect("command is a string");
        assert!(
            cmd.starts_with("linesmith --config "),
            "env-resolved path should still produce --config snippet, got: {cmd}"
        );
        assert!(
            cmd.contains(custom.to_string_lossy().as_ref()),
            "command should reference the env-resolved path: {cmd}"
        );
    }

    #[test]
    fn init_snippet_uses_bare_command_for_implicit_xdg_path() {
        // The other side of the contract: when neither --config nor
        // LINESMITH_CONFIG is set, the resolved path is the XDG
        // default. Plain `linesmith` finds it without --config, so
        // the snippet stays bare (no spurious --config flag).
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, stdout, _stderr) = run_init(init_choices("minimal", "default"), None, b"", &env);
        assert_eq!(code, 0);
        let parsed = parse_snippet(&stdout);
        assert_eq!(parsed["statusLine"]["command"], "linesmith");
    }

    #[test]
    fn init_warns_about_env_resolved_path_with_spaces() {
        // The user-edit warning must also fire on env-resolved paths,
        // not just --config. Same broken-snippet failure mode
        // either way.
        let dir = tempdir();
        let custom = dir.path().join("env path with spaces.toml");
        let env = CliEnv {
            linesmith_config: Some(custom.to_string_lossy().into_owned()),
            ..env_with_home(dir.path())
        };
        let (code, _stdout, stderr) = run_init(init_choices("minimal", "default"), None, b"", &env);
        assert_eq!(code, 0);
        assert!(
            stderr.contains("characters that need shell quoting"),
            "missing shell-quoting warning for env-resolved path:\n{stderr}"
        );
    }

    // NTFS forbids `"` in filenames, so this case can't be exercised
    // on Windows; backslash escaping there is exercised by
    // `init_snippet_preserves_explicit_config_flag` (Windows tempdir
    // paths always contain `\`, and `parse_snippet` panics on invalid
    // JSON).
    #[cfg(not(windows))]
    #[test]
    fn init_snippet_escapes_quotes_and_backslashes_in_config_path() {
        // The path is interpolated into a JSON string, so backslashes
        // (Windows paths) and double quotes must be escaped or the
        // emitted snippet is invalid JSON.
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let weird = dir.path().join("weird\"path\\ok.toml");
        let (code, stdout, _stderr) = run_init(
            init_choices("minimal", "default"),
            Some(weird.clone()),
            b"",
            &env,
        );
        assert_eq!(code, 0);
        // The decisive assertion: parse_snippet panics on invalid JSON.
        let parsed = parse_snippet(&stdout);
        let cmd = parsed["statusLine"]["command"]
            .as_str()
            .expect("command is a string");
        assert!(cmd.contains(weird.to_string_lossy().as_ref()));
    }

    #[test]
    fn init_succeeds_when_both_config_and_backup_already_exist() {
        // A user who init'd once (creating config.toml.bak), then
        // init'd a second time and confirmed `y`, must not be
        // stranded on a third `init` because the .bak from run #1
        // still exists. The `y` confirmation already captured intent
        // to lose prior content, so init clobbers .bak unconditionally.
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        let bak = dir.path().join(".config/linesmith/config.toml.bak");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# current\n").unwrap();
        std::fs::write(&bak, "# older generation\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) =
            run_init(init_choices("minimal", "default"), None, b"y\n", &env);
        assert_eq!(
            code, 0,
            "init must not deadlock when .bak already exists\nstderr:\n{stderr}"
        );
        // The previously-current config moved to .bak; the older .bak
        // generation was clobbered.
        assert_eq!(std::fs::read_to_string(&bak).unwrap(), "# current\n");
        assert_eq!(
            std::fs::read_to_string(&cfg).unwrap(),
            config::with_schema_directive(presets::body("minimal").unwrap())
        );
    }

    #[test]
    fn init_user_says_no_to_overwrite_does_not_clobber_backup() {
        // Init only clobbers .bak after a confirmed `y`. An `n` must
        // leave both files untouched, or every aborted init would
        // destroy the .bak.
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        let bak = dir.path().join(".config/linesmith/config.toml.bak");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# current\n").unwrap();
        std::fs::write(&bak, "# older generation\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, _stderr) =
            run_init(init_choices("minimal", "default"), None, b"n\n", &env);
        assert_eq!(code, 1);
        assert_eq!(std::fs::read_to_string(&cfg).unwrap(), "# current\n");
        assert_eq!(
            std::fs::read_to_string(&bak).unwrap(),
            "# older generation\n"
        );
    }

    #[test]
    fn init_writes_schema_directive_at_top_of_config() {
        // taplo / VS Code / Zed pick up the published JSON Schema via
        // the `#:schema URL` directive at the top of the file. Pin
        // the position (very first bytes) so a future refactor that
        // shifts the directive below comments or theme lines breaks
        // editor schema attachment.
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_init(init_choices("minimal", "default"), None, b"", &env);
        assert_eq!(code, 0, "stderr:\n{stderr}");
        let written = std::fs::read_to_string(dir.path().join(".config/linesmith/config.toml"))
            .expect("file exists");
        assert!(
            written.starts_with("#:schema https://"),
            "expected schema directive at top, got:\n{written}"
        );
        assert!(
            written.contains("config.schema.json"),
            "schema URL must point at config.schema.json"
        );
    }

    #[test]
    fn presets_apply_writes_schema_directive_at_top_of_config() {
        // Same contract as init: presets apply must also emit the
        // `#:schema` directive at the top.
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, _stdout, _stderr) = run_cli_main(&["presets", "apply", "minimal"], b"", &env);
        assert_eq!(code, 0);
        let written = std::fs::read_to_string(dir.path().join(".config/linesmith/config.toml"))
            .expect("file exists");
        assert!(
            written.starts_with("#:schema https://"),
            "expected schema directive at top, got:\n{written}"
        );
    }

    #[test]
    fn init_default_theme_omits_theme_field_to_match_presets_apply() {
        // When the user picks `default`, the written file should be
        // byte-identical to `presets apply minimal` so diff tools and
        // future migrations don't see a redundant `theme = "default"`.
        // Both paths prepend the same `#:schema` directive, so
        // byte-equality holds against the schema-wrapped preset.
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_init(init_choices("minimal", "default"), None, b"", &env);
        assert_eq!(code, 0, "stderr:\n{stderr}");
        let written = std::fs::read_to_string(dir.path().join(".config/linesmith/config.toml"))
            .expect("file exists");
        assert_eq!(
            written,
            config::with_schema_directive(presets::body("minimal").expect("preset registered"))
        );
    }

    #[test]
    fn init_overwrite_prompts_and_backs_up_existing_config() {
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# prior content\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) =
            run_init(init_choices("minimal", "default"), None, b"y\n", &env);
        assert_eq!(code, 0, "stderr:\n{stderr}");
        let backup = dir.path().join(".config/linesmith/config.toml.bak");
        assert!(backup.exists(), "expected .bak");
        assert_eq!(
            std::fs::read_to_string(&backup).unwrap(),
            "# prior content\n"
        );
        // `confirm_overwrite`'s prompt wording is owned by that helper;
        // assert only the post-prompt diagnostic that's specific to
        // this code path. The backup's existence + contents above
        // already prove the prompt was honored.
        assert!(stderr.contains("backed up previous config to"));
    }

    #[test]
    fn init_eof_on_overwrite_aborts_without_clobbering() {
        let dir = tempdir();
        let cfg = dir.path().join(".config/linesmith/config.toml");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(&cfg, "# prior\n").unwrap();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_init(init_choices("minimal", "default"), None, b"", &env);
        assert_eq!(code, 1);
        assert!(stderr.contains("aborted"));
        // Original content untouched.
        assert_eq!(std::fs::read_to_string(&cfg).unwrap(), "# prior\n");
    }

    #[test]
    fn init_honors_explicit_config_flag_over_xdg_path() {
        let dir = tempdir();
        let custom = dir.path().join("custom-init.toml");
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_init(
            init_choices("minimal", "default"),
            Some(custom.clone()),
            b"",
            &env,
        );
        assert_eq!(code, 0, "stderr:\n{stderr}");
        assert!(custom.exists(), "expected init at --config path");
        assert!(!dir.path().join(".config/linesmith/config.toml").exists());
    }

    #[test]
    fn init_unknown_preset_in_choices_errors() {
        // The dialoguer wrapper only ever emits registered names, but a
        // hand-built `InitChoices` with an unknown preset should still
        // get a clear error rather than silently writing nothing.
        let dir = tempdir();
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) =
            run_init(init_choices("not-a-preset", "default"), None, b"", &env);
        assert_eq!(code, 1);
        assert!(stderr.contains("unknown preset 'not-a-preset'"));
    }

    #[test]
    fn init_without_resolvable_path_errors() {
        let env = CliEnv {
            home: None,
            ..CliEnv::for_tests()
        };
        let (code, _stdout, stderr) = run_init(init_choices("minimal", "default"), None, b"", &env);
        assert_eq!(code, 1);
        assert!(stderr.contains("cannot resolve"));
    }

    #[test]
    fn compose_init_body_default_theme_passes_body_through() {
        let body = "# hdr\n\n[line]\nsegments = [\"model\"]\n";
        assert_eq!(super::compose_init_body(body, "default"), body);
    }

    #[test]
    fn compose_init_body_inserts_theme_before_first_table() {
        let body = "# hdr\n# more\n\n[line]\nsegments = [\"model\"]\n";
        let out = super::compose_init_body(body, "catppuccin-mocha");
        assert_eq!(
            out,
            "# hdr\n# more\n\ntheme = \"catppuccin-mocha\"\n\n[line]\nsegments = [\"model\"]\n"
        );
    }

    #[test]
    fn compose_init_body_escapes_quotes_and_backslashes_in_theme_name() {
        // Theme names from user files may contain unusual characters;
        // the TOML string literal must remain valid.
        let body = "[line]\nsegments = []\n";
        let out = super::compose_init_body(body, "weird\"name\\foo");
        assert!(
            out.starts_with("theme = \"weird\\\"name\\\\foo\"\n\n"),
            "unexpected escape: {out:?}"
        );
        // Confirm round-trip parse.
        use std::str::FromStr;
        let cfg = config::Config::from_str(&out).expect("escaped TOML parses");
        assert_eq!(cfg.theme.as_deref(), Some("weird\"name\\foo"));
    }

    #[test]
    fn compose_init_body_handles_table_header_at_byte_zero() {
        // No leading comment, no leading newline: `find("\n[")`
        // wouldn't see this header, so we route through
        // `starts_with('[')` to inject `theme` at the top instead of
        // burying it after the segments.
        use std::str::FromStr;
        let body = "[line]\nsegments = [\"model\"]\n";
        let out = super::compose_init_body(body, "catppuccin-mocha");
        assert_eq!(
            out,
            "theme = \"catppuccin-mocha\"\n\n[line]\nsegments = [\"model\"]\n"
        );
        let cfg = config::Config::from_str(&out).expect("parses");
        assert_eq!(cfg.theme.as_deref(), Some("catppuccin-mocha"));
    }

    #[test]
    fn compose_init_body_falls_back_when_preset_has_no_tables() {
        // Truly-degenerate preset (comments only, no `[table]` ever):
        // the function still produces parseable TOML rather than
        // returning the body unchanged.
        use std::str::FromStr;
        let body = "# only a comment\n";
        let out = super::compose_init_body(body, "catppuccin-mocha");
        assert!(
            out.starts_with("theme = \"catppuccin-mocha\"\n\n"),
            "unexpected output: {out:?}"
        );
        assert!(out.ends_with("# only a comment\n"));
        let cfg = config::Config::from_str(&out).expect("parses");
        assert_eq!(cfg.theme.as_deref(), Some("catppuccin-mocha"));
    }

    #[test]
    fn init_warns_when_config_path_contains_spaces() {
        // Snippet stays valid JSON, but Claude Code tokenizes the
        // `command` field as a shell argv — the user has to add quotes
        // before pasting. Without the warning the snippet would look
        // fine and break silently at exec time.
        let dir = tempdir();
        let custom = dir.path().join("path with spaces.toml");
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) = run_init(
            init_choices("minimal", "default"),
            Some(custom.clone()),
            b"",
            &env,
        );
        assert_eq!(code, 0);
        assert!(
            stderr.contains("characters that need shell quoting"),
            "missing shell-quoting warning, stderr:\n{stderr}"
        );
        assert!(
            stderr.contains("path with spaces.toml"),
            "warning should name the path"
        );
    }

    #[test]
    fn init_does_not_warn_for_plain_ascii_config_path() {
        // Plain paths shouldn't get a warning — false positives would
        // train users to ignore the channel.
        let dir = tempdir();
        let custom = dir.path().join("plain.toml");
        let env = env_with_home(dir.path());
        let (_code, _stdout, stderr) =
            run_init(init_choices("minimal", "default"), Some(custom), b"", &env);
        assert!(
            !stderr.contains("characters that need shell quoting"),
            "false-positive warning, stderr:\n{stderr}"
        );
        assert!(
            !stderr.contains("non-UTF-8"),
            "false-positive warning, stderr:\n{stderr}"
        );
    }

    #[test]
    fn init_warns_when_config_path_contains_shell_metacharacters() {
        // Single-quote, dollar, ampersand, etc. all change shell
        // tokenization. One of them is enough to trigger the warning.
        let dir = tempdir();
        let custom = dir.path().join("weird$path.toml");
        let env = env_with_home(dir.path());
        let (code, _stdout, stderr) =
            run_init(init_choices("minimal", "default"), Some(custom), b"", &env);
        assert_eq!(code, 0);
        assert!(
            stderr.contains("characters that need shell quoting"),
            "missing warning for `$`, stderr:\n{stderr}"
        );
    }

    #[test]
    fn needs_shell_quoting_flags_metacharacters_and_whitespace() {
        // Direct unit test of the predicate so a future addition of a
        // new metachar to the match arm doesn't silently regress.
        // `^` and `%` are cmd.exe specials; the rest are POSIX
        // metacharacters most shells respect.
        for c in [' ', '\t', '\'', '"', '$', '&', ';', '|', '*', '?', '^', '%'] {
            assert!(
                super::needs_shell_quoting(c),
                "expected {c:?} to need quoting"
            );
        }
        for c in ['a', 'Z', '0', '_', '-', '.', '/'] {
            assert!(
                !super::needs_shell_quoting(c),
                "expected {c:?} to NOT need quoting"
            );
        }
        // `\` and `~` pin the platform split: POSIX shell
        // escape / home expansion vs. Windows path separator and 8.3
        // short-name literal. Without these arms a future refactor
        // that collapses the cfg split slips past CI on every host.
        for c in ['\\', '~'] {
            #[cfg(not(windows))]
            assert!(
                super::needs_shell_quoting(c),
                "POSIX: {c:?} must need quoting"
            );
            #[cfg(windows)]
            assert!(
                !super::needs_shell_quoting(c),
                "Windows: {c:?} must not need quoting (path separator / 8.3 short name)"
            );
        }
    }

    #[test]
    fn warn_if_path_needs_user_edit_flags_leading_dash() {
        // Position-sensitive case: a path string starting with `-`
        // (e.g. `LINESMITH_CONFIG=-relative.toml` or a relative
        // `--config -foo`) becomes a flag at the snippet's
        // re-invocation. lexopt rejects the unknown flag and the
        // statusline silently fails on every Claude Code tick.
        // Tested directly because `tempdir().join("-foo")` produces
        // an absolute path that starts with `/`, not `-`.
        let mut stderr = Vec::new();
        super::warn_if_path_needs_user_edit(Some(Path::new("-relative.toml")), &mut stderr);
        let stderr = String::from_utf8(stderr).unwrap();
        assert!(
            stderr.contains("starts with `-`"),
            "missing leading-dash warning:\n{stderr}"
        );
    }

    #[test]
    fn warn_if_path_needs_user_edit_quiet_for_clean_paths() {
        // The leading-dash, shell-quoting, and non-UTF-8 branches
        // are mutually exclusive guards. A plain absolute path with
        // no metacharacters trips none of them.
        let mut stderr = Vec::new();
        super::warn_if_path_needs_user_edit(
            Some(Path::new("/etc/linesmith/config.toml")),
            &mut stderr,
        );
        assert!(
            stderr.is_empty(),
            "false-positive warning:\n{}",
            String::from_utf8_lossy(&stderr)
        );
    }

    #[test]
    fn overwrite_policy_constructors_lock_the_bit_mapping() {
        // The doc on `OverwritePolicy` promises that the named
        // constructors keep the bit-mapping from drifting between
        // call sites. Pin the contract directly so a future refactor
        // that "simplifies" `presets(force)` to `{ skip_prompt: false,
        // clobber_backup: force }` (or similar) breaks here, not at
        // a call site that no longer exists.
        let presets_off = super::OverwritePolicy::presets(false);
        assert!(!presets_off.skip_prompt);
        assert!(!presets_off.clobber_backup);

        let presets_on = super::OverwritePolicy::presets(true);
        assert!(presets_on.skip_prompt);
        assert!(presets_on.clobber_backup);

        let init = super::OverwritePolicy::init();
        assert!(!init.skip_prompt, "init must always prompt");
        assert!(init.clobber_backup, "init must always clobber .bak");
    }

    // The rename-after-clobber breadcrumb path (where
    // `write_config_with_backup` removes an existing .bak and the
    // subsequent rename then fails) isn't unit-tested. Triggering
    // rename failure between a successful `remove_file` and `rename`
    // requires interposition (race) or cross-mount setup. The branch
    // is short and visually inspectable in `write_config_with_backup`;
    // revisit if the helper grows.

    #[test]
    fn presets_apply_without_resolvable_path_errors() {
        let env = CliEnv {
            home: None,
            ..CliEnv::for_tests()
        };
        let (code, _stdout, stderr) = run_cli_main(&["presets", "apply", "minimal"], b"", &env);
        assert_eq!(code, 1);
        assert!(stderr.contains("cannot resolve"));
    }

    #[test]
    fn doctor_subcommand_dispatches_via_cli_main() {
        // CLI-dispatch coverage: parsing reaches doctor_action and
        // emits a report header. We don't assert exit code here
        // because doctor_action calls DoctorEnv::from_process() —
        // exit code depends on the host's $HOME / $TERM / TTY state,
        // which test runners can leave in any shape (`env -i` etc.).
        // The hermetic exit-code contracts live in the
        // `doctor_action_with_env_*` tests below.
        let (_code, stdout, _stderr) = run_cli_main(&["doctor"], b"", &CliEnv::for_tests());
        assert!(stdout.contains("linesmith doctor"), "{stdout}");
        assert!(stdout.contains("Self"), "{stdout}");
    }

    /// Build a doctor-style stdout/stderr pair driven from a hermetic
    /// `DoctorEnv`. Avoids the `from_process()` call that
    /// `doctor_action` itself makes, so tests can assert exit codes
    /// without depending on the host's env.
    fn run_doctor(env: crate::doctor::DoctorEnv, plain: bool) -> (u8, String, String) {
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        let code = doctor_action_with_env(env, plain, &mut stdout, &mut stderr);
        (
            code,
            String::from_utf8(stdout).expect("utf8 stdout"),
            String::from_utf8(stderr).expect("utf8 stderr"),
        )
    }

    #[test]
    fn doctor_action_with_healthy_env_renders_and_exits_zero() {
        let (code, stdout, stderr) = run_doctor(crate::doctor::DoctorEnv::healthy(), false);
        assert_eq!(code, 0, "healthy env must exit 0; stderr was: {stderr}");
        assert!(stdout.contains("linesmith doctor"), "{stdout}");
        assert!(stdout.contains("Environment"), "{stdout}");
        assert!(stdout.contains("Config"), "{stdout}");
        assert!(stdout.contains("Self"), "{stdout}");
        assert!(stdout.contains("Exit: 0"), "{stdout}");
        assert!(stderr.is_empty(), "{stderr}");
    }

    #[test]
    fn doctor_action_with_home_unset_exits_one() {
        // The hermetic counterpart to the CLI-dispatch smoke: when
        // env.home FAILs, doctor_action surfaces it as exit 1
        // end-to-end, not just at the build_report layer.
        let mut env = crate::doctor::DoctorEnv::healthy();
        env.home_env = crate::doctor::EnvVarState::Unset;
        let (code, _stdout, _stderr) = run_doctor(env, false);
        assert_eq!(code, 1);
    }

    #[test]
    fn doctor_plain_output_is_ascii_only() {
        let (code, stdout, _stderr) = run_doctor(crate::doctor::DoctorEnv::healthy(), true);
        assert_eq!(code, 0);
        assert!(stdout.is_ascii(), "plain output had non-ASCII:\n{stdout}");
        assert!(stdout.contains("OK"), "{stdout}");
    }

    #[test]
    fn doctor_default_output_includes_unicode_glyph() {
        let (code, stdout, _stderr) = run_doctor(crate::doctor::DoctorEnv::healthy(), false);
        assert_eq!(code, 0);
        assert!(stdout.contains(''), "{stdout}");
    }

    #[test]
    fn doctor_unknown_flag_exits_two() {
        let (code, _stdout, stderr) =
            run_cli_main(&["doctor", "--bogus"], b"", &CliEnv::for_tests());
        assert_eq!(code, 2);
        assert!(
            stderr.contains("bogus") || stderr.contains("Try --help"),
            "{stderr}"
        );
    }

    #[test]
    fn doctor_config_override_to_missing_explicit_path_fails() {
        // An explicit `--config <path>` that doesn't exist is a FAIL,
        // not a WARN: the user named *this file*. Falling back to
        // defaults silently (same WARN as the implicit-cascade case)
        // would hide a typo. Guards against a regression where the
        // override gets silently dropped before reaching `from_process`.
        let mut env = crate::doctor::DoctorEnv::healthy();
        env.config = crate::doctor::DoctorConfigSnapshot {
            cli_override: Some(PathBuf::from("/nonexistent.toml")),
            resolved: Some(crate::config::ConfigPath {
                path: PathBuf::from("/nonexistent.toml"),
                explicit: true,
            }),
            read: crate::doctor::ConfigReadOutcome::NotFound {
                path: PathBuf::from("/nonexistent.toml"),
                explicit: true,
            },
            plugin_dirs: Vec::new(),
            known_segment_ids: crate::doctor::DoctorConfigSnapshot::built_in_segment_ids(),
            known_theme_names: crate::doctor::DoctorConfigSnapshot::built_in_theme_names(),
        };
        let (code, stdout, _stderr) = run_doctor(env, false);
        assert_eq!(code, 1, "explicit missing config must FAIL → exit 1");
        assert!(
            stdout.contains("/nonexistent.toml"),
            "report should name the missing path:\n{stdout}"
        );
    }

    #[test]
    fn doctor_action_threads_cli_override_through_from_process() {
        // Locks the production wire-up that the snapshot-based
        // `doctor_config_override_to_missing_explicit_path_fails`
        // test can't see: a regression that drops `cli_override`
        // before reaching `DoctorEnv::from_process` would still
        // pass that test (because the snapshot is built directly).
        // This goes through `doctor_action` — the path the binary
        // actually takes — and asserts the rendered report names
        // the path the user supplied. If the override is silently
        // dropped, the report instead describes the host's
        // $HOME-derived path (or "no config path resolved"), which
        // wouldn't contain `unique-marker.toml`.
        let dir = tempdir();
        let cfg_path = dir.path().join("unique-marker.toml");
        std::fs::write(&cfg_path, r#"theme = "default""#).expect("write tempfile");
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        let _code = doctor_action(true, Some(cfg_path.clone()), &mut stdout, &mut stderr);
        let stdout_str = String::from_utf8(stdout).expect("utf8 stdout");
        assert!(
            stdout_str.contains(&cfg_path.display().to_string()),
            "report must name the supplied --config path; \
             got stdout (override probably dropped before from_process):\n{stdout_str}",
        );
        // Discovery must have produced a PASS line for our file —
        // catches a refactor that threads the path but reads the
        // wrong file.
        assert!(
            stdout_str.contains("Config file:"),
            "expected `Config file:` PASS line:\n{stdout_str}",
        );
    }

    #[test]
    fn doctor_stdout_failure_logs_to_stderr_and_returns_report_exit_code() {
        // Pin the documented divergence from `Run`'s render path:
        // doctor surfaces stdout I/O errors via stderr but lets the
        // report's exit code through. A future "fix" that promotes
        // I/O errors to exit 1 would be a contract change.
        struct FailingWriter;
        impl Write for FailingWriter {
            fn write(&mut self, _: &[u8]) -> io::Result<usize> {
                Err(io::Error::new(io::ErrorKind::BrokenPipe, "closed"))
            }
            fn flush(&mut self) -> io::Result<()> {
                Ok(())
            }
        }

        let mut stderr = Vec::new();
        let code = doctor_action_with_env(
            crate::doctor::DoctorEnv::healthy(),
            false,
            &mut FailingWriter,
            &mut stderr,
        );
        assert_eq!(
            code, 0,
            "all-PASS report exits 0 even when stdout is broken"
        );
        let stderr_str = String::from_utf8(stderr).expect("utf8");
        assert!(
            stderr_str.contains("linesmith: doctor:"),
            "expected doctor diagnostic on stderr, got: {stderr_str}"
        );
    }

    fn tempdir() -> TempDir {
        // Timestamp + monotonic counter: parallel tests can hit the
        // same nanosecond under cargo test's thread pool, so the
        // counter is the last line of defense against collisions.
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let base = std::env::temp_dir().join(format!(
            "linesmith-driver-test-{}-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("clock")
                .as_nanos(),
            COUNTER.fetch_add(1, Ordering::Relaxed),
        ));
        std::fs::create_dir_all(&base).expect("mkdir");
        TempDir(base)
    }
}