paperboy 0.5.5

A Rust TUI API tester
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
//! Framework-agnostic request logic shared by the GUI and the terminal UI:
//! app-level default variables (`AppVars`) and the Hurl-collection request
//! building / running, so both front-ends behave identically.

use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap};
use std::rc::Rc;
use std::sync::mpsc::{self, Receiver, TryRecvError};
use std::sync::{Arc, Mutex};
use std::thread;

use base64::{Engine, engine::general_purpose::STANDARD};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::collection::Collection;
use crate::environment::{EnvUpdate, Environment, ValueSource, substitute};
use crate::generators::GenError;
use crate::http::ApiResponse;
use crate::hurl::{
    EntryOutcome, FormField, HurlEntry, KvRow, RunOutput, RunStatus, collection_to_hurl,
    expand_base64_form_fields, run_hurl, stage_out_of_scope_form_files,
};

/// The top-bar Base URL. It seeds the URL field when composing a new request,
/// but is intentionally NOT injected as a `{{ BASE_URL }}` substitution
/// variable — `BASE_URL` must come from the environment (or a capture) so that
/// `{{ BASE_URL }}` stays unresolved when the environment doesn't define it.
#[derive(Clone)]
pub struct AppVars {
    pub base_url: String,
}

impl Default for AppVars {
    fn default() -> Self {
        Self {
            base_url: "http://127.0.0.1:8080".to_string(),
        }
    }
}

/// Build the variable map used to substitute `{{ VAR }}` placeholders: the
/// collection's environment file, then values captured from prior responses
/// (each layer overrides the previous, so a fresh capture wins). The top-bar
/// Base URL is deliberately excluded — `{{ BASE_URL }}` resolves only when the
/// environment (or a capture) supplies `BASE_URL`.
pub fn collection_vars(
    env: Option<&Environment>,
    captures: &HashMap<String, String>,
) -> HashMap<String, String> {
    let mut vars = HashMap::new();
    if let Some(env) = env {
        for v in &env.vars {
            vars.insert(v.key.clone(), v.value.clone());
        }
    }
    for (k, v) in captures {
        vars.insert(k.clone(), v.clone());
    }
    vars
}

/// How a `{{ VAR }}` substitution should be coloured, reflecting whether its
/// value is available yet. Drives both the request preview / list colouring and
/// the environment-panel status dot so they agree.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SubstKind {
    /// A plain literal environment value (shown substituted, cyan).
    Literal,
    /// Resolved from an external source — env var, 1Password, SSM — or an
    /// initialised capture (shown substituted, green).
    Loaded,
    /// A secret reference still being fetched in the background (kept as
    /// `{{ VAR }}`, orange).
    Pending,
    /// Failed to resolve, or a capture not yet initialised from a response
    /// (kept as `{{ VAR }}`, red).
    Failed,
    /// Produced by the request's `# [Gen]` block at send time.
    ///
    /// Kept as `{{ VAR }}` rather than substituted, because the value doesn't
    /// exist yet and inventing one for the preview would mean a new random
    /// number or timestamp on every frame — a flickering preview that also
    /// wouldn't be what gets sent. Coloured as loaded, since it *will* have a
    /// value; a row that can't evaluate is reported separately (see
    /// [`generator_problems`]).
    Computed,
    /// Referenced by the request but defined nowhere at all — no environment
    /// variable, no `[Captures]` name, no captured value (kept as `{{ VAR }}`,
    /// red).
    ///
    /// Kept apart from [`SubstKind::Failed`] because the two ask for different
    /// things: a failed variable exists and can be retried, while an undefined
    /// one is usually a typo or a missing environment and has to be *added*.
    /// Before this existed an undefined placeholder matched nothing in the
    /// substitution map and was drawn as ordinary body text, so the one kind of
    /// broken variable the user could do nothing about was also the only one
    /// that looked completely fine.
    Undefined,
}

/// How one referenced variable should be rendered when substituted.
pub struct SubstInfo {
    /// `Some(value)` to substitute the value; `None` to keep the `{{ VAR }}`
    /// placeholder (its value isn't available yet).
    pub shown: Option<String>,
    pub kind: SubstKind,
}

/// Classify every variable a collection's requests might reference, so
/// `{{ VAR }}` placeholders can be substituted and colour-coded by whether they
/// are loaded. Resolution order (later wins): collection `[Captures]` names
/// (not-yet-captured → red) → environment variables → captured values (green).
/// Resolved secrets are shown masked; their real value is never exposed.
pub fn subst_map(col: &Collection, env: Option<&Environment>) -> HashMap<String, SubstInfo> {
    let mut out: HashMap<String, SubstInfo> = HashMap::new();

    // Every `[Captures]` name defined in the collection that hasn't produced a
    // value yet is "not initialised" → red placeholder.
    for e in &col.entries {
        for (name, _) in &e.captures {
            out.insert(
                name.clone(),
                SubstInfo {
                    shown: None,
                    kind: SubstKind::Failed,
                },
            );
        }
    }

    if let Some(env) = env {
        for v in &env.vars {
            let info = if v.loading {
                SubstInfo {
                    shown: None,
                    kind: SubstKind::Pending,
                }
            } else if v.resolved {
                match v.source {
                    ValueSource::Literal => SubstInfo {
                        shown: Some(v.value.clone()),
                        kind: SubstKind::Literal,
                    },
                    _ => SubstInfo {
                        shown: Some(v.display_value()),
                        kind: SubstKind::Loaded,
                    },
                }
            } else {
                SubstInfo {
                    shown: None,
                    kind: SubstKind::Failed,
                }
            };
            out.insert(v.key.clone(), info);
        }
    }

    // A capture that has produced a value is loaded → green (overrides env).
    for (k, val) in &col.captures {
        out.insert(
            k.clone(),
            SubstInfo {
                shown: Some(val.clone()),
                kind: SubstKind::Loaded,
            },
        );
    }

    // Names an entry's own `# [Gen]` block computes, applied *last* so they win
    // over `col.captures`. This branch merges a completed send's generated
    // values into `CaptureUpdate::values`, so from the first send onwards a
    // generator name is also present in `col.captures` with its value — and a
    // computed value must never be shown: it may be an HMAC of a secret (unlike
    // an environment secret, a capture is rendered in the clear), and the
    // preview would in any case be a lie, showing the *previous* send's nonce
    // while the next send computes a fresh one. Rendered as `{{name}}` in the
    // computed colour instead, whatever `col.captures` holds.
    for e in &col.entries {
        for (name, _) in &e.generators {
            out.insert(
                name.clone(),
                SubstInfo {
                    shown: None,
                    kind: SubstKind::Computed,
                },
            );
        }
    }

    out
}

/// The request text with every *known* `{{ VAR }}` replaced by its substituted
/// value (placeholders whose value isn't available are kept). Used to measure
/// the displayed length for horizontal-scroll clamping in the list.
pub fn subst_display(text: &str, map: &HashMap<String, SubstInfo>) -> String {
    let mut out = String::new();
    let mut rest = text;
    while let Some(open) = rest.find("{{") {
        let Some(close_rel) = rest[open + 2..].find("}}") else {
            break;
        };
        let close = open + 2 + close_rel;
        let end = close + 2;
        let inner = rest[open + 2..close].trim();
        out.push_str(&rest[..open]);
        match map.get(inner).and_then(|i| i.shown.as_ref()) {
            Some(val) => out.push_str(val),
            None => out.push_str(&rest[open..end]),
        }
        rest = &rest[end..];
    }
    out.push_str(rest);
    out
}

/// A header/cookie/query-param value together with its enabled flag, as it
/// appears in the Raw JSON editor. An **enabled** entry serializes as a bare
/// scalar (`"X-Foo": "bar"`) so the common case stays clean and hand-editable;
/// a **disabled** entry serializes as a `[value, false]` pair so the flag
/// survives a round trip through the editor. On parse it tolerates either
/// shape: any bare scalar (string, number, bool, null) is treated as enabled,
/// while a `[value, enabled?, desc?]` array carries an explicit flag
/// (defaulting to enabled when omitted) and an optional description.
#[derive(Clone)]
struct KvValue {
    value: String,
    enabled: bool,
    /// The row's description. Carried through so the Code view round-trips a
    /// note rather than silently dropping it on the way back.
    desc: String,
}

impl serde::Serialize for KvValue {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        // The plain-string form is kept for the overwhelmingly common
        // "enabled, no note" row, so the Code view stays readable; the array
        // grows a third element only when there is actually a note to carry.
        if self.enabled && self.desc.is_empty() {
            s.serialize_str(&self.value)
        } else if self.desc.is_empty() {
            (&self.value, self.enabled).serialize(s)
        } else {
            (&self.value, self.enabled, &self.desc).serialize(s)
        }
    }
}

impl<'de> serde::Deserialize<'de> for KvValue {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        Ok(match Value::deserialize(d)? {
            Value::Array(arr) => KvValue {
                value: arr.first().map(value_as_text).unwrap_or_default(),
                enabled: arr.get(1).and_then(Value::as_bool).unwrap_or(true),
                desc: arr.get(2).map(value_as_text).unwrap_or_default(),
            },
            other => KvValue {
                value: value_as_text(&other),
                enabled: true,
                desc: String::new(),
            },
        })
    }
}

/// A header/cookie/query-param/basic-auth value. Serializes as a JSON string;
/// on parse it tolerantly coerces any hand-edited scalar (number, bool, null)
/// to text.
#[derive(Clone, Default)]
struct TextValue(String);

impl serde::Serialize for TextValue {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&self.0)
    }
}

impl<'de> serde::Deserialize<'de> for TextValue {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        Ok(TextValue(value_as_text(&Value::deserialize(d)?)))
    }
}

/// A JSON scalar as plain text: strings as-is, `null` as empty, anything else
/// via its JSON text.
fn value_as_text(v: &Value) -> String {
    match v {
        Value::String(s) => s.clone(),
        Value::Null => String::new(),
        other => other.to_string(),
    }
}

/// Form field `type`, lower-cased. Unknown or missing values parse as `Text`.
#[derive(Serialize, Default, Clone, Copy)]
#[serde(rename_all = "lowercase")]
enum FormKind {
    #[default]
    Text,
    File,
    #[serde(rename = "base64file")]
    Base64File,
}

impl<'de> serde::Deserialize<'de> for FormKind {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let v = Value::deserialize(d)?;
        Ok(match v.as_str() {
            Some("file") => FormKind::File,
            Some("base64file") => FormKind::Base64File,
            _ => FormKind::Text,
        })
    }
}

/// One `form_fields` entry (fields alphabetical, matching [`RequestJson`]).
#[derive(Serialize, Deserialize)]
struct FormFieldJson {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    base64_prefix: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    content_type: Option<String>,
    #[serde(default)]
    key: TextValue,
    #[serde(rename = "type", default)]
    kind: FormKind,
    #[serde(default)]
    value: TextValue,
    #[serde(default = "default_true", skip_serializing_if = "is_true")]
    enabled: bool,
    /// The field's note. Omitted from the JSON entirely when empty, which is
    /// almost always, so the Code view isn't cluttered by it.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    desc: String,
}

fn default_true() -> bool {
    true
}

fn is_true(b: &bool) -> bool {
    *b
}

impl From<&FormField> for FormFieldJson {
    fn from(f: &FormField) -> Self {
        Self {
            base64_prefix: f.base64_prefix.clone(),
            content_type: f.content_type.clone(),
            key: TextValue(f.key.clone()),
            kind: match f.kind {
                crate::hurl::FormFieldKind::File => FormKind::File,
                crate::hurl::FormFieldKind::Base64File => FormKind::Base64File,
                crate::hurl::FormFieldKind::Text => FormKind::Text,
            },
            value: TextValue(f.value.clone()),
            enabled: f.enabled,
            desc: f.desc.clone(),
        }
    }
}

impl From<FormFieldJson> for FormField {
    fn from(f: FormFieldJson) -> Self {
        Self {
            key: f.key.0,
            value: f.value.0,
            kind: match f.kind {
                FormKind::File => crate::hurl::FormFieldKind::File,
                FormKind::Base64File => crate::hurl::FormFieldKind::Base64File,
                FormKind::Text => crate::hurl::FormFieldKind::Text,
            },
            content_type: f.content_type,
            base64_prefix: f.base64_prefix,
            enabled: f.enabled,
            desc: f.desc,
        }
    }
}

/// `basic_auth` object; both fields default so a hand-edit dropping one still
/// parses.
#[derive(Serialize, Deserialize, Default)]
struct BasicAuthJson {
    #[serde(default)]
    pass: TextValue,
    #[serde(default)]
    user: TextValue,
}

/// Serde mirror of the Raw JSON editor's request shape. Fields are alphabetical
/// so the pretty output stays byte-identical to serde_json's default
/// `BTreeMap` ordering; header/cookie/param maps dedupe+sort keys; `body` is a
/// raw JSON value so JSON bodies inline while anything else stays a string.
#[derive(Serialize, Deserialize)]
struct RequestJson {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    basic_auth: Option<BasicAuthJson>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    body: Option<Value>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    cookies: BTreeMap<String, KvValue>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    form_fields: Vec<FormFieldJson>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    headers: BTreeMap<String, KvValue>,
    method: String,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    query_params: BTreeMap<String, KvValue>,
    url: String,
}

fn rows_to_map(rows: &[KvRow]) -> BTreeMap<String, KvValue> {
    rows.iter()
        .map(|r| {
            (
                r.key.clone(),
                KvValue {
                    value: r.value.clone(),
                    enabled: r.enabled,
                    desc: r.desc.clone(),
                },
            )
        })
        .collect()
}

fn map_to_rows(map: BTreeMap<String, KvValue>) -> Vec<KvRow> {
    map.into_iter()
        .map(|(key, kv)| KvRow {
            key,
            value: kv.value,
            enabled: kv.enabled,
            desc: kv.desc,
        })
        .collect()
}

/// Pretty-printed JSON of the request in its RAW, editable form: `{{ VAR }}`
/// placeholders are kept intact and basic auth is shown as a readable
/// `basic_auth` object (not an encoded header). The wire request is re-derived
/// (substituted + encoded) from the entry by [`resolve_entry`].
pub fn build_request_json(entry: &HurlEntry) -> String {
    let dto = RequestJson {
        basic_auth: entry.basic_auth.as_ref().map(|(user, pass)| BasicAuthJson {
            pass: TextValue(pass.clone()),
            user: TextValue(user.clone()),
        }),
        body: entry.body_src.as_deref().map(|raw| {
            serde_json::from_str(raw).unwrap_or_else(|_| Value::String(raw.to_string()))
        }),
        cookies: rows_to_map(&entry.cookies),
        form_fields: entry.form_fields.iter().map(FormFieldJson::from).collect(),
        headers: rows_to_map(&entry.headers),
        method: entry.method.clone(),
        query_params: rows_to_map(&entry.queries),
        url: entry.url.clone(),
    };
    serde_json::to_string_pretty(&dto).unwrap_or_else(|_| "{}".into())
}

/// Parse the JSON from [`build_request_json`] back into a [`HurlEntry`],
/// carrying over the fields this view doesn't expose (`title`,
/// `expected_status`, `captures`, `asserts`, `user_added`) unchanged from
/// `base`. Errs on anything that isn't an object with a `method` and `url`.
pub fn apply_request_json(base: &HurlEntry, text: &str) -> Result<HurlEntry, String> {
    let dto: RequestJson = serde_json::from_str(text).map_err(|e| e.to_string())?;

    let body = match dto.body {
        None | Some(Value::Null) => None,
        Some(Value::String(s)) => Some(s),
        Some(v) => Some(serde_json::to_string_pretty(&v).unwrap_or_default()),
    };

    let mut entry = base.clone();
    entry.method = dto.method;
    entry.url = dto.url;
    entry.basic_auth = dto.basic_auth.map(|ba| (ba.user.0, ba.pass.0));
    entry.headers = map_to_rows(dto.headers);
    entry.cookies = map_to_rows(dto.cookies);
    entry.queries = map_to_rows(dto.query_params);
    entry.form_fields = dto.form_fields.into_iter().map(FormField::from).collect();
    entry.body_src = body;
    Ok(entry)
}

/// Which textual representation of a request the Main (Request) panel shows
/// by default, and copies whole when the panel has focus with nothing
/// selected: the pretty-printed JSON preview, or the actual Hurl text. An
/// app-wide preference (Settings → Preferences → Default Request View) that
/// applies to every request, not just the one currently selected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum RequestView {
    Json,
    #[default]
    Hurl,
}

/// The wire request resolved for the selected entry: `{{ VAR }}` placeholders
/// substituted and basic auth encoded into an `Authorization` header.
pub struct ResolvedRequest {
    pub method: String,
    pub url: String,
    pub headers: Vec<(String, String)>,
    pub cookies: Vec<(String, String)>,
    pub form_fields: Vec<FormField>,
    pub body: Option<String>,
}

/// Resolve one arbitrary `HurlEntry`'s `{{ VAR }}` placeholders against `vars`,
/// folding any `basic_auth` into an `Authorization` header. Callers pass the
/// entry they want (e.g. a collection's *selected* entry with the collection's
/// own vars); the report interpreter reuses it to resolve a request chosen by
/// name with its own scoped vars.
pub fn resolve_entry(entry: &HurlEntry, vars: &HashMap<String, String>) -> ResolvedRequest {
    let method = entry.method.clone();
    let url = substitute(&entry.url, vars);
    let mut headers: Vec<(String, String)> = entry
        .headers
        .iter()
        .filter(|r| r.enabled)
        .map(|r| (r.key.clone(), substitute(&r.value, vars)))
        .collect();
    if let Some((user, pass)) = &entry.basic_auth {
        let cred = STANDARD.encode(format!(
            "{}:{}",
            substitute(user, vars),
            substitute(pass, vars)
        ));
        headers.push(("Authorization".to_string(), format!("Basic {cred}")));
    }
    let cookies: Vec<(String, String)> = entry
        .cookies
        .iter()
        .filter(|r| r.enabled)
        .map(|r| (substitute(&r.key, vars), substitute(&r.value, vars)))
        .collect();
    let form_fields: Vec<FormField> = entry
        .form_fields
        .iter()
        .filter(|f| f.enabled)
        .map(|f| FormField {
            key: substitute(&f.key, vars),
            value: substitute(&f.value, vars),
            kind: f.kind,
            content_type: f.content_type.as_deref().map(|ct| substitute(ct, vars)),
            base64_prefix: f.base64_prefix.as_deref().map(|p| substitute(p, vars)),
            enabled: f.enabled,
            desc: String::new(),
        })
        .collect();

    // Resolving builds the request that actually goes out, so the comments
    // come off here and never reach the wire.
    let body = entry.body_wire().as_deref().map(|b| substitute(b, vars));
    ResolvedRequest {
        method,
        url,
        headers,
        cookies,
        form_fields,
        body,
    }
}

/// The result of running the collection's selected entry, routed back to its
/// collection: captured values (if any) plus a snapshot of the response
/// actually received, so it can be remembered per-entry (see
/// `HurlEntry::last_response`) rather than only in the shared "live" state.
pub struct CaptureUpdate {
    pub col_id: u64,
    pub entry_idx: usize,
    /// Whether the runner considered this entry a pass (status expectation,
    /// asserts and transport all satisfied) — mirrors `EntryOutcome::ok`, so
    /// the front-end can stamp the entry's pass/fail marker without re-deriving
    /// it from the response.
    pub ok: bool,
    pub values: HashMap<String, String>,
    pub response: ApiResponse,
}

/// The result of a "Run All" (Alt+F5) pass over an entire collection, routed
/// back to its collection on the main thread.
pub struct BatchRunUpdate {
    pub col_id: u64,
    /// Per-entry pass/fail, in the same order as `Collection::entries`.
    /// `None` for an entry the runner never reached (e.g. the whole file
    /// failed to parse before any entry ran).
    pub results: Vec<Option<bool>>,
    /// Every value captured across the whole run, merged in entry order (a
    /// later entry's capture of the same name wins) — the exact multi-entry
    /// equivalent of the single-entry `CaptureUpdate::values`.
    pub captures: HashMap<String, String>,
    /// Per-entry response snapshot, in the same order as `Collection::entries`
    /// and `results`. `None` for an entry the runner never reached — its
    /// previous `HurlEntry::last_response` (if any) is left untouched rather
    /// than cleared, since that's still the last response actually received
    /// for it.
    pub responses: Vec<Option<ApiResponse>>,
}

/// Build the Hurl entry to run for the selected entry, honoring an edited
/// request-JSON buffer for the request line/headers/body while keeping the
/// entry's `[Captures]`/`[Asserts]` (which the JSON model doesn't carry).
/// Returned unserialized (rather than as Hurl text directly) so the caller
/// can stage any out-of-scope `[Form]`/`[Multipart]` file fields first (see
/// [`stage_out_of_scope_form_files`]).
/// Build the concrete `HurlEntry` to run from a `base` entry and its already
/// resolved request line/headers/body/form fields, keeping the base's
/// `[Query]`/`[Captures]`/`[Asserts]`/`[Reports]`/expected-status metadata.
/// Shared by [`run_content`] and the report interpreter so both assemble the
/// run entry identically.
fn to_run_entry(base: &HurlEntry, resolved: ResolvedRequest) -> HurlEntry {
    let is_multipart = resolved
        .form_fields
        .iter()
        .any(|form| form.kind.is_multipart());
    HurlEntry {
        // Stamped when the collection adopts these entries as its baseline.
        uid: 0,
        unparsed: None,
        title: String::new(),
        method: resolved.method,
        url: resolved.url,
        headers: resolved
            .headers
            .iter()
            .map(|(k, v)| KvRow::new(k.clone(), v.clone()))
            .collect(),
        basic_auth: None, // already encoded into `headers` by resolve_entry
        form_fields: resolved.form_fields,
        is_multipart,
        queries: base.queries.clone(),
        cookies: resolved
            .cookies
            .iter()
            .map(|(k, v)| KvRow::new(k.clone(), v.clone()))
            .collect(),
        // Per-request `[Options]` (retry, insecure, delay, …) genuinely affect
        // the run, so carry them through to the executed entry.
        options: base.options.clone(),
        body_src: resolved.body,
        expected_status: base.expected_status,
        // Expected response version/headers/body are real (implicit) asserts in
        // the source `.hurl`, so preserve them on the run entry too — dropping
        // them would silently skip assertions the request author wrote.
        response_version: base.response_version.clone(),
        response_headers: base.response_headers.clone(),
        response_body: base.response_body.clone(),
        captures: base.captures.clone(),
        asserts: base.asserts.clone(),
        reports: base.reports.clone(),
        // Generators have already been evaluated into the variable set by
        // `effective_vars_reporting`, and their placeholders are ordinary `{{name}}`
        // references that Hurl resolves from it. Carrying the definitions onto
        // the run entry would re-emit the block as a comment in text nobody
        // reads back, and risk them being evaluated twice.
        generators: Vec::new(),
        // A transient copy that's only executed, never serialized — comments
        // don't affect the run.
        comments: Vec::new(),
        user_added: base.user_added,
        modified: base.modified,
        baseline: None,
        last_run: base.last_run,
        last_response: None,
    }
}

/// The variables a request actually runs with: those it is given, plus its own
/// declared parameter defaults, plus its `# [Gen]` rows evaluated over both.
/// Also returns whatever went wrong in the block, so the caller can say so
/// (see [`generator_problems`]) rather than sending a request whose signature
/// is still `{{sig}}`.
///
/// A computed value needs no separate secret handling even when it is derived
/// from one: it is only ever put into this map, which goes to `run_hurl` as
/// variables and is dropped afterwards. It reaches no preview (a generator name
/// renders as [`SubstKind::Computed`], keeping its braces rather than showing a
/// value) and no `state.json`. That is the same transient path a resolved
/// `op://` secret already takes, so an HMAC of a secret is no more exposed than
/// the secret was.
pub fn effective_vars_reporting<'a>(
    base: &HurlEntry,
    vars: &'a HashMap<String, String>,
) -> (Cow<'a, HashMap<String, String>>, Vec<GenError>) {
    effective_vars_with(base, vars, &crate::generators::SystemSource::new())
}

/// [`effective_vars_reporting`] against a chosen world, so a caller that is
/// only asking whether the block *would* work can use
/// [`crate::generators::DryRunSource`] and leave the counters where it found
/// them.
pub fn effective_vars_with<'a>(
    base: &HurlEntry,
    vars: &'a HashMap<String, String>,
    src: &dyn crate::generators::GenSource,
) -> (Cow<'a, HashMap<String, String>>, Vec<GenError>) {
    let defaults = base.variable_defaults();
    if defaults.is_empty() && base.generators.is_empty() {
        return (Cow::Borrowed(vars), Vec::new());
    }
    let mut merged = vars.clone();
    for (name, value) in defaults {
        if merged.contains_key(&name) {
            continue;
        }
        let value = substitute(&value, &merged);
        merged.insert(name, value);
    }
    // Evaluated last so a generator may read a declared parameter, and bound
    // over anything of the same name: a row that computes `nonce` is a
    // statement that *this* is where `nonce` comes from.
    let errors = crate::generators::expand(&base.generators, &mut merged, src);
    (Cow::Owned(merged), errors)
}

/// Remove the `variable:` rows from a run entry's `[Options]`, having already
/// folded them into the variable set via [`effective_vars_reporting`].
///
/// Left in, they would undo the default semantics for everything
/// [`resolve_entry`] does not substitute in Rust — `[Captures]` and `[Asserts]`
/// templates are resolved by Hurl itself, and Hurl treats the option as an
/// assignment that beats the passed-in variables. A report binding `FILE` would
/// then see its value in the URL and the request's sample value in an assert,
/// which is the sort of half-applied override that takes a day to find.
///
/// Dropping them also closes the documented oddity that a `variable:` option
/// leaks into *subsequent* entries of the same file, unlike every other option.
fn strip_variable_options(entry: &mut HurlEntry) {
    entry
        .options
        .retain(|r| !(r.enabled && r.key.trim().eq_ignore_ascii_case("variable")));
}

/// The whole-file counterpart of [`strip_variable_options`]: drop only the
/// `variable:` rows whose name the caller has **already bound**, and report
/// whether anything was removed.
///
/// A whole-collection run (the TUI's "Run All" in batch mode, `paperboy -c …`)
/// hands the serialized file to Hurl without resolving it in Rust first, so
/// Hurl applies the remaining defaults itself — which is exactly what is wanted
/// for a name nobody bound. Removing just the bound ones therefore produces the
/// same "default unless overridden" reading as the single-request path, with no
/// second implementation of the precedence rule.
///
/// One residual difference is Hurl's, not ours: a surviving default still leaks
/// into the entries *after* it in the same file (the documented exception to
/// per-entry options). That is what a hand-written `.hurl` does today, so it is
/// left alone rather than silently changed.
pub fn strip_bound_variable_options(
    entries: &mut [HurlEntry],
    vars: &HashMap<String, String>,
) -> bool {
    let mut stripped = false;
    for entry in entries {
        let bound: Vec<String> = entry
            .variable_defaults()
            .into_iter()
            .filter(|(name, _)| vars.contains_key(name))
            .map(|(name, _)| name)
            .collect();
        if bound.is_empty() {
            continue;
        }
        entry.options.retain(|r| {
            let is_bound_default = r.enabled
                && r.key.trim().eq_ignore_ascii_case("variable")
                && r.value
                    .split_once('=')
                    .is_some_and(|(name, _)| bound.iter().any(|b| b == name.trim()));
            !is_bound_default
        });
        stripped = true;
    }
    stripped
}

/// Run one already-chosen `base` entry with `vars` through the full per-request
/// pipeline used for a normal single-request send — base64-form expansion →
/// out-of-scope form-file staging → content-length defaulting → `to_hurl` →
/// [`run_hurl`] — and return the raw [`RunOutput`]. Front-end agnostic (no
/// `ApiResponse`/threads), so both [`run_collection`] and the report interpreter
/// execute a request through exactly the same code path.
///
/// `extra_captures` are appended to the entry's `[Captures]` before running
/// (used by the report interpreter to evaluate `[Reports]`/`WITH` fields as
/// transient captures); pass an empty slice for a plain send. A base64/staging
/// failure is surfaced as `RunOutput { entries: [], error: Some(..) }`.
pub fn run_resolved_entry(
    base: &HurlEntry,
    vars: &HashMap<String, String>,
    file_root: Option<&std::path::Path>,
    extra_captures: &[(String, String)],
) -> RunOutput {
    run_resolved_entry_reporting(base, vars, file_root, extra_captures).0
}

/// [`run_resolved_entry`], also handing back what the request's `# [Gen]` block
/// computed for *this* send, and the rows that failed to compute.
///
/// A generator is the pre-request script's job: a request that signs itself
/// computes a `nonce` the request after it is expected to echo. Re-evaluating
/// the block to find that value would produce a different `uuid` and a later
/// `timestamp` than the one actually sent, so the values have to travel out of
/// the send that made them. The caller merges them where captures go
/// ([`CaptureUpdate::values`]), which is memory only — a computed value still
/// reaches no `state.json`, since it may be an HMAC of a secret and is in any
/// case usually good for one request.
pub fn run_resolved_entry_reporting(
    base: &HurlEntry,
    vars: &HashMap<String, String>,
    file_root: Option<&std::path::Path>,
    extra_captures: &[(String, String)],
) -> (RunOutput, HashMap<String, String>, Vec<GenError>) {
    // Declared parameters are resolved *here*, not left to Hurl's own late
    // binding, because everything downstream works on resolved text: an
    // unresolved `{{FILE}}` in a `[Multipart]` file path would reach
    // `stage_out_of_scope_form_files` as a literal filename, fail to stage, and
    // then be rejected by Hurl's file sandbox when it finally did resolve.
    // Text the file could not be read at has no method, URL or body to send.
    // Handing it to the runner would produce a parse error naming a line
    // number in a document the user never sees, so say what is actually wrong.
    if base.is_unreadable() {
        return (
            RunOutput {
                entries: vec![],
                error: Some(UNREADABLE_REQUEST_ERROR.to_string()),
            },
            HashMap::new(),
            Vec::new(),
        );
    }
    let (vars, gen_errors) = effective_vars_reporting(base, vars);
    // A failed `[Gen]` row is left unbound, so going on would send the request
    // with `{{sig}}` where the signature should be — a 401 whose cause is three
    // screens away. The interactive send refuses for this reason; a report run
    // has to refuse too, or the one path nobody is watching becomes the one
    // that lies. Reported in English like `UNREADABLE_REQUEST_ERROR`: this is
    // the front-end-agnostic layer and has no `Strings`, and the alternative —
    // a `RunOutput` that carries the errors structurally — is a wider change
    // than the message is worth.
    if !gen_errors.is_empty() {
        let english = crate::i18n::Strings::for_language(&crate::i18n::Language::English);
        return (
            RunOutput {
                entries: vec![],
                error: Some(crate::i18n::summarise_gen_errors(&english, &gen_errors).join("; ")),
            },
            HashMap::new(),
            gen_errors,
        );
    }
    // Read off the block's results *before* the borrow of `vars` ends: these
    // are the values this send actually used, not what a second evaluation
    // would produce.
    let generated: HashMap<String, String> = base
        .generators
        .iter()
        .filter_map(|(name, _)| vars.get(name).map(|v| (name.clone(), v.clone())))
        .collect();
    let resolved = resolve_entry(base, &vars);
    let mut run_entry = to_run_entry(base, resolved);
    run_entry.captures.extend(extra_captures.iter().cloned());
    strip_variable_options(&mut run_entry);

    let mut entries = [run_entry];
    if let Err(e) = expand_base64_form_fields(&mut entries, file_root) {
        return (
            RunOutput {
                entries: vec![],
                error: Some(format!("Base64 file error: {e}")),
            },
            generated,
            Vec::new(),
        );
    }
    let staged_dir = stage_out_of_scope_form_files(&mut entries, file_root).unwrap_or_default();
    let mut run_entry = entries.into_iter().next().unwrap();
    run_entry.ensure_run_content_length();
    let run_root = staged_dir.as_deref().or(file_root);

    let content = run_entry.to_hurl();
    let out = run_hurl(&content, &vars, run_root);
    if let Some(dir) = &staged_dir {
        let _ = std::fs::remove_dir_all(dir);
    }
    (out, generated, Vec::new())
}

/// Why a request that could not be read cannot be sent. Front-end agnostic, so
/// the terminal, the GUI and the report interpreter all say the same thing.
pub const UNREADABLE_REQUEST_ERROR: &str = "This request could not be read from the file, so there is nothing to send. \
     Open it in Raw Mode (Shift+H) to repair the Hurl text.";

/// Human-readable error for the one request shape that must never be sent: a
/// `[Form]`/`[Multipart]` section together with a raw body.
///
/// Hurl builds both onto the same libcurl handle, so the body overwrites the
/// form and the fields never leave the machine — while the `Content-Type` is
/// still chosen from the form, so the body goes out mislabelled. Nothing
/// errors: the request returns a perfectly good response to something the user
/// never asked for. Both front-ends refuse such a request before it gets here
/// (see [`body_form_conflicts`]); this is the backstop for every other caller,
/// and for a `.hurl` edited by hand.
const BODY_FORM_CONFLICT_ERROR: &str = "Can't send: a request can't have both a Body and Form/Multipart fields (Hurl sends the body and silently drops the fields) — remove one.";

/// Run the collection's selected entry on a background thread via the Hurl
/// runner, mapping the result (status, body, headers, `[Asserts]`, error) into
/// the shared `ApiResponse` (used for the "in flight" spinner while sending)
/// **and** returning a `Receiver` that carries the same finished response
/// (plus any captured values) tagged with the collection id and the entry's
/// index, so [`drain_capture_updates`] can remember it as that specific
/// entry's [`HurlEntry::last_response`]. Returns `None` when the request
/// can't be built at all (e.g. the Body/Form conflict) — nothing ran, so
/// there's nothing to route back.
pub fn run_collection(
    col: &Collection,
    env: Option<&Environment>,
    state: Arc<Mutex<ApiResponse>>,
) -> Option<Receiver<CaptureUpdate>> {
    let base = col.entries.get(col.selected_entry)?;
    if base.body_form_conflict() {
        let mut r = state.lock().unwrap();
        r.loading = false;
        r.error = BODY_FORM_CONFLICT_ERROR.to_string();
        return None;
    }
    let base = base.clone();
    let vars = collection_vars(env, &col.captures);
    let col_id = col.id;
    let entry_idx = col.selected_entry;
    let file_root = col
        .path
        .as_ref()
        .and_then(|p| p.parent().map(std::path::PathBuf::from));

    let (tx, rx) = mpsc::channel();
    thread::spawn(move || {
        // The whole per-request pipeline (base64-form expansion, out-of-scope
        // form-file staging, content-length defaulting, serialize + run) lives
        // in the shared, front-end-agnostic `run_resolved_entry` so this send
        // and the report interpreter stay in exact lockstep. A base64/staging
        // failure comes back as `RunOutput { entries: [], error }` and surfaces
        // via the `None` arm below.
        let (out, generated, gen_errors) =
            run_resolved_entry_reporting(&base, &vars, file_root.as_deref(), &[]);
        let mut r = state.lock().unwrap();
        r.loading = false;
        // Kept structurally as well as in `error`: only the front-end knows
        // the language, and "Request error:" is the wrong heading for a
        // request that was never made (see `ApiResponse::error_text`).
        r.gen_errors = gen_errors;
        match out.entries.into_iter().next() {
            Some(eo) => {
                r.status = eo.status;
                r.status_text = eo.status_text;
                r.body = Arc::from(eo.body);
                r.headers = eo.headers;
                r.assert_results = eo.asserts;
                r.duration_ms = Some(eo.duration_ms);
                // Surface a transport failure / failed assert on the status bar.
                r.error = eo.error.or(out.error).unwrap_or_default();
                // The block's values go back with the captures, so the next
                // request sees a `nonce` this one computed exactly the way it
                // sees a token this one captured. A `[Captures]` row of the
                // same name is the later, more specific statement and wins.
                let mut values: HashMap<String, String> = generated;
                values.extend(eo.captures);
                let _ = tx.send(CaptureUpdate {
                    col_id,
                    entry_idx,
                    ok: eo.ok,
                    values,
                    response: r.clone(),
                });
            }
            None => {
                // Parse error, or nothing ran (a failed `# [Gen]` row, an
                // unreadable body file, a staging failure).
                r.error = out.error.unwrap_or_else(|| "no response".to_string());
                // This is still the end of the send, so it has to be announced
                // like one. Saying nothing left the entry stamped `Running`
                // for the rest of the session — the spinner and "Sending…"
                // never cleared, so a request refused *because* it could not
                // be built looked exactly like one waiting on a dead server,
                // which is the opposite of the message. `ok: false` with no
                // values: nothing was captured or computed, and a request
                // that never left is a failure.
                let _ = tx.send(CaptureUpdate {
                    col_id,
                    entry_idx,
                    ok: false,
                    values: HashMap::new(),
                    response: r.clone(),
                });
            }
        }
    });
    Some(rx)
}

/// Build the standalone `ApiResponse` the Response pane shows for one entry of
/// a "Run All" pass (used by both the batch and streaming paths).
fn entry_response(eo: &EntryOutcome) -> ApiResponse {
    ApiResponse {
        status: eo.status,
        status_text: eo.status_text.clone(),
        body: Arc::from(eo.body.as_str()),
        loading: false,
        error: eo.error.clone().unwrap_or_default(),
        headers: eo.headers.clone(),
        assert_results: eo.asserts.clone(),
        duration_ms: Some(eo.duration_ms),
        // A "Run All" entry that ran has no generator failure to carry: the
        // whole-file block is expanded once, up front, and its failures are
        // reported for the run rather than against one response.
        gen_errors: Vec::new(),
    }
}

/// Run every entry in the collection, in order.
///
/// Two modes, mirroring the CLI:
/// - **Streaming** (`batch == false`, the default): each entry runs on its
///   own and results are pushed out as they finish, so the Requests list
///   stamps each pass/fail marker live. Hurl's automatic cookie jar does
///   *not* carry from one request to the next in this mode (an explicit
///   `[Cookies]` section is unaffected) — the caller raises a status-bar
///   warning about that when a streaming Run All starts.
/// - **Batch** (`batch == true`): the whole collection runs in one Hurl
///   execution, so the cookie jar and `[Captures]` chaining apply across the
///   entire run exactly as they would from the command line; a single update
///   is sent when it finishes.
///
/// Always returns a `Receiver` (except when the collection is empty or a
/// Body/Form conflict blocks it outright) since the caller needs it to learn
/// per-entry pass/fail and each entry's own response, regardless of whether
/// anything was captured.
pub fn run_all_entries(
    col: &Collection,
    env: Option<&Environment>,
    state: Arc<Mutex<ApiResponse>>,
    batch: bool,
) -> Option<Receiver<BatchRunUpdate>> {
    if col.entries.is_empty() {
        return None;
    }
    if let Some(bad) = col.entries.iter().find(|e| e.body_form_conflict()) {
        let mut r = state.lock().unwrap();
        r.loading = false;
        r.error = format!("{} ({})", BODY_FORM_CONFLICT_ERROR, bad.title);
        return None;
    }
    let vars = collection_vars(env, &col.captures);
    let col_id = col.id;
    let file_root = col
        .path
        .as_ref()
        .and_then(|p| p.parent().map(std::path::PathBuf::from));
    let total = col.entries.len();
    // Requests the file could not be read at are skipped rather than sent.
    // They are text, not requests, so there is nothing to send — and putting
    // them in the run document would fail the *whole* run to parse, which is
    // the all-or-nothing failure recovery exists to end. Their positions are
    // kept so every result still lands on the request it came from.
    let run_positions: Vec<usize> = col
        .entries
        .iter()
        .enumerate()
        .filter(|(_, e)| !e.is_unreadable())
        .map(|(i, _)| i)
        .collect();
    if run_positions.is_empty() {
        return None;
    }
    let mut run_entries: Vec<HurlEntry> = run_positions
        .iter()
        .map(|&i| col.entries[i].clone())
        .collect();

    let (tx, rx) = mpsc::channel();
    thread::spawn(move || {
        // See the matching comment in `run_collection`: bring any
        // out-of-scope `[Form]`/`[Multipart]` files into a temp directory
        // alongside file_root first. `collection_to_hurl` is (re)computed
        // after expansion + staging so it reflects both.
        // See `run_collection`: expand Base64File fields to their sent Text
        // form before staging, surfacing a read failure as an error.
        if let Err(e) = expand_base64_form_fields(&mut run_entries, file_root.as_deref()) {
            let mut r = state.lock().unwrap();
            r.loading = false;
            r.error = format!("Base64 file error: {e}");
            return;
        }
        let staged_dir = stage_out_of_scope_form_files(&mut run_entries, file_root.as_deref())
            .unwrap_or_default();
        let run_root = staged_dir.as_deref().or(file_root.as_deref());
        for e in &mut run_entries {
            e.ensure_run_content_length();
        }
        // A request's own `[Options] variable:` rows are defaults, so any name
        // the environment/captures already bind must not be re-assigned by the
        // request itself (Hurl's own reading). The rest stay in and Hurl
        // applies them, which is what a default should do.
        strip_bound_variable_options(&mut run_entries, &vars);
        let content = collection_to_hurl(&run_entries);

        let mut results: Vec<Option<bool>> = vec![None; total];
        let mut captures: HashMap<String, String> = HashMap::new();
        let mut responses: Vec<Option<ApiResponse>> = vec![None; total];

        // `# [Gen]` blocks. Same split as the headless runner: batch has no
        // per-request moment to evaluate anything in, so everything is bound
        // once up front (and a name two requests compute collapses to one
        // value — the caller warns before starting such a run); streaming
        // evaluates each block in its own window, which is also what lets a
        // generator read a value an earlier request captured.
        let mut vars = vars;
        if batch {
            let blocks = expand_batch_generators(
                &run_entries,
                &vars,
                &crate::generators::SystemSource::new(),
            );
            // A failed block stops the whole batch, because batch cannot stop
            // less than that: it is one Hurl call over the whole file, so
            // there is no way to run the other requests and skip this one (a
            // streaming run skips just the one — see `EntrySetup::Skip`). And
            // going on is the worst of the three options: the failed row's
            // name is left to whatever else binds it — an environment value, a
            // carried-over capture — so the request goes out well-formed,
            // signed with the wrong thing, and is answered. Nothing is sent.
            if !blocks.errors.is_empty() {
                let flat: Vec<crate::generators::GenError> = blocks
                    .errors
                    .iter()
                    .flat_map(|(_, errs)| errs.iter().cloned())
                    .collect();
                let english = crate::i18n::Strings::for_language(&crate::i18n::Language::English);
                let mut r = state.lock().unwrap();
                r.loading = false;
                r.error = crate::i18n::summarise_gen_errors(&english, &flat).join("; ");
                r.gen_errors = flat;
                // No update is sent: dropping `tx` disconnects the receiver,
                // and the drain takes that as "the run is over", clearing the
                // in-flight marks the caller set on every entry.
                if let Some(dir) = &staged_dir {
                    let _ = std::fs::remove_dir_all(dir);
                }
                return;
            }
            // Bound *and* reported back as captures: a computed value is as
            // much a result of the run as a `[Captures]` row, and the request
            // after it — run on its own afterwards — needs to see it.
            captures.extend(blocks.bound.iter().map(|(k, v)| (k.clone(), v.clone())));
            vars.extend(blocks.bound);
        }
        let out = if batch {
            run_hurl(&content, &vars, run_root)
        } else {
            // Streaming: run each entry on its own and push a cumulative
            // snapshot after every one, so the Requests list stamps each
            // pass/fail marker the instant that entry finishes rather than
            // only once the whole run is done. The poll side drains every
            // queued message per frame, so the intermediate snapshots simply
            // supersede one another. (Cookies set by one request don't carry
            // to the next in this mode — the caller warns about that.)
            // Hurl reports which request each outcome belongs to, and that is
            // not the outcome's ordinal: `[Options] repeat`/`retry` make one
            // request produce several. Trusting the ordinal slid every later
            // result up and dropped the last one off the end.
            let gen_entries = run_entries.clone();
            // Written by the before-each-entry hook and read by the
            // after-each-entry one; both run on this thread, one at a time, so
            // a `RefCell` is enough to let the two closures share it.
            let generated: Rc<RefCell<HashMap<String, String>>> = Rc::default();
            let record_gen = Rc::clone(&generated);
            // A block that fails to evaluate leaves its `{{name}}` unbound, so
            // the request goes out with a literal placeholder and Hurl aborts it
            // on `Undefined variable` — three screens from the real cause. The
            // actual generator error was previously discarded here; collect it
            // so it can be surfaced as the run's error below.
            let gen_errors: Rc<RefCell<Vec<crate::generators::GenError>>> = Rc::default();
            let record_errs = Rc::clone(&gen_errors);
            let mut streamed = crate::hurl::run::run_hurl_streaming_with(
                &content,
                &vars,
                run_root,
                |i, known| {
                    let Some(entry) = gen_entries.get(i) else {
                        return crate::hurl::EntrySetup::Bind(Vec::new());
                    };
                    if entry.generators.is_empty() {
                        return crate::hurl::EntrySetup::Bind(Vec::new());
                    }
                    let mut merged = known.clone();
                    let errs = crate::generators::expand(
                        &entry.generators,
                        &mut merged,
                        &crate::generators::SystemSource::new(),
                    );
                    // A block that failed leaves its name unbound, and the
                    // environment (or an earlier capture) may well bind the
                    // same name -- so the request would go out signed with the
                    // wrong thing and be answered. Not sent: see
                    // `EntrySetup::Skip`.
                    if !errs.is_empty() {
                        let english =
                            crate::i18n::Strings::for_language(&crate::i18n::Language::English);
                        let reason = crate::i18n::summarise_gen_errors(&english, &errs).join("; ");
                        record_errs.borrow_mut().extend(errs);
                        return crate::hurl::EntrySetup::Skip { reason };
                    }
                    let bound: Vec<(String, String)> = entry
                        .generators
                        .iter()
                        .filter_map(|(name, _)| merged.get(name).map(|v| (name.clone(), v.clone())))
                        .collect();
                    record_gen.borrow_mut().extend(bound.iter().cloned());
                    crate::hurl::EntrySetup::Bind(bound)
                },
                |eo| {
                    if let Some(&at) = run_positions.get(eo.entry_index) {
                        results[at] = Some(eo.ok);
                        // Computed values first so a `[Captures]` row of the same
                        // name — the later, more specific statement — still wins.
                        captures.extend(
                            generated
                                .borrow()
                                .iter()
                                .map(|(k, v)| (k.clone(), v.clone())),
                        );
                        captures.extend(eo.captures.iter().cloned());
                        responses[at] = Some(entry_response(eo));
                    }
                    let _ = tx.send(BatchRunUpdate {
                        col_id,
                        results: results.clone(),
                        captures: captures.clone(),
                        responses: responses.clone(),
                    });
                },
            );
            // Prefer the generator error over Hurl's downstream `Undefined
            // variable`: the unbound placeholder is a symptom, the failed block
            // is the cause. Only when the run itself reported nothing else.
            if streamed.error.is_none() {
                let errs = gen_errors.borrow();
                if !errs.is_empty() {
                    let english =
                        crate::i18n::Strings::for_language(&crate::i18n::Language::English);
                    streamed.error =
                        Some(crate::i18n::summarise_gen_errors(&english, &errs).join("; "));
                }
            }
            streamed
        };
        if let Some(dir) = &staged_dir {
            let _ = std::fs::remove_dir_all(dir);
        }
        let mut r = state.lock().unwrap();
        r.loading = false;
        match out.entries.last() {
            Some(last) => {
                r.status = last.status;
                r.status_text = last.status_text.clone();
                r.body = Arc::from(last.body.as_str());
                r.headers = last.headers.clone();
                r.assert_results = last.asserts.clone();
                r.error = last
                    .error
                    .clone()
                    .or_else(|| out.error.clone())
                    .unwrap_or_default();
            }
            None => {
                r.error = out
                    .error
                    .clone()
                    .unwrap_or_else(|| "no response".to_string());
            }
        }
        // Batch ran the whole collection in one call, so fill the per-entry
        // vectors from the final result set and send a single update.
        // (Streaming already emitted its final cumulative snapshot above.)
        if batch {
            for eo in out.entries.iter() {
                // Keyed by the request Hurl says produced this outcome, not by
                // the outcome's position: see the streaming path above.
                let Some(&at) = run_positions.get(eo.entry_index) else {
                    continue;
                };
                results[at] = Some(eo.ok);
                captures.extend(eo.captures.iter().cloned());
                responses[at] = Some(entry_response(eo));
            }
            let _ = tx.send(BatchRunUpdate {
                col_id,
                results,
                captures,
                responses,
            });
        }
    });
    Some(rx)
}

/// Drain completed single-entry run results: merge captured values into the
/// collection's capture map, invalidate its cached preview so newly captured
/// values flow into subsequent requests, and remember the response on the
/// entry itself (`HurlEntry::last_response`) so the Response pane keeps
/// showing that entry's own last response even after the user selects a
/// different one. Returns `true` while any run is still in flight (so the UI
/// keeps repainting).
pub fn drain_capture_updates(
    pending: &mut Vec<Receiver<CaptureUpdate>>,
    collections: &mut [Collection],
) -> bool {
    if pending.is_empty() {
        return false;
    }
    let mut still = Vec::new();
    for rx in std::mem::take(pending) {
        let mut disconnected = false;
        loop {
            match rx.try_recv() {
                Ok(update) => {
                    for col in collections.iter_mut() {
                        if col.id == update.col_id {
                            for (k, v) in &update.values {
                                col.captures.insert(k.clone(), v.clone());
                            }
                            col.invalidate_request_json();
                            if let Some(entry) = col.entries.get_mut(update.entry_idx) {
                                entry.last_response = Some(update.response.clone());
                                // The send finished — stamp the pass/fail marker
                                // and clear the "sending" (Running) state so the
                                // Response pane stops showing the spinner for
                                // this entry (only the still-in-flight entry does).
                                entry.last_run = if update.ok {
                                    RunStatus::Passed
                                } else {
                                    RunStatus::Failed
                                };
                            }
                        }
                    }
                }
                Err(TryRecvError::Empty) => break,
                Err(TryRecvError::Disconnected) => {
                    disconnected = true;
                    break;
                }
            }
        }
        if !disconnected {
            still.push(rx);
        }
    }
    *pending = still;
    !pending.is_empty()
}

/// Every piece of an entry's text that reaches the wire *as a Hurl template* —
/// so anything scanning for `{{…}}` sees exactly the places a placeholder is
/// substituted, and no more.
///
/// Shared by [`entry_referenced_keys`] and [`entry_placeholder_problems`]
/// deliberately: the set of fields "a variable can appear in" and the set it is
/// "checked in" drifting apart would mean a placeholder that resolves but is
/// never validated, which is the exact bug the validation exists to catch.
fn for_each_wire_text(entry: &HurlEntry, mut visit: impl FnMut(&str)) {
    visit(&entry.url);
    for r in entry.headers.iter().chain(&entry.queries) {
        visit(&r.key);
        visit(&r.value);
    }
    for f in &entry.form_fields {
        visit(&f.key);
        visit(&f.value);
    }
    for r in &entry.cookies {
        visit(&r.key);
        visit(&r.value);
    }
    if let Some((u, p)) = &entry.basic_auth {
        visit(u);
        visit(p);
    }
    // A `{{ var }}` written inside a comment is never sent, so it doesn't
    // count as a use of that variable.
    if let Some(body) = entry.body_wire() {
        visit(&body);
    }
}

/// Environment variable names referenced (via `{{ KEY }}`) anywhere in `entry`.
pub fn entry_referenced_keys(entry: &HurlEntry) -> std::collections::HashSet<String> {
    let mut keys = std::collections::HashSet::new();
    for_each_wire_text(entry, |text| {
        keys.extend(crate::environment::referenced_keys(text))
    });
    keys
}

/// Placeholders in `entry` that Hurl would read differently from PaperBoy — see
/// [`placeholder_problems`](crate::hurl::placeholder_problems). Deduplicated,
/// keeping the order written, since the same `{{ api.key }}` typed into three
/// headers is one mistake and should be said once.
pub fn entry_placeholder_problems(entry: &HurlEntry) -> Vec<crate::hurl::PlaceholderProblem> {
    let mut out: Vec<crate::hurl::PlaceholderProblem> = Vec::new();
    for_each_wire_text(entry, |text| {
        for p in crate::hurl::placeholder_problems(text) {
            if !out.contains(&p) {
                out.push(p);
            }
        }
    });
    out
}

/// Secret variables the selected entry needs but that haven't resolved yet.
/// While this is non-empty the request must not be sent. Returns the blocking
/// variable names, sorted for stable display.
pub fn pending_request_keys(col: &Collection, env: Option<&Environment>) -> Vec<String> {
    let Some(env) = env else { return Vec::new() };
    let Some(entry) = col.entries.get(col.selected_entry) else {
        return Vec::new();
    };
    let referenced = entry_referenced_keys(entry);
    let mut blocking: Vec<String> = env
        .vars
        .iter()
        .filter(|v| v.is_pending() && referenced.contains(&v.key))
        .map(|v| v.key.clone())
        .collect();
    blocking.sort();
    blocking.dedup();
    blocking
}

/// Same as [`pending_request_keys`], but across every entry in the collection
/// — used to gate "Run All", which sends every request, not just the selected
/// one.
pub fn pending_request_keys_all(col: &Collection, env: Option<&Environment>) -> Vec<String> {
    let Some(env) = env else { return Vec::new() };
    let mut referenced = std::collections::HashSet::new();
    for entry in &col.entries {
        referenced.extend(entry_referenced_keys(entry));
    }
    let mut blocking: Vec<String> = env
        .vars
        .iter()
        .filter(|v| v.is_pending() && referenced.contains(&v.key))
        .map(|v| v.key.clone())
        .collect();
    blocking.sort();
    blocking.dedup();
    blocking
}

/// Every variable name the collection can supply a value for, whether or not it
/// has one yet: the environment's variables, every `[Captures]` name any request
/// defines, and anything already captured this session.
///
/// A `[Captures]` name counts as defined even before it holds a value, because
/// a collection that logs in and then uses `{{ token }}` is correct — the value
/// arrives mid-run. Treating those as undefined would put a warning on every
/// well-formed collection, which is the fastest way to teach someone to ignore
/// warnings.
fn defined_keys(col: &Collection, env: Option<&Environment>) -> std::collections::HashSet<String> {
    let mut defined: std::collections::HashSet<String> = std::collections::HashSet::new();
    if let Some(env) = env {
        defined.extend(env.vars.iter().map(|v| v.key.clone()));
    }
    for entry in &col.entries {
        defined.extend(entry.captures.iter().map(|(name, _)| name.clone()));
        // A `# [Gen]` row defines its name just as surely as a capture does —
        // it is computed rather than fetched, but `{{sig}}` is not a typo.
        defined.extend(entry.generators.iter().map(|(name, _)| name.clone()));
    }
    defined.extend(col.captures.keys().cloned());
    defined
}

/// Variables the selected request references that nothing defines — the typo'd
/// `{{ tokn }}`, or the whole environment nobody remembered to activate.
///
/// Unlike [`pending_request_keys`] this does **not** block the run: sending a
/// literal `{{ tokn }}` is legal (Hurl will do it), and a front-end may have
/// good reason to. It is reported instead, loudly, because the failure it
/// causes otherwise surfaces as an unexplained 401 several steps later.
/// Sorted for stable display.
pub fn undefined_request_keys(col: &Collection, env: Option<&Environment>) -> Vec<String> {
    let Some(entry) = col.entries.get(col.selected_entry) else {
        return Vec::new();
    };
    let defined = defined_keys(col, env);
    let mut out: Vec<String> = entry_referenced_keys(entry)
        .into_iter()
        .filter(|k| !defined.contains(k))
        .collect();
    out.sort();
    out
}

/// [`undefined_request_keys`] across every entry in the collection — used by
/// "Run All", which sends all of them.
pub fn undefined_request_keys_all(col: &Collection, env: Option<&Environment>) -> Vec<String> {
    let defined = defined_keys(col, env);
    let mut out: Vec<String> = col
        .entries
        .iter()
        .flat_map(entry_referenced_keys)
        .filter(|k| !defined.contains(k))
        .collect();
    out.sort();
    out.dedup();
    out
}

/// The name of the selected request if it carries both a raw body and enabled
/// form fields, which Hurl cannot send together — see
/// [`HurlEntry::body_form_conflict`](crate::hurl::HurlEntry::body_form_conflict).
///
/// Unlike [`undefined_request_keys`] this *does* block the run. An undefined
/// variable is sent literally and fails visibly; this one succeeds while
/// quietly dropping every form field, so the only way for a user to find out is
/// to notice that the server behaved as though the fields were never there.
pub fn body_form_conflicts(col: &Collection) -> Vec<String> {
    col.entries
        .get(col.selected_entry)
        .filter(|e| e.body_form_conflict())
        .map(|e| vec![request_label(e)])
        .unwrap_or_default()
}

/// [`body_form_conflicts`] across every entry — used by "Run All".
pub fn body_form_conflicts_all(col: &Collection) -> Vec<String> {
    col.entries
        .iter()
        .filter(|e| e.body_form_conflict())
        .map(request_label)
        .collect()
}

/// Placeholders in the selected request that Hurl reads differently from
/// PaperBoy, rendered for display as `written → read`.
///
/// Blocking, for the same reason as [`body_form_conflicts`] and not the same
/// reason as [`undefined_request_keys`]: an undefined variable goes on the wire
/// literally and fails loudly, but `{{ api.key }}` goes on the wire as the
/// value of `api` and the server answers as though it were asked a sensible
/// question. There is no version of that a user finds by reading the response.
pub fn truncated_placeholders(col: &Collection) -> Vec<String> {
    col.entries
        .get(col.selected_entry)
        .map(|e| describe_placeholder_problems(&entry_placeholder_problems(e)))
        .unwrap_or_default()
}

/// [`truncated_placeholders`] across every entry — used by "Run All".
pub fn truncated_placeholders_all(col: &Collection) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for e in &col.entries {
        for d in describe_placeholder_problems(&entry_placeholder_problems(e)) {
            if !out.contains(&d) {
                out.push(d);
            }
        }
    }
    out
}

/// Anything wrong with the selected request's `# [Gen]` block, described for a
/// status message.
///
/// Reported rather than blocking, unlike [`truncated_placeholders`]. A row that
/// fails binds nothing, so its `{{sig}}` goes on the wire literally and comes
/// back a loud 401 — the [`undefined_request_keys`] situation, not the
/// [`body_form_conflicts`] one. The reason is still worth saying at the moment
/// it happens, because "401" is a poor way to learn you misspelled `hmac_sha256`.
pub fn generator_problems(col: &Collection, env: Option<&Environment>) -> Vec<GenError> {
    let Some(entry) = col.entries.get(col.selected_entry) else {
        return Vec::new();
    };
    describe_generator_errors(entry, env, &col.captures)
}

/// [`generator_problems`] across every entry — used by "Run All".
pub fn generator_problems_all(col: &Collection, env: Option<&Environment>) -> Vec<GenError> {
    let mut out: Vec<GenError> = Vec::new();
    for entry in &col.entries {
        for d in describe_generator_errors(entry, env, &col.captures) {
            if !out.contains(&d) {
                out.push(d);
            }
        }
    }
    out
}

/// Everything a whole-file run needs to know about the `# [Gen]` blocks it is
/// about to carry.
///
/// A block belongs to one request and is evaluated per send, which a streaming
/// run can honour (each entry gets its own window) but a batch run cannot: batch
/// is a single Hurl call over the whole file, so there is no "before this
/// request" moment to evaluate anything in. Everything is therefore evaluated
/// once, up front, against the environment alone — and a name two requests each
/// compute collapses to one value for both, which is the [`collisions`] list.
///
/// [`collisions`]: BatchGenerators::collisions
pub struct BatchGenerators {
    /// The names bound for the run. Where two requests compute the same name
    /// the first request's value is kept, matching the order Hurl would have
    /// run them in.
    pub bound: HashMap<String, String>,
    /// What failed, per request, paired with that request's title so the
    /// caller can say which one it was.
    pub errors: Vec<(String, Vec<GenError>)>,
    /// Names computed by more than one request. In a streaming run each of
    /// those requests gets its own value; in a batch run they share the first,
    /// so the caller warns rather than silently sending one request's nonce
    /// with another's signature.
    pub collisions: Vec<String>,
    /// Names a `# [Gen]` block computes that an environment variable (or a
    /// carried-over capture) *already* binds. A streaming run shadows the
    /// environment value only from the computing request onwards; a batch run
    /// shares one value set across the whole file, so binding the computed value
    /// up front would rewrite it for the requests *above* the generator too —
    /// requests that may have no block at all. Batch therefore leaves the
    /// environment value in place (see [`expand_batch_generators`]) and lists
    /// the name here so the user is told their generator did nothing in batch.
    pub shadowed: Vec<String>,
}

/// Evaluate every entry's `# [Gen]` block once, for a batch (whole-file) run.
///
/// Shared by the headless runner and "Run All" in batch mode so the two cannot
/// drift on which value a name ends up with.
pub fn expand_batch_generators(
    entries: &[crate::hurl::HurlEntry],
    vars: &HashMap<String, String>,
    src: &dyn crate::generators::GenSource,
) -> BatchGenerators {
    let mut bound = HashMap::new();
    let mut errors = Vec::new();
    let mut collisions: Vec<String> = Vec::new();
    let mut shadowed: Vec<String> = Vec::new();
    // Which request first claimed each name, so a second claim is recognised
    // as a collision rather than as the same request being listed twice (a
    // repeated name *within* one block is that block's own business).
    let mut claimed: HashMap<String, usize> = HashMap::new();

    for (i, e) in entries.iter().enumerate() {
        if e.generators.is_empty() {
            continue;
        }
        // Each block is evaluated against the environment plus what earlier
        // blocks bound — not against the run's own captures, which do not
        // exist yet in a batch run.
        let mut merged = vars.clone();
        merged.extend(
            bound
                .iter()
                .map(|(k, v): (&String, &String)| (k.clone(), v.clone())),
        );
        let errs = crate::generators::expand(&e.generators, &mut merged, src);
        if !errs.is_empty() {
            errors.push((e.title.clone(), errs));
        }
        for (name, _) in &e.generators {
            match claimed.get(name) {
                Some(&first) if first != i => {
                    if !collisions.contains(name) {
                        collisions.push(name.clone());
                    }
                    continue;
                }
                _ => {}
            }
            claimed.entry(name.clone()).or_insert(i);
            // A name the *environment* already binds is left alone: overriding
            // it here would rewrite it for every request in the file, including
            // the ones above this generator that never asked. Streaming would
            // shadow it only from here on, which one shared value set can't
            // reproduce — so keep the environment value and warn instead.
            if vars.contains_key(name) {
                if !shadowed.contains(name) {
                    shadowed.push(name.clone());
                }
                continue;
            }
            if let Some(v) = merged.get(name) {
                bound.entry(name.clone()).or_insert_with(|| v.clone());
            }
        }
    }

    BatchGenerators {
        bound,
        errors,
        collisions,
        shadowed,
    }
}

/// The `# [Gen]` names more than one request in the collection computes.
///
/// Only a batch run has to care (see [`BatchGenerators::collisions`]); the
/// front-ends call this to warn before starting one.
pub fn generator_collisions(col: &Collection) -> Vec<String> {
    let mut claimed: Vec<&str> = Vec::new();
    let mut out: Vec<String> = Vec::new();
    for e in &col.entries {
        let mut seen_here: Vec<&str> = Vec::new();
        for (name, _) in &e.generators {
            if seen_here.contains(&name.as_str()) {
                continue;
            }
            seen_here.push(name);
            if claimed.contains(&name.as_str()) {
                if !out.contains(name) {
                    out.push(name.clone());
                }
            } else {
                claimed.push(name);
            }
        }
    }
    out
}

/// The `# [Gen]` names a batch run would compute over a value the environment
/// (or a carried-over capture) already binds.
///
/// Only a batch run has to care (see [`BatchGenerators::shadowed`]): it shares
/// one value set, so it leaves the environment value in place for the whole
/// file rather than rewriting it for the requests above the generator. The
/// front-ends call this to warn before starting such a run — the computed value
/// won't be used, and dropping `--batch` is usually the fix.
pub fn generator_env_shadows(col: &Collection, env: Option<&Environment>) -> Vec<String> {
    let vars = collection_vars(env, &col.captures);
    let mut out: Vec<String> = Vec::new();
    for e in &col.entries {
        for (name, _) in &e.generators {
            if vars.contains_key(name) && !out.contains(name) {
                out.push(name.clone());
            }
        }
    }
    out
}

fn describe_generator_errors(
    entry: &crate::hurl::HurlEntry,
    env: Option<&Environment>,
    captures: &HashMap<String, String>,
) -> Vec<GenError> {
    if entry.generators.is_empty() {
        return Vec::new();
    }
    let vars = collection_vars(env, captures);
    // Every failure this reports is deterministic — a syntax error, an unknown
    // function, a bad reference — so evaluating here and again at send time
    // cannot disagree, even though the random and time values will differ.
    //
    // Deterministic is not the same as free, though: this runs on the way to
    // every send, and a `counter` advanced by being asked about would count
    // each send twice (1, 3, 5). `DryRunSource` reads the counter instead.
    effective_vars_with(entry, &vars, &crate::generators::DryRunSource).1
}

/// Render placeholder problems for a status message. A truncation says what it
/// becomes (`{{api.key}} → api`), because the whole difficulty of the bug is
/// that the text looks right; an unparsable one has no "becomes" to show.
fn describe_placeholder_problems(problems: &[crate::hurl::PlaceholderProblem]) -> Vec<String> {
    use crate::hurl::PlaceholderProblem as P;
    problems
        .iter()
        .map(|p| match p {
            P::Truncated { written, read } => format!("{written}{read}"),
            P::Unparsable { written } => written.clone(),
        })
        .collect()
}

/// How a request is named in a message about it: its title, or its method and
/// URL when it hasn't been given one (an untitled request is still worth
/// pointing at).
fn request_label(e: &crate::hurl::HurlEntry) -> String {
    if e.title.trim().is_empty() {
        format!("{} {}", e.method, e.url)
    } else {
        e.title.clone()
    }
}

/// Drain background secret-resolution results, applying each to the matching
/// Global Environment and invalidating every collection's cached preview (any
/// of them might reference it, linked or active-global). Disconnected
/// channels are dropped. Returns `true` while any resolution is still in
/// flight. Shared by both front-ends' per-frame/per-tick update loop.
pub fn drain_env_updates(
    pending: &mut Vec<Receiver<EnvUpdate>>,
    global_envs: &mut [Environment],
    collections: &mut [Collection],
) -> bool {
    if pending.is_empty() {
        return false;
    }
    let mut still = Vec::new();
    for rx in std::mem::take(pending) {
        let mut disconnected = false;
        loop {
            match rx.try_recv() {
                Ok(update) => {
                    for env in global_envs.iter_mut() {
                        if env.id == update.env_id {
                            env.apply_update(&update);
                            for col in collections.iter_mut() {
                                col.invalidate_request_json();
                            }
                        }
                    }
                }
                Err(TryRecvError::Empty) => break,
                Err(TryRecvError::Disconnected) => {
                    disconnected = true;
                    break;
                }
            }
        }
        if !disconnected {
            still.push(rx);
        }
    }
    *pending = still;
    !pending.is_empty()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::collection::Collection;
    use crate::environment::{EnvVar, Environment, ValueSource};
    use crate::hurl::{FormField, FormFieldKind, HurlEntry};

    #[test]
    fn request_json_is_alphabetically_ordered_and_round_trips() {
        let entry = HurlEntry {
            method: "POST".into(),
            url: "http://example.com/api".into(),
            // Deliberately out of order to prove keys come out sorted.
            headers: vec![
                KvRow::toggled("X-Zed", "z", true),
                KvRow::toggled("Authorization", "Bearer t", true),
            ],
            cookies: vec![KvRow::toggled("session", "abc", true)],
            queries: vec![KvRow::toggled("page", "2", true)],
            basic_auth: Some(("alice".into(), "secret".into())),
            form_fields: vec![
                FormField {
                    key: "name".into(),
                    value: "widget".into(),
                    kind: FormFieldKind::Text,
                    content_type: None,
                    base64_prefix: None,
                    enabled: true,
                    desc: String::new(),
                },
                FormField {
                    key: "file".into(),
                    value: "./a.bin".into(),
                    kind: FormFieldKind::File,
                    content_type: Some("application/octet-stream".into()),
                    base64_prefix: None,
                    enabled: true,
                    desc: String::new(),
                },
            ],
            body_src: Some(r#"{"a":1}"#.into()),
            ..Default::default()
        };

        let json = build_request_json(&entry);
        let expected = r#"{
  "basic_auth": {
    "pass": "secret",
    "user": "alice"
  },
  "body": {
    "a": 1
  },
  "cookies": {
    "session": "abc"
  },
  "form_fields": [
    {
      "key": "name",
      "type": "text",
      "value": "widget"
    },
    {
      "content_type": "application/octet-stream",
      "key": "file",
      "type": "file",
      "value": "./a.bin"
    }
  ],
  "headers": {
    "Authorization": "Bearer t",
    "X-Zed": "z"
  },
  "method": "POST",
  "query_params": {
    "page": "2"
  },
  "url": "http://example.com/api"
}"#;
        assert_eq!(json, expected);

        // Re-parsing yields the same request fields (headers/cookies/params are
        // sorted by key on the round trip, matching the serialized object).
        let back = apply_request_json(&HurlEntry::default(), &json).unwrap();
        assert_eq!(back.method, "POST");
        assert_eq!(back.url, "http://example.com/api");
        assert_eq!(back.basic_auth, Some(("alice".into(), "secret".into())));
        assert_eq!(
            back.headers,
            vec![
                ("Authorization".to_string(), "Bearer t".to_string(), true),
                ("X-Zed".to_string(), "z".to_string(), true),
            ]
        );
        assert_eq!(back.form_fields.len(), 2);
        assert_eq!(back.form_fields[1].kind, FormFieldKind::File);
        assert_eq!(back.body_src.as_deref(), Some("{\n  \"a\": 1\n}"));
    }

    #[test]
    fn apply_request_json_tolerates_non_string_scalars_and_unknown_form_type() {
        let base = HurlEntry::default();
        let text = r#"{
            "method": "GET",
            "url": "http://x",
            "headers": { "X-Count": 5 },
            "form_fields": [ { "key": "k", "value": "v", "type": "weird" } ]
        }"#;
        let entry = apply_request_json(&base, text).unwrap();
        assert_eq!(
            entry.headers,
            vec![("X-Count".to_string(), "5".to_string(), true)]
        );
        assert_eq!(entry.form_fields[0].kind, FormFieldKind::Text);
    }

    #[test]
    fn apply_request_json_rejects_input_without_method_or_url() {
        let base = HurlEntry::default();
        assert!(apply_request_json(&base, "[]").is_err());
        assert!(apply_request_json(&base, r#"{"url":"http://x"}"#).is_err());
    }

    fn me_entry() -> HurlEntry {
        HurlEntry {
            method: "GET".into(),
            url: "{{ BASE_URL }}/me".into(),
            headers: vec![KvRow::new("Authorization", "Bearer {{ API_TOKEN }}")],
            ..Default::default()
        }
    }

    fn env_token(value: &str, resolved: bool) -> Environment {
        Environment {
            id: 0,
            name: "e".into(),
            vars: vec![EnvVar {
                key: "API_TOKEN".into(),
                value: value.into(),
                source: ValueSource::OnePassword,
                resolved,
                loading: false,
                original_value: value.into(),
                modified: false,
                user_added: false,
                raw: String::new(),
            }],
            path: None,
            git_origin: None,
        }
    }

    fn auth_header(col: &Collection, env: Option<&Environment>) -> String {
        let vars = collection_vars(env, &col.captures);
        let headers = resolve_entry(&col.entries[col.selected_entry], &vars).headers;
        headers
            .into_iter()
            .find(|(k, _)| k == "Authorization")
            .unwrap()
            .1
    }

    /// The send path is always rebuilt from the entry + current environment, so
    /// reloading an environment (e.g. once an `op://` secret exists) is reflected
    /// in the request without any cache to invalidate.
    #[test]
    fn reloading_environment_refreshes_the_sent_request() {
        let col = Collection::new("c".into(), vec![me_entry()]);

        // 1st load: the op:// reference doesn't resolve yet → sent unresolved.
        let env = env_token("{{ op://Eng/demo-api/token }}", false);
        assert!(
            auth_header(&col, Some(&env)).contains("op://"),
            "unresolved ref is sent while missing"
        );

        // Secret now exists; reload the environment (resolved).
        let env = env_token("real-secret", true);

        assert_eq!(auth_header(&col, Some(&env)), "Bearer real-secret");
    }

    fn secret_var(key: &str, resolved: bool, loading: bool) -> EnvVar {
        EnvVar {
            key: key.into(),
            value: "{{ op://x }}".into(),
            source: ValueSource::OnePassword,
            resolved,
            loading,
            original_value: "{{ op://x }}".into(),
            modified: false,
            user_added: false,
            raw: String::new(),
        }
    }

    fn env_with(vars: Vec<EnvVar>) -> Environment {
        Environment {
            id: 1,
            name: "e".into(),
            vars,
            path: None,
            git_origin: None,
        }
    }

    #[test]
    fn pending_secret_blocks_the_referencing_request() {
        let entry = HurlEntry {
            method: "GET".into(),
            url: "{{ BASE_URL }}/{{ API_TOKEN }}".into(),
            ..Default::default()
        };
        let col = Collection::new("c".into(), vec![entry]);
        let env = env_with(vec![secret_var("API_TOKEN", false, true)]);

        assert_eq!(
            pending_request_keys(&col, Some(&env)),
            vec!["API_TOKEN".to_string()]
        );
    }

    #[test]
    fn resolved_secret_does_not_block() {
        let entry = HurlEntry {
            method: "GET".into(),
            url: "{{ BASE_URL }}/{{ API_TOKEN }}".into(),
            ..Default::default()
        };
        let col = Collection::new("c".into(), vec![entry]);
        let env = env_with(vec![secret_var("API_TOKEN", true, false)]);

        assert!(pending_request_keys(&col, Some(&env)).is_empty());
    }

    #[test]
    fn unreferenced_pending_secret_does_not_block() {
        let entry = HurlEntry {
            method: "GET".into(),
            url: "{{ BASE_URL }}/plain".into(),
            ..Default::default()
        };
        let col = Collection::new("c".into(), vec![entry]);
        let env = env_with(vec![secret_var("OTHER", false, true)]);

        assert!(
            pending_request_keys(&col, Some(&env)).is_empty(),
            "request doesn't use the loading secret"
        );
    }

    #[test]
    fn pending_request_keys_all_checks_every_entry_not_just_the_selected_one() {
        let first = HurlEntry {
            method: "GET".into(),
            url: "{{ BASE_URL }}/plain".into(),
            ..Default::default()
        };
        let second = HurlEntry {
            method: "GET".into(),
            url: "{{ BASE_URL }}/{{ API_TOKEN }}".into(),
            ..Default::default()
        };
        let mut col = Collection::new("c".into(), vec![first, second]);
        col.selected_entry = 0; // the pending secret is only used by entry 1
        let env = env_with(vec![secret_var("API_TOKEN", false, true)]);

        assert!(
            pending_request_keys(&col, Some(&env)).is_empty(),
            "the selected entry alone doesn't reference it"
        );
        assert_eq!(
            pending_request_keys_all(&col, Some(&env)),
            vec!["API_TOKEN".to_string()],
            "Run All must check every entry, not just the selected one"
        );
    }

    // ── Undefined variables ───────────────────────────────────────────────

    fn plain_var(key: &str, value: &str) -> EnvVar {
        EnvVar {
            key: key.into(),
            value: value.into(),
            source: ValueSource::Literal,
            resolved: true,
            loading: false,
            original_value: value.into(),
            modified: false,
            user_added: false,
            raw: String::new(),
        }
    }

    #[test]
    fn a_variable_no_one_defines_is_reported() {
        let entry = HurlEntry {
            method: "GET".into(),
            url: "{{ BASE_URL }}/{{ tokn }}".into(),
            ..Default::default()
        };
        let col = Collection::new("c".into(), vec![entry]);
        let env = env_with(vec![plain_var("BASE_URL", "http://x")]);

        assert_eq!(
            undefined_request_keys(&col, Some(&env)),
            vec!["tokn".to_string()],
            "the typo is named; the variable that exists is not"
        );
    }

    #[test]
    fn a_capture_name_counts_as_defined_before_it_has_a_value() {
        // The "log in, then use {{ token }}" shape: the value only exists
        // mid-run, but the collection is correct and must not be flagged.
        let login = HurlEntry {
            method: "POST".into(),
            url: "http://x/login".into(),
            captures: vec![("token".into(), "jsonpath \"$.token\"".into())],
            ..Default::default()
        };
        let use_it = HurlEntry {
            method: "GET".into(),
            url: "http://x/me?t={{ token }}".into(),
            ..Default::default()
        };
        let mut col = Collection::new("c".into(), vec![login, use_it]);
        col.selected_entry = 1;

        assert!(
            undefined_request_keys(&col, None).is_empty(),
            "a captured name is defined even before the capture has run"
        );
    }

    #[test]
    fn a_pending_secret_is_defined_not_undefined() {
        // It has a source and is on its way — that's WaitingSecrets' job to
        // report, and double-reporting it would be noise.
        let entry = HurlEntry {
            method: "GET".into(),
            url: "{{ API_TOKEN }}".into(),
            ..Default::default()
        };
        let col = Collection::new("c".into(), vec![entry]);
        let env = env_with(vec![secret_var("API_TOKEN", false, true)]);

        assert!(undefined_request_keys(&col, Some(&env)).is_empty());
    }

    #[test]
    fn with_no_environment_active_every_referenced_variable_is_undefined() {
        let entry = HurlEntry {
            method: "GET".into(),
            url: "{{ BASE_URL }}/x".into(),
            headers: vec![KvRow::toggled(
                "Authorization",
                "Bearer {{ API_KEY }}",
                true,
            )],
            ..Default::default()
        };
        let col = Collection::new("c".into(), vec![entry]);

        assert_eq!(
            undefined_request_keys(&col, None),
            vec!["API_KEY".to_string(), "BASE_URL".to_string()],
            "sorted, and headers are scanned as well as the URL"
        );
    }

    #[test]
    fn undefined_request_keys_all_checks_every_entry_and_dedupes() {
        let first = HurlEntry {
            method: "GET".into(),
            url: "{{ nope }}/a".into(),
            ..Default::default()
        };
        let second = HurlEntry {
            method: "GET".into(),
            url: "{{ nope }}/b".into(),
            ..Default::default()
        };
        let mut col = Collection::new("c".into(), vec![first, second]);
        col.selected_entry = 0;

        assert_eq!(
            undefined_request_keys_all(&col, None),
            vec!["nope".to_string()],
            "reported once, not once per entry that uses it"
        );
    }

    // ── Captures ──────────────────────────────────────────────────────────

    fn entry_with_generators(url: &str, rows: &[(&str, &str)]) -> HurlEntry {
        let mut e = HurlEntry {
            method: "GET".into(),
            url: url.into(),
            ..Default::default()
        };
        e.generators = rows
            .iter()
            .map(|(n, x)| (n.to_string(), x.to_string()))
            .collect();
        e
    }

    // ── Whole-file `# [Gen]` handling ──────────────────────────────────

    /// A batch run has one variable set for the whole file, so two requests
    /// that each compute `nonce` cannot each have their own. Silently picking
    /// one is how a signature ends up computed over the other request's nonce,
    /// so the collision is named and the caller warns.
    #[test]
    fn two_requests_computing_one_name_collide_in_a_batch_run() {
        let entries = vec![
            entry_with_generators("https://x/", &[("nonce", "\"first\"")]),
            entry_with_generators("https://y/", &[("nonce", "\"second\"")]),
        ];
        let blocks = expand_batch_generators(
            &entries,
            &HashMap::new(),
            &crate::generators::SystemSource::new(),
        );
        assert_eq!(blocks.collisions, vec!["nonce".to_string()]);
        assert_eq!(
            blocks.bound.get("nonce").map(String::as_str),
            Some("first"),
            "the first request in the file keeps its value"
        );
        assert!(blocks.errors.is_empty(), "{:?}", blocks.errors);
    }

    /// Only a *second request* claiming the name is a collision. One request
    /// listing a name twice is that block's own business, and reporting it as
    /// a batch-only hazard would send the user looking for a request that
    /// isn't there.
    #[test]
    fn one_request_is_never_in_collision_with_itself() {
        let entries = vec![entry_with_generators(
            "https://x/",
            &[("n", "\"a\""), ("n", "\"b\"")],
        )];
        let blocks = expand_batch_generators(
            &entries,
            &HashMap::new(),
            &crate::generators::SystemSource::new(),
        );
        assert!(blocks.collisions.is_empty(), "{:?}", blocks.collisions);
    }

    /// A later block may read what an earlier one computed — the same reading
    /// streaming gives, so moving between the two modes doesn't change which
    /// names resolve.
    #[test]
    fn a_later_gen_block_can_read_an_earlier_one_in_batch() {
        let entries = vec![
            entry_with_generators("https://x/", &[("base", "\"abc\"")]),
            entry_with_generators("https://y/", &[("derived", "base")]),
        ];
        let blocks = expand_batch_generators(
            &entries,
            &HashMap::new(),
            &crate::generators::SystemSource::new(),
        );
        assert_eq!(blocks.bound.get("derived").map(String::as_str), Some("abc"));
    }

    /// Failures are reported per request: "one of them is wrong" is not a
    /// report when the file has thirty requests in it.
    #[test]
    fn a_failing_block_is_reported_against_its_own_request() {
        let mut bad = entry_with_generators("https://y/", &[("sig", "nope()")]);
        bad.title = "Sign".into();
        let entries = vec![entry_with_generators("https://x/", &[("n", "uuid")]), bad];
        let blocks = expand_batch_generators(
            &entries,
            &HashMap::new(),
            &crate::generators::SystemSource::new(),
        );
        assert_eq!(blocks.errors.len(), 1);
        assert_eq!(blocks.errors[0].0, "Sign");
    }

    /// The pre-flight warning the front-ends show, which reads the collection
    /// rather than evaluating anything.
    #[test]
    fn generator_collisions_names_only_what_two_requests_share() {
        let col = Collection::new(
            "c".into(),
            vec![
                entry_with_generators("https://x/", &[("nonce", "uuid"), ("ts", "timestamp")]),
                entry_with_generators("https://y/", &[("nonce", "uuid")]),
            ],
        );
        assert_eq!(generator_collisions(&col), vec!["nonce".to_string()]);
    }

    /// A computed value is a result of the send, not a detail of it: the
    /// request after the one that signed itself has to be able to echo the
    /// nonce, so the value has to travel out of the send that made it rather
    /// than be recomputed (which would produce a different `uuid`).
    #[test]
    fn a_send_hands_back_what_its_gen_block_computed() {
        let entry = entry_with_generators("https://127.0.0.1:1/", &[("nonce", "\"fixed\"")]);
        let (_out, generated, _errs) =
            run_resolved_entry_reporting(&entry, &HashMap::new(), None, &[]);
        assert_eq!(
            generated.get("nonce").map(String::as_str),
            Some("fixed"),
            "the connection failing doesn't unmake the value it was sent with"
        );
    }

    /// A name the `# [Gen]` block computes is defined by that block. Before
    /// `defined_keys` knew about generators, a request that signed itself
    /// correctly was still reported as referring to an undefined variable.
    #[test]
    fn a_generated_name_is_not_reported_as_undefined() {
        let col = Collection::new(
            "c".into(),
            vec![entry_with_generators(
                "https://x/?n={{nonce}}",
                &[("nonce", "uuid")],
            )],
        );
        assert!(
            undefined_request_keys(&col, None).is_empty(),
            "the block defines nonce"
        );
        assert!(
            generator_problems(&col, None).is_empty(),
            "and the row evaluates"
        );
    }

    /// A row that cannot evaluate is *reported*, not blocked: nothing binds
    /// `sig`, so `{{sig}}` goes out literally and the server rejects it loudly.
    /// The report exists to name the actual mistake instead of leaving the user
    /// to infer it from a 401.
    #[test]
    fn a_generator_row_that_cannot_evaluate_is_reported() {
        let col = Collection::new(
            "c".into(),
            vec![entry_with_generators(
                "https://x/?s={{sig}}",
                &[("sig", "hmac_sha526(k, m)")],
            )],
        );
        let problems = generator_problems(&col, None);
        assert_eq!(problems.len(), 1, "one bad row, one report");
        assert!(
            matches!(
                &problems[0],
                crate::generators::GenError::UnknownFunction { name, function }
                    if name == "sig" && function == "hmac_sha526"
            ),
            "the report names the row and the misspelling: {:?}",
            problems[0]
        );
    }

    /// A request that never leaves has still *finished*, and has to say so.
    /// A failed `# [Gen]` row means nothing is built and no entry runs, and
    /// the arm that handles "nothing ran" used to set the error and stop —
    /// leaving the entry stamped `Running`, so the spinner and "Sending…" sat
    /// there for the rest of the session. The one case where the client knows
    /// immediately that the send is hopeless looked exactly like a request
    /// waiting on a dead server.
    #[test]
    fn a_request_that_could_not_be_built_still_reports_that_it_is_over() {
        let col = Collection::new(
            "c".into(),
            vec![entry_with_generators(
                "https://x/?s={{sig}}",
                &[("sig", "hmac_sha526(k, m)")],
            )],
        );
        let state = Arc::new(Mutex::new(ApiResponse::default()));
        let rx = run_collection(&col, None, state).expect("the run starts");
        let update = rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("a refusal is still an ending, and must be announced");
        assert!(!update.ok, "a request that never left did not pass");
        assert!(
            update.response.error.contains("hmac_sha526"),
            "and the response says which row stopped it: {:?}",
            update.response.error
        );
        assert!(!update.response.loading, "nothing is in flight any more");

        // A second run, drained the way a front-end drains it (the first
        // update was consumed by `recv_timeout` above).
        let state = Arc::new(Mutex::new(ApiResponse::default()));
        let rx = run_collection(&col, None, state).expect("the run starts");
        let mut cols = [col];
        cols[0].entries[0].last_run = RunStatus::Running;
        let mut pending = vec![rx];
        for _ in 0..5 {
            std::thread::sleep(std::time::Duration::from_millis(20));
            drain_capture_updates(&mut pending, &mut cols);
        }
        assert_eq!(
            cols[0].entries[0].last_run,
            RunStatus::Failed,
            "so the front-end stops spinning"
        );
    }

    /// Every failing row is reported, not just the first, so a block with two
    /// mistakes takes one round of fixing rather than two.
    #[test]
    fn every_failing_generator_row_is_reported() {
        let col = Collection::new(
            "c".into(),
            vec![entry_with_generators(
                "https://x/",
                &[("a", "nope()"), ("b", "uuid"), ("c", "{{")],
            )],
        );
        let problems = generator_problems(&col, None);
        let rows: Vec<&str> = problems.iter().map(|e| e.row()).collect();
        assert_eq!(rows, vec!["a", "c"], "b is fine and says nothing");
    }

    /// `generator_problems` only looks at the selected request; `_all` looks at
    /// every one, matching the two run commands they back.
    #[test]
    fn generator_problems_follow_the_selection() {
        let mut col = Collection::new(
            "c".into(),
            vec![
                entry_with_generators("https://x/", &[("a", "uuid")]),
                entry_with_generators("https://y/", &[("b", "nope()")]),
            ],
        );
        col.selected_entry = 0;
        assert!(
            generator_problems(&col, None).is_empty(),
            "entry 0 is sound"
        );
        assert_eq!(
            generator_problems_all(&col, None).len(),
            1,
            "Run All still sees entry 1's mistake"
        );
    }

    /// A generated name keeps its braces in the preview rather than showing a
    /// value: the value doesn't exist until the request is sent, and inventing
    /// one per frame would flicker and still not be what goes on the wire.
    #[test]
    fn a_generated_name_previews_as_computed() {
        let col = Collection::new(
            "c".into(),
            vec![entry_with_generators("https://x/", &[("nonce", "uuid")])],
        );
        let map = subst_map(&col, None);
        let info = map.get("nonce").expect("the generator name is known");
        assert_eq!(info.kind, SubstKind::Computed);
        assert!(info.shown.is_none(), "no value is invented for the preview");
    }

    #[test]
    fn collection_vars_includes_captures_overriding_env() {
        let env = env_with(vec![EnvVar {
            key: "access_token".into(),
            value: "from-env".into(),
            source: ValueSource::Literal,
            resolved: true,
            loading: false,
            original_value: "from-env".into(),
            modified: false,
            user_added: false,
            raw: String::new(),
        }]);
        let mut captures = HashMap::new();
        captures.insert("access_token".to_string(), "from-capture".to_string());

        let vars = collection_vars(Some(&env), &captures);
        assert_eq!(
            vars.get("access_token").unwrap(),
            "from-capture",
            "a fresh capture wins over env"
        );
    }

    /// The top-bar Base URL must not act as a `{{ BASE_URL }}` source: when the
    /// environment doesn't define `BASE_URL`, the placeholder stays unresolved.
    #[test]
    fn base_url_is_not_substituted_from_app_vars() {
        let env = env_with(vec![EnvVar {
            key: "API_TOKEN".into(),
            value: "t".into(),
            source: ValueSource::Literal,
            resolved: true,
            loading: false,
            original_value: "t".into(),
            modified: false,
            user_added: false,
            raw: String::new(),
        }]);
        let vars = collection_vars(Some(&env), &HashMap::new());
        assert!(
            !vars.contains_key("BASE_URL"),
            "BASE_URL is not injected from the app"
        );
        assert_eq!(
            substitute("{{ BASE_URL }}/me", &vars),
            "{{ BASE_URL }}/me",
            "an unresolved {{ BASE_URL }} is left intact so the user notices"
        );
    }

    #[test]
    fn drain_capture_updates_routes_to_the_matching_collection() {
        let c1 = Collection::new("c1".into(), vec![]);
        let mut c2 = Collection::new("c2".into(), vec![HurlEntry::default()]);
        // c2 has a cached preview that must be invalidated when captures arrive.
        c2.request_json_for = Some(0);
        let target = c2.id;

        let (tx, rx) = mpsc::channel();
        let mut values = HashMap::new();
        values.insert("access_token".to_string(), "tok".to_string());
        let response = ApiResponse {
            status: 200,
            body: "ok".into(),
            ..Default::default()
        };
        tx.send(CaptureUpdate {
            col_id: target,
            entry_idx: 0,
            ok: true,
            values,
            response,
        })
        .unwrap();
        drop(tx); // sender gone -> receiver disconnects after the one message

        let mut pending = vec![rx];
        let mut cols = [c1, c2];
        // Drain a few passes so the queued message is consumed.
        for _ in 0..3 {
            drain_capture_updates(&mut pending, &mut cols);
        }

        assert_eq!(
            cols[1].captures.get("access_token").unwrap(),
            "tok",
            "matching collection updated"
        );
        assert!(cols[0].captures.is_empty(), "other collection untouched");
        assert_eq!(cols[1].request_json_for, None, "target preview invalidated");
        assert_eq!(
            cols[1].entries[0].last_response.as_ref().map(|r| r.status),
            Some(200),
            "the entry remembers its own last response"
        );
    }

    /// `run_all_entries` always returns a receiver for a non-empty collection
    /// (unlike `run_collection`, which only returns one when there are
    /// captures) — the caller needs it purely to learn per-entry pass/fail.
    /// Doesn't wait for the background run to finish (this environment's test
    /// network hangs rather than fails fast on unroutable hosts — see
    /// `tui::tests::app_in_main_pane`'s doc comment) — only that the run was
    /// actually kicked off.
    #[test]
    fn run_all_entries_returns_a_receiver_for_a_non_empty_collection() {
        let e1 = HurlEntry {
            method: "GET".into(),
            url: "http://192.0.2.1/one".into(),
            ..Default::default()
        };
        let e2 = HurlEntry {
            method: "GET".into(),
            url: "http://192.0.2.1/two".into(),
            ..Default::default()
        };
        let col = Collection::new("c".into(), vec![e1, e2]);
        let state = Arc::new(Mutex::new(ApiResponse::default()));

        assert!(
            run_all_entries(&col, None, state, false).is_some(),
            "a non-empty collection must start a streaming run"
        );
    }

    #[test]
    fn run_all_entries_rejects_a_body_form_conflict_naming_the_offending_entry() {
        let ok_entry = HurlEntry {
            method: "GET".into(),
            url: "http://192.0.2.1/ok".into(),
            ..Default::default()
        };
        let bad_entry = HurlEntry {
            title: "Bad One".into(),
            method: "POST".into(),
            url: "http://192.0.2.1/bad".into(),
            body_src: Some("{}".into()),
            form_fields: vec![FormField {
                key: "f".into(),
                value: "v".into(),
                enabled: true,
                ..Default::default()
            }],
            ..Default::default()
        };
        let col = Collection::new("c".into(), vec![ok_entry, bad_entry]);
        let state = Arc::new(Mutex::new(ApiResponse::default()));

        let rx = run_all_entries(&col, None, state.clone(), false);

        assert!(rx.is_none(), "must not start a run that can never be built");
        let r = state.lock().unwrap();
        assert!(!r.loading);
        assert!(
            r.error.contains("Body") && r.error.contains("Form") && r.error.contains("Bad One")
        );
    }

    // ── Substitution status classification (colour coding) ────────────────

    #[test]
    fn subst_map_classifies_variables_by_status() {
        use crate::environment::SECRET_MASK;
        let env = env_with(vec![
            EnvVar {
                key: "HOST".into(),
                value: "h".into(),
                source: ValueSource::Literal,
                resolved: true,
                loading: false,
                original_value: "h".into(),
                modified: false,
                user_added: false,
                raw: String::new(),
            },
            EnvVar {
                key: "PORT".into(),
                value: "8080".into(),
                source: ValueSource::ProcessEnv,
                resolved: true,
                loading: false,
                original_value: "8080".into(),
                modified: false,
                user_added: false,
                raw: String::new(),
            },
            secret_var("TOK", false, true),  // loading (pending)
            secret_var("BAD", false, false), // failed
            EnvVar {
                key: "SECRET".into(),
                value: "real".into(),
                source: ValueSource::OnePassword,
                resolved: true,
                loading: false,
                original_value: "real".into(),
                modified: false,
                user_added: false,
                raw: String::new(),
            },
        ]);
        let mut col = Collection::new(
            "c".into(),
            vec![HurlEntry {
                captures: vec![("token".into(), "jsonpath \"$.t\"".into())],
                ..Default::default()
            }],
        );
        col.captures.insert("post_id".into(), "42".into());

        let m = subst_map(&col, Some(&env));
        assert!(matches!(m["HOST"].kind, SubstKind::Literal));
        assert_eq!(
            m["HOST"].shown.as_deref(),
            Some("h"),
            "literal is substituted"
        );
        assert!(matches!(m["PORT"].kind, SubstKind::Loaded));
        assert_eq!(
            m["PORT"].shown.as_deref(),
            Some("8080"),
            "env-var value is substituted"
        );
        assert!(
            matches!(m["TOK"].kind, SubstKind::Pending) && m["TOK"].shown.is_none(),
            "loading secret is pending, kept"
        );
        assert!(
            matches!(m["BAD"].kind, SubstKind::Failed) && m["BAD"].shown.is_none(),
            "failed secret is red, kept"
        );
        assert!(matches!(m["SECRET"].kind, SubstKind::Loaded));
        assert_eq!(
            m["SECRET"].shown.as_deref(),
            Some(SECRET_MASK),
            "a resolved secret is masked, not revealed"
        );
        assert!(
            matches!(m["token"].kind, SubstKind::Failed) && m["token"].shown.is_none(),
            "uninitialised capture is red, kept"
        );
        assert!(matches!(m["post_id"].kind, SubstKind::Loaded));
        assert_eq!(
            m["post_id"].shown.as_deref(),
            Some("42"),
            "an initialised capture is substituted"
        );
    }

    #[test]
    fn subst_display_substitutes_known_and_keeps_unavailable() {
        let env = env_with(vec![
            EnvVar {
                key: "HOST".into(),
                value: "example.test".into(),
                source: ValueSource::Literal,
                resolved: true,
                loading: false,
                original_value: "example.test".into(),
                modified: false,
                user_added: false,
                raw: String::new(),
            },
            secret_var("TOK", false, true), // loading -> kept
        ]);
        let col = Collection::new("c".into(), vec![]);
        let m = subst_map(&col, Some(&env));
        assert_eq!(
            subst_display("{{ HOST }}/{{ TOK }}/{{ NOPE }}", &m),
            "example.test/{{ TOK }}/{{ NOPE }}",
            "known values are substituted; pending and unknown placeholders are kept",
        );
    }

    // --- request-level parameter defaults ---------------------------------

    /// A request that declares a parameter and is opened on its own runs with
    /// the author's sample value — the whole point of the feature: the request
    /// stays usable outside the report that drives it.
    #[test]
    fn a_declared_parameter_supplies_its_default_when_nobody_binds_it() {
        let entry = param_entry("FILE", "./samples/invoice.pdf");
        let vars = HashMap::new();

        let effective = effective_vars_reporting(&entry, &vars).0;

        assert_eq!(
            effective.get("FILE"),
            Some(&"./samples/invoice.pdf".to_string()),
        );
        assert_eq!(
            resolve_entry(&entry, &effective).form_fields[0].value,
            "./samples/invoice.pdf",
            "the default reaches the multipart file field as a real path",
        );
    }

    /// The same request driven from a PaperTrail loop takes the loop's value.
    /// Hurl's own reading of an `[Options] variable:` row is the opposite (it
    /// overwrites the caller), so this is the flip that makes one request serve
    /// both a person and a report.
    #[test]
    fn a_caller_binding_beats_the_declared_default() {
        let entry = param_entry("FILE", "./samples/invoice.pdf");
        let vars = HashMap::from([("FILE".to_string(), "./inbox/real.pdf".to_string())]);

        let effective = effective_vars_reporting(&entry, &vars).0;

        assert_eq!(effective.get("FILE"), Some(&"./inbox/real.pdf".to_string()));
    }

    /// The bound row is removed from the entry that is handed to Hurl, so the
    /// request cannot re-assert its own value in the parts PaperBoy does not
    /// substitute itself (`[Captures]`/`[Asserts]`).
    #[test]
    fn the_run_entry_never_carries_a_variable_option_to_hurl() {
        let entry = param_entry("FILE", "./samples/invoice.pdf");
        let vars = HashMap::from([("FILE".to_string(), "./inbox/real.pdf".to_string())]);

        let mut run_entry = to_run_entry(&entry, resolve_entry(&entry, &vars));
        strip_variable_options(&mut run_entry);

        assert!(
            run_entry.options.iter().all(|r| r.key != "variable"),
            "a variable: row would override the caller inside Hurl",
        );
        assert!(
            run_entry.options.iter().any(|r| r.key == "retry"),
            "behavioural options are untouched",
        );
    }

    /// A whole-collection run hands the file to Hurl unresolved, so only the
    /// *bound* defaults are removed — the rest stay in for Hurl to apply, which
    /// is what a default should do.
    #[test]
    fn a_whole_file_run_strips_only_the_defaults_the_caller_bound() {
        let mut entries = vec![param_entry("FILE", "./samples/invoice.pdf")];
        entries[0]
            .options
            .push(KvRow::new("variable", "MODE=draft"));
        let vars = HashMap::from([("FILE".to_string(), "./inbox/real.pdf".to_string())]);

        assert!(strip_bound_variable_options(&mut entries, &vars));

        let rows: Vec<&str> = entries[0]
            .options
            .iter()
            .map(|r| r.value.as_str())
            .collect();
        assert_eq!(
            rows,
            vec!["3", "MODE=draft"],
            "the bound FILE default is gone; the unbound MODE default survives",
        );
    }

    /// Nothing to strip means nothing to re-serialize: the CLI runs a plain
    /// `.hurl` file verbatim, and round-tripping one nobody parameterised would
    /// be churn for its own sake.
    #[test]
    fn a_whole_file_run_reports_when_it_changed_nothing() {
        let mut entries = vec![param_entry("FILE", "./samples/invoice.pdf")];

        assert!(!strip_bound_variable_options(&mut entries, &HashMap::new()));
        assert_eq!(entries[0].options.len(), 2);
    }

    /// One parameter may be written in terms of another, and in terms of a
    /// caller-supplied variable — defaults are applied in written order so the
    /// composition is predictable rather than hash-order luck.
    #[test]
    fn a_default_may_reference_another_variable() {
        let mut entry = param_entry("SAMPLES", "{{ROOT}}/samples");
        entry
            .options
            .push(KvRow::new("variable", "DOC={{SAMPLES}}/invoice.pdf"));
        let vars = HashMap::from([("ROOT".to_string(), "/srv".to_string())]);

        let effective = effective_vars_reporting(&entry, &vars).0;

        assert_eq!(effective.get("SAMPLES"), Some(&"/srv/samples".to_string()));
        assert_eq!(
            effective.get("DOC"),
            Some(&"/srv/samples/invoice.pdf".to_string()),
        );
    }

    /// `[Options]` is a free-text grid the user may be mid-way through typing.
    /// A half-written row is ignored, never an error that refuses the send.
    #[test]
    fn malformed_and_disabled_parameter_rows_are_ignored() {
        let mut entry = param_entry("FILE", "./samples/invoice.pdf");
        entry.options = vec![
            KvRow::new("variable", "no-equals-sign"),
            KvRow::new("variable", "=novalue"),
            KvRow::new("variable", "SPACED NAME=x"),
            KvRow::toggled("variable", "OFF=x", false),
            KvRow::new("VARIABLE", "SHOUTED=y"),
        ];

        assert_eq!(
            entry.variable_defaults(),
            vec![("SHOUTED".to_string(), "y".to_string())],
            "only the well-formed enabled row counts, and the option name is \
             matched case-insensitively",
        );
    }

    /// A request that declares nothing is handed the caller's own map, not a
    /// clone of it — a send is hot enough that the common case should allocate
    /// nothing.
    #[test]
    fn a_request_without_parameters_borrows_the_callers_variables() {
        let entry = me_entry();
        let vars = HashMap::from([("TOKEN".to_string(), "abc".to_string())]);

        assert!(matches!(
            effective_vars_reporting(&entry, &vars).0,
            std::borrow::Cow::Borrowed(_)
        ));
    }

    /// A request declaring `FILE` for a `[Multipart]` file field — the case the
    /// feature exists for.
    fn param_entry(name: &str, default: &str) -> HurlEntry {
        HurlEntry {
            title: "upload_document".into(),
            method: "POST".into(),
            url: "https://example.test/documents".into(),
            options: vec![
                KvRow::new("retry", "3"),
                KvRow::new("variable", format!("{name}={default}")),
            ],
            is_multipart: true,
            form_fields: vec![FormField {
                key: "file".into(),
                value: format!("{{{{{name}}}}}"),
                kind: FormFieldKind::File,
                enabled: true,
                ..Default::default()
            }],
            ..Default::default()
        }
    }

    /// A failed `[Gen]` row leaves its name unbound, so running on would send
    /// `{{sig}}` where a signature belongs. The interactive send refuses; the
    /// report runner used to throw the errors away and send it anyway, which
    /// is the one path with nobody watching.
    #[test]
    fn a_request_whose_computed_value_failed_is_not_sent() {
        let entry = HurlEntry {
            method: "GET".to_string(),
            url: "http://127.0.0.1:9/{{sig}}".to_string(),
            generators: vec![("sig".to_string(), "hmac_sha526(k, m)".to_string())],
            ..Default::default()
        };
        let out = run_resolved_entry(&entry, &HashMap::new(), None, &[]);
        assert!(out.entries.is_empty(), "nothing was sent");
        let error = out.error.unwrap_or_default();
        assert!(
            error.contains("hmac_sha526"),
            "and the reason names the row's fault: {error}"
        );
    }

    /// A pinned clock and "random" source, so a computed value can be asserted.
    struct Fixed;
    impl crate::generators::GenSource for Fixed {
        fn now(&self) -> (i64, u32) {
            (1_700_000_000, 0)
        }
        fn fill_random(&self, buf: &mut [u8]) {
            for (i, b) in buf.iter_mut().enumerate() {
                *b = i as u8;
            }
        }
        fn counter(&self, _name: &str) -> u64 {
            1
        }
    }

    /// A tiny loopback HTTP server that records the request bytes it was sent,
    /// so a test can assert on what actually reached the wire. Returns the bound
    /// port and a handle to the recorded requests.
    fn recording_server(responses: usize) -> (u16, std::sync::Arc<std::sync::Mutex<Vec<String>>>) {
        use std::io::{Read, Write};
        let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("a free port");
        let port = listener.local_addr().unwrap().port();
        let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let record = std::sync::Arc::clone(&seen);
        std::thread::spawn(move || {
            for _ in 0..responses {
                let Ok((mut sock, _)) = listener.accept() else {
                    return;
                };
                let mut buf = [0u8; 8192];
                let n = sock.read(&mut buf).unwrap_or(0);
                record
                    .lock()
                    .unwrap()
                    .push(String::from_utf8_lossy(&buf[..n]).to_string());
                let _ = sock.write_all(
                    b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok",
                );
                let _ = sock.flush();
            }
        });
        (port, seen)
    }

    /// A request whose `# [Gen]` block failed is not sent by a streaming run.
    ///
    /// The failed row leaves its name unbound -- and something else of that
    /// name is then used in its place: an environment value, a capture from an
    /// earlier request. The request goes out looking perfectly well-formed,
    /// signed with the wrong thing, and is answered 200. That is the worst
    /// shape a fault can take in an API client: the run *passes*. A single
    /// send has always refused; the whole-collection run reported the error
    /// and sent it anyway.
    #[test]
    fn a_streaming_run_does_not_send_a_request_whose_block_failed() {
        let (port, seen) = recording_server(1);
        let mut e = HurlEntry::from_fields(
            "Signed",
            "GET",
            &format!("http://127.0.0.1:{port}/orders"),
            vec![KvRow::new("X-Sig", "{{sig}}")],
            "",
        );
        e.generators = vec![("sig".to_string(), "no_such_function()".to_string())];
        let entries = vec![e];
        let content = collection_to_hurl(&entries);
        // The environment binds the very name the block failed to compute,
        // which is what used to go out.
        let vars = HashMap::from([("sig".to_string(), "from-the-environment".to_string())]);
        let out = crate::hurl::run::run_hurl_streaming_with(
            &content,
            &vars,
            None,
            move |i, known| {
                let entry = &entries[i];
                let mut merged = known.clone();
                let errs = crate::generators::expand(
                    &entry.generators,
                    &mut merged,
                    &crate::generators::SystemSource::new(),
                );
                if !errs.is_empty() {
                    return crate::hurl::EntrySetup::Skip {
                        reason: "block failed".to_string(),
                    };
                }
                crate::hurl::EntrySetup::Bind(
                    entry
                        .generators
                        .iter()
                        .filter_map(|(n, _)| merged.get(n).map(|v| (n.clone(), v.clone())))
                        .collect(),
                )
            },
            |_| {},
        );

        assert!(
            seen.lock().unwrap().is_empty(),
            "nothing may reach the wire: {:?}",
            seen.lock().unwrap()
        );
        assert_eq!(out.entries.len(), 1, "the entry is still accounted for");
        assert!(!out.entries[0].ok, "and it is a failure, not a pass");
        assert_eq!(out.entries[0].entry_index, 0, "credited to its own request");
        assert!(
            out.entries[0].url.contains("/orders"),
            "and says which request it was: {:?}",
            out.entries[0].url
        );
    }

    /// The same, for a batch run. Batch is one Hurl call over the whole file,
    /// so it cannot skip the one request -- it refuses the run instead, which
    /// is still better than sending a signature computed from nothing.
    #[test]
    fn a_batch_run_is_refused_when_a_block_failed() {
        let mut e = HurlEntry::from_fields(
            "Signed",
            "GET",
            "http://127.0.0.1:1/orders",
            vec![KvRow::new("X-Sig", "{{sig}}")],
            "",
        );
        e.generators = vec![("sig".to_string(), "no_such_function()".to_string())];
        let vars = HashMap::from([("sig".to_string(), "from-the-environment".to_string())]);
        let blocks = expand_batch_generators(&[e], &vars, &Fixed);
        assert_eq!(blocks.errors.len(), 1, "the failure is reported");
        assert!(
            !blocks.bound.contains_key("sig"),
            "and nothing is bound for the row that failed"
        );
    }

    /// A computed value must never reach the request preview: a generator name
    /// keeps its `{{braces}}` in the *computed* colour, even after the block's
    /// last result has been merged into `Collection::captures`. Otherwise an
    /// HMAC-of-a-secret would be shown in plaintext, and the preview would show
    /// the previous send's value while the next send computes a fresh one.
    #[test]
    fn a_computed_value_never_reaches_the_preview() {
        let mut col = Collection::new("c".to_string(), vec![]);
        let mut e = HurlEntry::from_fields(
            "Signed",
            "GET",
            "http://h/a",
            vec![KvRow::new("Authorization", "{{sig}}")],
            "",
        );
        e.generators = vec![(
            "sig".to_string(),
            r#"hmac_sha256(API_SECRET, "canonical")"#.to_string(),
        )];
        col.entries.push(e);

        // What a completed send leaves behind: the block's result folded into
        // the captures.
        col.captures.insert(
            "sig".to_string(),
            "9f8e7d6c5b4a-hmac-of-a-secret".to_string(),
        );

        let map = subst_map(&col, None);
        assert_eq!(
            map["sig"].kind,
            SubstKind::Computed,
            "a name the block defines stays Computed even when captured"
        );
        let shown = subst_display("Authorization: {{sig}}", &map);
        assert_eq!(
            shown, "Authorization: {{sig}}",
            "the preview keeps the braces rather than showing the value"
        );
    }

    /// A whole-collection run must let a generator read its own request's
    /// `[Options] variable:` rows, exactly as a single send does — otherwise a
    /// signature over a declared key binds nothing and `{{sig}}` goes out
    /// literally. The streaming path now layers the entry's own defaults before
    /// evaluating the block.
    #[test]
    fn a_generator_sees_its_own_requests_parameters_in_a_run_all() {
        let (port, seen) = recording_server(1);
        let mut e = HurlEntry::from_fields(
            "Signed",
            "GET",
            &format!("http://127.0.0.1:{port}/orders"),
            vec![KvRow::new("X-Sig", "{{sig}}")],
            "",
        );
        e.options = vec![KvRow::new("variable", "SAMPLE_KEY=s3cret")];
        e.generators = vec![(
            "sig".to_string(),
            r#"hmac_sha256(SAMPLE_KEY, "m")"#.to_string(),
        )];

        // What a single send computes, for reference — this one always worked.
        let empty = HashMap::new();
        let (vars, errs) = effective_vars_reporting(&e, &empty);
        assert!(errs.is_empty(), "a single send resolves it: {errs:?}");
        let expected = vars["sig"].clone();

        // What "Run All" / `paperboy -c` do: the block is evaluated in
        // `before_entry`, over what the run has bound at that moment — which now
        // includes the entry's own `[Options] variable:` rows.
        let entries = vec![e];
        let content = collection_to_hurl(&entries);
        let gen_errors: std::rc::Rc<std::cell::RefCell<Vec<crate::generators::GenError>>> =
            std::rc::Rc::default();
        let record_errs = std::rc::Rc::clone(&gen_errors);
        let out = crate::hurl::run::run_hurl_streaming_with(
            &content,
            &HashMap::new(),
            None,
            move |i, known| {
                let entry = &entries[i];
                let mut merged = known.clone();
                record_errs.borrow_mut().extend(crate::generators::expand(
                    &entry.generators,
                    &mut merged,
                    &crate::generators::SystemSource::new(),
                ));
                crate::hurl::EntrySetup::Bind(
                    entry
                        .generators
                        .iter()
                        .filter_map(|(n, _)| merged.get(n).map(|v| (n.clone(), v.clone())))
                        .collect(),
                )
            },
            |_| {},
        );

        let sent = seen.lock().unwrap().join("\n");
        assert!(
            sent.contains(&format!("X-Sig: {expected}")),
            "the run must send the signature a single send would (`{expected}`).\n\
             errors: {:?}\nrun error: {:?}\nwire:\n{sent}",
            gen_errors.borrow(),
            out.error
        );
    }

    /// The pre-flight panel and the run it precedes must agree about a block:
    /// both now apply the request's own `[Options] variable:` rows before
    /// evaluating, so a signature over a declared key is clean in both.
    #[test]
    fn the_run_all_preflight_agrees_with_the_run() {
        let mut e = HurlEntry::from_fields("Signed", "GET", "http://127.0.0.1:1/x", vec![], "");
        e.options = vec![KvRow::new("variable", "SAMPLE_KEY=s3cret")];
        e.generators = vec![(
            "sig".to_string(),
            r#"hmac_sha256(SAMPLE_KEY, "m")"#.to_string(),
        )];
        let col = Collection::new("c".to_string(), vec![e.clone()]);

        // What the panel says before the run.
        let preflight = generator_problems_all(&col, None);
        // What the run's `before_entry` hook now sees: the entry's own defaults
        // layered in first (mirroring the fixed streaming path), then the block.
        let mut merged: HashMap<String, String> = HashMap::new();
        for (name, value) in e.variable_defaults() {
            merged.entry(name).or_insert(value);
        }
        let at_run_time = crate::generators::expand(&e.generators, &mut merged, &Fixed);

        assert_eq!(
            preflight.len(),
            at_run_time.len(),
            "pre-flight: {preflight:?}\nat run time: {at_run_time:?}"
        );
        assert!(preflight.is_empty() && at_run_time.is_empty());
    }

    /// A batch run must not rewrite an environment variable behind the back of a
    /// request that has no `# [Gen]` block. Batch shares one value set, so a
    /// name a *later* request computes would otherwise replace the environment
    /// value for the requests above it too. Batch leaves the environment value
    /// in place and records the name in `shadowed` so the user is told.
    #[test]
    fn batch_does_not_rewrite_an_earlier_requests_variable() {
        let (port, seen) = recording_server(2);
        let first = HurlEntry::from_fields(
            "Reads TOKEN",
            "GET",
            &format!("http://127.0.0.1:{port}/first"),
            vec![KvRow::new("X-Token", "{{TOKEN}}")],
            "",
        );
        let mut second = HurlEntry::from_fields(
            "Computes TOKEN",
            "GET",
            &format!("http://127.0.0.1:{port}/second"),
            vec![KvRow::new("X-Token", "{{TOKEN}}")],
            "",
        );
        second.generators = vec![(
            "TOKEN".to_string(),
            r#""computed-by-request-two""#.to_string(),
        )];

        let entries = vec![first, second];
        let mut vars = HashMap::new();
        vars.insert("TOKEN".to_string(), "from-the-environment".to_string());

        let blocks = expand_batch_generators(&entries, &vars, &Fixed);
        assert_eq!(
            blocks.shadowed,
            vec!["TOKEN".to_string()],
            "the environment binding of TOKEN is reported as shadowed, not rewritten"
        );
        assert!(
            !blocks.bound.contains_key("TOKEN"),
            "the computed value is not bound over the environment for the whole file"
        );
        vars.extend(blocks.bound.clone());
        let content = collection_to_hurl(&entries);
        let _ = crate::hurl::run_hurl(&content, &vars, None);

        let sent = seen.lock().unwrap().join("\n");
        let first_req = sent.split("GET /second").next().unwrap_or("").to_string();
        assert!(
            first_req.contains("X-Token: from-the-environment"),
            "the first request keeps the environment's TOKEN.\nwire:\n{sent}"
        );
    }

    /// A wholly blank generator row is a row still being typed, not a mistake:
    /// [`effective_vars_reporting`] (the send) must ignore it just as the
    /// editor's `check` does, so a half-typed row never blocks the send with a
    /// message that names no row.
    #[test]
    fn a_blank_row_does_not_block_the_send() {
        let mut e = HurlEntry::from_fields("T", "GET", "http://h/a", vec![], "");
        e.generators = vec![
            ("nonce".to_string(), "uuid".to_string()),
            (String::new(), String::new()),
        ];
        let vars = HashMap::new();
        let (_, errors) = effective_vars_reporting(&e, &vars);
        assert!(
            errors.is_empty(),
            "a row the editor is still waiting on must not refuse the send: {errors:?}"
        );
    }

    /// `counter(name)` must count *across* sends, per the README: the counter
    /// state lives in a process-global table, not on the per-send
    /// `SystemSource`, so two sends of the same request draw different values.
    #[test]
    fn a_counter_counts_across_sends() {
        let mut e = HurlEntry::from_fields("T", "GET", "http://h/a", vec![], "");
        e.generators = vec![(
            "page".to_string(),
            r#"counter("request-test-counter")"#.to_string(),
        )];
        let empty = HashMap::new();
        let first = effective_vars_reporting(&e, &empty).0["page"].clone();
        let second = effective_vars_reporting(&e, &empty).0["page"].clone();
        assert_ne!(
            first, second,
            "two sends of the same request drew the same counter value ({first})"
        );
    }

    /// ...and counts each send *once*. The block is evaluated twice per send —
    /// once to find out whether it works, once for real — and the first of
    /// those used to advance the counter too, so a request numbering its pages
    /// went 1, 3, 5.
    #[test]
    fn asking_whether_a_block_works_leaves_its_counter_alone() {
        let mut col = Collection::new(
            "c".into(),
            vec![entry_with_generators(
                "http://h/a",
                &[("page", r#"counter("request-test-precheck")"#)],
            )],
        );
        col.selected_entry = 0;
        let empty = HashMap::new();
        // The pre-flight check every send makes, twice over.
        assert!(generator_problems(&col, None).is_empty());
        assert!(generator_problems(&col, None).is_empty());
        let sent = effective_vars_reporting(&col.entries[0], &empty).0["page"].clone();
        assert_eq!(
            sent, "1",
            "the checks took the send's numbers: it drew {sent} rather than 1"
        );
    }
}