vortix 0.4.0

Terminal UI for WireGuard and OpenVPN with real-time telemetry and leak guarding
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
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
use super::*;
use crate::core::scanner::ActiveSession;
use std::time::Instant;

fn init_test_env() {
    use std::sync::Once;
    static INIT: Once = Once::new();
    INIT.call_once(|| {
        let dir = tempfile::Builder::new()
            .prefix("vortix_unit_test_")
            .tempdir()
            .expect("failed to create test temp dir");
        let path = dir.path().to_path_buf();
        // Leak intentionally: shared across all tests in this module via Once
        std::mem::forget(dir);
        let _ = std::fs::create_dir_all(&path);
        crate::config::set_config_dir(path);
    });
}

/// Build a minimal `App` for unit testing (no filesystem / scanner / telemetry).
fn test_app() -> App {
    init_test_env();
    let mut runtime = crate::vpn_runtime::VpnRuntime::new_test();
    runtime.config_dir = std::env::temp_dir().join(format!("vortix_test_{}", std::process::id()));
    App {
        runtime,
        engine_handle: None,
        registry: crate::vortix_core::engine::TunnelRegistry::new(),
        should_quit: false,
        logs_scroll: 0,
        logs_auto_scroll: true,
        logs_max_scroll: 0,
        log_level_filter: None,
        focused_panel: FocusedPanel::Sidebar,
        zoomed_panel: None,
        panel_flipped: std::collections::HashSet::new(),
        flip_animation: None,
        input_mode: InputMode::Normal,
        show_config: false,
        show_action_menu: false,
        show_bulk_menu: false,
        action_menu_state: ratatui::widgets::ListState::default(),
        config_scroll: 0,
        cached_config: None,
        search_match_count: 0,
        profile_list_state: ratatui::widgets::TableState::default(),
        panel_areas: std::collections::HashMap::new(),
        toast: None,
        terminal_size: (80, 24),
    }
}

/// Helper: put app into a Connected state for a given profile name.
///
/// Mirrors into the registry so helpers/renderers (which read
/// registry-only after P5a) see the active state. Production code does
/// the same via `mirror_connect_into_registry` on every successful
/// connect.
fn set_connected(app: &mut App, name: &str) {
    if !app.runtime.profiles.iter().any(|p| p.name == name) {
        add_profiles(app, &[name]);
    }
    app.runtime.session_start = Some(Instant::now());
    let details = DetailedConnectionInfo {
        interface: "wg0".to_string(),
        pid: Some(12345),
        ..Default::default()
    };
    app.mirror_connect_into_registry(name, &details, Instant::now());
}

/// Helper: put app into a Disconnecting state for a given profile name.
///
/// Production semantics: a profile only reaches Disconnecting via
/// Connected → user-initiated disconnect. The registry's
/// `set_disconnecting` is a no-op without a prior Connected entry, so
/// this helper seeds Connected first (if not already present) so the
/// transition lands in both the legacy field and the registry. Tests
/// can rely on this single call to leave the app in a fully
/// consistent Disconnecting state.
fn set_disconnecting(app: &mut App, name: &str) {
    use crate::vortix_core::profile::ProfileId;
    if app.registry.snapshot(&ProfileId::new(name)).is_none() {
        set_connected(app, name);
    }
    app.mirror_disconnecting_into_registry(name);
}

/// Helper: create a fake `ActiveSession` for scanner results.
fn fake_session(name: &str) -> ActiveSession {
    ActiveSession {
        name: name.to_string(),
        interface: "wg0".to_string(),
        interface_authoritative: true,
        endpoint: "1.2.3.4:51820".to_string(),
        internal_ip: "10.0.0.2".to_string(),
        mtu: "1420".to_string(),
        public_key: String::new(),
        listen_port: "51820".to_string(),
        transfer_rx: "100 KiB".to_string(),
        transfer_tx: "50 KiB".to_string(),
        latest_handshake: "5 seconds ago".to_string(),
        pid: Some(12345),
        started_at: None,
    }
}

// ====================================================================
// DisconnectResult handler tests
// ====================================================================

#[test]
fn test_disconnect_result_success_transitions_to_disconnected() {
    let mut app = test_app();
    set_disconnecting(&mut app, "test-vpn");

    app.handle_message(Message::DisconnectResult {
        profile: "test-vpn".to_string(),
        success: true,
        error: None,
    });

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnected),
        "Expected Disconnected after successful DisconnectResult"
    );
    assert!(app.runtime.session_start.is_none());
}

#[test]
fn test_disconnect_result_failure_stays_disconnecting() {
    let mut app = test_app();
    set_disconnecting(&mut app, "test-vpn");

    app.handle_message(Message::DisconnectResult {
        profile: "test-vpn".to_string(),
        success: false,
        error: Some("permission denied".to_string()),
    });

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "Should remain Disconnecting after failed disconnect (VPN may still be running)"
    );
    let toast = app.toast.as_ref().expect("toast should be set");
    assert_eq!(toast.toast_type, ToastType::Error);
    assert!(toast.message.contains("Disconnect failed"));
    assert!(toast.message.contains("force-disconnect"));
}

#[test]
fn test_disconnect_result_success_from_non_disconnecting_state() {
    let mut app = test_app();
    // Disconnected = empty registry; nothing to set up explicitly.

    app.handle_message(Message::DisconnectResult {
        profile: "test-vpn".to_string(),
        success: true,
        error: None,
    });

    assert!(matches!(app.legacy_state(), ConnectionState::Disconnected));
}

// ====================================================================
// Scanner debounce guard tests (SyncSystemState while Disconnecting)
// ====================================================================

#[test]
fn test_scanner_never_overrides_disconnecting_to_connected() {
    let mut app = test_app();
    set_disconnecting(&mut app, "test-vpn");

    let sessions = vec![fake_session("test-vpn")];
    app.handle_message(Message::SyncSystemState {
        sessions,
        default_route_interface: None,
    });

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "Scanner must never override Disconnecting to Connected, got {:?}",
        app.legacy_state()
    );
}

#[test]
fn test_scanner_confirms_disconnect_when_interface_gone() {
    let mut app = test_app();
    set_disconnecting(&mut app, "test-vpn");

    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnected),
        "Scanner should confirm Disconnected when interface is gone"
    );
    assert!(app.runtime.session_start.is_none());
}

#[test]
fn test_scanner_safety_timeout_after_30s() {
    use crate::vortix_core::profile::ProfileId;

    let mut app = test_app();
    // Seed the registry with a Disconnecting entry whose started_at
    // is 31s in the past. set_disconnecting is a no-op without a
    // prior Connected entry, so set_connected first.
    set_connected(&mut app, "test-vpn");
    let past = std::time::SystemTime::now() - std::time::Duration::from_secs(31);
    app.registry
        .set_disconnecting(&ProfileId::new("test-vpn"), past);

    let sessions = vec![fake_session("test-vpn")];
    app.handle_message(Message::SyncSystemState {
        sessions,
        default_route_interface: None,
    });

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnected),
        "Should time out to Disconnected after 30s"
    );
    let toast = app.toast.as_ref().expect("timeout should show toast");
    assert_eq!(toast.toast_type, ToastType::Warning);
    assert!(toast.message.contains("timed out"));
}

#[test]
fn test_scanner_disconnecting_does_not_affect_other_profiles() {
    let mut app = test_app();
    set_disconnecting(&mut app, "vpn-a");

    let sessions = vec![fake_session("vpn-b")];
    app.handle_message(Message::SyncSystemState {
        sessions,
        default_route_interface: None,
    });

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnected),
        "Should detect our profile is gone even if other profiles are active"
    );
}

// ====================================================================
// Force disconnect (d pressed twice) tests
// ====================================================================

#[test]
fn test_d_while_disconnecting_escalates_to_force() {
    let mut app = test_app();
    set_disconnecting(&mut app, "test-vpn");
    add_profiles(&mut app, &["test-vpn"]);

    let before = if let ConnectionState::Disconnecting { started, .. } = &app.legacy_state() {
        *started
    } else {
        panic!("expected Disconnecting");
    };

    app.handle_message(Message::Disconnect);

    assert!(matches!(
        app.legacy_state(),
        ConnectionState::Disconnecting { .. }
    ));

    if let ConnectionState::Disconnecting { started, .. } = &app.legacy_state() {
        assert!(*started >= before);
    }

    let toast = app.toast.as_ref().expect("force disconnect shows toast");
    assert_eq!(toast.toast_type, ToastType::Warning);
    assert!(toast.message.contains("Force"));
}

#[test]
fn test_d_while_disconnected_is_noop() {
    let mut app = test_app();
    app.handle_message(Message::Disconnect);
    assert!(matches!(app.legacy_state(), ConnectionState::Disconnected));
}

// ====================================================================
// Helpers for new tests
// ====================================================================

/// Helper: put app into a Connecting state for a given profile name.
///
/// Auto-adds the profile to the catalog if missing (mirror_* helpers
/// require a catalog entry to register). Then sets the legacy field
/// and mirrors the Connecting transition into the registry, matching
/// the production Path A connect flow.
fn set_connecting(app: &mut App, name: &str) {
    if !app.runtime.profiles.iter().any(|p| p.name == name) {
        add_profiles(app, &[name]);
    }
    app.mirror_connecting_into_registry(name);
}

/// Helper: add test profiles to the app.
fn add_profiles(app: &mut App, names: &[&str]) {
    for name in names {
        app.runtime.profiles.push(VpnProfile {
            name: (*name).to_string(),
            protocol: Protocol::WireGuard,
            config_path: std::path::PathBuf::from(format!("/tmp/{name}.conf")),
            location: "Test".to_string(),
            last_used: None,
        });
    }
}

// ====================================================================
// Pending connect / VPN switching tests
// ====================================================================

#[test]
fn toggle_connected_different_profile_opens_takeover_overlay() {
    // When the user toggles a different profile while already
    // connected, the takeover overlay opens. The overlay offers
    // three choices: [Y] Switch (legacy), [B] Connect both
    // (multi-connect), [N] Cancel. This test just covers the
    // overlay-opens branch; the keybinding-specific behaviors are
    // covered by `takeover_y_key_dispatches_switch_path` and
    // `takeover_b_key_dispatches_multi_connect_path`.
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_connected(&mut app, "vpn-a");

    app.toggle_connection(1);

    assert!(
        matches!(
            app.input_mode,
            InputMode::ConfirmDefaultRouteTakeover { ref to_profile_id, .. }
                if to_profile_id.as_str() == "vpn-b"
        ),
        "Expected ConfirmDefaultRouteTakeover dialog, got {:?}",
        app.input_mode
    );
}

#[test]
fn confirm_default_route_takeover_message_runs_multi_connect_path() {
    // Message-handler-level test (not keybinding): when
    // `Message::ConfirmDefaultRouteTakeover` fires directly, the
    // multi-connect path runs — no pending_connect queue, no
    // Disconnecting state. Plan 001 SC3 "primary inverts": both
    // tunnels stay connected, the new one claims the default
    // route. This message is what the overlay's [B] key produces;
    // the keybinding test covers the input path.
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_connected(&mut app, "vpn-a");

    app.handle_message(Message::ConfirmDefaultRouteTakeover { idx: 1 });

    assert!(
        app.runtime.pending_connect.is_none(),
        "multi-connect path must not queue a pending_connect; got {:?}",
        app.runtime.pending_connect
    );
    assert!(
        !matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "multi-connect path must not transition to Disconnecting; got {:?}",
        app.legacy_state()
    );
    // Note: `connection_state` is the legacy single-tunnel mirror —
    // it can only hold one profile at a time, so vpn-b's connect
    // necessarily overwrites vpn-a's slot. Once plan 001 P5 retires
    // this enum entirely, both tunnels' states will be visible via
    // the registry exclusively.
}

#[test]
fn mirror_connecting_makes_registry_hold_connecting_state() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    // Plan A.3: when `connect_profile_inner` sets legacy
    // `ConnectionState = Connecting{...}`, the registry should also
    // hold Connection::Connecting so the sidebar renders `◐` during
    // the connect window. Pre-Path-A, the registry stayed empty
    // until the worker thread's success reply.
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a"]);
    set_connecting(&mut app, "vpn-a");
    app.mirror_connecting_into_registry("vpn-a");

    let snap = app
        .registry
        .snapshot(&ProfileId::new("vpn-a"))
        .expect("registry must hold the Connecting entry");
    assert!(
        matches!(snap.state, Connection::Connecting { .. }),
        "expected Connection::Connecting, got {:?}",
        snap.state
    );
}

#[test]
fn mirror_disconnecting_transitions_existing_connected_entry() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    // Plan A.3: when the legacy disconnect path sets state to
    // Disconnecting, the registry's existing Connected entry
    // should transition to Disconnecting (not vanish). Sidebar
    // renders `◑` during the teardown window.
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a"]);
    // Seed Connected via the authoritative protocol-layer path
    // (mirror_connect_into_registry). Pre-U4 this test used scanner
    // promotion (set_connecting + SyncSystemState); U4 removed that
    // path entirely.
    set_connected(&mut app, "vpn-a");
    assert!(matches!(
        app.registry
            .snapshot(&ProfileId::new("vpn-a"))
            .unwrap()
            .state,
        Connection::Connected { .. }
    ));

    // Now trigger Disconnecting mirror.
    app.mirror_disconnecting_into_registry("vpn-a");

    let snap = app
        .registry
        .snapshot(&ProfileId::new("vpn-a"))
        .expect("registry entry must persist through Disconnecting");
    assert!(
        matches!(snap.state, Connection::Disconnecting { .. }),
        "expected Connection::Disconnecting, got {:?}",
        snap.state
    );
}

#[test]
fn mirror_disconnecting_no_op_when_registry_has_no_entry() {
    // Disconnecting only makes sense for a tunnel that exists.
    // Calling mirror_disconnecting on an unknown profile must not
    // insert a phantom entry.
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a"]);
    app.mirror_disconnecting_into_registry("vpn-a");
    assert_eq!(
        app.registry.tunnel_count(),
        0,
        "Disconnecting mirror must not insert when nothing existed"
    );
}

#[test]
fn mirror_failed_makes_registry_hold_disconnected_with_failure() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    // Plan A.3: when `handle_connect_result` failure branch fires,
    // the registry should hold Disconnected{ last_failure: Some }
    // so the sidebar renders `✗` until the user retries (which
    // overwrites with Connecting) or explicitly clears.
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a"]);
    set_connecting(&mut app, "vpn-a");

    // Worker thread reports failure.
    app.handle_message(Message::ConnectResult {
        profile: "vpn-a".to_string(),
        success: false,
        error: Some("handshake timeout".to_string()),
        interface: None,
        pid: None,
    });

    let snap = app
        .registry
        .snapshot(&ProfileId::new("vpn-a"))
        .expect("registry must hold the failed entry");
    let Connection::Disconnected { last_failure } = snap.state else {
        panic!("expected Disconnected, got {:?}", snap.state);
    };
    assert!(
        last_failure.is_some(),
        "failure must be marked so sidebar renders the ✗ badge"
    );
}

#[test]
fn takeover_y_key_dispatches_switch_path() {
    // [Y]/Enter on the takeover overlay fires the legacy "switch
    // VPNs" path (disconnect current, then connect new). This is
    // the recommended default for users coming from the
    // pre-multi-tunnel UX; the new "keep both" multi-connect path
    // is opt-in via [B].
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_connected(&mut app, "vpn-a");

    app.toggle_connection(1);
    assert!(
        matches!(
            app.input_mode,
            InputMode::ConfirmDefaultRouteTakeover { .. }
        ),
        "expected takeover overlay open"
    );

    // Cursor defaults to [Y]es — press Enter to confirm.
    {
        use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
        app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    }

    // Behavior contract: disconnect path fires. pending_connect
    // queues vpn-b for after teardown; legacy state transitions to
    // Disconnecting (single-tunnel "switch" semantics).
    assert_eq!(
        app.runtime.pending_connect,
        Some(1),
        "vpn-b should be queued for after-disconnect connect"
    );
    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "expected Disconnecting state, got {:?}",
        app.legacy_state()
    );
    // Overlay closes after the keypress is handled.
    assert!(matches!(app.input_mode, InputMode::Normal));
}

#[test]
fn takeover_b_key_dispatches_multi_connect_path() {
    // [B]/[b] on the takeover overlay fires the opt-in multi-connect
    // path: both tunnels stay connected, the new one becomes the
    // active exit, the prior primary becomes split-tunnel-yielded.
    // No pending_connect queue; no Disconnecting state.
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_connected(&mut app, "vpn-a");
    // connect_profile_forced (the multi-connect path's downstream)
    // checks `is_root`; without it we'd hit InputMode::PermissionDenied
    // instead of Normal. The test cares about the behavioral path,
    // not the privilege check.
    app.runtime.is_root = true;
    app.toggle_connection(1);

    app.handle_key(key_char('b'));

    // Behavior contract: NO disconnect of the existing tunnel.
    assert!(
        app.runtime.pending_connect.is_none(),
        "multi-connect path must not queue a pending_connect"
    );
    assert!(
        !matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "multi-connect path must not transition to Disconnecting; got {:?}",
        app.legacy_state()
    );
    assert!(matches!(app.input_mode, InputMode::Normal));
}

#[test]
fn switch_path_disconnect_completion_removes_old_profile_from_registry() {
    use crate::vortix_core::profile::ProfileId;

    // Switch-flow regression: pressing [Y]/Enter on the takeover
    // overlay queues pending_connect + fires disconnect.
    // `complete_disconnect` drains `pending_connect` and fires the
    // new connect — but the old branch early-returned before
    // calling `mirror_disconnect_into_registry`, leaving the old
    // profile's entry in the registry. Result: sidebar dot stayed
    // green and header still listed the disconnected tunnel.
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);

    // Set up vpn-a fully connected via the authoritative protocol-layer
    // path. Pre-U4 this used scanner promotion (set_connecting +
    // SyncSystemState); U4 removed that path.
    set_connected(&mut app, "vpn-a");
    assert_eq!(app.registry.tunnel_count(), 1, "setup precondition");

    // User toggles vpn-b, accepts the takeover overlay via Enter
    // (default [Y]es selection — the recommended Switch path).
    app.toggle_connection(1);
    {
        use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
        app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    }
    assert_eq!(
        app.runtime.pending_connect,
        Some(1),
        "setup precondition: pending switch queued"
    );
    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "setup precondition"
    );

    // Worker thread reports vpn-a's disconnect completed.
    app.handle_message(Message::DisconnectResult {
        profile: "vpn-a".to_string(),
        success: true,
        error: None,
    });

    // vpn-a must be gone from the registry — the switch flow drained
    // pending_connect AND removed the old entry.
    assert!(
        app.registry.snapshot(&ProfileId::new("vpn-a")).is_none(),
        "vpn-a must be removed from registry after switch-path disconnect completes"
    );
}

#[test]
fn takeover_capital_b_also_dispatches_multi_connect() {
    // Case-insensitive: [B] should work whether shift is held or not.
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_connected(&mut app, "vpn-a");
    app.runtime.is_root = true;
    app.toggle_connection(1);

    app.handle_key(key_char('B'));

    assert!(app.runtime.pending_connect.is_none());
    assert!(!matches!(
        app.legacy_state(),
        ConnectionState::Disconnecting { .. }
    ));
}

#[test]
fn test_toggle_connected_same_profile_disconnects_without_pending() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a"]);
    set_connected(&mut app, "vpn-a");

    app.toggle_connection(0);

    assert_eq!(
        app.runtime.pending_connect, None,
        "Same-profile toggle should not set pending"
    );
    // P5d: registry.disconnect drives the FSM through the placeholder
    // MockTunnel synchronously, so the tunnel ends Disconnected.
    assert!(!matches!(
        app.legacy_state(),
        ConnectionState::Connected { .. }
    ));
}

#[test]
fn test_toggle_while_disconnecting_queues_pending() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_disconnecting(&mut app, "vpn-a");

    app.toggle_connection(1);

    assert_eq!(app.runtime.pending_connect, Some(1));
    assert!(matches!(
        app.legacy_state(),
        ConnectionState::Disconnecting { .. }
    ));
}

#[test]
fn test_toggle_while_connecting_is_rejected() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_connecting(&mut app, "vpn-a");

    app.toggle_connection(1);

    assert!(matches!(
        app.legacy_state(),
        ConnectionState::Connecting { .. }
    ));
    assert_eq!(app.runtime.pending_connect, None);
}

#[test]
fn test_pending_connect_drained_on_disconnect_success() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_disconnecting(&mut app, "vpn-a");
    app.runtime.pending_connect = Some(1);
    app.runtime.is_root = true;

    app.handle_message(Message::DisconnectResult {
        profile: "vpn-a".to_string(),
        success: true,
        error: None,
    });

    assert_eq!(app.runtime.pending_connect, None);
    assert!(
        matches!(app.legacy_state(), ConnectionState::Connecting { ref profile, .. } if profile == "vpn-b"),
        "Expected Connecting to vpn-b, got {:?}",
        app.legacy_state()
    );
}

#[test]
fn test_pending_connect_drained_on_scanner_interface_gone() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_disconnecting(&mut app, "vpn-a");
    app.runtime.pending_connect = Some(1);
    app.runtime.is_root = true;

    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });

    assert_eq!(app.runtime.pending_connect, None);
    assert!(
        matches!(app.legacy_state(), ConnectionState::Connecting { ref profile, .. } if profile == "vpn-b"),
        "Expected auto-connect to vpn-b after scanner confirms disconnect"
    );
}

#[test]
fn test_pending_preserved_on_disconnect_failure() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    set_disconnecting(&mut app, "vpn-a");
    app.runtime.pending_connect = Some(1);

    app.handle_message(Message::DisconnectResult {
        profile: "vpn-a".to_string(),
        success: false,
        error: Some("permission denied".to_string()),
    });

    // pending_connect is preserved so it can fire after force-disconnect
    assert_eq!(app.runtime.pending_connect, Some(1));
    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "Should remain Disconnecting after failed disconnect"
    );
}

#[test]
fn test_pending_cleared_on_30s_timeout() {
    use crate::vortix_core::profile::ProfileId;

    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    // Seed Connected then back-date the Disconnecting transition by
    // 31s so the scanner's per-profile timeout branch fires.
    set_connected(&mut app, "vpn-a");
    let past = std::time::SystemTime::now() - std::time::Duration::from_secs(31);
    app.registry
        .set_disconnecting(&ProfileId::new("vpn-a"), past);
    app.runtime.pending_connect = Some(1);

    let sessions = vec![fake_session("vpn-a")];
    app.handle_message(Message::SyncSystemState {
        sessions,
        default_route_interface: None,
    });

    assert_eq!(app.runtime.pending_connect, None);
    assert!(matches!(app.legacy_state(), ConnectionState::Disconnected));
}

// ====================================================================
// ConnectResult tests
// ====================================================================

#[test]
fn test_connect_result_success_transitions_to_connected() {
    let mut app = test_app();
    add_profiles(&mut app, &["test-vpn"]);
    set_connecting(&mut app, "test-vpn");

    app.handle_message(Message::ConnectResult {
        profile: "test-vpn".to_string(),
        success: true,
        error: None,
        interface: None,
        pid: None,
    });

    assert!(
        matches!(app.legacy_state(), ConnectionState::Connected { ref profile, .. } if profile == "test-vpn"),
        "Successful ConnectResult should transition to Connected"
    );
}

#[test]
fn test_connect_result_failure_transitions_to_disconnected() {
    let mut app = test_app();
    // Disable retry so failure short-circuits straight to the final
    // "Failed to connect" toast instead of scheduling a retry — the
    // behavior this test was originally written to exercise.
    app.runtime.config.connect_max_retries = 0;
    set_connecting(&mut app, "test-vpn");

    app.handle_message(Message::ConnectResult {
        profile: "test-vpn".to_string(),
        success: false,
        error: Some("wg-quick: already exists".to_string()),
        interface: None,
        pid: None,
    });

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnected),
        "Failed ConnectResult should transition to Disconnected"
    );
    let toast = app.toast.as_ref().expect("should show error toast");
    assert_eq!(toast.toast_type, ToastType::Error);
    assert!(toast.message.contains("Failed to connect"));
}

#[test]
fn test_connect_result_failure_clears_pending() {
    let mut app = test_app();
    set_connecting(&mut app, "test-vpn");
    app.runtime.pending_connect = Some(1);

    app.handle_message(Message::ConnectResult {
        profile: "test-vpn".to_string(),
        success: false,
        error: Some("error".to_string()),
        interface: None,
        pid: None,
    });

    assert_eq!(
        app.runtime.pending_connect, None,
        "Connect failure should clear pending"
    );
}

// ====================================================================
// Disconnect from Connecting state tests
// ====================================================================

#[test]
fn test_disconnect_from_connecting_state() {
    let mut app = test_app();
    add_profiles(&mut app, &["test-vpn"]);
    set_connecting(&mut app, "test-vpn");

    app.disconnect();

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "disconnect() should work from Connecting state, got {:?}",
        app.legacy_state()
    );
}

#[test]
fn test_d_key_from_connecting_state_disconnects() {
    let mut app = test_app();
    add_profiles(&mut app, &["test-vpn"]);
    set_connecting(&mut app, "test-vpn");

    app.handle_message(Message::Disconnect);

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "d key should cancel Connecting state"
    );
}

// ====================================================================
// Reconnect uses pending_connect (no race)
// ====================================================================

#[test]
fn test_reconnect_sets_pending_not_immediate_connect() {
    let mut app = test_app();
    add_profiles(&mut app, &["test-vpn"]);
    set_connected(&mut app, "test-vpn");

    app.reconnect();

    assert_eq!(app.runtime.pending_connect, Some(0));
    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "Reconnect should disconnect first"
    );
}

#[test]
fn test_reconnect_auto_connects_after_disconnect_completes() {
    let mut app = test_app();
    add_profiles(&mut app, &["test-vpn"]);
    set_disconnecting(&mut app, "test-vpn");
    app.runtime.pending_connect = Some(0);
    app.runtime.is_root = true;

    app.handle_message(Message::DisconnectResult {
        profile: "test-vpn".to_string(),
        success: true,
        error: None,
    });

    assert_eq!(app.runtime.pending_connect, None);
    assert!(
        matches!(app.legacy_state(), ConnectionState::Connecting { ref profile, .. } if profile == "test-vpn"),
        "Reconnect should auto-connect after disconnect"
    );
}

// ====================================================================
// QuickConnect (1-9) edge cases
// ====================================================================

#[test]
fn test_quick_connect_while_connected_shows_confirm() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b", "vpn-c"]);
    set_connected(&mut app, "vpn-a");

    app.handle_message(Message::QuickConnect(1));

    assert!(
        matches!(
            app.input_mode,
            InputMode::ConfirmDefaultRouteTakeover { ref to_profile_id, .. }
                if to_profile_id.as_str() == "vpn-b"
        ),
        "Expected ConfirmDefaultRouteTakeover dialog for QuickConnect, got {:?}",
        app.input_mode,
    );
}

#[test]
fn test_quick_connect_while_disconnecting_updates_pending() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b", "vpn-c"]);
    set_disconnecting(&mut app, "vpn-a");
    app.runtime.pending_connect = Some(1);

    app.handle_message(Message::QuickConnect(2));

    assert_eq!(
        app.runtime.pending_connect,
        Some(2),
        "Should update pending to new choice"
    );
}

#[test]
fn test_quick_connect_from_disconnected() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a"]);
    app.runtime.is_root = true;

    app.handle_message(Message::QuickConnect(0));

    assert!(
        matches!(app.legacy_state(), ConnectionState::Connecting { .. }),
        "QuickConnect from Disconnected should go to Connecting"
    );
    assert_eq!(app.runtime.pending_connect, None);
}

// ====================================================================
// Auth prompt tests
// ====================================================================

/// Helper: add `OpenVPN` profiles with a temp config file containing auth-user-pass.
fn add_openvpn_profiles_with_auth(app: &mut App, names: &[&str], dir: &std::path::Path) {
    let _ = std::fs::create_dir_all(dir);
    for name in names {
        let config_path = dir.join(format!("{name}.ovpn"));
        std::fs::write(
            &config_path,
            "client\nremote example.com 1194\nauth-user-pass\ndev tun\nproto udp\n",
        )
        .unwrap();
        app.runtime.profiles.push(VpnProfile {
            name: (*name).to_string(),
            protocol: Protocol::OpenVPN,
            config_path,
            location: "Test".to_string(),
            last_used: None,
        });
    }
}

/// Helper: add `OpenVPN` profiles with a `static-challenge` directive
/// alongside auth-user-pass (plan 2026-06-02-001, #191).
fn add_openvpn_profiles_with_static_challenge(
    app: &mut App,
    names: &[&str],
    dir: &std::path::Path,
) {
    let _ = std::fs::create_dir_all(dir);
    for name in names {
        let config_path = dir.join(format!("{name}.ovpn"));
        std::fs::write(
            &config_path,
            "client\nremote example.com 1194\nauth-user-pass\nstatic-challenge \"Enter TOTP code\" 1\ndev tun\nproto udp\n",
        )
        .unwrap();
        app.runtime.profiles.push(VpnProfile {
            name: (*name).to_string(),
            protocol: Protocol::OpenVPN,
            config_path,
            location: "Test".to_string(),
            last_used: None,
        });
    }
}

/// Helper: add `OpenVPN` profiles WITHOUT auth-user-pass.
fn add_openvpn_profiles_no_auth(app: &mut App, names: &[&str], dir: &std::path::Path) {
    let _ = std::fs::create_dir_all(dir);
    for name in names {
        let config_path = dir.join(format!("{name}.ovpn"));
        std::fs::write(
            &config_path,
            "client\nremote example.com 1194\ndev tun\nproto udp\n<ca>\n</ca>\n",
        )
        .unwrap();
        app.runtime.profiles.push(VpnProfile {
            name: (*name).to_string(),
            protocol: Protocol::OpenVPN,
            config_path,
            location: "Test".to_string(),
            last_used: None,
        });
    }
}

#[test]
fn test_auth_prompt_shown_for_openvpn_with_auth_user_pass() {
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_auth(&mut app, &["auth-vpn"], tmp.path());
    app.runtime.is_root = true;

    crate::utils::delete_openvpn_auth_file("auth-vpn");

    app.connect_profile(0);

    assert!(
        matches!(app.input_mode, InputMode::AuthPrompt { .. }),
        "OpenVPN with auth-user-pass and no saved creds should show AuthPrompt"
    );
    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnected),
        "Should not start connecting before credentials are provided"
    );
}

#[test]
fn test_auth_prompt_skipped_when_creds_saved() {
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_auth(&mut app, &["saved-vpn"], tmp.path());
    app.runtime.is_root = true;

    let _ = crate::utils::write_openvpn_auth_file("saved-vpn", "user", "pass");

    app.connect_profile(0);

    assert!(
        !matches!(app.input_mode, InputMode::AuthPrompt { .. }),
        "Should not show AuthPrompt when creds are already saved"
    );
    assert!(
        matches!(app.legacy_state(), ConnectionState::Connecting { .. }),
        "Should proceed to Connecting with saved credentials"
    );

    crate::utils::delete_openvpn_auth_file("saved-vpn");
}

#[test]
fn test_auth_prompt_fires_for_static_challenge_even_with_saved_creds() {
    // Plan 2026-06-02-001 U3 / overlay-skip bug fix: a profile with
    // `static-challenge` MUST surface the auth overlay on every connect
    // attempt regardless of saved-creds state, because the OTP is
    // single-use and cannot be persisted. When creds are pre-saved the
    // overlay starts with them filled and focuses the OTP field directly.
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_static_challenge(&mut app, &["mfa-saved"], tmp.path());
    app.runtime.is_root = true;

    let _ = crate::utils::write_openvpn_auth_file("mfa-saved", "user", "pass");

    app.connect_profile(0);

    if let InputMode::AuthPrompt {
        username,
        password,
        focused_field,
        static_challenge_prompt,
        ..
    } = &app.input_mode
    {
        assert_eq!(username, "user", "username should be pre-filled");
        assert_eq!(password, "pass", "password should be pre-filled");
        assert_eq!(
            focused_field,
            &AuthField::Otp,
            "focus should jump to the OTP field when creds are pre-filled"
        );
        assert_eq!(
            static_challenge_prompt.as_deref(),
            Some("Enter TOTP code"),
            "the directive's prompt text should reach the overlay"
        );
    } else {
        panic!(
            "Expected AuthPrompt overlay for static-challenge profile with saved creds; got {:?}",
            app.input_mode
        );
    }

    crate::utils::delete_openvpn_auth_file("mfa-saved");
}

#[test]
fn test_auth_prompt_fires_for_static_challenge_without_saved_creds() {
    // Same gate, no saved creds path: overlay should still fire, with
    // empty fields focused on Username (the legacy initial-focus
    // behaviour, since the user has to type everything).
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_static_challenge(&mut app, &["mfa-fresh"], tmp.path());
    app.runtime.is_root = true;

    crate::utils::delete_openvpn_auth_file("mfa-fresh");

    app.connect_profile(0);

    if let InputMode::AuthPrompt {
        username,
        password,
        focused_field,
        static_challenge_prompt,
        ..
    } = &app.input_mode
    {
        assert!(username.is_empty());
        assert!(password.is_empty());
        assert_eq!(focused_field, &AuthField::Username);
        assert_eq!(static_challenge_prompt.as_deref(), Some("Enter TOTP code"));
    } else {
        panic!(
            "Expected AuthPrompt overlay for static-challenge profile without saved creds; got {:?}",
            app.input_mode
        );
    }
}

#[test]
fn test_auth_prompt_skipped_for_wireguard() {
    let mut app = test_app();
    add_profiles(&mut app, &["wg-vpn"]);
    app.runtime.is_root = true;

    app.connect_profile(0);

    assert!(
        !matches!(app.input_mode, InputMode::AuthPrompt { .. }),
        "WireGuard profiles should never show AuthPrompt"
    );
}

#[test]
fn test_auth_prompt_skipped_for_openvpn_without_auth_directive() {
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_noauth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_no_auth(&mut app, &["noauth-vpn"], tmp.path());
    app.runtime.is_root = true;

    app.connect_profile(0);

    assert!(
        !matches!(app.input_mode, InputMode::AuthPrompt { .. }),
        "OpenVPN without auth-user-pass should not show AuthPrompt"
    );
    assert!(
        matches!(app.legacy_state(), ConnectionState::Connecting { .. }),
        "Should proceed to Connecting directly"
    );
}

#[test]
fn test_auth_submit_triggers_connect() {
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_auth(&mut app, &["submit-vpn"], tmp.path());
    app.runtime.is_root = true;

    crate::utils::delete_openvpn_auth_file("submit-vpn");

    app.handle_message(Message::AuthSubmit {
        idx: 0,
        username: "testuser".to_string(),
        password: "testpass".to_string(),
        otp: None,
        save: true,
        connect_after: true,
    });

    assert_eq!(app.input_mode, InputMode::Normal);
    assert!(
        matches!(app.legacy_state(), ConnectionState::Connecting { .. }),
        "AuthSubmit should trigger connect_profile"
    );

    let creds = crate::utils::read_openvpn_saved_auth("submit-vpn");
    assert!(creds.is_some());
    let (user, pass) = creds.unwrap();
    assert_eq!(user, "testuser");
    assert_eq!(pass, "testpass");

    crate::utils::delete_openvpn_auth_file("submit-vpn");
}

#[test]
fn test_auth_submit_with_otp_and_save_restores_plain_after_connect() {
    // Plan 2026-06-02-001 U3 / PF-3: when `save=true` AND `otp=Some(...)`,
    // the canonical auth file must end up plain-text on disk after the
    // connect call returns. The submit handler writes plain, then SCRV1,
    // then restores plain — the on-disk state after handle_auth_submit
    // returns is what subsequent `read_openvpn_saved_auth` callers see.
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_auth(&mut app, &["mfa-save-vpn"], tmp.path());
    app.runtime.is_root = true;
    crate::utils::delete_openvpn_auth_file("mfa-save-vpn");

    app.handle_message(Message::AuthSubmit {
        idx: 0,
        username: "u".to_string(),
        password: "p".to_string(),
        otp: Some("123456".to_string()),
        save: true,
        connect_after: true,
    });

    // After the handler returns, read_openvpn_saved_auth must see the
    // plain password, not the SCRV1 envelope.
    let creds = crate::utils::read_openvpn_saved_auth("mfa-save-vpn");
    assert!(creds.is_some(), "saved file must exist after save+connect");
    let (_, line2) = creds.unwrap();
    assert!(
        !line2.starts_with("SCRV1:"),
        "auth file must be restored to plain after connect; got line 2 = {line2:?}"
    );
    assert_eq!(line2, "p", "expected plain password, got {line2:?}");

    crate::utils::delete_openvpn_auth_file("mfa-save-vpn");
}

#[test]
fn test_auth_submit_does_not_reopen_overlay_for_static_challenge_profile() {
    // Regression for the submit-loop bug discovered after U3 landed:
    // handle_auth_submit calls connect_profile, which (via the
    // overlay-fires-fix) used to see static_challenge.is_some() and
    // re-open the auth overlay with an empty OTP — so pressing Enter
    // appeared to do nothing because the freshly-opened overlay was
    // then overwritten by the pre-submit values. The fix routes the
    // post-submit connect through connect_profile_after_auth, which
    // skips the overlay gate. This test asserts input_mode lands on
    // Normal (or Connecting) — never on a re-opened AuthPrompt.
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_static_challenge(&mut app, &["mfa-resubmit"], tmp.path());
    app.runtime.is_root = true;
    crate::utils::delete_openvpn_auth_file("mfa-resubmit");

    app.handle_message(Message::AuthSubmit {
        idx: 0,
        username: "u".to_string(),
        password: "p".to_string(),
        otp: Some("123456".to_string()),
        save: true,
        connect_after: true,
    });

    assert!(
        !matches!(app.input_mode, InputMode::AuthPrompt { .. }),
        "AuthSubmit must NOT re-open the AuthPrompt overlay for a static-challenge profile; got {:?}",
        app.input_mode
    );

    crate::utils::delete_openvpn_auth_file("mfa-resubmit");
}

#[test]
fn test_auth_submit_with_otp_no_save_deletes_file() {
    // Plan 2026-06-02-001 U3 / PF-4: when `save=false` AND `otp=Some(...)`,
    // the auth file must be deleted after the connect call returns —
    // OTP is single-use and the user explicitly chose not to persist
    // credentials.
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_auth(&mut app, &["mfa-no-save-vpn"], tmp.path());
    app.runtime.is_root = true;
    crate::utils::delete_openvpn_auth_file("mfa-no-save-vpn");

    app.handle_message(Message::AuthSubmit {
        idx: 0,
        username: "u".to_string(),
        password: "p".to_string(),
        otp: Some("123456".to_string()),
        save: false,
        connect_after: true,
    });

    assert!(
        crate::utils::read_openvpn_saved_auth("mfa-no-save-vpn").is_none(),
        "auth file must be deleted after one-time MFA connect"
    );
}

#[test]
fn test_auth_cancel_returns_to_normal() {
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_auth(&mut app, &["cancel-vpn"], tmp.path());
    app.runtime.is_root = true;

    crate::utils::delete_openvpn_auth_file("cancel-vpn");

    app.connect_profile(0);
    assert!(matches!(app.input_mode, InputMode::AuthPrompt { .. }));

    app.handle_message(Message::CloseOverlay);
    assert_eq!(app.input_mode, InputMode::Normal);
    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnected),
        "Cancelling auth should keep Disconnected state"
    );
}

#[test]
fn test_auth_field_otp_appears_in_tab_cycle_for_static_challenge_profile() {
    // Plan 2026-06-02-001 U3: tab cycle becomes a 4-stop cycle when
    // static_challenge_prompt.is_some() — Username -> Password -> Otp ->
    // SaveCheckbox -> Username.
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
    let mut app = test_app();
    app.input_mode = InputMode::AuthPrompt {
        profile_idx: 0,
        profile_name: "mfa".to_string(),
        username: String::new(),
        username_cursor: 0,
        password: String::new(),
        password_cursor: 0,
        otp: String::new(),
        otp_cursor: 0,
        focused_field: AuthField::Username,
        save_credentials: true,
        connect_after: true,
        static_challenge_prompt: Some("Enter code".to_string()),
    };

    // Username -> Password -> Otp -> SaveCheckbox -> Username
    let expected = [
        AuthField::Password,
        AuthField::Otp,
        AuthField::SaveCheckbox,
        AuthField::Username,
    ];
    for expected_field in &expected {
        app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE));
        if let InputMode::AuthPrompt { focused_field, .. } = &app.input_mode {
            assert_eq!(focused_field, expected_field, "tab cycle drifted");
        } else {
            panic!("Expected AuthPrompt");
        }
    }
}

#[test]
fn test_auth_field_switching() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    app.input_mode = InputMode::AuthPrompt {
        profile_idx: 0,
        profile_name: "test".to_string(),
        username: String::new(),
        username_cursor: 0,
        password: String::new(),
        password_cursor: 0,
        otp: String::new(),
        otp_cursor: 0,
        focused_field: AuthField::Username,
        save_credentials: true,
        connect_after: true,
        static_challenge_prompt: None,
    };

    // Tab from Username -> Password
    app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE));
    if let InputMode::AuthPrompt { focused_field, .. } = &app.input_mode {
        assert_eq!(*focused_field, AuthField::Password);
    } else {
        panic!("Expected AuthPrompt");
    }

    // Tab from Password -> SaveCheckbox
    app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE));
    if let InputMode::AuthPrompt { focused_field, .. } = &app.input_mode {
        assert_eq!(*focused_field, AuthField::SaveCheckbox);
    } else {
        panic!("Expected AuthPrompt");
    }

    // Tab from SaveCheckbox -> Username (wraps around)
    app.handle_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE));
    if let InputMode::AuthPrompt { focused_field, .. } = &app.input_mode {
        assert_eq!(*focused_field, AuthField::Username);
    } else {
        panic!("Expected AuthPrompt");
    }
}

#[test]
fn test_auth_delete_profile_cleans_auth_file() {
    let mut app = test_app();
    let tmp = tempfile::Builder::new()
        .prefix("vortix_auth_")
        .tempdir()
        .unwrap();
    add_openvpn_profiles_with_auth(&mut app, &["del-vpn"], tmp.path());
    app.profile_list_state.select(Some(0));

    let auth_path = crate::utils::write_openvpn_auth_file("del-vpn", "user", "pass").unwrap();
    assert!(auth_path.exists());

    app.confirm_delete(0);

    assert!(
        !auth_path.exists(),
        "Auth file should be deleted when profile is deleted"
    );
}

// ====================================================================
// v0.3.0 — "Trustworthy & Alive" tests
// ====================================================================

// --- Phase 1: DNS leak detection (#46) ---

#[test]
fn test_dns_leak_detected_when_dns_unchanged_after_vpn() {
    let mut app = test_app();
    app.runtime.real_dns = Some("192.168.1.1".to_string());
    app.runtime.dns_server = "192.168.1.1".to_string();
    set_connected(&mut app, "vpn-a");

    assert_eq!(
        app.runtime.dns_server,
        app.runtime.real_dns.as_ref().unwrap().as_str(),
        "DNS unchanged = leak"
    );
}

#[test]
fn test_dns_not_leaking_when_vpn_pushed_new_dns() {
    let mut app = test_app();
    app.runtime.real_dns = Some("192.168.1.1".to_string());
    app.runtime.dns_server = "10.8.0.1".to_string();
    set_connected(&mut app, "vpn-a");

    assert_ne!(
        app.runtime.dns_server,
        app.runtime.real_dns.as_ref().unwrap().as_str(),
        "Different DNS = not leaking"
    );
}

#[test]
fn test_real_dns_captured_when_disconnected() {
    // Updated post-gate: real_dns caching requires the scanner to
    // have reported zero kernel sessions before the DNS sample is
    // trusted. test_app() doesn't auto-tick the scanner, so we
    // drive a clean SyncSystemState first to mirror what production
    // does (~1s after launch). The dedicated gate-behavior tests
    // (real_dns_not_cached_when_scanner_has_not_ticked_yet etc.)
    // pin each gate branch individually.
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();
    assert!(app.runtime.real_dns.is_none());

    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });

    app.handle_message(Message::Telemetry(TelemetryUpdate::Dns(
        "8.8.8.8".to_string(),
    )));

    assert_eq!(app.runtime.real_dns, Some("8.8.8.8".to_string()));
}

// --- Phase 1: Last security check timestamp (#47) ---

#[test]
fn test_last_security_check_updated_on_ip_telemetry() {
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();
    assert!(app.runtime.last_security_check.is_none());

    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "1.2.3.4".to_string(),
    )));

    assert!(app.runtime.last_security_check.is_some());
}

#[test]
fn test_last_security_check_updated_on_dns_telemetry() {
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();
    assert!(app.runtime.last_security_check.is_none());

    app.handle_message(Message::Telemetry(TelemetryUpdate::Dns(
        "1.1.1.1".to_string(),
    )));

    assert!(app.runtime.last_security_check.is_some());
}

#[test]
fn test_last_security_check_updated_on_ipv6_telemetry() {
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();
    assert!(app.runtime.last_security_check.is_none());

    app.handle_message(Message::Telemetry(TelemetryUpdate::Ipv6Leak(false)));

    assert!(app.runtime.last_security_check.is_some());
}

// --- Phase 1: Reconnect from Disconnected (#49) ---

#[test]
fn test_reconnect_from_disconnected_with_last_profile() {
    let mut app = test_app();
    add_profiles(&mut app, &["my-vpn"]);
    app.runtime.last_connected_profile = Some("my-vpn".to_string());
    app.runtime.is_root = true;

    app.reconnect();

    assert!(
        matches!(app.legacy_state(), ConnectionState::Connecting { ref profile, .. } if profile == "my-vpn"),
        "Should initiate connection to last used profile"
    );
}

#[test]
fn test_reconnect_from_disconnected_without_last_profile_is_noop() {
    let mut app = test_app();
    add_profiles(&mut app, &["my-vpn"]);
    assert!(app.runtime.last_connected_profile.is_none());

    app.reconnect();

    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnected),
        "Should stay disconnected when no last_connected_profile"
    );
}

// --- Phase 1: Timeout toast color (#50) ---

#[test]
fn test_connection_timeout_shows_error_toast() {
    let mut app = test_app();
    add_profiles(&mut app, &["timeout-vpn"]);
    set_connecting(&mut app, "timeout-vpn");

    app.handle_message(Message::ConnectionTimeout("timeout-vpn".to_string()));

    assert!(app.toast.is_some(), "Should show a toast");
    assert_eq!(
        app.toast.as_ref().unwrap().toast_type,
        crate::state::ToastType::Error,
        "Timeout toast should be Error, not Warning"
    );
}

// --- Phase 1: last_connected_profile set on success (#49 + reconnect) ---

#[test]
fn test_last_connected_profile_set_on_connect_success() {
    let mut app = test_app();
    add_profiles(&mut app, &["success-vpn"]);
    set_connecting(&mut app, "success-vpn");

    app.handle_message(Message::ConnectResult {
        profile: "success-vpn".to_string(),
        success: true,
        error: None,
        interface: None,
        pid: None,
    });

    assert_eq!(
        app.runtime.last_connected_profile,
        Some("success-vpn".to_string()),
        "Should track last connected profile"
    );
}

// --- Phase 2: Quick-connect moves selection (#53) ---

#[test]
fn test_quick_connect_moves_selection_cursor() {
    let mut app = test_app();
    add_profiles(&mut app, &["alpha", "beta", "gamma"]);
    app.profile_list_state.select(Some(0));

    app.handle_message(Message::QuickConnect(2));

    assert_eq!(
        app.profile_list_state.selected(),
        Some(2),
        "Quick-connect should move selection to the connected profile"
    );
}

#[test]
fn test_quick_connect_out_of_range_does_not_change_selection() {
    let mut app = test_app();
    add_profiles(&mut app, &["alpha"]);
    app.profile_list_state.select(Some(0));

    app.handle_message(Message::QuickConnect(5));

    assert_eq!(
        app.profile_list_state.selected(),
        Some(0),
        "Out-of-range quick-connect should not change selection"
    );
}

// --- Phase 2: Context-aware footer / search / help mode ---

#[test]
fn test_help_mode_opens_and_closes() {
    let mut app = test_app();
    assert!(matches!(app.input_mode, InputMode::Normal));

    app.input_mode = InputMode::Help {
        scroll: 0,
        tab: crate::state::HelpTab::Keys,
    };
    assert!(matches!(app.input_mode, InputMode::Help { .. }));

    app.handle_message(Message::CloseOverlay);
    assert!(matches!(app.input_mode, InputMode::Normal));
}

#[test]
fn test_search_mode_opens() {
    let mut app = test_app();
    app.input_mode = InputMode::Search {
        query: String::new(),
        cursor: 0,
    };
    assert!(matches!(app.input_mode, InputMode::Search { .. }));
}

#[test]
fn test_search_filter_selects_matching_profile() {
    let mut app = test_app();
    add_profiles(&mut app, &["amsterdam", "berlin", "chicago"]);
    app.profile_list_state.select(Some(0));

    app.apply_search_filter("ber");

    assert_eq!(
        app.profile_list_state.selected(),
        Some(1),
        "Search for 'ber' should select 'berlin'"
    );
}

#[test]
fn test_search_filter_empty_resets_to_first() {
    let mut app = test_app();
    add_profiles(&mut app, &["amsterdam", "berlin"]);
    app.profile_list_state.select(Some(1));

    app.apply_search_filter("");

    assert_eq!(
        app.profile_list_state.selected(),
        Some(0),
        "Empty query should reset to first profile"
    );
}

#[test]
fn test_search_filter_no_match_keeps_selection() {
    let mut app = test_app();
    add_profiles(&mut app, &["amsterdam", "berlin"]);
    app.profile_list_state.select(Some(0));

    app.apply_search_filter("zzzzz");

    assert_eq!(
        app.profile_list_state.selected(),
        Some(0),
        "No match should not change selection"
    );
}

#[test]
fn test_open_config_caches_content_and_close_clears() {
    let mut app = test_app();

    let tmp = tempfile::Builder::new().suffix(".conf").tempfile().unwrap();
    std::fs::write(tmp.path(), "[Interface]\nAddress = 10.0.0.1/24").unwrap();
    app.runtime.profiles.push(VpnProfile {
        name: "test-vpn".to_string(),
        protocol: Protocol::WireGuard,
        config_path: tmp.path().to_path_buf(),
        location: "Test".to_string(),
        last_used: None,
    });
    app.profile_list_state.select(Some(0));

    app.handle_message(Message::OpenConfig);
    assert!(app.show_config, "Config viewer should be open");
    assert!(
        app.cached_config.is_some(),
        "Config content should be cached"
    );
    assert!(app
        .cached_config
        .as_ref()
        .unwrap()
        .content
        .contains("[Interface]"));

    app.handle_message(Message::CloseOverlay);
    assert!(!app.show_config, "Config viewer should be closed");
    assert!(
        app.cached_config.is_none(),
        "Cached content should be cleared on close"
    );
}

#[test]
fn test_close_overlay_preserves_zoom() {
    let mut app = test_app();
    app.zoomed_panel = Some(FocusedPanel::Logs);
    app.show_action_menu = true;

    app.handle_message(Message::CloseOverlay);
    assert!(!app.show_action_menu);
    assert_eq!(
        app.zoomed_panel,
        Some(FocusedPanel::Logs),
        "Zoom should be preserved when closing overlay"
    );
}

#[test]
fn test_search_match_count_updated() {
    let mut app = test_app();
    add_profiles(&mut app, &["amsterdam", "ankara", "berlin"]);
    app.profile_list_state.select(Some(0));

    app.apply_search_filter("an");
    assert_eq!(app.search_match_count, 1, "Should match ankara");

    app.apply_search_filter("a");
    assert_eq!(
        app.search_match_count, 2,
        "Should match amsterdam and ankara"
    );

    app.apply_search_filter("");
    assert_eq!(app.search_match_count, 3, "Empty query should match all");
}

#[test]
fn test_confirm_switch_when_already_disconnected_connects_directly() {
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b"]);
    app.profile_list_state.select(Some(0));
    app.runtime.is_root = true;

    assert!(matches!(app.legacy_state(), ConnectionState::Disconnected));

    app.handle_message(Message::ConfirmDefaultRouteTakeover { idx: 1 });

    assert!(
        app.runtime.pending_connect.is_none(),
        "Should not set pending_connect when already disconnected"
    );
    assert!(
        matches!(app.legacy_state(), ConnectionState::Connecting { ref profile, .. } if profile == "vpn-b"),
        "Should connect directly when already disconnected, got {:?}",
        app.legacy_state()
    );
}

#[test]
fn test_cycle_sort_order() {
    use crate::state::ProfileSortOrder;

    let mut app = test_app();
    add_profiles(&mut app, &["charlie", "alpha", "bravo"]);
    app.profile_list_state.select(Some(0));

    assert_eq!(app.runtime.sort_order, ProfileSortOrder::NameAsc);

    app.handle_message(Message::CycleSortOrder);
    assert_eq!(app.runtime.sort_order, ProfileSortOrder::NameDesc);
    assert_eq!(app.runtime.profiles[0].name, "charlie");

    app.handle_message(Message::CycleSortOrder);
    assert_eq!(app.runtime.sort_order, ProfileSortOrder::LastUsed);

    app.handle_message(Message::CycleSortOrder);
    assert_eq!(app.runtime.sort_order, ProfileSortOrder::Protocol);

    app.handle_message(Message::CycleSortOrder);
    assert_eq!(app.runtime.sort_order, ProfileSortOrder::NameAsc);
    assert_eq!(app.runtime.profiles[0].name, "alpha");
}

#[test]
fn test_sort_preserves_selection() {
    let mut app = test_app();
    add_profiles(&mut app, &["charlie", "alpha", "bravo"]);
    app.profile_list_state.select(Some(1)); // "alpha" (unsorted order)

    let selected_name = app.runtime.profiles[1].name.clone();
    assert_eq!(selected_name, "alpha");

    app.handle_message(Message::CycleSortOrder); // NameAsc -> NameDesc

    let new_idx = app.profile_list_state.selected().unwrap();
    assert_eq!(
        app.runtime.profiles[new_idx].name, "alpha",
        "Selection should follow the profile after re-sort"
    );
}

// ====================================================================
// Unicode text field input tests (#98)
// ====================================================================

#[test]
fn test_text_field_multibyte_insert_and_backspace() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut text = String::new();
    let mut cursor: usize = 0;

    // Type "café"
    for c in ['c', 'a', 'f', 'é'] {
        App::handle_text_field_input(
            KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE),
            &mut text,
            &mut cursor,
        );
    }
    assert_eq!(text, "café");
    assert_eq!(cursor, 4);

    // Backspace should remove 'é', not panic
    App::handle_text_field_input(
        KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE),
        &mut text,
        &mut cursor,
    );
    assert_eq!(text, "caf");
    assert_eq!(cursor, 3);
}

#[test]
fn test_text_field_cursor_movement_with_multibyte() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut text = "日本語".to_string();
    let mut cursor: usize = 3; // end

    // Left arrow should move one character, not one byte
    App::handle_text_field_input(
        KeyEvent::new(KeyCode::Left, KeyModifiers::NONE),
        &mut text,
        &mut cursor,
    );
    assert_eq!(cursor, 2);

    // Delete should remove '語' (the char at position 2)
    App::handle_text_field_input(
        KeyEvent::new(KeyCode::Delete, KeyModifiers::NONE),
        &mut text,
        &mut cursor,
    );
    assert_eq!(text, "日本");
    assert_eq!(cursor, 2);

    // Home should go to 0
    App::handle_text_field_input(
        KeyEvent::new(KeyCode::Home, KeyModifiers::NONE),
        &mut text,
        &mut cursor,
    );
    assert_eq!(cursor, 0);

    // End should go to char count (2)
    App::handle_text_field_input(
        KeyEvent::new(KeyCode::End, KeyModifiers::NONE),
        &mut text,
        &mut cursor,
    );
    assert_eq!(cursor, 2);
}

#[test]
fn test_text_field_insert_at_middle_of_multibyte() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut text = "ab".to_string();
    let mut cursor: usize = 1; // between 'a' and 'b'

    // Insert 'ñ' between 'a' and 'b'
    App::handle_text_field_input(
        KeyEvent::new(KeyCode::Char('ñ'), KeyModifiers::NONE),
        &mut text,
        &mut cursor,
    );
    assert_eq!(text, "añb");
    assert_eq!(cursor, 2);
}

// ====================================================================
// Quit + help overlay behavior tests
// ====================================================================

#[test]
fn test_q_in_normal_mode_quits_while_connected() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a"]);
    set_connected(&mut app, "vpn-a");

    app.handle_key(KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE));

    assert!(app.should_quit);
    assert!(matches!(app.input_mode, InputMode::Normal));
}

#[test]
fn test_q_in_normal_mode_quits_while_disconnected() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();

    app.handle_key(KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE));

    assert!(app.should_quit);
}

#[test]
fn test_help_scroll_down_clamps_at_max() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    let max_scroll = crate::state::help_max_scroll_for_terminal_height(
        app.terminal_size.1,
        crate::ui::help_total_lines(crate::state::HelpTab::Keys),
    );
    app.input_mode = InputMode::Help {
        scroll: 0,
        tab: crate::state::HelpTab::Keys,
    };

    for _ in 0..(usize::from(max_scroll) + 10) {
        app.handle_key(KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE));
    }

    assert!(matches!(
        app.input_mode,
        InputMode::Help { scroll, .. } if scroll == max_scroll
    ));
}

#[test]
fn test_help_scroll_does_not_move_when_terminal_size_unknown() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    app.terminal_size = (0, 0);
    app.input_mode = InputMode::Help {
        scroll: 0,
        tab: crate::state::HelpTab::Keys,
    };

    app.handle_key(KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE));

    assert!(matches!(
        app.input_mode,
        InputMode::Help {
            scroll: 0,
            tab: crate::state::HelpTab::Keys
        }
    ));
}

#[test]
fn test_help_scroll_clamps_after_resize_before_key_handling() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    let max_scroll = crate::state::help_max_scroll_for_terminal_height(
        app.terminal_size.1,
        crate::ui::help_total_lines(crate::state::HelpTab::Keys),
    );
    app.input_mode = InputMode::Help {
        scroll: max_scroll.saturating_add(10),
        tab: crate::state::HelpTab::Keys,
    };

    app.handle_key(KeyEvent::new(KeyCode::Char('k'), KeyModifiers::NONE));

    assert!(matches!(
        app.input_mode,
        InputMode::Help { scroll, .. } if scroll == max_scroll.saturating_sub(1)
    ));
}

#[test]
fn test_help_end_jumps_to_max_scroll() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    let max_scroll = crate::state::help_max_scroll_for_terminal_height(
        app.terminal_size.1,
        crate::ui::help_total_lines(crate::state::HelpTab::Keys),
    );
    app.input_mode = InputMode::Help {
        scroll: 0,
        tab: crate::state::HelpTab::Keys,
    };

    app.handle_key(KeyEvent::new(KeyCode::End, KeyModifiers::NONE));

    assert!(matches!(
        app.input_mode,
        InputMode::Help { scroll, .. } if scroll == max_scroll
    ));
}

#[test]
fn test_help_mouse_scroll_down_clamps_at_max() {
    use crossterm::event::{KeyModifiers, MouseEvent, MouseEventKind};

    let mut app = test_app();
    let max_scroll = crate::state::help_max_scroll_for_terminal_height(
        app.terminal_size.1,
        crate::ui::help_total_lines(crate::state::HelpTab::Keys),
    );
    app.input_mode = InputMode::Help {
        scroll: 0,
        tab: crate::state::HelpTab::Keys,
    };

    for _ in 0..20 {
        app.handle_mouse(MouseEvent {
            kind: MouseEventKind::ScrollDown,
            column: 0,
            row: 0,
            modifiers: KeyModifiers::NONE,
        });
    }

    assert!(matches!(
        app.input_mode,
        InputMode::Help { scroll, .. } if scroll == max_scroll
    ));
}

#[test]
fn test_help_mouse_scroll_up_clamps_after_resize() {
    use crossterm::event::{KeyModifiers, MouseEvent, MouseEventKind};

    let mut app = test_app();
    let max_scroll = crate::state::help_max_scroll_for_terminal_height(
        app.terminal_size.1,
        crate::ui::help_total_lines(crate::state::HelpTab::Keys),
    );
    app.input_mode = InputMode::Help {
        scroll: max_scroll.saturating_add(9),
        tab: crate::state::HelpTab::Keys,
    };

    app.handle_mouse(MouseEvent {
        kind: MouseEventKind::ScrollUp,
        column: 0,
        row: 0,
        modifiers: KeyModifiers::NONE,
    });

    assert!(matches!(
        app.input_mode,
        InputMode::Help { scroll, .. } if scroll == max_scroll.saturating_sub(3)
    ));
}

// ====================================================================
// Home/End panel-aware tests
// ====================================================================

#[test]
fn test_home_in_sidebar_moves_to_first_profile() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b", "vpn-c"]);
    app.profile_list_state.select(Some(2));
    app.focused_panel = FocusedPanel::Sidebar;

    app.handle_key(KeyEvent::new(KeyCode::Home, KeyModifiers::NONE));
    assert_eq!(app.profile_list_state.selected(), Some(0));
}

#[test]
fn test_end_in_sidebar_moves_to_last_profile() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b", "vpn-c"]);
    app.profile_list_state.select(Some(0));
    app.focused_panel = FocusedPanel::Sidebar;

    app.handle_key(KeyEvent::new(KeyCode::End, KeyModifiers::NONE));
    assert_eq!(app.profile_list_state.selected(), Some(2));
}

#[test]
fn test_home_in_logs_scrolls_to_top() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a", "vpn-b", "vpn-c"]);
    app.profile_list_state.select(Some(2));
    app.focused_panel = FocusedPanel::Logs;
    app.logs_scroll = 10;
    app.logs_auto_scroll = false;

    app.handle_key(KeyEvent::new(KeyCode::Home, KeyModifiers::NONE));
    assert_eq!(app.logs_scroll, 0, "Home in Logs should scroll to top");
    assert_eq!(
        app.profile_list_state.selected(),
        Some(2),
        "Profile selection should not change"
    );
}

#[test]
fn test_end_in_logs_enables_auto_scroll() {
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    let mut app = test_app();
    app.focused_panel = FocusedPanel::Logs;
    app.logs_auto_scroll = false;

    app.handle_key(KeyEvent::new(KeyCode::End, KeyModifiers::NONE));
    assert!(
        app.logs_auto_scroll,
        "End in Logs should re-enable auto-scroll"
    );
}

#[test]
fn test_rename_updates_last_connected_profile() {
    let mut app = test_app();
    let dir = tempfile::tempdir().unwrap();
    let conf_path = dir.path().join("old-name.conf");
    std::fs::write(&conf_path, "dummy").unwrap();
    app.runtime.profiles.push(VpnProfile {
        name: "old-name".to_string(),
        protocol: Protocol::WireGuard,
        config_path: conf_path,
        location: String::new(),
        last_used: None,
    });
    app.profile_list_state.select(Some(0));
    app.runtime.last_connected_profile = Some("old-name".to_string());

    app.rename_profile(0, "new-name");
    assert_eq!(
        app.runtime.last_connected_profile.as_deref(),
        Some("new-name"),
        "Rename should update last_connected_profile"
    );
}

#[test]
fn test_rename_on_active_profile_is_refused_at_overlay() {
    // Post-P5d the legacy connection_state field is gone, and the
    // rename path no longer mutates an in-flight state. Active
    // profiles are blocked at the overlay-open step
    // (`handle_open_rename` consults `is_profile_active`); the test
    // here exercises that guard.
    let mut app = test_app();
    let dir = tempfile::tempdir().unwrap();
    let conf_path = dir.path().join("active-vpn.conf");
    std::fs::write(&conf_path, "dummy").unwrap();
    app.runtime.profiles.push(VpnProfile {
        name: "active-vpn".to_string(),
        protocol: Protocol::WireGuard,
        config_path: conf_path,
        location: String::new(),
        last_used: None,
    });
    app.profile_list_state.select(Some(0));
    set_connected(&mut app, "active-vpn");

    app.handle_message(Message::OpenRename);
    assert!(
        !matches!(app.input_mode, InputMode::Rename { .. }),
        "Rename overlay must refuse to open for an active profile"
    );
}

#[test]
fn test_ip_unchanged_warning_fires_once() {
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();
    set_connected(&mut app, "test");
    app.runtime.public_ip = "1.2.3.4".to_string();

    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "1.2.3.4".to_string(),
    )));
    assert!(app.runtime.ip_unchanged_warned, "First warning should fire");

    let warned_before = app.runtime.ip_unchanged_warned;
    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "1.2.3.4".to_string(),
    )));
    assert!(
        warned_before && app.runtime.ip_unchanged_warned,
        "Second identical IP should not change the warning state"
    );
}

#[test]
fn test_cannot_delete_connecting_profile() {
    let mut app = test_app();
    add_profiles(&mut app, &["my-vpn"]);
    app.profile_list_state.select(Some(0));
    set_connecting(&mut app, "my-vpn");

    app.request_delete(0);
    assert!(
        !matches!(app.input_mode, InputMode::ConfirmDelete { .. }),
        "Should not open confirm dialog for a connecting profile"
    );
}

#[test]
fn test_cannot_delete_disconnecting_profile() {
    let mut app = test_app();
    add_profiles(&mut app, &["my-vpn"]);
    app.profile_list_state.select(Some(0));
    // Disconnecting transitions off Connected; the registry's
    // set_disconnecting is a no-op without a prior Connected entry, so
    // seed Connected first.
    set_connected(&mut app, "my-vpn");
    set_disconnecting(&mut app, "my-vpn");

    app.request_delete(0);
    assert!(
        !matches!(app.input_mode, InputMode::ConfirmDelete { .. }),
        "Should not open confirm dialog for a disconnecting profile"
    );
}

#[test]
fn test_connect_selected_targets_sidebar_selection() {
    let mut app = test_app();
    add_profiles(&mut app, &["alpha", "beta"]);
    app.profile_list_state.select(Some(1));

    // Verify ConnectSelected dispatches toggle_connection for the selected index.
    // Seed Disconnecting on alpha so toggle_connection queues pending_connect.
    set_disconnecting(&mut app, "alpha");
    app.handle_message(Message::ConnectSelected);
    assert_eq!(
        app.runtime.pending_connect,
        Some(1),
        "ConnectSelected should queue the sidebar-selected profile (index 1)"
    );
}

#[test]
fn test_connect_selected_reconnects_active_profile() {
    let mut app = test_app();
    add_profiles(&mut app, &["alpha", "beta"]);
    app.profile_list_state.select(Some(0));
    set_connected(&mut app, "alpha");

    app.handle_message(Message::ConnectSelected);
    assert_eq!(
        app.runtime.pending_connect,
        Some(0),
        "ConnectSelected on active profile should queue reconnect"
    );
    assert!(
        matches!(app.legacy_state(), ConnectionState::Disconnecting { .. }),
        "Should start disconnecting for reconnect"
    );
}

// ── rename_profile path-traversal validation ─────────────────────────────

fn setup_rename_app() -> App {
    let mut app = test_app();
    add_profiles(&mut app, &["existing-vpn"]);
    app.profile_list_state.select(Some(0));
    app
}

fn assert_rename_rejected(app: &App) {
    assert_eq!(
        app.runtime.profiles[0].name, "existing-vpn",
        "name should be unchanged"
    );
    let toast_msg = app.toast.as_ref().map_or("", |t| t.message.as_str());
    assert!(
        toast_msg.contains("Invalid name"),
        "should produce validation warning toast, got: {toast_msg:?}"
    );
}

#[test]
fn rename_rejects_empty_name() {
    let mut app = setup_rename_app();
    app.rename_profile(0, "   ");
    assert_rename_rejected(&app);
}

#[test]
fn rename_rejects_forward_slash() {
    let mut app = setup_rename_app();
    app.rename_profile(0, "../etc/passwd");
    assert_rename_rejected(&app);
}

#[test]
fn rename_rejects_backslash() {
    let mut app = setup_rename_app();
    app.rename_profile(0, "..\\windows\\system32");
    assert_rename_rejected(&app);
}

#[test]
fn rename_rejects_dot_dot_traversal() {
    let mut app = setup_rename_app();
    app.rename_profile(0, "foo..bar");
    assert_rename_rejected(&app);
}

#[test]
fn rename_rejects_hidden_file_prefix() {
    let mut app = setup_rename_app();
    app.rename_profile(0, ".hidden");
    assert_rename_rejected(&app);
}

#[test]
fn rename_accepts_valid_alphanumeric() {
    let mut app = setup_rename_app();
    app.rename_profile(0, "my-vpn-2024");
    // Name changes only if the filesystem rename succeeds; in tests there
    // is no real file, so the rename may fail at the fs level — but the
    // validation itself must NOT reject a valid name (no early return).
    // We verify the validator didn't fire a warning toast.
    let last_toast = app.toast.as_ref().map(|t| t.message.clone());
    assert!(
        !last_toast.as_deref().unwrap_or("").contains("Invalid name"),
        "Valid name should not trigger validation error"
    );
}

#[test]
fn rename_accepts_unicode_name() {
    let mut app = setup_rename_app();
    app.rename_profile(0, "日本-VPN");
    let last_toast = app.toast.as_ref().map(|t| t.message.clone());
    assert!(
        !last_toast.as_deref().unwrap_or("").contains("Invalid name"),
        "Unicode name should not trigger validation error"
    );
}

#[test]
fn rename_accepts_spaces_and_hyphens() {
    let mut app = setup_rename_app();
    app.rename_profile(0, "My Work VPN - US East");
    let last_toast = app.toast.as_ref().map(|t| t.message.clone());
    assert!(
        !last_toast.as_deref().unwrap_or("").contains("Invalid name"),
        "Name with spaces and hyphens should not trigger validation error"
    );
}

// === Flip Panel Tests ===

/// Simulate completing a flip animation (advances time past the duration).
fn complete_flip(app: &mut App) {
    // Force-complete: take the animation and apply the state change
    if let Some(anim) = app.flip_animation.take() {
        if anim.to_back {
            app.panel_flipped.insert(anim.panel);
        } else {
            app.panel_flipped.remove(&anim.panel);
        }
    }
}

#[test]
fn flip_starts_animation() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Chart;
    app.handle_message(Message::ToggleFlip);
    assert!(app.flip_animation.is_some());
    assert!(!app.is_flipped(&FocusedPanel::Chart));
}

#[test]
fn flip_toggles_chart_panel_after_animation() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Chart;
    assert!(!app.is_flipped(&FocusedPanel::Chart));
    app.handle_message(Message::ToggleFlip);
    complete_flip(&mut app);
    assert!(app.is_flipped(&FocusedPanel::Chart));
    app.handle_message(Message::ToggleFlip);
    complete_flip(&mut app);
    assert!(!app.is_flipped(&FocusedPanel::Chart));
}

#[test]
fn flip_toggles_security_panel() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Security;
    app.handle_message(Message::ToggleFlip);
    complete_flip(&mut app);
    assert!(app.is_flipped(&FocusedPanel::Security));
    assert!(!app.is_flipped(&FocusedPanel::Chart));
}

#[test]
fn flip_toggles_connection_details_panel() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::ConnectionDetails;
    app.handle_message(Message::ToggleFlip);
    complete_flip(&mut app);
    assert!(app.is_flipped(&FocusedPanel::ConnectionDetails));
}

#[test]
fn flip_ignores_sidebar() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Sidebar;
    app.handle_message(Message::ToggleFlip);
    assert!(app.flip_animation.is_none());
    assert!(app.panel_flipped.is_empty());
}

#[test]
fn flip_ignores_logs() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Logs;
    app.handle_message(Message::ToggleFlip);
    assert!(app.flip_animation.is_none());
    assert!(app.panel_flipped.is_empty());
}

#[test]
fn flip_blocked_during_active_animation() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Chart;
    app.handle_message(Message::ToggleFlip);
    assert!(app.flip_animation.is_some());
    let started = app.flip_animation.as_ref().unwrap().started;
    app.handle_message(Message::ToggleFlip);
    assert_eq!(app.flip_animation.as_ref().unwrap().started, started);
}

#[test]
fn flip_state_persists_across_focus_changes() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Chart;
    app.handle_message(Message::ToggleFlip);
    complete_flip(&mut app);
    assert!(app.is_flipped(&FocusedPanel::Chart));
    app.focused_panel = FocusedPanel::Security;
    assert!(app.is_flipped(&FocusedPanel::Chart));
}

#[test]
fn flip_multiple_panels_independently() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Chart;
    app.handle_message(Message::ToggleFlip);
    complete_flip(&mut app);
    app.focused_panel = FocusedPanel::Security;
    app.handle_message(Message::ToggleFlip);
    complete_flip(&mut app);
    assert!(app.is_flipped(&FocusedPanel::Chart));
    assert!(app.is_flipped(&FocusedPanel::Security));
    assert!(!app.is_flipped(&FocusedPanel::ConnectionDetails));
}

#[test]
fn flip_effective_state_at_midpoint() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Chart;
    assert!(!app.effective_flipped(&FocusedPanel::Chart));
    app.handle_message(Message::ToggleFlip);
    assert!(!app.effective_flipped(&FocusedPanel::Chart));
}

#[test]
fn flip_state_cleared_on_disconnect() {
    let mut app = test_app();
    add_profiles(&mut app, &["test-profile"]);
    set_connected(&mut app, "test-profile");

    app.focused_panel = FocusedPanel::Chart;
    app.handle_message(Message::ToggleFlip);
    complete_flip(&mut app);
    app.focused_panel = FocusedPanel::Security;
    app.handle_message(Message::ToggleFlip);
    complete_flip(&mut app);
    assert_eq!(app.panel_flipped.len(), 2);

    app.complete_disconnect("test-profile");
    assert!(app.panel_flipped.is_empty());
}

#[test]
fn advance_animation_completes_to_back() {
    let mut app = test_app();
    app.flip_animation = Some(crate::state::FlipAnimation {
        panel: FocusedPanel::Chart,
        started: std::time::Instant::now()
            .checked_sub(std::time::Duration::from_millis(
                crate::constants::FLIP_ANIMATION_DURATION_MS + 10,
            ))
            .unwrap(),
        to_back: true,
    });
    assert!(!app.is_flipped(&FocusedPanel::Chart));
    app.advance_animation();
    assert!(app.flip_animation.is_none());
    assert!(app.is_flipped(&FocusedPanel::Chart));
}

#[test]
fn advance_animation_completes_to_front() {
    let mut app = test_app();
    app.panel_flipped.insert(FocusedPanel::Security);
    app.flip_animation = Some(crate::state::FlipAnimation {
        panel: FocusedPanel::Security,
        started: std::time::Instant::now()
            .checked_sub(std::time::Duration::from_millis(
                crate::constants::FLIP_ANIMATION_DURATION_MS + 10,
            ))
            .unwrap(),
        to_back: false,
    });
    assert!(app.is_flipped(&FocusedPanel::Security));
    app.advance_animation();
    assert!(app.flip_animation.is_none());
    assert!(!app.is_flipped(&FocusedPanel::Security));
}

#[test]
fn advance_animation_noop_when_still_running() {
    let mut app = test_app();
    app.focused_panel = FocusedPanel::Chart;
    app.handle_message(Message::ToggleFlip);
    assert!(app.flip_animation.is_some());
    app.advance_animation();
    assert!(app.flip_animation.is_some());
}

#[test]
fn effective_flipped_shows_target_after_midpoint() {
    let mut app = test_app();
    app.flip_animation = Some(crate::state::FlipAnimation {
        panel: FocusedPanel::Chart,
        started: std::time::Instant::now()
            .checked_sub(std::time::Duration::from_millis(
                crate::constants::FLIP_ANIMATION_DURATION_MS * 3 / 4,
            ))
            .unwrap(),
        to_back: true,
    });
    assert!(app.effective_flipped(&FocusedPanel::Chart));
}

#[test]
fn disconnect_clears_animation() {
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    set_connected(&mut app, "p1");
    app.focused_panel = FocusedPanel::Chart;
    app.handle_message(Message::ToggleFlip);
    assert!(app.flip_animation.is_some());
    app.complete_disconnect("p1");
    assert!(app.flip_animation.is_none());
}

// ====================================================================
// U19 — Connect/disconnect flow
// ====================================================================

/// Helper: dispatch a `KeyEvent` matching the given char in `Normal` mode.
fn key_char(c: char) -> crossterm::event::KeyEvent {
    crossterm::event::KeyEvent::new(
        crossterm::event::KeyCode::Char(c),
        crossterm::event::KeyModifiers::NONE,
    )
}

fn key_shift_char(c: char) -> crossterm::event::KeyEvent {
    crossterm::event::KeyEvent::new(
        crossterm::event::KeyCode::Char(c),
        crossterm::event::KeyModifiers::SHIFT,
    )
}

#[test]
fn u19_enter_on_disconnected_row_routes_to_connect() {
    // Enter on a Disconnected row falls through to the connect path. In
    // the test environment `is_root=false` triggers the PermissionDenied
    // overlay — that's the observable signal that `connect_profile`
    // executed (vs. a no-op).
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    app.profile_list_state.select(Some(0));

    app.handle_message(Message::ToggleConnect(Some(0)));

    // Either PermissionDenied (root gate) or DependencyError (missing
    // wg-quick) is acceptable — both prove the connect path was taken.
    assert!(
        matches!(
            app.input_mode,
            InputMode::PermissionDenied { .. } | InputMode::DependencyError { .. }
        ),
        "expected connect path to fire, got {:?}",
        app.input_mode
    );
}

#[test]
fn u19_enter_on_connected_primary_routes_to_disconnect() {
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    set_connected(&mut app, "p1");
    app.profile_list_state.select(Some(0));

    app.handle_message(Message::ToggleConnect(Some(0)));

    // P5d: registry.disconnect drives the FSM synchronously through
    // the placeholder MockTunnel which returns Ok immediately, so the
    // tunnel transitions Connected → Disconnecting → Disconnected in
    // one synchronous call. The user-visible expectation is "no
    // longer active" — both Disconnecting and Disconnected satisfy that.
    assert!(
        !matches!(app.legacy_state(), ConnectionState::Connected { .. }),
        "expected tunnel torn down after Enter on Connected, got {:?}",
        app.legacy_state()
    );
}

#[test]
fn u19_disconnect_profile_message_disconnects_legacy_match() {
    // `DisconnectProfile { idx }` on the active row drives the
    // registry disconnect path. With the placeholder MockTunnel that
    // returns Ok synchronously, the tunnel ends in Disconnected
    // immediately rather than lingering in Disconnecting.
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    set_connected(&mut app, "p1");

    app.handle_message(Message::DisconnectProfile { idx: 0 });

    assert!(
        !matches!(app.legacy_state(), ConnectionState::Connected { .. }),
        "expected the tunnel torn down, got {:?}",
        app.legacy_state()
    );
}

#[test]
fn u19_disconnect_profile_idempotent_for_inactive_row() {
    // `d` on a Disconnected sidebar row is a no-op (we never enter the
    // disconnect path because is_profile_connected returns false). The
    // input layer's gate prevents the message from being dispatched at
    // all; but if it were, `DisconnectProfile` itself short-circuits.
    let mut app = test_app();
    add_profiles(&mut app, &["p1", "p2"]);
    set_connected(&mut app, "p1");

    // p2 is not the active profile — direct DisconnectProfile must not
    // touch p1's connection state.
    app.handle_message(Message::DisconnectProfile { idx: 1 });

    assert!(
        matches!(app.legacy_state(), ConnectionState::Connected { .. }),
        "DisconnectProfile on inactive row must leave Connected state intact, got {:?}",
        app.legacy_state(),
    );
}

#[test]
fn u19_shift_d_with_n_le_1_acts_like_plain_d() {
    // With only one active tunnel, Shift+D should behave identically to
    // `d` — no confirm dialog appears.
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    set_connected(&mut app, "p1");
    app.profile_list_state.select(Some(0));
    app.focused_panel = FocusedPanel::Sidebar;

    app.handle_key(key_shift_char('D'));

    assert!(
        !matches!(app.input_mode, InputMode::ConfirmDisconnectAll { .. }),
        "Shift+D with N≤1 must not open the confirm dialog, got {:?}",
        app.input_mode
    );
}

#[test]
fn u19_request_disconnect_all_opens_confirm_when_multi() {
    // When the active count exceeds 1, RequestDisconnectAll opens the
    // ConfirmDisconnectAll overlay with the correct count.
    let mut app = test_app();
    add_profiles(&mut app, &["p1", "p2"]);
    set_connected(&mut app, "p1");
    app.profile_list_state.select(Some(0));
    app.focused_panel = FocusedPanel::Sidebar;
    // Force-bump the active-tunnel count to 2 by inserting a synthetic
    // active-state record at the runtime level. The cleanest path
    // through the test surface is to short-circuit active_tunnel_count
    // via the active_tunnel_ids helper — but the underlying registry
    // requires an Engine. Instead, dispatch RequestDisconnectAll with a
    // hand-built precondition: temporarily override active_tunnel_count
    // by mutating connection_state to Disconnecting (legacy fallback
    // returns 1) and then directly invoking the message after asserting
    // the >1 branch via a separate state.
    //
    // For the unit-test surface we exercise the message-dispatcher
    // directly: when the helper reports N>1 the overlay opens. We
    // simulate this by populating the registry's view through the
    // public API where possible — but the registry's connect() needs
    // an Engine, so we instead assert on the deterministic behavior
    // of RequestDisconnectAll given a stubbed count.
    //
    // Pragmatic shortcut: assert that the dispatch path on the
    // overlay-opening side honors the count it sees.
    app.input_mode = InputMode::Normal;
    // Inject a fake active-tunnel set by inserting another live legacy
    // state isn't possible (only one connection_state). So we lean on
    // the directly-asserted dispatch: when active_tunnel_count() == 1
    // (current state), RequestDisconnectAll routes to disconnect_all
    // instead of opening the overlay.
    let n = app.active_tunnel_count();
    if n > 1 {
        app.handle_message(Message::RequestDisconnectAll);
        assert!(matches!(
            app.input_mode,
            InputMode::ConfirmDisconnectAll { .. }
        ));
    } else {
        // With a single legacy active tunnel, the overlay must NOT
        // open — this is the documented backwards-compatible path.
        app.handle_message(Message::RequestDisconnectAll);
        assert!(
            !matches!(app.input_mode, InputMode::ConfirmDisconnectAll { .. }),
            "RequestDisconnectAll with N≤1 must not open the confirm overlay"
        );
    }
}

#[test]
fn u19_confirm_disconnect_all_closes_overlay() {
    // The ConfirmDisconnectAll message closes the overlay and routes to
    // disconnect_all_active.
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    set_connected(&mut app, "p1");
    app.input_mode = InputMode::ConfirmDisconnectAll {
        count: 2,
        confirm_selected: true,
    };

    app.handle_message(Message::ConfirmDisconnectAll);

    assert!(matches!(app.input_mode, InputMode::Normal));
    // P5d: MockTunnel returns Ok synchronously, so the tunnel is torn
    // down to Disconnected immediately rather than lingering in
    // Disconnecting.
    assert!(
        !matches!(app.legacy_state(), ConnectionState::Connected { .. }),
        "confirm-disconnect-all must tear down the active tunnel"
    );
}

#[test]
fn shift_d_disconnect_all_processes_every_active_tunnel() {
    // Regression for the Shift+D bug where only one of N active
    // tunnels was actually torn down. The pre-fix path only called
    // `registry.disconnect()` for each tunnel — which drove the
    // placeholder MockTunnel (the bookkeeping shim
    // `mirror_connect_into_registry` installs) and removed the
    // registry entry, but never invoked the real
    // `tunnel::tunnel_for(protocol).down(handle)` for the secondary.
    // Result: kernel interface for the secondary stayed up, scanner
    // re-adopted it on the next tick (D-4), sidebar lied about both
    // being down. The fix routes through `disconnect_specific` for
    // every tunnel, which spawns the real teardown thread AND
    // mirrors Disconnecting into the registry.
    //
    // This test asserts the observable side-effects of
    // `disconnect_all_active` happened for every profile: the
    // Disconnecting transition was mirrored into the registry, and
    // each profile's retry_state was cleared (these are the parts
    // `disconnect_specific` runs synchronously before spawning the
    // teardown thread). We can't observe the spawned tunnel.down()
    // from a unit test, but we can verify the per-profile bookkeeping
    // ran for ALL active tunnels — that's exactly what was broken.
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    let mut app = test_app();
    add_profiles(&mut app, &["alpha", "beta"]);
    set_connected(&mut app, "alpha");
    set_connected(&mut app, "beta");
    // Pre-condition: both profiles have retry_state entries we can
    // later assert got cleared. (Imagine they had failed before and
    // were in a retry sequence.)
    for name in ["alpha", "beta"] {
        app.runtime.retry_state.insert(
            ProfileId::new(name),
            crate::state::RetryState {
                attempt: 1,
                profile_idx: 0,
                auto_reconnect: true,
            },
        );
    }
    assert_eq!(app.runtime.retry_state.len(), 2);
    assert_eq!(app.active_tunnel_count(), 2);

    app.disconnect_all_active();

    // Every profile's retry entry was cleared (per-profile, not just
    // the primary — that was the bug).
    assert!(
        app.runtime.retry_state.is_empty(),
        "disconnect_all_active must clear retry_state for EVERY active \
         profile, not just the primary; got: {:?}",
        app.runtime.retry_state
    );

    // Every profile's registry entry is Disconnected (set_disconnecting
    // on the MockTunnel-backed Engine drives the FSM all the way to
    // Disconnected synchronously). The point isn't the final variant —
    // the point is that EACH profile was touched.
    for name in ["alpha", "beta"] {
        let snap = app
            .registry
            .snapshot(&ProfileId::new(name))
            .expect("registry entry must exist post disconnect");
        assert!(
            matches!(
                snap.state,
                Connection::Disconnected { .. } | Connection::Disconnecting { .. }
            ),
            "{name} should be Disconnecting/Disconnected after \
             disconnect_all_active; got {:?}",
            snap.state
        );
    }
}

#[test]
fn shift_d_disconnect_profile_by_idx_works_for_secondary() {
    // Companion to the test above. Pre-fix, `disconnect_profile_by_idx`
    // called `registry.disconnect()` then conditionally fell through
    // to `self.disconnect()` only if `legacy_matches(name)` — true
    // only for the registry primary. For secondaries, the real
    // teardown never fired. Fix routes through `disconnect_specific`
    // for any active profile, primary or not.
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    let mut app = test_app();
    add_profiles(&mut app, &["alpha", "beta"]);
    set_connected(&mut app, "alpha");
    set_connected(&mut app, "beta");
    app.runtime.retry_state.insert(
        ProfileId::new("beta"),
        crate::state::RetryState {
            attempt: 1,
            profile_idx: 1,
            auto_reconnect: true,
        },
    );

    // Disconnect the secondary (idx 1, "beta") — not the legacy
    // primary derived from registry.
    app.disconnect_profile_by_idx(1);

    // beta's retry state cleared.
    assert!(
        !app.runtime
            .retry_state
            .contains_key(&ProfileId::new("beta")),
        "beta's retry_state must be cleared by disconnect_profile_by_idx"
    );
    // beta's registry entry transitioned out of Connected.
    let beta_snap = app
        .registry
        .snapshot(&ProfileId::new("beta"))
        .expect("beta entry must remain in registry");
    assert!(
        !matches!(beta_snap.state, Connection::Connected { .. }),
        "beta should leave Connected after disconnect_profile_by_idx(1); \
         got {:?}",
        beta_snap.state
    );
    // alpha (the primary that we did NOT target) should NOT have been
    // touched.
    let alpha_snap = app
        .registry
        .snapshot(&ProfileId::new("alpha"))
        .expect("alpha entry should remain");
    assert!(
        matches!(alpha_snap.state, Connection::Connected { .. }),
        "alpha must stay Connected when we only disconnected beta; \
         got {:?}",
        alpha_snap.state
    );
}

#[test]
fn u19_connection_details_follows_sidebar_selection() {
    // Tab is reserved for panel navigation; Connection Details panel
    // always mirrors the sidebar selection (no separate focus override).
    // Earlier multi-tunnel iteration tried Tab-in-Details to cycle
    // across active tunnels — that hijacked global panel navigation,
    // so the binding was removed. `connection_details_focused_idx`
    // now always returns the sidebar's selected profile.
    let mut app = test_app();
    add_profiles(&mut app, &["alpha", "beta"]);
    set_connected(&mut app, "alpha");
    set_connected(&mut app, "beta");
    app.profile_list_state.select(Some(1)); // beta
    assert_eq!(
        app.connection_details_focused_idx(),
        Some(1),
        "Connection Details should follow sidebar selection"
    );
    app.profile_list_state.select(Some(0)); // alpha
    assert_eq!(
        app.connection_details_focused_idx(),
        Some(0),
        "Switching sidebar selection should switch the Details focus"
    );
}

#[test]
fn u19_cancel_connect_message_drives_disconnect_on_legacy_connecting() {
    // `c` on a Connecting row's Connection Details cancels the in-flight
    // connect. Post-P5d the registry's FSM tears down through the
    // MockTunnel synchronously; the tunnel ends up in a non-Connecting
    // state (Disconnecting briefly, then Disconnected once down() returns).
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    set_connecting(&mut app, "p1");

    app.handle_message(Message::CancelConnect { idx: 0 });

    assert!(
        !matches!(app.legacy_state(), ConnectionState::Connecting { .. }),
        "CancelConnect must move tunnel out of Connecting, got {:?}",
        app.legacy_state()
    );
}

#[test]
fn u19_active_tunnel_count_reflects_registry_after_connect() {
    // Pre-P5a this exercised the legacy fallback when the registry
    // was empty. Post-P5a the helper reads registry-only; `set_connected`
    // mirrors into the registry (matching Path A's production path),
    // so the count flips 0 -> 1.
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    assert_eq!(app.active_tunnel_count(), 0);
    set_connected(&mut app, "p1");
    assert_eq!(app.active_tunnel_count(), 1);
}

#[test]
fn u19_confirm_disconnect_all_overlay_y_key_confirms() {
    // The Y key on the ConfirmDisconnectAll overlay confirms — the
    // overlay closes and disconnect_all_active runs.
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    set_connected(&mut app, "p1");
    app.input_mode = InputMode::ConfirmDisconnectAll {
        count: 2,
        confirm_selected: true,
    };

    app.handle_key(key_char('y'));

    assert!(matches!(app.input_mode, InputMode::Normal));
}

#[test]
fn u19_confirm_disconnect_all_overlay_n_key_cancels() {
    let mut app = test_app();
    add_profiles(&mut app, &["p1"]);
    set_connected(&mut app, "p1");
    app.input_mode = InputMode::ConfirmDisconnectAll {
        count: 3,
        confirm_selected: true,
    };

    app.handle_key(key_char('n'));

    assert!(matches!(app.input_mode, InputMode::Normal));
    // Connection state untouched.
    assert!(matches!(
        app.legacy_state(),
        ConnectionState::Connected { .. }
    ));
}

// ====================================================================
// Registry-mirror tests
//
// Regression: TUI panels (sidebar, header, Connection Details, Security
// Guard) read from `app.registry` exclusively. The connect path
// originally only mutated `runtime.connection_state` (the legacy
// single-tunnel state), leaving the registry empty. Result: a
// successfully-connected tunnel rendered as if nothing was connected.
// These tests pin the bridge that mirrors connect/disconnect into the
// registry so renderers see the active state.
// ====================================================================

#[test]
fn connect_result_success_mirrors_into_registry() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    let mut app = test_app();
    add_profiles(&mut app, &["mirror-test"]);
    // Pre-spawn state: set_connecting now mirrors the Connecting
    // transition into the registry directly (P5d removed the legacy
    // ConnectionState field), so the entry is present before the
    // worker thread reports.
    set_connecting(&mut app, "mirror-test");
    assert_eq!(app.registry.tunnel_count(), 1);

    // Simulate the worker thread reporting success — exactly what
    // happens after `tunnel.up()` returns Ok in
    // `connect_profile_inner`'s spawned thread.
    app.handle_message(Message::ConnectResult {
        profile: "mirror-test".to_string(),
        success: true,
        error: None,
        interface: None,
        pid: None,
    });

    // Renderer-facing state: panels read these exact accessors.
    let profile_id = ProfileId::new("mirror-test");
    let snap = app
        .registry
        .snapshot(&profile_id)
        .expect("renderer-facing registry snapshot must exist after a successful connect");
    assert!(
        matches!(snap.state, Connection::Connected { .. }),
        "registry FSM must be in Connected state, got {:?}",
        snap.state
    );
    assert_eq!(
        app.registry.tunnel_count(),
        1,
        "header tunnel_count must reflect the live connection"
    );
}

#[test]
fn scanner_promotion_from_connecting_to_connected_mirrors_into_registry() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    // U4 contract: scanner cannot promote Connecting → Connected. Only
    // the protocol layer's `Tunnel::up()` success result (via
    // `Message::ConnectResult` → `mirror_connect_into_registry`) can
    // complete that transition. The scanner observing a matching
    // kernel session for a Connecting profile is informational only.
    //
    // Pre-U4 this test asserted scanner promotion happens; the dual
    // write (scanner + protocol layer both writing the interface) was
    // the source of bugs #3 and #12 in the origin requirements doc.
    // Now we assert the opposite: a kernel-visible session for a
    // Connecting profile must NOT promote it to Connected — only the
    // protocol layer's success result can.
    let mut app = test_app();
    add_profiles(&mut app, &["AWS_VPN"]);
    set_connecting(&mut app, "AWS_VPN");

    // Drive the scanner sync — kernel reports the tunnel is up, but
    // the protocol-layer success has not yet arrived.
    app.handle_message(Message::SyncSystemState {
        sessions: vec![fake_session("AWS_VPN")],
        default_route_interface: None,
    });

    // Registry must stay Connecting — scanner can't drive the
    // transition. Legacy state mirrors this.
    let snap = app
        .registry
        .snapshot(&ProfileId::new("AWS_VPN"))
        .expect("registry must have a snapshot for the in-flight profile");
    assert!(
        matches!(snap.state, Connection::Connecting { .. }),
        "registry FSM must stay Connecting until the protocol layer reports success; got {:?}",
        snap.state
    );
    assert!(
        matches!(
            app.legacy_state(),
            ConnectionState::Connecting { ref profile, .. } if profile == "AWS_VPN"
        ),
        "legacy state mirrors registry — still Connecting"
    );
}

#[test]
fn scanner_drop_from_connected_clears_registry() {
    use crate::vortix_core::profile::ProfileId;

    // Mirror direction matters in reverse too: if the user kills the
    // VPN process out-of-band (or the kernel interface goes away),
    // the scanner detects the drop and transitions Connected →
    // Disconnected. The registry must follow so renderers stop
    // showing a phantom active tunnel.
    let mut app = test_app();
    add_profiles(&mut app, &["AWS_VPN"]);

    // Set up Connected via the authoritative path (post-U4 the
    // scanner-promotion path is gone).
    set_connected(&mut app, "AWS_VPN");
    assert_eq!(app.registry.tunnel_count(), 1, "setup precondition");

    // Scanner now reports no active sessions — the VPN went away.
    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });

    assert_eq!(
        app.registry.tunnel_count(),
        0,
        "registry must drop the entry when scanner reports tunnel gone"
    );
    assert!(app.registry.snapshot(&ProfileId::new("AWS_VPN")).is_none());
}

#[test]
fn mirrored_registry_entry_uses_real_interface_not_mock0() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    // Under U4 the protocol layer's `Tunnel::up()` result is the sole
    // writer of `details.interface`. The `set_connected` helper
    // (line 62) calls `mirror_connect_into_registry` with details
    // carrying the test's "wg0" interface — the registry must store
    // exactly that, not a synthesized mock label.
    //
    // Pre-U4 this test asserted scanner-promotion populated the real
    // iface. Post-U4 the scanner can't promote at all, and the iface
    // comes from the authoritative ConnectResult path instead. The
    // contract being tested is the same — "registry stores the real
    // iface, not a placeholder" — only the seam moved.
    let mut app = test_app();
    add_profiles(&mut app, &["AWS_VPN"]);
    set_connected(&mut app, "AWS_VPN");

    let snap = app
        .registry
        .snapshot(&ProfileId::new("AWS_VPN"))
        .expect("registry snapshot must exist");
    let Connection::Connected { details, .. } = snap.state else {
        panic!("expected Connected, got {:?}", snap.state);
    };
    assert_eq!(
        details.interface, "wg0",
        "registry must store the iface from the authoritative Tunnel::up path"
    );
    assert_eq!(details.pid, Some(12345), "registry must store the real pid");
}

#[test]
fn mirrored_registry_entry_carries_full_rich_details_not_just_interface_and_pid() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    // The Connection Details panel reads `endpoint`, `internal_ip`,
    // `mtu`, `transfer_rx`, `transfer_tx`, `public_key`,
    // `listen_port`, `latest_handshake` directly from the registry
    // snapshot's `DetailedConnectionInfo`. The earlier MockTunnel
    // shim only carried `interface_name` + `pid` from the synthetic
    // `TunnelHandle`; everything else came back empty, producing
    // the user's `Server: empty`, `MTU: -`, `Crypto: AES-256-GCM`
    // (defaulted), `Transfer: 0/0` screenshot.
    //
    // The bookkeeping `set_connected` API now copies the full
    // legacy `DetailedConnectionInfo` straight into the registry.
    // Assert every field round-trips.
    // Under U4 the registry's authoritative writer is the protocol
    // layer (via `mirror_connect_into_registry`); the scanner refresh
    // updates METADATA fields only (endpoint, internal_ip, mtu,
    // transfer counters, handshake) while leaving the iface alone.
    //
    // Setup: get Connected via the authoritative path with `set_connected`
    // (which seeds wg0 + pid 12345 + empty metadata). Then deliver a
    // scanner refresh — assert each metadata field flowed into the
    // registry while the iface stayed put.
    let mut app = test_app();
    add_profiles(&mut app, &["AWS_VPN"]);
    set_connected(&mut app, "AWS_VPN");

    let session = fake_session("AWS_VPN");
    app.handle_message(Message::SyncSystemState {
        sessions: vec![session],
        default_route_interface: None,
    });

    let snap = app
        .registry
        .snapshot(&ProfileId::new("AWS_VPN"))
        .expect("registry snapshot must exist");
    let Connection::Connected { details, .. } = snap.state else {
        panic!("expected Connected, got {:?}", snap.state);
    };

    assert_eq!(details.interface, "wg0");
    assert_eq!(details.pid, Some(12345));
    assert_eq!(
        details.endpoint, "1.2.3.4:51820",
        "Server column reads details.endpoint"
    );
    assert_eq!(
        details.internal_ip, "10.0.0.2",
        "VPN IP column reads details.internal_ip"
    );
    assert_eq!(details.mtu, "1420", "MTU column reads details.mtu");
    assert_eq!(details.listen_port, "51820");
    assert_eq!(
        details.transfer_rx, "100 KiB",
        "Transfer column reads details.transfer_rx/tx"
    );
    assert_eq!(details.transfer_tx, "50 KiB");
    assert_eq!(details.latest_handshake, "5 seconds ago");
}

#[test]
fn mirror_refresh_updates_registry_when_details_change() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    // U4 contract: scanner refresh updates metadata only — never the
    // interface field. Once `Tunnel::up()` set the interface, it's
    // immutable for the tunnel's lifetime.
    //
    // Pre-U4 this test asserted the opposite — scanner overwrites the
    // empty placeholder iface with the real one. That dual-write
    // pattern was exactly the source of bugs #3 / #12 in the
    // multi-OpenVPN scenarios. Now we assert the inverse: a scanner
    // refresh reporting a DIFFERENT iface than the protocol layer
    // recorded must NOT modify the stored interface.
    let mut app = test_app();
    add_profiles(&mut app, &["wg-test"]);
    set_connected(&mut app, "wg-test");

    // Pre-condition: the entry's iface is "wg0" (set by `set_connected`
    // via the mirror_connect path).
    {
        let snap = app
            .registry
            .snapshot(&ProfileId::new("wg-test"))
            .expect("setup precondition");
        let Connection::Connected { details, .. } = snap.state else {
            panic!("expected Connected setup, got {:?}", snap.state);
        };
        assert_eq!(details.interface, "wg0", "setup precondition");
    }

    // Scanner reports the session with a DIFFERENT iface (simulates
    // the macOS multi-OpenVPN ifconfig collision where Method B
    // returns the wrong utun).
    let mut session = fake_session("wg-test");
    session.interface = "utun99-wrong-from-scanner".to_string();
    app.handle_message(Message::SyncSystemState {
        sessions: vec![session],
        default_route_interface: None,
    });

    // Post-refresh: iface MUST still be the protocol-layer's "wg0".
    let snap = app
        .registry
        .snapshot(&ProfileId::new("wg-test"))
        .expect("registry snapshot must exist after refresh");
    let Connection::Connected { details, .. } = snap.state else {
        panic!("expected Connected, got {:?}", snap.state);
    };
    assert_eq!(
        details.interface, "wg0",
        "scanner refresh must NOT overwrite the authoritative iface set by Tunnel::up — preserves the contract that prevents bugs #3 / #12"
    );
    // Metadata fields DID update though — that's the legitimate
    // metadata-only refresh path.
    assert_eq!(details.endpoint, "1.2.3.4:51820");
}

#[test]
fn disconnect_result_success_removes_from_registry() {
    use crate::vortix_core::profile::ProfileId;

    let mut app = test_app();
    add_profiles(&mut app, &["mirror-test"]);

    // Get into Connected first via the same handler the bug fix wires.
    set_connecting(&mut app, "mirror-test");
    app.handle_message(Message::ConnectResult {
        profile: "mirror-test".to_string(),
        success: true,
        error: None,
        interface: None,
        pid: None,
    });
    assert_eq!(app.registry.tunnel_count(), 1, "setup precondition");

    // Now disconnect: registry path goes Connected -> Disconnecting ->
    // (worker thread tears down kernel state) -> DisconnectResult ->
    // complete_disconnect.
    app.mirror_disconnecting_into_registry("mirror-test");
    app.handle_message(Message::DisconnectResult {
        profile: "mirror-test".to_string(),
        success: true,
        error: None,
    });

    assert_eq!(
        app.registry.tunnel_count(),
        0,
        "registry must reflect that the tunnel is gone after disconnect"
    );
    assert!(
        app.registry
            .snapshot(&ProfileId::new("mirror-test"))
            .is_none(),
        "no leftover snapshot for the disconnected profile"
    );
}

/// Connecting → Connected race-arrival regression (post-U4 shape).
///
/// Under U4 the scanner cannot promote Connecting → Connected, so the
/// pre-U4 race (scanner adopts as Connected before `ConnectResult`
/// arrives) is structurally impossible. The race that DOES still
/// matter is the inverse: kernel session visible to the scanner
/// while the protocol-layer connect is in flight — the registry must
/// stay Connecting and the eventual `ConnectResult` must promote
/// cleanly through `mirror_connect_into_registry`.
#[test]
fn connect_result_success_arrives_after_scanner_observes_kernel_session() {
    use crate::vortix_core::profile::ProfileId;

    let mut app = test_app();
    add_profiles(&mut app, &["race-test"]);

    // Connect kicks off; scanner sees the openvpn process and reports
    // a matching ActiveSession while the protocol layer's success
    // hasn't yet arrived.
    set_connecting(&mut app, "race-test");
    app.handle_message(Message::SyncSystemState {
        sessions: vec![fake_session("race-test")],
        default_route_interface: None,
    });
    assert!(
        matches!(
            app.legacy_state(),
            ConnectionState::Connecting { ref profile, .. } if profile == "race-test"
        ),
        "registry must stay Connecting — scanner can't drive promotion under U4"
    );

    // Now the connect-worker's ConnectResult arrives — the authoritative
    // success path. mirror_connect_into_registry runs the transition.
    app.handle_message(Message::ConnectResult {
        profile: "race-test".to_string(),
        success: true,
        error: None,
        interface: None,
        pid: None,
    });

    // Bookkeeping that the success handler is responsible for:
    assert_eq!(
        app.runtime.last_connected_profile.as_deref(),
        Some("race-test"),
        "last_connected_profile must be set by the success handler"
    );
    let snap = app
        .registry
        .snapshot(&ProfileId::new("race-test"))
        .expect("registry must contain the now-Connected tunnel");
    assert!(
        matches!(
            snap.state,
            crate::vortix_core::engine::state::Connection::Connected { .. }
        ),
        "Connected state lands via the authoritative ConnectResult path"
    );
}

/// `ConnectResult` success carries the authoritative iface + pid from the
/// connect-worker thread (`Tunnel::up`'s return value) through to
/// `mirror_connect_into_registry`. After U4 made the scanner metadata-only,
/// this is the ONLY write path for `details.interface` on a vortix-
/// initiated connect. If it stays empty, `recompute_primary` can't match
/// against the kernel-iface cache → primary=None → Role=Addressable for
/// what's actually a Primary tunnel.
#[test]
fn connect_result_success_seeds_authoritative_iface_into_registry() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    let mut app = test_app();
    add_profiles(&mut app, &["ovpn-cert"]);
    set_connecting(&mut app, "ovpn-cert");

    // ConnectResult arrives with the authoritative iface from Tunnel::up.
    app.handle_message(Message::ConnectResult {
        profile: "ovpn-cert".to_string(),
        success: true,
        error: None,
        interface: Some("utun8".to_string()),
        pid: Some(7155),
    });

    let snap = app
        .registry
        .snapshot(&ProfileId::new("ovpn-cert"))
        .expect("registry must have a snapshot for the connected profile");
    let Connection::Connected { details, .. } = snap.state else {
        panic!("expected Connected, got {:?}", snap.state);
    };
    assert_eq!(
        details.interface, "utun8",
        "ConnectResult must seed the registry entry's interface field — empty iface breaks primary-election"
    );
    assert_eq!(details.pid, Some(7155), "PID seeded same path");
    assert!(
        details.interface_authoritative,
        "ConnectResult success path is authoritative by construction (came from Tunnel::up's log scrape)"
    );
}

/// Multi-tunnel takeover regression: pressing Shift+B fires a second
/// connect while the first profile is still primary. The
/// `ConnectResult` for the second profile MUST NOT be dropped as
/// stale by the handler. Pre-U4 the bug was hidden because the
/// scanner would promote Connecting → Connected for the second
/// profile shortly after; post-U4 the scanner is metadata-only, so
/// a dropped `ConnectResult` leaves the second tunnel stuck in
/// Connecting indefinitely. The stale-check must read the specific
/// profile's registry state, not `legacy_state()` (which returns the
/// primary's state).
#[test]
fn connect_result_for_secondary_profile_during_takeover_is_not_stale() {
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    let mut app = test_app();
    add_profiles(&mut app, &["ovpn-cert", "vpn-secondary"]);

    // Connect ovpn-cert first via the authoritative path. It becomes
    // (notionally) the primary in the legacy view.
    set_connected(&mut app, "ovpn-cert");

    // User triggers takeover-Both: registry now has ovpn-cert
    // (Connected) plus vpn-secondary (Connecting). The connect thread
    // for vpn-secondary is in flight.
    set_connecting(&mut app, "vpn-secondary");

    // Connect thread for vpn-secondary reports success.
    app.handle_message(Message::ConnectResult {
        profile: "vpn-secondary".to_string(),
        success: true,
        error: None,
        interface: Some("utun9".to_string()),
        pid: Some(8888),
    });

    let snap = app
        .registry
        .snapshot(&ProfileId::new("vpn-secondary"))
        .expect("vpn-secondary must be in the registry after a successful ConnectResult");
    let Connection::Connected { details, .. } = snap.state else {
        panic!(
            "expected vpn-secondary Connected (NOT stuck in Connecting); got {:?}",
            snap.state
        );
    };
    assert_eq!(
        details.interface, "utun9",
        "the second profile's iface must seed into the registry from its ConnectResult — \
         the stale check must not drop this message just because the legacy view points at ovpn-cert"
    );
}

/// `CachedConfigView::from_content` pre-counts lines and pre-highlights
/// every line so the scroll path doesn't have to re-iterate the file.
/// Aggressive scrolling on a large inline-cert `.ovpn` used to wedge the
/// TUI because both `get_config_max_scroll` and the renderer each did
/// `content.lines().count()` / `.map(highlight).collect()` per keystroke.
#[test]
fn cached_config_view_precomputes_total_lines_and_highlighted_vec() {
    use crate::app::CachedConfigView;

    let content = "[Interface]\nAddress = 10.0.0.2/24\nPrivateKey = abc\n\n[Peer]\nPublicKey = def\nAllowedIPs = 0.0.0.0/0\n";
    let view = CachedConfigView::from_content(content.to_string());

    assert_eq!(view.total_lines, 7, "total_lines must be pre-computed");
    assert_eq!(
        view.highlighted_lines.len(),
        7,
        "highlighted_lines must have one entry per content line"
    );
    assert_eq!(view.content, content, "raw content preserved verbatim");
}

/// `get_config_max_scroll` must read from the cache, NOT iterate the
/// content string. Regression guard for the O(N²)-on-keypress wedge.
#[test]
fn get_config_max_scroll_reads_from_cache() {
    use crate::app::CachedConfigView;

    let mut app = test_app();
    // Synthesize a long enough content that max_scroll would diverge from
    // zero even after subtracting the viewport height.
    let mut content = String::new();
    for i in 0..200 {
        use std::fmt::Write;
        let _ = writeln!(content, "line {i}");
    }
    app.terminal_size = (120, 40);
    app.cached_config = Some(CachedConfigView::from_content(content));

    let max = app.get_config_max_scroll();
    assert!(
        max > 0,
        "200 lines must produce a positive max-scroll on a 40-row terminal"
    );
    // Calling again must be cheap — same value, no observable side effects
    // (caching invariant; can't directly time but assert idempotency).
    assert_eq!(app.get_config_max_scroll(), max);
}

#[test]
fn refresh_registry_preserves_authoritative_iface_across_scanner_ticks() {
    // Regression for the multi-openvpn "primary jumps to the second
    // (split) tunnel after Shift+B" bug. The macOS scanner's
    // ifconfig-scan fallback can't distinguish per-PID iface and
    // resolves both openvpn processes to the same utun token. If
    // `refresh_registry_from_session` clobbers the authoritative iface
    // recorded by `Tunnel::up()`, recompute_primary's HashMap iteration
    // order picks an arbitrary tunnel and the asterisk/header drift to
    // whichever tunnel happens to iterate first.
    use crate::vortix_core::engine::state::Connection;
    use crate::vortix_core::profile::ProfileId;

    let mut app = App::new_test();
    add_profiles(&mut app, &["ovpn-cert"]);

    // Simulate Tunnel::up() landing the authoritative iface "utun8".
    let details = DetailedConnectionInfo {
        interface: "utun8".to_string(),
        pid: Some(7155),
        ..Default::default()
    };
    app.mirror_connect_into_registry("ovpn-cert", &details, Instant::now());

    // Scanner tick reports the same profile but with a wrong iface
    // (e.g. "utun3" — what Method B picks when another openvpn lower
    // in the utun list owns its own inet). The preservation guard
    // must NOT overwrite "utun8" with "utun3".
    let mut session = fake_session("ovpn-cert");
    session.interface = "utun3".to_string();
    app.refresh_registry_from_session("ovpn-cert", &session);

    let snap = app
        .registry
        .snapshot(&ProfileId::new("ovpn-cert"))
        .expect("registry entry");
    let iface = match snap.state {
        Connection::Connected { details, .. } => details.interface.clone(),
        other => panic!("expected Connected, got {other:?}"),
    };
    assert_eq!(
        iface, "utun8",
        "scanner must NOT overwrite authoritative iface set by Tunnel::up()"
    );
}

// ====================================================================
// Real-IP cache gate — startup-race regression suite
// ====================================================================
//
// Bug: vortix opened while a VPN tunnel is already up cached the
// VPN's exit IP as `real_ip`. Cause: telemetry's first PublicIp
// poll fires before the scanner's first SyncSystemState tick, so
// the registry is briefly empty, `!is_connected` is true, and the
// VPN exit IP gets baked into `real_ip`. Fix: require positive
// proof of zero VPN sessions (scanner has ticked AND kernel
// reports zero sessions AND registry has zero Connected) before
// caching. The tests below pin each branch of that gate.

#[test]
fn real_ip_not_cached_when_scanner_has_not_ticked_yet() {
    // Telemetry fires before scanner. The bug: this used to cache
    // the IP unconditionally because `!is_connected` was true.
    // Fix: scanner_first_tick_done starts false → cache withheld.
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();
    assert!(!app.runtime.scanner_first_tick_done);
    assert!(app.runtime.real_ip.is_none());

    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "46.101.235.146".to_string(),
    )));

    assert!(
        app.runtime.real_ip.is_none(),
        "real_ip must stay None until scanner reports kernel state"
    );
}

#[test]
fn real_ip_not_cached_when_kernel_has_active_sessions() {
    // Scanner reports an active kernel session (a tunnel started
    // outside vortix that hasn't been adopted yet, or one in flight).
    // Telemetry then fires — the IP IS the VPN's exit IP, so we
    // must withhold caching.
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a"]);

    // Scanner sees a kernel session but registry hasn't adopted yet.
    app.handle_message(Message::SyncSystemState {
        sessions: vec![fake_session("vpn-a")],
        default_route_interface: Some("wg0".to_string()),
    });
    assert!(app.runtime.scanner_first_tick_done);
    assert_eq!(app.runtime.last_kernel_session_count, 1);

    // The fake session got adopted in handle_sync_system_state, so
    // is_connected is now true too. Verify that even if we manually
    // reset is_connected (by removing the registry entry) but leave
    // last_kernel_session_count > 0, the gate STILL holds.
    let pid = crate::vortix_core::profile::ProfileId::new("vpn-a");
    app.registry.set_disconnected(&pid);
    assert!(!app.has_active_connection());
    assert_eq!(app.runtime.last_kernel_session_count, 1);

    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "46.101.235.146".to_string(),
    )));

    assert!(
        app.runtime.real_ip.is_none(),
        "real_ip must stay None while kernel reports any VPN session"
    );
}

#[test]
fn real_ip_cached_after_clean_scanner_tick_with_zero_sessions() {
    // Happy path: scanner has ticked and reports zero sessions,
    // registry is empty, telemetry fires — cache the IP as real_ip.
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();

    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });
    assert!(app.runtime.scanner_first_tick_done);
    assert_eq!(app.runtime.last_kernel_session_count, 0);

    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "203.0.113.5".to_string(),
    )));

    assert_eq!(
        app.runtime.real_ip.as_deref(),
        Some("203.0.113.5"),
        "real_ip must cache once scanner confirms zero sessions"
    );
}

#[test]
fn real_ip_overwrites_on_disconnected_telemetry_samples() {
    // After a clean disconnect, subsequent telemetry samples
    // should overwrite real_ip (in case the user moved networks).
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();

    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });

    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "203.0.113.5".to_string(),
    )));
    assert_eq!(app.runtime.real_ip.as_deref(), Some("203.0.113.5"));

    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "198.51.100.10".to_string(),
    )));
    assert_eq!(
        app.runtime.real_ip.as_deref(),
        Some("198.51.100.10"),
        "real_ip must update when user moves networks"
    );
}

#[test]
fn real_ip_telemetry_persists_to_disk_cache() {
    // After a clean scanner tick + telemetry sample, the cache
    // file on disk must contain the captured real IP. Future
    // launches load this so the Real IP row populates even when
    // vortix is opened with a VPN already up.
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();

    // Point the runtime at a fresh scratch dir so we can inspect
    // the cache file without colliding with the user's real config.
    let scratch =
        std::env::temp_dir().join(format!("vortix-real-ip-cache-app-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&scratch);
    std::fs::create_dir_all(&scratch).expect("scratch dir");
    app.runtime.config_dir = scratch.clone();

    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });
    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "203.0.113.42".to_string(),
    )));

    // In-memory caches.
    assert_eq!(app.runtime.real_ip.as_deref(), Some("203.0.113.42"));

    // Disk cache populated.
    let loaded = crate::core::real_ip_cache::load(&scratch)
        .expect("on-disk cache must exist after a safe-to-cache telemetry sample");
    assert_eq!(loaded.ip, "203.0.113.42");

    // Cleanup.
    let _ = std::fs::remove_dir_all(&scratch);
}

#[test]
fn real_dns_not_cached_when_scanner_has_not_ticked_yet() {
    // Parallel to real_ip_not_cached_when_scanner_has_not_ticked_yet.
    // DNS telemetry firing before the scanner's first SyncSystemState
    // tick must NOT cache the VPN's pushed DNS as `real_dns` — that
    // would later read as a DNS leak in `verdict_for_protected`
    // and lock the header into PARTIAL forever.
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();
    assert!(!app.runtime.scanner_first_tick_done);
    assert!(app.runtime.real_dns.is_none());

    app.handle_message(Message::Telemetry(TelemetryUpdate::Dns(
        "10.8.0.1".to_string(),
    )));

    assert!(
        app.runtime.real_dns.is_none(),
        "real_dns must stay None until scanner reports kernel state"
    );
    assert_eq!(
        app.runtime.dns_server, "10.8.0.1",
        "dns_server still updates — only the real_dns cache is gated"
    );
}

#[test]
fn real_dns_not_cached_when_kernel_has_active_sessions() {
    // If the scanner sees a kernel session, the DNS sample is the
    // VPN's pushed resolver — don't cache it as real_dns.
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();
    add_profiles(&mut app, &["vpn-a"]);

    app.handle_message(Message::SyncSystemState {
        sessions: vec![fake_session("vpn-a")],
        default_route_interface: Some("wg0".to_string()),
    });
    assert!(app.runtime.scanner_first_tick_done);
    assert_eq!(app.runtime.last_kernel_session_count, 1);

    let pid = crate::vortix_core::profile::ProfileId::new("vpn-a");
    app.registry.set_disconnected(&pid);
    assert!(!app.has_active_connection());

    app.handle_message(Message::Telemetry(TelemetryUpdate::Dns(
        "10.8.0.1".to_string(),
    )));

    assert!(
        app.runtime.real_dns.is_none(),
        "real_dns must stay None while kernel reports any VPN session"
    );
}

#[test]
fn real_dns_cached_after_clean_scanner_tick_with_zero_sessions() {
    // Happy path: scanner says zero kernel sessions, registry has
    // no Connected tunnel, DNS sample is genuinely your ISP's DNS.
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();

    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });

    app.handle_message(Message::Telemetry(TelemetryUpdate::Dns(
        "1.1.1.1".to_string(),
    )));

    assert_eq!(
        app.runtime.real_dns.as_deref(),
        Some("1.1.1.1"),
        "real_dns must cache once scanner confirms zero sessions"
    );
}

#[test]
fn real_ip_frozen_once_connected_then_thaws_on_disconnect() {
    // While connected, telemetry samples are the VPN's exit IP and
    // must NOT overwrite real_ip. After disconnect (kernel session
    // count drops to 0), the next sample can re-cache.
    use crate::core::telemetry::TelemetryUpdate;
    let mut app = test_app();

    // Clean tick → cache real IP.
    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });
    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "203.0.113.5".to_string(),
    )));
    assert_eq!(app.runtime.real_ip.as_deref(), Some("203.0.113.5"));

    // VPN comes up — kernel reports a session.
    add_profiles(&mut app, &["vpn-a"]);
    app.handle_message(Message::SyncSystemState {
        sessions: vec![fake_session("vpn-a")],
        default_route_interface: Some("wg0".to_string()),
    });

    // Telemetry while connected (VPN exit IP) — must NOT overwrite.
    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "46.101.235.146".to_string(),
    )));
    assert_eq!(
        app.runtime.real_ip.as_deref(),
        Some("203.0.113.5"),
        "real_ip must stay frozen while connected"
    );

    // VPN goes away — kernel reports zero sessions, registry too.
    let pid = crate::vortix_core::profile::ProfileId::new("vpn-a");
    app.registry.set_disconnected(&pid);
    app.handle_message(Message::SyncSystemState {
        sessions: vec![],
        default_route_interface: None,
    });

    // Now telemetry can re-cache (user may have moved networks).
    app.handle_message(Message::Telemetry(TelemetryUpdate::PublicIp(
        "198.51.100.99".to_string(),
    )));
    assert_eq!(
        app.runtime.real_ip.as_deref(),
        Some("198.51.100.99"),
        "real_ip must thaw and update after clean disconnect"
    );
}