imessage-rs 0.1.0

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

// Global lock ensuring tests run serially even without --test-threads=1
static SERIAL: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

/// Declare a serial E2E test. Acquires a global lock so tests run one at a time
/// even without `--test-threads=1`.
macro_rules! e2e_test {
    (fn $name:ident() $body:block) => {
        #[test]
        #[ignore]
        fn $name() {
            let _lock = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
            $body
        }
    };
}

// ===========================================================================
// WebhookReceiver — in-process HTTP server that captures webhook POSTs
// ===========================================================================

struct WebhookReceiver {
    url: String,
    events: Arc<Mutex<Vec<Value>>>,
}

/// Lock a Mutex, recovering from poison (prior panic in another test).
fn lock_events(m: &Mutex<Vec<Value>>) -> std::sync::MutexGuard<'_, Vec<Value>> {
    m.lock().unwrap_or_else(|e| e.into_inner())
}

impl WebhookReceiver {
    fn start() -> Self {
        let listener =
            std::net::TcpListener::bind("127.0.0.1:0").expect("failed to bind webhook receiver");
        let port = listener.local_addr().unwrap().port();
        let url = format!("http://127.0.0.1:{port}");
        let events: Arc<Mutex<Vec<Value>>> = Arc::new(Mutex::new(Vec::new()));
        let events_clone = Arc::clone(&events);

        std::thread::spawn(move || {
            for stream in listener.incoming() {
                let Ok(mut stream) = stream else { continue };
                // Set a read timeout so threads don't hang forever
                let _ = stream.set_read_timeout(Some(std::time::Duration::from_secs(30)));
                let events = Arc::clone(&events_clone);

                std::thread::spawn(move || {
                    let mut reader = BufReader::new(stream.try_clone().unwrap());
                    let mut content_length: usize = 0;

                    // Read request line
                    let mut request_line = String::new();
                    if reader.read_line(&mut request_line).is_err() {
                        return;
                    }

                    // Read headers (case-insensitive content-length extraction)
                    loop {
                        let mut line = String::new();
                        match reader.read_line(&mut line) {
                            Ok(0) => break, // EOF
                            Err(_) => break,
                            _ => {}
                        }
                        if line == "\r\n" || line == "\n" {
                            break;
                        }
                        let lower = line.to_ascii_lowercase();
                        if let Some(val) = lower.strip_prefix("content-length:") {
                            content_length = val.trim().parse().unwrap_or(0);
                        }
                    }

                    // Read body
                    if content_length > 0 {
                        let mut body = vec![0u8; content_length];
                        if reader.read_exact(&mut body).is_ok() {
                            if let Ok(payload) = serde_json::from_slice::<Value>(&body) {
                                lock_events(&events).push(payload);
                            }
                        }
                    }

                    // Respond 200 OK with Connection: close to prevent keep-alive reuse
                    let response =
                        "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 0\r\n\r\n";
                    let _ = stream.write_all(response.as_bytes());
                });
            }
        });

        Self { url, events }
    }

    /// Take all collected events, clearing the buffer.
    fn drain(&self) -> Vec<Value> {
        std::mem::take(&mut *lock_events(&self.events))
    }

    /// Poll until an event with matching `type` and `data.guid` appears, or panic on timeout.
    fn wait_for_event_with_guid(
        &self,
        event_type: &str,
        guid: &str,
        timeout: std::time::Duration,
    ) -> Value {
        let start = std::time::Instant::now();
        loop {
            {
                let events = lock_events(&self.events);
                if let Some(ev) = events
                    .iter()
                    .find(|ev| ev["type"] == event_type && ev["data"]["guid"] == guid)
                {
                    return ev.clone();
                }
            }
            if start.elapsed() > timeout {
                let events = lock_events(&self.events);
                panic!(
                    "Timed out waiting for webhook event type={event_type} guid={guid}\n\
                     Received events: {events:?}"
                );
            }
            std::thread::sleep(std::time::Duration::from_millis(250));
        }
    }

    /// Poll until an event with matching `type` appears, or panic on timeout.
    fn wait_for_event(&self, event_type: &str, timeout: std::time::Duration) -> Value {
        let start = std::time::Instant::now();
        loop {
            {
                let events = lock_events(&self.events);
                if let Some(ev) = events.iter().find(|ev| ev["type"] == event_type) {
                    return ev.clone();
                }
            }
            if start.elapsed() > timeout {
                let events = lock_events(&self.events);
                panic!(
                    "Timed out waiting for webhook event type={event_type}\n\
                     Received events: {events:?}"
                );
            }
            std::thread::sleep(std::time::Duration::from_millis(250));
        }
    }
}

// ===========================================================================
// Auto-spawn server
// ===========================================================================

/// PID of auto-spawned server process, or 0 if not spawned.
static SERVER_PID: AtomicU32 = AtomicU32::new(0);

/// `atexit` handler: send SIGTERM to auto-spawned server.
extern "C" fn kill_server() {
    let pid = SERVER_PID.load(Ordering::SeqCst);
    if pid != 0 {
        eprintln!("[e2e] Stopping server (pid {pid})...");
        unsafe {
            libc::kill(pid as i32, libc::SIGTERM);
        }
    }
}

/// Check if anything is listening on the given port.
fn port_in_use(port: u16) -> bool {
    std::net::TcpStream::connect_timeout(
        &std::net::SocketAddr::from(([127, 0, 0, 1], port)),
        std::time::Duration::from_millis(500),
    )
    .is_ok()
}

/// Check if any `imessage-rs` process is already running (by PID file, then by process name).
/// Returns `Some(description)` if a conflict is detected.
fn detect_existing_server() -> Option<String> {
    // 1. Check the PID file the server writes on startup
    let pid_file = home::home_dir()
        .unwrap_or_else(|| std::path::PathBuf::from("/tmp"))
        .join("Library/Application Support/imessage-rs/.imessage-rs.pid");
    if let Ok(contents) = std::fs::read_to_string(&pid_file) {
        if let Ok(pid) = contents.trim().parse::<i32>() {
            if unsafe { libc::kill(pid, 0) } == 0 {
                return Some(format!(
                    "PID file {pid_file:?} points to live process {pid}"
                ));
            }
        }
    }

    // 2. Check for any running process named `imessage-rs` (catches servers started without PID file)
    if let Ok(output) = std::process::Command::new("pgrep")
        .args(["-x", "imessage-rs"])
        .output()
    {
        if output.status.success() {
            let pids = String::from_utf8_lossy(&output.stdout);
            let pids = pids.trim();
            if !pids.is_empty() {
                return Some(format!("imessage-rs process already running (pid {pids})"));
            }
        }
    }

    None
}

/// Spawn a fresh server binary and wait for it to become ready.
/// Returns the base URL. Panics if an existing server is detected.
fn spawn_server(password: &str, webhook_url: &str) -> String {
    let port: u16 = 1234;

    // Check for existing server — process-level check first (catches servers still starting up),
    // then port check as a fallback
    if let Some(conflict) = detect_existing_server() {
        panic!(
            "[e2e] Existing server detected: {conflict}\n\
             Two processes cannot hook into Messages.app simultaneously.\n\
             Either stop it, or set E2E_BASE to use it explicitly."
        );
    }
    if port_in_use(port) {
        panic!(
            "[e2e] Port {port} is already in use (but no imessage-rs process found).\n\
             Something else is bound to the port. Free it or set E2E_BASE."
        );
    }

    let binary = env!("CARGO_BIN_EXE_imessage-rs");
    eprintln!("[e2e] Spawning server: {binary}");
    let mut child = std::process::Command::new(binary)
        .args([
            "--password",
            password,
            "--enable-private-api",
            "true",
            "--enable-facetime-private-api",
            "true",
            "--enable-findmy-private-api",
            "true",
            "--markdown-to-formatting",
            "true",
            "--webhook",
            webhook_url,
        ])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::inherit())
        .spawn()
        .unwrap_or_else(|e| panic!("[e2e] Failed to spawn server: {e}"));

    let pid = child.id();
    SERVER_PID.store(pid, Ordering::SeqCst);
    unsafe {
        libc::atexit(kill_server);
    }

    // Wait for full readiness: HTTP up AND Private API fully initialized.
    // The dylib eagerly initializes IMCore singletons and sends a "ready" event,
    // which the server exposes as private_api_ready in /server/info.
    let base = format!("http://127.0.0.1:{port}/api/v1");
    let info_url = format!("{base}/server/info?password={password}");
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(180);
    eprintln!("[e2e] Waiting for server on port {port}...");
    let mut http_ready = false;
    let mut helper_connected = false;
    loop {
        match child.try_wait() {
            Ok(Some(status)) => panic!("[e2e] Server exited prematurely: {status}"),
            Err(e) => panic!("[e2e] Failed to check server status: {e}"),
            Ok(None) => {} // still running
        }
        if std::time::Instant::now() > deadline {
            unsafe {
                libc::kill(pid as i32, libc::SIGTERM);
            }
            panic!("[e2e] Server did not become ready within 180 seconds");
        }
        match reqwest::blocking::get(&info_url) {
            Ok(resp) if resp.status().is_success() => {
                if !http_ready {
                    eprintln!("[e2e] HTTP ready, waiting for Private API...");
                    http_ready = true;
                }
                if let Ok(json) = resp.json::<Value>() {
                    if !helper_connected && json["data"]["helper_connected"].as_bool() == Some(true)
                    {
                        eprintln!("[e2e] Helper connected, waiting for IMCore initialization...");
                        helper_connected = true;
                    }
                    if json["data"]["private_api_ready"].as_bool() == Some(true)
                        && json["data"]["findmy_private_api_ready"].as_bool() == Some(true)
                    {
                        break;
                    }
                }
            }
            _ => {}
        }
        std::thread::sleep(std::time::Duration::from_millis(500));
    }

    // Server is ready — leak the Child handle; atexit will SIGTERM by PID
    std::mem::forget(child);
    eprintln!("[e2e] Server ready (pid {pid})");
    base
}

// ===========================================================================
// Dynamic test configuration
// ===========================================================================

#[allow(dead_code)]
struct TestConfig {
    base: String,
    password: String,
    self_addr: String,
    self_chat: String,
    peers: Vec<String>,
    group_chat: String,
    webhook_receiver: WebhookReceiver,
}

static CONFIG: LazyLock<TestConfig> = LazyLock::new(|| {
    let password = std::env::var("E2E_PASSWORD").unwrap_or_else(|_| "test".to_string());

    // Start webhook receiver first — auto-spawn needs the URL as a CLI arg
    let webhook_receiver = WebhookReceiver::start();

    // If E2E_BASE is set, use the externally-managed server; otherwise auto-spawn
    let base = match std::env::var("E2E_BASE") {
        Ok(url) => {
            eprintln!("[e2e] Using externally-managed server: {url}");
            url
        }
        Err(_) => spawn_server(&password, &webhook_receiver.url),
    };

    let peers: Vec<String> = std::env::var("E2E_PEER_ADDRS")
        .expect("E2E_PEER_ADDRS required (comma-separated, min 2, e.g. a@x.com,b@x.com,c@x.com)")
        .split(',')
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();
    assert!(
        peers.len() >= 2,
        "E2E_PEER_ADDRS needs at least 2 addresses, got {}",
        peers.len()
    );

    // Fetch self address from server info
    let info_url = format!("{base}/server/info?password={password}");
    let resp: Value = reqwest::blocking::get(&info_url)
        .expect("Failed to connect to server")
        .json()
        .expect("Invalid JSON from /server/info");
    let self_addr = resp["data"]["detected_imessage"]
        .as_str()
        .expect("/server/info missing detected_imessage — is the server configured?")
        .to_string();
    assert!(!self_addr.is_empty(), "detected_imessage is empty");
    let self_chat = format!("iMessage;-;{self_addr}");

    // Find or create group chat
    let group_chat = find_or_create_group(&base, &password, &peers[0], &peers[1], &self_addr);

    // Seed the self-chat so tests that query it don't depend on pre-existing history.
    // Send a few messages to ensure minimum counts for pagination tests (need >= 6).
    let seed_url = format!("{base}/message/text?password={password}");
    let c = reqwest::blocking::Client::new();
    for i in 0..6 {
        let body = serde_json::json!({
            "chatGuid": &self_chat,
            "message": format!("E2E seed message {i}"),
            "method": "private-api",
        });
        let resp = c
            .post(&seed_url)
            .json(&body)
            .send()
            .expect("seed send failed");
        assert_eq!(resp.status(), 200, "seed message {i} failed");
    }
    // Let the watcher pick up all seed messages before tests start
    std::thread::sleep(std::time::Duration::from_secs(3));

    let server_pid = SERVER_PID.load(Ordering::SeqCst);
    eprintln!("=== E2E TestConfig ===");
    eprintln!(
        "  server:          {}",
        if server_pid != 0 {
            format!("auto-spawned (pid {server_pid})")
        } else {
            "external".to_string()
        }
    );
    eprintln!("  base:            {base}");
    eprintln!("  self_addr:       {self_addr}");
    eprintln!("  self_chat:       {self_chat}");
    eprintln!("  peers:           {peers:?}");
    eprintln!("  group_chat:      {group_chat}");
    eprintln!("  webhook_url:     {}", webhook_receiver.url);
    eprintln!("======================");

    TestConfig {
        base,
        password,
        self_addr,
        self_chat,
        peers,
        group_chat,
        webhook_receiver,
    }
});

/// Find an existing group chat containing exactly the two peers (and self), or create one.
fn find_or_create_group(
    base: &str,
    password: &str,
    peer: &str,
    group_peer: &str,
    self_addr: &str,
) -> String {
    let query_url = format!("{base}/chat/query?password={password}");
    let body = json!({
        "limit": 1000,
        "with": ["participants"],
    });
    let resp = reqwest::blocking::Client::new()
        .post(&query_url)
        .json(&body)
        .send()
        .expect("Failed to query chats");
    let json: Value = resp.json().expect("Invalid JSON from /chat/query");
    let chats = json["data"].as_array().expect("/chat/query data not array");

    // Find a group chat (style == 43) with exactly the expected members.
    // Participants may or may not include self, so accept groups where every
    // participant is one of {peer, group_peer, self} and both peers are present.
    let is_expected = |addr: &str| {
        addr.eq_ignore_ascii_case(peer)
            || addr.eq_ignore_ascii_case(group_peer)
            || addr.eq_ignore_ascii_case(self_addr)
    };
    for chat in chats {
        if chat["style"].as_i64() != Some(43) {
            continue;
        }
        let participants = match chat["participants"].as_array() {
            Some(p) => p,
            None => continue,
        };
        let addrs: Vec<&str> = participants
            .iter()
            .filter_map(|p| p["address"].as_str())
            .collect();
        if addrs.iter().all(|a| is_expected(a))
            && addrs.iter().any(|a| a.eq_ignore_ascii_case(peer))
            && addrs.iter().any(|a| a.eq_ignore_ascii_case(group_peer))
        {
            let guid = chat["guid"].as_str().unwrap().to_string();
            eprintln!("  Found existing group chat: {guid}");
            return guid;
        }
    }

    // No existing group found — create one via Private API
    eprintln!("  No existing group chat found, creating one...");
    let create_url = format!("{base}/chat/new?password={password}");
    let create_body = json!({
        "addresses": [peer, group_peer],
        "message": "E2E test group setup",
        "method": "private-api",
    });
    let resp = reqwest::blocking::Client::new()
        .post(&create_url)
        .json(&create_body)
        .send()
        .expect("Failed to create group chat");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON from /chat/new");
    assert_eq!(status, 200, "Failed to create group chat: {json}");
    let guid = json["data"]["guid"]
        .as_str()
        .expect("Created group chat missing guid")
        .to_string();
    eprintln!("  Created new group chat: {guid}");

    // Wait for chat to settle in DB
    std::thread::sleep(std::time::Duration::from_secs(3));
    guid
}

// ===========================================================================
// Helpers
// ===========================================================================

fn url(path: &str) -> String {
    let base = &CONFIG.base;
    let pw = &CONFIG.password;
    if path.contains('?') {
        format!("{base}/{path}&password={pw}")
    } else {
        format!("{base}/{path}?password={pw}")
    }
}

fn run_id() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs()
}

fn client() -> reqwest::blocking::Client {
    reqwest::blocking::Client::new()
}

/// Extract the specific error detail from an error response envelope.
/// Error responses use: `{ "error": { "type": "...", "message": "<detail>" } }`.
fn error_detail(json: &Value) -> &str {
    json["error"]["message"].as_str().unwrap_or("")
}

fn get_json(path: &str) -> Value {
    let resp = reqwest::blocking::get(url(path)).expect("HTTP GET failed");
    assert_eq!(resp.status(), 200, "GET {path} returned {}", resp.status());
    resp.json().expect("Invalid JSON")
}

fn get_raw(path: &str) -> reqwest::blocking::Response {
    reqwest::blocking::get(url(path)).expect("HTTP GET failed")
}

fn post_json(path: &str, body: &Value) -> (u16, Value) {
    let resp = client()
        .post(url(path))
        .json(body)
        .send()
        .expect("HTTP POST failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    (status, json)
}

fn put_json(path: &str, body: &Value) -> (u16, Value) {
    let resp = client()
        .put(url(path))
        .json(body)
        .send()
        .expect("HTTP PUT failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    (status, json)
}

fn delete_no_body(path: &str) -> (u16, Value) {
    let resp = client()
        .delete(url(path))
        .send()
        .expect("HTTP DELETE failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    (status, json)
}

/// A minimal valid 1x1 red PNG for attachment/icon tests.
fn test_png() -> Vec<u8> {
    vec![
        0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, // PNG signature
        0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, // IHDR chunk
        0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77,
        0x53, 0xde, 0x00, 0x00, 0x00, 0x0c, 0x49, 0x44, 0x41, // IDAT chunk
        0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf, 0xc0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0xe2, 0x21,
        0xbc, 0x33, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, // IEND chunk
        0x44, 0xae, 0x42, 0x60, 0x82,
    ]
}

/// Send a test PNG attachment and return the message GUID.
/// Used by tests that need a known attachment in the DB.
fn send_test_attachment() -> String {
    let id = run_id();
    let form = reqwest::blocking::multipart::Form::new()
        .text("chatGuid", CONFIG.self_chat.clone())
        .text("name", format!("rust-e2e-setup-{id}.png"))
        .text("method", "private-api")
        .part(
            "attachment",
            reqwest::blocking::multipart::Part::bytes(test_png())
                .file_name(format!("rust-e2e-setup-{id}.png"))
                .mime_str("image/png")
                .unwrap(),
        );
    let resp = client()
        .post(url("message/attachment"))
        .multipart(form)
        .send()
        .expect("send_test_attachment POST failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    assert_eq!(status, 200, "send_test_attachment failed: {json}");
    json["data"]["guid"].as_str().unwrap().to_string()
}

// ===========================================================================
// Health & Info
// ===========================================================================

e2e_test! {
fn e2e_ping() {
    let json = get_json("ping");
    assert_eq!(json["status"], 200);
    assert_eq!(json["data"], "pong");
}
}

e2e_test! {
fn e2e_server_info() {
    let json = get_json("server/info");
    let data = &json["data"];
    assert_eq!(data["private_api"], true);
    assert_eq!(data["helper_connected"], true);
    // Dynamic: just verify both fields are non-empty strings
    assert!(
        data["detected_icloud"].as_str().map_or(false, |s| !s.is_empty()),
        "detected_icloud should be a non-empty string"
    );
    assert!(
        data["detected_imessage"].as_str().map_or(false, |s| !s.is_empty()),
        "detected_imessage should be a non-empty string"
    );
    assert!(data["server_version"].is_string());
    assert!(data["os_version"].is_string());
}
}

e2e_test! {
fn e2e_auth_rejection() {
    let resp = reqwest::blocking::get(format!("{}/ping", CONFIG.base)).expect("HTTP GET failed");
    assert_eq!(resp.status(), 401);
}
}

// ===========================================================================
// Server routes (logs, permissions, update check, restart/soft, alerts)
// ===========================================================================

e2e_test! {
fn e2e_server_logs() {
    let json = get_json("server/logs?count=5");
    assert!(json["data"].is_string(), "logs data should be a string");
}
}

e2e_test! {
fn e2e_server_permissions() {
    let json = get_json("server/permissions");
    let data = json["data"].as_array().expect("permissions should be array");
    assert_eq!(data.len(), 4, "should have 4 permission entries");
    for perm in data {
        assert!(perm["name"].is_string(), "permission should have name");
        assert!(perm["pass"].is_boolean(), "permission should have pass");
        assert!(perm["solution"].is_string(), "permission should have solution");
    }
}
}

// ===========================================================================
// Statistics
// ===========================================================================

e2e_test! {
fn e2e_statistics_totals() {
    let json = get_json("server/statistics/totals");
    let data = &json["data"];
    assert!(data["handles"].is_u64());
    assert!(data["messages"].is_u64());
    assert!(data["chats"].is_u64());
    assert!(data["attachments"].is_u64());
}
}

e2e_test! {
fn e2e_statistics_media() {
    let json = get_json("server/statistics/media");
    let data = &json["data"];
    assert!(data["images"].is_u64());
    assert!(data["videos"].is_u64());
}
}

// ===========================================================================
// Messages (read-only)
// ===========================================================================

e2e_test! {
fn e2e_message_count() {
    let json = get_json("message/count");
    assert!(json["data"]["total"].is_u64());
}
}

e2e_test! {
fn e2e_message_count_updated() {
    let json = get_json("message/count/updated?after=0");
    assert!(json["data"]["total"].is_u64());
}
}

e2e_test! {
fn e2e_message_count_me() {
    let json = get_json("message/count/me");
    assert!(json["data"]["total"].is_u64());
}
}

e2e_test! {
fn e2e_message_query() {
    let body = json!({
        "limit": 3,
        "sort": "DESC",
        "with": ["chat", "chats", "attachment", "attachments"]
    });
    let (status, json) = post_json("message/query", &body);
    assert_eq!(status, 200);
    let data = json["data"].as_array().expect("data should be an array");
    assert!(!data.is_empty(), "should have at least one message");
    assert!(data.len() <= 3, "should respect limit=3, got {}", data.len());
    assert_eq!(json["metadata"]["limit"], 3);
    assert!(json["metadata"]["total"].is_u64(), "metadata should have total");
    assert!(json["metadata"]["offset"].is_u64(), "metadata should have offset");

    let msg = &data[0];
    assert!(msg["guid"].is_string());
    assert!(msg["originalROWID"].is_u64());
    assert!(msg["isFromMe"].is_boolean());
    assert!(msg["dateCreated"].is_u64());
    assert!(msg["attachments"].is_array());
    assert!(msg["chats"].is_array());

    // Verify DESC sort order if multiple messages returned
    if data.len() >= 2 {
        let d0 = data[0]["dateCreated"].as_u64().unwrap_or(0);
        let d1 = data[1]["dateCreated"].as_u64().unwrap_or(0);
        assert!(d0 >= d1, "messages should be in DESC order: {d0} < {d1}");
    }
}
}

e2e_test! {
fn e2e_message_serialization_fields() {
    let body = json!({"limit": 1, "sort": "DESC"});
    let (_, json) = post_json("message/query", &body);
    let msg = &json["data"][0];

    let expected_fields = [
        "originalROWID", "guid", "text", "attributedBody", "handle", "handleId",
        "otherHandle", "attachments", "subject", "error", "dateCreated", "dateRead",
        "dateDelivered", "isDelivered", "isFromMe", "hasDdResults", "isArchived",
        "itemType", "groupTitle", "groupActionType", "balloonBundleId",
        "associatedMessageGuid", "associatedMessageType", "expressiveSendStyleId",
        "threadOriginatorGuid", "hasPayloadData", "country", "isDelayed",
        "isAutoReply", "isSystemMessage", "isServiceMessage", "isForward",
        "threadOriginatorPart", "isCorrupt", "datePlayed", "cacheRoomnames",
        "isSpam", "isExpired", "timeExpressiveSendPlayed", "isAudioMessage",
        "replyToGuid", "shareStatus", "shareDirection", "wasDeliveredQuietly",
        "didNotifyRecipient", "chats", "messageSummaryInfo", "payloadData",
        "dateEdited", "dateRetracted", "partCount",
    ];

    let obj = msg.as_object().expect("message should be an object");
    for field in &expected_fields {
        assert!(obj.contains_key(*field), "missing field: {field}");
    }
}
}

// ===========================================================================
// Handles
// ===========================================================================

e2e_test! {
fn e2e_handle_count() {
    let json = get_json("handle/count");
    assert!(json["data"]["total"].is_u64());
}
}

e2e_test! {
fn e2e_handle_imessage_availability() {
    let addr = &CONFIG.peers[0];

    // GET
    let json = get_json(&format!("handle/availability/imessage?address={addr}"));
    assert!(json["data"]["available"].is_boolean());

    // POST
    let (status, json) = post_json("handle/availability/imessage", &json!({"address": addr}));
    assert_eq!(status, 200);
    assert!(json["data"]["available"].is_boolean());
}
}

e2e_test! {
fn e2e_handle_query() {
    let body = json!({"limit": 3});
    let (status, json) = post_json("handle/query", &body);
    assert_eq!(status, 200);
    let handles = json["data"].as_array().expect("data should be an array");
    assert!(!handles.is_empty(), "should have at least one handle");
    assert!(handles.len() <= 3, "should respect limit=3, got {}", handles.len());
    assert!(json["metadata"]["total"].is_u64());
    assert!(json["metadata"]["offset"].is_u64(), "metadata should have offset");
    // Verify handle shape
    let h = &handles[0];
    assert!(h["address"].is_string());
    assert!(h["service"].is_string());
    assert!(h["originalROWID"].is_u64());
}
}

e2e_test! {
fn e2e_handle_find() {
    let json = get_json(&format!("handle/{}", CONFIG.peers[0]));
    let data = &json["data"];
    assert!(data["address"].is_string());
    assert_eq!(
        data["address"].as_str().unwrap().to_lowercase(),
        CONFIG.peers[0].to_lowercase(),
        "returned address should match queried address"
    );
    assert!(data["originalROWID"].is_u64());
    assert!(data["service"].is_string(), "handle should have service");
}
}

e2e_test! {
fn e2e_handle_query_with_chats() {
    let body = json!({"limit": 3, "with": ["chats"]});
    let (status, json) = post_json("handle/query", &body);
    assert_eq!(status, 200);
    let handles = json["data"].as_array().unwrap();
    assert!(!handles.is_empty());
    // Each handle should have a "chats" array
    assert!(
        handles[0]["chats"].is_array(),
        "handle should have chats array when with=[chats]"
    );
}
}

e2e_test! {
fn e2e_handle_facetime_availability() {
    let addr = &CONFIG.peers[0];

    // GET
    let json = get_json(&format!("handle/availability/facetime?address={addr}"));
    assert!(json["data"]["available"].is_boolean());

    // POST
    let (status, json) = post_json("handle/availability/facetime", &json!({"address": addr}));
    assert_eq!(status, 200);
    assert!(json["data"]["available"].is_boolean());
}
}

e2e_test! {
fn e2e_handle_focus_status() {
    let json = get_json(&format!("handle/{}/focus", CONFIG.peers[0]));
    let status = json["data"]["status"].as_str().expect("focus status should have status string");
    assert!(
        status == "none" || status == "silenced" || status == "unknown",
        "focus status should be none, silenced, or unknown, got: {status}"
    );
}
}

// ===========================================================================
// Chats (1:1)
// ===========================================================================

e2e_test! {
fn e2e_chat_count() {
    let json = get_json("chat/count");
    assert!(json["data"]["total"].is_u64());
}
}

e2e_test! {
fn e2e_chat_query() {
    let body = json!({"limit": 3, "sort": "lastmessage", "with": ["lastMessage"]});
    let (status, json) = post_json("chat/query", &body);
    assert_eq!(status, 200);
    let chats = json["data"].as_array().expect("data should be an array");
    assert!(!chats.is_empty(), "should have at least one chat");
    assert!(chats.len() <= 3, "should respect limit=3, got {}", chats.len());
    // Verify first chat has expected fields
    let chat = &chats[0];
    assert!(chat["guid"].is_string(), "chat should have guid");
    assert!(chat["style"].is_u64(), "chat should have style");
}
}

e2e_test! {
fn e2e_chat_query_with_last_message() {
    let body = json!({
        "limit": 5,
        "sort": "lastmessage",
        "with": ["lastMessage"]
    });
    let (status, json) = post_json("chat/query", &body);
    assert_eq!(status, 200);
    let chats = json["data"].as_array().unwrap();
    assert!(!chats.is_empty(), "should have at least one chat");
    assert!(chats.len() <= 5, "should respect limit=5, got {}", chats.len());
    // All chats should have a lastMessage field
    for (i, chat) in chats.iter().enumerate() {
        assert!(
            chat.get("lastMessage").is_some(),
            "chat[{i}] should have lastMessage when with=[lastMessage]"
        );
    }
    // Verify sort order (descending by lastMessage date)
    if chats.len() >= 2 {
        let d0 = chats[0]["lastMessage"]["dateCreated"].as_u64().unwrap_or(0);
        let d1 = chats[1]["lastMessage"]["dateCreated"].as_u64().unwrap_or(0);
        assert!(d0 >= d1, "chats should be sorted by lastMessage DESC: {d0} < {d1}");
    }
}
}

e2e_test! {
fn e2e_chat_find() {
    // Uses iMessage prefix; on Tahoe the server normalizes to any;
    let json = get_json(&format!("chat/{}", CONFIG.self_chat));
    assert!(json["data"]["guid"].is_string());
}
}

e2e_test! {
fn e2e_chat_messages() {
    let json = get_json(&format!("chat/{}/message?limit=5&sort=DESC", CONFIG.self_chat));
    let msgs = json["data"].as_array().expect("data should be an array");
    assert!(!msgs.is_empty(), "self-chat should have messages");
    assert!(msgs.len() <= 5, "should respect limit=5, got {}", msgs.len());
    // Verify message shape
    let msg = &msgs[0];
    assert!(msg["guid"].is_string(), "message should have guid");
    assert!(msg["dateCreated"].is_u64(), "message should have dateCreated");
    assert!(msg["isFromMe"].is_boolean(), "message should have isFromMe");
    // Verify DESC sort order
    if msgs.len() >= 2 {
        let d0 = msgs[0]["dateCreated"].as_u64().unwrap_or(0);
        let d1 = msgs[1]["dateCreated"].as_u64().unwrap_or(0);
        assert!(d0 >= d1, "messages should be in DESC order: {d0} < {d1}");
    }
}
}

// ===========================================================================
// Private API: Chat operations (1:1)
// ===========================================================================

e2e_test! {
fn e2e_chat_mark_read() {
    let resp = client()
        .post(url(&format!("chat/{}/read", CONFIG.self_chat)))
        .send()
        .expect("POST failed");
    assert_eq!(resp.status(), 200);
}
}

e2e_test! {
fn e2e_chat_mark_unread() {
    let resp = client()
        .post(url(&format!("chat/{}/unread", CONFIG.self_chat)))
        .send()
        .expect("POST failed");
    assert_eq!(resp.status(), 200);
}
}

e2e_test! {
fn e2e_chat_typing() {
    // Start typing
    let resp = client()
        .post(url(&format!("chat/{}/typing", CONFIG.self_chat)))
        .send()
        .unwrap();
    assert_eq!(resp.status(), 200);

    // Stop typing
    let resp = client()
        .delete(url(&format!("chat/{}/typing", CONFIG.self_chat)))
        .send()
        .unwrap();
    assert_eq!(resp.status(), 200);
}
}

// ===========================================================================
// Private API: Send message (core test)
// ===========================================================================

e2e_test! {
fn e2e_send_text_private_api() {
    let id = run_id();
    CONFIG.webhook_receiver.drain(); // clear stale events

    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Rust E2E #{id}: Private API"),
        "method": "private-api",
        "tempGuid": format!("temp-rust-{id}")
    });

    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 200, "send failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert_eq!(json["data"]["tempGuid"], format!("temp-rust-{id}"));
    assert!(json["data"]["guid"].is_string());
    assert!(json["data"]["dateCreated"].is_u64());
    assert_eq!(json["data"]["error"], 0);

    // Verify message appears in DB
    let guid = json["data"]["guid"].as_str().unwrap();
    std::thread::sleep(std::time::Duration::from_secs(2));

    let found = get_json(&format!("message/{guid}?with=chat,attachment"));
    assert_eq!(found["data"]["guid"], guid);
    assert_eq!(found["data"]["isFromMe"], true);

    // Verify webhook delivery
    let wh = CONFIG
        .webhook_receiver
        .wait_for_event_with_guid("new-message", guid, std::time::Duration::from_secs(10));
    assert_eq!(wh["type"], "new-message");
    assert_eq!(wh["data"]["guid"], guid);
}
}

// ===========================================================================
// AppleScript: Send text and attachment
// ===========================================================================

e2e_test! {
fn e2e_send_text_applescript() {
    let id = run_id();

    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Rust E2E #{id}: AppleScript"),
        "method": "apple-script",
        "tempGuid": format!("temp-as-{id}")
    });

    let resp = client()
        .post(url("message/text"))
        .json(&body)
        .timeout(std::time::Duration::from_secs(45))
        .send()
        .expect("HTTP POST failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    assert_eq!(status, 200, "AppleScript send failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(json["data"]["guid"].is_string());
    assert_eq!(json["data"]["error"], 0);
}
}

e2e_test! {
fn e2e_send_attachment_applescript() {
    let id = run_id();

    let form = reqwest::blocking::multipart::Form::new()
        .text("chatGuid", CONFIG.self_chat.clone())
        .text("name", format!("rust-e2e-as-{id}.png"))
        .text("method", "apple-script")
        .text("tempGuid", format!("temp-as-att-{id}"))
        .part(
            "attachment",
            reqwest::blocking::multipart::Part::bytes(test_png())
                .file_name(format!("rust-e2e-as-{id}.png"))
                .mime_str("image/png")
                .unwrap(),
        );

    let resp = client()
        .post(url("message/attachment"))
        .multipart(form)
        .timeout(std::time::Duration::from_secs(45))
        .send()
        .expect("POST failed");

    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    assert_eq!(status, 200, "AppleScript attachment send failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(json["data"]["guid"].is_string());
    assert_eq!(json["data"]["error"], 0);
    // tempGuid should be echoed back in the response
    assert_eq!(
        json["data"]["tempGuid"],
        format!("temp-as-att-{id}"),
        "tempGuid should be echoed in response"
    );
    // Attachments array should be present
    assert!(
        json["data"]["attachments"].is_array(),
        "response should include attachments array"
    );
}
}

// ===========================================================================
// Private API: Send attachment
// ===========================================================================

e2e_test! {
fn e2e_send_attachment_private_api() {
    let id = run_id();

    let form = reqwest::blocking::multipart::Form::new()
        .text("chatGuid", CONFIG.self_chat.clone())
        .text("name", format!("rust-e2e-{id}.png"))
        .text("method", "private-api")
        .text("tempGuid", format!("temp-att-{id}"))
        .part(
            "attachment",
            reqwest::blocking::multipart::Part::bytes(test_png())
                .file_name(format!("rust-e2e-{id}.png"))
                .mime_str("image/png")
                .unwrap(),
        );

    let resp = client()
        .post(url("message/attachment"))
        .multipart(form)
        .send()
        .expect("POST failed");

    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    assert_eq!(status, 200, "attachment send failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(json["data"]["guid"].is_string());
    assert_eq!(json["data"]["error"], 0);
    // tempGuid should be echoed back in the response
    assert_eq!(
        json["data"]["tempGuid"],
        format!("temp-att-{id}"),
        "tempGuid should be echoed in response"
    );
    // Attachments array should be present
    assert!(
        json["data"]["attachments"].is_array(),
        "response should include attachments array"
    );
}
}

// ===========================================================================
// Private API: Chunked attachment upload
// ===========================================================================

e2e_test! {
fn e2e_send_attachment_chunk() {
    let id = run_id();
    let att_guid = format!("e2e-chunk-{id}");
    let png = test_png();
    let mid = png.len() / 2;
    let chunk0 = png[..mid].to_vec();
    let chunk1 = png[mid..].to_vec();

    // Upload chunk 0 (non-final)
    let form0 = reqwest::blocking::multipart::Form::new()
        .text("chatGuid", CONFIG.self_chat.clone())
        .text("attachmentGuid", att_guid.clone())
        .text("name", format!("rust-chunk-{id}.png"))
        .text("chunkIndex", "0")
        .text("totalChunks", "2")
        .text("isComplete", "false")
        .text("method", "private-api")
        .part(
            "chunk",
            reqwest::blocking::multipart::Part::bytes(chunk0)
                .file_name("chunk0")
                .mime_str("application/octet-stream")
                .unwrap(),
        );

    let resp = client()
        .post(url("message/attachment/chunk"))
        .multipart(form0)
        .send()
        .expect("chunk 0 POST failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    assert_eq!(status, 200, "chunk 0 upload failed: {json}");
    assert_eq!(json["data"]["chunkIndex"], 0);
    assert_eq!(json["data"]["remainingChunks"], 1);

    // Upload chunk 1 (final, triggers assembly + send)
    let form1 = reqwest::blocking::multipart::Form::new()
        .text("chatGuid", CONFIG.self_chat.clone())
        .text("attachmentGuid", att_guid.clone())
        .text("name", format!("rust-chunk-{id}.png"))
        .text("chunkIndex", "1")
        .text("totalChunks", "2")
        .text("isComplete", "true")
        .text("method", "private-api")
        .part(
            "chunk",
            reqwest::blocking::multipart::Part::bytes(chunk1)
                .file_name("chunk1")
                .mime_str("application/octet-stream")
                .unwrap(),
        );

    let resp = client()
        .post(url("message/attachment/chunk"))
        .multipart(form1)
        .send()
        .expect("chunk 1 POST failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    assert_eq!(status, 200, "chunk 1 (final) failed: {json}");
    // Final chunk returns the sent message
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(json["data"]["guid"].is_string());
}
}

// ===========================================================================
// Private API: Reactions
// ===========================================================================

e2e_test! {
fn e2e_send_reaction() {
    let id = run_id();
    CONFIG.webhook_receiver.drain();

    let send_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("React target #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &send_body);
    assert_eq!(status, 200, "send failed: {send_json}");
    let target_guid = send_json["data"]["guid"].as_str().unwrap().to_string();

    // Wait for the watcher to detect the new message BEFORE reacting.
    // This ensures the watcher has the message in its seen_guids cache,
    // so the reaction will be detected as "updated-message" (not "new-message").
    CONFIG.webhook_receiver.wait_for_event_with_guid(
        "new-message",
        &target_guid,
        std::time::Duration::from_secs(15),
    );
    CONFIG.webhook_receiver.drain();

    // Send a "love" reaction
    let react_body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": target_guid,
        "reaction": "love",
    });
    let (status, json) = post_json("message/react", &react_body);
    assert_eq!(status, 200, "reaction failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(json["data"]["associatedMessageGuid"].is_string());

    // Verify webhook delivery — reaction is a separate new-message with associatedMessageGuid
    // We need to find the specific new-message whose associatedMessageGuid references the target,
    // since other new-message events (e.g., the reaction response from ourselves) may arrive too.
    {
        let timeout = std::time::Duration::from_secs(15);
        let start = std::time::Instant::now();
        loop {
            {
                let events = lock_events(&CONFIG.webhook_receiver.events);
                if let Some(ev) = events.iter().find(|ev| {
                    ev["type"] == "new-message"
                        && ev["data"]["associatedMessageGuid"]
                            .as_str()
                            .map_or(false, |g| g.contains(&target_guid))
                }) {
                    assert_eq!(ev["type"], "new-message");
                    break;
                }
            }
            if start.elapsed() > timeout {
                let events = lock_events(&CONFIG.webhook_receiver.events);
                panic!(
                    "Timed out waiting for reaction new-message referencing {target_guid}\n\
                     Received events: {events:?}"
                );
            }
            std::thread::sleep(std::time::Duration::from_millis(250));
        }
    }

    std::thread::sleep(std::time::Duration::from_secs(2));

    // Remove the reaction
    let unreact_body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": target_guid,
        "reaction": "-love",
    });
    let (status, json) = post_json("message/react", &unreact_body);
    assert_eq!(status, 200, "unreaction failed: {json}");
}
}

e2e_test! {
fn e2e_send_emoji_reaction() {
    let id = run_id();
    CONFIG.webhook_receiver.drain();

    // Send a target message
    let send_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Emoji react target #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &send_body);
    assert_eq!(status, 200, "send failed: {send_json}");
    let target_guid = send_json["data"]["guid"].as_str().unwrap().to_string();

    CONFIG.webhook_receiver.wait_for_event_with_guid(
        "new-message",
        &target_guid,
        std::time::Duration::from_secs(15),
    );
    CONFIG.webhook_receiver.drain();

    // Send an emoji reaction
    let react_body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": target_guid,
        "reaction": "\u{1F440}", // 👀
    });
    let (status, json) = post_json("message/react", &react_body);
    assert_eq!(status, 200, "emoji reaction failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(json["data"]["associatedMessageGuid"].is_string());
    assert_eq!(
        json["data"]["associatedMessageType"], "emoji",
        "associatedMessageType should be 'emoji': {json}"
    );
    assert_eq!(
        json["data"]["associatedMessageEmoji"], "\u{1F440}",
        "associatedMessageEmoji should be the emoji: {json}"
    );

    // Verify the reaction is readable via GET
    let react_guid = json["data"]["guid"].as_str().unwrap();
    let fetched = get_json(&format!("message/{react_guid}"));
    assert_eq!(fetched["data"]["associatedMessageType"], "emoji");
    assert_eq!(fetched["data"]["associatedMessageEmoji"], "\u{1F440}");

    std::thread::sleep(std::time::Duration::from_secs(2));

    // Remove the emoji reaction
    let unreact_body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": target_guid,
        "reaction": "-\u{1F440}", // -👀
    });
    let (status, json) = post_json("message/react", &unreact_body);
    assert_eq!(status, 200, "emoji unreaction failed: {json}");
    assert_eq!(
        json["data"]["associatedMessageType"], "-emoji",
        "removal associatedMessageType should be '-emoji': {json}"
    );
}
}

e2e_test! {
fn e2e_send_emoji_reaction_zwj() {
    let id = run_id();
    CONFIG.webhook_receiver.drain();

    // Send a target message
    let send_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("ZWJ emoji target #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &send_body);
    assert_eq!(status, 200, "send failed: {send_json}");
    let target_guid = send_json["data"]["guid"].as_str().unwrap().to_string();

    CONFIG.webhook_receiver.wait_for_event_with_guid(
        "new-message",
        &target_guid,
        std::time::Duration::from_secs(15),
    );
    CONFIG.webhook_receiver.drain();

    // Send a ZWJ sequence emoji (woman technologist: 👩‍💻)
    let react_body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": target_guid,
        "reaction": "\u{1F469}\u{200D}\u{1F4BB}",
    });
    let (status, json) = post_json("message/react", &react_body);
    assert_eq!(status, 200, "ZWJ emoji reaction failed: {json}");
    assert_eq!(json["data"]["associatedMessageType"], "emoji");
    assert_eq!(json["data"]["associatedMessageEmoji"], "\u{1F469}\u{200D}\u{1F4BB}");
}
}

e2e_test! {
fn e2e_send_sticker_reaction() {
    let id = run_id();
    CONFIG.webhook_receiver.drain();

    // Send a target message
    let send_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Sticker react target #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &send_body);
    assert_eq!(status, 200, "send failed: {send_json}");
    let target_guid = send_json["data"]["guid"].as_str().unwrap().to_string();

    CONFIG.webhook_receiver.wait_for_event_with_guid(
        "new-message",
        &target_guid,
        std::time::Duration::from_secs(15),
    );
    CONFIG.webhook_receiver.drain();

    // Minimal 1x1 red PNG (base64)
    let tiny_png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==";
    let data_url = format!("data:image/png;base64,{tiny_png}");

    // Send a sticker reaction
    let react_body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": target_guid,
        "reaction": "sticker",
        "sticker": data_url,
    });
    let (status, json) = post_json("message/react", &react_body);
    assert_eq!(status, 200, "sticker reaction failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert_eq!(
        json["data"]["associatedMessageType"], "sticker-tapback",
        "associatedMessageType should be 'sticker-tapback': {json}"
    );

    // Verify the sticker reaction is readable via GET
    let react_guid = json["data"]["guid"].as_str().unwrap();
    let fetched = get_json(&format!("message/{react_guid}"));
    assert_eq!(fetched["data"]["associatedMessageType"], "sticker-tapback");

    std::thread::sleep(std::time::Duration::from_secs(2));

    // Remove the sticker reaction
    let unreact_body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": target_guid,
        "reaction": "-sticker",
    });
    let (status, json) = post_json("message/react", &unreact_body);
    assert_eq!(status, 200, "sticker unreaction failed: {json}");
    assert_eq!(
        json["data"]["associatedMessageType"], "-sticker-tapback",
        "removal associatedMessageType should be '-sticker-tapback': {json}"
    );
}
}

// ===========================================================================
// Private API: Edit message
// ===========================================================================

e2e_test! {
fn e2e_edit_message() {
    let id = run_id();
    CONFIG.webhook_receiver.drain();

    let send_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Original #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &send_body);
    assert_eq!(status, 200);
    let guid = send_json["data"]["guid"].as_str().unwrap().to_string();

    // Wait for the watcher to detect the new message BEFORE editing.
    // This ensures the watcher has the message in its seen_guids cache,
    // so the edit will be detected as "updated-message" (not "new-message").
    CONFIG.webhook_receiver.wait_for_event_with_guid(
        "new-message",
        &guid,
        std::time::Duration::from_secs(15),
    );
    CONFIG.webhook_receiver.drain();

    let edit_body = json!({
        "editedMessage": format!("Edited #{id}"),
        "backwardsCompatibilityMessage": format!("Edited to \"Edited #{id}\""),
    });
    let (status, json) = post_json(&format!("message/{guid}/edit"), &edit_body);
    assert_eq!(status, 200, "edit failed: {json}");
    assert!(
        json["data"]["dateEdited"].is_u64(),
        "dateEdited should be set after edit"
    );

    // Re-fetch the message and verify the edited text is present
    std::thread::sleep(std::time::Duration::from_secs(2));
    let refetch = get_json(&format!("message/{guid}"));
    let text = refetch["data"]["text"].as_str().unwrap_or("");
    assert!(
        text.contains(&format!("Edited #{id}")),
        "re-fetched message text should contain edited content, got: {text}"
    );

    // Verify webhook delivery for the edit
    let wh = CONFIG.webhook_receiver.wait_for_event_with_guid(
        "updated-message",
        &guid,
        std::time::Duration::from_secs(15),
    );
    assert_eq!(wh["type"], "updated-message");
    assert_eq!(wh["data"]["guid"], guid);
}
}

// ===========================================================================
// Private API: Unsend message
// ===========================================================================

e2e_test! {
fn e2e_unsend_message() {
    let id = run_id();
    CONFIG.webhook_receiver.drain();

    let send_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("To be unsent #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &send_body);
    assert_eq!(status, 200);
    let guid = send_json["data"]["guid"].as_str().unwrap().to_string();

    // Wait for the watcher to detect the new message BEFORE unsending.
    // This ensures the watcher has the message in its seen_guids cache,
    // so the unsend will be detected as "updated-message" (not "new-message").
    CONFIG.webhook_receiver.wait_for_event_with_guid(
        "new-message",
        &guid,
        std::time::Duration::from_secs(15),
    );
    CONFIG.webhook_receiver.drain();

    let (status, json) = post_json(&format!("message/{guid}/unsend"), &json!({}));
    assert_eq!(status, 200, "unsend failed: {json}");
    // On macOS Tahoe, unsending a self-message sets dateEdited (not dateRetracted)
    assert!(
        json["data"]["dateEdited"].is_u64() || json["data"]["dateRetracted"].is_u64(),
        "dateEdited or dateRetracted should be set after unsend"
    );

    // Re-fetch and verify the message shows as retracted/edited
    std::thread::sleep(std::time::Duration::from_secs(2));
    let refetch = get_json(&format!("message/{guid}"));
    assert!(
        refetch["data"]["dateEdited"].is_u64() || refetch["data"]["dateRetracted"].is_u64(),
        "re-fetched message should have dateEdited or dateRetracted set"
    );

    // Verify webhook delivery for the unsend
    let wh = CONFIG.webhook_receiver.wait_for_event_with_guid(
        "updated-message",
        &guid,
        std::time::Duration::from_secs(10),
    );
    assert_eq!(wh["type"], "updated-message");
    assert_eq!(wh["data"]["guid"], guid);
}
}

// ===========================================================================
// Private API: Subject line
// ===========================================================================

e2e_test! {
fn e2e_send_with_subject() {
    let id = run_id();

    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Body #{id}"),
        "subject": format!("Subject #{id}"),
        "method": "private-api",
    });
    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 200, "send with subject failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert_eq!(json["data"]["subject"], format!("Subject #{id}"));
}
}

// ===========================================================================
// Private API: Reply threading
// ===========================================================================

e2e_test! {
fn e2e_send_reply() {
    let id = run_id();

    let send_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Thread origin #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &send_body);
    assert_eq!(status, 200);
    let origin_guid = send_json["data"]["guid"].as_str().unwrap().to_string();

    std::thread::sleep(std::time::Duration::from_secs(2));

    let reply_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Reply #{id}"),
        "method": "private-api",
        "selectedMessageGuid": origin_guid,
        "partIndex": 0,
    });
    let (status, json) = post_json("message/text", &reply_body);
    assert_eq!(status, 200, "reply failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(
        json["data"]["threadOriginatorGuid"].is_string(),
        "reply should have threadOriginatorGuid"
    );
}
}

// ===========================================================================
// Private API: Effect ID (expressive send)
// ===========================================================================

e2e_test! {
fn e2e_send_with_effect() {
    let id = run_id();

    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Slam #{id}"),
        "method": "private-api",
        "effectId": "com.apple.MobileSMS.expressivesend.impact",
    });
    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 200, "send with effect failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert_eq!(
        json["data"]["expressiveSendStyleId"],
        "com.apple.MobileSMS.expressivesend.impact"
    );
}
}

// ===========================================================================
// Private API: Send cache dedup
// ===========================================================================

e2e_test! {
fn e2e_send_cache_dedup() {
    let id = run_id();
    let temp_guid = format!("dedup-test-{id}");

    // Fire two concurrent sends with the same tempGuid.
    // The first should succeed; the second should be rejected while the first is in-flight.
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Dedup test #{id}"),
        "method": "private-api",
        "tempGuid": &temp_guid,
    });

    let body_clone = body.clone();
    let t1 = std::thread::spawn(move || post_json("message/text", &body_clone));

    // Small delay so thread 1 gets cached first
    std::thread::sleep(std::time::Duration::from_millis(200));
    let (status2, json2) = post_json("message/text", &body);
    let (status1, json1) = t1.join().unwrap();

    // Exactly one should succeed and one should be rejected
    let one_succeeded = status1 == 200 || status2 == 200;
    let one_rejected = status1 != 200 || status2 != 200;
    assert!(
        one_succeeded && one_rejected,
        "expected one success and one rejection, got status1={status1} status2={status2}\n\
         json1={json1}\njson2={json2}"
    );
}
}

// ===========================================================================
// Private API: Multipart send
// ===========================================================================

e2e_test! {
fn e2e_send_multipart() {
    let id = run_id();

    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "parts": [
            {"partIndex": 0, "text": format!("Part A #{id}")},
            {"partIndex": 1, "text": format!("Part B #{id}")},
        ],
        "tempGuid": format!("temp-multi-{id}"),
    });
    let (status, json) = post_json("message/multipart", &body);
    assert_eq!(status, 200, "multipart send failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(json["data"]["guid"].is_string());
}
}

// ===========================================================================
// Private API: Notify silenced message
// ===========================================================================

e2e_test! {
fn e2e_message_notify() {
    let id = run_id();

    // Send a message first
    let send_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Notify test #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &send_body);
    assert_eq!(status, 200);
    let guid = send_json["data"]["guid"].as_str().unwrap();

    std::thread::sleep(std::time::Duration::from_secs(2));

    // Notify the message — fresh message so did_notify_recipient is false
    let resp = client()
        .post(url(&format!("message/{guid}/notify")))
        .json(&json!({}))
        .send()
        .expect("POST failed");
    let status = resp.status().as_u16();
    let body: Value = resp.json().expect("Invalid JSON");
    assert_eq!(status, 200, "notify should return 200 for fresh message: {body}");
    // Verify response envelope is well-formed
    assert!(body["status"].is_u64(), "response should have status: {body}");
    assert!(body["message"].is_string(), "response should have message: {body}");
}
}

// ===========================================================================
// Private API: Text formatting — explicit textFormatting array
// ===========================================================================

e2e_test! {
fn e2e_send_with_explicit_formatting() {
    let id = run_id();

    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Hello bold #{id}"),
        "method": "private-api",
        "textFormatting": [
            {"start": 0, "length": 5, "styles": ["bold"]},
        ],
    });
    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 200, "send with explicit formatting failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(json["data"]["guid"].is_string());

    // The send response uses for_sent_message() which parses attributedBody
    assert!(
        !json["data"]["attributedBody"].is_null(),
        "send response attributedBody should be set for formatted message"
    );

    // Verify the GET endpoint also returns attributedBody when requested
    let guid = json["data"]["guid"].as_str().unwrap();
    std::thread::sleep(std::time::Duration::from_secs(2));

    let found = get_json(&format!("message/{guid}?with=attributedbody"));
    assert_eq!(found["data"]["guid"], guid);
    assert!(
        !found["data"]["attributedBody"].is_null(),
        "GET attributedBody should be set when requested via ?with=attributedbody"
    );
}
}

// ===========================================================================
// Private API: Text formatting — auto markdown conversion
// ===========================================================================

e2e_test! {
fn e2e_send_with_markdown_auto() {
    let id = run_id();

    // Send markdown text; server should strip markers and apply formatting.
    // Requires server started with --markdown-to-formatting true.
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("**bold** and *italic* #{id}"),
        "method": "private-api",
    });
    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 200, "send with markdown auto failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);

    // The returned text should have markdown markers stripped
    let text = json["data"]["text"].as_str().unwrap_or("");
    assert!(
        !text.contains("**") && !text.contains("*italic*"),
        "markdown markers should be stripped, got: {text}"
    );
    assert!(
        text.contains("bold") && text.contains("italic"),
        "clean text should contain the words, got: {text}"
    );

    // The send response uses for_sent_message() which parses attributedBody
    assert!(
        !json["data"]["attributedBody"].is_null(),
        "send response attributedBody should be set for auto-formatted markdown"
    );

    // Verify via GET with ?with=attributedbody
    let guid = json["data"]["guid"].as_str().unwrap();
    std::thread::sleep(std::time::Duration::from_secs(2));

    let found = get_json(&format!("message/{guid}?with=attributedbody"));
    assert!(
        !found["data"]["attributedBody"].is_null(),
        "GET attributedBody should be set for auto-formatted markdown message"
    );
}
}

// ===========================================================================
// Private API: Text formatting — conflict rejected
// ===========================================================================

e2e_test! {
fn e2e_formatting_conflict_rejected() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": "conflict test",
        "method": "private-api",
        "textFormatting": [{"start": 0, "length": 4, "styles": ["bold"]}],
        "attributedBody": [{"text": "conflict test"}],
    });
    let (status, _json) = post_json("message/text", &body);
    assert_eq!(
        status, 400,
        "sending both textFormatting and attributedBody should be rejected"
    );
}
}

// ===========================================================================
// Private API: Embedded media
// ===========================================================================

e2e_test! {
fn e2e_embedded_media() {
    // Send a text message, then try to get embedded media. Plain text has no embedded
    // media, so Private API may return null/error or the request may time out.
    let id = run_id();
    let send_body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": format!("Embedded media test #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &send_body);
    assert_eq!(status, 200);
    let guid = send_json["data"]["guid"].as_str().unwrap();

    std::thread::sleep(std::time::Duration::from_secs(2));

    // Use a short timeout since the Private API may hang for text messages
    let tc = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(15))
        .build()
        .unwrap();
    let result = tc.get(url(&format!("message/{guid}/embedded-media"))).send();
    let resp = result.expect("embedded-media request failed");
    let status = resp.status().as_u16();
    // Plain text messages have no balloonBundleId; handler returns 400 before hitting Private API
    assert_eq!(
        status, 400,
        "embedded-media for plain text should be 400, got {status}"
    );
}
}

// ===========================================================================
// Response envelope
// ===========================================================================

e2e_test! {
fn e2e_response_envelope() {
    let json = get_json("ping");
    assert_eq!(json["status"], 200);
    assert!(json["message"].is_string());
}
}

e2e_test! {
fn e2e_404_plain_text() {
    let resp = reqwest::blocking::get(url("nonexistent")).expect("GET failed");
    assert_eq!(resp.status(), 404);
    assert_eq!(resp.text().unwrap(), "Not Found");
}
}

e2e_test! {
fn e2e_bad_request() {
    let body = json!({"chatGuid": ""});
    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 400);
    assert!(json["message"].is_string());
}
}

// ===========================================================================
// Pretty JSON middleware
// ===========================================================================

e2e_test! {
fn e2e_pretty_json() {
    let resp = get_raw("ping?pretty");
    assert_eq!(resp.status(), 200);
    let body = resp.text().unwrap();
    // Pretty-printed JSON has newlines and indentation
    assert!(body.contains('\n'), "pretty JSON should contain newlines");
    // Should still be valid JSON
    let parsed: Value =
        serde_json::from_str(&body).expect("pretty response should be valid JSON");
    assert_eq!(parsed["data"], "pong");
}
}

// ===========================================================================
// Webhook list
// ===========================================================================

e2e_test! {
fn e2e_webhook_list() {
    let json = get_json("webhook");
    let all = json["data"].as_array().expect("webhook data should be array");
    assert!(!all.is_empty(), "should have at least the test receiver webhook");
    assert!(
        all.iter().any(|w| w["url"].as_str().map_or(false, |u| u.contains("127.0.0.1"))),
        "should find the test webhook receiver"
    );
}
}

// ===========================================================================
// Attachment routes
// ===========================================================================

e2e_test! {
fn e2e_attachment_count() {
    let json = get_json("attachment/count");
    assert!(json["data"]["total"].is_u64());
}
}

e2e_test! {
fn e2e_attachment_find_and_download() {
    // Send our own attachment so this test is self-contained
    let msg_guid = send_test_attachment();
    std::thread::sleep(std::time::Duration::from_secs(2));

    // Fetch the message to get the attachment GUID
    let msg_json = get_json(&format!("message/{msg_guid}?with=attachment"));
    let att_guid = msg_json["data"]["attachments"][0]["guid"]
        .as_str()
        .expect("sent message should have an attachment");

    // Find attachment by GUID
    let att_json = get_json(&format!("attachment/{att_guid}"));
    assert!(att_json["data"]["guid"].is_string());
    assert!(
        att_json["data"]["mimeType"].is_string() || att_json["data"]["mimeType"].is_null()
    );

    // Download the freshly-sent attachment
    let resp = get_raw(&format!("attachment/{att_guid}/download"));
    assert_eq!(
        resp.status().as_u16(),
        200,
        "download should return 200 for fresh attachment"
    );
    let ct = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    assert!(!ct.contains("json"), "download should not return JSON");
}
}

e2e_test! {
fn e2e_attachment_upload() {
    let id = run_id();
    let data = b"e2e test file content";

    let form = reqwest::blocking::multipart::Form::new().part(
        "attachment",
        reqwest::blocking::multipart::Part::bytes(data.to_vec())
            .file_name(format!("e2e-upload-{id}.txt"))
            .mime_str("text/plain")
            .unwrap(),
    );

    let resp = client()
        .post(url("attachment/upload"))
        .multipart(form)
        .send()
        .expect("upload failed");

    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON");
    assert_eq!(status, 200, "upload failed: {json}");
    assert!(json["data"]["path"].is_string(), "upload should return path");
}
}

e2e_test! {
fn e2e_attachment_blurhash_not_found() {
    // Non-existent attachment GUID should return 404
    let resp = get_raw("attachment/some-guid/blurhash");
    assert_eq!(resp.status(), 404);
}
}

e2e_test! {
fn e2e_attachment_blurhash() {
    // Send our own PNG so this test is self-contained
    let msg_guid = send_test_attachment();
    std::thread::sleep(std::time::Duration::from_secs(2));

    let msg_json = get_json(&format!("message/{msg_guid}?with=attachment"));
    let att_guid = msg_json["data"]["attachments"][0]["guid"]
        .as_str()
        .expect("sent message should have an attachment");

    let json = get_json(&format!("attachment/{att_guid}/blurhash"));
    assert_eq!(json["status"], 200);
    let blurhash = json["data"]["blurhash"].as_str().expect("should have blurhash string");
    assert!(!blurhash.is_empty(), "blurhash should not be empty");
}
}

e2e_test! {
fn e2e_attachment_live_photo() {
    // Send our own PNG so this test is self-contained
    let msg_guid = send_test_attachment();
    std::thread::sleep(std::time::Duration::from_secs(2));

    let msg_json = get_json(&format!("message/{msg_guid}?with=attachment"));
    let att_guid = msg_json["data"]["attachments"][0]["guid"]
        .as_str()
        .expect("sent message should have an attachment");

    let resp = get_raw(&format!("attachment/{att_guid}/live"));
    // Our test PNGs have no Live Photo companion (.mov), so 404 is expected
    assert_eq!(
        resp.status().as_u16(),
        404,
        "test PNG should not have a live photo companion"
    );
}
}

// ===========================================================================
// Attachment serializer fields (hasLivePhoto, height, width)
// ===========================================================================

e2e_test! {
fn e2e_attachment_serializer_fields() {
    // Send our own attachment so this test is self-contained
    let msg_guid = send_test_attachment();
    std::thread::sleep(std::time::Duration::from_secs(2));

    let msg_json = get_json(&format!("message/{msg_guid}?with=attachment"));
    let att = &msg_json["data"]["attachments"][0];
    let obj = att.as_object().expect("attachment should be object");
    // These fields should always be present in serialized attachments
    assert!(
        obj.contains_key("hasLivePhoto"),
        "attachment should have hasLivePhoto field"
    );
    assert!(
        obj.contains_key("height"),
        "attachment should have height field"
    );
    assert!(
        obj.contains_key("width"),
        "attachment should have width field"
    );
    assert!(
        att["hasLivePhoto"].is_boolean(),
        "hasLivePhoto should be boolean"
    );
}
}

// ===========================================================================
// iCloud routes
// ===========================================================================

e2e_test! {
fn e2e_icloud_account() {
    let json = get_json("icloud/account");
    // data may be null or an object with account info
    assert_eq!(json["status"], 200);
}
}

e2e_test! {
fn e2e_icloud_contact() {
    let addr = &CONFIG.peers[0];
    let json = get_json(&format!("icloud/contact?address={addr}"));
    assert_eq!(json["status"], 200);
    assert!(
        json["message"].is_string(),
        "response should have message field"
    );
}
}

e2e_test! {
fn e2e_icloud_change_alias_validation() {
    // Empty alias should return 400
    let (status, json) = post_json("icloud/account/alias", &json!({"alias": ""}));
    assert_eq!(status, 400, "empty alias should return 400: {json}");
}
}

e2e_test! {
fn e2e_icloud_findmy_devices() {
    // GET reads and decrypts Devices.data from disk (lazy-fetches key on first call).
    // Needs a longer timeout — FindMy.app must connect and report ready for key fetch.
    let client = client();
    let resp = client
        .get(url("icloud/findmy/devices"))
        .timeout(std::time::Duration::from_secs(30))
        .send()
        .expect("FindMy devices GET failed");
    let json: Value = resp.json().expect("Invalid JSON");
    assert_eq!(json["status"], 200, "findmy/devices should succeed: {json}");
}
}

e2e_test! {
fn e2e_icloud_findmy_devices_refresh() {
    // POST refresh restarts FindMy.app, waits for reconnect + 10s, then re-reads Devices.data
    let client = client();
    let resp = client
        .post(url("icloud/findmy/devices/refresh"))
        .json(&json!({}))
        .timeout(std::time::Duration::from_secs(60))
        .send()
        .expect("FindMy devices refresh failed");
    let json: Value = resp.json().expect("Invalid JSON");
    assert_eq!(json["status"], 200, "findmy/devices/refresh should succeed: {json}");
}
}

e2e_test! {
fn e2e_icloud_findmy_friends() {
    let json = get_json("icloud/findmy/friends");
    assert_eq!(json["status"], 200);
    assert!(
        json["data"].is_array(),
        "findmy/friends data should be array"
    );
}
}

e2e_test! {
fn e2e_icloud_findmy_friends_refresh() {
    // Refresh triggers the Private API to poll FindMy locations.
    // This is a slow operation (up to 15s timeout inside the dylib).
    let client = client();
    let resp = client
        .post(url("icloud/findmy/friends/refresh"))
        .json(&json!({}))
        .timeout(std::time::Duration::from_secs(30))
        .send()
        .expect("FindMy friends refresh HTTP POST failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON from findmy refresh");

    assert_eq!(status, 200, "findmy/friends/refresh should succeed: {json}");
    assert!(
        json["data"].is_array(),
        "findmy/friends/refresh data should be array, got: {}",
        json["data"]
    );
    assert_eq!(
        json["message"].as_str().unwrap_or(""),
        "Successfully refreshed FindMy friends!",
        "should return success message"
    );
}
}

e2e_test! {
fn e2e_icloud_findmy_friends_refresh_populates_cache() {
    // After refresh, GET /findmy/friends should return the same (or superset) data
    let client = client();
    let refresh_resp = client
        .post(url("icloud/findmy/friends/refresh"))
        .json(&json!({}))
        .timeout(std::time::Duration::from_secs(30))
        .send()
        .expect("FindMy friends refresh failed");
    assert_eq!(refresh_resp.status(), 200);
    let refresh_json: Value = refresh_resp.json().expect("Invalid JSON");
    let refresh_data = refresh_json["data"].as_array().expect("refresh data should be array");

    // GET the cache — should have at least as many entries as the refresh returned
    let cache_json = get_json("icloud/findmy/friends");
    let cache_data = cache_json["data"].as_array().expect("cache data should be array");
    assert!(
        cache_data.len() >= refresh_data.len(),
        "cache should have >= refresh entries ({} < {})",
        cache_data.len(),
        refresh_data.len()
    );

    // If we got any locations, validate the shape
    for loc in cache_data {
        assert!(
            loc.get("handle").is_some(),
            "each location should have a 'handle' field, got: {loc}"
        );
    }
}
}

// ===========================================================================
// FaceTime
// ===========================================================================

// POST /api/v1/facetime/session → 200, data has "link" key
// Requires FaceTime dylib to be injected into FaceTime.app.
// The server sends callUUID: null to generate a new link (not tied to an existing call).
e2e_test! {
fn e2e_facetime_session() {
    // Use a longer timeout — FaceTime link generation can take a few seconds
    let c = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(15))
        .build()
        .unwrap();
    let resp = c
        .post(url("facetime/session"))
        .json(&json!({}))
        .send();

    let r = resp.expect("facetime session: request failed");
    let status = r.status().as_u16();
    let body: Value = r.json().unwrap_or_default();
    assert_eq!(status, 200, "facetime session failed: {body}");
    let link = body["data"]["link"]
        .as_str()
        .expect("missing link in data");
    assert!(
        link.starts_with("https://facetime.apple.com/"),
        "link should be a FaceTime URL: {link}"
    );
}
}

// ===========================================================================
// Group Chat Operations
// ===========================================================================

e2e_test! {
fn e2e_group_chat_find() {
    let guid = &CONFIG.group_chat;
    let json = get_json(&format!("chat/{guid}"));
    let data = &json["data"];
    assert!(data["guid"].is_string(), "group chat should have guid");
    // Verify it's a group chat (style == 43)
    assert_eq!(data["style"], 43, "group chat should have style 43");
}
}

e2e_test! {
fn e2e_group_chat_messages() {
    let guid = &CONFIG.group_chat;
    let json = get_json(&format!("chat/{guid}/message?limit=5&sort=DESC"));
    assert!(json["data"].is_array());
    // Group chat should have at least one message (from creation)
    let msgs = json["data"].as_array().unwrap();
    assert!(!msgs.is_empty(), "group chat should have messages");
}
}

e2e_test! {
fn e2e_group_chat_icon_get() {
    let guid = &CONFIG.group_chat;

    // Set a known icon first so the GET has a deterministic outcome
    let form = reqwest::blocking::multipart::Form::new().part(
        "icon",
        reqwest::blocking::multipart::Part::bytes(test_png())
            .file_name("e2e-icon-get-test.png")
            .mime_str("image/png")
            .unwrap(),
    );
    let resp = client()
        .post(url(&format!("chat/{guid}/icon")))
        .multipart(form)
        .send()
        .expect("POST icon failed");
    assert_eq!(resp.status().as_u16(), 200, "set icon failed");
    std::thread::sleep(std::time::Duration::from_secs(3));

    // Now GET the icon — should return 200 with binary image data
    let resp = get_raw(&format!("chat/{guid}/icon"));
    assert_eq!(resp.status().as_u16(), 200, "icon GET should return 200 after setting icon");
    let ct = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    assert!(
        ct.starts_with("image/"),
        "icon content-type should be an image, got: {ct}"
    );
}
}

e2e_test! {
fn e2e_group_chat_share_contact_status() {
    let guid = &CONFIG.group_chat;
    let json = get_json(&format!("chat/{guid}/share/contact/status"));
    // data is a boolean
    assert!(
        json["data"].is_boolean(),
        "share contact status data should be boolean"
    );
}
}

e2e_test! {
fn e2e_group_chat_rename() {
    let guid = &CONFIG.group_chat;
    CONFIG.webhook_receiver.drain();

    // Get current display name
    let before = get_json(&format!("chat/{guid}"));
    let original_name = before["data"]["displayName"]
        .as_str()
        .unwrap_or("")
        .to_string();

    // Rename to test name
    let test_name = format!("E2E Test {}", run_id());
    let (status, json) = put_json(
        &format!("chat/{guid}"),
        &json!({"displayName": &test_name}),
    );
    assert_eq!(status, 200, "rename failed: {json}");

    // Verify webhook delivery for group name change
    let wh = CONFIG.webhook_receiver.wait_for_event(
        "group-name-change",
        std::time::Duration::from_secs(10),
    );
    assert_eq!(wh["type"], "group-name-change");

    std::thread::sleep(std::time::Duration::from_secs(3));

    // Verify rename took effect
    let after = get_json(&format!("chat/{guid}"));
    assert_eq!(
        after["data"]["displayName"], test_name,
        "displayName should be updated"
    );

    // Restore original name
    let restore_name = if original_name.is_empty() {
        "E2E Group"
    } else {
        &original_name
    };
    let (status, _) = put_json(
        &format!("chat/{guid}"),
        &json!({"displayName": restore_name}),
    );
    assert_eq!(status, 200, "rename restore failed");

    std::thread::sleep(std::time::Duration::from_secs(2));
}
}

e2e_test! {
fn e2e_group_chat_icon_set_remove() {
    let guid = &CONFIG.group_chat;

    // Set icon (1x1 PNG)
    let form = reqwest::blocking::multipart::Form::new().part(
        "icon",
        reqwest::blocking::multipart::Part::bytes(test_png())
            .file_name("e2e-icon.png")
            .mime_str("image/png")
            .unwrap(),
    );
    let resp = client()
        .post(url(&format!("chat/{guid}/icon")))
        .multipart(form)
        .send()
        .expect("POST icon failed");
    let status = resp.status().as_u16();
    assert_eq!(status, 200, "set icon failed");

    std::thread::sleep(std::time::Duration::from_secs(3));

    // Remove icon
    let resp = client()
        .delete(url(&format!("chat/{guid}/icon")))
        .send()
        .expect("DELETE icon failed");
    let status = resp.status().as_u16();
    assert_eq!(status, 200, "remove icon failed");

    std::thread::sleep(std::time::Duration::from_secs(2));
}
}

e2e_test! {
fn e2e_group_chat_send_text() {
    let id = run_id();
    CONFIG.webhook_receiver.drain();

    let body = json!({
        "chatGuid": CONFIG.group_chat,
        "message": format!("E2E group test #{id}"),
        "method": "private-api",
        "tempGuid": format!("temp-group-{id}"),
    });
    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 200, "group send failed: {json}");
    assert_eq!(json["data"]["isFromMe"], true);
    assert!(json["data"]["guid"].is_string());

    // Verify webhook delivery for group message
    let guid = json["data"]["guid"].as_str().unwrap();
    let wh = CONFIG.webhook_receiver.wait_for_event_with_guid(
        "new-message",
        guid,
        std::time::Duration::from_secs(10),
    );
    assert_eq!(wh["type"], "new-message");
    assert_eq!(wh["data"]["guid"], guid);
}
}

e2e_test! {
fn e2e_group_chat_delete_message() {
    let id = run_id();

    // Send a test message to the group
    let body = json!({
        "chatGuid": CONFIG.group_chat,
        "message": format!("To be deleted #{id}"),
        "method": "private-api",
    });
    let (status, send_json) = post_json("message/text", &body);
    assert_eq!(status, 200, "send for delete test failed: {send_json}");
    let msg_guid = send_json["data"]["guid"].as_str().unwrap().to_string();

    std::thread::sleep(std::time::Duration::from_secs(2));

    // Unsend first so other participants don't see test artifacts
    let (unsend_status, unsend_json) = post_json(&format!("message/{msg_guid}/unsend"), &json!({}));
    assert_eq!(unsend_status, 200, "unsend before delete failed: {unsend_json}");

    std::thread::sleep(std::time::Duration::from_secs(2));

    // Delete the message from local chat.db
    let resp = client()
        .delete(url(&format!(
            "chat/{}/{}",
            CONFIG.group_chat, msg_guid
        )))
        .send()
        .expect("DELETE message failed");
    let status = resp.status().as_u16();
    assert_eq!(status, 200, "delete message failed");
}
}

// ===========================================================================
// Auth edge cases
// ===========================================================================

e2e_test! {
fn e2e_auth_wrong_password() {
    let base = &CONFIG.base;
    let resp = reqwest::blocking::get(format!("{base}/ping?password=WRONG"))
        .expect("HTTP GET failed");
    assert_eq!(resp.status(), 401, "wrong password should return 401");
}
}

// ===========================================================================
// Not-found 404 tests (nonexistent GUIDs)
// ===========================================================================

e2e_test! {
fn e2e_message_find_nonexistent() {
    let resp = get_raw("message/nonexistent-guid-12345");
    assert_eq!(resp.status(), 404);
    let json: Value = resp.json().unwrap();
    assert!(
        error_detail(&json).contains("Message does not exist"),
        "expected 'Message does not exist' error: {json}"
    );
}
}

e2e_test! {
fn e2e_chat_find_nonexistent() {
    let resp = get_raw("chat/nonexistent-guid-12345");
    assert_eq!(resp.status(), 404);
    let json: Value = resp.json().unwrap();
    assert!(
        error_detail(&json).contains("Chat does not exist"),
        "expected 'Chat does not exist' error: {json}"
    );
}
}

e2e_test! {
fn e2e_chat_messages_nonexistent() {
    let resp = get_raw("chat/nonexistent-guid-12345/message");
    assert_eq!(resp.status(), 404);
    let json: Value = resp.json().unwrap();
    assert!(
        error_detail(&json).contains("Chat does not exist"),
        "expected 'Chat does not exist' error: {json}"
    );
}
}

e2e_test! {
fn e2e_chat_delete_nonexistent() {
    let (status, json) = delete_no_body("chat/nonexistent-guid-12345");
    assert_eq!(status, 404);
    assert!(
        error_detail(&json).contains("Chat not found"),
        "expected 'Chat not found' error: {json}"
    );
}
}

e2e_test! {
fn e2e_handle_find_nonexistent() {
    let resp = get_raw("handle/nobody-99999@nowhere.invalid");
    assert_eq!(resp.status(), 404);
    let json: Value = resp.json().unwrap();
    assert!(
        error_detail(&json).contains("Handle not found"),
        "expected 'Handle not found' error: {json}"
    );
}
}

e2e_test! {
fn e2e_handle_focus_nonexistent() {
    let resp = get_raw("handle/nobody-99999@nowhere.invalid/focus");
    assert_eq!(resp.status(), 404);
    let json: Value = resp.json().unwrap();
    assert!(
        error_detail(&json).contains("Handle not found"),
        "expected 'Handle not found' error: {json}"
    );
}
}

e2e_test! {
fn e2e_attachment_find_nonexistent() {
    let resp = get_raw("attachment/nonexistent-att-guid-12345");
    assert_eq!(resp.status(), 404);
    let json: Value = resp.json().unwrap();
    assert!(
        error_detail(&json).contains("Attachment does not exist"),
        "expected 'Attachment does not exist' error: {json}"
    );
}
}

e2e_test! {
fn e2e_attachment_download_nonexistent() {
    let resp = get_raw("attachment/nonexistent-att-guid-12345/download");
    assert_eq!(resp.status(), 404);
    let json: Value = resp.json().unwrap();
    assert!(
        error_detail(&json).contains("Attachment does not exist"),
        "expected 'Attachment does not exist' error: {json}"
    );
}
}

// ===========================================================================
// Message validation 400 tests
// ===========================================================================

e2e_test! {
fn e2e_send_text_empty_rejected() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": "",
        "method": "private-api",
    });
    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 400, "empty message should return 400: {json}");
    assert!(
        error_detail(&json).contains("required"),
        "error should mention 'required': {json}"
    );
}
}

e2e_test! {
fn e2e_send_text_invalid_method() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": "test",
        "method": "invalid-method",
    });
    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 400, "invalid method should return 400: {json}");
    assert!(
        error_detail(&json).contains("Invalid method"),
        "error should mention 'Invalid method': {json}"
    );
}
}

e2e_test! {
fn e2e_send_text_applescript_no_tempguid() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "message": "test",
        "method": "apple-script",
    });
    let (status, json) = post_json("message/text", &body);
    assert_eq!(status, 400, "apple-script without tempGuid should return 400: {json}");
    assert!(
        error_detail(&json).contains("tempGuid"),
        "error should mention 'tempGuid': {json}"
    );
}
}

e2e_test! {
fn e2e_send_reaction_empty_reaction() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": "fake-guid",
        "reaction": "",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "empty reaction should return 400: {json}");
    assert!(
        error_detail(&json).contains("reaction is required"),
        "error should mention 'reaction is required': {json}"
    );
}
}

e2e_test! {
fn e2e_send_reaction_invalid_type() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": "fake-guid",
        "reaction": "invalid-reaction",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "invalid reaction should return 400: {json}");
    assert!(
        error_detail(&json).contains("Invalid reaction"),
        "error should mention 'Invalid reaction': {json}"
    );
}
}

e2e_test! {
fn e2e_send_reaction_empty_chatguid() {
    let body = json!({
        "chatGuid": "",
        "selectedMessageGuid": "fake-guid",
        "reaction": "love",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "empty chatGuid should return 400: {json}");
    assert!(
        error_detail(&json).contains("chatGuid"),
        "error should mention 'chatGuid': {json}"
    );
}
}

e2e_test! {
fn e2e_send_reaction_nonexistent_message() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE",
        "reaction": "love",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "reaction on nonexistent message should return 400: {json}");
    assert!(
        error_detail(&json).contains("does not exist"),
        "error should mention 'does not exist': {json}"
    );
}
}

e2e_test! {
fn e2e_send_reaction_empty_selected_guid() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": "",
        "reaction": "love",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "empty selectedMessageGuid should return 400: {json}");
    assert!(
        error_detail(&json).contains("selectedMessageGuid"),
        "error should mention 'selectedMessageGuid': {json}"
    );
}
}

e2e_test! {
fn e2e_send_sticker_reaction_missing_data_url() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE",
        "reaction": "sticker",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "sticker without data URL should return 400: {json}");
    assert!(
        error_detail(&json).contains("sticker data URL is required"),
        "error should mention 'sticker data URL is required': {json}"
    );
}
}

e2e_test! {
fn e2e_send_sticker_reaction_invalid_data_url() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE",
        "reaction": "sticker",
        "sticker": "not-a-data-url",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "sticker with invalid data URL should return 400: {json}");
    assert!(
        error_detail(&json).contains("data:"),
        "error should mention 'data:': {json}"
    );
}
}

e2e_test! {
fn e2e_send_sticker_reaction_invalid_base64() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE",
        "reaction": "sticker",
        "sticker": "data:image/png;base64,!!!not-valid-base64!!!",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "sticker with invalid base64 should return 400: {json}");
    assert!(
        error_detail(&json).contains("base64"),
        "error should mention 'base64': {json}"
    );
}
}

e2e_test! {
fn e2e_send_sticker_reaction_empty_base64() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE",
        "reaction": "sticker",
        "sticker": "data:image/png;base64,",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "sticker with empty base64 should return 400: {json}");
    assert!(
        error_detail(&json).contains("empty"),
        "error should mention 'empty': {json}"
    );
}
}

e2e_test! {
fn e2e_send_sticker_reaction_invalid_mime() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "selectedMessageGuid": "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE",
        "reaction": "sticker",
        "sticker": "data:text/plain;base64,SGVsbG8=",
    });
    let (status, json) = post_json("message/react", &body);
    assert_eq!(status, 400, "sticker with non-image MIME should return 400: {json}");
    assert!(
        error_detail(&json).contains("MIME type"),
        "error should mention 'MIME type': {json}"
    );
}
}

e2e_test! {
fn e2e_edit_message_nonexistent() {
    let body = json!({
        "editedMessage": "edited text",
        "backwardsCompatibilityMessage": "edited",
    });
    let (status, json) = post_json("message/fake-guid-12345/edit", &body);
    assert_eq!(status, 400, "editing nonexistent message should return 400: {json}");
    assert!(
        error_detail(&json).contains("does not exist"),
        "error should mention 'does not exist': {json}"
    );
}
}

e2e_test! {
fn e2e_edit_message_empty_body() {
    let body = json!({
        "editedMessage": "",
        "backwardsCompatibilityMessage": "compat",
    });
    let (status, json) = post_json("message/fake-guid-12345/edit", &body);
    assert_eq!(status, 400, "empty editedMessage should return 400: {json}");
    assert!(
        error_detail(&json).contains("editedMessage is required"),
        "error should mention 'editedMessage is required': {json}"
    );
}
}

e2e_test! {
fn e2e_unsend_message_nonexistent() {
    let (status, json) = post_json("message/fake-guid-12345/unsend", &json!({}));
    assert_eq!(status, 400, "unsending nonexistent message should return 400: {json}");
    assert!(
        error_detail(&json).contains("does not exist"),
        "error should mention 'does not exist': {json}"
    );
}
}

e2e_test! {
fn e2e_notify_message_nonexistent() {
    let (status, json) = post_json("message/fake-guid-12345/notify", &json!({}));
    assert_eq!(status, 400, "notifying nonexistent message should return 400: {json}");
    assert!(
        error_detail(&json).contains("does not exist"),
        "error should mention 'does not exist': {json}"
    );
}
}

e2e_test! {
fn e2e_send_multipart_empty_parts() {
    let body = json!({
        "chatGuid": CONFIG.self_chat,
        "parts": [],
    });
    let (status, json) = post_json("message/multipart", &body);
    assert_eq!(status, 400, "empty parts should return 400: {json}");
    assert!(
        error_detail(&json).contains("parts"),
        "error should mention 'parts': {json}"
    );
}
}

// ===========================================================================
// Chat validation 400/500 tests
// ===========================================================================

e2e_test! {
fn e2e_chat_new_empty_addresses() {
    let body = json!({
        "addresses": [],
        "message": "test",
        "method": "private-api",
    });
    let (status, json) = post_json("chat/new", &body);
    assert_eq!(status, 400, "empty addresses should return 400: {json}");
    assert!(
        error_detail(&json).contains("addresses"),
        "error should mention 'addresses': {json}"
    );
}
}

e2e_test! {
fn e2e_chat_rename_1to1_rejected() {
    let self_chat = &CONFIG.self_chat;
    let (status, json) = put_json(
        &format!("chat/{self_chat}"),
        &json!({"displayName": "Fail"}),
    );
    assert_eq!(status, 500, "renaming 1:1 chat should return 500: {json}");
    assert!(
        error_detail(&json).contains("non-group"),
        "error should mention 'non-group': {json}"
    );
}
}

e2e_test! {
fn e2e_chat_rename_no_displayname() {
    let group = &CONFIG.group_chat;
    let (status, json) = put_json(&format!("chat/{group}"), &json!({}));
    assert_eq!(status, 200, "no displayName should return 200: {json}");
    assert!(
        json["message"].as_str().unwrap_or("").contains("not updated"),
        "message should contain 'not updated': {json}"
    );
}
}

e2e_test! {
fn e2e_chat_icon_set_1to1_rejected() {
    let self_chat = &CONFIG.self_chat;
    let form = reqwest::blocking::multipart::Form::new().part(
        "icon",
        reqwest::blocking::multipart::Part::bytes(test_png())
            .file_name("e2e-icon-1to1.png")
            .mime_str("image/png")
            .unwrap(),
    );
    let resp = client()
        .post(url(&format!("chat/{self_chat}/icon")))
        .multipart(form)
        .send()
        .expect("POST icon failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().unwrap();
    assert_eq!(status, 500, "setting icon on 1:1 should return 500: {json}");
    assert!(
        error_detail(&json).contains("not a group"),
        "error should mention 'not a group': {json}"
    );
}
}

e2e_test! {
fn e2e_chat_participant_add_empty_address() {
    let group = &CONFIG.group_chat;
    let (status, json) = post_json(
        &format!("chat/{group}/participant/add"),
        &json!({"address": ""}),
    );
    assert_eq!(status, 400, "empty address should return 400: {json}");
    assert!(
        error_detail(&json).contains("address"),
        "error should mention 'address': {json}"
    );
}
}

// ===========================================================================
// Attachment & iCloud validation
// ===========================================================================

e2e_test! {
fn e2e_attachment_force_download_nonexistent() {
    let resp = get_raw("attachment/nonexistent-att-guid-12345/download/force");
    assert_eq!(
        resp.status().as_u16(),
        400,
        "force download of nonexistent attachment should return 400"
    );
    let json: Value = resp.json().unwrap();
    assert!(
        error_detail(&json).contains("does not exist"),
        "error should mention 'does not exist': {json}"
    );
}
}

e2e_test! {
fn e2e_icloud_contact_no_address() {
    let resp = get_raw("icloud/contact");
    assert_eq!(
        resp.status().as_u16(),
        400,
        "icloud contact without address should return 400"
    );
    let json: Value = resp.json().unwrap();
    assert!(
        error_detail(&json).contains("address"),
        "error should mention 'address': {json}"
    );
}
}

// ===========================================================================
// Query parameter variations
// ===========================================================================

e2e_test! {
fn e2e_message_query_asc() {
    let body = json!({
        "limit": 5,
        "sort": "ASC",
    });
    let (status, json) = post_json("message/query", &body);
    assert_eq!(status, 200);
    let data = json["data"].as_array().expect("data should be an array");
    assert!(data.len() >= 2, "need at least 2 messages for sort check");
    let d0 = data[0]["dateCreated"].as_u64().unwrap_or(0);
    let d1 = data[1]["dateCreated"].as_u64().unwrap_or(0);
    assert!(d0 <= d1, "messages should be in ASC order: {d0} > {d1}");
}
}

e2e_test! {
fn e2e_message_query_pagination() {
    // Page 1: first 3 messages
    let body1 = json!({ "limit": 3, "offset": 0, "sort": "DESC" });
    let (status1, json1) = post_json("message/query", &body1);
    assert_eq!(status1, 200);
    let page1 = json1["data"].as_array().expect("page1 should be array");
    assert_eq!(page1.len(), 3, "page1 should have 3 messages");

    // Page 2: next 3 messages
    let body2 = json!({ "limit": 3, "offset": 3, "sort": "DESC" });
    let (status2, json2) = post_json("message/query", &body2);
    assert_eq!(status2, 200);
    let page2 = json2["data"].as_array().expect("page2 should be array");
    assert_eq!(page2.len(), 3, "page2 should have 3 messages");
    assert_eq!(json2["metadata"]["offset"], 3, "page2 metadata.offset should be 3");

    // No overlap between pages
    let guids1: Vec<&str> = page1.iter().filter_map(|m| m["guid"].as_str()).collect();
    let guids2: Vec<&str> = page2.iter().filter_map(|m| m["guid"].as_str()).collect();
    for g in &guids2 {
        assert!(
            !guids1.contains(g),
            "page2 should not overlap with page1, found duplicate: {g}"
        );
    }
}
}

e2e_test! {
fn e2e_message_query_with_chat_participants() {
    let body = json!({
        "limit": 3,
        "sort": "DESC",
        "with": ["chat", "chat.participants"],
    });
    let (status, json) = post_json("message/query", &body);
    assert_eq!(status, 200);
    let data = json["data"].as_array().expect("data should be array");
    assert!(!data.is_empty(), "should have at least one message");

    // Find a message that has chats (most will)
    let msg_with_chats = data.iter().find(|m| {
        m["chats"].as_array().map_or(false, |c| !c.is_empty())
    });
    if let Some(msg) = msg_with_chats {
        assert!(
            msg["chats"][0]["participants"].is_array(),
            "chat should have participants array when with=[chat.participants]"
        );
    }
}
}

e2e_test! {
fn e2e_chat_query_with_participants() {
    let body = json!({
        "limit": 5,
        "with": ["participants"],
    });
    let (status, json) = post_json("chat/query", &body);
    assert_eq!(status, 200);
    let chats = json["data"].as_array().expect("data should be array");
    assert!(!chats.is_empty(), "should have at least one chat");
    for (i, chat) in chats.iter().enumerate() {
        assert!(
            chat["participants"].is_array(),
            "chat[{i}] should have participants array when with=[participants]"
        );
    }
}
}

e2e_test! {
fn e2e_chat_find_with_last_message() {
    let self_chat = &CONFIG.self_chat;
    let json = get_json(&format!("chat/{self_chat}?with=lastmessage"));
    let data = &json["data"];
    assert!(
        data["lastMessage"].is_object(),
        "should have lastMessage object: {data}"
    );
    assert!(
        data["lastMessage"]["guid"].is_string(),
        "lastMessage should have guid"
    );
    assert!(
        data["lastMessage"]["dateCreated"].is_u64(),
        "lastMessage should have dateCreated"
    );
}
}

e2e_test! {
fn e2e_chat_find_with_participants() {
    let group = &CONFIG.group_chat;
    let json = get_json(&format!("chat/{group}?with=participants"));
    let data = &json["data"];
    let participants = data["participants"]
        .as_array()
        .expect("should have participants array");
    assert!(
        participants.len() >= 2,
        "group chat should have at least 2 participants, got {}",
        participants.len()
    );
    for (i, p) in participants.iter().enumerate() {
        assert!(
            p["address"].is_string(),
            "participant[{i}] should have address"
        );
    }
}
}

e2e_test! {
fn e2e_chat_count_breakdown() {
    let json = get_json("chat/count");
    let data = &json["data"];
    assert!(data["total"].is_u64(), "should have total");
    assert!(
        data["breakdown"].is_object(),
        "should have breakdown object: {data}"
    );
    assert!(
        data["breakdown"]["iMessage"].is_u64() || data["breakdown"]["iMessage"].is_i64(),
        "breakdown should have iMessage key: {data}"
    );
}
}

e2e_test! {
fn e2e_statistics_totals_only_filter() {
    let json = get_json("server/statistics/totals?only=messages");
    let data = &json["data"];
    assert!(
        data["messages"].is_u64(),
        "data.messages should be present when only=messages"
    );
    assert!(
        data.get("handles").is_none(),
        "data.handles should be absent when only=messages: {data}"
    );
    assert!(
        data.get("chats").is_none(),
        "data.chats should be absent when only=messages: {data}"
    );
    assert!(
        data.get("attachments").is_none(),
        "data.attachments should be absent when only=messages: {data}"
    );
}
}

e2e_test! {
fn e2e_server_logs_count() {
    let json = get_json("server/logs?count=3");
    let log_text = json["data"].as_str().expect("logs data should be a string");
    let line_count = log_text.lines().count();
    assert!(
        line_count >= 1 && line_count <= 3,
        "log count should be between 1 and 3, got {line_count}"
    );
}
}

// ===========================================================================
// Previously untested routes
// ===========================================================================

e2e_test! {
fn e2e_group_chat_share_contact() {
    let guid = &CONFIG.group_chat;
    let (status, json) = post_json(&format!("chat/{guid}/share/contact"), &json!({}));
    assert_eq!(status, 200, "share contact should return 200: {json}");
    assert!(
        json["message"].as_str().unwrap_or("").contains("Shared")
            || json["message"].as_str().unwrap_or("").contains("shared"),
        "message should mention sharing: {json}"
    );
}
}

e2e_test! {
fn e2e_facetime_leave() {
    let c = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(15))
        .build()
        .unwrap();
    let resp = c
        .post(url("facetime/leave/00000000-0000-0000-0000-000000000000"))
        .json(&json!({}))
        .send()
        .expect("facetime leave request failed");
    let status = resp.status().as_u16();
    let json: Value = resp.json().unwrap_or_default();
    assert_eq!(status, 201, "facetime leave should return 201: {json}");
    assert_eq!(json["status"], 201);
    assert_eq!(json["message"], "No Data");
}
}

// ===========================================================================
// Webhook verification: mark read/unread dispatches events
// ===========================================================================

e2e_test! {
fn e2e_mark_read_unread_webhook() {
    let self_chat = &CONFIG.self_chat;
    let timeout = std::time::Duration::from_secs(10);

    // --- Mark unread ---
    CONFIG.webhook_receiver.drain();
    let resp = client()
        .post(url(&format!("chat/{self_chat}/unread")))
        .send()
        .expect("POST unread failed");
    assert_eq!(resp.status(), 200, "mark unread should return 200");

    let wh = CONFIG
        .webhook_receiver
        .wait_for_event("chat-read-status-changed", timeout);
    assert_eq!(wh["type"], "chat-read-status-changed");
    assert_eq!(
        wh["data"]["read"], false,
        "mark_unread webhook should have read=false: {wh}"
    );

    // --- Mark read ---
    CONFIG.webhook_receiver.drain();
    let resp = client()
        .post(url(&format!("chat/{self_chat}/read")))
        .send()
        .expect("POST read failed");
    assert_eq!(resp.status(), 200, "mark read should return 200");

    let wh = CONFIG
        .webhook_receiver
        .wait_for_event("chat-read-status-changed", timeout);
    assert_eq!(wh["type"], "chat-read-status-changed");
    assert_eq!(
        wh["data"]["read"], true,
        "mark_read webhook should have read=true: {wh}"
    );
}
}

// ===========================================================================
// Group lifecycle tests (require 3 peers)
// ===========================================================================

// Full group lifecycle: create (4 members) → verify → remove participant → verify →
// add back → verify → leave (4→3, still valid group) → best-effort delete.
//
// Requires 3 peers (4 members with self) because iMessage won't let you:
// - remove a member if it would drop below 3
// - leave a group if it would drop below 3
e2e_test! {
fn e2e_group_lifecycle() {
    if CONFIG.peers.len() < 3 {
        eprintln!("Skipping lifecycle test: need 3 peers, have {}", CONFIG.peers.len());
        return;
    }

    // Client with longer timeout — participant/leave ops poll DB up to 30s
    let tc = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(45))
        .build()
        .unwrap();

    // --- Create group with all 3 peers (4 members total with self) ---
    let create_body = json!({
        "addresses": [&CONFIG.peers[0], &CONFIG.peers[1], &CONFIG.peers[2]],
        "message": "E2E lifecycle test group",
        "method": "private-api",
    });
    let resp = tc
        .post(url("chat/new"))
        .json(&create_body)
        .send()
        .expect("Failed to create lifecycle group");
    let status = resp.status().as_u16();
    let json: Value = resp.json().expect("Invalid JSON from /chat/new");
    assert_eq!(status, 200, "create lifecycle group failed: {json}");
    let guid = json["data"]["guid"]
        .as_str()
        .expect("created group missing guid")
        .to_string();
    eprintln!("  Lifecycle group created: {guid}");

    std::thread::sleep(std::time::Duration::from_secs(3));

    // --- Verify: style=43, participants >= 3 ---
    let chat_json = get_json(&format!("chat/{guid}?with=participants"));
    assert_eq!(chat_json["data"]["style"], 43, "should be a group chat");
    let participants = chat_json["data"]["participants"]
        .as_array()
        .expect("should have participants");
    assert!(
        participants.len() >= 3,
        "lifecycle group should have >= 3 participants, got {}",
        participants.len()
    );

    // --- Remove peers[2] (4→3) ---
    let remove_body = json!({"address": &CONFIG.peers[2]});
    let resp = tc
        .post(url(&format!("chat/{guid}/participant/remove")))
        .json(&remove_body)
        .send()
        .expect("remove participant failed");
    let status = resp.status().as_u16();
    let remove_json: Value = resp.json().expect("Invalid JSON from participant/remove");
    assert_eq!(status, 200, "remove participant failed: {remove_json}");

    std::thread::sleep(std::time::Duration::from_secs(3));

    // Verify peers[2] is absent
    let after_remove = get_json(&format!("chat/{guid}?with=participants"));
    let addrs_after_remove: Vec<String> = after_remove["data"]["participants"]
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .filter_map(|p| p["address"].as_str().map(|s| s.to_lowercase()))
        .collect();
    assert!(
        !addrs_after_remove.contains(&CONFIG.peers[2].to_lowercase()),
        "peers[2] should be absent after removal, participants: {addrs_after_remove:?}"
    );

    // --- Add peers[2] back (3→4) ---
    let add_body = json!({"address": &CONFIG.peers[2]});
    let resp = tc
        .post(url(&format!("chat/{guid}/participant/add")))
        .json(&add_body)
        .send()
        .expect("add participant failed");
    let status = resp.status().as_u16();
    let add_json: Value = resp.json().expect("Invalid JSON from participant/add");
    assert_eq!(status, 200, "add participant failed: {add_json}");

    std::thread::sleep(std::time::Duration::from_secs(3));

    // Verify peers[2] is present again
    let after_add = get_json(&format!("chat/{guid}?with=participants"));
    let addrs_after_add: Vec<String> = after_add["data"]["participants"]
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .filter_map(|p| p["address"].as_str().map(|s| s.to_lowercase()))
        .collect();
    assert!(
        addrs_after_add.contains(&CONFIG.peers[2].to_lowercase()),
        "peers[2] should be present after re-add, participants: {addrs_after_add:?}"
    );

    // --- Leave group (4→3, still valid) ---
    let resp = tc
        .post(url(&format!("chat/{guid}/leave")))
        .json(&json!({}))
        .send()
        .expect("leave group failed");
    let status = resp.status().as_u16();
    let leave_json: Value = resp.json().expect("Invalid JSON from /chat/leave");
    assert_eq!(status, 200, "leave group failed: {leave_json}");

    // Best-effort cleanup: delete the group
    let _ = delete_no_body(&format!("chat/{guid}"));
}
}