reference-query 0.55.1

Reference Query — find the code you're looking for.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
//! End-to-end: drive the real `rq` binary through index → search.
//!
//! Hermetic and reproducible — no shell `cd`, no git required. Each run uses an
//! isolated `RQ_DB`, a fresh temp repo, and sets the subprocess working
//! directory, so the shell's cwd is irrelevant.

use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

/// Unique temp paths for this test (repo dir + db file), cleaned first.
fn scratch(label: &str) -> (PathBuf, PathBuf) {
    let base = std::env::temp_dir();
    let dir = base.join(format!("rq-e2e-{}-{label}", std::process::id()));
    let db = base.join(format!("rq-e2e-{}-{label}.db", std::process::id()));
    let _ = fs::remove_dir_all(&dir);
    for suffix in ["", "-wal", "-shm"] {
        let _ = fs::remove_file(format!("{}{suffix}", db.display()));
    }
    fs::create_dir_all(&dir).unwrap();
    (dir, db)
}

/// Run the built binary with an isolated db and a set working directory.
/// Detached warming is off so each invocation is hermetic (no child process
/// racing the test's asserts/cleanup); the detach path has its own test.
fn rq(db: &Path, cwd: &Path, args: &[&str]) -> (bool, String) {
    let out = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(args)
        .current_dir(cwd)
        .env("RQ_DB", db)
        .env("RQ_WARM_DETACH", "0")
        .output()
        .expect("run rq");
    (
        out.status.success(),
        String::from_utf8_lossy(&out.stdout).into_owned(),
    )
}

/// Run the binary and hand back stdout *and* stderr — `--profile` reports to
/// stderr so stdout stays exactly the machine-readable result.
fn rq_both(db: &Path, cwd: &Path, args: &[&str]) -> (bool, String, String) {
    let out = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(args)
        .current_dir(cwd)
        .env("RQ_DB", db)
        .env("RQ_WARM_DETACH", "0")
        .output()
        .expect("run rq");
    (
        out.status.success(),
        String::from_utf8_lossy(&out.stdout).into_owned(),
        String::from_utf8_lossy(&out.stderr).into_owned(),
    )
}

fn first_line(s: &str) -> &str {
    s.lines().next().unwrap_or("")
}

/// Run a `-v` search and report whether it decided to warm (traced to stderr).
/// Detach is off so the decision runs in-process: with it on, a hit hands the
/// same check to a child (`rq --warm`), which would race the assert.
fn warmed(db: &Path, cwd: &Path, query: &str) -> bool {
    let run = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["-v", query])
        .current_dir(cwd)
        .env("RQ_DB", db)
        .env("RQ_WARM_DETACH", "0")
        .output()
        .expect("run rq");
    String::from_utf8_lossy(&run.stderr).contains("background warm")
}

/// `git init` a directory (no commits needed) so it reads as a git repo.
fn git_init(dir: &Path) {
    let _ = Command::new("git")
        .arg("init")
        .arg("-q")
        .current_dir(dir)
        .output();
}

/// `git init` + commit everything, so files are *tracked* (warming enumerates a
/// committed repo from `git ls-files`, not a filesystem walk).
fn git_init_commit(dir: &Path) {
    git_init(dir);
    let git = |args: &[&str]| {
        let _ = Command::new("git").args(args).current_dir(dir).output();
    };
    git(&["add", "-A"]);
    git(&[
        "-c",
        "user.email=t@e.st",
        "-c",
        "user.name=test",
        "commit",
        "-qm",
        "init",
    ]);
}

#[test]
fn index_search_and_status_through_the_cli() {
    let (dir, db) = scratch("basic");
    fs::write(dir.join("alpha.rb"), "class HandlerA\nend\n").unwrap();
    fs::write(dir.join("beta.rb"), "class HandlerB\nend\n").unwrap();

    // index the working directory
    let (ok, out) = rq(&db, &dir, &["--index"]);
    assert!(ok, "index failed: {out}");
    assert!(out.contains("symbol"), "index output: {out}");

    // search — the tie breaks alphabetically, so HandlerA leads
    let (ok, out) = rq(&db, &dir, &["handler"]);
    assert!(ok, "search failed: {out}");
    assert!(
        first_line(&out).contains("HandlerA"),
        "search output: {out}"
    );

    // status shows the repo
    let (ok, out) = rq(&db, &dir, &["--status"]);
    assert!(ok, "status failed: {out}");
    assert!(out.contains("local:"), "status output: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_strong_match_suppresses_the_scattered_tail() {
    // when the query lands an exact/prefix name match, fuzzy near-matches are
    // dropped — `employeescontroller` keeps EmployeesController, not the scattered
    // EmployeeStatusController (employee + s + …controller, skipping "tatus").
    let (dir, db) = scratch("gate");
    fs::write(
        dir.join("employees_controller.rb"),
        "class EmployeesController\nend\n",
    )
    .unwrap();
    fs::write(
        dir.join("employee_status_controller.rb"),
        "class EmployeeStatusController\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    let (_, out) = rq(&db, &dir, &["employeescontroller", "--ndjson"]);
    assert!(out.contains("EmployeesController"), "exact kept: {out}");
    assert!(
        !out.contains("EmployeeStatusController"),
        "scattered fuzzy dropped: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_wildcard_bridges_an_explicit_gap() {
    // `*` reaches across words the fuzzy matcher deliberately won't skip:
    // `widget*controller` finds WidgetAlphaBravoController, where the plain
    // `widgetcontroller` query is rejected (it would have to skip whole words).
    // An unrelated file stays out.
    let (dir, db) = scratch("wildcard");
    fs::write(
        dir.join("widget_alpha_bravo_controller.rb"),
        "class WidgetAlphaBravoController\nend\n",
    )
    .unwrap();
    fs::write(dir.join("gadget_service.rb"), "class GadgetService\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["widget*controller", "--ndjson"]);
    assert!(ok, "wildcard search should match: {out}");
    assert!(
        out.contains("WidgetAlphaBravoController"),
        "star bridges the gap: {out}"
    );
    assert!(!out.contains("GadgetService"), "non-match excluded: {out}");

    // the same query without the star is too scattered for the fuzzy matcher
    let (matched, _) = rq(&db, &dir, &["widgetcontroller"]);
    assert!(!matched, "plain fuzzy won't skip whole words");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn cold_index_builds_a_working_fuzzy_index() {
    // a cold `--index` suspends per-row index upkeep and builds in bulk at the
    // end; a mid-word substring (not a prefix of the name) only resolves
    // through fuzzy recall, so this proves the bulk pass produced a usable index
    let (dir, db) = scratch("coldindex");
    fs::write(dir.join("a.rb"), "class AlphaWidgetController\nend\n").unwrap();
    let (ok, out) = rq(&db, &dir, &["--index"]);
    assert!(ok, "index failed: {out}");

    // "widget" is mid-word in AlphaWidgetController — exact/prefix can't reach it
    let (ok, out) = rq(&db, &dir, &["widget"]);
    assert!(ok, "fuzzy recall should find it: {out}");
    assert!(out.contains("AlphaWidgetController"), "fuzzy recall: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn the_name_index_answers_fuzzy_queries_and_keeps_up_with_edits() {
    // fuzzy recall reads the name index: built at the end of a cold index,
    // then appended to as files are written
    let (dir, db) = scratch("nameindex");
    fs::write(dir.join("a.rb"), "class AlphaWidgetController\nend\n").unwrap();
    fs::write(dir.join("connection_pool.rb"), "module Base\nend\n").unwrap();
    let (ok, out) = rq(&db, &dir, &["--index"]);
    assert!(ok, "index failed: {out}");
    let scan = |query: &str| {
        let out = Command::new(env!("CARGO_BIN_EXE_rq"))
            .args([query, "--json"])
            .current_dir(&dir)
            .env("RQ_DB", &db)
            .env("RQ_WARM_DETACH", "0")
            .output()
            .expect("run rq");
        String::from_utf8_lossy(&out.stdout).into_owned()
    };
    // an abbreviation, not a substring of the name
    assert!(scan("wdgctl").contains("AlphaWidgetController"));
    // a module named only by its file
    assert!(scan("conpool").contains("\"Base\""));

    fs::write(dir.join("b.rb"), "class BetaGadgetFactory\nend\n").unwrap();
    let (ok, out) = rq(&db, &dir, &["--index"]);
    assert!(ok, "reindex failed: {out}");
    assert!(
        scan("gdgfac").contains("BetaGadgetFactory"),
        "an appended name"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_search_that_warms_a_cold_repo_leaves_a_working_fuzzy_index() {
    // the same, when the first contact is a search rather than `--index`: the
    // budgeted warm it runs must leave the name index usable, both for this
    // answer and for the next query
    let (dir, db) = scratch("coldwarm");
    fs::write(dir.join("a.rb"), "class AlphaWidgetController\nend\n").unwrap();
    fs::write(dir.join("b.rb"), "class BetaGadget\nend\n").unwrap();
    git_init_commit(&dir);

    let (ok, out) = rq(&db, &dir, &["widget"]);
    assert!(ok && out.contains("AlphaWidgetController"), "warm: {out}");
    let (ok, out) = rq(&db, &dir, &["gadget"]);
    assert!(ok && out.contains("BetaGadget"), "next query: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_compact_namespaced_class_is_found_by_its_leaf_name() {
    // `class A::B::EmployeesController` must be found by `employeescontroller`
    // (its leaf), and must survive next to a top-level EmployeesController — the
    // relevance gate shouldn't prune a legitimate exact-leaf match
    let (dir, db) = scratch("namespaced");
    fs::write(
        dir.join("a.rb"),
        "class My::Module::EmployeesController\n  def index; end\nend\n",
    )
    .unwrap();
    fs::write(
        dir.join("b.rb"),
        "class EmployeesController\n  def show; end\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["employeescontroller", "--ndjson"]);
    assert!(ok, "search failed: {out}");
    // both files surface — the namespaced one isn't pruned
    assert!(out.contains("a.rb"), "namespaced class kept: {out}");
    assert!(out.contains("b.rb"), "top-level class kept: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn foo_dot_new_finds_the_constructor() {
    // `Widget.new` runs `initialize` (Ruby) / `__init__` (Python) — the name
    // the user typed is not the name the definition carries.
    let (dir, db) = scratch("constructor");
    fs::write(
        dir.join("a.rb"),
        "class Widget\n  def initialize; end\n  def self.build; end\nend\nclass Other\n  def initialize; end\nend\n",
    )
    .unwrap();
    fs::write(
        dir.join("b.py"),
        "class Gadget:\n    def __init__(self):\n        pass\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["Widget.new", "--ndjson"]);
    assert!(ok, "Widget.new should resolve: {out}");
    assert!(out.contains("\"initialize\""), "finds initialize: {out}");
    assert!(out.contains("\"line\":2"), "the one in Widget: {out}");
    assert!(!out.contains("\"line\":6"), "not Other's: {out}");

    let (ok, out) = rq(&db, &dir, &["Gadget.new", "--ndjson"]);
    assert!(ok && out.contains("__init__"), "python too: {out}");

    // `.` is a scope separator generally, not only for `new`
    let (ok, out) = rq(&db, &dir, &["Widget.build", "--ndjson"]);
    assert!(ok && out.contains("\"build\""), "class method: {out}");

    // a slip in the scope recovers on the typo retry, flagged as a guess
    let (ok, out) = rq(&db, &dir, &["Widgit.new", "--ndjson"]);
    assert!(
        ok && out.contains("\"line\":2"),
        "scope typo recovers: {out}"
    );
    assert!(!out.contains("\"confidence\":1.0"), "not certain: {out}");

    // no scope answers `Widget.Builder`, so `.` falls back to a one-char wildcard
    fs::write(dir.join("c.rb"), "class Widget2Builder\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);
    let (ok, out) = rq(&db, &dir, &["Widget.Builder", "--ndjson"]);
    assert!(
        ok && out.contains("Widget2Builder"),
        "wildcard fallback: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn foo_dot_new_without_its_own_constructor_finds_the_class() {
    // Widget inherits `initialize`; rq doesn't track inheritance, so the class
    // is the answer — never a similarly named class's constructor.
    let (dir, db) = scratch("inherited-constructor");
    fs::write(
        dir.join("a.rb"),
        "class Base\n  def initialize; end\nend\nclass Widget < Base\n  def run; end\nend\nclass Widgey\n  def initialize; end\nend\n",
    )
    .unwrap();
    fs::write(
        dir.join("b.py"),
        "class Gadget(Base):\n    def run(self):\n        pass\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["Widget.new", "--ndjson"]);
    assert!(ok, "Widget.new should resolve: {out}");
    assert_eq!(out.lines().count(), 1, "just the class: {out}");
    assert!(
        out.contains("\"class\"") && out.contains("\"line\":4"),
        "Widget: {out}"
    );
    assert!(out.contains("constructor_owner"), "flagged: {out}");
    assert!(out.contains("\"confidence\":0.75"), "not certain: {out}");

    let (ok, out) = rq(&db, &dir, &["Gadget.new", "--ndjson"]);
    assert!(ok && out.contains("\"Gadget\""), "python too: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_qualified_query_resolves_to_the_method_in_the_named_scope() {
    // `Foo::Bar#baz` must find the `baz` defined inside `Foo::Bar` and, since a
    // scope match exists, suppress the same-named `baz` in another scope.
    let (dir, db) = scratch("qualified");
    fs::write(
        dir.join("a.rb"),
        "module Foo\n  class Bar\n    def baz; end\n  end\nend\n",
    )
    .unwrap();
    fs::write(
        dir.join("b.rb"),
        "module Other\n  class Bar\n    def baz; end\n  end\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["Foo::Bar#baz", "--ndjson"]);
    assert!(ok, "search failed: {out}");
    assert!(out.contains("a.rb"), "in-scope baz surfaces: {out}");
    assert!(
        !out.contains("b.rb"),
        "out-of-scope baz is gated out: {out}"
    );

    // A scope nothing lives in is a miss, not a fallback to every candidate.
    // It used to fall back, which made a made-up owner indistinguishable from
    // the real one whenever the leaf name was unique.
    let (ok, out) = rq(&db, &dir, &["Nope::Bar#baz", "--ndjson"]);
    assert!(!ok, "an unmatched scope must not succeed: {out}");
    assert!(out.contains("scope_not_found"), "and says why: {out}");
    // reported as "not there, but here" — the useful half of the answer
    assert!(out.contains("found_in"), "names where it does live: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn json_results_carry_the_definition_span() {
    // a result reports line..=end_line so a caller can read the exact span
    let (dir, db) = scratch("endline");
    fs::write(
        dir.join("a.rb"),
        "class Widget\n  def go\n    1\n  end\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["Widget", "-l", "1", "--ndjson"]);
    assert!(ok, "search failed: {out}");
    // class Widget spans line 1 through its `end` on line 5
    assert!(out.contains("\"line\":1"), "start line present: {out}");
    assert!(out.contains("\"end_line\":5"), "end line present: {out}");
    // absent optionals are omitted, not null — same shape as --symbols
    assert!(
        !out.contains("\"parent\":null"),
        "no null fields in hit JSON: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn show_prints_the_body_when_confident_and_lists_when_not() {
    let (dir, db) = scratch("show");
    fs::write(
        dir.join("a.rb"),
        "class Widget\n  def go\n    42\n  end\nend\n",
    )
    .unwrap();
    // two same-named classes so a bare fuzzy query is ambiguous
    fs::write(dir.join("b.rb"), "class Thing\nend\nclass Thang\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // confident (exact, dominant): --show prints the full source span as `body`
    let (ok, out) = rq(&db, &dir, &["--show", "Widget", "-j"]);
    assert!(ok, "show failed: {out}");
    assert!(
        out.contains("\"body\""),
        "confident show carries body: {out}"
    );
    assert!(
        out.contains("def go"),
        "body has the definition source: {out}"
    );
    assert!(!out.contains("\"score\""), "score dropped from JSON: {out}");
    assert!(out.contains("\"confidence\""), "confidence present: {out}");

    // ambiguous (two fuzzy `Th*` matches): no body, falls back to the list
    let (_ok, out) = rq(&db, &dir, &["--show", "Th", "-j"]);
    assert!(
        !out.contains("\"body\""),
        "ambiguous show prints no body: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_leading_kind_keyword_filters_like_dash_k() {
    // `rq class Widget` and `rq method go` scope by kind without needing -k, and
    // without quoting. A same-named symbol of another kind is filtered out.
    let (dir, db) = scratch("kindkw");
    fs::write(
        dir.join("a.rb"),
        "SIZE = 5\nclass Widget\n  def go; end\nend\nmodule Widget\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    // unquoted keyword restricts to the class, dropping the module of the same name
    let (ok, out) = rq(&db, &dir, &["class", "Widget", "--ndjson"]);
    assert!(ok, "keyword search failed: {out}");
    assert!(out.contains("\"kind\":\"class\""), "class kept: {out}");
    assert!(
        !out.contains("\"kind\":\"module\""),
        "module filtered: {out}"
    );

    // the method keyword finds the def; equivalent to -k method
    let (ok, out) = rq(&db, &dir, &["method", "go", "--ndjson"]);
    assert!(ok, "method keyword failed: {out}");
    assert!(out.contains("\"kind\":\"method\""), "method found: {out}");

    // constants are indexed and reachable via the keyword form too
    let (ok, out) = rq(&db, &dir, &["constant", "SIZE", "--ndjson"]);
    assert!(ok, "constant keyword failed: {out}");
    assert!(
        out.contains("\"kind\":\"constant\""),
        "constant found: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_search_does_not_leak_another_indexed_repo() {
    // two repos in one index; a query from inside repo A must never surface repo
    // B's definitions — the reported leak. `--all-repos` opts back into both.
    let (dir_a, db) = scratch("repo-a");
    let dir_b = dir_a.with_file_name(format!("rq-e2e-{}-repo-b", std::process::id()));
    let _ = fs::remove_dir_all(&dir_b);
    fs::create_dir_all(&dir_b).unwrap();
    fs::write(dir_a.join("a.rb"), "class Alpha\nend\n").unwrap();
    fs::write(dir_b.join("b.rb"), "class Gadget\nend\n").unwrap();
    rq(&db, &dir_a, &["--index"]);
    rq(&db, &dir_b, &["--index"]);

    // Gadget lives only in repo B; from repo A it's a definitive miss, not B's hit
    let (ok, out) = rq(&db, &dir_a, &["Gadget", "--ndjson"]);
    assert!(!ok, "no Gadget in repo A — should miss");
    assert!(!out.contains("b.rb"), "must not leak repo B: {out}");
    assert!(
        out.contains("\"status\":\"no_match\""),
        "reports no_match: {out}"
    );

    // --all-repos opts into the cross-repo search and finds it
    let (ok, out) = rq(&db, &dir_a, &["Gadget", "--all-repos", "--ndjson"]);
    assert!(ok, "--all-repos should find Gadget in repo B: {out}");
    assert!(out.contains("b.rb"), "cross-repo hit surfaces: {out}");

    // -a is the short form of the same flag
    let (ok, out) = rq(&db, &dir_a, &["Gadget", "-a", "--ndjson"]);
    assert!(ok, "-a should find Gadget in repo B: {out}");
    assert!(out.contains("b.rb"), "cross-repo hit surfaces: {out}");

    let _ = fs::remove_dir_all(&dir_a);
    let _ = fs::remove_dir_all(&dir_b);
}

#[test]
fn a_repo_with_no_commits_is_a_repo_like_any_other() {
    // An unborn HEAD (`git init`, files not yet committed) must not read as
    // "no repo": its first query is scoped to it, not to every indexed repo,
    // and once indexed it settles like a committed repo instead of reindexing
    // (and calling every miss provisional) on each search.
    let (dir_a, db) = scratch("unborn-a");
    let dir_b = dir_a.with_file_name(format!("rq-e2e-{}-unborn-b", std::process::id()));
    let _ = fs::remove_dir_all(&dir_b);
    fs::create_dir_all(&dir_b).unwrap();
    fs::write(dir_b.join("b.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir_b);
    rq(&db, &dir_b, &["--index"]);
    fs::write(dir_a.join("a.rb"), "class Gadget\nend\n").unwrap();
    git_init(&dir_a);

    let (ok, out) = rq(&db, &dir_a, &["Widget", "--json"]);
    assert!(!ok, "no Widget here — the first query must miss: {out}");
    assert!(!out.contains("b.rb"), "must not leak the other repo: {out}");

    let (ok, out) = rq(&db, &dir_a, &["Gadget", "--json"]);
    assert!(ok, "its own untracked file is indexed: {out}");
    let root = dir_a.canonicalize().unwrap();
    assert!(out.contains(&*root.to_string_lossy()), "{out}");
    assert!(
        !warmed(&db, &dir_a, "gadget"),
        "an indexed, unchanged unborn repo does not re-warm"
    );

    let _ = fs::remove_dir_all(&dir_a);
    let _ = fs::remove_dir_all(&dir_b);
}

#[test]
fn structured_results_name_the_checkout_root_their_file_is_relative_to() {
    // Each result carries its own `root`: `--all-repos` spans checkouts, so a
    // caller can't assume the cwd's repo is the one `file` is relative to.
    let (dir_a, db) = scratch("root-a");
    let dir_b = dir_a.with_file_name(format!("rq-e2e-{}-root-b", std::process::id()));
    let _ = fs::remove_dir_all(&dir_b);
    fs::create_dir_all(dir_b.join("lib")).unwrap();
    fs::write(dir_a.join("a.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir_b.join("lib/b.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir_a);
    git_init_commit(&dir_b);
    rq(&db, &dir_a, &["--index"]);
    rq(&db, &dir_b, &["--index"]);
    let root_of = |d: &Path| d.canonicalize().unwrap().to_string_lossy().into_owned();
    let expect = |file: &str| {
        if file == "a.rb" {
            root_of(&dir_a)
        } else {
            root_of(&dir_b)
        }
    };

    let (ok, out) = rq(&db, &dir_a, &["Widget", "--all-repos", "--json"]);
    assert!(ok, "hit: {out}");
    let rows: Vec<serde_json::Value> = serde_json::from_str(&out).expect("json array");
    assert_eq!(rows.len(), 2, "both repos: {out}");
    for row in &rows {
        let file = row["file"].as_str().unwrap();
        assert_eq!(row["root"].as_str(), Some(expect(file).as_str()), "{out}");
    }

    // ndjson carries the same field, from a subdirectory of the other repo too
    let (ok, out) = rq(&db, &dir_b.join("lib"), &["Widget", "-a", "--ndjson"]);
    assert!(ok, "hit: {out}");
    for line in out.lines() {
        let row: serde_json::Value = serde_json::from_str(line).expect("ndjson line");
        let file = row["file"].as_str().unwrap();
        assert_eq!(row["root"].as_str(), Some(expect(file).as_str()), "{out}");
    }

    // an outline names its root as well
    let (ok, out) = rq(&db, &dir_b.join("lib"), &["--symbols", "b.rb", "--ndjson"]);
    assert!(ok, "outline: {out}");
    let row: serde_json::Value = serde_json::from_str(first_line(&out)).expect("ndjson");
    assert_eq!(row["file"], "lib/b.rb");
    assert_eq!(row["root"].as_str(), Some(root_of(&dir_b).as_str()));

    let _ = fs::remove_dir_all(&dir_a);
    let _ = fs::remove_dir_all(&dir_b);
}

#[test]
fn another_repos_exact_match_does_not_hide_a_fuzzy_one_here() {
    // Repo B defines `wdgt` exactly; repo A only has `Widget`, an abbreviation
    // match. Searching from A must still find Widget — B's exact hit is out of
    // scope, so it can't be what lets recall skip the fuzzy layers.
    let (dir_a, db) = scratch("fuzzy-a");
    let dir_b = dir_a.with_file_name(format!("rq-e2e-{}-fuzzy-b", std::process::id()));
    let _ = fs::remove_dir_all(&dir_b);
    fs::create_dir_all(&dir_b).unwrap();
    fs::write(dir_a.join("a.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir_b.join("b.rb"), "def wdgt\nend\n").unwrap();
    rq(&db, &dir_a, &["--index"]);
    rq(&db, &dir_b, &["--index"]);

    let (ok, out) = rq(&db, &dir_a, &["wdgt", "--ndjson"]);
    assert!(ok, "Widget in repo A should answer `wdgt`: {out}");
    assert!(out.contains("a.rb"), "finds this repo's Widget: {out}");
    assert!(!out.contains("b.rb"), "must not leak repo B: {out}");

    let _ = fs::remove_dir_all(&dir_a);
    let _ = fs::remove_dir_all(&dir_b);
}

#[test]
fn a_clean_complete_repo_does_not_re_warm_on_search() {
    // a fully-indexed, clean git repo is provably unchanged (HEAD matches, no
    // dirty files), so a search must skip the background warm entirely rather
    // than re-walk the whole tree per query — at any size
    let (dir, db) = scratch("nowarm");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);

    assert!(
        !warmed(&db, &dir, "widget"),
        "clean complete repo should not re-warm"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_tracked_edit_warms_but_a_new_untracked_file_does_not() {
    // the dirty check skips the untracked-file scan for speed: a tracked edit
    // still triggers a warm (so the change is picked up), but a brand-new
    // untracked file is the accepted tradeoff — not seen until committed/indexed
    // The edit is to a file the search doesn't hit: a top hit is revalidated
    // before the check runs, so its own edit is already indexed by then.
    let (dir, db) = scratch("dirty-check");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir.join("c.rb"), "class Gizmo\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);

    fs::write(dir.join("c.rb"), "class Gizmo\n  def go; end\nend\n").unwrap();
    assert!(warmed(&db, &dir, "widget"), "tracked edit triggers a warm");

    // restore the tracked file to its committed content (tree clean again) —
    // itself a change the index has to take in — then add an untracked file,
    // which the cheaper check intentionally ignores
    fs::write(dir.join("c.rb"), "class Gizmo\nend\n").unwrap();
    assert!(warmed(&db, &dir, "widget"), "the restore is reindexed");
    fs::write(dir.join("b.rb"), "class Gadget\nend\n").unwrap();
    assert!(
        !warmed(&db, &dir, "widget"),
        "a new untracked file does not trigger a warm (accepted tradeoff)"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn two_clones_of_one_repo_each_read_their_own_files() {
    // Two checkouts share one identity (same remote), so one set of rows. A
    // search from either must revalidate and read source from *that* checkout
    // — not whichever was recorded first or last.
    let (dir_a, db) = scratch("clone-a");
    let dir_b = dir_a.with_file_name(format!("rq-e2e-{}-clone-b", std::process::id()));
    let _ = fs::remove_dir_all(&dir_b);
    fs::create_dir_all(&dir_b).unwrap();
    for (dir, tag) in [(&dir_a, "a"), (&dir_b, "b")] {
        fs::write(dir.join("w.rb"), format!("class Widget # {tag}\nend\n")).unwrap();
        git_init_commit(dir);
        let _ = Command::new("git")
            .args([
                "remote",
                "add",
                "origin",
                "https://github.com/acme/widgets.git",
            ])
            .current_dir(dir)
            .output();
    }
    rq(&db, &dir_a, &["--index"]);
    rq(&db, &dir_b, &["--index"]);

    for (dir, tag) in [(&dir_a, "a"), (&dir_b, "b"), (&dir_a, "a")] {
        let (ok, out) = rq(&db, dir, &["Widget", "--ndjson"]);
        assert!(ok, "hit: {out}");
        assert!(
            out.contains(&format!("\"signature\":\"class Widget # {tag}\"")),
            "clone {tag} reads its own source: {out}"
        );
    }

    let _ = fs::remove_dir_all(&dir_a);
    let _ = fs::remove_dir_all(&dir_b);
}

#[test]
fn a_hit_leaves_the_worktree_check_to_the_warm_child() {
    // A hit on a complete repo must not wait on `git status`: it hands the
    // check to `rq --warm`, which bows out when nothing moved and sweeps when
    // something did.
    let (dir, db) = scratch("warm-child");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir.join("c.rb"), "class Gizmo\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);

    let warm = |dir: &Path| {
        let out = Command::new(env!("CARGO_BIN_EXE_rq"))
            .args(["-v", "--warm"])
            .current_dir(dir)
            .env("RQ_DB", &db)
            .output()
            .expect("run rq --warm");
        String::from_utf8_lossy(&out.stderr).into_owned()
    };
    let idle = warm(&dir);
    assert!(idle.contains("nothing to do"), "clean: {idle}");
    assert!(
        !idle.contains("(budget"),
        "clean repo must not sweep: {idle}"
    );

    fs::write(dir.join("c.rb"), "class Gizmo\n  def go; end\nend\n").unwrap();
    let busy = warm(&dir);
    assert!(busy.contains("(budget"), "an edit is swept: {busy}");

    // last: the child this hit spawns holds the single-flight lock a while
    let hit = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["Widget", "--profile", "--json"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .output()
        .expect("run rq");
    let profile = String::from_utf8_lossy(&hit.stderr);
    assert!(hit.status.success(), "hit: {profile}");
    assert!(
        !profile.contains("worktree changed?") && !profile.contains("staleness"),
        "a hit must not run the check itself: {profile}"
    );

    let _ = fs::remove_dir_all(&dir);
}

/// Run a `-v` hit with detach on (and optional extra env), and report whether
/// it spawned the detached warm child.
fn spawned_warm(db: &Path, cwd: &Path, env: &[(&str, &str)]) -> bool {
    let out = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["-v", "Widget"])
        .current_dir(cwd)
        .env("RQ_DB", db)
        .env("RQ_WARM_DETACH", "1")
        .envs(env.iter().copied())
        .output()
        .expect("run rq");
    assert!(out.status.success(), "a hit");
    String::from_utf8_lossy(&out.stderr).contains("background warm (detached)")
}

/// Run `rq --warm` to completion, as the detached child would.
fn warm_now(db: &Path, cwd: &Path) {
    Command::new(env!("CARGO_BIN_EXE_rq"))
        .arg("--warm")
        .current_dir(cwd)
        .env("RQ_DB", db)
        .output()
        .expect("run rq --warm");
}

#[test]
fn a_hit_skips_the_warm_spawn_once_a_warm_found_nothing_moved() {
    let (dir, db) = scratch("warm-verified");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);

    warm_now(&db, &dir);
    assert!(!spawned_warm(&db, &dir, &[]), "verified clean: no spawn");
    assert!(!spawned_warm(&db, &dir, &[]), "and again, back to back");

    // last: the child this hit spawns may outlive the test
    assert!(
        spawned_warm(&db, &dir, &[("RQ_WARM_RECHECK_MS", "0")]),
        "an expired verdict spawns"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn staging_or_committing_voids_the_verdict_but_a_bare_edit_waits() {
    let (dir, db) = scratch("warm-moved");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir.join("c.rb"), "class Gizmo\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);
    warm_now(&db, &dir);

    // An unstaged edit touches nothing in .git, so only the window catches it.
    fs::write(dir.join("c.rb"), "class Gizmo\n  def go; end\nend\n").unwrap();
    assert!(
        !spawned_warm(&db, &dir, &[]),
        "a bare edit waits out the recheck window"
    );

    let _ = Command::new("git")
        .args(["add", "c.rb"])
        .current_dir(&dir)
        .output();
    assert!(spawned_warm(&db, &dir, &[]), "staging spawns");

    git_init_commit(&dir);
    assert!(spawned_warm(&db, &dir, &[]), "a commit spawns");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_dirty_tree_whose_edits_are_indexed_reads_as_unchanged() {
    // Dirty is not the same as stale: once the edit has been indexed, a search
    // must neither re-warm on every query nor call a miss "still warming".
    let (dir, db) = scratch("dirty-indexed");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir.join("c.rb"), "class Gizmo\nend\n").unwrap();
    fs::write(dir.join("README.md"), "docs\n").unwrap();
    git_init_commit(&dir);
    fs::write(dir.join("c.rb"), "class Gizmo\n  def go; end\nend\n").unwrap();
    fs::write(dir.join("README.md"), "edited docs\n").unwrap();
    rq(&db, &dir, &["--index"]);

    assert!(
        !warmed(&db, &dir, "widget"),
        "an indexed edit (and a non-source one) must not re-warm"
    );
    let miss = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["Nonexistent"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .output()
        .expect("run rq");
    assert_eq!(
        miss.status.code(),
        Some(1),
        "a definitive miss, not warming"
    );

    // a further edit is still seen
    fs::write(dir.join("c.rb"), "class Gizmo\n  def stop; end\nend\n").unwrap();
    assert!(warmed(&db, &dir, "widget"), "a new edit still warms");

    let _ = fs::remove_dir_all(&dir);
}

fn git_checkout_file(dir: &Path, file: &str) {
    let _ = Command::new("git")
        .args(["checkout", "--", file])
        .current_dir(dir)
        .output();
}

/// Symbols the index holds for the repo, per `--status`.
fn indexed_symbols(db: &Path, dir: &Path) -> i64 {
    let (_, out) = rq(db, dir, &["--status", "--json"]);
    let rows: Vec<serde_json::Value> = serde_json::from_str(&out).expect("status json");
    rows[0]["symbols"].as_i64().expect("symbols")
}

#[test]
fn a_discarded_edit_is_reindexed() {
    // `git checkout -- f` leaves the tree clean, so `git status` no longer
    // names f — yet the index still holds the edit. The staleness check has to
    // remember which files it took in as edits, or the edit's symbols linger
    // until something else happens to reindex f.
    let (dir, db) = scratch("discarded-edit");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir.join("c.rb"), "class Gizmo\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);
    let clean = indexed_symbols(&db, &dir);

    fs::write(dir.join("c.rb"), "class Gizmo\n  def spin; end\nend\n").unwrap();
    let (ok, out) = rq(&db, &dir, &["spin", "--ndjson"]);
    // the miss that notices the edit reindexes it; the retry finds it
    let (ok2, out2) = rq(&db, &dir, &["spin", "--ndjson"]);
    assert!(ok || ok2, "the edit is indexed: {out} {out2}");
    assert_eq!(indexed_symbols(&db, &dir), clean + 1);

    git_checkout_file(&dir, "c.rb");
    // a search elsewhere — its hit's file is revalidated, not c.rb's
    assert!(
        warmed(&db, &dir, "widget"),
        "the discard is seen as a change"
    );
    assert_eq!(
        indexed_symbols(&db, &dir),
        clean,
        "the edit's symbol is gone"
    );
    let (found, out) = rq(&db, &dir, &["spin", "--ndjson"]);
    assert!(!found, "no longer found: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_discarded_deletion_is_found_again() {
    // The costly direction: an edit removed a method, the index took that in,
    // and discarding it brings the method back. Until reindexed, a search for
    // it is a confident "no match" (exit 1) for a symbol that is right there.
    let (dir, db) = scratch("discarded-deletion");
    fs::write(dir.join("c.rb"), "class Gizmo\n  def spin; end\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);

    fs::write(dir.join("c.rb"), "class Gizmo\nend\n").unwrap();
    rq(&db, &dir, &["spin"]); // revalidates c.rb: the method is gone
    let (found, _) = rq(&db, &dir, &["spin"]);
    assert!(!found, "the deletion is indexed");

    git_checkout_file(&dir, "c.rb");
    rq(&db, &dir, &["spin"]); // notices the discard and reindexes
    let (found, out) = rq(&db, &dir, &["spin", "--ndjson"]);
    assert!(found, "the restored method is found: {out}");

    let _ = fs::remove_dir_all(&dir);
}

/// Three `save`s; unanchored, the tie falls to path order and Gadget's leads.
fn three_saves(label: &str) -> (PathBuf, PathBuf) {
    let (dir, db) = scratch(label);
    fs::create_dir_all(dir.join("app/models")).unwrap();
    fs::create_dir_all(dir.join("lib")).unwrap();
    let class = |name: &str, extra: &str| {
        format!("class {name}\n  def save\n  end\n  def persist\n    save{extra}\n  end\nend\n")
    };
    fs::write(dir.join("app/models/widget.rb"), class("Widget", "")).unwrap();
    fs::write(dir.join("app/models/gadget.rb"), class("Gadget", "")).unwrap();
    fs::write(dir.join("lib/aaa.rb"), class("Aaa", "")).unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);
    (dir, db)
}

fn top_file(ndjson: &str) -> String {
    let row: serde_json::Value = serde_json::from_str(first_line(ndjson)).expect("ndjson");
    row["file"].as_str().unwrap_or_default().to_string()
}

#[test]
fn an_anchor_ranks_the_enclosing_class_first() {
    let (dir, db) = three_saves("anchor");
    let (_, plain) = rq(&db, &dir, &["save", "-k", "method", "--ndjson"]);
    assert_eq!(
        top_file(&plain),
        "app/models/gadget.rb",
        "baseline: {plain}"
    );
    assert!(!plain.contains("enclosing") && !plain.contains("proximity"));

    // line 5 is the `save` call inside Widget#persist
    let at = "app/models/widget.rb:5";
    let (ok, out) = rq(
        &db,
        &dir,
        &["save", "-k", "method", "--anchor", at, "--ndjson"],
    );
    assert!(ok, "{out}");
    assert_eq!(top_file(&out), "app/models/widget.rb", "{out}");
    let row: serde_json::Value = serde_json::from_str(first_line(&out)).unwrap();
    let features: Vec<&str> = row["features"]
        .as_array()
        .unwrap()
        .iter()
        .filter_map(|f| f.as_str())
        .collect();
    assert!(features.contains(&"enclosing") && features.contains(&"proximity"));

    // text + --explain name both features; a column is accepted
    let (_, text) = rq(
        &db,
        &dir,
        &[
            "save",
            "-k",
            "method",
            "--anchor",
            "app/models/widget.rb:5:7",
            "--explain",
        ],
    );
    assert!(
        first_line(&text).starts_with("app/models/widget.rb"),
        "{text}"
    );
    assert!(
        text.contains("enclosing 60") && text.contains("proximity 90"),
        "{text}"
    );

    // FILE resolves against the cwd, here a subdirectory
    let (_, sub) = rq(
        &db,
        &dir.join("app"),
        &[
            "save",
            "-k",
            "method",
            "--anchor",
            "models/widget.rb:5",
            "--json",
        ],
    );
    let rows: Vec<serde_json::Value> = serde_json::from_str(&sub).expect("json");
    assert_eq!(rows[0]["file"], "app/models/widget.rb", "{sub}");

    // every line of a batch is asked from the same place
    let mut child = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["-J", "-k", "method", "--anchor", at])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env("RQ_WARM_DETACH", "0")
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .spawn()
        .expect("spawn rq");
    {
        use std::io::Write;
        let mut stdin = child.stdin.take().unwrap();
        stdin.write_all(b"save\npersist\n").unwrap();
    }
    let batch = String::from_utf8(child.wait_with_output().unwrap().stdout).unwrap();
    let first_for = |q: &str| {
        batch
            .lines()
            .map(|l| serde_json::from_str::<serde_json::Value>(l).unwrap())
            .find(|r| r["query"] == q)
            .map(|r| r["file"].as_str().unwrap_or_default().to_string())
    };
    assert_eq!(
        first_for("save").as_deref(),
        Some("app/models/widget.rb"),
        "{batch}"
    );
    assert_eq!(
        first_for("persist").as_deref(),
        Some("app/models/widget.rb"),
        "{batch}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn an_anchor_file_the_index_has_not_seen_is_read_live() {
    // A file created after indexing, reopening Widget: nothing about it is in
    // the index, so only a live read knows line 3 sits inside Widget.
    let (dir, db) = three_saves("anchor-live");
    fs::write(
        dir.join("app/models/widget_ext.rb"),
        "class Widget\n  def again\n    save\n  end\nend\n",
    )
    .unwrap();
    let at = "app/models/widget_ext.rb:3";
    let (ok, out) = rq(
        &db,
        &dir,
        &["save", "-k", "method", "--anchor", at, "--ndjson"],
    );
    assert!(ok, "{out}");
    assert_eq!(top_file(&out), "app/models/widget.rb", "{out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn an_anchor_is_rejected_where_it_means_nothing() {
    let (dir, db) = scratch("anchor-bad");
    for args in [
        &["--status", "--anchor", "a.rb:1"][..],
        &["--symbols", "a.rb", "--anchor", "a.rb:1"],
        &["save", "--anchor", "a.rb"],
        &["save", "--anchor", "a.rb:0"],
    ] {
        let (ok, out, err) = rq_both(&db, &dir, args);
        assert!(!ok, "{args:?} should fail: {out}");
        assert!(err.contains("--anchor"), "{args:?}: {err}");
    }
    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn indexing_a_subdir_scopes_to_it_but_keeps_root_relative_paths() {
    // `rq --index <subdir>` seeds only that subtree in this run, yet stores
    // paths relative to the repo root so a later search still resolves them
    let (dir, db) = scratch("index-subdir");
    fs::create_dir_all(dir.join("sub")).unwrap();
    fs::create_dir_all(dir.join("other")).unwrap();
    fs::write(dir.join("sub/a.rb"), "class InScope\nend\n").unwrap();
    fs::write(dir.join("other/b.rb"), "class OutOfScope\nend\n").unwrap();
    git_init_commit(&dir);

    let (ok, out) = rq(&db, &dir, &["--index", "sub"]);
    assert!(ok, "scoped index failed: {out}");
    assert!(out.contains("subtree"), "subdir index is a seed: {out}");
    assert!(
        out.contains("1 files"),
        "this run indexed exactly the one in-scope file: {out}"
    );

    // the seeded class is found, at a repo-root-relative path
    let (ok, out) = rq(&db, &dir, &["inscope", "--ndjson"]);
    assert!(ok, "search failed: {out}");
    assert!(out.contains("InScope"), "in-scope class indexed: {out}");
    assert!(
        out.contains("\"file\":\"sub/a.rb\""),
        "path is root-relative, not subdir-relative: {out}"
    );

    // the seed is not a fence: searching warms the rest of the repo, so the
    // out-of-scope class is found — and persisted
    let (found, out) = rq(&db, &dir, &["outofscope", "--ndjson"]);
    assert!(found, "warming finds the out-of-scope class: {out}");
    assert!(out.contains("\"file\":\"other/b.rb\""), "warm hit: {out}");
    let (_, status) = rq(&db, &dir, &["--status", "--ndjson"]);
    assert!(
        status.contains("\"files\":2"),
        "warming persisted the rest of the repo: {status}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn empty_status_points_at_the_real_index_flag() {
    // the hint must name the actual flag (`rq --index`), not a non-existent
    // `rq index` subcommand
    let (dir, db) = scratch("empty-status");
    let (ok, out) = rq(&db, &dir, &["--status"]);
    assert!(ok, "status on an empty db should succeed: {out}");
    assert!(out.contains("rq --index"), "hint names the flag: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn open_launches_the_top_hit() {
    // `rq --open` picks the top hit and execs the launcher. RQ_OPEN drives a
    // harmless command here.
    let (dir, db) = scratch("open");
    fs::write(dir.join("user.rb"), "class User\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // RQ_OPEN runs `true` — exits 0, no editor needed; non-TTY takes the top hit
    let run = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["--open", "user"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env("RQ_OPEN", "true")
        .output()
        .expect("run rq");
    assert!(run.status.success(), "open should exit 0 via the launcher");

    // a launcher with no placeholder is handed path:line as its last argument;
    // one with a placeholder gets exactly what it asked for
    let launched = |template: &str| {
        let run = Command::new(env!("CARGO_BIN_EXE_rq"))
            .args(["--open", "user"])
            .current_dir(&dir)
            .env("RQ_DB", &db)
            .env("RQ_OPEN", template)
            .output()
            .expect("run rq");
        String::from_utf8_lossy(&run.stdout).trim().to_string()
    };
    let bare = launched("echo --wait");
    assert!(
        bare.starts_with("--wait ") && bare.ends_with("user.rb:1"),
        "{bare}"
    );
    assert_eq!(launched("echo line={line}"), "line=1");

    // with no launcher and no editor, --open prints the resolved path:line
    let run = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["--open", "user"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env_remove("EDITOR")
        .env_remove("VISUAL")
        .env("PATH", "/nonexistent") // hide any `code` on PATH
        .output()
        .expect("run rq");
    let printed = String::from_utf8_lossy(&run.stdout);
    assert!(
        printed.trim().ends_with("user.rb:1"),
        "prints resolved path:line: {printed}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn open_resolves_a_hit_against_its_own_checkout() {
    // under --all-repos the top hit can live in another repo; its file is
    // relative to that checkout, not the one rq was run from
    let (dir_a, db) = scratch("open-a");
    let dir_b = dir_a.with_file_name(format!("rq-e2e-{}-open-b", std::process::id()));
    let _ = fs::remove_dir_all(&dir_b);
    fs::create_dir_all(&dir_b).unwrap();
    fs::write(dir_a.join("a.rb"), "class Alpha\nend\n").unwrap();
    fs::write(dir_b.join("b.rb"), "class Gadget\nend\n").unwrap();
    rq(&db, &dir_a, &["--index"]);
    rq(&db, &dir_b, &["--index"]);

    let run = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["-a", "--open", "Gadget"])
        .current_dir(&dir_a)
        .env("RQ_DB", &db)
        .env("RQ_OPEN", "echo {file}")
        .output()
        .expect("run rq");
    let opened = String::from_utf8_lossy(&run.stdout).trim().to_string();
    let expected = dir_b.canonicalize().unwrap().join("b.rb");
    assert_eq!(opened, expected.to_string_lossy());

    let _ = fs::remove_dir_all(&dir_a);
    let _ = fs::remove_dir_all(&dir_b);
}

#[test]
fn web_links_the_newest_pushed_commit() {
    let (dir, db) = scratch("web");
    fs::write(dir.join("user.rb"), "\nclass User\nend\n").unwrap();
    git_init_commit(&dir);
    let git = |args: &[&str]| {
        let out = Command::new("git")
            .args(args)
            .current_dir(&dir)
            .output()
            .expect("run git");
        String::from_utf8_lossy(&out.stdout).trim().to_string()
    };
    // BROWSER=echo stands in for the browser, so the URL lands on stdout
    let web = || {
        let run = Command::new(env!("CARGO_BIN_EXE_rq"))
            .args(["-w", "user"])
            .current_dir(&dir)
            .env("RQ_DB", &db)
            .env("RQ_WARM_DETACH", "0")
            .env("BROWSER", "echo")
            .output()
            .expect("run rq");
        (
            run.status.success(),
            String::from_utf8_lossy(&run.stdout).trim().to_string(),
        )
    };

    // no remote → nothing to link to, but say what was picked and how else to
    // reach it
    rq(&db, &dir, &["--index"]);
    assert!(!web().0, "a local-only repo has no URL");
    let (_, _, err) = rq_both(&db, &dir, &["-w", "user"]);
    assert!(err.contains("user.rb:2"), "names the match: {err}");
    assert!(
        err.contains("-o") && err.contains("git remote add"),
        "{err}"
    );

    git(&["remote", "add", "origin", "git@github.com:org/app.git"]);
    rq(&db, &dir, &["--drop"]);
    rq(&db, &dir, &["--index"]);

    // nothing pushed yet: fall back to the host's default branch
    let (ok, url) = web();
    assert!(ok, "web should exit 0 via the launcher");
    assert_eq!(url, "https://github.com/org/app/blob/HEAD/user.rb#L2");

    // pushed, then committed past: link the pushed sha, not the unpushed HEAD
    let pushed = git(&["rev-parse", "HEAD"]);
    git(&["update-ref", "refs/remotes/origin/main", "HEAD"]);
    git(&[
        "-c",
        "user.email=t@e.st",
        "-c",
        "user.name=test",
        "commit",
        "-qm",
        "local",
        "--allow-empty",
    ]);
    assert_eq!(
        web().1,
        format!("https://github.com/org/app/blob/{pushed}/user.rb#L2")
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn drop_honors_json_output() {
    // --drop --json/-J report what was removed, so a script can act on it
    let (dir, db) = scratch("drop-json");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["--drop", "--json"]);
    assert!(ok, "drop --json failed: {out}");
    assert!(out.trim_start().starts_with('{'), "json object: {out}");
    assert!(out.contains("\"dropped\": true"), "reports dropped: {out}");

    // already gone → compact ndjson, dropped:false, still exit 0 (idempotent)
    let (ok, out) = rq(&db, &dir, &["--drop", "--ndjson"]);
    assert!(ok, "second drop should not error: {out}");
    let line = out.lines().next().unwrap_or("");
    assert!(
        line.starts_with('{') && line.contains("\"dropped\":false"),
        "ndjson dropped:false: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn drop_removes_a_repos_index() {
    // --drop is the inverse of --index: the repo disappears from coverage, and
    // dropping again is idempotent (a clear message, no error)
    let (dir, db) = scratch("drop");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    let (_, before) = rq(&db, &dir, &["--status"]);
    assert!(before.contains("symbol"), "indexed before drop: {before}");

    let (ok, out) = rq(&db, &dir, &["--drop"]);
    assert!(ok, "drop failed: {out}");
    assert!(out.contains("dropped"), "drop confirms: {out}");

    let (ok2, after) = rq(&db, &dir, &["--status"]);
    assert!(ok2, "status after drop: {after}");
    assert!(
        !after.contains("symbol"),
        "coverage gone after drop: {after}"
    );

    // idempotent: nothing left to drop, but not an error
    let (ok3, again) = rq(&db, &dir, &["--drop"]);
    assert!(ok3, "second drop should not error: {again}");
    assert!(again.contains("not indexed"), "idempotent message: {again}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn status_reads_a_first_index_in_progress_as_warming() {
    // A first pass commits files long before it writes coverage; until then
    // the repo is partially indexed, not "never" indexed.
    let (dir, db) = scratch("status-first-pass");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);
    rusqlite::Connection::open(&db)
        .unwrap()
        .execute("DELETE FROM coverage", [])
        .unwrap();

    let (ok, out) = rq(&db, &dir, &["--status", "--ndjson"]);
    assert!(ok, "status failed: {out}");
    assert!(out.contains("\"status\":\"warming\""), "mid-pass: {out}");
    assert!(out.contains("\"symbols\":1"), "keeps its totals: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn record_is_a_searchable_word_not_a_subcommand() {
    let (dir, db) = scratch("disambig");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // `rq record` searches for the symbol "record" (no hook, no match here)
    let (ok, out) = rq(&db, &dir, &["record"]);
    assert!(!ok, "no-match search should exit non-zero");
    assert!(out.is_empty(), "expected no result lines, got: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn status_and_index_honor_json_output() {
    let (dir, db) = scratch("ops-json");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();

    // --index --json: a single object with this-run and total counts
    let (ok, out) = rq(&db, &dir, &["--index", "--json"]);
    assert!(ok, "index --json failed: {out}");
    assert!(
        out.trim_start().starts_with('{'),
        "index json object: {out}"
    );
    assert!(out.contains("\"files_added\""), "index run counts: {out}");
    assert!(out.contains("\"repo\""), "index repo field: {out}");

    // --status --json: an array of coverage rows
    let (ok, out) = rq(&db, &dir, &["--status", "--json"]);
    assert!(ok, "status --json failed: {out}");
    assert!(
        out.trim_start().starts_with('['),
        "status json array: {out}"
    );
    assert!(
        out.contains("\"status\": \"complete\""),
        "status field: {out}"
    );

    // --status -J: one compact object per line
    let (ok, out) = rq(&db, &dir, &["--status", "--ndjson"]);
    assert!(ok, "status -J failed: {out}");
    let line = out.lines().next().unwrap_or("");
    assert!(
        line.starts_with('{') && line.ends_with('}') && line.contains("\"repo\""),
        "ndjson object per line: {out}"
    );

    // with nothing indexed, --status --json is still well-formed (an empty array)
    rq(&db, &dir, &["--drop"]);
    let (ok, out) = rq(&db, &dir, &["--status", "--json"]);
    assert!(ok && out.trim() == "[]", "empty status json is []: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn json_and_ndjson_output() {
    let (dir, db) = scratch("json");
    fs::write(dir.join("alpha.rb"), "class HandlerA\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // --json: a pretty array with named fields
    let (ok, out) = rq(&db, &dir, &["handler", "--json"]);
    assert!(ok, "json search failed: {out}");
    assert!(
        out.trim_start().starts_with('['),
        "expected a JSON array: {out}"
    );
    assert!(out.contains("\"name\": \"HandlerA\""), "name field: {out}");
    assert!(out.contains("\"file\": \"alpha.rb\""), "file field: {out}");
    assert!(out.contains("\"repo\":"), "repo field: {out}");
    assert!(
        out.contains("\"signature\": \"class HandlerA\""),
        "signature: {out}"
    );

    // --ndjson: one compact object per line
    let (ok, out) = rq(&db, &dir, &["handler", "--ndjson"]);
    assert!(ok, "ndjson search failed: {out}");
    let first = out.lines().next().unwrap_or("");
    assert!(
        first.starts_with('{') && first.ends_with('}'),
        "object per line: {out}"
    );
    assert!(
        first.contains("\"name\":\"HandlerA\""),
        "compact name: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn path_filter_restricts_results() {
    let (dir, db) = scratch("path");
    fs::create_dir_all(dir.join("app/services")).unwrap();
    fs::create_dir_all(dir.join("app/models")).unwrap();
    fs::write(
        dir.join("app/services/widget.rb"),
        "class WidgetService\nend\n",
    )
    .unwrap();
    fs::write(dir.join("app/models/widget.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // unfiltered: both files match "widget"
    let (_, out) = rq(&db, &dir, &["widget", "--ndjson"]);
    assert!(
        out.contains("app/models/widget.rb"),
        "expected models hit: {out}"
    );
    assert!(
        out.contains("app/services/widget.rb"),
        "expected services hit: {out}"
    );

    // --path app/services: only the services result survives
    let (ok, out) = rq(&db, &dir, &["widget", "--path", "app/services", "--ndjson"]);
    assert!(ok, "path search failed: {out}");
    assert!(
        out.contains("app/services/widget.rb"),
        "services hit kept: {out}"
    );
    assert!(
        !out.contains("app/models/widget.rb"),
        "models hit filtered out: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn path_filter_accepts_absolute_and_relative_paths() {
    let (dir, db) = scratch("path-abs");
    fs::create_dir_all(dir.join("app/services")).unwrap();
    fs::create_dir_all(dir.join("app/models")).unwrap();
    fs::write(
        dir.join("app/services/widget.rb"),
        "class WidgetService\nend\n",
    )
    .unwrap();
    fs::write(dir.join("app/models/widget.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // absolute, ./-relative, and bare repo-relative all normalize to the same
    // filter and keep only the services hit.
    let abs = dir.join("app/services");
    let abs = abs.to_str().unwrap();
    for spec in [abs, "./app/services", "app/services"] {
        let (ok, out) = rq(&db, &dir, &["widget", "--path", spec, "--ndjson"]);
        assert!(ok, "path search failed for {spec:?}: {out}");
        assert!(
            out.contains("app/services/widget.rb"),
            "services kept for {spec:?}: {out}"
        );
        assert!(
            !out.contains("app/models/widget.rb"),
            "models filtered for {spec:?}: {out}"
        );
    }

    // a path outside the repo normalizes to nothing, not everything
    let (_, out) = rq(
        &db,
        &dir,
        &["widget", "--path", "/nonexistent/elsewhere", "--ndjson"],
    );
    assert!(
        !out.contains("\"file\":"),
        "outside path yields no hits: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn limit_caps_the_number_of_results() {
    let (dir, db) = scratch("limit");
    // more definitions than the default cap, so `--limit 0` is distinguishable
    // from the default rather than just fitting under it
    let body: String = (0..12)
        .map(|i| format!("class Handler{i}\nend\n"))
        .collect();
    fs::write(dir.join("a.rb"), body).unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["handler", "--limit", "1", "--ndjson"]);
    assert!(ok, "limited search failed: {out}");
    assert_eq!(out.lines().count(), 1, "expected exactly one result: {out}");

    let (ok, out) = rq(&db, &dir, &["handler", "--ndjson"]);
    assert!(ok, "default search failed: {out}");
    assert_eq!(out.lines().count(), 10, "default caps at 10: {out}");

    // 0 lifts the cap rather than asking for nothing
    let (ok, out) = rq(&db, &dir, &["handler", "--limit", "0", "--ndjson"]);
    assert!(ok, "unlimited search failed: {out}");
    assert_eq!(out.lines().count(), 12, "expected every match: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_scope_that_matches_nothing_is_a_miss_not_the_top_hit() {
    let (dir, db) = scratch("scope");
    // one globally-unique method under a real owner — the condition under which
    // a wrong owner used to be silently discarded
    fs::write(
        dir.join("a.rb"),
        "module Shop\n  class Cart\n    def recalculate_totals\n      1\n    end\n  end\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    // the real owner resolves
    let (ok, out) = rq(&db, &dir, &["Cart#recalculate_totals", "--ndjson"]);
    assert!(ok, "real owner should resolve: {out}");
    assert!(out.contains("recalculate_totals"), "found it: {out}");

    // a made-up owner must not return that same definition. It used to, at
    // confidence 1.0 — the strongest signal available, on the one query whose
    // constraint had been thrown away.
    let (ok, out) = rq(&db, &dir, &["NoSuchClass#recalculate_totals", "--ndjson"]);
    assert!(!ok, "a bogus owner must not succeed: {out}");
    assert!(
        !out.contains("\"confidence\":1.0"),
        "and never at full confidence: {out}"
    );
    // and it's distinguishable from a name that simply isn't there, since
    // "wrong owner" is the more useful answer of the two
    assert!(
        out.contains("scope_not_found"),
        "reports the scope miss: {out}"
    );
    assert!(out.contains("Shop::Cart"), "says where it does live: {out}");

    let (_, out) = rq(&db, &dir, &["Cart#no_such_method_at_all", "--ndjson"]);
    assert!(
        out.contains("no_match"),
        "an absent name is still a plain miss: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_class_is_reachable_by_the_name_of_its_file() {
    let (dir, db) = scratch("pathrecall");
    // the class isn't named after the file, so only path recall can reach it
    fs::write(
        dir.join("billing.rb"),
        "class Invoicer\n  def run\n  end\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["billing", "--ndjson"]);
    assert!(ok, "path recall should find it: {out}");
    assert!(out.contains("Invoicer"), "the class in billing.rb: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_short_abbreviation_still_reaches_a_longer_name() {
    let (dir, db) = scratch("abbrev");
    fs::write(
        dir.join("a.rb"),
        "class UserAccount\n  def run\n  end\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    // `usr` skips letters, so neither an exact nor a prefix match reaches it —
    // this is what the first-character anchor pass exists for
    let (ok, out) = rq(&db, &dir, &["usr", "--ndjson"]);
    assert!(ok, "short abbreviation should resolve: {out}");
    assert!(
        out.contains("UserAccount"),
        "reached the longer name: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn one_name_declared_in_several_files_is_one_result() {
    let (dir, db) = scratch("reopen");
    // a module reopened across files, the Ruby (and Rust `impl`) reality —
    // four rows for one answer is the opposite of what ranking is for
    fs::write(dir.join("a.rb"), "module Shop\n  module Cart\n  end\nend\n").unwrap();
    fs::write(
        dir.join("b.rb"),
        "module Shop\n  module Cart\n    def add\n      1\n    end\n  end\nend\n",
    )
    .unwrap();
    fs::write(dir.join("c.rb"), "module Shop\n  module Cart\n  end\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq(&db, &dir, &["Cart", "--ndjson"]);
    assert!(ok, "search failed: {out}");
    assert_eq!(out.lines().count(), 1, "one result for one name: {out}");
    // and the fold is lossless — the other declarations are still reported
    assert!(
        out.contains("\"declarations\":3"),
        "counts the declarations: {out}"
    );
    assert!(out.contains("also_in"), "names where the others are: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_non_ascii_name_is_an_exact_match_however_it_is_cased() {
    // the index folds names with Unicode lowercasing; the query must fold the
    // same way, or `über` misses `Über` (and `ÜBER` misses both)
    let (dir, db) = scratch("unicode");
    fs::write(dir.join("a.py"), "class Über:\n    pass\n").unwrap();
    rq(&db, &dir, &["--index"]);
    for query in ["Über", "über", "ÜBER", "übe"] {
        let (ok, out) = rq(&db, &dir, &[query, "--ndjson"]);
        assert!(ok, "{query}: {out}");
        let row: serde_json::Value = serde_json::from_str(first_line(&out)).expect("ndjson");
        assert_eq!(row["name"], "Über", "{query}: {out}");
        let features = row["features"].to_string();
        assert!(
            features.contains("\"exact\"") || features.contains("\"prefix\""),
            "{query} is a literal match: {out}"
        );
    }
    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_typo_still_finds_the_definition() {
    let (dir, db) = scratch("typo");
    fs::write(
        dir.join("a.rb"),
        "class ConnectionPool\n  def checkout\n  end\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    // swapped letters and a doubled one both used to be hard misses: every
    // query character has to appear in order for a subsequence match
    for typo in ["connectoin_pool", "connection_poool"] {
        let (ok, out) = rq(&db, &dir, &[typo, "--ndjson"]);
        assert!(ok, "{typo} should find something: {out}");
        assert!(out.contains("ConnectionPool"), "{typo} finds it: {out}");
    }
    // a real word that simply isn't there is still a definitive miss
    let (ok, _) = rq(&db, &dir, &["WidgetFactory", "--ndjson"]);
    assert!(!ok, "an absent symbol is still a miss");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn confidence_and_total_do_not_depend_on_the_limit() {
    let (dir, db) = scratch("confidence");
    let body: String = (0..6).map(|i| format!("class Widget{i}\nend\n")).collect();
    fs::write(dir.join("a.rb"), body).unwrap();
    rq(&db, &dir, &["--index"]);

    // confidence is a comparison against the runner-up, so measuring it over
    // the returned window made `-l 1` unconditionally certain — and that
    // reading is what gates --show
    let conf = |n: &str| {
        let (_, out) = rq(&db, &dir, &["Widget", "--ndjson", "-l", n]);
        out.lines().next().unwrap_or_default().to_string()
    };
    let one = conf("1");
    assert!(
        !one.contains("\"confidence\":1.0"),
        "-l 1 isn't automatically certain: {one}"
    );
    // and the caller can tell how big the set it saw ten of actually was
    assert!(
        one.contains("\"total\":6"),
        "total reports the full match count: {one}"
    );

    // a filter narrows what total counts, but the limit still doesn't cut it
    fs::write(dir.join("b.rb"), "def widget_helper\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);
    for args in [&["class", "Widget"][..], &["Widget", "-k", "class"][..]] {
        let (_, out) = rq(&db, &dir, &[args, &["--ndjson", "-l", "1"]].concat());
        assert!(out.contains("\"total\":6"), "{args:?}: {out}");
    }

    // --explain reaches structured output instead of being silently dropped
    let (_, out) = rq(&db, &dir, &["Widget", "--ndjson", "-e", "-l", "1"]);
    assert!(
        out.contains("\"explain\""),
        "explain is carried in JSON: {out}"
    );
    // in whole points, as the text shows them — not the float the sum is made of
    let row: serde_json::Value = serde_json::from_str(first_line(&out)).expect("ndjson");
    let explain = row["explain"].as_object().expect("explain map");
    assert!(
        explain.contains_key("extent"),
        "a fractional feature: {out}"
    );
    let (_, text) = rq(&db, &dir, &["Widget", "-e", "-l", "1"]);
    for (name, value) in explain {
        let v = value.as_f64().unwrap();
        assert_eq!(v, v.round(), "{name} is whole: {out}");
        assert!(text.contains(&format!("{name} {v}")), "{name} {v}: {text}");
    }

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn an_unknown_kind_or_lang_is_an_error_not_a_miss() {
    let (dir, db) = scratch("badflag");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // a typo used to come back as a definitive no_match — the one exit code a
    // script is supposed to trust as "this symbol does not exist"
    let (ok, out) = rq(&db, &dir, &["Widget", "-k", "banana"]);
    assert!(!ok, "unknown kind should fail: {out}");
    assert!(
        !out.contains("no_match"),
        "reported as an error, not a miss: {out}"
    );
    let (ok, out) = rq(&db, &dir, &["Widget", "-x", "cobol"]);
    assert!(!ok, "unknown lang should fail: {out}");
    assert!(
        !out.contains("no_match"),
        "reported as an error, not a miss: {out}"
    );
    // a real one still works
    let (ok, out) = rq(&db, &dir, &["Widget", "-k", "class"]);
    assert!(ok, "known kind still searches: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn usage_counts_searches_by_caller_and_flags() {
    let (dir, db) = scratch("usage");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // nothing recorded yet exits 1, like a search that finds nothing
    let (ok, out) = rq(&db, &dir, &["--usage", "--ndjson"]);
    assert!(!ok, "empty usage should exit non-zero: {out}");

    rq(&db, &dir, &["Widget", "--ndjson"]);
    rq(&db, &dir, &["Widget", "--json", "--all-repos"]);
    rq(&db, &dir, &["NoSuchThing", "--ndjson"]);

    let (ok, out) = rq(&db, &dir, &["--usage", "--ndjson"]);
    assert!(ok, "usage failed: {out}");
    // the flag set is recorded, so agentic calls are separable from bare ones
    assert!(
        out.contains("\"flags\":\"ndjson\""),
        "bare ndjson row: {out}"
    );
    assert!(
        out.contains("\"flags\":\"json,all-repos\""),
        "flag set recorded in order: {out}"
    );
    // the miss is counted as a search that found nothing, not dropped: the two
    // bare `--ndjson` calls share a row, and one of them found nothing
    assert!(
        out.contains("\"searches\":2") && out.contains("\"misses\":1"),
        "the miss is counted, not dropped: {out}"
    );
    // a definitive miss against a ready index is not a warming one — rq
    // separates them in its exit codes, so the counts must too
    assert!(
        out.contains("\"warming\":0"),
        "an indexed repo's miss isn't counted as warming: {out}"
    );
    assert!(
        out.contains("\"on_complete\":2"),
        "queries against a complete index are counted: {out}"
    );

    // --show counts once whether it prints the body or falls through to the
    // list, which counts on its own exit
    fs::write(dir.join("b.rb"), "class Widgetry\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);
    let (_, shown) = rq(&db, &dir, &["Widget", "--show", "--ndjson"]);
    assert!(shown.contains("\"body\""), "confident --show: {shown}");
    let (_, fell) = rq(&db, &dir, &["Widg", "--show", "--ndjson"]);
    assert!(!fell.contains("\"body\""), "--show fell through: {fell}");
    let (_, out) = rq(&db, &dir, &["--usage", "--ndjson"]);
    let show_row = out
        .lines()
        .find(|l| l.contains("\"flags\":\"ndjson,show\""))
        .unwrap_or_else(|| panic!("no --show row: {out}"));
    assert!(show_row.contains("\"searches\":2"), "{show_row}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn index_a_subset_of_a_repo() {
    let (dir, db) = scratch("subset");
    fs::create_dir_all(dir.join("app/services")).unwrap();
    fs::create_dir_all(dir.join("app/models")).unwrap();
    fs::write(dir.join("app/services/charge.rb"), "class Charge\nend\n").unwrap();
    fs::write(dir.join("app/models/account.rb"), "class Account\nend\n").unwrap();
    // a git repo, so a search won't live-scan the whole tree (defeating the point)
    git_init(&dir);

    // seed only the services subtree: this run indexes just that, and
    // coverage stays warming (a seed, not a fence)
    let (ok, out) = rq(&db, &dir, &["--index", "--path", "app/services", "-J"]);
    assert!(ok, "subset index failed: {out}");
    assert!(out.contains("\"scope\":\"subtree\""), "seed marker: {out}");
    assert!(out.contains("\"files\":1"), "seeded one file: {out}");
    let (_, status) = rq(&db, &dir, &["--status", "--ndjson"]);
    assert!(
        status.contains("\"status\":\"warming\""),
        "a seed leaves coverage warming: {status}"
    );

    // the seeded subtree is searchable, with a repo-relative path
    let (ok, out) = rq(&db, &dir, &["charge", "--ndjson"]);
    assert!(ok, "charge search failed: {out}");
    assert!(
        out.contains("\"file\":\"app/services/charge.rb\""),
        "subset hit: {out}"
    );

    // a symbol outside the seed: warming continues over the rest of the repo,
    // finds it, and persists it
    let (ok, out) = rq(&db, &dir, &["account", "--ndjson"]);
    assert!(ok, "warming finds the unseeded symbol: {out}");
    assert!(
        out.contains("\"file\":\"app/models/account.rb\""),
        "warm hit: {out}"
    );
    let (_, status) = rq(&db, &dir, &["--status", "--ndjson"]);
    assert!(
        status.contains("\"files\":2"),
        "warming persisted the rest: {status}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn positional_paths_filter_like_rg() {
    let (dir, db) = scratch("pospath");
    fs::create_dir_all(dir.join("app/services")).unwrap();
    fs::create_dir_all(dir.join("app/models")).unwrap();
    fs::write(
        dir.join("app/services/widget.rb"),
        "class WidgetService\nend\n",
    )
    .unwrap();
    fs::write(dir.join("app/models/widget.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // path given positionally after the query, rg-style
    let (ok, out) = rq(&db, &dir, &["widget", "app/services", "--ndjson"]);
    assert!(ok, "positional-path search failed: {out}");
    assert!(
        out.contains("app/services/widget.rb"),
        "services hit kept: {out}"
    );
    assert!(
        !out.contains("app/models/widget.rb"),
        "models hit filtered out: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn kind_filter_scopes_by_symbol_kind() {
    let (dir, db) = scratch("kind");
    fs::write(dir.join("a.rb"), "class Charge\n  def charge\n  end\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // both a class and a method match "charge"
    let (_, out) = rq(&db, &dir, &["charge", "--ndjson"]);
    assert!(out.contains("\"kind\":\"class\""), "class present: {out}");
    assert!(out.contains("\"kind\":\"method\""), "method present: {out}");

    // -k m (shortcut for method) keeps only the method
    let (ok, out) = rq(&db, &dir, &["charge", "-k", "m", "--ndjson"]);
    assert!(ok, "kind search failed: {out}");
    assert!(out.contains("\"kind\":\"method\""), "method kept: {out}");
    assert!(
        !out.contains("\"kind\":\"class\""),
        "class filtered out: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn kind_constant_selects_constants_across_languages() {
    let (dir, db) = scratch("kind-const");
    fs::write(
        dir.join("limits.go"),
        "package limits\n\nconst MaxRetries = 3\n\nfunc MaxRetriesFor() int { return MaxRetries }\n",
    )
    .unwrap();
    fs::write(
        dir.join("limits.py"),
        "MAX_RETRIES = 3\n\ndef max_retries_for():\n    return MAX_RETRIES\n",
    )
    .unwrap();
    fs::write(
        dir.join("limits.ts"),
        "export const retryLimit = 3;\n\nexport function retryLimitFor() {\n  return retryLimit;\n}\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    for (query, lang) in [
        ("MaxRetries", "go"),
        ("MAX_RETRIES", "python"),
        ("retryLimit", "typescript"),
    ] {
        let (ok, out) = rq(&db, &dir, &[query, "-k", "constant", "--ndjson"]);
        assert!(ok, "{lang} constant search failed: {out}");
        assert!(
            out.contains(&format!("\"kind\":\"constant\",\"language\":\"{lang}\"")),
            "{lang} constant kept: {out}"
        );
        assert!(
            !out.contains("\"kind\":\"function\""),
            "{lang} function filtered: {out}"
        );
    }

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn an_index_from_before_constants_gains_them_on_the_next_search() {
    let (dir, db) = scratch("v14");
    fs::write(
        dir.join("limits.go"),
        "package limits\n\nconst MaxRetries = 3\n\nfunc Build() {}\n",
    )
    .unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);

    // rewind to what a v13 binary left behind: the same file, no constant
    {
        let conn = rusqlite::Connection::open(&db).unwrap();
        conn.execute_batch(
            "DELETE FROM symbols WHERE kind = 'constant'; PRAGMA user_version = 13;",
        )
        .unwrap();
    }

    // Opening migrates, which demotes the repo to warming. A search that the
    // old rows can't answer then holds for the in-process warm (detach is off
    // here), which re-parses the file the migration un-stamped.
    let (ok, out) = rq(&db, &dir, &["MaxRetries", "--ndjson"]);
    assert!(ok, "constant found after the upgrade: {out}");
    assert!(
        first_line(&out).contains("\"kind\":\"constant\""),
        "constant ranks first: {out}"
    );
    let (_, status) = rq(&db, &dir, &["--status", "--json"]);
    assert!(
        status.contains("\"status\": \"complete\""),
        "the sweep completed: {status}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn first_query_warms_the_index_without_an_explicit_reindex() {
    // A git repo that was never explicitly indexed: the first query opportunistically
    // warms the index (time-bounded) and still answers.
    let (dir, db) = scratch("warm");
    git_init(&dir);
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();

    let (ok, out) = rq(&db, &dir, &["widget", "--ndjson"]);
    assert!(ok, "cold search failed: {out}");
    assert!(out.contains("\"name\":\"Widget\""), "result present: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn explicitly_indexes_and_recognizes_a_non_git_directory() {
    // A non-git directory (no git_init): you can still index it explicitly, and
    // a later search recognizes it as the current repo and self-heals.
    let (dir, db) = scratch("nongit");
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();

    let (ok, out) = rq(&db, &dir, &["--index"]);
    assert!(ok, "index of a non-git dir failed: {out}");

    let (ok, out) = rq(&db, &dir, &["widget", "--ndjson"]);
    assert!(ok, "search failed: {out}");
    assert!(out.contains("\"name\":\"Widget\""), "result present: {out}");
    // recognized as the current repo → the current-repo boost applies
    assert!(
        out.contains("current_repo"),
        "current-repo boost applied: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn lang_filter_scopes_by_language() {
    let (dir, db) = scratch("lang");
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir.join("widget.rs"), "pub struct Widget {}\n").unwrap();
    rq(&db, &dir, &["--index"]);

    // both languages match "widget"
    let (_, out) = rq(&db, &dir, &["widget", "--ndjson"]);
    assert!(out.contains("\"language\":\"ruby\""), "ruby present: {out}");
    assert!(out.contains("\"language\":\"rust\""), "rust present: {out}");

    // -x rs keeps only rust
    let (ok, out) = rq(&db, &dir, &["widget", "-x", "rs", "--ndjson"]);
    assert!(ok, "lang search failed: {out}");
    assert!(out.contains("\"language\":\"rust\""), "rust kept: {out}");
    assert!(
        !out.contains("\"language\":\"ruby\""),
        "ruby filtered: {out}"
    );

    // -x r matches ruby + rust (prefix match) — both kept
    let (_, out) = rq(&db, &dir, &["widget", "-x", "r", "--ndjson"]);
    assert!(
        out.contains("\"language\":\"ruby\"") && out.contains("\"language\":\"rust\""),
        "both kept for -x r: {out}"
    );

    // spelled out keeps only that language
    let (_, out) = rq(&db, &dir, &["widget", "-x", "ruby", "--ndjson"]);
    assert!(out.contains("\"language\":\"ruby\""), "ruby kept: {out}");
    assert!(
        !out.contains("\"language\":\"rust\""),
        "rust filtered: {out}"
    );

    // -x py matches neither → no results
    let (ok, out) = rq(&db, &dir, &["widget", "-x", "py", "--ndjson"]);
    assert!(!ok, "no python here, should exit non-zero");
    assert!(
        out.contains("\"status\":\"no_match\""),
        "a definitive miss reports no_match: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn searching_from_a_subdirectory_uses_the_repo_root() {
    // Regression: warming/indexing must key off the repo root, not the cwd. A
    // search run from a subdirectory previously re-keyed the same repo under
    // subdir-relative paths and a fresh checkout root, so the reconcile and
    // staleness revalidation forgot everything indexed from the root.
    let (dir, db) = scratch("subdir");
    git_init(&dir);
    let sub = dir.join("nested");
    fs::create_dir_all(&sub).unwrap();
    fs::write(dir.join("top.rb"), "class TopWidget\nend\n").unwrap();
    fs::write(sub.join("deep.rb"), "class DeepWidget\nend\n").unwrap();

    // index the whole repo from its root — paths are repo-root-relative
    let (ok, _) = rq(&db, &dir, &["--index"]);
    assert!(ok);
    let (_, out) = rq(&db, &dir, &["DeepWidget"]);
    assert!(out.contains("nested/deep.rb"), "root-relative path: {out}");

    // searching from the subdirectory must reuse the same repo, not fork a new
    // one keyed at the subdir — the top-level symbol stays found, and there's
    // still exactly one repository with both files.
    let (_, out) = rq(&db, &sub, &["TopWidget"]);
    assert!(
        out.contains("TopWidget"),
        "top symbol found from subdir: {out}"
    );
    let (_, status) = rq(&db, &dir, &["--status"]);
    assert_eq!(
        status.lines().count(),
        1,
        "one repo, not re-keyed: {status}"
    );
    assert!(status.contains("2 files"), "both files retained: {status}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn warming_a_committed_repo_indexes_tracked_source() {
    // A committed git repo enumerates candidates from `git ls-files` (reading
    // git's index) instead of walking the filesystem — the path that keeps a huge
    // repo from burning its warm budget on a non-source tree. The tracked source
    // is found on the first search; a non-source file is ignored.
    let (dir, db) = scratch("gitwarm");
    fs::create_dir_all(dir.join("lib")).unwrap();
    fs::write(dir.join("lib/widget.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir.join("README.md"), "# docs, not source\n").unwrap();
    git_init_commit(&dir);

    let (ok, out) = rq(&db, &dir, &["Widget"]);
    assert!(ok, "warmed search failed: {out}");
    assert!(
        out.contains("lib/widget.rb"),
        "tracked source warmed: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_cold_repo_blocks_to_an_answer_instead_of_a_false_miss() {
    // The core fix: a query against an unindexed repo would otherwise hit the
    // bounded budget and see a *false* "no matches" while the symbol sits
    // unindexed. With a tiny answer budget the old bounded path gives up first;
    // now the query blocks and keeps indexing until the answer appears. This is
    // the *programmatic* (--json, non-TTY) path — correctness for agents/scripts,
    // no progress UI.
    let (dir, db) = scratch("block-json");
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir);

    let run = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["Widget", "--json"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env("RQ_ANSWER_BUDGET_MS", "1") // bounded path would give up immediately
        .output()
        .expect("run rq");
    let out = String::from_utf8_lossy(&run.stdout);
    assert!(
        run.status.success(),
        "a programmatic query should block until it finds the symbol; \
         stdout={out:?} stderr={:?}",
        String::from_utf8_lossy(&run.stderr)
    );
    assert!(out.contains("widget.rb"), "found in the right file: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn an_interactive_cold_repo_shows_progress_and_finds_the_answer() {
    // The human path: forcing interactive turns on the stderr heads-up + Ctrl-C
    // handling, but the blocking-until-answered behavior is the same as --json.
    let (dir, db) = scratch("block-tty");
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir);

    let run = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["Widget"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env("RQ_ANSWER_BUDGET_MS", "1")
        .env("RQ_ASSUME_INTERACTIVE", "1") // pretend a TTY
        .output()
        .expect("run rq");
    let out = String::from_utf8_lossy(&run.stdout);
    assert!(run.status.success(), "should find the symbol: {out:?}");
    assert!(out.contains("widget.rb"), "found in the right file: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn an_incomplete_index_reports_an_indeterminate_miss_not_a_definitive_one() {
    // A miss while the index is still warming is "not yet", not "absent". Capping
    // the pass below the repo size keeps coverage "warming", so a query for a
    // symbol that isn't in the indexed slice must exit 2 (indeterminate) — letting
    // an agent retry rather than conclude the symbol doesn't exist.
    let (dir, db) = scratch("indeterminate");
    for i in 0..20 {
        fs::write(
            dir.join(format!("m{i:02}.rb")),
            format!("class Widget{i}\nend\n"),
        )
        .unwrap();
    }
    git_init_commit(&dir);

    let run = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["Nonexistent", "--json"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env("RQ_COLLECT_CAP", "5") // one pass can't finish → stays "warming"
        .output()
        .expect("run rq");
    let out = String::from_utf8_lossy(&run.stdout);
    assert!(
        out.contains("\"status\": \"warming\""),
        "an incomplete-index miss reports warming in JSON: {out:?}"
    );
    assert_eq!(
        run.status.code(),
        Some(2),
        "an incomplete-index miss is indeterminate (exit 2), not definitive"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_cold_search_finds_a_symbol_beyond_what_one_pass_parses() {
    // The file naming the query is indexed ahead of walk order, so the first
    // search answers even when the pass is capped short of it — by a method
    // whose name is nowhere in its path.
    let (dir, db) = scratch("demand");
    for i in 0..20 {
        fs::write(
            dir.join(format!("m{i:02}.rb")),
            format!("class M{i}\nend\n"),
        )
        .unwrap();
    }
    fs::write(
        dir.join("m19.rb"),
        "class M19\n  def render_totals\n  end\nend\n",
    )
    .unwrap();
    git_init_commit(&dir);

    let run = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["M19#render_totals", "--json"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env("RQ_WARM_DETACH", "0")
        .env("RQ_COLLECT_CAP", "2")
        .output()
        .expect("run rq");
    let out = String::from_utf8_lossy(&run.stdout);
    assert_eq!(
        run.status.code(),
        Some(0),
        "answered on the first search: {out}"
    );
    assert!(out.contains("m19.rb"), "found in the right file: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn no_wait_returns_without_blocking_on_a_rebuild() {
    // `--no-wait` is the agent/script escape hatch: a query issued while the index
    // is (re)building must answer from the committed index *now*, never
    // block-until-answered on the writer. A default programmatic miss on an
    // incomplete index blocks up to the wait budget before giving up (see
    // `an_incomplete_index_reports_an_indeterminate_miss_not_a_definitive_one`);
    // here the wait budget is 10 minutes, so if --no-wait blocked at all this test
    // would hang. It returns at once instead — an honest `warming` miss (exit 2,
    // "retry"), not a multi-minute hang and not a false definitive absence.
    let (dir, db) = scratch("no-wait");
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir);

    // Both spellings must return at once (not block): the bare flag and its
    // duration form `--wait 0`. RQ_WAIT_BUDGET_MS is 10 minutes throughout, so a
    // block on either would hang the test.
    for flags in [["--no-wait"].as_slice(), ["--wait", "0"].as_slice()] {
        let mut args = vec!["Nonexistent", "--json"];
        args.extend_from_slice(flags);
        let run = Command::new(env!("CARGO_BIN_EXE_rq"))
            .args(&args)
            .current_dir(&dir)
            .env("RQ_DB", &db)
            .env("RQ_WARM_DETACH", "0") // hermetic: no detached child races cleanup
            .env("RQ_WAIT_BUDGET_MS", "600000") // a block, if it happened, would hang the test
            .output()
            .expect("run rq");
        let out = String::from_utf8_lossy(&run.stdout);
        assert!(
            out.contains("\"status\": \"warming\""),
            "{flags:?} miss on a not-yet-complete index reports warming (retry), \
             not a block or a false absence: {out:?}"
        );
        assert_eq!(
            run.status.code(),
            Some(2),
            "{flags:?} miss on an incomplete index is indeterminate (exit 2)"
        );
    }

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn symbols_outlines_a_file_in_line_order() {
    let (dir, db) = scratch("symbols");
    fs::write(
        dir.join("widget.rb"),
        "class Widget\n  def build\n  end\n  def render\n  end\nend\n",
    )
    .unwrap();
    fs::write(dir.join("other.rb"), "class Other\nend\n").unwrap();
    git_init_commit(&dir);

    // ndjson outline: the file's symbols, in line order, with kind/parent/signature.
    let (ok, out) = rq(&db, &dir, &["--symbols", "widget.rb", "--ndjson"]);
    assert!(ok, "symbols failed: {out}");
    let lines: Vec<&str> = out.lines().collect();
    assert_eq!(lines.len(), 3, "class + two methods: {out}");
    assert!(
        first_line(&out).contains("\"name\":\"Widget\""),
        "class first: {out}"
    );
    assert!(out.contains("\"name\":\"build\""), "build present: {out}");
    assert!(
        out.contains("\"parent\":\"Widget\""),
        "method nests under class: {out}"
    );
    assert!(
        out.contains("\"signature\":\"def build\""),
        "signature read: {out}"
    );
    // scoped to the named file only
    assert!(!out.contains("Other"), "other file excluded: {out}");

    // --kind filters the outline to just methods (drops the class).
    let (ok, out) = rq(
        &db,
        &dir,
        &["--symbols", "widget.rb", "-k", "method", "--ndjson"],
    );
    assert!(ok, "filtered symbols failed: {out}");
    assert_eq!(out.lines().count(), 2, "two methods only: {out}");
    assert!(
        !out.contains("\"name\":\"Widget\""),
        "class filtered out: {out}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn symbols_reflects_the_file_as_it_is_on_disk_now() {
    // On a complete index the outline must track the file itself — an edit, a
    // brand-new untracked file, a deletion — without a repo-wide re-index.
    let (dir, db) = scratch("symbols-fresh");
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);

    fs::write(
        dir.join("widget.rb"),
        "class Widget\n  def added\n  end\nend\n",
    )
    .unwrap();
    let (ok, out) = rq(&db, &dir, &["--symbols", "widget.rb", "--ndjson"]);
    assert!(
        ok && out.contains("\"name\":\"added\""),
        "edit shows: {out}"
    );

    fs::write(dir.join("fresh.rb"), "class Fresh\nend\n").unwrap();
    let (ok, out) = rq(&db, &dir, &["--symbols", "fresh.rb", "--ndjson"]);
    assert!(
        ok && out.contains("\"name\":\"Fresh\""),
        "new file shows: {out}"
    );

    fs::remove_file(dir.join("widget.rb")).unwrap();
    let (ok, out) = rq(&db, &dir, &["--symbols", "widget.rb", "--ndjson"]);
    assert!(!ok, "a deleted file has no outline: {out}");
    assert!(!out.contains("Widget"), "no stale rows: {out}");

    let (ok, out) = rq(&db, &dir, &["--symbols", "notes.txt", "--ndjson"]);
    assert!(!ok, "an unsupported file has no outline: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn symbols_outlines_its_file_on_a_cold_repo_too_big_for_one_pass() {
    // The named file is indexed first however far the pass gets — including a
    // short name that looks nothing like its own path.
    let (dir, db) = scratch("symbols-cold");
    for i in 0..20 {
        fs::write(
            dir.join(format!("m{i:02}.rb")),
            format!("class M{i}\nend\n"),
        )
        .unwrap();
    }
    git_init_commit(&dir);

    let out = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["--symbols", "m19.rb", "--ndjson"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env("RQ_WARM_DETACH", "0")
        .env("RQ_COLLECT_CAP", "2")
        .output()
        .expect("run rq");
    let text = String::from_utf8_lossy(&out.stdout);
    assert!(
        out.status.success() && text.contains("\"name\":\"M19\""),
        "the file is outlined: {text}"
    );

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn bare_invocation_prints_help() {
    let (dir, db) = scratch("help");
    let (ok, out) = rq(&db, &dir, &[]);
    assert!(ok, "bare rq should exit 0");
    assert!(
        out.contains("rq finds where a symbol is defined"),
        "help banner: {out}"
    );
    assert!(out.contains("Usage:"), "usage in help: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn detached_warm_finishes_coverage_in_the_background() {
    let (dir, db) = scratch("detach");
    for i in 0..30 {
        fs::write(
            dir.join(format!("f{i:02}.rb")),
            format!("class K{i:02}\nend\n"),
        )
        .unwrap();
    }
    git_init_commit(&dir);

    // Cap each in-process pass tiny so the answering query can't finish the
    // sweep itself; detached warming (on) must pick up the remainder. The
    // child inherits the cap, so it needs several passes — exercising its
    // sweep-until-complete loop too.
    let out = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["K00"])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env("RQ_WARM_DETACH", "1")
        .env("RQ_COLLECT_CAP", "5")
        .output()
        .expect("run rq");
    assert!(
        out.status.success(),
        "search failed: {}",
        String::from_utf8_lossy(&out.stdout)
    );

    // the detached child completes the sweep with no further queries
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
    loop {
        let (_, status) = rq(&db, &dir, &["--status", "--ndjson"]);
        if status.contains("\"status\":\"complete\"") && status.contains("\"files\":30") {
            break;
        }
        assert!(
            std::time::Instant::now() < deadline,
            "detached warm never completed: {status}"
        );
        std::thread::sleep(std::time::Duration::from_millis(200));
    }

    let _ = fs::remove_dir_all(&dir);
}

/// Run the binary with queries piped on stdin, returning stdout.
fn rq_stdin(db: &Path, cwd: &Path, args: &[&str], stdin: &str) -> (bool, String) {
    use std::io::Write;
    let mut child = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(args)
        .current_dir(cwd)
        .env("RQ_DB", db)
        .env("RQ_WARM_DETACH", "0")
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("run rq");
    child
        .stdin
        .take()
        .expect("stdin piped")
        .write_all(stdin.as_bytes())
        .expect("write queries");
    let out = child.wait_with_output().expect("rq exits");
    (
        out.status.success(),
        String::from_utf8_lossy(&out.stdout).into_owned(),
    )
}

#[test]
fn batch_answers_every_piped_query_and_says_which_is_which() {
    // One process, many questions: each row has to name the query it answers,
    // or a caller can't tell whose results are whose.
    let (dir, db) = scratch("batch");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    fs::write(dir.join("b.rb"), "class Gadget\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);

    let (ok, out) = rq_stdin(
        &db,
        &dir,
        &["-J", "-l", "1"],
        "widget\ngadget\nnosuchthing\n",
    );
    assert!(ok, "a batch that found something exits 0: {out}");

    let rows: Vec<serde_json::Value> = out
        .lines()
        .filter(|l| !l.trim().is_empty())
        .map(|l| serde_json::from_str(l).expect("ndjson line"))
        .collect();
    let for_query = |q: &str| {
        rows.iter()
            .find(|r| r["query"] == q)
            .unwrap_or_else(|| panic!("no row for {q}: {out}"))
            .clone()
    };
    assert_eq!(for_query("widget")["name"], "Widget");
    assert_eq!(for_query("gadget")["name"], "Gadget");
    // a miss is reported, not silently dropped — otherwise it's
    // indistinguishable from a query that never ran
    assert_eq!(for_query("nosuchthing")["status"], "no_match");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn batch_refuses_the_output_and_flags_it_cannot_frame() {
    let (dir, db) = scratch("batch-flags");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir);
    rq(&db, &dir, &["--index"]);

    // --json would have to frame N result sets as one array; --ndjson is the
    // shape that streams
    let (ok, _) = rq_stdin(&db, &dir, &["--json"], "widget\n");
    assert!(!ok, "--json is refused for a batch");
    // --show/--open act on one result
    let (ok, _) = rq_stdin(&db, &dir, &["-J", "--show"], "widget\n");
    assert!(!ok, "--show is refused for a batch");

    let _ = fs::remove_dir_all(&dir);
}

/// Run the binary with extra env and optional piped stdin; hand back the exit
/// code, stdout and stderr.
fn rq_full(
    db: &Path,
    cwd: &Path,
    args: &[&str],
    env: &[(&str, &str)],
    stdin: Option<&str>,
) -> (i32, String, String) {
    use std::io::Write;
    let mut cmd = Command::new(env!("CARGO_BIN_EXE_rq"));
    cmd.args(args)
        .current_dir(cwd)
        .env("RQ_DB", db)
        .env("RQ_WARM_DETACH", "0")
        .stdin(if stdin.is_some() {
            std::process::Stdio::piped()
        } else {
            std::process::Stdio::null()
        })
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped());
    for (k, v) in env {
        cmd.env(k, v);
    }
    let mut child = cmd.spawn().expect("run rq");
    if let Some(input) = stdin {
        child
            .stdin
            .take()
            .expect("stdin piped")
            .write_all(input.as_bytes())
            .expect("write stdin");
    }
    let out = child.wait_with_output().expect("rq exits");
    (
        out.status.code().expect("exit code"),
        String::from_utf8_lossy(&out.stdout).into_owned(),
        String::from_utf8_lossy(&out.stderr).into_owned(),
    )
}

#[test]
fn a_structured_caller_gets_its_errors_as_json() {
    let (dir, db) = scratch("json-errors");
    fs::write(dir.join("a.rb"), "class Widget\nend\n").unwrap();
    rq(&db, &dir, &["--index"]);
    // a file where the database's directory should be: nothing can be created
    let blocked = dir.join("blocked");
    fs::write(&blocked, "").unwrap();
    let unwritable = blocked.join("rq.db");
    let corrupt = dir.join("corrupt.db");
    fs::write(&corrupt, "not a database, just bytes ".repeat(200)).unwrap();

    let unwritable = unwritable.to_str().unwrap();
    let corrupt = corrupt.to_str().unwrap();
    // args, extra env, piped stdin, the error kind expected
    type Case<'a> = (
        &'a [&'a str],
        &'a [(&'a str, &'a str)],
        Option<&'a str>,
        &'a str,
    );
    let cases: &[Case] = &[
        (&["Widget", "-k", "banana", "--json"], &[], None, "usage"),
        (&["Widget", "-x", "cobol", "-J"], &[], None, "usage"),
        (&["  ", "--json"], &[], None, "usage"),
        // clap rejects these before rq has read its own flags
        (&["Widget", "--wait", "soon", "--json"], &[], None, "usage"),
        (&["Widget", "-ej", "--wait", "soon"], &[], None, "usage"),
        (&["-w", "Widget", "-J"], &[], None, "usage"),
        (&["--no-such-flag", "Widget"], &[], None, "usage"),
        (&["--json"], &[], Some("Widget\n"), "usage"),
        (
            &["Widget", "--json"],
            &[("RQ_DB", unwritable)],
            None,
            "database",
        ),
        (&["Widget", "-J"], &[("RQ_DB", corrupt)], None, "database"),
        (
            &["--status", "--json"],
            &[("RQ_DB", corrupt)],
            None,
            "database",
        ),
        (&["--symbols", "gone.rb", "--json"], &[], None, "not_found"),
    ];
    // sysexits(3): none shared with a hit (0), a miss (1) or warming (2)
    let exit_code = |kind| match kind {
        "usage" => 64,
        "not_found" => 66,
        "database" => 74,
        other => panic!("no expected exit code for {other}"),
    };
    for &(args, env, stdin, kind) in cases {
        let (code, out, err) = rq_full(&db, &dir, args, env, stdin);
        assert_eq!(code, exit_code(kind), "{args:?}: exit code for {kind}");
        // no structured flag: this case is about the text path's exit code
        if !args.iter().any(|a| matches!(*a, "--json" | "-J" | "-ej")) {
            assert!(out.is_empty(), "{args:?}: nothing on stdout: {out:?}");
            continue;
        }
        let obj: serde_json::Value = serde_json::from_str(out.trim())
            .unwrap_or_else(|e| panic!("{args:?}: one JSON object on stdout ({e}): {out:?}"));
        assert_eq!(obj["kind"], kind, "{args:?}: {obj}");
        assert_eq!(
            obj["code"], code,
            "{args:?}: the object carries the exit code"
        );
        let message = obj["error"].as_str().expect("an error message");
        // the human still reads it on stderr, the same words
        assert!(
            err.contains(message),
            "{args:?}: stderr {err:?} vs {message:?}"
        );

        // without a structured flag nothing lands on stdout, and the exit
        // code is the same (`-w` alone is legal, so it has nothing to compare)
        let plain: Vec<&str> = args
            .iter()
            .copied()
            .filter(|a| !matches!(*a, "--json" | "-J"))
            .map(|a| if a == "-ej" { "-e" } else { a })
            .collect();
        if stdin.is_none() && !args.contains(&"-w") {
            let (plain_code, out, _) = rq_full(&db, &dir, &plain, env, None);
            assert_eq!(plain_code, code, "{plain:?}: same exit code as text");
            assert!(out.is_empty(), "{plain:?}: nothing on stdout: {out:?}");
        }
    }
    // a value that merely contains `j` is not the flag
    let (_, out, _) = rq_full(&db, &dir, &["Widget", "-xj", "-k", "banana"], &[], None);
    assert!(out.is_empty(), "`-xj` is --lang j, not --json: {out:?}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn help_and_version_are_not_usage_errors() {
    let (dir, db) = scratch("help-version");
    for args in [["--help"], ["-h"], ["--version"], ["-V"]] {
        let (code, out, _) = rq_full(&db, &dir, &args, &[], None);
        assert_eq!(code, 0, "{args:?} succeeds");
        assert!(!out.is_empty(), "{args:?} prints to stdout");
    }
    let (_, help, _) = rq_full(&db, &dir, &["--help"], &[], None);
    assert!(help.contains("EXIT CODES"), "--help lists them: {help}");
    assert!(help.contains("--drop"), "and the commands' own: {help}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn commands_with_nothing_to_do_still_succeed() {
    // A command that ran but had nothing to report or remove still succeeded;
    // exit 1 stays a search's "absent" (and --usage's "nothing recorded").
    let (dir, db) = scratch("nothing-to-do");
    for args in [&["--status"][..], &["--drop"], &["--index"], &["--index"]] {
        let (code, out, _) = rq_full(&db, &dir, args, &[], None);
        assert_eq!(code, 0, "{args:?}: {out}");
    }

    let _ = fs::remove_dir_all(&dir);
}

/// `--profile` on an index run reports phases, counters and the slowest files —
/// to stderr, structured when the output mode is, and nothing at all when off.
#[test]
fn index_profile_reports_phases_and_counters() {
    let (dir, db) = scratch("index-profile");
    fs::write(dir.join("alpha.rb"), "class HandlerA\nend\n").unwrap();
    fs::write(dir.join("beta.rb"), "class HandlerB\ndef go\nend\nend\n").unwrap();

    // off: stdout carries the result, stderr stays silent
    let (ok, out, err) = rq_both(&db, &dir, &["--index"]);
    assert!(ok, "index failed: {out}");
    assert!(
        !err.contains("walk+parse+write"),
        "profile leaked with --profile off: {err}"
    );

    // text: a human-readable table on stderr, stdout untouched
    let (dir2, db2) = scratch("index-profile-text");
    fs::write(dir2.join("alpha.rb"), "class HandlerA\nend\n").unwrap();
    let (ok, out, err) = rq_both(&db2, &dir2, &["--index", "--profile"]);
    assert!(ok, "index failed: {out}");
    for phase in [
        "index: setup",
        "index: walk+parse+write",
        "index: store writes",
    ] {
        assert!(err.contains(phase), "missing {phase} in profile: {err}");
    }
    for counter in ["files seen", "files parsed", "batches", "parse jobs"] {
        assert!(err.contains(counter), "missing {counter} in profile: {err}");
    }
    assert!(err.contains("slowest"), "missing slowest files: {err}");
    assert!(
        !out.contains("walk+parse+write"),
        "profile must not pollute stdout: {out}"
    );

    // json: one object on stderr with the same phase names; stdout stays parseable
    let (dir3, db3) = scratch("index-profile-json");
    fs::write(dir3.join("alpha.rb"), "class HandlerA\nend\n").unwrap();
    let (ok, out, err) = rq_both(&db3, &dir3, &["--index", "--profile", "--json"]);
    assert!(ok, "index failed: {out}");
    let stdout_json: serde_json::Value =
        serde_json::from_str(&out).unwrap_or_else(|e| panic!("stdout not json ({e}): {out}"));
    assert_eq!(stdout_json["scope"], "full");
    let profile: serde_json::Value = serde_json::from_str(err.trim())
        .unwrap_or_else(|e| panic!("profile not json ({e}): {err}"));
    assert!(profile["total_ms"].is_number(), "total_ms missing: {err}");
    let names: Vec<&str> = profile["phases"]
        .as_array()
        .expect("phases array")
        .iter()
        .filter_map(|p| p["name"].as_str())
        .collect();
    assert!(
        names.contains(&"index: walk+parse+write"),
        "phases: {names:?}"
    );
    assert_eq!(profile["counters"]["files parsed"], 1);
    assert!(
        profile["counters"]["parse jobs"].as_u64().unwrap_or(0) >= 1,
        "parse jobs: {err}"
    );
    let slowest = profile["slowest"].as_array().expect("slowest array");
    assert_eq!(slowest.len(), 1, "one file parsed → one slowest entry");
    assert_eq!(slowest[0]["file"], "alpha.rb");
}

/// A search's profile covers the whole run: the time to the first answer is
/// marked within `total`, and a miss reports too.
#[test]
fn search_profile_covers_the_whole_run() {
    let (dir, db) = scratch("search-profile");
    fs::write(dir.join("alpha.rb"), "class HandlerA\nend\n").unwrap();
    let (ok, out, _) = rq_both(&db, &dir, &["--index"]);
    assert!(ok, "index failed: {out}");

    let phases = |args: &[&str]| {
        let (_, _, err) = rq_both(&db, &dir, args);
        let profile: serde_json::Value = serde_json::from_str(err.trim())
            .unwrap_or_else(|e| panic!("profile not json ({e}): {err}"));
        let total = profile["total_ms"].as_f64().expect("total_ms");
        let phases: Vec<(String, f64)> = profile["phases"]
            .as_array()
            .expect("phases array")
            .iter()
            .map(|p| {
                (
                    p["name"].as_str().unwrap_or_default().to_string(),
                    p["ms"].as_f64().unwrap_or(0.0),
                )
            })
            .collect();
        (total, phases)
    };

    let (total, hit) = phases(&["HandlerA", "--profile", "--json"]);
    let answer = hit
        .iter()
        .find(|(n, _)| n == "first answer")
        .unwrap_or_else(|| panic!("no first answer: {hit:?}"));
    assert!(answer.1 <= total, "first answer after total: {hit:?}");
    // the usage write is bookkeeping: it must never hold up the answer
    let at = |name: &str| hit.iter().position(|(n, _)| n == name);
    assert!(
        at("first answer") < at("after: record usage"),
        "usage recorded before the answer: {hit:?}"
    );

    let (_, miss) = phases(&["Nonexistent", "--profile", "--json"]);
    assert!(
        miss.iter().any(|(n, _)| n == "query"),
        "miss unreported: {miss:?}"
    );

    let (_, symbols) = phases(&["--symbols", "alpha.rb", "--profile", "--json"]);
    assert!(
        symbols.iter().any(|(n, _)| n == "symbols: query"),
        "{symbols:?}"
    );
}

#[test]
fn a_ruby_predicate_is_found_by_its_full_name() {
    // `?` ends a Ruby predicate's name; typed in full it must not read as a
    // one-char wildcard that can never match the name's own `?`
    let (dir, db) = scratch("predicate");
    fs::write(
        dir.join("a.rb"),
        "class Widget\n  def empty?; end\n  def save!; end\n  def only_uploads?; end\nend\n",
    )
    .unwrap();
    rq(&db, &dir, &["--index"]);

    for q in ["empty?", "Widget#empty?", "save!", "only_uploads?"] {
        let (ok, out) = rq(&db, &dir, &[q]);
        assert!(ok && out.contains("a.rb"), "{q}: {out}");
    }
    // a `?` inside a query is still a wildcard
    let (ok, out) = rq(&db, &dir, &["emp?y?"]);
    assert!(ok && out.contains("empty?"), "glob: {out}");
    // and a glob crosses the name's `_` the way it ignores the query's
    let (ok, out) = rq(&db, &dir, &["only_up*s"]);
    assert!(ok && out.contains("only_uploads?"), "glob over _: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn foo_dot_new_finds_the_class_outside_git_too() {
    // an untracked dir answers from a live scan, which must fall back to the
    // class like the index does when the constructor is implicit
    let (dir, db) = scratch("constructor-live");
    fs::write(
        dir.join("account.rb"),
        "module Account\n  class Ledger\n    def self.open\n      new\n    end\n  end\nend\n",
    )
    .unwrap();
    for q in ["Ledger.new", "Account::Ledger.new"] {
        let (ok, out) = rq(&db, &dir, &[q, "--ndjson"]);
        assert!(
            ok && out.contains("\"class\"") && out.contains("\"line\":2"),
            "{q}: {out}"
        );
    }

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn no_wait_on_an_unindexed_repo_answers_from_a_live_scan() {
    // --no-wait won't index before answering, so a git repo with no finished
    // pass — never indexed, or just dropped — is scanned live instead: a hit
    // says `live`, and a miss is `warming` because the index can't vouch yet.
    let (dir, db) = scratch("no-wait-live");
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir);
    let source = |out: &str| {
        let row: serde_json::Value = serde_json::from_str(first_line(out)).expect("a JSON row");
        row["source"].as_str().unwrap_or_default().to_string()
    };

    let (code, out, _) = rq_full(&db, &dir, &["Widget", "-J", "--no-wait"], &[], None);
    assert_eq!(code, 0, "{out}");
    assert_eq!(source(&out), "live", "cold: {out}");
    let (code, _, _) = rq_full(&db, &dir, &["Gizmo", "-J", "--no-wait"], &[], None);
    assert_eq!(code, 2, "a cold miss is warming, not absent");

    rq(&db, &dir, &["--index"]);
    let (_, out, _) = rq_full(&db, &dir, &["Widget", "-J", "--no-wait"], &[], None);
    assert_eq!(source(&out), "index", "indexed: {out}");

    rq(&db, &dir, &["--drop"]);
    let (_, out, _) = rq_full(&db, &dir, &["Widget", "-J", "--no-wait"], &[], None);
    assert_eq!(source(&out), "live", "dropped: {out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn a_live_scan_answer_says_so() {
    // never indexed and not a git repo, so the only answer is a live scan
    let (dir, db) = scratch("live-source");
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();
    let source = |out: &str| {
        let row: serde_json::Value = serde_json::from_str(first_line(out)).expect("a JSON row");
        row["source"].as_str().unwrap_or_default().to_string()
    };

    // quiet by default: the row says where it came from, stderr says nothing
    let (code, out, err) = rq_full(&db, &dir, &["Widget", "-J"], &[], None);
    assert_eq!(code, 0, "{out}");
    assert_eq!(source(&out), "live", "{out}");
    assert!(!err.contains("live scan"), "no note without -v: {err}");

    // -v names the scan and what it cost; --profile shows its span
    let (_, _, err) = rq_full(&db, &dir, &["Widget", "-J", "-v", "--profile"], &[], None);
    assert!(
        err.contains("answered from a live scan of") && err.contains("1 file in"),
        "-v notes the live answer: {err}"
    );
    assert!(err.contains("live scan: prefiltered"), "profiled: {err}");

    // a miss scans too, but answered nothing
    let (code, _, err) = rq_full(&db, &dir, &["Gizmo", "-J", "-v"], &[], None);
    assert_eq!(code, 1);
    assert!(
        !err.contains("answered from a live scan"),
        "a miss isn't a live answer: {err}"
    );

    // --usage counts the live answers apart
    let (_, usage, _) = rq_full(&db, &dir, &["--usage", "-J"], &[], None);
    let live: i64 = usage
        .lines()
        .filter_map(|l| serde_json::from_str::<serde_json::Value>(l).ok())
        .filter_map(|row| row["live"].as_i64())
        .sum();
    assert_eq!(live, 2, "two searches answered from a live scan: {usage}");

    // once indexed, the same query answers from the index
    rq(&db, &dir, &["--index"]);
    let (_, out, _) = rq_full(&db, &dir, &["Widget", "-J"], &[], None);
    assert_eq!(source(&out), "index", "{out}");

    let _ = fs::remove_dir_all(&dir);
}

#[test]
fn an_index_pass_whose_writes_fail_exits_instead_of_hanging() {
    // A failed batch write stops the consumer, but the parse workers and the
    // walk keep sending into bounded channels nobody drains: past one channel's
    // worth of files in flight, the pass parks at 0% CPU forever. A lock that
    // outlasts busy_timeout does this for real; a trigger makes it certain.
    let (dir, db) = scratch("sink-error");
    for i in 0..2500 {
        fs::write(
            dir.join(format!("w{i}.rb")),
            format!("def widget_{i}\nend\n"),
        )
        .unwrap();
    }
    git_init_commit(&dir);
    rq(&db, &dir, &["--status"]); // lay down the schema
    rusqlite::Connection::open(&db)
        .unwrap()
        .execute_batch(
            "CREATE TRIGGER fail_symbols BEFORE INSERT ON symbols \
             BEGIN SELECT RAISE(ABORT, 'injected'); END;",
        )
        .unwrap();

    let mut child = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["--index", "."])
        .current_dir(&dir)
        .env("RQ_DB", &db)
        .env("RQ_WARM_DETACH", "0")
        .env("RQ_JOBS", "1")
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .expect("run rq");
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
    let status = loop {
        if let Some(status) = child.try_wait().unwrap() {
            break Some(status);
        }
        if std::time::Instant::now() >= deadline {
            let _ = child.kill();
            let _ = child.wait();
            break None;
        }
        std::thread::sleep(std::time::Duration::from_millis(50));
    };
    let _ = fs::remove_dir_all(&dir);

    let status = status.expect("rq --index hung after a failed write");
    assert!(!status.success(), "a failed write is an error, not success");
}

#[test]
fn concurrent_first_queries_share_a_fresh_database() {
    // Every opener of a new database used to read user_version 0 and lay down
    // the schema itself, and the WAL switch ran before busy_timeout: some of a
    // burst failed with "table already exists" or "database is locked".
    let (dir, db) = scratch("fresh-burst");
    fs::write(dir.join("widget.rb"), "class Widget\nend\n").unwrap();
    git_init_commit(&dir);
    let runs: Vec<_> = (0..16)
        .map(|_| {
            Command::new(env!("CARGO_BIN_EXE_rq"))
                .args(["--no-wait", "--json", "Widget"])
                .current_dir(&dir)
                .env("RQ_DB", &db)
                .env("RQ_WARM_DETACH", "0")
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::piped())
                .spawn()
                .expect("run rq")
        })
        .collect();
    let failures: Vec<String> = runs
        .into_iter()
        .map(|c| c.wait_with_output().unwrap())
        .filter(|o| o.status.code() == Some(74))
        .map(|o| {
            String::from_utf8_lossy(&o.stderr)
                .lines()
                .next()
                .unwrap_or("")
                .to_string()
        })
        .collect();
    let _ = fs::remove_dir_all(&dir);
    assert!(failures.is_empty(), "{failures:?}");
}

#[test]
fn a_relative_rq_db_is_a_usage_error() {
    let (dir, _) = scratch("relative-db");
    let out = Command::new(env!("CARGO_BIN_EXE_rq"))
        .args(["Widget", "--json"])
        .current_dir(&dir)
        .env("RQ_DB", "dbs/rq.db")
        .env("RQ_WARM_DETACH", "0")
        .output()
        .expect("run rq");
    let created = dir.join("dbs").exists();
    let _ = fs::remove_dir_all(&dir);

    assert_eq!(out.status.code(), Some(64));
    let err: serde_json::Value = serde_json::from_slice(&out.stdout).expect("error json");
    assert_eq!(err["kind"], "usage");
    assert!(err["error"].as_str().unwrap().contains("RQ_DB"), "{err}");
    assert!(!created, "nothing is written beside the caller");
}

#[test]
fn an_empty_rq_db_means_the_default_and_a_directory_is_refused() {
    let (dir, _) = scratch("empty-db");
    let run = |db: &str| {
        Command::new(env!("CARGO_BIN_EXE_rq"))
            .args(["--status", "--json"])
            .current_dir(&dir)
            .env("RQ_DB", db)
            .env("HOME", &dir)
            .output()
            .expect("run rq")
    };
    let empty = run("");
    let default_made = dir.join(".local/share/rq/rq.db").exists();
    let slash = format!("{}/dbs/", dir.display());
    let trailing = run(&slash);
    let dir_made = dir.join("dbs").exists();
    let _ = fs::remove_dir_all(&dir);

    assert_eq!(empty.status.code(), Some(0));
    assert!(default_made, "the default path under HOME");
    assert_eq!(trailing.status.code(), Some(64));
    let err: serde_json::Value = serde_json::from_slice(&trailing.stdout).expect("error json");
    assert_eq!(err["kind"], "usage");
    assert!(!dir_made, "nothing is created for a refused path");
}