mahbot 0.9.0

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
//! Telegram transport tests: update and callback decoding, attachment markers,
//! listener framing, intake claims and Bot API HTML rendering.

use super::*;
use crate::channels::telegram_intake::{IN_PROGRESS, Intake, RETENTION_HOURS, TelegramIntakeStore};
use crate::util::test::{TicketFixture, canonical_without_verbatim_prefix};
use std::collections::VecDeque;
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::atomic::{AtomicBool, Ordering};

/// Create a Telegram Update JSON with sensible defaults, then apply
/// shallow top-level overrides for test-specific fields.
///
/// Base defaults:
/// ```json
/// { "update_id": 1, "message": { "message_id": 33, "text": "hello",
///   "from": {"id": 555, "username": "alice"},
///   "chat": {"id": -100_200_300} } }
/// ```
///
/// Overrides replace entire top-level keys (shallow merge). To change
/// nested fields, pass the full nested value:
///
/// ```ignore
/// let update = test_update(&[(
///     "message",
///     json!({
///         "message_id": 42, "text": "hi",
///         "from": {"id": 555, "username": "alice"},
///         "chat": {"id": -100_200_300},
///         "message_thread_id": 789
///     }),
/// )]);
/// ```
fn test_update(overrides: &[(&str, serde_json::Value)]) -> serde_json::Value {
    let mut update = serde_json::json!({
        "update_id": 1,
        "message": {
            "message_id": 33,
            "text": "hello",
            "from": { "id": 555, "username": "alice" },
            "chat": { "id": -100_200_300 }
        }
    });
    let obj = update.as_object_mut().unwrap();
    for (key, value) in overrides {
        obj.insert(key.to_string(), value.clone());
    }
    update
}

/// Create a TelegramChannel with a test store initialized.
/// Uses token `"token"` and calls `init_test_store` before returning.
async fn test_channel() -> TelegramChannel {
    crate::users::test_util::init_test_store().await;
    TelegramChannel::new_with("token".into(), CLOUD_API_BASE.to_string(), false)
}

#[test]
fn test_parse_recipient() {
    assert_eq!(parse_recipient("12345"), ("12345", None));
    assert_eq!(parse_recipient("12345:678"), ("12345", Some("678")));
    assert_eq!(parse_recipient(""), ("", None));
}

#[test]
fn test_markdown_to_telegram_html() {
    // escapes quotes in link href
    let r = markdown_to_telegram_html("[click](https://example.com?q=\"x\"&a='b')");
    assert_eq!(
        r,
        "<a href=\"https://example.com?q=&quot;x&quot;&amp;a=&#39;b&#39;\">click</a>"
    );
    // escapes quotes/ampersand in plain text
    let r = markdown_to_telegram_html("say \"hi\" & <tag> 'ok'");
    assert_eq!(r, "say &quot;hi&quot; &amp; &lt;tag&gt; &#39;ok&#39;");
    // drops language attribute from code blocks
    let r = markdown_to_telegram_html("```rust\" onclick=\"alert(1)\nlet x = 1;\n```");
    assert_eq!(r, "<pre><code>let x = 1;</code></pre>");
    assert!(!r.contains("language-"));
    assert!(!r.contains("onclick"));

    // Inline formatting inside code blocks is preserved literally
    let r = markdown_to_telegram_html("```\nsome **bold** and `code`\n```");
    assert_eq!(r, "<pre><code>some **bold** and `code`</code></pre>");

    // HTML special characters in code blocks are escaped
    let r = markdown_to_telegram_html("```\n<div> & \"it\" 'works'\n```");
    assert_eq!(
        r,
        "<pre><code>&lt;div&gt; &amp; &quot;it&quot; &#39;works&#39;</code></pre>"
    );

    // Literal </code> in code block must not break the HTML
    let r = markdown_to_telegram_html("```\nuse &lt;/code&gt;\n```");
    assert_eq!(r, "<pre><code>use &amp;lt;/code&amp;gt;</code></pre>");

    // ── Blockquote pass-through ────────────────────────────

    // Opening tag on its own line passes through unchanged
    let r = markdown_to_telegram_html("<blockquote>");
    assert_eq!(r, "<blockquote>");

    // Closing tag on its own line passes through unchanged
    let r = markdown_to_telegram_html("</blockquote>");
    assert_eq!(r, "</blockquote>");

    // Multi-line blockquote: content between tags gets inline formatting
    let r = markdown_to_telegram_html("<blockquote>\nHello **world**\n</blockquote>");
    assert_eq!(r, "<blockquote>\nHello <b>world</b>\n</blockquote>");

    // Malformed tag name: <blockquote123> should NOT pass through
    let r = markdown_to_telegram_html("<blockquote123>");
    assert_eq!(r, "&lt;blockquote123&gt;");

    // Tag with attributes: <blockquote class="x"> should NOT pass through
    let r = markdown_to_telegram_html("<blockquote class=\"x\">");
    assert_eq!(r, "&lt;blockquote class=&quot;x&quot;&gt;");

    // Tag with trailing space inside: <blockquote > should NOT pass through
    let r = markdown_to_telegram_html("<blockquote >");
    assert_eq!(r, "&lt;blockquote &gt;");

    // A heading with a link drops its bold so the link stays clickable.
    let r = markdown_to_telegram_html("## [docs](https://example.com)");
    assert_eq!(r, "<a href=\"https://example.com\">docs</a>");
}

/// The `/board` listing formats each ticket via [`format_board_line`] and
/// converts the joined text through `markdown_to_telegram_html` line-by-line.
/// A title with markdown-special characters must never corrupt other lines'
/// formatting or leave unbalanced HTML tags (which would make Telegram reject
/// the whole message).
#[test]
fn board_listing_isolates_hostile_titles() {
    // Pin the production format string — if `handle_board_listing`'s helper
    // drifts, this test fails instead of silently covering a stale fixture.
    assert_eq!(
        format_board_line(&TicketPhase::InDevelopment, "mahbot-123", "Title"),
        "🔨 `mahbot-123` Title"
    );

    let state = TicketPhase::InDevelopment;
    let lines = [
        format_board_line(&state, "mahbot-1", "Fix * unclosed italic"),
        format_board_line(&state, "mahbot-2", "Use `git status` and *pair* ok"),
        format_board_line(&state, "mahbot-3", "<script>alert(1)</script> & tags"),
        format_board_line(&state, "mahbot-4", "Bold **crash** inside title"),
        format_board_line(&state, "mahbot-5", "[link](https://example.com/x?y=1&z=2)"),
        format_board_line(&state, "mahbot-6", "backtick ` unclosed"),
        format_board_line(&state, "mahbot-7", "Normal ticket"),
    ];
    let listing = lines.join("\n");
    let html = markdown_to_telegram_html(&listing);

    // Every line keeps its phase emoji and monospace ID.
    for line in html.split('\n') {
        assert!(line.starts_with("🔨 "), "phase emoji lost: {line:?}");
        assert!(
            line.contains("<code>") && line.contains("</code> "),
            "id formatting lost: {line:?}"
        );
    }
    // No unbalanced tags anywhere — an unclosed marker in a title must be
    // escaped, not emitted as a dangling tag (that would degrade the whole
    // listing to plain text via Telegram's HTML parse failure fallback).
    for (open, close) in [("<b>", "</b>"), ("<i>", "</i>"), ("<code>", "</code>")] {
        assert_eq!(
            html.matches(open).count(),
            html.matches(close).count(),
            "unbalanced {open}/{close} in: {html}"
        );
    }
}

/// The `/board` reply text speaks for all workspaces: a header that says so over
/// one line per ticket, each entry marked by its own ticket id prefix — and an
/// empty case in the same voice.
#[test]
fn board_listing_text_speaks_for_all_workspaces() {
    assert_eq!(board_listing_text(&[]), "All workspaces — no tickets");

    let tickets = [
        TicketFixture::new("mahbot-1", TicketPhase::Backlog)
            .title("First")
            .build(),
        TicketFixture::new("mahbot_rlx-2", TicketPhase::InDevelopment)
            .title("Second")
            .build(),
    ];
    let ordered: Vec<&Ticket> = tickets.iter().collect();
    let text = board_listing_text(&ordered);

    let mut lines = text.lines();
    assert_eq!(lines.next(), Some("All workspaces — 2 tickets"));
    for ticket in &tickets {
        let line = lines.next().expect("one line per ticket");
        assert!(
            line.contains(&ticket.id) && line.contains(&ticket.title),
            "each entry is its own ticket's id and title, in order: {text:?}"
        );
    }
    assert!(lines.next().is_none(), "nothing is added after the listing");

    // One ticket is named in the singular, as the empty listing is called out.
    let one = [&tickets[0]];
    assert!(board_listing_text(&one).starts_with("All workspaces — 1 ticket\n"));
}

// ── Inline formatting tests ──────────────────────────────────────

#[test]
#[expect(clippy::too_many_lines)] // one cohesive table of inline-formatting cases
fn test_inline_formatting() {
    struct Case {
        name: &'static str,
        input: &'static str,
        expected: &'static str,
    }
    let cases = vec![
        Case {
            name: "bold double asterisk",
            input: "**hello** world",
            expected: "<b>hello</b> world",
        },
        Case {
            name: "bold double underscore",
            input: "__hello__ world",
            expected: "<b>hello</b> world",
        },
        Case {
            name: "italic",
            input: "*hello* world",
            expected: "<i>hello</i> world",
        },
        Case {
            name: "inline code",
            input: "use `hello()` in your code",
            expected: "use <code>hello()</code> in your code",
        },
        Case {
            name: "strikethrough",
            input: "this is ~~wrong~~ fixed",
            expected: "this is <s>wrong</s> fixed",
        },
        Case {
            name: "combined",
            input: "**bold** and *italic* and `code` and ~~strike~~",
            expected: "<b>bold</b> and <i>italic</i> and <code>code</code> and <s>strike</s>",
        },
        Case {
            name: "bold inside text",
            input: "before **middle** after",
            expected: "before <b>middle</b> after",
        },
        Case {
            name: "escaping inner HTML",
            input: "**a < b & c > d**",
            expected: "<b>a &lt; b &amp; c &gt; d</b>",
        },
        // `**` without closing should be rendered literally
        Case {
            name: "unmatched double asterisk",
            input: "hello ** world",
            expected: "hello ** world",
        },
        // `*` without closing should be rendered literally
        Case {
            name: "unmatched single asterisk",
            input: "hello * world",
            expected: "hello * world",
        },
        // `***` is not a valid bold or italic construct; rendered literally
        Case {
            name: "triple asterisk",
            input: "***",
            expected: "***",
        },
        // `` ` `` without closing should be rendered literally (the opening ` is pushed as text)
        Case {
            name: "unmatched backtick",
            input: "hello ` world",
            expected: "hello ` world",
        },
        // ` `` ` (two backticks) — the first opens, the second closes (empty content), and since
        // the `end > 0` guard rejects empty matches, both are rendered literally.
        Case {
            name: "double backtick",
            input: "hello `` world",
            expected: "hello `` world",
        },
        // `~` without matching pair should be rendered literally
        Case {
            name: "unmatched tilde",
            input: "hello ~ world",
            expected: "hello ~ world",
        },
        // Bold takes priority over italic for `**`
        Case {
            name: "bold and italic overlap",
            input: "***bold**",
            expected: "<b>*bold</b>",
        },
        // A formatting span whose content holds a markdown link drops its
        // formatting — the anchor is emitted at the top level, not nested.
        Case {
            name: "bold span with link",
            input: "**see [docs](https://example.com) now**",
            expected: "see <a href=\"https://example.com\">docs</a> now",
        },
        Case {
            name: "italic and strikethrough spans with link",
            input: "*[a](https://a.example)* and ~~[b](https://b.example)~~",
            expected: "<a href=\"https://a.example\">a</a> and <a href=\"https://b.example\">b</a>",
        },
        Case {
            name: "nested spans with link",
            input: "**bold *inner [docs](https://example.com)* tail**",
            expected: "bold inner <a href=\"https://example.com\">docs</a> tail",
        },
        // No link in the span, so the formatting stays.
        Case {
            name: "bold span without link",
            input: "**hello [not a link]**",
            expected: "<b>hello [not a link]</b>",
        },
        // Link syntax in code renders no anchor, so the bold around it stays
        // (span content is never re-parsed for formatting without a link).
        Case {
            name: "bold span around code with link syntax",
            input: "**bold `[docs](https://example.com)` tail**",
            expected: "<b>bold `[docs](https://example.com)` tail</b>",
        },
        // A media marker is delivery scaffolding, not prose: an anchor labelled
        // with the marker's own text would fabricate a link out of it.
        Case {
            name: "marker label is not a link",
            input: "[IMAGE:data:image/png;base64,AAAA](https://example.com)",
            expected: "[IMAGE:data:image/png;base64,AAAA](https://example.com)",
        },
        Case {
            name: "lowercase marker label is not a link",
            input: "[image:/tmp/x.txt](https://example.com)",
            expected: "[image:/tmp/x.txt](https://example.com)",
        },
        // Any other bracketed word is prose, however it is spelled.
        Case {
            name: "uppercase prose label stays a link",
            input: "[NOTE:42](https://example.com)",
            expected: "<a href=\"https://example.com\">NOTE:42</a>",
        },
    ];
    for case in cases {
        let result = markdown_to_telegram_html(case.input);
        assert_eq!(result, case.expected, "case: {}", case.name);
    }
}

#[tokio::test]
async fn parse_update_message_uses_chat_id_as_reply_target() {
    let ch = test_channel().await;
    let update = test_update(&[]);

    let msg = ch
        .parse_update_message(&update)
        .await
        .expect("parse the update")
        .expect("message should parse");

    assert_eq!(msg.user_name, "alice");
    assert_eq!(msg.reply_target, "-100200300");
    assert_eq!(msg.content, "hello");
}

/// Write a real 1×1 red PNG raster — the classifier rejects non-raster
/// "images", so fixtures must decode.
fn write_test_png(path: &std::path::Path) {
    let img = image::RgbaImage::from_pixel(1, 1, image::Rgba([255, 0, 0, 255]));
    let mut buf = Vec::new();
    img.write_to(&mut std::io::Cursor::new(&mut buf), image::ImageFormat::Png)
        .expect("test PNG must encode");
    std::fs::write(path, buf).unwrap();
}

#[test]
fn parse_attachment_markers_tests() {
    let dir = tempfile::tempdir().unwrap();
    let png = dir.path().join("a.png");
    let ogg = dir.path().join("voice.ogg");
    let vid = dir.path().join("vid.mp4");
    write_test_png(&png);
    std::fs::write(&ogg, b"fake-ogg").unwrap();
    std::fs::write(&vid, b"fake-mp4").unwrap();
    let roots = vec![dir.path().to_path_buf()];

    // A bare word naming no file is prose explaining the syntax: the one case
    // kept exactly as written.
    let (cleaned, att, refusals) = parse_attachment_markers(
        "use `[IMAGE:path]` or `[VIDEO:...]`",
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "use `[IMAGE:path]` or `[VIDEO:...]`");
    assert!(att.is_empty());
    assert!(refusals.is_empty());

    // Existing files become attachments, carrying the path as written.
    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!(
            "Here are files [IMAGE:{}] and [AUDIO:{}]",
            png.display(),
            ogg.display()
        ),
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "Here are files  and");
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 2);
    assert_eq!(att[0].kind, TelegramAttachmentKind::Image);
    assert_eq!(att[0].target, png.to_string_lossy());
    assert_eq!(att[1].kind, TelegramAttachmentKind::Audio);
    assert_eq!(att[1].target, ogg.to_string_lossy());

    // http(s) URLs become attachments: an image by address, a video fetched.
    let (cleaned, att, refusals) = parse_attachment_markers(
        "See [VIDEO:https://example.com/vid.mp4] and [IMAGE:https://example.com/a.png]",
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "See  and");
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 2);
    assert_eq!(att[0].kind, TelegramAttachmentKind::Video);
    assert_eq!(att[0].target, "https://example.com/vid.mp4");
    assert_eq!(att[1].kind, TelegramAttachmentKind::Image);
    assert_eq!(att[1].target, "https://example.com/a.png");

    // An image address that is not well-formed to fetch is refused, not sent
    // and not left as markup.
    let (cleaned, att, refusals) = parse_attachment_markers(
        "[IMAGE:https://example.com:bad/x.png]",
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"x.png\": it is not a valid address."]
    );

    // Unknown markers kept as text: a marker-shaped word of a kind outside the
    // vocabulary is not a marker at all, so the parser leaves the user's text
    // alone.
    let (cleaned, att, refusals) =
        parse_attachment_markers("Report [UNKNOWN:/tmp/a.bin]", &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "Report [UNKNOWN:/tmp/a.bin]");
    assert!(att.is_empty());
    assert!(refusals.is_empty());

    // Case-insensitive matching with a real file.
    let (cleaned, att, _) = parse_attachment_markers(
        &format!("[image:{}]", png.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert_eq!(att.len(), 1);
    assert_eq!(att[0].kind, TelegramAttachmentKind::Image);

    // Regression (case-insensitive gate): a lowercase `[image:...]` targeting an
    // existing NON-raster file must not bypass the classifier — it is refused
    // rather than shown to the user as marker text.
    let nonimg = dir.path().join("note.txt");
    std::fs::write(&nonimg, b"not an image").unwrap();
    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!("[image:{}]", nonimg.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert!(att.is_empty(), "a non-raster image marker must not attach");
    assert_eq!(
        refusals,
        vec!["Could not send \"note.txt\": it is not an image."]
    );
}

/// A relative media target names a file in the workspace the message belongs
/// to, exactly as `[FILE:...]` does: it is attached from the resolved ABSOLUTE
/// path, resolved against the first root that has it.
#[test]
fn a_relative_media_marker_resolves_against_the_messages_workspace() {
    let dir = tempfile::tempdir().unwrap();
    let other = tempfile::tempdir().unwrap();
    let generated = dir.path().join("generated");
    std::fs::create_dir(&generated).unwrap();
    let png = generated.join("a.png");
    let ogg = dir.path().join("voice.ogg");
    let vid = dir.path().join("clip.mp4");
    write_test_png(&png);
    std::fs::write(&ogg, b"fake-ogg").unwrap();
    std::fs::write(&vid, b"fake-mp4").unwrap();
    let roots = vec![dir.path().to_path_buf()];

    let (cleaned, att, refusals) = parse_attachment_markers(
        "[IMAGE:generated/a.png] [AUDIO:voice.ogg] [VIDEO:clip.mp4]",
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 3);
    assert_eq!(att[0].kind, TelegramAttachmentKind::Image);
    assert_eq!(att[0].target, png.to_string_lossy());
    assert_eq!(att[1].kind, TelegramAttachmentKind::Audio);
    assert_eq!(att[1].target, ogg.to_string_lossy());
    assert_eq!(att[2].kind, TelegramAttachmentKind::Video);
    assert_eq!(att[2].target, vid.to_string_lossy());

    // Relayed content names a file in the originating workspace: the root that
    // has it wins, as `[FILE:...]` already resolves.
    let relayed = other.path().join("relayed.png");
    write_test_png(&relayed);
    let (_, att, refusals) = parse_attachment_markers(
        "[IMAGE:relayed.png]",
        &[dir.path().to_path_buf(), other.path().to_path_buf()],
        TransferLimits::CLOUD,
    );
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1);
    assert_eq!(att[0].target, relayed.to_string_lossy());
}

/// A target that cannot be delivered never reaches the user as raw marker text:
/// it leaves a short refusal naming what could not be sent.
#[test]
fn an_undeliverable_marker_becomes_a_refusal() {
    let dir = tempfile::tempdir().unwrap();
    let roots = vec![dir.path().to_path_buf()];
    let pictures = dir.path().join("pictures");
    std::fs::create_dir(&pictures).unwrap();
    std::fs::write(dir.path().join("notes.txt"), b"not an image").unwrap();

    // A relative image no root holds: refused by name, with the marker gone.
    let (cleaned, att, refusals) =
        parse_attachment_markers("[IMAGE:missing/a.png]", &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(refusals, vec!["Could not send \"a.png\": file not found."]);

    // A directory target is refused as what it is.
    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!("[IMAGE:{}]", pictures.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"pictures\": it is a directory."]
    );

    // An existing file that is not a raster cannot be sent as a picture.
    let (cleaned, att, refusals) =
        parse_attachment_markers("[IMAGE:notes.txt]", &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"notes.txt\": it is not an image."]
    );

    // An inline payload has no file name worth quoting: the refusal names the
    // kind and never echoes the payload.
    let blob = format!("[IMAGE:data:image/png;base64,{}]", "A".repeat(500));
    let (cleaned, att, refusals) = parse_attachment_markers(&blob, &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"image\": it carries an inline payload, not a file."]
    );
}

/// One refused marker does not stop the others in the same message: every valid
/// target is attached and only the failing one leaves a notice, in body order.
#[test]
fn one_refused_marker_does_not_cost_the_others() {
    let dir = tempfile::tempdir().unwrap();
    let generated = dir.path().join("generated");
    std::fs::create_dir(&generated).unwrap();
    let png = generated.join("a.png");
    let vid = dir.path().join("clip.mp4");
    write_test_png(&png);
    std::fs::write(&vid, b"fake-mp4").unwrap();

    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!(
            "Two of three [IMAGE:generated/a.png] ok [IMAGE:missing/a.png] [VIDEO:{}]",
            vid.display()
        ),
        &[dir.path().to_path_buf()],
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "Two of three  ok");
    assert_eq!(att.len(), 2);
    assert_eq!(att[0].target, png.to_string_lossy());
    assert_eq!(att[1].kind, TelegramAttachmentKind::Video);
    assert_eq!(refusals, vec!["Could not send \"a.png\": file not found."]);
}

/// `[FILE:...]` is the one local-only kind: it resolves against the
/// authorizing workspace roots, carries the CANONICAL path, and turns every
/// refusal (rather than a literal marker) into a notice naming the file.
#[test]
fn parse_file_markers_resolve_within_workspace_roots() {
    let dir = tempfile::tempdir().unwrap();
    let outside = tempfile::tempdir().unwrap();
    let roots = vec![dir.path().to_path_buf()];
    let escape = outside.path().join("secret.txt");
    std::fs::write(&escape, b"secret").unwrap();

    // Inside the workspace: delivered as a document with the canonical path.
    let pdf = dir.path().join("specs.pdf");
    std::fs::write(&pdf, b"fake-pdf").unwrap();
    let canonical = canonical_without_verbatim_prefix(&pdf);
    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!("Here: [FILE:{}]", pdf.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "Here:");
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1);
    assert_eq!(att[0].kind, TelegramAttachmentKind::Document);
    assert_eq!(att[0].target, canonical.to_string_lossy());

    // A relative target resolves against the first (authoring) root.
    let (cleaned, att, refusals) =
        parse_attachment_markers("[FILE:specs.pdf]", &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "");
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1);
    assert_eq!(att[0].target, canonical.to_string_lossy());

    // A relative target that no earlier root holds resolves against the first
    // root that does: relayed Manager→Assistant content names a file in the
    // originating workspace, which is the last root.
    let other = tempfile::tempdir().unwrap();
    let relayed = other.path().join("relayed.pdf");
    std::fs::write(&relayed, b"fake-pdf").unwrap();
    let (cleaned, att, refusals) = parse_attachment_markers(
        "[FILE:relayed.pdf]",
        &[dir.path().to_path_buf(), other.path().to_path_buf()],
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1);
    assert_eq!(
        att[0].target,
        canonical_without_verbatim_prefix(&relayed).to_string_lossy()
    );

    // An existing file outside the roots is refused by name.
    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!("[FILE:{}]", escape.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"secret.txt\": it is outside your workspace."]
    );

    // A missing target is refused (no attachment, no literal marker left).
    let (cleaned, att, refusals) =
        parse_attachment_markers("[FILE:missing.pdf]", &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"missing.pdf\": file not found."]
    );

    // A directory target is refused.
    let sub = dir.path().join("reports");
    std::fs::create_dir(&sub).unwrap();
    let (_, att, refusals) = parse_attachment_markers(
        &format!("[FILE:{}]", sub.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"reports\": it is a directory."]
    );

    // `[FILE:...]` is local-only — a URL target is not part of the mechanism.
    let (cleaned, att, refusals) =
        parse_attachment_markers("[FILE:http://x/y.pdf]", &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"y.pdf\": an address is not a local file."]
    );

    // No roots → no workspace to authorize a local file (the plain-text/mirror
    // sends).
    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!("[FILE:{}]", pdf.display()),
        &[],
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"specs.pdf\": the message's workspace is unknown."]
    );

    // An inline payload is not a file to hand over, and the notice never echoes
    // it back at the user.
    let blob = format!("[FILE:data:application/pdf;base64,{}]", "A".repeat(500));
    let (cleaned, att, refusals) = parse_attachment_markers(&blob, &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"file\": it carries an inline payload, not a file."]
    );
}

/// A symlink inside the workspace pointing outside it is refused: containment
/// is checked against the CANONICAL target, so the link cannot smuggle a file
/// out of the workspace.
#[cfg(unix)]
#[test]
fn parse_file_markers_refuse_symlink_out_of_workspace() {
    let dir = tempfile::tempdir().unwrap();
    let outside = tempfile::tempdir().unwrap();
    let escape = outside.path().join("secret.txt");
    std::fs::write(&escape, b"secret").unwrap();
    let link = dir.path().join("link.txt");
    std::os::unix::fs::symlink(&escape, &link).unwrap();

    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!("[FILE:{}]", link.display()),
        &[dir.path().to_path_buf()],
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusals,
        vec!["Could not send \"link.txt\": it is outside your workspace."]
    );
}

/// Only the delivery route collapses for a video: where one may come from does
/// not. A remote video is fetched and uploaded — never handed to Telegram by
/// address, whose by-address document form would only accept a couple of types —
/// while images and audio keep the by-address methods, and any existing file is
/// uploaded from disk.
#[test]
fn a_video_route_depends_only_on_its_target() {
    use AttachmentRoute::{ByAddress, FetchThenUpload, LocalFile};

    assert_eq!(
        attachment_route(
            TelegramAttachmentKind::Video,
            "https://example.com/clip.mov"
        ),
        FetchThenUpload
    );
    assert_eq!(
        attachment_route(TelegramAttachmentKind::Video, "/nowhere/clip.mov"),
        LocalFile
    );
    assert_eq!(
        attachment_route(TelegramAttachmentKind::Audio, "https://example.com/a.ogg"),
        ByAddress
    );
    assert_eq!(
        attachment_route(TelegramAttachmentKind::Image, "https://example.com/a.png"),
        ByAddress
    );
}

/// A fetched video is named so a client can pick a player: the address's own
/// last segment when it is a video name, otherwise that stem with the extension
/// the declared type gives — the rule the receive direction already applies.
#[test]
fn a_fetched_video_is_named_for_a_player() {
    let name = |url: &str, content_type: Option<&str>| video_upload_name(url, content_type);
    assert_eq!(
        name("https://example.com/dir/clip.mov?sig=abc#t", None),
        "clip.mov"
    );
    assert_eq!(
        name("https://example.com/clip.mov", Some("video/mp4")),
        "clip.mov"
    );
    assert_eq!(
        name(
            "https://example.com/download?id=7",
            Some("video/quicktime; q=1")
        ),
        "download.mov"
    );
    assert_eq!(
        name("https://example.com/clip.php", Some("video/webm")),
        "clip.webm"
    );
    assert_eq!(name("https://example.com/", Some("video/mp4")), "video.mp4");
    assert_eq!(name("https://example.com", None), "video.mp4");
    assert_eq!(name("https://example.com/a/..", None), "video.mp4");
}

/// The document-upload wire shape: one upload, no video message, detection off, no
/// geometry claimed, and `bytes` carried verbatim under `name`.
fn assert_document_upload(fake: &FakeTelegram, name: &str, bytes: &str) {
    assert_eq!(fake.count("sendDocument"), 1);
    assert_eq!(fake.count("sendVideo"), 0);
    let body = String::from_utf8_lossy(&fake.bodies("sendDocument")[0]).into_owned();
    assert!(body.contains("name=\"document\""), "{body}");
    assert!(body.contains("disable_content_type_detection"), "{body}");
    assert!(body.contains(name), "the file keeps its name: {body}");
    assert!(
        body.contains(bytes),
        "the file's bytes go out as they are: {body}"
    );
    for geometry in [
        "width",
        "height",
        "duration",
        "thumbnail",
        "supports_streaming",
    ] {
        assert!(
            !body.contains(geometry),
            "no {geometry} may be claimed: {body}"
        );
    }
}

/// What the product actually puts on the wire for a video: a document upload
/// carrying the file's own bytes, with content-type detection disabled and no
/// geometry claim — the recipient's own player, not the message, describes the
/// clip.
#[tokio::test]
async fn a_video_is_uploaded_as_a_document_with_no_geometry() {
    crate::users::test_util::init_test_store().await;
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    let telegram = TelegramChannel::new_with("token-a".into(), fake.base.clone(), false);
    let dir = tempfile::tempdir().unwrap();
    let clip = dir.path().join("clip.mov");
    std::fs::write(&clip, b"fake-mov-bytes").unwrap();

    telegram
        .send(&SendMessage {
            content: format!("[VIDEO:{}]", clip.display()),
            recipient: "42".to_string(),
            reply_markup: None,
            file_roots: Vec::new(),
        })
        .await
        .expect("the video is delivered");

    assert_document_upload(&fake, "clip.mov", "fake-mov-bytes");
}

/// The composition the change turns on: a video addressed by a URL reaches the
/// chat as one document upload carrying the fetched bytes — never a video message,
/// never a handover by address.
#[tokio::test]
async fn a_remote_video_is_fetched_and_uploaded_as_a_document() {
    crate::users::test_util::init_test_store().await;
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_file("clip", b"remote-video-bytes", Duration::ZERO);
    let telegram = TelegramChannel::new_with("token-a".into(), fake.base.clone(), false);
    let url = format!("{}/file/bottoken-a/{}", fake.base, file_path_of("clip"));

    telegram
        .send(&SendMessage {
            content: format!("[VIDEO:{url}]"),
            recipient: "42".to_string(),
            reply_markup: None,
            file_roots: Vec::new(),
        })
        .await
        .expect("the remote video is delivered");

    assert_document_upload(&fake, "clip.mp4", "remote-video-bytes");
}

/// An image addressed by a URL keeps its by-address method: Telegram fetches and
/// serves it as a photo, not as an upload of ours.
#[tokio::test]
async fn an_image_by_address_keeps_the_photo_method() {
    crate::users::test_util::init_test_store().await;
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    let telegram = TelegramChannel::new_with("token-a".into(), fake.base.clone(), false);
    let url = "https://example.com/pictures/sunset.png".to_string();

    telegram
        .send(&SendMessage {
            content: format!("[IMAGE:{url}]"),
            recipient: "42".to_string(),
            reply_markup: None,
            file_roots: Vec::new(),
        })
        .await
        .expect("the image is delivered");

    assert_eq!(fake.count("sendPhoto"), 1);
    assert_eq!(fake.count("sendDocument"), 0);
    let body: serde_json::Value =
        serde_json::from_slice(&fake.bodies("sendPhoto")[0]).expect("the by-address body is JSON");
    assert_eq!(body["photo"], url);
}

/// `[FILE:]` keeps its route: a local file goes out as a document upload of its
/// own bytes.
#[tokio::test]
async fn a_file_is_uploaded_as_a_document() {
    crate::users::test_util::init_test_store().await;
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    let telegram = TelegramChannel::new_with("token-a".into(), fake.base.clone(), false);
    let dir = tempfile::tempdir().unwrap();
    let report = dir.path().join("report.pdf");
    std::fs::write(&report, b"%PDF-1.4 fake").unwrap();

    telegram
        .send(&SendMessage {
            content: format!("[FILE:{}]", report.display()),
            recipient: "42".to_string(),
            reply_markup: None,
            file_roots: vec![dir.path().to_path_buf()],
        })
        .await
        .expect("the file is delivered");

    assert_document_upload(&fake, "report.pdf", "%PDF-1.4 fake");
}

/// A fetched copy lives in a temp directory of its own and goes away with the
/// value that carries it, whatever the upload that follows did.
#[tokio::test]
async fn a_fetched_copy_is_removed_with_its_delivery() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_file("clip", b"remote-video-bytes", Duration::ZERO);
    let telegram = TelegramChannel::new_with("token-a".into(), fake.base.clone(), false);

    let url = format!("{}/file/bottoken-a/{}", fake.base, file_path_of("clip"));
    let fetched = telegram.fetch_remote_upload(&url).await.expect("fetch");
    let path = fetched.path.clone();
    drop(fetched);
    assert!(
        !path.exists(),
        "the fetched copy must not outlive the delivery"
    );
}

/// An address the model supplied passes the shared screen on every hop: the first
/// one it wrote and each one a redirect names. A metadata target is refused
/// whether addressed directly or reached through a hop, and a hop to the file
/// itself is followed.
#[tokio::test]
async fn an_address_is_screened_on_every_hop() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_file("clip.mov", b"remote-video-bytes", Duration::ZERO);
    let telegram = TelegramChannel::new_with("token-a".into(), fake.base.clone(), false);
    let file = format!("{}/file/bottoken-a/{}", fake.base, file_path_of("clip.mov"));

    let direct = telegram
        .fetch_remote_upload("http://169.254.169.254/latest/meta-data")
        .await
        .err()
        .expect("a metadata address is refused on the first hop");
    assert!(format!("{direct:#}").contains("SSRF"), "{direct:#}");

    let followed = telegram
        .fetch_remote_upload(&format!("{}/redirect?to={file}", fake.base))
        .await
        .expect("a redirect to the file itself is followed");
    assert_eq!(
        tokio::fs::read(&followed.path).await.unwrap(),
        b"remote-video-bytes"
    );

    let hopped = telegram
        .fetch_remote_upload(&format!(
            "{}/redirect?to=http://169.254.169.254/latest/meta-data",
            fake.base
        ))
        .await
        .err()
        .expect("a metadata hop is refused");
    assert!(format!("{hopped:#}").contains("SSRF"), "{hopped:#}");
}

/// The shared body stream refuses a declared length over its ceiling before the
/// file is created, and stores a body at the ceiling byte for byte — the bound
/// both the inbound download and the outbound fetch rely on.
#[tokio::test]
async fn a_declared_length_over_the_ceiling_is_refused_before_writing() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_file("clip", b"twelve bytes", Duration::ZERO);
    let telegram = TelegramChannel::new_with("token-a".into(), fake.base.clone(), false);
    let url = format!("{}/file/bottoken-a/{}", fake.base, file_path_of("clip"));
    let dir = tempfile::tempdir().unwrap();
    let over_limit = |size: u64, max: u64| anyhow::anyhow!("{size} over {max}");

    let over = dir.path().join("over.bin");
    let response = telegram.transfer_client().get(&url).send().await.unwrap();
    let refused = stream_capped(response, &over, Some(4), over_limit)
        .await
        .expect_err("the declared length is over the ceiling");
    assert_eq!(refused.to_string(), "12 over 4");
    assert!(!over.exists(), "a refused body must not be created");

    let exact = dir.path().join("exact.bin");
    let response = telegram.transfer_client().get(&url).send().await.unwrap();
    stream_capped(response, &exact, Some(12), over_limit)
        .await
        .expect("a body at the ceiling is delivered");
    assert_eq!(tokio::fs::read(&exact).await.unwrap(), b"twelve bytes");
}

/// A video address whose bytes cannot be had is reported, never pasted into the
/// reply as a bare link: the user reads why, and nothing is uploaded.
#[tokio::test]
async fn an_unfetchable_video_address_is_reported_not_pasted_as_a_link() {
    crate::users::test_util::init_test_store().await;
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    let telegram = TelegramChannel::new_with("token-a".into(), fake.base.clone(), false);
    // Nothing listens on port 1: the fetch fails before any upload.
    let address = "http://127.0.0.1:1/clip.mp4".to_string();

    telegram
        .send(&SendMessage {
            content: format!("[VIDEO:{address}]"),
            recipient: "42".to_string(),
            reply_markup: None,
            file_roots: Vec::new(),
        })
        .await
        .expect_err("a failed delivery is reported to the caller");

    assert_eq!(fake.count("sendDocument"), 0);
    // The body is rendered as Telegram HTML, so the quotes arrive escaped.
    assert_eq!(
        fake.sent_texts(),
        vec!["Could not send &quot;clip.mp4&quot;: it could not be fetched or sent."]
    );
}

#[test]
fn bare_path_without_marker_is_plain_text() {
    let dir = tempfile::tempdir().unwrap();
    let png = dir.path().join("snap.png");
    write_test_png(&png);

    // A reply that is exactly an existing file path — but carries no marker —
    // must be delivered as plain text with no attachment.
    let path = png.to_string_lossy().to_string();
    let (cleaned, att, _) =
        parse_attachment_markers(&path, &[dir.path().to_path_buf()], TransferLimits::CLOUD);
    assert_eq!(cleaned, path);
    assert!(att.is_empty());
}

/// The local server hands a file over as a path on this machine instead of
/// serving it: the file is copied from there — left in place, because the server's
/// own record still names it — and no HTTP request is involved (the base points at
/// a port nothing listens on).
#[tokio::test]
async fn a_local_server_hands_a_file_over_as_a_path() {
    let root = tempfile::tempdir().unwrap();
    let _files = crate::util::test::set_env_var(
        "MAHBOT_TGAPI_FILES_DIR",
        Some(root.path().to_str().unwrap()),
    );
    let source = root.path().join("clip.bin");
    std::fs::write(&source, vec![7u8; 4096]).unwrap();
    let dest = root.path().join("staged.bin");
    let channel = TelegramChannel::new_with("token".into(), "http://127.0.0.1:9".into(), true);

    channel
        .download_file(&source.to_string_lossy(), &dest, Some(4096))
        .await
        .expect("the file on disk is taken without a transfer");

    assert_eq!(std::fs::read(&dest).unwrap(), vec![7u8; 4096]);
    assert!(
        source.exists(),
        "the server's copy stays where its own record names it"
    );
}

/// A hand-over naming a file outside the directory the product gave the server is
/// refused: nothing is copied, and the named file is not deleted.
#[tokio::test]
async fn a_local_hand_over_outside_the_servers_directory_is_refused() {
    let root = tempfile::tempdir().unwrap();
    let _files = crate::util::test::set_env_var(
        "MAHBOT_TGAPI_FILES_DIR",
        Some(root.path().to_str().unwrap()),
    );
    let elsewhere = tempfile::tempdir().unwrap();
    let source = elsewhere.path().join("owner.txt");
    std::fs::write(&source, b"owner data").unwrap();
    let dest = root.path().join("staged.txt");
    let channel = TelegramChannel::new_with("token".into(), "http://127.0.0.1:9".into(), true);

    let started = std::time::Instant::now();
    let err = channel
        .download_file(&source.to_string_lossy(), &dest, Some(10))
        .await
        .expect_err("a hand-over outside the server's directory must be refused");
    assert!(
        err.to_string().contains(LOCAL_FILE_OUTSIDE_REASON),
        "the refusal must name the hand-over: {err}"
    );
    // Refused as what it is, before the wait: not probed for a minute and then
    // reported as a transfer that never finished.
    assert!(
        started.elapsed() < Duration::from_secs(5),
        "an outside path is not waited for"
    );
    assert!(source.exists(), "the named file must be left untouched");
    assert!(!dest.exists(), "nothing may be copied out of the refusal");
}

/// A size that stops changing short of the declared one is a stall, not the end of
/// the transfer: the wait keeps waiting and completes when the write reaches the
/// size Telegram declared.
#[tokio::test]
async fn a_hand_over_survives_a_stall_and_completes_at_the_declared_size() {
    let dir = tempfile::tempdir().unwrap();
    let source = dir.path().join("clip.bin");
    std::fs::write(&source, vec![7u8; 3]).unwrap();

    let (waited, ()) = tokio::join!(wait_for_hand_over(&source, Some(9)), async {
        // Long enough for the size to look settled for several samples.
        tokio::time::sleep(Duration::from_millis(300)).await;
        std::fs::write(&source, vec![7u8; 9]).unwrap();
    });
    waited.expect("a stalled write is not the end of the transfer");
}

/// A hand-over that is exactly the declared size is the whole file as soon as it is
/// measured; with no declared size there is nothing to compare against, so a size
/// that no longer changes is what completes it.
#[tokio::test]
async fn a_hand_over_at_the_declared_size_is_complete() {
    let dir = tempfile::tempdir().unwrap();
    let source = dir.path().join("clip.bin");
    std::fs::write(&source, vec![7u8; 9]).unwrap();

    wait_for_hand_over(&source, Some(9))
        .await
        .expect("a file of the size the attachment declared is the whole file");
    wait_for_hand_over(&source, None)
        .await
        .expect("an undeclared size is complete once it stops changing");
}

/// Each way a local hand-over fails reaches the owner as what it is: a file handed
/// over from outside the directory the server was given, one that never arrived, and
/// one that stopped short are three different faults, and only a failure nothing
/// recognises is still the transport's own refusal.
#[test]
fn every_local_hand_over_failure_reaches_the_owner_as_itself() {
    let outside: anyhow::Error = LocalHandOver::Outside.into();
    assert_eq!(download_failure_reason(&outside), LOCAL_FILE_OUTSIDE_REASON);

    let missing: anyhow::Error = LocalHandOver::Missing.into();
    assert_eq!(download_failure_reason(&missing), LOCAL_FILE_MISSING_REASON);

    let incomplete: anyhow::Error = LocalHandOver::Incomplete {
        got: 3,
        expected: 9,
    }
    .into();
    assert_eq!(
        download_failure_reason(&incomplete),
        local_file_incomplete_reason(3, 9)
    );

    let unknown = anyhow::anyhow!("the staging disk went away");
    assert_eq!(
        download_failure_reason(&unknown),
        TELEGRAM_TRANSFER_REFUSED_REASON
    );
}

/// The limit a refusal names is the one in force: the cloud caps a download at
/// 20 MB, the local server has no cap at all.
#[test]
fn the_refusal_limit_is_the_one_the_mode_has() {
    assert!(declared_size_refusal(TransferLimits::CLOUD, 20 * 1024 * 1024 + 1).is_some());
    assert!(declared_size_refusal(TransferLimits::LOCAL, 20 * 1024 * 1024 + 1).is_none());
    assert_eq!(TransferLimits::LOCAL.upload, 2000 * 1024 * 1024);
    assert_eq!(TransferLimits::CLOUD.upload, FILE_MAX_BYTES);
}

/// Boundaries of the declared-size gate: exactly at the cloud's limit passes,
/// one byte over trips it, and the local mode — which has no download cap — never
/// refuses on size at all.
#[test]
fn declared_size_refusal_boundaries() {
    const MIB: u64 = 1024 * 1024;
    let cloud = TransferLimits::CLOUD;
    let limit = telegram_download_limit_reason(TELEGRAM_MAX_FILE_DOWNLOAD_BYTES);
    let cases: &[(u64, Option<String>)] = &[
        (0, None),
        (20 * MIB, None),
        (20 * MIB + 1, Some(limit.clone())),
        (2000 * MIB, Some(limit)),
    ];
    for (size, expected) in cases {
        assert_eq!(
            declared_size_refusal(cloud, *size),
            *expected,
            "size {size} bytes"
        );
    }
    assert_eq!(declared_size_refusal(TransferLimits::LOCAL, u64::MAX), None);
}

/// The rejection note is caption-then-note, with an empty caption treated as
/// absent so no stray blank line reaches the agent; a nameless rejection (the
/// recorded-voice refusal) leaves the name out instead of inventing one.
#[test]
fn attachment_rejection_content_shapes() {
    let reason = telegram_download_limit_reason(TELEGRAM_MAX_FILE_DOWNLOAD_BYTES);
    assert_eq!(
        attachment_rejection_content(Some("report.pdf"), NOT_RECEIVED, &reason, Some("my report")),
        format!("my report\n\n[File report.pdf: not received — {reason}]")
    );
    assert_eq!(
        attachment_rejection_content(Some("report.pdf"), NOT_STORED, &reason, None),
        format!("[File report.pdf: could not be stored — {reason}]")
    );
    assert_eq!(
        attachment_rejection_content(None, NOT_RECEIVED, &reason, None),
        format!("[File not received — {reason}]")
    );
    for note in [
        attachment_rejection_content(Some("report.pdf"), NOT_RECEIVED, &reason, None),
        attachment_rejection_content(None, NOT_RECEIVED, &reason, None),
    ] {
        assert!(
            !crate::util::MEDIA_MARKER_RE.is_match(&note),
            "a rejection note must not read as a media marker: {note}"
        );
    }
}

#[tokio::test]
async fn parse_update_message_denies_user_without_username() {
    let ch = test_channel().await;
    let update = test_update(&[(
        "message",
        serde_json::json!({
            "message_id": 9,
            "text": "ping",
            "from": {
                "id": 555
            },
            "chat": {
                "id": 12345
            }
        }),
    )]);

    assert!(
        ch.parse_update_message(&update)
            .await
            .expect("parse the update")
            .is_none(),
        "user without username should be denied"
    );
}

#[tokio::test]
async fn parse_update_message_extracts_thread_id_for_forum_topic() {
    let ch = test_channel().await;
    let update = test_update(&[(
        "message",
        serde_json::json!({
            "message_id": 42,
            "text": "hello from topic",
            "from": {
                "id": 555,
                "username": "alice"
            },
            "chat": {
                "id": -100_200_300
            },
            "message_thread_id": 789
        }),
    )]);

    let msg = ch
        .parse_update_message(&update)
        .await
        .expect("parse the update")
        .expect("message with thread_id should parse");

    assert_eq!(msg.user_name, "alice");
    assert_eq!(msg.reply_target, "-100200300:789");
    assert_eq!(msg.content, "hello from topic");
}

/// Helper: create a callback_query sub-object for testing.
/// Overrides are applied as top-level keys on the callback_query.
fn test_callback_query(overrides: &[(&str, serde_json::Value)]) -> serde_json::Value {
    let mut cq = serde_json::json!({
        "id": "12345",
        "data": "set_model|test-model",
        "from": { "id": 555, "username": "alice" },
        "message": {
            "message_id": 100,
            "chat": { "id": -100_200_300 },
            "date": 1_700_000_000
        }
    });
    let obj = cq.as_object_mut().unwrap();
    for (key, value) in overrides {
        obj.insert(key.to_string(), value.clone());
    }
    cq
}

#[tokio::test]
async fn parse_callback_query_returns_message_with_extracted_fields() {
    let ch = test_channel().await;
    let cq = test_callback_query(&[]);

    let msg = ch
        .parse_callback_query(&cq)
        .await
        .expect("parse the query")
        .expect("callback query should parse with valid user");

    assert_eq!(msg.user_name, "alice");
    assert_eq!(msg.reply_target, "-100200300");
    assert_eq!(msg.content, "set_model|test-model");
    assert_eq!(msg.channel, "telegram");
    assert_eq!(msg.callback_query_id.as_deref(), Some("12345"));
    assert_eq!(msg.chat_id.as_deref(), Some("-100200300"));
    assert_eq!(msg.message_id, Some(100));
}

#[tokio::test]
async fn parse_callback_query_rejects_invalid_inputs() {
    let ch = test_channel().await;
    let unknown_user = serde_json::json!({ "id": 999, "username": "unknown_user" });
    let cases = [
        ("no data", [("data", serde_json::Value::Null)]),
        ("no message", [("message", serde_json::Value::Null)]),
        ("unknown user", [("from", unknown_user)]),
    ];
    for (name, overrides) in &cases {
        let cq = test_callback_query(overrides);
        assert!(
            ch.parse_callback_query(&cq)
                .await
                .expect("parse the query")
                .is_none(),
            "case {name}: expected rejection"
        );
    }
}

#[tokio::test]
async fn parse_callback_query_accepts_empty_data_and_null_id() {
    // Regression pair: the ? guard in parse_callback_query only rejects
    // absent/null data — NOT an empty string, which yields empty content
    // (distinct from process_updates' unwrap_or("") ACK check). A
    // null/absent callback_query_id likewise stays valid, becoming None
    // rather than Some — unlike missing data which rejects.
    let ch = test_channel().await;
    let cases = [
        (
            "empty-string data",
            [("data", serde_json::json!(""))],
            "",
            Some("12345"),
        ),
        (
            "null id",
            [("id", serde_json::Value::Null)],
            "set_model|test-model",
            None,
        ),
    ];
    for (name, overrides, content, cq_id) in &cases {
        let cq = test_callback_query(overrides);
        let msg = ch
            .parse_callback_query(&cq)
            .await
            .expect("parse the query")
            .unwrap_or_else(|| panic!("case {name}: expected a valid message"));
        assert_eq!(msg.content, *content, "case {name}");
        assert_eq!(msg.callback_query_id.as_deref(), *cq_id, "case {name}");
    }
}

#[test]
fn telegram_message_splitting() {
    // basic: exact limit → no split
    assert_eq!(
        split_message_for_telegram(&"a".repeat(TELEGRAM_MAX_MESSAGE_LENGTH)).len(),
        1
    );
    assert!(split_message_for_telegram(&"a".repeat(TELEGRAM_MAX_MESSAGE_LENGTH + 1)).len() >= 2);
    let long = "a".repeat(5000);
    let parts = split_message_for_telegram(&long);
    assert!(parts.len() >= 2);
    assert_eq!(parts.join(""), long);
    assert!(split_message_for_telegram("   \n\n\t  ").len() <= 1);

    // edge: code block spanning boundary
    let msg = format!("```python\n{}```\nMore text", "x".repeat(4085));
    for p in &split_message_for_telegram(&msg) {
        assert!(p.len() <= TELEGRAM_MAX_MESSAGE_LENGTH);
    }
    // emoji at boundary
    let msg = format!("{}🎉🎊", "a".repeat(4094));
    for p in &split_message_for_telegram(&msg) {
        assert!(p.chars().count() <= TELEGRAM_MAX_MESSAGE_LENGTH);
    }
}

#[test]
fn newline_split_fallback_prevents_mid_word_break() {
    // Regression: when the only newline is in the first half of the
    // search window and no spaces exist, the old code would hard-split
    // mid-word. The newline fallback (tier 3) prevents this.
    let msg = format!("{}\n{}", "a".repeat(1000), "x".repeat(5000));
    let chunks = split_message_for_telegram(&msg);

    // All chunks must respect the length limit
    for (i, chunk) in chunks.iter().enumerate() {
        assert!(
            chunk.chars().count() <= TELEGRAM_MAX_MESSAGE_LENGTH,
            "chunk {} has {} chars (limit {})",
            i,
            chunk.chars().count(),
            TELEGRAM_MAX_MESSAGE_LENGTH,
        );
    }

    // Concatenation must reconstruct the original message
    assert_eq!(chunks.join(""), msg);

    // The first chunk must end with the newline (not split mid-word)
    assert!(
        chunks[0].ends_with('\n'),
        "first chunk should end with newline, got: {:?}",
        chunks[0].chars().rev().take(10).collect::<String>()
    );
}

#[test]
fn wrapped_chunks_respect_telegram_limit() {
    // Simulate send_text_chunks wrapping to verify no chunk exceeds the
    // 4096-char limit after continuation prefixes are prepended.
    //
    // Middle chunk overhead: "(continued)\n\n...\n\n(continues...)" = 29 chars
    // Last chunk overhead:  "(continued)\n\n..."                   = 13 chars
    // First chunk (multi):  "...\n\n(continues...)"               = 16 chars

    // Build a message designed to force 3+ chunks, then verify wrapping.
    let msg = format!("X{}X", "a".repeat(9000));
    let chunks = split_message_for_telegram(&msg);
    assert!(
        chunks.len() >= 3,
        "expected 3+ chunks to exercise all continuation variants"
    );

    for (i, chunk) in chunks.iter().enumerate() {
        let wrapped = wrap_chunk(chunk, i, chunks.len());
        assert!(
            wrapped.chars().count() <= 4096,
            "chunk {} wrapped length {} exceeds 4096",
            i,
            wrapped.chars().count()
        );
    }

    // Boundary: last chunk exactly at 4066 chars (new limit) — wrapping
    // produces 4066+13=4079 ≤ 4096.  Under the old code, a 4095-char last
    // chunk would wrap to 4108 > 4096.
    //
    // Intentionally uses raw format! rather than wrap_chunk() — this tests
    // the splitter's TELEGRAM_CONTINUATION_OVERHEAD constant, not the
    // wrapping helper. wrap_chunk would return the chunk bare for total==1.
    let boundary = "b".repeat(4066);
    let chunks = split_message_for_telegram(&boundary);
    assert_eq!(chunks.len(), 1, "4066-char message should not split");
    let wrapped = format!("(continued)\n\n{}", chunks[0]);
    assert!(
        wrapped.chars().count() <= 4096,
        "boundary wrapped: {} > 4096",
        wrapped.chars().count()
    );

    // Old bug reproducer: a 4095-char last chunk would wrap to 4108.
    // With the fix, a message that produces a ~4095-char last chunk
    // shouldn't exist; the splitter caps non-first chunks at 4066.
    let near_limit = "c".repeat(4096);
    let chunks = split_message_for_telegram(&near_limit);
    assert_eq!(chunks.len(), 1, "4096-char message should not split");
}

#[test]
fn tag_extension_clamped_to_telegram_limit() {
    // Regression: extend_past_open_tag could push a chunk
    // past the sendable limit when the split point landed inside a long
    // HTML tag whose '>' fell beyond the 4066-char window. The raw chunk
    // then exceeded 4096 chars and Telegram rejected the send (HTML and
    // the plain-text retry both fail), dropping the message.
    //
    // Well-formed variant of the ticket repro — what
    // markdown_to_telegram_html actually emits: a link whose href opens
    // within the last ~100 chars of the window and whose '>' lands past
    // it. Under the old code the first chunk was 4151 chars (wrapped 4166).
    let tag = format!(
        "<a href=\"https://example.com/{}\">link</a>",
        "y".repeat(120)
    );

    // ASCII body — the original overflow reproducer.
    let ascii_msg = format!("{}{}", "x".repeat(4000), tag);
    // Multibyte bodies: the clamp must target the byte offset of the
    // 4066-char boundary (hard_split), never a raw byte count — clamping
    // to byte 4066 panics on 3-byte CJK and under-fills 2-byte Cyrillic.
    let cjk_msg = format!("{}{}", "界".repeat(4000), tag);
    let cyrillic_msg = format!("{}{}", "ы".repeat(4000), tag);

    for (name, msg) in [
        ("ascii", ascii_msg),
        ("cjk", cjk_msg),
        ("cyrillic", cyrillic_msg),
    ] {
        let chunks = split_message_for_telegram(&msg);
        // Reconstruction must be lossless.
        assert_eq!(chunks.join(""), msg, "{name}: reconstruction");
        assert!(chunks.len() >= 2, "{name}: expected a split");
        for (i, chunk) in chunks.iter().enumerate() {
            // Raw chunks never exceed the 4066-char budget...
            assert!(
                chunk.chars().count()
                    <= TELEGRAM_MAX_MESSAGE_LENGTH - TELEGRAM_CONTINUATION_OVERHEAD,
                "{name}: chunk {i} raw {} chars exceeds the 4066 budget",
                chunk.chars().count(),
            );
            // ...and the wrapped form (as sent by send_text_chunks) never
            // exceeds Telegram's hard 4096-char limit.
            let wrapped = wrap_chunk(chunk, i, chunks.len());
            assert!(
                wrapped.chars().count() <= TELEGRAM_MAX_MESSAGE_LENGTH,
                "{name}: chunk {i} wrapped {} chars exceeds 4096",
                wrapped.chars().count(),
            );
        }
        // The budget is used effectively: the first chunk sits near the
        // 4066-char ceiling instead of being truncated by a byte clamp.
        assert!(
            chunks[0].chars().count() >= 4000,
            "{name}: first chunk only {} chars — budget underused",
            chunks[0].chars().count(),
        );
    }
}

// ─────────────────────────────────────────────────────────────────────
// extract_sender_user_name tests
// ─────────────────────────────────────────────────────────────────────

#[test]
fn test_extract_sender_user_name() {
    let username =
        extract_sender_user_name(&serde_json::json!({"from": {"id": 123, "username": "alice"}}));
    assert_eq!(username, "alice");
    let username = extract_sender_user_name(&serde_json::json!({"from": {"id": 42}}));
    assert_eq!(username, "unknown");
}

#[tokio::test]
async fn usernameless_sender_stays_unauthorized_despite_legacy_binding() {
    // Initializes the shared global test store with alice/bob bound.
    let _ch = test_channel().await;
    // Simulate a legacy row binding the "unknown" sentinel to alice.
    crate::users::store()
        .bind_channel("alice", "telegram", "unknown")
        .await
        .unwrap();
    // The sender carries no nickname at all and no numeric binding exists, so
    // the legacy "unknown" row must not authorize them.
    for sender in [
        // A real username-less sender.
        serde_json::json!({"from": {"id": 1}}),
        // A sender whose reported nickname is the reserved word itself.
        serde_json::json!({"from": {"id": 1, "username": "unknown"}}),
    ] {
        assert!(
            resolve_authorized_sender(&sender, &serde_json::json!({"chat": {"id": 42}}))
                .await
                .expect("resolve the sender")
                .is_none(),
            "the reserved sentinel must never authorize: {sender}"
        );
    }
    // Clean up so the shared store isn't polluted for later tests.
    crate::users::store()
        .unbind_channel("alice", "telegram", "unknown")
        .await
        .unwrap();
}

#[tokio::test]
async fn usernameless_sender_is_authorized_by_a_numeric_binding() {
    // The same sender as the sentinel test above, but bound by number: the
    // no-nickname refusal belongs to the nickname way only, so the legacy
    // "unknown" row (still bound to alice here) must not swallow a numeric
    // match. The message itself carries no nickname at all.
    let _ch = test_channel().await;
    let store = crate::users::store();
    store
        .bind_channel("alice", "telegram", "unknown")
        .await
        .unwrap();
    store.add_user("num_user").await.unwrap();
    store.bind_telegram("num_user", "31415926").await.unwrap();
    assert_eq!(
        resolve_authorized_sender(
            &serde_json::json!({"from": {"id": 31_415_926}}),
            &serde_json::json!({"chat": {"id": 42}}),
        )
        .await
        .expect("resolve the sender"),
        Some(("num_user".to_string(), "42".to_string(), "42".to_string())),
    );
    // Clean up so the shared store isn't polluted for later tests.
    store
        .unbind_channel("num_user", "telegram", "31415926")
        .await
        .unwrap();
    store
        .unbind_channel("alice", "telegram", "unknown")
        .await
        .unwrap();
}

#[tokio::test]
async fn numeric_binding_outranks_a_nickname_bound_to_another_account() {
    let _ch = test_channel().await;
    let store = crate::users::store();
    store.add_user("num_rank").await.unwrap();
    store.add_user("nick_rank").await.unwrap();
    // The nickname's row is visited before the number's (the scan follows the
    // `(channel, identifier)` unique index, and `!` sorts before any digit), so
    // an implementation returning the first matching row of either kind would
    // return the nickname's account: the number has to win on identity, not on
    // scan order.
    store.bind_telegram("num_rank", "27182818").await.unwrap();
    store.bind_telegram("nick_rank", "!ranknick").await.unwrap();

    // The sender carries both identities, each bound to a different account:
    // the number decides, since it is the one that survives a rename.
    let authorized = resolve_authorized_sender(
        &serde_json::json!({"from": {"id": 27_182_818, "username": "!ranknick"}}),
        &serde_json::json!({"chat": {"id": 42}}),
    )
    .await
    .expect("resolve the sender")
    .expect("the number binds");
    assert_eq!(authorized.0, "num_rank");

    // Clean up so the shared store isn't polluted for later tests.
    store
        .unbind_channel("num_rank", "telegram", "27182818")
        .await
        .unwrap();
    store
        .unbind_channel("nick_rank", "telegram", "!ranknick")
        .await
        .unwrap();
}

#[tokio::test]
async fn bot_sender_is_refused_even_when_its_nickname_is_bound() {
    // "alice" is bound to alice in the shared fixtures, so only the bot
    // identity itself can produce the refusal.
    let _ch = test_channel().await;
    assert!(
        resolve_authorized_sender(
            &serde_json::json!({
                "from": {"id": 424_242, "username": "alice", "is_bot": true}
            }),
            &serde_json::json!({"chat": {"id": 42}}),
        )
        .await
        .expect("resolve the sender")
        .is_none()
    );
}

#[tokio::test]
async fn chat_attributed_message_is_refused_even_when_its_sender_is_bound() {
    // A channel post (or the anonymous group administrator) cites a `from`
    // that names the bound nickname, yet the message speaks for the chat, not
    // for that person.
    let _ch = test_channel().await;
    assert!(
        resolve_authorized_sender(
            &serde_json::json!({
                "sender_chat": {"id": -100_200_300, "type": "channel", "title": "Chan"},
                "from": {"id": 424_243, "username": "alice"}
            }),
            &serde_json::json!({"chat": {"id": 42}}),
        )
        .await
        .expect("resolve the sender")
        .is_none()
    );
}

#[tokio::test]
async fn telegram_service_identity_senders_are_refused() {
    let _ch = test_channel().await;
    // Each carries a bound nickname too, so only the identity itself can
    // produce the refusal.
    let cases = [
        ("anonymous group administrator", 1_087_968_824_i64),
        ("message attributed to a channel", 136_817_688),
        ("auto-forwarded channel post", 777_000),
    ];
    for (name, id) in cases {
        assert!(
            resolve_authorized_sender(
                &serde_json::json!({"from": {"id": id, "username": "alice"}}),
                &serde_json::json!({"chat": {"id": 42}}),
            )
            .await
            .expect("resolve the sender")
            .is_none(),
            "case {name}: expected rejection"
        );
    }
}

#[tokio::test]
async fn callback_query_authorizes_the_person_when_its_message_is_chat_attributed() {
    // The clicker is `from` of the callback_query; the keyboard's message can
    // be chat-attributed (`sender_chat`), which must not refuse the person who
    // pressed the button.
    let ch = test_channel().await;
    let cq = test_callback_query(&[(
        "message",
        serde_json::json!({
            "message_id": 100,
            "sender_chat": {"id": -100_200_300, "type": "channel", "title": "Chan"},
            "chat": {"id": -100_200_300}
        }),
    )]);

    let msg = ch
        .parse_callback_query(&cq)
        .await
        .expect("parse the query")
        .expect("the clicker's own identity authorizes");
    assert_eq!(msg.user_name, "alice");
    assert_eq!(msg.chat_id.as_deref(), Some("-100200300"));
    assert_eq!(msg.message_id, Some(100));
}

// ─────────────────────────────────────────────────────────────────────
// build_reply_reference / replied_to_sender_label tests
// ─────────────────────────────────────────────────────────────────────

#[test]
fn build_reply_reference_text_snippet_normalized() {
    // Entities decoded, angle brackets stripped, newlines collapsed.
    let message = serde_json::json!({
        "reply_to_message": {
            "from": { "username": "alice" },
            "text": "say &quot;hi&quot; &amp; <bye>\nsecond line"
        }
    });
    let reply = build_reply_reference(&message).unwrap();
    assert_eq!(reply.author, "@alice");
    assert_eq!(reply.snippet, "say \"hi\" & bye second line");
}

#[test]
fn build_reply_reference_snippet_caps_at_max() {
    let long = "a".repeat(crate::channels::reply::REPLY_SNIPPET_MAX_CHARS + 50);
    let message = serde_json::json!({
        "reply_to_message": {
            "from": { "username": "alice" },
            "text": long
        }
    });
    let reply = build_reply_reference(&message).unwrap();
    assert_eq!(
        reply.snippet.chars().count(),
        crate::channels::reply::REPLY_SNIPPET_MAX_CHARS
    );
    assert!(reply.snippet.ends_with('…'));
}

#[test]
fn build_reply_reference_caption_fallback() {
    // A media message with an empty `text` falls back to the caption.
    let message = serde_json::json!({
        "reply_to_message": {
            "from": { "username": "bob" },
            "photo": [{ "file_id": "x", "width": 1, "height": 1 }],
            "caption": "at the <beach>"
        }
    });
    let reply = build_reply_reference(&message).unwrap();
    assert_eq!(reply.snippet, "at the beach");
}

#[test]
fn build_reply_reference_media_kind_placeholders() {
    for (field, expected) in [
        ("photo", "[Photo]"),
        ("document", "[Document]"),
        ("video", "[Video]"),
        ("voice", "[Voice message]"),
        ("audio", "[Voice message]"),
        ("sticker", "[Sticker]"),
        ("animation", "[Message]"),
    ] {
        let mut reply_to = serde_json::Map::new();
        reply_to.insert("from".to_string(), serde_json::json!({ "username": "bob" }));
        reply_to.insert(field.to_string(), serde_json::json!({ "file_id": "x" }));
        let message = serde_json::json!({ "reply_to_message": reply_to });
        let reply = build_reply_reference(&message).unwrap();
        assert_eq!(reply.snippet, expected, "field: {field}");
    }
    // Unknown kind (video_note) also falls back to [Message].
    let message = serde_json::json!({
        "reply_to_message": {
            "from": { "username": "bob" },
            "video_note": { "file_id": "x" }
        }
    });
    assert_eq!(
        build_reply_reference(&message).unwrap().snippet,
        "[Message]"
    );
}

#[test]
fn build_reply_reference_no_reply_is_none() {
    let message = serde_json::json!({ "text": "no reply here" });
    assert!(build_reply_reference(&message).is_none());
    // A null reply_to_message (not an object) is also "no reply".
    let message = serde_json::json!({ "text": "hi", "reply_to_message": null });
    assert!(build_reply_reference(&message).is_none());
}

#[test]
fn build_reply_reference_mirrored_message() {
    // A reply to a GUI-mirrored message: its raw text IS the mirror's own
    // blockquote HTML, which normalizes away leaving just the `↩` line.
    let message = serde_json::json!({
        "reply_to_message": {
            "from": { "username": "alice" },
            "text": "<blockquote>\n↩ alice: hi there\n</blockquote>"
        }
    });
    let reply = build_reply_reference(&message).unwrap();
    assert_eq!(reply.snippet, "↩ alice: hi there");
}

#[test]
fn replied_to_sender_label_chain() {
    // @username.
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "username": "alice" } })),
        "@alice"
    );
    // No username + first_name → first_name.
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "id": 1, "first_name": "Bob" } })),
        "Bob"
    );
    // Neither → "user".
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "id": 1 } })),
        "user"
    );
    // sender_chat with title → title.
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "sender_chat": { "title": "Daily News" } })),
        "Daily News"
    );
    // sender_chat without title → "channel".
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "sender_chat": { "id": 1 } })),
        "channel"
    );
    // Bot with username → @username.
    assert_eq!(
        replied_to_sender_label(
            &serde_json::json!({ "from": { "is_bot": true, "username": "poll_bot" } })
        ),
        "@poll_bot"
    );
    // Bot without username → "bot".
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "is_bot": true } })),
        "bot"
    );
    // Angle brackets in first_name are stripped.
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "first_name": "<Bob>" } })),
        "Bob"
    );
    // Neither sender_chat nor from → "user".
    assert_eq!(replied_to_sender_label(&serde_json::json!({})), "user");
}

#[tokio::test]
async fn parse_update_message_populates_reply_reference() {
    let ch = test_channel().await;
    let update = test_update(&[(
        "message",
        serde_json::json!({
            "message_id": 10,
            "text": "translate this",
            "from": { "id": 1, "username": "alice" },
            "chat": { "id": 100, "type": "private" },
            "reply_to_message": {
                "from": { "username": "bot" },
                "text": "Bonjour le monde"
            }
        }),
    )]);
    let parsed = ch
        .parse_update_message(&update)
        .await
        .expect("parse the update")
        .expect("a message");
    // No leading `> ` quote — the content is the raw user text only.
    assert_eq!(parsed.content, "translate this");
    let reply = parsed
        .reply_reference
        .expect("parsed message should carry the reply reference");
    assert_eq!(reply.author, "@bot");
    assert_eq!(reply.snippet, "Bonjour le monde");
}

#[tokio::test]
async fn attachment_message_reply_carries_reference() {
    // `try_parse_attachment_message` builds its context through
    // `extract_message_context`, so a reply to a media message must also
    // carry the reference (the no-silent-drop path for replies to media).
    let ch = test_channel().await;
    let message = serde_json::json!({
        "message_id": 7,
        "from": { "id": 1, "username": "alice" },
        "chat": { "id": 100, "type": "private" },
        "photo": [{ "file_id": "x", "width": 1, "height": 1 }],
        "reply_to_message": {
            "from": { "username": "bob" },
            "photo": [{ "file_id": "y", "width": 1, "height": 1 }]
        }
    });
    let ctx = ch
        .extract_message_context(&message)
        .await
        .expect("resolve the context")
        .expect("valid context");
    let reply = ctx.reply_reference.expect("media reply carries reference");
    assert_eq!(reply.author, "@bob");
    assert_eq!(reply.snippet, "[Photo]");
    assert_eq!(ctx.chat_id, "100");
    assert_eq!(ctx.message_id, 7);
}

#[tokio::test]
async fn reply_that_is_a_command_parses_as_command() {
    let ch = test_channel().await;
    let update = test_update(&[(
        "message",
        serde_json::json!({
            "message_id": 11,
            "text": "/clear",
            "from": { "id": 1, "username": "alice" },
            "chat": { "id": 100, "type": "private" },
            "reply_to_message": {
                "from": { "username": "bot" },
                "text": "some earlier text"
            }
        }),
    )]);
    let parsed = ch
        .parse_update_message(&update)
        .await
        .expect("parse the update")
        .expect("a message");
    // The raw command text reaches the content, so parse_bot_command matches
    // and the command handler takes over (the reply reference is dropped).
    assert_eq!(parsed.content, "/clear");
    assert!(crate::parse_bot_command(&parsed.content).is_some());
}

#[tokio::test]
async fn reply_to_command_keeps_reference() {
    let ch = test_channel().await;
    let update = test_update(&[(
        "message",
        serde_json::json!({
            "message_id": 12,
            "text": "why did you clear?",
            "from": { "id": 1, "username": "alice" },
            "chat": { "id": 100, "type": "private" },
            "reply_to_message": {
                "from": { "username": "bot" },
                "text": "/clear"
            }
        }),
    )]);
    let parsed = ch
        .parse_update_message(&update)
        .await
        .expect("parse the update")
        .expect("a message");
    // A non-command reply to a command message keeps its reference and routes
    // to the agent as usual.
    assert_eq!(parsed.content, "why did you clear?");
    assert!(crate::parse_bot_command(&parsed.content).is_none());
    let reply = parsed
        .reply_reference
        .expect("non-command reply keeps its reference");
    assert_eq!(reply.snippet, "/clear");
}

// ── IncomingAttachment / parse_attachment_metadata tests ─────────

#[test]
fn test_parse_attachment_metadata() {
    // Document with all fields
    let att = TelegramChannel::parse_attachment_metadata(&serde_json::json!({
            "document": {"file_id": "BQ", "file_name": "report.pdf", "file_size": 12345, "mime_type": "application/pdf"}
        }))
        .unwrap();
    assert_eq!(att.kind, IncomingAttachmentKind::Document);
    assert_eq!(att.file_id, "BQ");
    assert_eq!(att.file_name.as_deref(), Some("report.pdf"));
    assert_eq!(att.file_size, Some(12345));
    assert_eq!(att.mime_type.as_deref(), Some("application/pdf"));
    assert!(att.caption.is_none());
    // Photo (picks largest by file_size)
    let att = TelegramChannel::parse_attachment_metadata(&serde_json::json!({
            "photo": [{"file_id": "small_id", "file_size": 100}, {"file_id": "large_id", "file_size": 2000}]
        })).unwrap();
    assert_eq!(att.kind, IncomingAttachmentKind::Photo);
    assert_eq!(att.file_id, "large_id");
    assert_eq!(att.file_size, Some(2000));
    // Caption extraction
    let att = TelegramChannel::parse_attachment_metadata(&serde_json::json!({
        "document": {"file_id": "doc_id", "file_name": "data.csv"}, "caption": "Monthly report"
    }))
    .unwrap();
    assert_eq!(att.caption.as_deref(), Some("Monthly report"));
    let att = TelegramChannel::parse_attachment_metadata(&serde_json::json!({
        "photo": [{"file_id": "photo_id", "file_size": 1000}], "caption": "Look at this"
    }))
    .unwrap();
    assert_eq!(att.caption.as_deref(), Some("Look at this"));
    // Document without optional fields
    let att = TelegramChannel::parse_attachment_metadata(&serde_json::json!({
        "document": {"file_id": "doc_no_name"}
    }))
    .unwrap();
    assert_eq!(att.kind, IncomingAttachmentKind::Document);
    assert_eq!(att.file_id, "doc_no_name");
    assert!(att.file_name.is_none());
    assert!(att.file_size.is_none());
    // Document with mime_type extraction
    let att = TelegramChannel::parse_attachment_metadata(&serde_json::json!({
        "document": {"file_id": "img_doc", "mime_type": "image/png"}
    }))
    .unwrap();
    assert_eq!(att.kind, IncomingAttachmentKind::Document);
    assert_eq!(att.mime_type.as_deref(), Some("image/png"));
    // Voice message — its own kind: Telegram carries no file name, and off
    // macOS the receive path refuses it rather than pretending it is a sound
    // file it could transcribe.
    let att = TelegramChannel::parse_attachment_metadata(
        &serde_json::json!({"voice": {"file_id": "v", "duration": 5}}),
    )
    .unwrap();
    assert_eq!(att.kind, IncomingAttachmentKind::Voice);
    assert_eq!(att.file_id, "v");
    assert!(att.file_name.is_none());
    // Video message
    let att = TelegramChannel::parse_attachment_metadata(&serde_json::json!({
        "video": {"file_id": "vid", "file_name": "clip.mp4", "file_size": 12345, "mime_type": "video/mp4", "duration": 4}
    }))
    .unwrap();
    assert_eq!(att.kind, IncomingAttachmentKind::Video);
    assert_eq!(att.file_id, "vid");
    assert_eq!(att.file_name.as_deref(), Some("clip.mp4"));
    assert_eq!(att.file_size, Some(12345));
    assert_eq!(att.mime_type.as_deref(), Some("video/mp4"));
    // Video note (no file_name/mime_type)
    let att = TelegramChannel::parse_attachment_metadata(
        &serde_json::json!({"video_note": {"file_id": "vn", "duration": 3, "file_size": 999}}),
    )
    .unwrap();
    assert_eq!(att.kind, IncomingAttachmentKind::Video);
    assert_eq!(att.file_id, "vn");
    assert!(att.file_name.is_none());
    assert!(att.mime_type.is_none());
    // Animation (GIF)
    let att = TelegramChannel::parse_attachment_metadata(&serde_json::json!({
        "animation": {"file_id": "anim", "file_name": "sticker.gif", "mime_type": "video/mp4"}
    }))
    .unwrap();
    assert_eq!(att.kind, IncomingAttachmentKind::Video);
    assert_eq!(att.file_id, "anim");
    assert_eq!(att.file_name.as_deref(), Some("sticker.gif"));
    // Audio message
    let att = TelegramChannel::parse_attachment_metadata(
        &serde_json::json!({"audio": {"file_id": "a", "file_name": "song.mp3", "file_size": 999}}),
    )
    .unwrap();
    assert_eq!(att.kind, IncomingAttachmentKind::Audio);
    assert_eq!(att.file_id, "a");
    assert_eq!(att.file_name.as_deref(), Some("song.mp3"));
    assert_eq!(att.file_size, Some(999));
    // No attachment cases
    assert!(
        TelegramChannel::parse_attachment_metadata(&serde_json::json!({"text": "Hello"})).is_none()
    );
    assert!(
        TelegramChannel::parse_attachment_metadata(&serde_json::json!({"photo": []})).is_none()
    );
}

// ── Attachment content format tests ──────────────────────────────

#[test]
#[expect(clippy::too_many_lines)]
fn attachment_content_format_rules() {
    // photo → [IMAGE:]
    let c = format_attachment_content(
        IncomingAttachmentKind::Photo,
        std::path::Path::new("/tmp/workspace/photo.jpg"),
        None,
    );
    assert_eq!(c, "[IMAGE:/tmp/workspace/photo.jpg]");
    // document → [FILE:]
    let c = format_attachment_content(
        IncomingAttachmentKind::Document,
        std::path::Path::new("/tmp/workspace/report.pdf"),
        None,
    );
    assert_eq!(c, "[FILE:/tmp/workspace/report.pdf]");
    assert!(!c.contains("[IMAGE:"));
    // A nameless, MIME-less image still reaches the model as an image: with
    // neither the name nor the declared type classifying it, the bytes decide
    // through the same gate enrichment uses for [IMAGE:...].
    let dir = tempfile::tempdir().unwrap();
    let nameless = dir.path().join("file_1_2.bin");
    write_test_png(&nameless);
    let c = format_attachment_content(IncomingAttachmentKind::Document, &nameless, None);
    assert_eq!(c, format!("[IMAGE:{}]", nameless.display()));
    // A nameless non-image falls through to [FILE:].
    let blob = dir.path().join("file_1_3.bin");
    std::fs::write(&blob, b"definitely not a raster").unwrap();
    let c = format_attachment_content(IncomingAttachmentKind::Document, &blob, None);
    assert_eq!(c, format!("[FILE:{}]", blob.display()));
    let c = format_attachment_content(
        IncomingAttachmentKind::Photo,
        std::path::Path::new("/tmp/workspace/notes.md"),
        None,
    );
    assert!(!c.contains("[IMAGE:"));
    assert!(c.starts_with("[FILE:"));
    // non-image files classified as Photo fall back to [FILE:]
    for path in [
        "/tmp/workspace/file.md",
        "/tmp/workspace/file.txt",
        "/tmp/workspace/file.pdf",
        "/tmp/workspace/file.csv",
        "/tmp/workspace/file.json",
        "/tmp/workspace/file.zip",
        "/tmp/workspace/file",
    ] {
        let c = format_attachment_content(
            IncomingAttachmentKind::Photo,
            std::path::Path::new(path),
            None,
        );
        assert!(!c.contains("[IMAGE:"), "{path}: should not get [IMAGE:]");
        assert!(c.starts_with("[FILE:"), "{path}: should use [FILE:]");
    }
    // image extensions produce [IMAGE:] (PNG/JPEG/WebP only — gif/bmp are
    // deliberately not routed as images)
    for ext in ["png", "jpg", "jpeg", "webp"] {
        let c = format_attachment_content(
            IncomingAttachmentKind::Photo,
            std::path::Path::new(&format!("/tmp/workspace/photo.{ext}")),
            None,
        );
        assert!(c.starts_with("[IMAGE:"), "{ext}: should get [IMAGE:]");
    }
    // Document kind + .jpg extension → [IMAGE:] (not [FILE:])
    let c = format_attachment_content(
        IncomingAttachmentKind::Document,
        std::path::Path::new("/tmp/workspace/image.jpg"),
        None,
    );
    assert_eq!(c, "[IMAGE:/tmp/workspace/image.jpg]");
    // Document kind + no extension + jpeg MIME types → [IMAGE:] (mime
    // fallback). Both the canonical image/jpeg and the legacy image/jpg
    // alias are admitted.
    for mime in ["image/jpeg", "image/jpg"] {
        let c = format_attachment_content(
            IncomingAttachmentKind::Document,
            std::path::Path::new("/tmp/workspace/image_no_ext"),
            Some(mime),
        );
        assert_eq!(c, "[IMAGE:/tmp/workspace/image_no_ext]", "{mime}");
    }
    // gif/bmp MIME types are NOT routed as images (mime fallback whitelist) —
    // they fall through to [FILE:]. The legacy image/x-ms-bmp alias is
    // excluded too.
    for mime in ["image/gif", "image/bmp", "image/x-ms-bmp"] {
        let c = format_attachment_content(
            IncomingAttachmentKind::Document,
            std::path::Path::new("/tmp/workspace/anim_no_ext"),
            Some(mime),
        );
        assert!(!c.contains("[IMAGE:"), "{mime}: should not get [IMAGE:]");
        assert!(c.starts_with("[FILE:"), "{mime}: should use [FILE:]");
    }
    // A sound file is transcribed where the local audio subsystem exists
    // ([AUDIO:]) and handed over as an ordinary attachment ([FILE:]) where it
    // does not — the marker follows the platform, not the extension.
    let audio_marker = |path: &str| {
        if cfg!(target_os = "macos") {
            format!("[AUDIO:{path}]")
        } else {
            format!("[FILE:{path}]")
        }
    };
    let c = format_attachment_content(
        IncomingAttachmentKind::Audio,
        std::path::Path::new("/tmp/workspace/voice.ogg"),
        None,
    );
    assert_eq!(c, audio_marker("/tmp/workspace/voice.ogg"));
    let c = format_attachment_content(
        IncomingAttachmentKind::Audio,
        std::path::Path::new("/tmp/workspace/song.mp3"),
        Some("audio/mpeg"),
    );
    assert_eq!(c, audio_marker("/tmp/workspace/song.mp3"));
    // Video kind produces [VIDEO:] marker
    let c = format_attachment_content(
        IncomingAttachmentKind::Video,
        std::path::Path::new("/tmp/workspace/clip.mp4"),
        None,
    );
    assert_eq!(c, "[VIDEO:/tmp/workspace/clip.mp4]");
    // Document kind + video extension → [VIDEO:] (like image extension routing)
    let c = format_attachment_content(
        IncomingAttachmentKind::Document,
        std::path::Path::new("/tmp/workspace/clip.mp4"),
        None,
    );
    assert_eq!(c, "[VIDEO:/tmp/workspace/clip.mp4]");
    // Document kind + no extension + mime_type "video/mp4" → [VIDEO:] (mime fallback)
    let c = format_attachment_content(
        IncomingAttachmentKind::Document,
        std::path::Path::new("/tmp/workspace/clip_no_ext"),
        Some("video/mp4"),
    );
    assert_eq!(c, "[VIDEO:/tmp/workspace/clip_no_ext]");
    // Video kind with a non-video extension falls back to [FILE:]
    let c = format_attachment_content(
        IncomingAttachmentKind::Video,
        std::path::Path::new("/tmp/workspace/notes.txt"),
        None,
    );
    assert_eq!(c, "[FILE:/tmp/workspace/notes.txt]");
}

// ── Filename sanitizer tests ─────────────────────────────────────

#[test]
fn sanitize_attachment_filename_neutralizes_traversal() {
    let fallback = "file_1_2.pdf";
    // Only the final component survives, so an absolute path or a `..` climb
    // can never escape the per-message directory.
    assert_eq!(
        sanitize_attachment_filename("../../etc/passwd", fallback),
        "passwd"
    );
    assert_eq!(
        sanitize_attachment_filename("/etc/passwd", fallback),
        "passwd"
    );
    // No component at all → the generated fallback.
    for rejected in ["..", ".", "", "/", "a/.."] {
        assert_eq!(
            sanitize_attachment_filename(rejected, fallback),
            fallback,
            "{rejected:?}"
        );
    }
    // The fallback is cleaned the same way, so the marker invariant does not
    // depend on the caller having pre-sanitized it.
    assert_eq!(
        sanitize_attachment_filename("..", "we]ird[\u{7}name.bin"),
        "we_ird__name.bin"
    );
    // A Windows-style path is one component on unix; both separators are
    // mapped out so it cannot smuggle one.
    assert_eq!(
        sanitize_attachment_filename("C:\\evil\\x.txt", fallback),
        "C:_evil_x.txt"
    );
    // Control characters map to `_`.
    assert_eq!(
        sanitize_attachment_filename("a\nb\r\t.txt", fallback),
        "a_b__.txt"
    );
    // Brackets map out too: the emitted `[FILE:<path>]` marker's path group
    // cannot carry a `]`, and an unsanitized one would truncate the marker.
    assert_eq!(
        sanitize_attachment_filename("notes]v2[1].pdf", fallback),
        "notes_v2_1_.pdf"
    );
    // A leading dot is prefixed so no dotfile is created.
    assert_eq!(
        sanitize_attachment_filename(".bashrc", fallback),
        "_.bashrc"
    );
    // A pathologically long name is truncated with the extension preserved:
    // 180 bytes total − 4 for ".pdf".
    let long = format!("{}.pdf", "x".repeat(500));
    let sanitized = sanitize_attachment_filename(&long, fallback);
    assert_eq!(sanitized, format!("{}.pdf", "x".repeat(176)));
    // A long extension-only name must not become a dotfile either, and the
    // leading `_` counts toward the budget.
    let long_ext = format!(".{}", "y".repeat(200));
    let dotted = sanitize_attachment_filename(&long_ext, fallback);
    assert!(dotted.starts_with("_."), "{dotted}");
    assert!(
        dotted.len() <= MAX_ATTACHMENT_FILENAME_BYTES,
        "{dotted} is {} bytes",
        dotted.len()
    );
    // The budget is hard: an extension longer than it leaves no stem room, so
    // the name truncates wholesale instead of appending the full extension.
    let huge_ext = format!("call.log.{}", "y".repeat(200));
    let capped = sanitize_attachment_filename(&huge_ext, fallback);
    assert_eq!(capped.len(), MAX_ATTACHMENT_FILENAME_BYTES);
    assert!(capped.starts_with("call.log."), "{capped}");
    // The same budget with a leading character that cannot be cut in two: the
    // stem keeps nothing, and a bare extension would be a dotfile.
    let narrow = format!("🙂.{}", "y".repeat(177));
    let squeezed = sanitize_attachment_filename(&narrow, fallback);
    assert!(!squeezed.starts_with('.'), "{squeezed}");
    assert_eq!(squeezed.len(), MAX_ATTACHMENT_FILENAME_BYTES);
}

#[test]
fn sanitize_attachment_filename_caps_bytes_not_chars() {
    let fallback = "file_1_2.bin";
    // The filesystem's per-component limit is bytes, so a name in a
    // multi-byte script must be cut by the byte budget: a character cap would
    // leave 360–480 bytes on a 120-char name and the write would fail. The
    // stems below fit 176 bytes ("漢" is 3 bytes → 58 whole characters, "🙂" is
    // 4 → 44) next to the preserved ".png", and truncation must land on a char
    // boundary.
    for (unit, stem_len) in [('漢', 58), ('🙂', 44)] {
        let long = format!("{}.png", unit.to_string().repeat(200));
        let sanitized = sanitize_attachment_filename(&long, fallback);
        assert!(
            sanitized.len() <= MAX_ATTACHMENT_FILENAME_BYTES,
            "{sanitized} is {} bytes",
            sanitized.len()
        );
        assert_eq!(
            sanitized,
            format!("{}.png", unit.to_string().repeat(stem_len)),
            "{unit}"
        );
    }
}

#[test]
fn local_attachment_name_uses_sanitized_sender_name_or_fallback() {
    let attachment = IncomingAttachment {
        file_id: "f".to_string(),
        file_name: Some("../../etc/passwd".to_string()),
        file_size: None,
        caption: None,
        kind: IncomingAttachmentKind::Document,
        mime_type: None,
    };
    assert_eq!(local_attachment_name(&attachment, "123", 7, None), "passwd");
    // Telegram often sends no file_name (photo/video_note/voice) — the
    // generated name carries the kind and the message ids.
    let generated = local_attachment_name(
        &IncomingAttachment {
            file_name: None,
            kind: IncomingAttachmentKind::Video,
            ..attachment.clone()
        },
        "123",
        7,
        None,
    );
    assert_eq!(generated, "video_123_7.mp4");
    // A degenerate sender name has no stem to carry an extension swap, so it
    // falls through to the generated name instead of becoming `_...mp4`.
    for degenerate in ["..", ".", ""] {
        assert_eq!(
            local_attachment_name(
                &IncomingAttachment {
                    file_name: Some(degenerate.to_string()),
                    kind: IncomingAttachmentKind::Video,
                    ..attachment.clone()
                },
                "123",
                7,
                Some("mp4"),
            ),
            "video_123_7.mp4",
            "name {degenerate:?}"
        );
    }
    // A document gets a format-neutral default: its extension carries no
    // routing meaning, and the image-shaped "jpg" read oddly in the notice.
    assert_eq!(
        local_attachment_name(
            &IncomingAttachment {
                file_name: None,
                ..attachment
            },
            "123",
            7,
            None
        ),
        "file_123_7.bin"
    );
}

#[test]
fn local_attachment_name_sanitizes_remote_extension_fallback() {
    let attachment = IncomingAttachment {
        file_id: "f".to_string(),
        file_name: None,
        file_size: None,
        caption: None,
        kind: IncomingAttachmentKind::Document,
        mime_type: None,
    };
    // Telegram reports no file_name for many kinds, so the fallback's
    // extension comes from `getFile` — remote input that must not survive
    // into the emitted `[FILE:<path>]` marker path group.
    let hostile = "..]x/漢\u{7}evil\u{202e}png";
    let name = local_attachment_name(&attachment, "123", 7, Some(hostile));
    assert_eq!(name, "file_123_7.xevilpng");
    // A remote extension that sanitizes to nothing falls back to the default.
    let name = local_attachment_name(&attachment, "123", 7, Some("]/漢\u{7}"));
    assert_eq!(name, "file_123_7.bin");
    // A pathologically long one is still capped, in bytes.
    let long_ext = "a".repeat(500);
    let name = local_attachment_name(&attachment, "123", 7, Some(long_ext.as_str()));
    assert_eq!(name.len(), MAX_ATTACHMENT_FILENAME_BYTES);
    assert!(name.starts_with("file_123_7."), "{name}");
}

#[test]
fn attachment_image_and_video_helpers() {
    // has_extension over telegram's receive-path IMAGE_EXTENSIONS
    for p in [
        "photo.png",
        "photo.jpg",
        "photo.jpeg",
        "photo.webp",
        "PHOTO.PNG",
    ] {
        assert!(crate::util::has_extension(
            std::path::Path::new(p),
            super::IMAGE_EXTENSIONS
        ));
    }
    // gif/bmp are NOT image extensions anymore (codec trim)
    for p in ["photo.gif", "photo.bmp"] {
        assert!(!crate::util::has_extension(
            std::path::Path::new(p),
            super::IMAGE_EXTENSIONS
        ));
    }
    for p in ["file.md", "file.txt", "file.pdf", "file.csv", "file"] {
        assert!(!crate::util::has_extension(
            std::path::Path::new(p),
            super::IMAGE_EXTENSIONS
        ));
    }
    // is_video_extension
    for p in [
        "clip.mp4",
        "clip.mov",
        "clip.mkv",
        "clip.avi",
        "clip.webm",
        "CLIP.MP4",
    ] {
        assert!(crate::util::is_video_extension(std::path::Path::new(p)));
    }
    for p in ["file.md", "file.png", "file", "clip.mpg"] {
        assert!(!crate::util::is_video_extension(std::path::Path::new(p)));
    }
}

#[test]
fn video_filename_normalization() {
    // GIF-picker animations: ".gif" file_name, H.264 MP4 bytes → mp4 extension
    assert_eq!(
        normalize_video_filename(
            IncomingAttachmentKind::Video,
            "tenor.gif",
            Some("video/mp4")
        ),
        "tenor.mp4"
    );
    // Recognized video extensions pass through untouched
    assert_eq!(
        normalize_video_filename(
            IncomingAttachmentKind::Video,
            "clip.webm",
            Some("video/webm")
        ),
        "clip.webm"
    );
    // No-extension names gain .mp4
    assert_eq!(
        normalize_video_filename(IncomingAttachmentKind::Video, "video_123_45", None),
        "video_123_45.mp4"
    );
    // Documents without a video MIME keep their name
    assert_eq!(
        normalize_video_filename(IncomingAttachmentKind::Document, "tenor.gif", None),
        "tenor.gif"
    );
    // Documents with a video MIME and a non-video extension get a video
    // extension derived from the MIME (webm not mislabeled as mp4)
    assert_eq!(
        normalize_video_filename(
            IncomingAttachmentKind::Document,
            "clip.xyz",
            Some("video/mp4")
        ),
        "clip.mp4"
    );
    assert_eq!(
        normalize_video_filename(
            IncomingAttachmentKind::Document,
            "clip.xyz",
            Some("video/webm")
        ),
        "clip.webm"
    );
}

// ── Album placement + merge tests ───────────────────────────────

/// One polled update of a photo album: only the field the placement plan reads.
fn album_member_update(message_id: i64, group: &str) -> serde_json::Value {
    test_update(&[(
        "message",
        serde_json::json!({ "message_id": message_id, "media_group_id": group }),
    )])
}

/// A button press whose keyboard sits on an album photo: the press carries its
/// own `message`, whose `media_group_id` puts it in that album's raw shape.
fn album_button_press_update(group: &str) -> serde_json::Value {
    test_update(&[(
        "callback_query",
        test_callback_query(&[(
            "message",
            serde_json::json!({ "message_id": 42, "media_group_id": group }),
        )]),
    )])
}

#[test]
fn poll_plan_parses_each_album_at_its_first_member() {
    // An album of two, separated by a plain message, plus a one-member album:
    // the album is parsed whole at its first member, the message between its
    // members stays where it is, and the later member is skipped there.
    let updates = vec![
        album_member_update(41, "g1"),
        test_update(&[]),
        album_member_update(42, "g1"),
        album_member_update(43, "g2"),
    ];
    assert_eq!(
        poll_plan(&updates),
        vec![
            PollPlacement::Album(vec![0, 2]),
            PollPlacement::Single,
            PollPlacement::Skip,
            PollPlacement::Album(vec![3]),
        ]
    );
}

#[test]
fn poll_plan_keeps_a_button_press_out_of_its_album() {
    // A press carries the message its button sits on, so a press on an album
    // photo must never join that album: joined, the photo would be handed over a
    // second time as an inbound message, or — where the press is the first member
    // of the batch, so the album is parsed there — the album's other members
    // would stay skipped and be lost.
    let press_first = vec![
        album_button_press_update("g1"),
        album_member_update(42, "g1"),
        album_member_update(43, "g1"),
    ];
    assert_eq!(
        poll_plan(&press_first),
        vec![
            PollPlacement::Callback,
            PollPlacement::Album(vec![1, 2]),
            PollPlacement::Skip,
        ]
    );

    let press_between = vec![
        album_member_update(41, "g1"),
        album_button_press_update("g1"),
        album_member_update(43, "g1"),
    ];
    assert_eq!(
        poll_plan(&press_between),
        vec![
            PollPlacement::Album(vec![0, 2]),
            PollPlacement::Callback,
            PollPlacement::Skip,
        ]
    );
}

#[test]
fn album_members_merge_into_one_message() {
    let first_chat = "-100";
    let second_chat = "-200";
    let first = ChannelMessage {
        message_id: Some(41),
        chat_id: Some(first_chat.into()),
        content: "a".into(),
        attachment_dirs: vec![crate::util::telegram_staging_dir_name(first_chat, 41)],
        update_ids: vec![7],
        ..test_msg("alice", "", "telegram", "chat")
    };
    let second = ChannelMessage {
        message_id: Some(42),
        chat_id: Some(second_chat.into()),
        content: "b".into(),
        attachment_dirs: vec![
            crate::util::telegram_staging_dir_name(second_chat, 42),
            crate::util::telegram_staging_dir_name(second_chat, 43),
        ],
        update_ids: vec![8],
        forwarded: true,
        ..test_msg("bob", "", "telegram", "other")
    };

    // The first member starts the album unchanged...
    let album = merge_album_member(None, first.clone());
    assert_eq!(album.content, "a");
    assert_eq!(album.attachment_dirs, first.attachment_dirs);
    assert!(
        !album.forwarded,
        "the first member is not forwarded content"
    );

    // ...and a later member joins its content with a newline and its staging
    // dirs onto the album.
    let album = merge_album_member(Some(album), second);
    assert_eq!(album.content, "a\nb");
    assert!(
        album.forwarded,
        "a forwarded member makes the album forwarded content"
    );
    assert_eq!(
        album.attachment_dirs,
        vec![
            crate::util::telegram_staging_dir_name(first_chat, 41),
            crate::util::telegram_staging_dir_name(second_chat, 42),
            crate::util::telegram_staging_dir_name(second_chat, 43),
        ]
    );
    // Every member's record entry comes along: an album is one incoming event
    // whose parts are each their own entry, and this merged message is what the
    // hand-over settles — a member left off it would stay held, and a later run
    // would take the album up a second time.
    assert_eq!(album.update_ids, vec![7, 8]);
}

// ── Control message tests ───────────────────────────────────────

#[test]
fn is_control_message_covers_commands_and_callbacks() {
    assert!(is_control_message(&test_msg(
        "alice", "/board", "telegram", "chat"
    )));
    assert!(is_control_message(&test_msg(
        "alice", "/clear", "telegram", "chat"
    )));
    assert!(is_control_message(&test_msg(
        "alice",
        "/image_models",
        "telegram",
        "chat"
    )));
    // The dispatch loop decodes `__act__` payloads from the text alone, so the
    // collector must too — otherwise a burst would fuse the payload into an
    // agent turn while the same text sent alone would be an action. The payload
    // comes from a keyboard a previous build delivered; it must stay a control
    // input even though its action is no longer handled.
    assert!(is_control_message(&test_msg(
        "alice",
        "__act__set_workspace|ws",
        "telegram",
        "chat"
    )));
    // The retired workspace, pause and maintenance commands are ordinary chat
    // now — in every spelling — so the collector must not hold them back.
    for text in [
        "/workspace",
        "/pause",
        "/unpause",
        "/maintenance",
        "/maintenance on",
        "/maintenance_on",
        "/MAINTENANCE_OFF",
    ] {
        assert!(
            control_input(&test_msg("alice", text, "telegram", "chat")).is_none(),
            "{text} is no longer a command"
        );
    }

    let mut callback = test_msg("alice", "anything", "telegram", "chat");
    callback.callback_query_id = Some("1".to_string());
    assert!(is_control_message(&callback));
    // The press carries no control input of its own, so it still reaches the
    // agent on the dispatch path — it is only barred from being collected.
    assert!(control_input(&callback).is_none());

    // Only Telegram routes commands; elsewhere a slash is ordinary text.
    assert!(!is_control_message(&test_msg(
        "alice", "/board", "gui", "chat"
    )));
    // Plain text, and text that merely looks like a command, is not control.
    assert!(!is_control_message(&test_msg(
        "alice", "hello", "telegram", "chat"
    )));
    assert!(!is_control_message(&test_msg(
        "alice",
        "/etc/hosts",
        "telegram",
        "chat"
    )));
}

// ── Forwarded message tests ─────────────────────────────────────

/// The one message `update` parses to, with the parse's own failure named — the shape
/// every case below reads its content through.
async fn parsed_message(ch: &TelegramChannel, update: &serde_json::Value) -> ChannelMessage {
    ch.parse_update_message(update)
        .await
        .expect("parse the update")
        .expect("a message")
}

/// The older forward fields still attribute a message — and still count it as
/// forwarded, which is the flag the burst collector's longer window rests on and
/// which the rendered prefix alone would not pin.
#[tokio::test]
async fn forward_attribution() {
    let ch = test_channel().await;

    let legacy = [
        (
            // A user with a username.
            serde_json::json!({
                "message_id": 50,
                "text": "Check this out",
                "from": { "id": 1, "username": "alice" },
                "chat": { "id": 999 },
                "forward_from": {
                    "id": 42,
                    "first_name": "Bob",
                    "username": "bob"
                },
                "forward_date": 1_700_000_000
            }),
            "[Forwarded from @bob] Check this out",
        ),
        (
            // A channel.
            serde_json::json!({
                "message_id": 51,
                "text": "Breaking news",
                "from": { "id": 1, "username": "alice" },
                "chat": { "id": 999 },
                "forward_from_chat": {
                    "id": -1_001_234_567_890_i64,
                    "title": "Daily News",
                    "username": "dailynews",
                    "type": "channel"
                },
                "forward_date": 1_700_000_000
            }),
            "[Forwarded from channel: Daily News] Breaking news",
        ),
        (
            // A user who hides their identity.
            serde_json::json!({
                "message_id": 52,
                "text": "Secret tip",
                "from": { "id": 1, "username": "alice" },
                "chat": { "id": 999 },
                "forward_sender_name": "Hidden User",
                "forward_date": 1_700_000_000
            }),
            "[Forwarded from Hidden User] Secret tip",
        ),
        (
            // A user without a username.
            serde_json::json!({
                "message_id": 54,
                "text": "Hello there",
                "from": { "id": 1, "username": "alice" },
                "chat": { "id": 999 },
                "forward_from": {
                    "id": 77,
                    "first_name": "Charlie"
                },
                "forward_date": 1_700_000_000
            }),
            "[Forwarded from Charlie] Hello there",
        ),
    ];
    for (message, expected) in legacy {
        let msg = parsed_message(&ch, &test_update(&[("message", message)])).await;
        assert_eq!(msg.content, expected);
        assert!(
            msg.forwarded,
            "a legacy forward field must count as forwarded"
        );
    }

    // A message with no forward field of any kind carries neither the prefix nor
    // the fact.
    let plain = test_update(&[(
        "message",
        serde_json::json!({
            "message_id": 53,
            "text": "Normal message",
            "from": { "id": 1, "username": "alice" },
            "chat": { "id": 999 }
        }),
    )]);
    let msg = parsed_message(&ch, &plain).await;
    assert_eq!(msg.content, "Normal message");
    assert!(!msg.forwarded);

    // A forwarded photo's attribution is the prefix the content opens with.
    let message = serde_json::json!({
        "message_id": 60,
        "from": { "id": 1, "username": "alice" },
        "chat": { "id": 999 },
        "photo": [
            { "file_id": "abc123", "file_unique_id": "u1", "width": 320, "height": 240 }
        ],
        "forward_from": {
            "id": 42,
            "username": "bob"
        },
        "forward_date": 1_700_000_000
    });
    let attr = TelegramChannel::forward(&message)
        .prefix
        .expect("should detect forward");
    assert_eq!(attr, "[Forwarded from @bob] ");
    let photo_content = "[IMAGE:/tmp/photo.jpg]".to_string();
    let content = format!("{attr}{photo_content}");
    assert_eq!(content, "[Forwarded from @bob] [IMAGE:/tmp/photo.jpg]");
}

/// Every modern `forward_origin` the product labels renders its attribution, and
/// an origin it has no label for still counts as forwarded: the flag the burst
/// collector's longer window rests on is the fact, the prefix only its rendering.
/// An update carrying both the modern origin and the legacy fields still yields
/// one attribution, while an origin of a kind the product has no label for falls
/// back to the legacy fields. The legacy fields on their own are covered by
/// `forward_attribution`.
#[tokio::test]
async fn forward_origin_attribution_covers_every_origin_and_the_legacy_fields() {
    let ch = test_channel().await;

    let with_origin = |origin: serde_json::Value| {
        test_update(&[(
            "message",
            serde_json::json!({
                "message_id": 70,
                "text": "Check this out",
                "from": { "id": 1, "username": "alice" },
                "chat": { "id": 999 },
                "forward_origin": origin,
            }),
        )])
    };

    let cases = [
        (
            serde_json::json!({
                "type": "user", "date": 1_700_000_000,
                "sender_user": { "id": 42, "username": "bob" }
            }),
            "[Forwarded from @bob] Check this out",
        ),
        (
            serde_json::json!({
                "type": "user", "date": 1_700_000_000,
                "sender_user": { "id": 77, "first_name": "Charlie" }
            }),
            "[Forwarded from Charlie] Check this out",
        ),
        (
            serde_json::json!({
                "type": "hidden_user", "date": 1_700_000_000,
                "sender_user_name": "Hidden User"
            }),
            "[Forwarded from Hidden User] Check this out",
        ),
        (
            serde_json::json!({
                "type": "chat", "date": 1_700_000_000,
                "sender_chat": { "id": -100_111_i64, "type": "supergroup", "title": "Group Chat" }
            }),
            "[Forwarded from Group Chat] Check this out",
        ),
        (
            serde_json::json!({
                "type": "channel", "date": 1_700_000_000,
                "sender_chat": { "id": -100_222_i64, "type": "channel", "title": "Daily News" }
            }),
            "[Forwarded from channel: Daily News] Check this out",
        ),
    ];
    for (origin, expected) in cases {
        let msg = parsed_message(&ch, &with_origin(origin)).await;
        assert_eq!(msg.content, expected);
        assert!(msg.forwarded, "a modern forward must count as forwarded");
    }

    // The modern origin wins over the legacy fields: one attribution, not two.
    let both = test_update(&[(
        "message",
        serde_json::json!({
            "message_id": 71,
            "text": "Check this out",
            "from": { "id": 1, "username": "alice" },
            "chat": { "id": 999 },
            "forward_origin": {
                "type": "channel",
                "date": 1_700_000_000,
                "sender_chat": {
                    "id": -100_222_i64,
                    "type": "channel",
                    "title": "Daily News"
                }
            },
            "forward_from": { "id": 42, "username": "bob" },
        }),
    )]);
    let msg = parsed_message(&ch, &both).await;
    assert_eq!(msg.content.matches("[Forwarded from").count(), 1);
    assert!(
        msg.content
            .starts_with("[Forwarded from channel: Daily News] ")
    );

    // An origin this product has no label for renders no attribution of its own,
    // and the older fields still get their chance to attribute the message.
    let unknown = with_origin(serde_json::json!({"type": "something_new", "date": 1_700_000_000}));
    let msg = parsed_message(&ch, &unknown).await;
    assert_eq!(msg.content, "Check this out");
    assert!(msg.forwarded);

    let mut unlabelled = with_origin(serde_json::json!({
        "type": "something_new", "date": 1_700_000_000,
        "sender_chat": { "id": -100_222_i64, "type": "channel", "title": "Ignored" }
    }));
    unlabelled["message"]["forward_from_chat"] =
        serde_json::json!({ "id": -100_333_i64, "title": "Fallback News" });
    let msg = parsed_message(&ch, &unlabelled).await;
    assert_eq!(
        msg.content,
        "[Forwarded from channel: Fallback News] Check this out"
    );
}

/// A refused forwarded attachment keeps its attribution: a refusal hands on a
/// message of its own, and the `[Forwarded from …]` prefix (with the forwarded
/// fact) must ride on it too. A declared size past Telegram's own download cap is
/// refused before any fetch.
#[tokio::test]
async fn a_refused_forwarded_attachment_keeps_its_attribution() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    let (store, _dir) = test_store().await;
    let ch = channel("token-a", &fake, &store);
    let update = forwarded_photo_update(1, 33, "huge", 30 * 1024 * 1024);

    let msg = ch
        .parse_incoming_update(&update)
        .await
        .expect("parse the update")
        .expect("a message");
    assert!(msg.forwarded);
    assert!(
        msg.content
            .starts_with("[Forwarded from channel: Daily News] [File "),
        "the refusal must keep its attribution: {}",
        msg.content
    );
    assert!(msg.content.contains("not received"));

    until(
        "the user is told the file could not be processed",
        || async {
            fake.sent_texts()
                .iter()
                .any(|text| text.contains("Could not process the file"))
        },
    )
    .await;
}

// ── strip_html_tags tests ──────────────────────────────────

#[test]
fn test_strip_html_tags() {
    struct Case {
        name: &'static str,
        input: &'static str,
        expected: &'static str,
    }
    let cases = vec![
        Case {
            name: "empty string",
            input: "",
            expected: "",
        },
        Case {
            name: "plain text",
            input: "hello world",
            expected: "hello world",
        },
        Case {
            name: "simple tag",
            input: "<b>bold</b>",
            expected: "bold",
        },
        Case {
            name: "nested tags",
            input: "<div><span>text</span></div>",
            expected: "text",
        },
        Case {
            name: "self-closing tag",
            input: "before<br/>after",
            expected: "beforeafter",
        },
        // Regression: '<' in tag starts in_tag, then '>' inside attribute
        // value must NOT close the tag.
        Case {
            name: "gt in double-quoted attribute",
            input: "<a title=\"a > b\">link</a>",
            expected: "link",
        },
        Case {
            name: "gt in single-quoted attribute",
            input: "<a title='a > b'>link</a>",
            expected: "link",
        },
        // Double-quoted attribute containing single quotes
        Case {
            name: "mixed quotes - double with single inside",
            input: "<a title=\"he said 'hello'\">text</a>",
            expected: "text",
        },
        // Single-quoted attribute containing double quotes
        Case {
            name: "mixed quotes - single with double inside",
            input: "<a title='he said \"hello\"'>text</a>",
            expected: "text",
        },
        // Multiple attributes, some with '>' inside quoted values
        Case {
            name: "multiple attrs with gt",
            input: "<input type=\"text\" value=\"a > b\" placeholder=\"x > y\">",
            expected: "",
        },
        // '>' that is not inside a tag should be preserved as text
        Case {
            name: "gt outside tag",
            input: "a > b",
            expected: "a > b",
        },
        // '<' that is not part of a tag should start tag mode
        // (pre-existing behavior: bare '<' starts tag stripping)
        Case {
            name: "lt outside tag",
            input: "a < b",
            expected: "a ",
        },
        Case {
            name: "html comment",
            input: "<!-- comment -->visible",
            expected: "visible",
        },
        // Realistic mixed content with tags and text
        Case {
            name: "mixed content",
            input: "Hello <b>world</b>, check <a href=\"https://example.com?q=a > b\">this</a> out!",
            expected: "Hello world, check this out!",
        },
    ];
    for case in cases {
        let result = strip_html_tags(case.input);
        assert_eq!(result, case.expected, "case: {}", case.name);
    }
}

// ── extend_past_open_tag tests ─────────────────────────────

#[expect(clippy::too_many_lines)]
#[test]
fn test_extend_past_open_tag() {
    struct Case {
        name: &'static str,
        input: &'static str,
        pos: usize,
        expected: Option<usize>,
    }
    let cases = vec![
        // No '<' before pos → None
        Case {
            name: "no tag near pos",
            input: "hello world",
            pos: 5,
            expected: None,
        },
        // <b>hello
        // 01234567
        // pos=1 (inside <b>, before '>') → extend past '>' at 2
        Case {
            name: "inside simple tag before gt",
            input: "<b>hello",
            pos: 1,
            expected: Some(3),
        },
        // pos=2 (at the '>' itself) → extend past it
        Case {
            name: "inside simple tag at gt",
            input: "<b>hello",
            pos: 2,
            expected: Some(3),
        },
        // <b>hello
        // 01234567
        // pos=3 (at 'h', past the '>') → None
        Case {
            name: "after simple tag at h",
            input: "<b>hello",
            pos: 3,
            expected: None,
        },
        // pos further into text → None
        Case {
            name: "after simple tag further",
            input: "<b>hello",
            pos: 5,
            expected: None,
        },
        // No '>' exists after the '<' → None (can't extend)
        Case {
            name: "no closing gt",
            input: "<div",
            pos: 3,
            expected: None,
        },
        // Regression: '>' inside double-quoted attribute must not be
        // treated as tag closer. The real '>' is at index 16.
        // <a title="a > b">text
        // 012345678901234567890   (indices)
        //           111111111122
        //      '>' at 12 is inside attribute, real '>' at 16
        // pos=13 (inside tag, past the quoted '>', before real '>') → extend to 17
        Case {
            name: "gt in double-quoted attr before real gt",
            input: "<a title=\"a > b\">text",
            pos: 13,
            expected: Some(17),
        },
        // pos=16 (at the real '>') → extend past it
        Case {
            name: "gt in double-quoted attr at real gt",
            input: "<a title=\"a > b\">text",
            pos: 16,
            expected: Some(17),
        },
        // pos after the real closing '>' → None
        // <a title="a > b">text
        // 012345678901234567890
        //           111111111122
        // real '>' at 16, 't' at 17
        Case {
            name: "after closed tag with gt in attr at 17",
            input: "<a title=\"a > b\">text",
            pos: 17,
            expected: None,
        },
        Case {
            name: "after closed tag with gt in attr at 20",
            input: "<a title=\"a > b\">text",
            pos: 20,
            expected: None,
        },
        // Same scenario with single quotes
        // <a title='a > b'>text
        // real '>' at 16
        Case {
            name: "gt in single-quoted attr",
            input: "<a title='a > b'>text",
            pos: 13,
            expected: Some(17),
        },
        // Double-quoted attr containing single quotes: '>' inside should still be
        // treated as quoted because we're inside double quotes, not single.
        // <a title="he said 'stop'">text
        // real '>' at 25
        // pos=17 (inside the attribute, past the single quotes) → extend past real '>'
        Case {
            name: "mixed quotes",
            input: "<a title=\"he said 'stop'\">text",
            pos: 17,
            expected: Some(26),
        },
        // <div><span>text
        // 012345678901234
        // last '<' at 5 (<span>), its '>' at 10.
        // pos=11 (after both tags are closed) → None
        Case {
            name: "after nested tags at 11",
            input: "<div><span>text",
            pos: 11,
            expected: None,
        },
        // pos=15 (end of string, still after both tags) → None
        Case {
            name: "after nested tags at 15",
            input: "<div><span>text",
            pos: 15,
            expected: None,
        },
        // <div><span>text
        // pos=6 (inside <span>, before its '>') → extend past '>' at 10
        Case {
            name: "inside nested tag",
            input: "<div><span>text",
            pos: 6,
            expected: Some(11),
        },
        // |<b>text → pos=0 is before any '<'
        Case {
            name: "pos at start",
            input: "<b>text",
            pos: 0,
            expected: None,
        },
    ];
    for case in cases {
        let result = extend_past_open_tag(case.input, case.pos);
        assert_eq!(result, case.expected, "case: {}", case.name);
    }
}

/// Both readers report offsets in bytes, so multi-byte text around and inside
/// a tag must not shift them.
#[test]
fn test_html_tag_scans_are_byte_offsets() {
    let text = "héllo <b x=\"ü>ö\">täil";
    assert_eq!(strip_html_tags(text), "héllo täil");
    // Byte 15 is the `>` inside the quoted attribute; 16 is just past it.
    assert_eq!(extend_past_open_tag(text, 16), Some(20));
    assert_eq!(extend_past_open_tag(text, text.find('t').unwrap()), None);

    assert_eq!(strip_html_tags("ä <b x=\"ö>ü"), "ä ");
}

// ── decode_action ─────────────────────────────────────────────────────

#[test]
fn test_decode_action() {
    struct Case {
        name: &'static str,
        input: &'static str,
        expected: Option<(&'static str, &'static str)>,
    }

    let cases = [
        Case {
            name: "with payload",
            input: "__act__set_image_model|google/gemini-3.1-flash-image-preview",
            expected: Some(("set_image_model", "google/gemini-3.1-flash-image-preview")),
        },
        Case {
            name: "empty payload pipe",
            input: "__act__clear_session|",
            expected: Some(("clear_session", "")),
        },
        Case {
            name: "no pipe",
            input: "__act__clear_session",
            expected: Some(("clear_session", "")),
        },
        Case {
            name: "rejects non prefix",
            input: "random_text",
            expected: None,
        },
        Case {
            name: "rejects empty",
            input: "",
            expected: None,
        },
    ];

    for case in &cases {
        let result = decode_action(case.input);
        let expected = case
            .expected
            .map(|(action, payload)| (action.to_string(), payload.to_string()));
        assert_eq!(result, expected, "case: {}", case.name);
    }
}

// ── GUI message → Telegram mirror tests ─────────────────────────────
//
// These tests verify that `mirror_gui_message_to_telegram` returns
// early (without sending) for each guard condition, and that
// blockquote-format messages are correctly sent to the user's Telegram
// bindings. They are serialized via [`MIRROR_TEST_LOCK`] because the
// channel registry and store singletons are global.

use crate::util::UnwrapPoison;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::OnceLock;

/// Serialization lock for all mirror tests — these tests share the global
/// [`CHANNEL_REGISTRY`] and store singletons, so they must run one at a time.
/// Uses `tokio::sync::Mutex` to avoid blocking worker threads while held
/// across await points.
static MIRROR_TEST_LOCK: OnceLock<tokio::sync::Mutex<()>> = OnceLock::new();

async fn acquire_mirror_lock() -> tokio::sync::MutexGuard<'static, ()> {
    MIRROR_TEST_LOCK
        .get_or_init(|| tokio::sync::Mutex::new(()))
        .lock()
        .await
}

/// Set up the channel registry with a spy Telegram channel and return a
/// shared sent-messages buffer. Idempotent — safe to call from every test.
fn setup_spy_channel() -> &'static Arc<Mutex<Vec<SendMessage>>> {
    static SPY_SENT: OnceLock<Arc<Mutex<Vec<SendMessage>>>> = OnceLock::new();
    SPY_SENT.get_or_init(|| {
        let (spy, sent) = crate::util::test::SpyChannel::new("telegram");
        let registry = crate::CHANNEL_REGISTRY.get_or_init(crate::ChannelRegistry::default);
        registry.register(Arc::new(spy) as Arc<dyn crate::Channel>);
        sent
    })
}

/// Ensure the user store has a test user with a Telegram binding and
/// reply_target. Idempotent. `ctx` labels setup panics for attribution.
async fn setup_user_with_telegram_binding(user_name: &str, reply_target: &str, ctx: &str) {
    use crate::users::store;
    let store = store();
    store
        .add_user(user_name)
        .await
        .unwrap_or_else(|e| panic!("{ctx}: add_user: {e}"));
    store
        .bind_channel(user_name, "telegram", user_name)
        .await
        .unwrap_or_else(|e| panic!("{ctx}: bind_channel: {e}"));
    store
        .update_channel_contact("telegram", user_name, reply_target)
        .await
        .unwrap_or_else(|e| panic!("{ctx}: update_channel_contact: {e}"));
}

/// Three-line preamble shared by all mirror tests: acquire the serialization
/// lock, initialise test stores, and set up the spy channel. Returns the spy
/// sent-messages buffer and the lock guard (kept alive for the test duration).
async fn setup_mirror_test_env() -> (
    &'static Arc<Mutex<Vec<SendMessage>>>,
    tokio::sync::MutexGuard<'static, ()>,
) {
    let lock = acquire_mirror_lock().await;
    crate::util::test::init_test_stores().await;
    let sent = setup_spy_channel();
    (sent, lock)
}

/// Shared `ChannelMessage` constructor core; the `gui_msg`/`telegram_msg`/
/// `voice_msg` wrappers differ only in `channel` and `reply_target`.
fn test_msg(user_name: &str, content: &str, channel: &str, reply_target: &str) -> ChannelMessage {
    ChannelMessage {
        user_name: user_name.to_string(),
        reply_target: reply_target.to_string(),
        content: content.to_string(),
        channel: channel.to_string(),
        workspace: "test".to_string(),
        optimistic_id: None,
        callback_query_id: None,
        reply_reference: None,
        chat_id: None,
        message_id: None,
        attachment_dirs: Vec::new(),
        parts: Vec::new(),
        update_date: None,
        forwarded: false,
        update_ids: Vec::new(),
    }
}

fn gui_msg(user_name: &str, content: &str) -> ChannelMessage {
    test_msg(user_name, content, "gui", "")
}

fn telegram_msg(user_name: &str, content: &str) -> ChannelMessage {
    test_msg(user_name, content, "telegram", "chat:thread")
}

fn voice_msg(user_name: &str, content: &str) -> ChannelMessage {
    test_msg(user_name, content, "voice", "")
}

fn cli_msg(user_name: &str, content: &str) -> ChannelMessage {
    test_msg(user_name, content, crate::cli::CHANNEL, "")
}

/// Shared happy-path mirror harness: sets up the mirror environment, binds
/// `user_name` to `reply_target`, mirrors `msg`, and returns the single
/// message sent to `reply_target` (panics unless exactly one was sent).
/// Per-test assertions stay in the callers.
async fn mirror_single_to_binding(
    user_name: &str,
    reply_target: &str,
    msg: &ChannelMessage,
) -> SendMessage {
    let (sent, _lock) = setup_mirror_test_env().await;
    setup_user_with_telegram_binding(user_name, reply_target, user_name).await;

    super::mirror_gui_message_to_telegram(msg).await;

    let guard = sent.lock().unwrap_poison();
    let our_msgs: Vec<_> = guard
        .iter()
        .filter(|m| m.recipient == reply_target)
        .collect();
    assert_eq!(our_msgs.len(), 1, "expected exactly one message");
    our_msgs[0].clone()
}

// ── Guard tests: early-return conditions ─────────────────────────────

/// Per-case setup for `assert_mirror_skips`, applied after the shared
/// mirror env is initialised (users::store() panics pre-init).
enum MirrorSkipSetup {
    BoundTo(&'static str, &'static str),
    Unbound(&'static str),
    BoundNoTarget(&'static str),
}

/// Mirror `msg` and assert nothing was sent to the recipient implied by
/// `setup` (reply_target when bound, user name otherwise). Each call
/// acquires the serialization lock; stores and spy are idempotent.
async fn assert_mirror_skips(setup: MirrorSkipSetup, msg: &ChannelMessage, reason: &str) {
    let (sent, _lock) = setup_mirror_test_env().await;
    let (user, filter_recipient) = match setup {
        MirrorSkipSetup::BoundTo(u, t) => {
            setup_user_with_telegram_binding(u, t, &format!("case {reason}")).await;
            (u, t)
        }
        MirrorSkipSetup::Unbound(u) => {
            let s = crate::users::store();
            s.add_user(u)
                .await
                .unwrap_or_else(|e| panic!("case {reason}: add_user: {e}"));
            (u, u)
        }
        MirrorSkipSetup::BoundNoTarget(u) => {
            let s = crate::users::store();
            s.add_user(u)
                .await
                .unwrap_or_else(|e| panic!("case {reason}: add_user: {e}"));
            s.bind_channel(u, "telegram", u)
                .await
                .unwrap_or_else(|e| panic!("case {reason}: bind_channel: {e}"));
            (u, u)
        }
    };
    // msg.user_name must match the setup user, or the filter matches nothing.
    assert_eq!(msg.user_name, user, "case {reason}");
    super::mirror_gui_message_to_telegram(msg).await;
    let guard = sent.lock().unwrap_poison();
    let our_msgs: Vec<_> = guard
        .iter()
        .filter(|m| m.recipient == filter_recipient)
        .collect();
    assert!(
        our_msgs.is_empty(),
        "case {reason}: got {} message(s)",
        our_msgs.len()
    );
}

#[tokio::test]
async fn mirror_skips_guard_cases() {
    // Each call acquires the serialization lock independently; the global
    // stores (OnceCell) and spy channel (OnceLock) are identical across
    // `setup_mirror_test_env()` calls.
    assert_mirror_skips(
        MirrorSkipSetup::BoundTo("skip_telegram", "target_non_gui"),
        &telegram_msg("skip_telegram", "hello from telegram"),
        "Telegram-originated messages should not send (telegram is the only \
         source that can originate a loop)",
    )
    .await;
    assert_mirror_skips(
        MirrorSkipSetup::BoundTo("skip_ew", "target_empty_ws"),
        &gui_msg("skip_ew", ""),
        "empty content should not send",
    )
    .await;
    assert_mirror_skips(
        MirrorSkipSetup::BoundTo("skip_ew", "target_empty_ws"),
        &gui_msg("skip_ew", "   \t\n  "),
        "whitespace content should not send",
    )
    .await;
    // Create the user but DO NOT bind a Telegram channel; no bindings
    // means no messages for this user at all, so its name is the filter.
    assert_mirror_skips(
        MirrorSkipSetup::Unbound("no_binding"),
        &gui_msg("no_binding", "hello"),
        "user with no bindings should not send",
    )
    .await;
    // Bind a Telegram channel but don't set reply_target (skip
    // update_channel_contact → reply_target stays NULL).
    assert_mirror_skips(
        MirrorSkipSetup::BoundNoTarget("no_target"),
        &gui_msg("no_target", "hello"),
        "binding without reply_target should not send",
    )
    .await;
    // Full binding: exercises the media-only guard (content emptied by
    // marker stripping), not the no-bindings guard.
    assert_mirror_skips(
        MirrorSkipSetup::BoundTo("media_only", "target_media"),
        &gui_msg("media_only", "[IMAGE:/path/to/img.png]"),
        "media-only content should not send",
    )
    .await;
    // A reply reference must NOT rescue an entirely-empty message: the
    // pre-strip trimmed-empty guard still skips it regardless of a reference.
    let mut empty_ref = gui_msg("skip_ew", "");
    empty_ref.reply_reference = Some(crate::channels::ReplyReference {
        author: "alice".to_string(),
        snippet: "hi".to_string(),
    });
    assert_mirror_skips(
        MirrorSkipSetup::BoundTo("skip_ew", "target_empty_ws"),
        &empty_ref,
        "empty content with a reply reference should not send",
    )
    .await;
}

// ── Happy path tests ─────────────────────────────────────────────────

#[tokio::test]
async fn sends_blockquote_to_single_binding() {
    let mirrored = mirror_single_to_binding(
        "single_user",
        "unique_single",
        &gui_msg("single_user", "Hello, world!"),
    )
    .await;
    assert_eq!(
        mirrored.content,
        "<blockquote>\nHello, world!\n</blockquote>"
    );
    assert!(mirrored.reply_markup.is_none());
}

#[tokio::test]
async fn mirrors_voice_transcript_to_telegram() {
    // Voice-originated messages (GUI mic-button recording / wake-word
    // command) must mirror exactly like GUI text: same recipient bindings,
    // same blockquote format, no audio payload.
    let mirrored = mirror_single_to_binding(
        "voice_user",
        "unique_voice",
        &voice_msg("voice_user", "Record this voice note"),
    )
    .await;
    assert_eq!(
        mirrored.content,
        "<blockquote>\nRecord this voice note\n</blockquote>"
    );
    assert!(mirrored.reply_markup.is_none());
}

#[tokio::test]
async fn mirrors_command_line_message_to_telegram() {
    // A message the command line sends is the owner's own, so it mirrors exactly
    // like GUI text and a voice transcript: same recipient bindings, same
    // blockquote format, and NOTHING in the mirrored text marks it as scripted.
    let mirrored = mirror_single_to_binding(
        "cli_user",
        "unique_cli",
        &cli_msg("cli_user", "Send this from a script"),
    )
    .await;
    assert_eq!(
        mirrored.content,
        "<blockquote>\nSend this from a script\n</blockquote>"
    );
    assert!(mirrored.reply_markup.is_none());
}
#[tokio::test]
async fn sends_to_multiple_telegram_bindings() {
    let (sent, _lock) = setup_mirror_test_env().await;
    let store = crate::users::store();
    store.add_user("multi_user").await.expect("add_user");
    // Bind two Telegram accounts with unique recipients.
    store
        .bind_channel("multi_user", "telegram", "multi_user_1")
        .await
        .expect("bind_channel_1");
    store
        .bind_channel("multi_user", "telegram", "multi_user_2")
        .await
        .expect("bind_channel_2");
    store
        .update_channel_contact("telegram", "multi_user_1", "unique_multi_a")
        .await
        .expect("update_channel_contact_1");
    store
        .update_channel_contact("telegram", "multi_user_2", "unique_multi_b")
        .await
        .expect("update_channel_contact_2");

    let msg = gui_msg("multi_user", "Hi both!");
    super::mirror_gui_message_to_telegram(&msg).await;

    let guard = sent.lock().unwrap_poison();
    let our_msgs: Vec<_> = guard
        .iter()
        .filter(|m| m.recipient == "unique_multi_a" || m.recipient == "unique_multi_b")
        .collect();
    assert_eq!(our_msgs.len(), 2, "expected two messages (one per binding)");
    // Both should have the same content.
    for m in &our_msgs {
        assert_eq!(m.content, "<blockquote>\nHi both!\n</blockquote>");
    }
    let recipients: Vec<&str> = our_msgs.iter().map(|m| m.recipient.as_str()).collect();
    assert!(recipients.contains(&"unique_multi_a"));
    assert!(recipients.contains(&"unique_multi_b"));
}

/// Shared helper for media-marker-stripping tests: mirrors a GUI message
/// with `content` and asserts that the quoted output equals `expected_quote`.
async fn assert_mirror_strips_markers(
    user_name: &str,
    reply_target: &str,
    content: &str,
    expected_quote: &str,
) {
    let mirrored =
        mirror_single_to_binding(user_name, reply_target, &gui_msg(user_name, content)).await;
    assert_eq!(mirrored.content, expected_quote);
}

#[tokio::test]
async fn strips_media_markers_from_content() {
    assert_mirror_strips_markers(
        "strip_markers",
        "unique_markers",
        "Check this [IMAGE:/tmp/screenshot.png] and my [AUDIO:/tmp/recording.mp3]",
        "<blockquote>\nCheck this  and my\n</blockquote>",
    )
    .await;
    // Regression: the mirror path must use a case-insensitive regex to strip
    // lowercase markers like [image:...] and [audio:...].
    assert_mirror_strips_markers(
        "strip_markers",
        "unique_lowercase",
        "See [image:/tmp/photo.png] and hear [audio:/tmp/sound.mp3]",
        "<blockquote>\nSee  and hear\n</blockquote>",
    )
    .await;
    // A marker-shaped word of an unknown kind is not a marker at all, so only
    // the four known kinds are deleted from the quote.
    assert_mirror_strips_markers(
        "strip_markers",
        "unique_unmappable",
        "See [DOCX:/tmp/report.docx] here",
        "<blockquote>\nSee [DOCX:/tmp/report.docx] here\n</blockquote>",
    )
    .await;
}

#[tokio::test]
async fn preserves_markdown_formatting_in_blockquote() {
    let mirrored = mirror_single_to_binding(
        "md_user",
        "unique_md",
        &gui_msg("md_user", "**bold** and `code` and *italic*"),
    )
    .await;
    // Markdown syntax inside the blockquote passes through — the Telegram
    // channel's markdown_to_telegram_html will handle formatting later.
    assert_eq!(
        mirrored.content,
        "<blockquote>\n**bold** and `code` and *italic*\n</blockquote>"
    );
}

#[tokio::test]
async fn mirrors_reply_reference_header() {
    let mut msg = gui_msg("ref_user", "hello");
    msg.reply_reference = Some(crate::channels::ReplyReference {
        author: "alice".to_string(),
        snippet: "hi there".to_string(),
    });
    let mirrored = mirror_single_to_binding("ref_user", "unique_ref", &msg).await;
    assert_eq!(
        mirrored.content,
        "<blockquote>\n↩ alice: hi there\nhello\n</blockquote>"
    );
}

#[tokio::test]
async fn mirrors_media_only_with_reply_reference() {
    let mut msg = gui_msg("media_ref", "[IMAGE:/tmp/screenshot.png]");
    msg.reply_reference = Some(crate::channels::ReplyReference {
        author: "bob".to_string(),
        snippet: "[Photo]".to_string(),
    });
    let mirrored = mirror_single_to_binding("media_ref", "unique_media_ref", &msg).await;
    // Media-only content (markers stripped) still mirrors the `↩` line.
    assert_eq!(
        mirrored.content,
        "<blockquote>\n↩ bob: [Photo]\n</blockquote>"
    );
}

#[tokio::test]
#[serial_test::serial(gui_admin_workspace)] // writes the shared seeded admin row
async fn user_command_entries_reflect_admin_state() {
    // Serialized with the sibling mirror tests: this test initialises the
    // shared user store, so it must not run concurrently with other store users.
    let _lock = acquire_mirror_lock().await;
    crate::users::test_util::init_test_store().await;
    let admin = crate::users::ADMIN_USER_NAME;

    // The admin: no role-switch entry (the Assistant is every account's only
    // role), the two workspace-independent admin commands, and the model
    // commands. In the (SourceTree) test environment the shared availability
    // cache seeds `available = true`, so `/update` is present for the admin.
    let admin_entries = user_command_entries(admin).await;
    let cmds: Vec<&str> = admin_entries.iter().map(|(c, _)| c.as_str()).collect();
    assert!(cmds.contains(&"board"));
    assert!(cmds.contains(&"archive"));
    assert!(cmds.contains(&"update"));
    // The workspace switcher, the pipeline freeze pair and the maintenance
    // toggle are gone — from the menu and from the grammar.
    for cmd in [
        "workspace",
        "pause",
        "unpause",
        "maintenance",
        "maintenance_on",
        "maintenance_off",
        "agents",
        "manager",
        "assistant",
        "engineer",
    ] {
        assert!(!cmds.contains(&cmd), "{cmd} must be gone from the menu");
    }
    // Model commands are unconditional (every user has the Assistant role).
    assert!(cmds.contains(&"image_models"));
    assert!(cmds.contains(&"video_models"));
    // Menu order: board, archive, update, then the model commands, with
    // /clear last.
    assert_eq!(cmds.last(), Some(&"clear"));
    let pos = |cmd: &str| cmds.iter().position(|c| *c == cmd).unwrap();
    assert!(pos("board") < pos("archive"));
    assert!(pos("archive") < pos("update"));
    assert!(pos("update") < pos("image_models"));
    assert!(pos("image_models") < pos("video_models"));
    assert!(pos("video_models") < pos("clear"));

    // Hidden branch: no available update → `/update` is absent even for the
    // admin (the cache is a process-local single source of truth). The RAII
    // guard restores the SourceTree default on drop.
    {
        let _guard = crate::self_update::set_update_cache_for_test(false, false);
        let hidden = user_command_entries(admin).await;
        let hidden_cmds: Vec<&str> = hidden.iter().map(|(c, _)| c.as_str()).collect();
        assert!(!hidden_cmds.contains(&"update"));
    }

    // bob: a guest — no admin commands and, with the Assistant as the only
    // role, no role-switch entry either. Model commands remain available.
    let bob = user_command_entries("bob").await;
    let cmds: Vec<&str> = bob.iter().map(|(c, _)| c.as_str()).collect();
    for cmd in [
        "agents",
        "board",
        "archive",
        "update",
        "workspace",
        "pause",
        "unpause",
        "maintenance",
        "maintenance_on",
        "maintenance_off",
        "assistant",
        "manager",
    ] {
        assert!(!cmds.contains(&cmd), "{cmd} must not reach a guest's menu");
    }
    assert!(cmds.contains(&"image_models"));
    assert!(cmds.contains(&"video_models"));
    assert_eq!(cmds[0], "image_models");
}

// ── Self-update notification texts ─────────────────────────────────────────

/// The self-update notifications must reach the admin as plain, single
/// messages: no character may be turned into markup, and none may be split by
/// the chunker (a split message reads as several notifications). The start-up
/// line is one of them — it is where an unattended update's new version is
/// named to the owner.
#[test]
fn test_update_notification_texts_render_as_plain_single_messages() {
    for text in [
        crate::self_update::UPDATE_BUILD_COMPLETE_MSG,
        crate::self_update::UPDATE_DOWNLOAD_COMPLETE_MSG,
        crate::self_update::UPDATE_RESTART_MSG,
        &crate::self_update::back_online_message(false),
        &crate::self_update::back_online_message(true),
    ] {
        assert_eq!(to_telegram_html(text), text, "not plain text: {text}");
        assert_eq!(
            split_message_for_telegram(text).len(),
            1,
            "split into several messages: {text}"
        );
    }

    // A failure's lead line is plain text like the notifications above; only the
    // detail under it (the captured build output) becomes a code block.
    let failure = crate::self_update::update_failure_notification(&anyhow::anyhow!(
        "Failed to build from source:\n```\nboom\n```"
    ));
    let html = to_telegram_html(&failure);
    assert_eq!(html.lines().next(), failure.lines().next());
    assert_eq!(split_message_for_telegram(&html).len(), 1);
}

// ── Telegram intake checks ──────────────────────────────────────────
//
// The intake's own guarantee — an event the platform handed over is never lost
// and is taken up exactly once — is checked against a fake Bot API: a listener
// is pointed at a local endpoint (`TelegramChannel::for_test`), scripted
// `getUpdates` batches drive it, and the record it writes into is read back
// through a store of this check's own.
//
// Every check here that starts a listener is serialized on the process-global
// drain flag: the listener consults it on entry and races it in its poll loop, so
// a check running while another test winds the daemon down would see its listener
// stop.

/// How long a check waits for state it can observe before failing. Generous on
/// purpose: everything waited for here is durable — a record row, a request the
/// fake logged, a message the pipeline holds — so a longer bound only delays
/// failure reporting, while a short one turns a machine that stalls a runtime
/// for seconds into a failing check.
const OBSERVABLE_TIMEOUT: Duration = Duration::from_secs(5);

/// How long a check waits for a poll that only arrives after the intake's own
/// pause on an unrecordable batch. Deliberately loose: that pause is seconds
/// long by design, and its length is not what the check asserts — a tight retry
/// would show up as a gap of milliseconds, not of seconds — so the bound only
/// has to be past the pause, not equal to it.
const BACKOFF_OBSERVABLE_TIMEOUT: Duration = Duration::from_secs(15);

/// How long the injected fetch of a forwarded attachment takes: past the group's
/// own ceiling, so a pair that reaches the assistant as one turn can only have
/// been held across the fetch — no window of the collector's own reaches that far.
const FETCH_DELAY: Duration = Duration::from_secs(2);

/// How long a check waits for a burst that has to fetch a file first: the fetch,
/// the group's own extended window, and room for a loaded machine.
const AT_MOST_ONE_TURN: Duration = Duration::from_secs(20);

/// A fake Telegram Bot API: scripted `getUpdates` batches, an account per token,
/// and a log of the methods a listener called.
struct FakeTelegram {
    base: String,
    state: Arc<FakeState>,
    /// Set by `Drop`, so the accept loop — which polls — lets go of the socket.
    stop: Arc<AtomicBool>,
    accept: Option<std::thread::JoinHandle<()>>,
}

/// What the fake's accept loop and its per-connection handlers share.
struct FakeState {
    /// The bot account each token resolves to.
    accounts: HashMap<String, i64>,
    /// The `getUpdates` batches queued for each token, oldest first.
    batches: Mutex<HashMap<String, VecDeque<Vec<serde_json::Value>>>>,
    /// Every request a listener made, in arrival order: what it was and what it
    /// carried.
    requests: Mutex<Vec<ServedRequest>>,
    /// The `text` of every `sendMessage`, in arrival order.
    sent_texts: Mutex<Vec<String>>,
    /// The files a listener may fetch, keyed by the `file_path` the fake's own
    /// `getFile` hands back: the bytes the transfer serves and how long it takes
    /// to answer them.
    files: Mutex<HashMap<String, FakeFile>>,
}

/// One request a listener made: the method, the `getUpdates` offset it asked for
/// (`None` for other methods), when it arrived, and the raw body — what an upload
/// actually put on the wire.
struct ServedRequest {
    method: String,
    offset: Option<i64>,
    at: std::time::Instant,
    body: Vec<u8>,
}

/// A file the fake serves over a transfer of its own: the bytes, and how long
/// the answer takes — a real fetch of a few megabytes takes seconds, which is
/// what a check about a slow fetch has to be able to reproduce.
struct FakeFile {
    bytes: Vec<u8>,
    delay: Duration,
}

/// One request as the fake read it.
struct FakeRequest {
    path: String,
    body: Vec<u8>,
}

/// A request's head: the request line's path, the `Content-Length` it declares,
/// and whatever of the body already arrived.
struct FakeHead {
    path: String,
    length: usize,
    buffer: Vec<u8>,
    head_end: usize,
}

impl FakeTelegram {
    /// Start a fake API for the given `(token, bot account id)` pairs.
    fn start(accounts: &[(&str, i64)]) -> Self {
        let listener = TcpListener::bind("127.0.0.1:0").expect("bind the fake Bot API");
        listener
            .set_nonblocking(true)
            .expect("the fake Bot API accepts without blocking");
        let port = listener.local_addr().expect("fake Bot API address").port();
        let state = Arc::new(FakeState {
            accounts: accounts
                .iter()
                .map(|(token, id)| ((*token).to_string(), *id))
                .collect(),
            batches: Mutex::new(HashMap::new()),
            requests: Mutex::new(Vec::new()),
            sent_texts: Mutex::new(Vec::new()),
            files: Mutex::new(HashMap::new()),
        });
        let stop = Arc::new(AtomicBool::new(false));
        let accept = std::thread::spawn({
            let state = Arc::clone(&state);
            let stop = Arc::clone(&stop);
            move || {
                while !stop.load(Ordering::Relaxed) {
                    match listener.accept() {
                        Ok((stream, _)) => {
                            // The accepted socket can inherit the listener's
                            // non-blocking mode (it does on macOS), and a
                            // handler that polls would then read nothing (or a
                            // half-arrived request) and close the connection
                            // before answering. A handler blocks, so put the
                            // stream back in blocking mode.
                            let _ = stream.set_nonblocking(false);
                            let state = Arc::clone(&state);
                            std::thread::spawn(move || serve(stream, &state));
                        }
                        // Polling rather than blocking, so the loop can see
                        // `stop`: a blocking accept would outlive the check that
                        // drops the fake.
                        Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {
                            std::thread::sleep(Duration::from_millis(2));
                        }
                        // A transient accept error (a client that went away
                        // before it was accepted is `ECONNABORTED`, ordinary on
                        // macOS) must not kill the fake: an accept loop that
                        // returns here stops serving every later request.
                        Err(_) => std::thread::sleep(Duration::from_millis(2)),
                    }
                }
            }
        });
        Self {
            base: format!("http://127.0.0.1:{port}"),
            state,
            stop,
            accept: Some(accept),
        }
    }

    /// Queue the next `getUpdates` result for `token`'s account.
    fn push_batch(&self, token: &str, updates: Vec<serde_json::Value>) {
        self.state
            .batches
            .lock()
            .expect("fake batches lock")
            .entry(token.to_string())
            .or_default()
            .push_back(updates);
    }

    /// Register a file `file_id` can be fetched as: `getFile` answers the path
    /// [`file_path_of`] builds, and the transfer serves `bytes` after `delay`.
    fn push_file(&self, file_id: &str, bytes: &[u8], delay: Duration) {
        self.state.files.lock().expect("fake files lock").insert(
            file_path_of(file_id),
            FakeFile {
                bytes: bytes.to_vec(),
                delay,
            },
        );
    }

    /// The `offset` every `getUpdates` poll asked for, in order.
    fn poll_offsets(&self) -> Vec<i64> {
        self.state
            .requests
            .lock()
            .expect("fake request log")
            .iter()
            .filter(|request| request.method == "getUpdates")
            .filter_map(|request| request.offset)
            .collect()
    }

    /// How many times `method` was called.
    fn count(&self, method: &str) -> usize {
        self.state
            .requests
            .lock()
            .expect("fake request log")
            .iter()
            .filter(|request| request.method == method)
            .count()
    }

    /// The raw bodies of every `method` call, in order.
    fn bodies(&self, method: &str) -> Vec<Vec<u8>> {
        self.state
            .requests
            .lock()
            .expect("fake request log")
            .iter()
            .filter(|request| request.method == method)
            .map(|request| request.body.clone())
            .collect()
    }

    /// The texts of the `sendMessage` calls, in order.
    fn sent_texts(&self) -> Vec<String> {
        self.state
            .sent_texts
            .lock()
            .expect("fake sendMessage log")
            .clone()
    }
}

impl Drop for FakeTelegram {
    fn drop(&mut self) {
        self.stop.store(true, Ordering::Relaxed);
        if let Some(accept) = self.accept.take() {
            let _ = accept.join();
        }
    }
}

/// The `(token, method)` a Bot API path carries, `None` for anything else.
fn bot_path(path: &str) -> Option<(&str, &str)> {
    let (token, method) = path.strip_prefix("/bot")?.split_once('/')?;
    (!token.is_empty() && !method.is_empty()).then_some((token, method))
}

/// The path the fake serves `file_id` at. The extension matters: the intake
/// classifies a downloaded photo by the name the path gives it.
fn file_path_of(file_id: &str) -> String {
    format!("photos/{file_id}.jpg")
}

/// The API's answer for something it does not serve.
fn not_found() -> serde_json::Value {
    serde_json::json!({ "ok": false, "error_code": 404, "description": "Not Found" })
}

/// Answer one request the way the Bot API does: the JSON envelope, and the
/// connection closed after it.
fn serve(mut stream: TcpStream, state: &FakeState) {
    let Some(head) = read_head(&mut stream) else {
        return;
    };
    let Some(request) = read_body(&mut stream, head) else {
        return;
    };
    // A GET carries its parameters in the request line, so the target is split
    // before the method is read.
    let target = request.path.split('?').next().unwrap_or_default();
    let method = target.rsplit('/').next().unwrap_or_default().to_string();
    // The `offset` a poll asked for, `None` for the methods that carry none,
    // read here because the log line below is where it is recorded.
    let offset = if method == "getUpdates" {
        serde_json::from_slice::<serde_json::Value>(&request.body)
            .ok()
            .and_then(|body| body["offset"].as_i64())
    } else {
        None
    };
    // Recorded as the request arrives — before its answer, so the log is the
    // durable evidence of what a listener called.
    state
        .requests
        .lock()
        .expect("fake request log")
        .push(ServedRequest {
            method: method.clone(),
            offset,
            at: std::time::Instant::now(),
            body: request.body.clone(),
        });

    // `/redirect?to=<address>` answers 302 to that address: a check scripts the
    // hop it wants the fetch to take.
    if target == "/redirect"
        && let Some(to) = request.path.split("?to=").nth(1)
    {
        let head = format!(
            "HTTP/1.1 302 Found\r\nLocation: {to}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
        );
        let _ = stream.write_all(head.as_bytes());
        return;
    }

    // A file transfer is not a Bot API method: its URL is
    // `{base}/file/bot<token>/<path>` and its body is the bytes themselves.
    if let Some(rest) = target.strip_prefix("/file/bot")
        && let Some((token, file_path)) = rest.split_once('/')
    {
        serve_file(&mut stream, state, token, file_path);
        return;
    }
    let Some((token, _)) = bot_path(target) else {
        respond(&mut stream, &not_found());
        return;
    };
    let Some(account) = state.accounts.get(token).copied() else {
        // A listener is only ever started on a token the fake was given (see
        // `FakeTelegram::start`), so nothing here models the platform's own
        // per-token authorization.
        respond(&mut stream, &not_found());
        return;
    };

    match method.as_str() {
        "getMe" => respond(
            &mut stream,
            &serde_json::json!({ "ok": true, "result": { "id": account } }),
        ),
        "getUpdates" => serve_get_updates(&mut stream, state, token, &request),
        "sendMessage" => serve_send_message(&mut stream, state, &request),
        "sendDocument" | "sendPhoto" | "sendAudio" => respond(
            &mut stream,
            &serde_json::json!({
                "ok": true,
                "result": { "message_id": 1, "chat": { "id": 5 }, "date": 0 },
            }),
        ),
        "getFile" => serve_get_file(&mut stream, state, &request),
        "setMessageReaction" | "answerCallbackQuery" | "setMyCommands" | "sendChatAction" => {
            respond(
                &mut stream,
                &serde_json::json!({ "ok": true, "result": true }),
            );
        }
        _ => respond(&mut stream, &not_found()),
    }
}

/// The `getFile` answer: the path the registered file is served at, after which
/// the listener makes a second request — the transfer the check can delay.
fn serve_get_file(stream: &mut TcpStream, state: &FakeState, request: &FakeRequest) {
    let Some(target) = request.path.split('?').nth(1) else {
        respond(stream, &not_found());
        return;
    };
    let Some((_, file_id)) = target.split_once('=') else {
        respond(stream, &not_found());
        return;
    };
    let path = file_path_of(file_id);
    let Some(size) = state
        .files
        .lock()
        .expect("fake files lock")
        .get(&path)
        .map(|file| file.bytes.len())
    else {
        respond(stream, &not_found());
        return;
    };
    respond(
        stream,
        &serde_json::json!({
            "ok": true,
            "result": { "file_id": file_id, "file_path": path, "file_size": size },
        }),
    );
}

/// The file transfer's answer: the registered bytes, after the transfer's own
/// delay. The connection is answered and closed like every other one, so a slow
/// transfer holds nothing but its own handler thread.
fn serve_file(stream: &mut TcpStream, state: &FakeState, token: &str, file_path: &str) {
    if !state.accounts.contains_key(token) {
        respond(stream, &not_found());
        return;
    }
    let registered = state
        .files
        .lock()
        .expect("fake files lock")
        .get(file_path)
        .map(|file| (file.bytes.clone(), file.delay));
    let Some((bytes, delay)) = registered else {
        respond(stream, &not_found());
        return;
    };
    std::thread::sleep(delay);
    let head = format!(
        "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
        bytes.len()
    );
    let _ = stream.write_all(head.as_bytes());
    let _ = stream.write_all(&bytes);
}

/// The `getUpdates` answer: the oldest queued batch, an immediate empty result
/// for the handshake's zero-timeout probe, or — for a long poll with nothing to
/// offer yet — the batch queued while the client waits, which is what the real
/// API's long poll does; the poll ends unanswered only when the client gives up.
fn serve_get_updates(
    stream: &mut TcpStream,
    state: &FakeState,
    token: &str,
    request: &FakeRequest,
) {
    if let Some(batch) = pop_batch(state, token) {
        return respond(stream, &serde_json::json!({ "ok": true, "result": batch }));
    }
    // The `timeout` the poll asked for, `0` when it asked for none.
    let timeout = serde_json::from_slice::<serde_json::Value>(&request.body)
        .ok()
        .and_then(|body| body["timeout"].as_u64())
        .unwrap_or(0);
    if timeout == 0 {
        return respond(stream, &serde_json::json!({ "ok": true, "result": [] }));
    }
    // A long poll with nothing to offer yet: the real API answers the moment an
    // update arrives, so a batch queued while the client waits is served too —
    // which is what lets a check queue a later batch after it has observed an
    // earlier one. The socket is put in non-blocking mode for the wait so the
    // queue can be re-read between reads; the `Ok(0)` below is the client giving
    // up, the one case the poll ends unanswered.
    let _ = stream.set_nonblocking(true);
    let mut sink = [0u8; 1024];
    loop {
        if let Some(batch) = pop_batch(state, token) {
            let _ = stream.set_nonblocking(false);
            return respond(stream, &serde_json::json!({ "ok": true, "result": batch }));
        }
        match stream.read(&mut sink) {
            Ok(0) => return,
            Ok(_) => {}
            Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {
                std::thread::sleep(Duration::from_millis(5));
            }
            Err(_) => return,
        }
    }
}

/// Take the oldest queued `getUpdates` batch for `token`, if any.
fn pop_batch(state: &FakeState, token: &str) -> Option<Vec<serde_json::Value>> {
    state
        .batches
        .lock()
        .expect("fake batches lock")
        .get_mut(token)
        .and_then(VecDeque::pop_front)
}

/// The `sendMessage` answer. The text is logged so a check can read back what
/// the listener told the user.
fn serve_send_message(stream: &mut TcpStream, state: &FakeState, request: &FakeRequest) {
    if let Some(text) = serde_json::from_slice::<serde_json::Value>(&request.body)
        .ok()
        .and_then(|body| body["text"].as_str().map(str::to_string))
    {
        state
            .sent_texts
            .lock()
            .expect("fake sendMessage log")
            .push(text);
    }
    respond(
        stream,
        &serde_json::json!({
            "ok": true,
            "result": { "message_id": 1, "chat": { "id": 5 }, "date": 0 },
        }),
    );
}

/// Read a request's head: the request line's path and the `Content-Length` it
/// declares.
fn read_head(stream: &mut TcpStream) -> Option<FakeHead> {
    let mut buffer = Vec::new();
    let mut chunk = [0u8; 1024];
    let head_end = loop {
        let read = stream.read(&mut chunk).ok()?;
        if read == 0 {
            return None;
        }
        buffer.extend_from_slice(&chunk[..read]);
        if let Some(at) = buffer.windows(4).position(|window| window == b"\r\n\r\n") {
            break at + 4;
        }
    };
    let (path, length) = {
        let head = String::from_utf8_lossy(&buffer[..head_end]);
        let path = head
            .lines()
            .next()?
            .split_whitespace()
            .nth(1)
            .unwrap_or_default()
            .to_string();
        let length = head
            .lines()
            .skip(1)
            .find_map(|line| {
                let (name, value) = line.split_once(':')?;
                name.eq_ignore_ascii_case("content-length")
                    .then(|| value.trim().parse::<usize>().ok())?
            })
            .unwrap_or(0);
        (path, length)
    };
    Some(FakeHead {
        path,
        length,
        buffer,
        head_end,
    })
}

/// Read the body `head` declared, on top of whatever of it already arrived.
fn read_body(stream: &mut TcpStream, head: FakeHead) -> Option<FakeRequest> {
    let FakeHead {
        path,
        length,
        mut buffer,
        head_end,
    } = head;
    let mut chunk = [0u8; 1024];
    while buffer.len() < head_end + length {
        let read = stream.read(&mut chunk).ok()?;
        if read == 0 {
            break;
        }
        buffer.extend_from_slice(&chunk[..read]);
    }
    let body = buffer[head_end..(head_end + length).min(buffer.len())].to_vec();
    Some(FakeRequest { path, body })
}

/// Write one response: the JSON body, and the closed connection `Connection:
/// close` promises.
fn respond(stream: &mut TcpStream, body: &serde_json::Value) {
    let text = body.to_string();
    let head = format!(
        "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
        text.len()
    );
    let _ = stream.write_all(head.as_bytes());
    let _ = stream.write_all(text.as_bytes());
}

/// Poll `probe` until it holds, or fail naming `what`. A check must not lean on
/// a fixed sleep for state it can observe: one long enough for the slowest
/// machine is a slow check, and a short one is a flaky check.
async fn until<F, Fut>(what: &str, probe: F)
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = bool>,
{
    until_within(OBSERVABLE_TIMEOUT, what, probe).await;
}

/// The same wait, with the failure bound chosen by the caller: state only
/// reached after the intake's own pause is a pause away, so the cheap bound every
/// other wait uses would fail on it — and only a failing run pays for the loose
/// one.
async fn until_within<F, Fut>(bound: Duration, what: &str, mut probe: F)
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = bool>,
{
    let deadline = tokio::time::Instant::now() + bound;
    loop {
        if probe().await {
            return;
        }
        assert!(
            tokio::time::Instant::now() < deadline,
            "timed out waiting until {what}"
        );
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
}

/// The next message the pipeline receives, or a panic naming `what`. A check
/// releases a burst through the collector's quiet window, so its message is a
/// second away, never immediate.
async fn next_message(
    rx: &mut tokio::sync::mpsc::Receiver<ChannelMessage>,
    what: &str,
) -> ChannelMessage {
    next_message_within(OBSERVABLE_TIMEOUT, rx, what).await
}

/// The next message the pipeline receives within `bound`, or a panic naming
/// `what`. A check releases a burst through the collector's windows first, so
/// its message is a second (or a fetch) away, never immediate.
async fn next_message_within(
    bound: Duration,
    rx: &mut tokio::sync::mpsc::Receiver<ChannelMessage>,
    what: &str,
) -> ChannelMessage {
    tokio::time::timeout(bound, rx.recv())
        .await
        .unwrap_or_else(|_| panic!("nothing reached the pipeline: {what}"))
        .unwrap_or_else(|| panic!("the pipeline closed before delivering: {what}"))
}

/// A message update from alice carrying `text`.
fn test_text_update(update_id: i64, text: &str) -> serde_json::Value {
    test_update(&[
        ("update_id", serde_json::json!(update_id)),
        (
            "message",
            serde_json::json!({
                "message_id": 33,
                "text": text,
                "from": { "id": 555, "username": "alice" },
                "chat": { "id": -100_200_300 },
            }),
        ),
    ])
}

/// One member of an album (a media group) from alice.
fn test_album_member(update_id: i64, group: &str, text: &str) -> serde_json::Value {
    test_update(&[
        ("update_id", serde_json::json!(update_id)),
        (
            "message",
            serde_json::json!({
                "message_id": 100 + update_id,
                "text": text,
                "media_group_id": group,
                "from": { "id": 555, "username": "alice" },
                "chat": { "id": -100_200_300 },
            }),
        ),
    ])
}

/// A forwarded photo from alice, as Telegram sends one: the modern
/// `forward_origin` names where it came from, and the attachment is the part a
/// slow fetch is injected for.
fn forwarded_photo_update(
    update_id: i64,
    message_id: i64,
    file_id: &str,
    file_size: u64,
) -> serde_json::Value {
    test_update(&[
        ("update_id", serde_json::json!(update_id)),
        (
            "message",
            serde_json::json!({
                "message_id": message_id,
                "date": 1_700_000_000,
                "from": { "id": 555, "username": "alice" },
                "chat": { "id": -100_200_300 },
                "photo": [{
                    "file_id": file_id,
                    "file_unique_id": "u1",
                    "width": 320,
                    "height": 240,
                    "file_size": file_size,
                }],
                "forward_origin": {
                    "type": "channel",
                    "date": 1_699_999_000,
                    "sender_chat": {
                        "id": -100_1234_5678_i64,
                        "type": "channel",
                        "title": "Daily News",
                    },
                },
            }),
        ),
    ])
}

/// A store of this check's own plus the `TempDir` holding its rows — one per
/// check, so no check leans on another's. The store comes back because a check
/// that restarts builds a second record over the same durable rows.
async fn test_store() -> (TelegramIntakeStore, tempfile::TempDir) {
    // Alice must be bound to Telegram for a parsed update to authorize her.
    crate::users::test_util::init_test_store().await;
    crate::open_test_store!(TelegramIntakeStore, "telegram_intake")
}

/// A run's own handle onto `store` — a listener's, or a check's own probe.
/// `claim_label` names the run it stands for: a restart, or a row a previous run
/// left behind, is another run, so it must claim under a different id.
fn run(store: &TelegramIntakeStore, claim_label: &str, account: &str) -> Intake {
    Intake::new(store.clone(), Arc::from(claim_label), account)
}

/// A listener pointed at `fake`, recording and claiming through `store`. Its claim
/// is its own, exactly as it is for a listener the product builds, so a second
/// listener over one store is the different run a restart makes of it.
fn channel(token: &str, fake: &FakeTelegram, store: &TelegramIntakeStore) -> TelegramChannel {
    TelegramChannel::for_test(token.into(), fake.base.clone(), store.clone())
}

/// The events the account's record still holds in progress, read straight from
/// the rows so a check observes them without claiming them.
async fn still_held(store: &TelegramIntakeStore, account: &str) -> Vec<i64> {
    let rows = store
        .conn
        .query(
            "SELECT update_id FROM telegram_intake \
             WHERE account_id = ?1 AND state = ?2",
            crate::db::params![account, IN_PROGRESS],
        )
        .await
        .expect("read the record");
    rows.into_iter()
        .map(|row| row.get::<i64>(0).expect("update_id"))
        .collect()
}

/// One `telegram_bursts` row as a check reads it back.
struct BurstRow {
    user_name: String,
    parts: i64,
    released_by: String,
    part_times: Vec<serde_json::Value>,
}

/// The burst records written so far, oldest first.
async fn burst_rows(store: &TelegramIntakeStore) -> Vec<BurstRow> {
    let rows = store
        .conn
        .query(
            "SELECT user_name, parts, released_by, part_times FROM telegram_bursts ORDER BY id",
            (),
        )
        .await
        .expect("read the burst record");
    rows.into_iter()
        .map(|row| BurstRow {
            user_name: row.get::<String>(0).expect("user_name"),
            parts: row.get::<i64>(1).expect("parts"),
            released_by: row.get::<String>(2).expect("released_by"),
            part_times: serde_json::from_str(&row.get::<String>(3).expect("part_times"))
                .expect("part_times is JSON"),
        })
        .collect()
}

/// An event the intake recorded but had not handed on when the process died is
/// taken up by the next run: without this, everything that arrived in the
/// intake's last moments would be silently dropped. The restart runs the token
/// rotated for the same account — the record is keyed on the bot account the
/// platform names rather than on the token's text, so what the dead run held is
/// this run's to take up.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn restart_takes_up_the_event_a_previous_listener_held() {
    // Two tokens the platform maps to one account: a rotation, not a new bot.
    let fake = FakeTelegram::start(&[("token-a", 700), ("token-b", 700)]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    let (store, _dir) = test_store().await;

    // The first listener records and holds the event under its own claim.
    let first = channel("token-a", &fake, &store);

    // The process dies mid-flight: the sink is already gone, so the collector's
    // hand-over can never succeed and the event stays held — exactly what a run
    // that died before handing the event on leaves behind. Dropping the receiver
    // up front also keeps the wait below off the collector's one-second window:
    // what it waits for is a state nothing in this check can clear.
    let (tx, rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = first.listen(tx).await;
    });
    drop(rx);

    until("the record holds the event", || async {
        !still_held(&store, "700").await.is_empty()
    })
    .await;
    listener.abort();
    let _ = listener.await;
    assert!(
        !still_held(&store, "700").await.is_empty(),
        "the event must survive the process that never handed it on"
    );

    // The restart is a listener of its own, so its claim is its own too, on the
    // rotated token for the same account, and the event the dead listener left
    // held is taken up through the claim path.
    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let restarted = channel("token-b", &fake, &store);
    let listener = tokio::spawn(async move {
        let _ = restarted.listen(tx).await;
    });

    let msg = next_message(&mut rx, "the event the previous run held").await;
    assert_eq!(msg.content, "hello");
    assert_eq!(msg.channel, "telegram");
    // The hand-over takes the record entries it settles off the message, so the
    // pipeline sees an event already accounted for.
    assert!(
        msg.update_ids.is_empty(),
        "the hand-over must settle the entries it carries"
    );

    until("the hand-over settles the recovered event", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;
    listener.abort();
}

/// An album is one incoming event: its members are recorded, handed over as one
/// message and settled together, never taken up as unrelated messages.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn an_album_is_taken_up_and_settled_as_one_event() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_batch(
        "token-a",
        vec![
            test_album_member(1, "group-1", "one"),
            test_album_member(2, "group-1", "two"),
        ],
    );
    let (store, _dir) = test_store().await;
    let telegram = channel("token-a", &fake, &store);

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let msg = next_message(&mut rx, "the album, as one message").await;
    assert_eq!(
        msg.content, "one\ntwo",
        "an album's members must reach the pipeline as one message"
    );
    // Both members are settled by that one hand-over — the record keeps no part of
    // the album behind.
    until("the album is settled whole", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;

    // And no member arrives as a message of its own.
    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "an album's member was handled as an unrelated message"
    );
    listener.abort();
}

/// An album the record holds is recovered as one event too: recovery re-parses the
/// stored updates, so the album's members regroup into the one message they are and
/// settle together.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_recovered_album_is_taken_up_as_one_event() {
    // Nothing is queued: the handshake's probe gets an empty result and the long
    // poll then hangs, which keeps the listener quiet while recovery runs.
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    let (store, _dir) = test_store().await;
    run(&store, "previous-run", "700")
        .hold(&[
            &test_album_member(1, "group-1", "one"),
            &test_album_member(2, "group-1", "two"),
        ])
        .await
        .expect("hold the album");

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let telegram = channel("token-a", &fake, &store);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let msg = next_message(&mut rx, "the recovered album, as one message").await;
    assert_eq!(
        msg.content, "one\ntwo",
        "a recovered album's members must reach the pipeline as one message"
    );
    until("the recovered album is settled whole", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;
    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "a recovered album's member was handled as an unrelated message"
    );
    listener.abort();
}

/// A token change to another account takes up that account's own backlog, and
/// leaves the previous account's held events alone: an `update_id` is only an
/// identity inside its account, so the new listener must not read the old
/// account's event as a repeat of its own.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_token_change_to_another_account_takes_its_own_backlog() {
    let fake = FakeTelegram::start(&[("token-a", 700), ("token-b", 800)]);
    let (store, _dir) = test_store().await;

    // A row the previous run left behind under its own claim.
    run(&store, "previous-run", "700")
        .hold(&[&test_text_update(1, "a-only")])
        .await
        .expect("hold the previous account's event");
    // The same `update_id` from the account the new token belongs to, carrying
    // content of its own.
    fake.push_batch("token-b", vec![test_text_update(1, "b-only")]);

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let telegram = channel("token-b", &fake, &store);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let msg = next_message(&mut rx, "the new account's own event").await;
    assert_eq!(msg.content, "b-only");

    // The previous account's held event is abandoned rather than retried under
    // the new account, so nothing else arrives.
    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "the previous account's event was taken up under the new account"
    );
    assert!(
        !still_held(&store, "700").await.is_empty(),
        "the previous account's held event must be left alone"
    );
    listener.abort();
}

/// The hand-over a token change runs — the replaced listener stopped and waited out,
/// the new one listening where it left off — takes up what the replaced listener held,
/// exactly once.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_token_change_hands_over_what_the_replaced_listener_held_once() {
    // Two tokens the platform maps to one account: a rotation, not a new bot.
    let fake = FakeTelegram::start(&[("token-a", 700), ("token-b", 700)]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    let (store, _dir) = test_store().await;

    // The listener being replaced cannot hand its burst on — its sink is gone — which
    // is what leaves the event held for the replacement to take up.
    let old: std::sync::Arc<dyn Channel> = std::sync::Arc::new(channel("token-a", &fake, &store));
    let (tx, rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn({
        let old = std::sync::Arc::clone(&old);
        async move {
            let _ = old.listen(tx).await;
        }
    });
    drop(rx);
    until("the record holds the event", || async {
        !still_held(&store, "700").await.is_empty()
    })
    .await;

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let new: std::sync::Arc<dyn Channel> = std::sync::Arc::new(channel("token-b", &fake, &store));
    // The registry's own record of what a replace moves past is what its hand-over
    // waits out, and the channel this token change displaces is `old`.
    let registry = crate::ChannelRegistry::default();
    registry.register(std::sync::Arc::clone(&old));
    let frames = registry.replaced("telegram");
    registry.replace(std::sync::Arc::clone(&new));
    let hand_over = tokio::spawn(hand_over_listener(frames, new, tx));

    let msg = next_message(&mut rx, "the event the replaced listener held").await;
    assert_eq!(msg.content, "hello");
    until("the hand-over settles the recovered event", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;
    assert!(
        tokio::time::timeout(crate::channels::telegram_group::QUIET_WINDOW * 2, rx.recv())
            .await
            .is_err(),
        "a recovered event must be handed on exactly once"
    );
    listener.abort();
    hand_over.abort();
}

/// A token cleared and then set again must not cost the channel that is live by then:
/// outbound sends and a later restart both reach the listener through the registry, so
/// a teardown that removed its channel by name would silently stop every Telegram send
/// after a quick clear-then-set. Both the teardown and the hand-over the save schedules
/// wait on the one frame the cleared listener runs, so ending that frame has to release
/// both of them.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_clear_then_set_releases_both_waiters_and_keeps_its_channel() {
    let fake = FakeTelegram::start(&[("token-a", 700), ("token-b", 700)]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    let (store, _dir) = test_store().await;

    // A registry of this check's own: the product's is a process global.
    let registry = crate::ChannelRegistry::default();

    // The listener being cleared, holding a burst it cannot hand on: the pipeline has
    // no room in it, so the frame the teardown waits for is still open.
    let (tx, mut rx) = tokio::sync::mpsc::channel(1);
    tx.send(ChannelMessage::default())
        .await
        .expect("the pipeline accepts the message that fills it");
    let old: std::sync::Arc<dyn Channel> = std::sync::Arc::new(channel("token-a", &fake, &store));
    registry.register(std::sync::Arc::clone(&old));
    let listener = tokio::spawn({
        let old = std::sync::Arc::clone(&old);
        async move {
            let _ = old.listen(tx).await;
        }
    });
    until("the intake takes the event on", || async {
        !still_held(&store, "700").await.is_empty()
    })
    .await;

    // The clear: its teardown records the cleared channel among the frames a hand-over
    // waits out and waits for the frame the check is holding open. `frames` is that same
    // list under the name, which the hand-over below waits out too.
    let frames = registry.replaced("telegram");
    let teardown = retire_listener(&registry, Some(std::sync::Arc::clone(&old)));
    tokio::pin!(teardown);
    assert!(
        tokio::time::timeout(Duration::from_millis(200), &mut teardown)
            .await
            .is_err(),
        "the clear's teardown must wait for the replaced listener's frame"
    );

    // The save that lands while it waits: the new token's channel takes the cleared
    // one's place, exactly as `restart_telegram_listener` does — which records it as a
    // frame to wait out — and the wait its hand-over starts lands on the same frame:
    // the second waiter. The hand-over is handed the channel it is about to start, as
    // `hand_over_listener` does.
    let new: std::sync::Arc<dyn Channel> = std::sync::Arc::new(channel("token-b", &fake, &store));
    registry.replace(std::sync::Arc::clone(&new));
    let mut hand_over = tokio::spawn({
        let frames = std::sync::Arc::clone(&frames);
        let new = std::sync::Arc::clone(&new);
        async move { stop_listeners(&frames, Some(&new)).await }
    });
    assert!(
        tokio::time::timeout(Duration::from_millis(200), &mut hand_over)
            .await
            .is_err(),
        "the hand-over's wait must park on the replaced listener's frame"
    );

    // Let the replaced frame end — the burst goes through and settles — and the
    // teardown and the hand-over waiting on it are both released by it. Taking the
    // filler out is what makes room.
    rx.recv()
        .await
        .expect("the message that filled the pipeline");
    let msg = next_message(&mut rx, "the burst the cleared listener held").await;
    assert_eq!(msg.content, "hello");
    tokio::time::timeout(OBSERVABLE_TIMEOUT, teardown)
        .await
        .expect("the clear's teardown must finish once the frame has ended");
    tokio::time::timeout(OBSERVABLE_TIMEOUT, hand_over)
        .await
        .expect("the hand-over waiting on the frame must finish with it")
        .expect("the hand-over task must not panic");

    assert!(
        registry
            .get("telegram")
            .is_some_and(|live| std::sync::Arc::ptr_eq(&live, &new)),
        "the channel that is live after a quick clear-then-set must stay registered"
    );
    listener.abort();
}

/// A hand-over never stops the very channel it is about to start, even when that channel
/// is in the frames list under the name — the state a superseded save leaves: the next
/// save's replace has already moved past the channel the first one built, while that
/// channel's own hand-over has not run yet. A stop would fire its cancel token, so the
/// listener the hand-over starts right after would return at once; it is left in the list
/// instead, for the hand-over that displaces it to stop.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_hand_over_never_stops_the_channel_it_is_about_to_start() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_batch("token-a", vec![test_text_update(1, "own")]);
    let (store, _dir) = test_store().await;

    let registry = crate::ChannelRegistry::default();
    let frames = registry.replaced("telegram");
    let new: std::sync::Arc<dyn Channel> = std::sync::Arc::new(channel("token-a", &fake, &store));
    // Two saves in a row, each replacing what the one before built: the second's replace
    // is what moves past `new`.
    registry.replace(std::sync::Arc::clone(&new));
    registry.replace(std::sync::Arc::new(channel("token-a", &fake, &store)));

    stop_listeners(&frames, Some(&new)).await;

    assert!(
        frames
            .snapshot()
            .iter()
            .any(|held| std::sync::Arc::ptr_eq(held, &new)),
        "a hand-over retired the channel it is about to start"
    );
    // And the listener that hand-over starts up still serves its own poll: the channel
    // was never cancelled.
    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn({
        let new = std::sync::Arc::clone(&new);
        async move {
            let _ = new.listen(tx).await;
        }
    });
    let msg = next_message(&mut rx, "the batch of the channel a hand-over left alone").await;
    assert_eq!(msg.content, "own");
    listener.abort();
}

/// The other half of a clear: when nothing took the cleared channel's place while its
/// teardown waited, the channel must not be left registered — outbound sends would
/// otherwise keep reaching a listener that is no longer running.
#[tokio::test(flavor = "multi_thread")]
async fn a_cleared_token_takes_its_channel_out_of_the_registry() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    let (store, _dir) = test_store().await;

    let registry = crate::ChannelRegistry::default();
    let old: std::sync::Arc<dyn Channel> = std::sync::Arc::new(channel("token-a", &fake, &store));
    registry.register(std::sync::Arc::clone(&old));

    retire_listener(&registry, Some(old)).await;
    assert!(
        registry.get("telegram").is_none(),
        "a cleared token must leave no channel behind"
    );
}

/// Saves landing on each other's heels: each is answered with the channel the one before
/// it built, whose listener has not begun, while the frame that really holds events — the
/// one at the head of the rebuild — is still open. The wait therefore cannot live on the
/// channels: whichever hand-over walks such a chain first consumes it, and the save after
/// that would start a listener with the frame that holds undelivered events still open,
/// taking those events over as they are handed on and reaching the pipeline twice with
/// the same content. It lives under the channel's name, so every hand-over waits out the
/// same frames.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_save_landing_on_another_waits_out_the_frame_that_holds_events() {
    let fake = FakeTelegram::start(&[
        ("token-a", 700),
        ("token-b", 700),
        ("token-c", 700),
        ("token-d", 700),
    ]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    // A batch of its own for each channel a save builds, left waiting there: a frame
    // begun on one would fetch it.
    fake.push_batch("token-b", vec![test_text_update(2, "from-b")]);
    fake.push_batch("token-c", vec![test_text_update(3, "from-c")]);
    fake.push_batch("token-d", vec![test_text_update(4, "from-d")]);
    let (store, _dir) = test_store().await;

    // A registry of this check's own: the product's is a process global.
    let registry = crate::ChannelRegistry::default();

    // The listener every save replaces, holding a burst it cannot hand on: its pipeline
    // has no room in it, so its frame stays open.
    let (tx, mut rx) = tokio::sync::mpsc::channel(1);
    tx.send(ChannelMessage::default())
        .await
        .expect("the pipeline accepts the message that fills it");
    let old: std::sync::Arc<dyn Channel> = std::sync::Arc::new(channel("token-a", &fake, &store));
    registry.register(std::sync::Arc::clone(&old));
    let listener = tokio::spawn({
        let old = std::sync::Arc::clone(&old);
        async move {
            let _ = old.listen(tx).await;
        }
    });
    until("the intake takes the event on", || async {
        !still_held(&store, "700").await.is_empty()
    })
    .await;

    // Three saves in a row, each spawning its hand-over as the product does: the second
    // and the third land while the first hand-over is still waiting on the frame that
    // holds the event, so the channels they are answered with have no frame of their own
    // yet, and a channel an answer displaced may well fetch its own batch before the save
    // after it stops it.
    let first: std::sync::Arc<dyn Channel> = std::sync::Arc::new(channel("token-b", &fake, &store));
    let second: std::sync::Arc<dyn Channel> =
        std::sync::Arc::new(channel("token-c", &fake, &store));
    let third: std::sync::Arc<dyn Channel> = std::sync::Arc::new(channel("token-d", &fake, &store));
    let frames = registry.replaced("telegram");

    let (tx_first, mut rx_first) = tokio::sync::mpsc::channel(8);
    let (tx_second, mut rx_second) = tokio::sync::mpsc::channel(8);
    let (tx_third, mut rx_third) = tokio::sync::mpsc::channel(8);
    registry.replace(std::sync::Arc::clone(&first));
    let hand_over_first = tokio::spawn(hand_over_listener(
        std::sync::Arc::clone(&frames),
        std::sync::Arc::clone(&first),
        tx_first,
    ));
    registry.replace(std::sync::Arc::clone(&second));
    let hand_over_second = tokio::spawn(hand_over_listener(
        std::sync::Arc::clone(&frames),
        std::sync::Arc::clone(&second),
        tx_second,
    ));
    registry.replace(std::sync::Arc::clone(&third));
    let hand_over_third = tokio::spawn(hand_over_listener(
        std::sync::Arc::clone(&frames),
        std::sync::Arc::clone(&third),
        tx_third,
    ));

    // While the frame that holds the event is still open, the event stays where it is: no
    // listener of a save that came after it may begin, on pain of taking that event over as
    // it is handed on. The check holds the frame open while it looks, and the negative wait
    // is the shape the property takes: `probe` must not come true while it is held.
    let save_listeners = [&first, &second, &third].map(|channel| {
        channel
            .as_any()
            .downcast_ref::<TelegramChannel>()
            .expect("the check's own channel")
    });
    assert!(
        tokio::time::timeout(crate::channels::telegram_group::QUIET_WINDOW * 2, async {
            while !save_listeners.iter().any(|listener| listener.running()) {
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        })
        .await
        .is_err(),
        "a save's listener began before the frame that holds events ended"
    );

    // Make room: the replaced frame ends, handing its burst on and settling it, and only
    // then can the hand-overs' listeners begin. Each of them was parked on that frame, so
    // the end of it releases every one. Taking the filler out is what makes room.
    rx.recv()
        .await
        .expect("the message that filled the pipeline");
    let msg = next_message(&mut rx, "the event the replaced listener held").await;
    assert_eq!(msg.content, "hello");
    until("the replaced frame settles the event", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;
    tokio::time::timeout(OBSERVABLE_TIMEOUT, hand_over_first)
        .await
        .expect("a hand-over waiting on the frame must finish with it")
        .expect("the hand-over task must not panic");
    tokio::time::timeout(OBSERVABLE_TIMEOUT, hand_over_second)
        .await
        .expect("a hand-over waiting on the frame must finish with it")
        .expect("the hand-over task must not panic");

    // Only the last save's listener began: the event the replaced frame handed on is not
    // handed on again, and what the last save's listener serves is its own batch.
    let msg = next_message(
        &mut rx_third,
        "the last save's listener takes up its own poll",
    )
    .await;
    let carried: Vec<&str> = std::iter::once(msg.content.as_str())
        .chain(msg.parts.iter().map(|part| part.content.as_str()))
        .collect();
    assert!(
        carried.contains(&"from-d"),
        "the listener that came last must take up its own batch: {carried:?}"
    );
    assert!(
        !carried.contains(&"hello"),
        "the event the replaced frame handed on must not reach the listener that came last: {carried:?}"
    );
    let handed_on_twice = tokio::time::timeout(
        crate::channels::telegram_group::QUIET_WINDOW * 2,
        rx_third.recv(),
    )
    .await;
    assert!(
        handed_on_twice.is_err(),
        "the event the replaced frame handed on was handled a second time"
    );
    // And the channels the earlier saves built — stopped by the save that came after them
    // — serve their own batches at most, never the event the replaced frame handed on.
    for (rx, what) in [
        (&mut rx_first, "the first save's channel"),
        (&mut rx_second, "the second save's channel"),
    ] {
        while let Ok(Some(msg)) =
            tokio::time::timeout(crate::channels::telegram_group::QUIET_WINDOW * 2, rx.recv()).await
        {
            let carried: Vec<&str> = std::iter::once(msg.content.as_str())
                .chain(msg.parts.iter().map(|part| part.content.as_str()))
                .collect();
            assert!(
                !carried.contains(&"hello"),
                "{what} handed on the event the replaced frame held: {carried:?}"
            );
        }
    }
    assert!(
        registry
            .get("telegram")
            .is_some_and(|live| std::sync::Arc::ptr_eq(&live, &third)),
        "the listener that came last is the one the registry holds"
    );
    listener.abort();
    hand_over_third.abort();
}

/// A row past the retention bound is swept before the first batch is matched: the
/// platform re-uses `update_id` once it has been silent long enough, so a decision
/// kept past the bound would be taken for the new event and swallow it.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn an_event_the_retention_bound_swept_is_not_taken_for_a_repeat() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    let (store, _dir) = test_store().await;

    // A decision older than the bound, for the very `update_id` the platform now
    // offers for a new event.
    let previous = run(&store, "previous-run", "700");
    previous
        .hold(&[&test_update(&[])])
        .await
        .expect("hold the old event");
    previous.settle(&[1]).await;
    let old = (chrono::Utc::now() - chrono::Duration::hours(RETENTION_HOURS + 1)).to_rfc3339();
    store
        .conn
        .execute(
            "UPDATE telegram_intake SET decided_at = ?1",
            crate::db::params![old],
        )
        .await
        .expect("age the decision past the bound");

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let telegram = channel("token-a", &fake, &store);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let msg = next_message(&mut rx, "the event the platform re-used the id of").await;
    assert_eq!(msg.content, "hello");
    listener.abort();
}

/// A listener's frame is over only once what it held has been handed on: the frame
/// waits for its collector, and the collector hands over — and settles — the burst it
/// was still holding. That is what lets the listener replacing a cancelled one take
/// over only what the cancelled one really left behind.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_cancelled_listener_ends_its_frame_only_once_what_it_held_is_handed_on() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    let (store, _dir) = test_store().await;

    // A pipeline with no room in it: the collector cannot hand the burst over until
    // the check takes something out, which is what holds the listener's frame open.
    let (tx, mut rx) = tokio::sync::mpsc::channel(1);
    tx.send(ChannelMessage::default())
        .await
        .expect("the pipeline accepts the message that fills it");
    let telegram = std::sync::Arc::new(channel("token-a", &fake, &store));
    let listener = tokio::spawn({
        let telegram = std::sync::Arc::clone(&telegram);
        async move {
            let _ = telegram.listen(tx).await;
        }
    });

    until("the intake takes the event on", || async {
        !still_held(&store, "700").await.is_empty()
    })
    .await;

    // How the listener that replaces this one stops it (see `stop_listeners`).
    telegram.cancel_own();
    assert!(
        tokio::time::timeout(Duration::from_millis(500), telegram.wait_stopped())
            .await
            .is_err(),
        "the frame ended while the burst it held was still unhanded"
    );

    // Make room: the hand-over goes through, which settles the event, and only then
    // does the frame end.
    let filler = rx
        .recv()
        .await
        .expect("the message that filled the pipeline");
    assert!(filler.content.is_empty());
    let msg = next_message(&mut rx, "the event the frame held").await;
    assert_eq!(msg.content, "hello");
    tokio::time::timeout(OBSERVABLE_TIMEOUT, telegram.wait_stopped())
        .await
        .expect("the frame must end once what it held was handed on");
    until("the hand-over settles the event", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;
    listener.abort();

    // The replacement a cancel-and-wait makes room for: because the frame ended only
    // once what it held had been handed on and settled, there is nothing left for the
    // replacement to take up, so the event reaches the pipeline exactly once. Nothing
    // can be delivered sooner than the collector's quiet window, so the wait below is
    // that window with room to spare.
    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let replacement = channel("token-a", &fake, &store);
    let listener = tokio::spawn(async move {
        let _ = replacement.listen(tx).await;
    });
    assert!(
        tokio::time::timeout(crate::channels::telegram_group::QUIET_WINDOW * 2, rx.recv())
            .await
            .is_err(),
        "the replacement must not hand on what the cancelled frame already settled"
    );
    listener.abort();
}

/// The frame is the collector's, not the listening future's: a listener that ends
/// abruptly — its task dropped, or a panic — still leaves its frame open while its
/// collector holds a burst that has not been handed on, so a replacement waits out the
/// hand-over instead of starting on top of it. The cancelled listener above ends the way
/// a token change stops one; this is the end nothing asks for.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_dropped_listener_leaves_its_frame_open_until_the_burst_is_handed_on() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    let (store, _dir) = test_store().await;

    // A pipeline with no room in it: the collector cannot hand the burst over until
    // the check takes something out, which is what holds the listener's frame open.
    let (tx, mut rx) = tokio::sync::mpsc::channel(1);
    tx.send(ChannelMessage::default())
        .await
        .expect("the pipeline accepts the message that fills it");
    let telegram = std::sync::Arc::new(channel("token-a", &fake, &store));
    let listener = tokio::spawn({
        let telegram = std::sync::Arc::clone(&telegram);
        async move {
            let _ = telegram.listen(tx).await;
        }
    });

    // The burst is in the collector's hands before the listener is dropped: the poll the
    // intake issues after taking the event on is the sign of it (a listener whose
    // hand-over to the collector failed never polls again).
    until("the intake handed the burst to the collector", || async {
        fake.count("getUpdates") >= 2
    })
    .await;
    listener.abort();
    let _ = listener.await;

    assert!(
        telegram.running(),
        "the frame must be the collector's, not the listening future's"
    );
    assert!(
        tokio::time::timeout(Duration::from_millis(500), telegram.wait_stopped())
            .await
            .is_err(),
        "the frame ended while the burst it held was still unhanded"
    );

    // Make room: the collector hands the burst over and settles it, and only then does
    // the frame end — the state a replacement waits for.
    let filler = rx
        .recv()
        .await
        .expect("the message that filled the pipeline");
    assert!(filler.content.is_empty());
    let msg = next_message(&mut rx, "the burst the dropped listener held").await;
    assert_eq!(msg.content, "hello");
    tokio::time::timeout(OBSERVABLE_TIMEOUT, telegram.wait_stopped())
        .await
        .expect("the frame must end once what it held was handed on");
    until("the hand-over settles the event", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;
}

/// Once the daemon winds down the intake stops fetching — but the event it already
/// holds is still handed over and settled. The hand-over happens under the wind-down:
/// the pipeline has no room for the burst until the check makes it, so the event sits
/// in the record while the listener winds down, and is neither dropped nor left
/// unhandled. Serialized on the process-global drain flag, as
/// `failure_classification_recognizes_drain` is.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_wind_down_stops_fetching_and_keeps_what_it_holds() {
    /// Restores the process-global drain flag even when an assertion fails: a
    /// flag left set would misclassify every later check that consults it.
    struct DrainRestored;
    impl Drop for DrainRestored {
        fn drop(&mut self) {
            crate::shutdown::drain_clear();
        }
    }

    let fake = FakeTelegram::start(&[("token-a", 700)]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    let (store, _dir) = test_store().await;
    let telegram = channel("token-a", &fake, &store);

    // A pipeline with no room in it: the burst the intake takes on cannot be handed
    // over until the check takes something out, which is what forces the hand-over
    // under the wind-down below.
    let (tx, mut rx) = tokio::sync::mpsc::channel(1);
    tx.send(ChannelMessage::default())
        .await
        .expect("the pipeline accepts the message that fills it");
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    // The intake has taken the event on: the acknowledgement its own parse sent is
    // the durable sign of it. The record's hold would not be — the collector clears
    // it once it hands the burst over — but the pipeline has no room, so the hold
    // stays where the assertion below reads it.
    until("the intake takes the event on", || async {
        fake.count("setMessageReaction") >= 1
    })
    .await;
    // The loop's own poll must be in flight before the count below is read: a
    // poll issued after the wind-down would answer a different question.
    until("the intake is polling", || async {
        fake.count("getUpdates") >= 2
    })
    .await;
    let polls_before = fake.count("getUpdates");

    let _restored = DrainRestored;
    crate::shutdown::drain_begin();

    // The listener cannot finish here: its frame is the collector's, still holding
    // the burst the pipeline has no room for, so an await on the task would hang
    // rather than fail. What the check can see, while the frame is open, is that the
    // event is still in the record — the wind-down did not drop what the intake had
    // already taken on.
    assert_eq!(
        still_held(&store, "700").await,
        vec![1],
        "the wind-down must keep the event the intake already took on"
    );

    // Make room: the collector hands the burst it held under the wind-down over, and
    // the event reaches the pipeline as itself, not as the filler the check sent.
    rx.recv()
        .await
        .expect("the message that filled the pipeline");
    let msg = next_message(&mut rx, "the event the wind-down flushed").await;
    assert_eq!(msg.content, "hello");
    until("the wind-down settles the event", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;

    // What the frame held has been handed on and settled, so the frame — and with it
    // the listener task — is over.
    tokio::time::timeout(OBSERVABLE_TIMEOUT, listener)
        .await
        .expect("the listener must stop when the daemon winds down")
        .expect("the listener task must not panic");

    // Nothing was taken on after the wind-down began. The count is read at the end
    // because the listener's frame cannot end while the burst is unhanded, so the
    // fetch-stop cannot be asserted any earlier.
    assert_eq!(
        fake.count("getUpdates"),
        polls_before,
        "the intake kept fetching while winding down"
    );
}

/// An event the record already holds is never taken up twice: the platform
/// re-offers a batch until its position moves past it, so the same update can
/// legitimately arrive more than once.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn an_event_the_record_already_holds_is_not_taken_up_again() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    // The handshake consumes the first batch and the first long poll the second.
    fake.push_batch("token-a", vec![test_update(&[])]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    let (store, _dir) = test_store().await;
    let telegram = channel("token-a", &fake, &store);

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let msg = next_message(&mut rx, "the event, exactly once").await;
    assert_eq!(msg.content, "hello");
    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "an event the record already held was taken up again"
    );

    // The record must also let the position move past what it holds: the
    // platform re-offers a batch until the position passes it, so a position
    // that stalls at the held event would re-offer it forever. A stalled
    // position pauses before its next poll, so the wait uses the widened bound:
    // with the cheap one it would time out right as the third poll arrives, and
    // the failure would be timing-luck rather than the assertion below.
    until_within(
        BACKOFF_OBSERVABLE_TIMEOUT,
        "the fake has logged three polls",
        || async { fake.count("getUpdates") >= 3 },
    )
    .await;
    // The handshake's poll is the first, at offset 0; every poll after it must
    // ask for an offset past the event the record holds — the fake serves
    // `update_id` 1 — or the batch is offered again forever because the position
    // never moved past what the record already holds.
    let offsets = fake.poll_offsets();
    assert!(
        offsets[1..].iter().all(|&offset| offset >= 2),
        "the batch is offered again forever because the position never moved past \
         the event the record already holds: poll offsets {offsets:?}"
    );

    // Settled once, by the one hand-over that happened.
    until("the hand-over settles the event", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;
    listener.abort();
}

/// Control input recovered after a restart is not executed out of context: the
/// user is told to repeat it instead, so a stale `/clear` cannot wipe a session
/// at a moment the user did not choose.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn recovered_control_input_is_not_executed_out_of_context() {
    // Nothing is queued: the handshake's probe gets an empty result and the long
    // poll then hangs, which keeps the listener quiet while recovery runs.
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    let (store, _dir) = test_store().await;
    // A command a previous run left behind, claimed under its own id, so the
    // listener — a new run — takes it up through the claim path.
    run(&store, "previous-run", "700")
        .hold(&[&test_text_update(1, "/clear")])
        .await
        .expect("hold the recovered command");

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let telegram = channel("token-a", &fake, &store);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    until("the recovered command is settled", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;

    assert!(
        fake.sent_texts()
            .iter()
            .any(|text| text == UNACTED_CONTROL_NOTICE),
        "the user must be told the recovered command was not carried out"
    );
    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "a recovered command must not be executed out of context"
    );
    listener.abort();
}

/// A backlog — what the platform accumulated while the product was down — is input
/// the product never acted on, wherever in it a command sits: the opening handshake's
/// batch is of unknown age and so is every page behind it, so a command in any of
/// them is refused and its user told to repeat it — a stale `/clear` must not wipe a
/// session at a moment the user did not choose.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn control_input_anywhere_in_a_backlog_is_not_executed() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    // A full page of what the platform accumulated while the product was down, with a
    // command at its end — the batch the opening handshake fetches — and a second page
    // carrying another command, which only a full first page puts behind the backlog's
    // end. The rest of the first page parses to nothing, so neither the record nor the
    // notices below can be confused with traffic those updates produce.
    let page = i64::try_from(UPDATES_PAGE).expect("a page of updates fits an update_id");
    let mut first: Vec<serde_json::Value> = (1..page)
        .map(|update_id| serde_json::json!({ "update_id": update_id }))
        .collect();
    first.push(test_text_update(page, "/clear"));
    fake.push_batch("token-a", first);
    fake.push_batch("token-a", vec![test_text_update(page + 1, "/clear")]);
    let (store, _dir) = test_store().await;

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let telegram = channel("token-a", &fake, &store);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    until("both backlogged commands are refused", || async {
        fake.sent_texts()
            .iter()
            .filter(|text| *text == UNACTED_CONTROL_NOTICE)
            .count()
            == 2
    })
    .await;
    until("the backlog is settled", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;

    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "control input anywhere in a backlog must not be executed"
    );
    listener.abort();
}

/// The complement of the refusals: a command this run's own poll fetched is acted on
/// as it always was — the intake hands it to the pipeline instead of refusing it. A
/// classification that refused every control input would leave every other check here
/// green while breaking every command in production.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn control_input_of_this_runs_own_poll_is_dispatched() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    // The handshake's zero-timeout probe takes the empty batch, which is what ends the
    // backlog and leaves the loop's own long poll fresh; the command is the batch
    // after it, so the loop is what fetches it.
    fake.push_batch("token-a", Vec::new());
    fake.push_batch("token-a", vec![test_text_update(1, "/clear")]);
    let (store, _dir) = test_store().await;

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let telegram = channel("token-a", &fake, &store);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let msg = next_message(&mut rx, "the command this run's own poll fetched").await;
    assert_eq!(msg.content, "/clear");
    assert!(
        !fake
            .sent_texts()
            .iter()
            .any(|text| text == UNACTED_CONTROL_NOTICE),
        "a command of this run's own poll is carried out, not refused"
    );
    until("the dispatched command is settled", || async {
        still_held(&store, "700").await.is_empty()
    })
    .await;
    listener.abort();
}

/// Control input in a batch the intake had to fetch twice is not executed: the
/// record could not be written for the batch the handshake fetched, so the poll
/// that collects it again is looking at events this run already saw once, and a
/// command of unknown age must not run.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn control_input_in_a_reoffered_batch_is_not_executed() {
    // The same command three times over: the handshake fetches the first copy with
    // the record unwritable, the loop's first poll the second (still unwritable), and
    // the poll after that the third, once the record is writable again.
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    for _ in 0..3 {
        fake.push_batch("token-a", vec![test_text_update(1, "/clear")]);
    }
    let (store, _dir) = test_store().await;
    store
        .conn
        .execute(
            "ALTER TABLE telegram_intake RENAME TO telegram_intake_hidden",
            (),
        )
        .await
        .expect("make the record unwritable");
    let telegram = channel("token-a", &fake, &store);

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    // The handshake's poll is the first `getUpdates` and the loop's the second, so
    // by the time the second arrives the command's first take-up has run and failed
    // to record it — which is what held the position at the command and what makes
    // the batch the loop fetches a re-offer. The handshake's failed take-up waits out
    // the backoff before the loop starts, hence the widened bound.
    until_within(BACKOFF_OBSERVABLE_TIMEOUT, "the loop's poll", || async {
        fake.count("getUpdates") >= 2
    })
    .await;
    store
        .conn
        .execute(
            "ALTER TABLE telegram_intake_hidden RENAME TO telegram_intake",
            (),
        )
        .await
        .expect("restore the record");

    until_within(
        BACKOFF_OBSERVABLE_TIMEOUT,
        "the user is told the re-offered command was not carried out",
        || async {
            fake.sent_texts()
                .iter()
                .any(|text| text == UNACTED_CONTROL_NOTICE)
        },
    )
    .await;
    // The notice and the settle both wait out a backoff, so their bound is the
    // widened one rather than the bound every other wait here uses.
    until_within(
        BACKOFF_OBSERVABLE_TIMEOUT,
        "the re-offered command is settled",
        || async { still_held(&store, "700").await.is_empty() },
    )
    .await;
    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "control input in a batch this run already fetched once must not be executed"
    );
    listener.abort();
}

/// An event whose record cannot be written for is neither acknowledged nor
/// confirmed: nothing is answered before the event's fate is durable, and the
/// intake backs off instead of re-polling the batch in a tight loop.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn an_event_the_record_cannot_be_written_for_is_neither_acknowledged_nor_confirmed() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    // Two batches: the handshake consumes the first and the loop's first poll
    // the second, which is what gives a poll an event whose record cannot be
    // written for — dropping the table alone is invisible until one arrives.
    fake.push_batch("token-a", vec![test_update(&[])]);
    fake.push_batch("token-a", vec![test_update(&[])]);
    let (store, _dir) = test_store().await;
    store
        .conn
        .execute("DROP TABLE telegram_intake", ())
        .await
        .expect("make the record unwritable");
    let telegram = channel("token-a", &fake, &store);

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    // The handshake consumed the first batch and the loop's first poll the
    // second, which is the poll whose record could not be written for. The poll
    // after it is the one the backoff has to hold off, and it arrives with
    // nothing left to serve, so it hangs (and is still logged on arrival).
    // Waiting for it needs a bound past a pause — and past two, since the
    // handshake pauses on the same unrecordable batch before the loop even
    // starts — hence the widened one rather than `until`, whose bound is the
    // cheap one every other wait here uses.
    until_within(
        BACKOFF_OBSERVABLE_TIMEOUT,
        "the intake retries the unrecordable batch after its backoff",
        || async { fake.count("getUpdates") >= 3 },
    )
    .await;

    // The pause belongs to the intake's own loop, and the handshake's poll is the
    // one that asked for offset 0, so the loop's polls are every poll whose
    // offset is above 0. The gap between the first two of them is the check's
    // real claim: a tight retry of the same unrecordable batch would put them
    // milliseconds apart, not a second apart.
    let loop_polls: Vec<std::time::Instant> = {
        let requests = fake.state.requests.lock().expect("fake request log");
        requests
            .iter()
            .filter(|request| {
                request.method == "getUpdates" && request.offset.is_some_and(|offset| offset > 0)
            })
            .map(|request| request.at)
            .collect()
    };
    let gap = loop_polls[1] - loop_polls[0];
    assert!(
        gap >= Duration::from_secs(1),
        "the intake re-polled an unrecordable batch after {gap:?}; it must back off"
    );

    // Nor is the event confirmed: every poll the fake saw asked for an offset at
    // or below the offered event's own id (`test_update` carries `update_id` 1),
    // because an event the record does not hold must stay on offer.
    let offsets = fake.poll_offsets();
    assert!(
        offsets.iter().all(|&offset| offset <= 1),
        "the intake confirmed an event the record does not hold: poll offsets {offsets:?}"
    );

    assert_eq!(
        fake.count("setMessageReaction"),
        0,
        "an event must not be acknowledged before its fate is durable"
    );
    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "an event the record could not be written for must not be handed on"
    );
    listener.abort();
}

/// A comment and the slow forwarded attachment it belongs to reach the assistant
/// as one turn. Both arrive in one poll with the comment first, so the group it
/// opens would be released after its own quiet window while the attachment is
/// still being fetched: the collector holds it on the preparation instead, which
/// is what makes the pair one request — and holds it past the group's own ceiling,
/// since the fetch takes longer than that.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_comment_and_a_slow_forwarded_attachment_reach_the_assistant_as_one_turn() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    // The handshake's zero-timeout probe takes the empty batch, so the pair below
    // arrives on the loop's own poll, in one batch as the platform delivers it.
    fake.push_batch("token-a", Vec::new());
    fake.push_batch(
        "token-a",
        vec![
            test_text_update(1, "look at this"),
            forwarded_photo_update(2, 34, "photo-1", 1024),
        ],
    );
    fake.push_file("photo-1", b"jpeg-bytes", FETCH_DELAY);
    let (store, _dir) = test_store().await;
    let telegram = channel("token-a", &fake, &store);

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let msg = next_message_within(
        AT_MOST_ONE_TURN,
        &mut rx,
        "the comment and its forwarded photo, as one turn",
    )
    .await;
    assert_eq!(msg.parts.len(), 2, "the pair must be one turn, not two");
    assert_eq!(msg.parts[0].content, "look at this");
    assert!(
        msg.parts[1]
            .content
            .starts_with("[Forwarded from channel: Daily News] [IMAGE:"),
        "the forwarded photo must keep its attribution: {}",
        msg.parts[1].content
    );
    assert!(
        msg.parts[1].forwarded,
        "the fetched forward must come back as forwarded"
    );

    // The pair is not also answered as its own turn.
    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "the comment and its forwarded photo must be one turn, not two"
    );

    // The record is the discriminating part: with nothing holding the group, the
    // two parts would be a window apart; the window the release names would be the
    // quiet one, and the attachment a turn of its own. A fetch apart is only
    // possible because the group was held while the fetch ran.
    until_within(AT_MOST_ONE_TURN, "the release is recorded", || async {
        !burst_rows(&store).await.is_empty()
    })
    .await;
    let rows = burst_rows(&store).await;
    assert_eq!(rows.len(), 1, "one burst must be recorded for the one turn");
    assert_eq!(rows[0].user_name, "alice");
    assert_eq!(rows[0].parts, 2);
    assert_eq!(rows[0].released_by, "extended_window");
    assert_eq!(rows[0].part_times.len(), 2);
    let taken_at = |at: usize| {
        chrono::DateTime::parse_from_rfc3339(
            rows[0].part_times[at]["taken_at"]
                .as_str()
                .expect("taken_at is a string"),
        )
        .expect("taken_at is RFC 3339")
    };
    let gap = (taken_at(1) - taken_at(0))
        .to_std()
        .expect("the second part was taken after the first");
    assert!(
        gap >= FETCH_DELAY.saturating_sub(Duration::from_millis(200)),
        "the two parts must be a fetch apart, not a window apart: {gap:?}"
    );

    // Each part's platform time rides along from the update itself: the text
    // message carries none (`test_text_update`), the forwarded photo carries the
    // date of `forwarded_photo_update`.
    assert!(rows[0].part_times[0]["sent_at"].is_null());
    assert_eq!(
        rows[0].part_times[1]["sent_at"].as_str(),
        Some(
            chrono::DateTime::from_timestamp(1_700_000_000, 0)
                .expect("a valid timestamp")
                .to_rfc3339()
                .as_str()
        )
    );
    listener.abort();
}

/// The other arrival order: the slow forwarded attachment lands first and its
/// comment follows, and the pair still reaches the assistant as one turn.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_slow_forwarded_attachment_and_its_comment_reach_the_assistant_as_one_turn() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    // The handshake's zero-timeout probe takes the empty batch, so the photo and
    // its comment both arrive on the loop's own polls.
    fake.push_batch("token-a", Vec::new());
    fake.push_batch(
        "token-a",
        vec![forwarded_photo_update(1, 33, "photo-2", 1024)],
    );
    fake.push_batch("token-a", vec![test_text_update(2, "my comment")]);
    fake.push_file("photo-2", b"jpeg-bytes", FETCH_DELAY);
    let (store, _dir) = test_store().await;
    let telegram = channel("token-a", &fake, &store);

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let msg = next_message_within(
        AT_MOST_ONE_TURN,
        &mut rx,
        "the forwarded photo and its comment, as one turn",
    )
    .await;
    assert_eq!(msg.parts.len(), 2, "the pair must be one turn, not two");
    assert!(
        msg.parts[0].forwarded,
        "the fetched forward must come back as forwarded"
    );
    assert!(
        msg.parts[0]
            .content
            .starts_with("[Forwarded from channel: Daily News] [IMAGE:"),
        "the forwarded photo must keep its attribution: {}",
        msg.parts[0].content
    );
    assert_eq!(msg.parts[1].content, "my comment");

    until_within(AT_MOST_ONE_TURN, "the release is recorded", || async {
        !burst_rows(&store).await.is_empty()
    })
    .await;
    let rows = burst_rows(&store).await;
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].parts, 2);
    listener.abort();
}

/// The poll loop keeps polling while a batch is being parsed: the fetch of a
/// forwarded photo runs on the processor, so the next `getUpdates` must arrive
/// while that fetch is still in flight — the point of splitting the intake in two.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn the_poll_loop_keeps_polling_while_a_batch_is_parsed() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    // The handshake's zero-timeout probe takes the empty batch; the photo's batch
    // is the first the processor parses, and the third poll is what the loop must
    // reach while that fetch is still running.
    fake.push_batch("token-a", Vec::new());
    fake.push_batch(
        "token-a",
        vec![forwarded_photo_update(1, 33, "photo-1", 1024)],
    );
    fake.push_batch("token-a", vec![test_text_update(2, "still here")]);
    fake.push_file("photo-1", b"jpeg-bytes", FETCH_DELAY);
    let (store, _dir) = test_store().await;
    let telegram = channel("token-a", &fake, &store);

    let (tx, _rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    // A third poll within a window shorter than the fetch: the loop issued it
    // while the photo was still being fetched, not after the parse returned.
    until_within(
        Duration::from_millis(1500),
        "a third getUpdates arrives while the photo is still being fetched",
        || async { fake.poll_offsets().len() >= 3 },
    )
    .await;
    listener.abort();
}

/// Ordinary behaviour is unchanged: two plain messages in quick succession still
/// merge into one turn, and a message that arrives after the group is gone is a
/// turn of its own — nothing is left holding it.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn two_plain_messages_in_quick_succession_still_merge_and_a_later_one_is_its_own_turn() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    // The handshake consumes this batch; the loop's poll then waits for the
    // later one, which is why it is queued only once the first turn is observed:
    // queued up front it would be fetched straight away and merge into the group.
    fake.push_batch(
        "token-a",
        vec![test_text_update(1, "first"), test_text_update(2, "second")],
    );
    let (store, _dir) = test_store().await;
    let telegram = channel("token-a", &fake, &store);

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let msg = next_message_within(AT_MOST_ONE_TURN, &mut rx, "the two messages, as one turn").await;
    assert_eq!(msg.parts.len(), 2);
    assert_eq!(msg.parts[0].content, "first");
    assert_eq!(msg.parts[1].content, "second");

    fake.push_batch("token-a", vec![test_text_update(3, "later")]);
    let later = next_message(&mut rx, "the later message, as its own turn").await;
    assert!(
        later.parts.is_empty(),
        "a message after the group is gone must be a turn of its own"
    );
    assert_eq!(later.content, "later");

    // A group of one is recorded like any other release: the row about the
    // message that went on its own is what a complaint about a split pair is
    // read against.
    until("both releases are recorded", || async {
        burst_rows(&store).await.len() == 2
    })
    .await;
    let rows = burst_rows(&store).await;
    assert_eq!(rows[0].parts, 2, "the pair's own row comes first");
    assert_eq!(rows[1].parts, 1, "the lone message is recorded too");
    assert_eq!(rows[1].released_by, "quiet_window");
    listener.abort();
}

/// Control input still leaves the group open in its chat ahead of itself: a real
/// bot command in a burst is handed over after the group's own message, never in
/// front of it, and is a turn of its own.
#[tokio::test(flavor = "multi_thread")]
#[serial_test::serial(drain)] // serializes the process-global drain flag
async fn a_command_in_a_burst_leaves_the_group_ahead_of_itself() {
    let fake = FakeTelegram::start(&[("token-a", 700)]);
    // The handshake's zero-timeout probe takes the empty batch, which ends the
    // backlog and leaves the loop's own poll to fetch the burst — a command of
    // this run's own poll, so it is dispatched rather than refused as stale.
    fake.push_batch("token-a", Vec::new());
    fake.push_batch(
        "token-a",
        vec![
            test_text_update(1, "the group"),
            test_text_update(2, "/clear"),
        ],
    );
    let (store, _dir) = test_store().await;
    let telegram = channel("token-a", &fake, &store);

    let (tx, mut rx) = tokio::sync::mpsc::channel(8);
    let listener = tokio::spawn(async move {
        let _ = telegram.listen(tx).await;
    });

    let group = next_message(&mut rx, "the group ahead of the command").await;
    assert_eq!(group.content, "the group");
    let command = next_message(&mut rx, "the command, as its own turn").await;
    assert!(command.parts.is_empty());
    assert_eq!(command.content, "/clear");

    assert!(
        tokio::time::timeout(Duration::from_millis(300), rx.recv())
            .await
            .is_err(),
        "nothing may follow the group and the command"
    );

    // The release a command forces is recorded like any other — by the rule that
    // forced it, which is what a complaint about a split burst is read against.
    until("the group's release is recorded", || async {
        !burst_rows(&store).await.is_empty()
    })
    .await;
    let rows = burst_rows(&store).await;
    assert_eq!(rows.len(), 1, "only the group's own release is recorded");
    assert_eq!(rows[0].released_by, "control");
    assert_eq!(rows[0].parts, 1);
    listener.abort();
}