mahbot 0.10.1

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
//! 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::canonical_without_verbatim_prefix;
use std::collections::VecDeque;
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::atomic::{AtomicBool, Ordering};

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
}

/// The rendering of each refusal the failure note carries, for the assertions below.
fn refusal_lines(refused: &[crate::RefusedMarker]) -> Vec<String> {
    refused
        .iter()
        .map(|refusal| format!("{}: {}", refusal.marker, refusal.reason))
        .collect()
}

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_markdown_to_telegram_html() {
    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>"
    );
    let r = markdown_to_telegram_html("say \"hi\" & <tag> 'ok'");
    assert_eq!(r, "say &quot;hi&quot; &amp; &lt;tag&gt; &#39;ok&#39;");
    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"));

    let r = markdown_to_telegram_html("```\nsome **bold** and `code`\n```");
    assert_eq!(r, "<pre><code>some **bold** and `code`</code></pre>");

    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>"
    );

    let r = markdown_to_telegram_html("```\nuse &lt;/code&gt;\n```");
    assert_eq!(r, "<pre><code>use &amp;lt;/code&amp;gt;</code></pre>");

    let r = markdown_to_telegram_html("<blockquote>");
    assert_eq!(r, "<blockquote>");

    let r = markdown_to_telegram_html("</blockquote>");
    assert_eq!(r, "</blockquote>");

    let r = markdown_to_telegram_html("<blockquote>\nHello **world**\n</blockquote>");
    assert_eq!(r, "<blockquote>\nHello <b>world</b>\n</blockquote>");

    let r = markdown_to_telegram_html("<blockquote123>");
    assert_eq!(r, "&lt;blockquote123&gt;");

    let r = markdown_to_telegram_html("<blockquote class=\"x\">");
    assert_eq!(r, "&lt;blockquote class=&quot;x&quot;&gt;");

    let r = markdown_to_telegram_html("<blockquote >");
    assert_eq!(r, "&lt;blockquote &gt;");

    let r = markdown_to_telegram_html("## [docs](https://example.com)");
    assert_eq!(r, "<a href=\"https://example.com\">docs</a>");
}

/// The `/board` listing converts the joined text line by line: a hostile title must never unbalance
/// another line's HTML, which would make Telegram reject it all.
#[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}"
        );
    }
}

#[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>",
        },
        Case {
            name: "unmatched double asterisk",
            input: "hello ** world",
            expected: "hello ** world",
        },
        Case {
            name: "unmatched single asterisk",
            input: "hello * world",
            expected: "hello * world",
        },
        Case {
            name: "triple asterisk",
            input: "***",
            expected: "***",
        },
        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",
        },
        Case {
            name: "unmatched tilde",
            input: "hello ~ world",
            expected: "hello ~ world",
        },
        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",
        },
        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)",
        },
        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);
    }
}

/// 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_attach_known_targets_and_refuse_the_rest() {
    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()];

    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());

    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());

    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");

    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!(
        refusal_lines(&refusals),
        vec!["[IMAGE:https://example.com:bad/x.png]: it is not a valid address."]
    );

    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());

    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!(
        refusal_lines(&refusals),
        vec![format!("[IMAGE:{}]: it is not an image.", nonimg.display())]
    );
}

/// A relative media target resolves against the first root that has the file, and is attached from
/// that resolved absolute path — exactly what `[FILE:...]` already does.
#[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 marker whose only defect is a mistyped directory is delivered from the file it meant, a
/// mistyped file name is refused rather than guessed at, and a file the message would deliver more
/// than once goes out once.
#[test]
fn a_mistyped_directory_is_delivered_and_a_mistyped_name_is_refused() {
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    let generated = root.join("userspaces").join("lyuba").join("generated");
    std::fs::create_dir_all(&generated).unwrap();
    let png = generated.join("shot.png");
    write_test_png(&png);
    let roots = vec![root.to_path_buf()];
    let typo = root
        .join("userseas")
        .join("lyuba")
        .join("generated")
        .join("shot.png");

    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!("Look [IMAGE:{}]", typo.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "Look");
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1);
    assert_eq!(att[0].target, png.to_string_lossy());

    // `[FILE:]` is workspace-confined: the repair is judged by the roots like any other path, so
    // it never makes an outside file deliverable.
    let (_, att, refusals) = parse_attachment_markers(
        &format!("[FILE:{}]", typo.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1);
    assert_eq!(
        att[0].target,
        canonical_without_verbatim_prefix(&png).to_string_lossy()
    );
    let elsewhere = tempfile::tempdir().unwrap();
    let (_, att, refusals) = parse_attachment_markers(
        &format!("[FILE:{}]", typo.display()),
        &[elsewhere.path().to_path_buf()],
        TransferLimits::CLOUD,
    );
    assert!(att.is_empty());
    assert_eq!(
        refusal_lines(&refusals),
        vec![format!(
            "[FILE:{}]: it is outside your workspace.",
            typo.display()
        )]
    );

    // The repaired marker and the correct one name one file, whichever kind each is written as.
    let (_, att, refusals) = parse_attachment_markers(
        &format!(
            "[IMAGE:{}] [FILE:{}] [IMAGE:{}]",
            typo.display(),
            typo.display(),
            png.display()
        ),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1, "one file, one attachment");

    // A mistyped file name is a different file: similar names are the norm here, so nothing is
    // guessed and the refusal stands.
    let name_typo = generated.join("shot.pgn");
    let (_, att, refusals) = parse_attachment_markers(
        &format!("[IMAGE:{}]", name_typo.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(att.is_empty());
    assert_eq!(
        refusal_lines(&refusals),
        vec![format!("[IMAGE:{}]: file not found.", name_typo.display())]
    );
}

/// One delivered file leaves one repair record, however many kinds or spellings named it, and a
/// repaired marker the kind's gate refuses leaves none: the row counts deliveries, not markers.
#[tokio::test]
#[serial_test::serial(provider)] // the telemetry store seam is process-global
async fn a_delivered_file_leaves_one_repair_record() {
    let (store, _store_tmp) = crate::open_test_store!(crate::logs::LogStore, "log");
    let _seam = crate::util::test::install_test_log_store(store.clone());
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path();
    let generated = root.join("userspaces").join("lyuba").join("generated");
    std::fs::create_dir_all(&generated).unwrap();
    let png = generated.join("shot.png");
    write_test_png(&png);
    std::fs::write(generated.join("clip.mp4"), b"fake-mp4").unwrap();
    let roots = vec![root.to_path_buf()];
    let typo = root
        .join("userseas")
        .join("lyuba")
        .join("generated")
        .join("shot.png");
    let other_typo = root
        .join("userspacse")
        .join("lyuba")
        .join("generated")
        .join("shot.png");

    // Two kinds, two spellings, one file: one attachment and one repair record.
    let (_, att, refusals) = parse_attachment_markers(
        &format!(
            "[IMAGE:{}] [FILE:{}] [IMAGE:{}]",
            typo.display(),
            typo.display(),
            other_typo.display()
        ),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1, "one file, one attachment");
    crate::telemetry::drain_for_test().await;
    let repair_rows = marker_row_workspaces(&store, MARKER_REPAIRED_MESSAGE, &typo).await;
    assert_eq!(repair_rows.len(), 1, "one file, one repair record");
    assert_eq!(
        repair_rows[0],
        root.file_name().unwrap().to_string_lossy().into_owned(),
        "the row belongs to the workspace holding the file"
    );

    // A correct marker first and a repaired twin after still names one delivered file: one row.
    let (_, att, refusals) = parse_attachment_markers(
        &format!("[IMAGE:{}] [IMAGE:{}]", png.display(), other_typo.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1);
    crate::telemetry::drain_for_test().await;
    assert_eq!(
        marker_row_workspaces(&store, MARKER_REPAIRED_MESSAGE, &other_typo)
            .await
            .len(),
        1,
        "a repaired twin of a delivered file is still one repair"
    );

    // Repaired, then refused by the kind's gate: nothing was delivered, so no repair row.
    let clip_typo = root
        .join("userseas")
        .join("lyuba")
        .join("generated")
        .join("clip.mp4");
    let (_, att, refusals) = parse_attachment_markers(
        &format!("[IMAGE:{}]", clip_typo.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(att.is_empty());
    assert_eq!(refusals.len(), 1);
    crate::telemetry::drain_for_test().await;
    assert!(
        marker_row_workspaces(&store, MARKER_REPAIRED_MESSAGE, &clip_typo)
            .await
            .is_empty(),
        "a repaired marker the gate refused is not a repair"
    );
}

/// A marker record for a path under no workspace carries no workspace at all, rather than an
/// unrelated one: the per-workspace counts stay honest. A repair outside every root is the same.
#[tokio::test]
#[serial_test::serial(provider)] // the telemetry store seam is process-global
async fn a_marker_record_outside_every_workspace_carries_no_workspace() {
    let (store, _store_tmp) = crate::open_test_store!(crate::logs::LogStore, "log");
    let _seam = crate::util::test::install_test_log_store(store.clone());
    let workspace = tempfile::tempdir().unwrap();
    let outside = tempfile::tempdir().unwrap();
    let roots = vec![workspace.path().to_path_buf()];

    let missing = outside.path().join("gone.png");
    let (_, att, refusals) = parse_attachment_markers(
        &format!("[IMAGE:{}]", missing.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(att.is_empty());
    assert_eq!(refusals.len(), 1);

    let pictures = outside.path().join("pictures");
    std::fs::create_dir(&pictures).unwrap();
    write_test_png(&pictures.join("shot.png"));
    let typo = outside.path().join("picturrs").join("shot.png");
    let (_, att, refusals) = parse_attachment_markers(
        &format!("[IMAGE:{}]", typo.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(refusals.is_empty());
    assert_eq!(att.len(), 1);

    crate::telemetry::drain_for_test().await;
    assert_eq!(
        marker_row_workspaces(&store, MARKER_REFUSED_MESSAGE, &missing).await,
        vec![String::new()],
        "a refusal under no workspace names none"
    );
    assert_eq!(
        marker_row_workspaces(&store, MARKER_REPAIRED_MESSAGE, &typo).await,
        vec![String::new()],
        "a repair under no workspace names none"
    );

    // A `..` cannot smuggle an outside path into a workspace's count.
    let escaped = workspace
        .path()
        .join("..")
        .join(outside.path().file_name().unwrap())
        .join("gone.png");
    let (_, att, refusals) = parse_attachment_markers(
        &format!("[IMAGE:{}]", escaped.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert!(att.is_empty());
    assert_eq!(refusals.len(), 1);
    crate::telemetry::drain_for_test().await;
    assert_eq!(
        marker_row_workspaces(&store, MARKER_REFUSED_MESSAGE, &escaped).await,
        vec![String::new()],
        "a `..` does not make an outside path belong to the workspace it is spelled under"
    );
}

/// The workspaces of the marker rows whose message is `message` and whose fields name `path`.
/// Filtered by the test-unique path: the telemetry seam is process-global, so a concurrent test
/// recording a marker of its own must not be counted here.
async fn marker_row_workspaces(
    store: &crate::logs::LogStore,
    message: &str,
    path: &std::path::Path,
) -> Vec<String> {
    let needle = path.to_string_lossy();
    let rows = store
        .conn
        .query(
            "SELECT workspace, fields FROM logs WHERE message = ?1",
            crate::db::params![message],
        )
        .await
        .expect("read the marker rows");
    rows.iter()
        .filter_map(|row| {
            let fields = row.get::<String>(1).ok()?;
            fields
                .contains(needle.as_ref())
                .then(|| row.get::<String>(0).unwrap_or_default())
        })
        .collect()
}

/// One refused marker does not stop the others in the same message: every valid target is attached
/// and only the failing one is reported, 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!(
        refusal_lines(&refusals),
        vec!["[IMAGE:missing/a.png]: file not found."]
    );
}

/// `[FILE:...]` is local-only: it resolves against the authorizing workspace roots, carries the
/// CANONICAL path, and reports every refusal with the marker as written.
#[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();

    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());

    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 no earlier root holds resolves against the first root that does: relayed
    // Manager→Assistant content names a file in the originating workspace.
    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()
    );

    let (cleaned, att, refusals) = parse_attachment_markers(
        &format!("[FILE:{}]", escape.display()),
        &roots,
        TransferLimits::CLOUD,
    );
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    let outside_marker = format!("[FILE:{}]: it is outside your workspace.", escape.display());
    assert_eq!(refusal_lines(&refusals), vec![outside_marker]);

    let (cleaned, att, refusals) =
        parse_attachment_markers("[FILE:missing.pdf]", &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusal_lines(&refusals),
        vec!["[FILE:missing.pdf]: file not found."]
    );

    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!(
        refusal_lines(&refusals),
        vec![format!("[FILE:{}]: it is a directory.", sub.display())]
    );

    let (cleaned, att, refusals) =
        parse_attachment_markers("[FILE:http://x/y.pdf]", &roots, TransferLimits::CLOUD);
    assert_eq!(cleaned, "");
    assert!(att.is_empty());
    assert_eq!(
        refusal_lines(&refusals),
        vec!["[FILE:http://x/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());
    let unknown = format!(
        "[FILE:{}]: the message's workspace is unknown.",
        pdf.display()
    );
    assert_eq!(refusal_lines(&refusals), vec![unknown]);

    // An inline payload is not a file to hand over, and the marker never echoes it back.
    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!(
        refusal_lines(&refusals),
        vec!["[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.
#[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!(
        refusal_lines(&refusals),
        vec![format!(
            "[FILE:{}]: it is outside your workspace.",
            link.display()
        )]
    );
}

/// Only the delivery route collapses for a video, not where it comes from: a remote one is fetched
/// and uploaded — never handed over by address, whose document form takes a couple of types — while
/// images and audio keep it, and files upload 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 last segment when it is a
/// video name, else that stem with the declared type's extension.
#[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");
}

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 puts on the wire for a video: a document upload carrying the file's own bytes,
/// content-type detection off and no geometry claim — the recipient's own player, not the message,
/// describes the clip. A URL-addressed video is the same after the fetch: one upload of the fetched
/// bytes, never a video message, never a handover by address.
#[tokio::test]
async fn a_video_reaches_the_chat_as_one_document_upload() {
    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");

    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);
}

/// 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 however it is reached, 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 inbound/outbound bound.
#[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);
    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);
    assert_eq!(
        fake.sent_texts(),
        vec!["Could not send &quot;clip.mp4&quot;: it could not be fetched or sent."]
    );
}

/// A marker naming a missing file leaves nothing in the delivered body: the conversation that
/// wrote it is told instead, so the owner gets no broken attachment, and the failure is reported
/// back for that conversation. A message that is nothing but the failed marker sends nothing at
/// all.
#[tokio::test]
async fn a_refused_marker_leaves_no_notice_in_the_delivered_message() {
    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 roots = vec![dir.path().to_path_buf()];

    let outcome = telegram
        .send(&SendMessage {
            content: "Here it is [FILE:missing.pdf]".to_string(),
            recipient: "42".to_string(),
            reply_markup: None,
            file_roots: roots.clone(),
        })
        .await
        .expect("the surrounding text is delivered");

    assert_eq!(
        refusal_lines(&outcome.refused),
        vec!["[FILE:missing.pdf]: file not found."]
    );
    assert_eq!(fake.sent_texts(), vec!["Here it is"]);

    let outcome = telegram
        .send(&SendMessage {
            content: "[FILE:missing.pdf]".to_string(),
            recipient: "42".to_string(),
            reply_markup: None,
            file_roots: roots,
        })
        .await
        .expect("nothing to deliver is not an error");

    assert_eq!(outcome.refused.len(), 1);
    assert_eq!(
        fake.sent_texts(),
        vec!["Here it is"],
        "a message that is only the failed marker sends nothing"
    );
}

/// An attachment that fails after a refused marker still returns the report: the text went out,
/// so the failure says the owner received the reply and only the attachment was lost.
#[tokio::test]
async fn a_failed_attachment_still_carries_the_refused_marker_report() {
    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 roots = vec![dir.path().to_path_buf()];

    let failure = telegram
        .send(&SendMessage {
            content: "Here it is [FILE:missing.pdf] [VIDEO:http://127.0.0.1:1/clip.mp4]"
                .to_string(),
            recipient: "42".to_string(),
            reply_markup: None,
            file_roots: roots,
        })
        .await
        .expect_err("the attachment failure is reported");

    assert_eq!(
        refusal_lines(&failure.refused),
        vec!["[FILE:missing.pdf]: file not found."]
    );
    assert!(
        failure.delivered,
        "the text went out before the attachment failed"
    );
    assert!(
        fake.sent_texts().iter().any(|text| text == "Here it is"),
        "the chat received the body text"
    );
}

/// 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.
#[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 hand-over exactly at the declared size is the whole file as soon as it is measured; one that
/// stops changing short of it is a stall, so the wait keeps waiting until the write reaches the
/// declared size. With none declared, a size that settles completes it.
#[tokio::test]
async fn a_hand_over_completes_at_the_declared_size_and_survives_a_stall() {
    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");

    let stalled = dir.path().join("stalled.bin");
    std::fs::write(&stalled, vec![7u8; 3]).unwrap();
    let (waited, ()) = tokio::join!(wait_for_hand_over(&stalled, Some(9)), async {
        tokio::time::sleep(Duration::from_millis(300)).await;
        std::fs::write(&stalled, vec![7u8; 9]).unwrap();
    });
    waited.expect("a stalled write is not the end of the transfer");
}

/// Each local hand-over failure reaches the owner as what it is — a file from outside the directory
/// the server was given, one that never arrived, one that stopped short — 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, TransferLimits::LOCAL),
        LOCAL_FILE_OUTSIDE_REASON
    );

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

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

    // Only a failure nothing recognises is still the transport's own refusal, and it hints at the
    // bot-download cap only in the mode that has one.
    let unknown = anyhow::anyhow!("the staging disk went away");
    assert_eq!(
        download_failure_reason(&unknown, TransferLimits::CLOUD),
        "Telegram refused the transfer (it may exceed what the bot can download)"
    );
    assert_eq!(
        download_failure_reason(&unknown, TransferLimits::LOCAL),
        "Telegram refused the transfer"
    );
}

/// The declared-size gate's boundaries: exactly at the cloud limit passes, one byte over trips it,
/// and the local mode — 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);
}

#[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");
}

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));
}

/// What a callback query narrows on: absent/null data or message, and a sender nothing authorizes,
/// are refused — while an empty-string data stays valid with empty content, and a null
/// callback_query_id stays valid as `None`.
#[tokio::test]
async fn parse_callback_query_narrows_only_on_absent_inputs() {
    let ch = test_channel().await;
    let unknown_user = serde_json::json!({ "id": 999, "username": "unknown_user" });
    let rejected = [
        ("no data", [("data", serde_json::Value::Null)]),
        ("no message", [("message", serde_json::Value::Null)]),
        ("unknown user", [("from", unknown_user)]),
    ];
    for (name, overrides) in &rejected {
        let cq = test_callback_query(overrides);
        assert!(
            ch.parse_callback_query(&cq)
                .await
                .expect("parse the query")
                .is_none(),
            "case {name}: expected rejection"
        );
    }

    // Regression pair: the `?` guard rejects absent/null data but not an empty string (empty
    // content, distinct from `process_updates`' `unwrap_or("")` ACK check), and a null/absent
    // `callback_query_id` stays valid as `None` — unlike missing data.
    let accepted = [
        (
            "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 &accepted {
        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}");
    }
}

/// The chunker never hands Telegram more than it accepts: the limit does not split, one character
/// past it does, the join rebuilds the message, a newline is preferred over a mid-word cut, and a
/// code block or emoji at the boundary stays within the limit.
#[test]
fn splitting_respects_the_telegram_limit() {
    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);

    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);
    }
    let msg = format!("{}🎉🎊", "a".repeat(4094));
    for p in &split_message_for_telegram(&msg) {
        assert!(p.chars().count() <= TELEGRAM_MAX_MESSAGE_LENGTH);
    }

    // Regression: when the only newline is in the first half of the search window and no spaces
    // exist, a hard split would land mid-word; the newline fallback (tier 3) stops it.
    let msg = format!("{}\n{}", "a".repeat(1000), "x".repeat(5000));
    let chunks = split_message_for_telegram(&msg);
    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,
        );
    }
    assert_eq!(chunks.join(""), msg);
    assert!(
        chunks[0].ends_with('\n'),
        "first chunk should end with newline, got: {:?}",
        chunks[0].chars().rev().take(10).collect::<String>()
    );

    // Wrapping: `send_text_chunks` prepends a continuation prefix (29 chars mid-message, 13 on the
    // last), so every raw chunk must leave that room — three chunks cover it.
    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()
        );
    }

    // The boundary itself is not split: a 4066-char message leaves the room its continuation prefix
    // needs, and a 4096-char one is sent without any prefix at all.
    for (size, wrapped) in [(4066, true), (4096, false)] {
        let boundary = "b".repeat(size);
        let chunks = split_message_for_telegram(&boundary);
        assert_eq!(chunks.len(), 1, "{size}-char message should not split");
        if wrapped {
            let wrapped = format!("(continued)\n\n{}", chunks[0]);
            assert!(
                wrapped.chars().count() <= 4096,
                "{size}-char boundary wrapped: {} > 4096",
                wrapped.chars().count()
            );
        }
    }

    // Regression: a split point inside a long HTML tag whose '>' fell beyond the window could push
    // a chunk past the sendable limit, and Telegram rejected the send (HTML and the plain-text
    // retry both fail), dropping the message. The clamp must target the byte offset of the
    // 4066-char boundary, never a raw byte count: clamping to byte 4066 panics on 3-byte CJK and
    // under-fills 2-byte Cyrillic.
    let tag = format!(
        "<a href=\"https://example.com/{}\">link</a>",
        "y".repeat(120)
    );
    for (name, msg) in [
        ("ascii", format!("{}{}", "x".repeat(4000), tag)),
        ("cjk", format!("{}{}", "界".repeat(4000), tag)),
        ("cyrillic", format!("{}{}", "ы".repeat(4000), tag)),
    ] {
        let chunks = split_message_for_telegram(&msg);
        assert_eq!(chunks.join(""), msg, "{name}: reconstruction");
        assert!(chunks.len() >= 2, "{name}: expected a split");
        for (i, chunk) in chunks.iter().enumerate() {
            assert!(
                chunk.chars().count()
                    <= TELEGRAM_MAX_MESSAGE_LENGTH - TELEGRAM_CONTINUATION_OVERHEAD,
                "{name}: chunk {i} raw {} chars exceeds the 4066 budget",
                chunk.chars().count(),
            );
            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(),
            );
        }
        assert!(
            chunks[0].chars().count() >= 4000,
            "{name}: first chunk only {} chars — budget underused",
            chunks[0].chars().count(),
        );
    }
}

#[tokio::test]
async fn usernameless_sender_stays_unauthorized_despite_legacy_binding() {
    let _ch = test_channel().await;
    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 [
        serde_json::json!({"from": {"id": 1}}),
        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}"
        );
    }
    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())),
    );
    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 first (the scan follows the `(channel, identifier)` unique
    // index, and `!` sorts before any digit), so returning the first matching row of either kind
    // would return the nickname's account: the number must win on identity.
    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");

    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 can refuse.
    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` naming 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.
    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` is the composition around the shared normalization (asserted in
/// `reply_tests`): it names the replied-to author, falls back to the caption when a media message's
/// `text` is empty, and reports nothing when the message is not a reply.
#[test]
fn build_reply_reference_reads_text_or_caption_and_needs_a_reply() {
    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.author, "@bob");
    assert_eq!(reply.snippet, "at the beach");

    // No reply: absent and a null reply_to_message (not an object) are nothing to report.
    for message in [
        serde_json::json!({ "text": "no reply here" }),
        serde_json::json!({ "text": "hi", "reply_to_message": null }),
    ] {
        assert!(build_reply_reference(&message).is_none(), "{message}");
    }
}

#[test]
fn replied_to_sender_label_chain() {
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "username": "alice" } })),
        "@alice"
    );
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "id": 1, "first_name": "Bob" } })),
        "Bob"
    );
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "id": 1 } })),
        "user"
    );
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "sender_chat": { "title": "Daily News" } })),
        "Daily News"
    );
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "sender_chat": { "id": 1 } })),
        "channel"
    );
    assert_eq!(
        replied_to_sender_label(
            &serde_json::json!({ "from": { "is_bot": true, "username": "poll_bot" } })
        ),
        "@poll_bot"
    );
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "is_bot": true } })),
        "bot"
    );
    assert_eq!(
        replied_to_sender_label(&serde_json::json!({ "from": { "first_name": "<Bob>" } })),
        "Bob"
    );
    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");
    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 carry the reference too (the no-silent-drop path).
    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");
    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.
    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");
}

#[test]
#[expect(clippy::too_many_lines)]
fn attachment_content_format_rules() {
    let c = format_attachment_content(
        IncomingAttachmentKind::Photo,
        std::path::Path::new("/tmp/workspace/photo.jpg"),
        None,
    );
    assert_eq!(c, "[IMAGE:/tmp/workspace/photo.jpg]");
    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:"));
    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()));
    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:"));
    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 not routed)
    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:]");
    }
    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), admitting both the
    // canonical image/jpeg and the legacy image/jpg alias.
    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) and 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 and a recorded voice message both arrive as an [AUDIO:] marker, which the
    // enrichment pass transcribes — the marker follows the kind, not the platform.
    let c = format_attachment_content(
        IncomingAttachmentKind::Audio,
        std::path::Path::new("/tmp/workspace/voice.ogg"),
        None,
    );
    assert_eq!(c, "[AUDIO:/tmp/workspace/voice.ogg]");
    let c = format_attachment_content(
        IncomingAttachmentKind::Voice,
        std::path::Path::new("/tmp/workspace/note.oga"),
        None,
    );
    assert_eq!(c, "[AUDIO:/tmp/workspace/note.oga]");
    let c = format_attachment_content(
        IncomingAttachmentKind::Audio,
        std::path::Path::new("/tmp/workspace/song.mp3"),
        Some("audio/mpeg"),
    );
    assert_eq!(c, "[AUDIO:/tmp/workspace/song.mp3]");
    let c = format_attachment_content(
        IncomingAttachmentKind::Video,
        std::path::Path::new("/tmp/workspace/clip.mp4"),
        None,
    );
    assert_eq!(c, "[VIDEO:/tmp/workspace/clip.mp4]");
    let c = format_attachment_content(
        IncomingAttachmentKind::Document,
        std::path::Path::new("/tmp/workspace/clip.mp4"),
        None,
    );
    assert_eq!(c, "[VIDEO:/tmp/workspace/clip.mp4]");
    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]");
    let c = format_attachment_content(
        IncomingAttachmentKind::Video,
        std::path::Path::new("/tmp/workspace/notes.txt"),
        None,
    );
    assert_eq!(c, "[FILE:/tmp/workspace/notes.txt]");
}

#[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"
    );
    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"
    );
    assert_eq!(
        sanitize_attachment_filename("C:\\evil\\x.txt", fallback),
        "C:_evil_x.txt"
    );
    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"
    );
    assert_eq!(
        sanitize_attachment_filename(".bashrc", fallback),
        "_.bashrc"
    );
    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; the `_` counts toward it.
    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 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,
        urgent: false,
    };
    assert_eq!(local_attachment_name(&attachment, "123", 7, None), "passwd");
    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:?}"
        );
    }
    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,
        urgent: false,
    };
    // Telegram reports no file_name for many kinds, so the fallback extension comes from `getFile`
    // — remote input that must not survive into the `[FILE:<path>]` marker path.
    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");
    let name = local_attachment_name(&attachment, "123", 7, Some("]/漢\u{7}"));
    assert_eq!(name, "file_123_7.bin");
    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}");
}

/// 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 }),
    )])
}

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 }),
        )]),
    )])
}

/// The batch placement plan: an album is parsed whole at its first member and its later members are
/// skipped there — a plain message between them stays where it is — and a button press is never
/// placed inside an album. A press carries the message its button sits on, so joining the album
/// would hand the photo over twice or lose the album's other members.
#[test]
fn poll_plan_places_each_album_and_keeps_a_button_press_out_of_it() {
    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]),
        ]
    );

    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")
    };

    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"
    );

    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 be taken up a second time by a later run.
    assert_eq!(album.update_ids, vec![7, 8]);
}

#[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 previous build's keyboard and 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());

    assert!(!is_control_message(&test_msg(
        "alice", "/board", "gui", "chat"
    )));
    assert!(!is_control_message(&test_msg(
        "alice", "hello", "telegram", "chat"
    )));
    assert!(!is_control_message(&test_msg(
        "alice",
        "/etc/hosts",
        "telegram",
        "chat"
    )));

    // A message that opened with the urgent mark is never a command: the mark was consumed, so its
    // content reads as one, but it must reach the agent.
    let mut urgent_command = test_msg("alice", "/clear", "telegram", "chat");
    urgent_command.urgent = true;
    assert!(!is_control_message(&urgent_command));
    assert!(control_input(&urgent_command).is_none());
}

#[tokio::test]
async fn a_leading_mark_is_consumed_from_a_typed_message() {
    let ch = test_channel().await;
    let update = test_update(&[(
        "message",
        serde_json::json!({
            "message_id": 44,
            "text": "  !/clear",
            "from": { "id": 555, "username": "alice" },
            "chat": { "id": -100_200_300 }
        }),
    )]);

    let msg = parsed_message(&ch, &update).await;

    assert_eq!(msg.content, "/clear");
    assert!(msg.urgent);
    assert!(control_input(&msg).is_none());
    assert!(!is_control_message(&msg));
}

/// Only the start of the typed text counts; a mark anywhere else is ordinary text and leaves the
/// message and its urgency as written.
#[tokio::test]
async fn a_mark_is_read_only_at_the_start_of_the_typed_text() {
    let ch = test_channel().await;
    for (typed, content, urgent) in [
        ("!hello", "hello", true),
        ("hello!", "hello!", false),
        ("what? !", "what? !", false),
    ] {
        let update = test_update(&[(
            "message",
            serde_json::json!({
                "message_id": 45,
                "text": typed,
                "from": { "id": 555, "username": "alice" },
                "chat": { "id": -100_200_300 }
            }),
        )]);
        let msg = parsed_message(&ch, &update).await;
        assert_eq!(msg.content, content, "{typed}");
        assert_eq!(msg.urgent, urgent, "{typed}");
    }
}

/// A caption is text the user typed, so it carries the mark like any message does; the album its
/// parts fold into keeps it.
#[test]
fn a_caption_carries_the_mark_and_the_album_keeps_it() {
    let attachment = |caption: &str| {
        TelegramChannel::build_attachment(
            &serde_json::json!({ "file_id": "x" }),
            &serde_json::json!({ "caption": caption }),
            IncomingAttachmentKind::Photo,
        )
        .expect("a photo attachment")
    };

    let marked = attachment("!\tsee this");
    assert_eq!(marked.caption.as_deref(), Some("see this"));
    assert!(marked.urgent);

    let plain = attachment("see this");
    assert_eq!(plain.caption.as_deref(), Some("see this"));
    assert!(!plain.urgent);

    let album = merge_album_member(
        Some(ChannelMessage {
            content: "a".into(),
            ..ChannelMessage::default()
        }),
        ChannelMessage {
            content: "b".into(),
            urgent: true,
            ..ChannelMessage::default()
        },
    );
    assert_eq!(album.content, "a\nb");
    assert!(album.urgent);

    let album = merge_album_member(
        Some(ChannelMessage {
            content: "a".into(),
            ..ChannelMessage::default()
        }),
        ChannelMessage {
            content: "b".into(),
            ..ChannelMessage::default()
        },
    );
    assert_eq!(album.content, "a\nb");
    assert!(!album.urgent);
}

async fn parsed_message(ch: &TelegramChannel, update: &serde_json::Value) -> ChannelMessage {
    ch.parse_update_message(update)
        .await
        .expect("parse the update")
        .expect("a message")
}

/// Every forward attribution: each labelled `forward_origin` renders it, and an origin with no
/// label still counts as forwarded — the fact the burst collector's longer window rests on, the
/// prefix only its rendering. An update with both the modern origin and the legacy fields yields
/// one attribution; an unlabelled origin falls back to the legacy fields, which alone still count.
#[tokio::test]
#[expect(clippy::too_many_lines)] // one cohesive table of forward cases
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] ")
    );

    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"
    );

    // They still attribute a message — and still count it as forwarded, the flag the burst
    // collector's longer window rests on, which the rendered prefix alone would not pin.
    let legacy = [
        (
            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",
        ),
        (
            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",
        ),
        (
            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",
        ),
        (
            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"
        );
    }

    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);

    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]");
}

/// 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;
}

/// 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");
    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=\"ö>ü"), "ä ");
}

// `mirror_gui_message_to_telegram` returns early for each guard condition, and otherwise sends a
// blockquote-format message to the user's bindings; serialized on 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: they share the global [`CHANNEL_REGISTRY`] and store
/// singletons. A `tokio::sync::Mutex` so holding it across await points blocks no worker thread.
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
}

/// A spy Telegram channel in the registry, and the buffer it records sends into. Idempotent.
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
    })
}

/// A test user with a Telegram binding and reply_target. Idempotent; `ctx` labels panics.
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}"));
}

/// Preamble shared by all mirror tests: take the lock, initialise stores, set up the spy. Returns
/// the spy buffer and the lock guard (kept alive for the test's 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(),
        display_content: None,
        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(),
        dictated: false,
        urgent: false,
    }
}

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", "")
}

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()
}

/// 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 `setup` implies (reply_target when
/// bound, user name otherwise). Each call takes the 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() {
    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;
    assert_mirror_skips(
        MirrorSkipSetup::Unbound("no_binding"),
        &gui_msg("no_binding", "hello"),
        "user with no bindings should not send",
    )
    .await;
    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).
    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;
}

#[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 (mic-button recording / wake word) must mirror exactly like GUI
    // text: same 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 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");
    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)");
    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"));
}

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: only the four known kinds go.
    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;
    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]
#[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: it initialises the shared user store.
    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. The SourceTree test env's
    // availability cache seeds `available = true`, so `/update` is present.
    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"));
    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");
    }
    assert!(cmds.contains(&"image_models"));
    assert!(cmds.contains(&"video_models"));
    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.
    {
        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, the Assistant being 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");
}

// The intake's 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 on a local endpoint
// (`TelegramChannel::for_test`), scripted `getUpdates` batches, and a store of this check's own.
//
// Every check that starts a listener is serialized on the process-global drain flag: the listener
// 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 is durable — a record row, a logged request, a held message — so a longer bound only
/// delays failure reporting, while a short one fails a stalled machine.
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, 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 reaching the assistant as one turn can only have been held across the fetch.
const FETCH_DELAY: Duration = Duration::from_secs(2);

const AT_MOST_ONE_TURN: Duration = Duration::from_secs(20);

struct FakeTelegram {
    base: String,
    state: Arc<FakeState>,
    stop: Arc<AtomicBool>,
    accept: Option<std::thread::JoinHandle<()>>,
}

struct FakeState {
    accounts: HashMap<String, i64>,
    batches: Mutex<HashMap<String, VecDeque<Vec<serde_json::Value>>>>,
    requests: Mutex<Vec<ServedRequest>>,
    sent_texts: Mutex<Vec<String>>,
    files: Mutex<HashMap<String, FakeFile>>,
}

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 a slow-fetch check must reproduce.
struct FakeFile {
    bytes: Vec<u8>,
    delay: Duration,
}

struct FakeRequest {
    path: String,
    body: Vec<u8>,
}

struct FakeHead {
    path: String,
    length: usize,
    buffer: Vec<u8>,
    head_end: usize,
}

impl FakeTelegram {
    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 polling handler would then read nothing (or a
                            // half-arrived request) and close the connection before answering. A
                            // handler blocks, so make the stream block.
                            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 gone before it was accepted,
                        // `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),
        }
    }

    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,
            },
        );
    }

    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()
    }

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

    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()
    }

    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();
        }
    }
}

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))
}

fn file_path_of(file_id: &str) -> String {
    format!("photos/{file_id}.jpg")
}

fn not_found() -> serde_json::Value {
    serde_json::json!({ "ok": false, "error_code": 404, "description": "Not Found" })
}

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 splits before the method.
    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 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: the log is the durable evidence.
    state
        .requests
        .lock()
        .expect("fake request log")
        .push(ServedRequest {
            method: method.clone(),
            offset,
            at: std::time::Instant::now(),
            body: request.body.clone(),
        });

    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: `{base}/file/bot<token>/<path>`, body is bytes.
    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, 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()),
    }
}

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. Answered and
/// closed like every other connection, so a slow transfer holds only its 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 — as the real long poll does — the batch queued while the client waits;
/// it 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 }));
    }
    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 observing an earlier one. The socket waits non-blocking so the queue can be
    // re-read between reads; `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,
        }
    }
}

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)
}

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 },
        }),
    );
}

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,
    })
}

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 })
}

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 fixed sleep 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 reached only 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 released burst crosses 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 released
/// burst crosses the collector's windows first, so its message is a second (or a fetch) away.
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}"))
}

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 },
            }),
        ),
    ])
}

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 attachment is what 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. The store comes
/// back because a restarting check 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, must claim under another 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, as
/// for a product listener, so a second listener over one store is a restart's other run.
fn channel(token: &str, fake: &FakeTelegram, store: &TelegramIntakeStore) -> TelegramChannel {
    TelegramChannel::for_test(token.into(), fake.base.clone(), store.clone())
}

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()
}

struct BurstRow {
    user_name: String,
    parts: i64,
    released_by: String,
    part_times: Vec<serde_json::Value>,
}

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 recorded but not handed on when the process died is taken up by the next run; otherwise
/// 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, not on the token's text, so what the dead run held is its to take.
#[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;

    let first = channel("token-a", &fake, &store);

    // The process dies mid-flight: the sink is gone, so the hand-over can never succeed and the
    // event stays held — what a run that died before handing it on leaves behind. Dropping the
    // receiver up front also keeps the wait below off the collector's one-second window.
    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"
    );

    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");
    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 behind.
    until("the 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(),
        "an album's member was handled as an unrelated message"
    );
    listener.abort();
}

#[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;

    run(&store, "previous-run", "700")
        .hold(&[&test_text_update(1, "a-only")])
        .await
        .expect("hold the previous account's event");
    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");

    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();
}

#[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));
    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 live by then: sends and a later
/// restart reach the listener through the registry, so a teardown removing its channel by name
/// would stop every Telegram send after a quick clear-then-set. Both the teardown and the save's
/// hand-over wait on the one frame the cleared listener runs.
#[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;

    let registry = crate::ChannelRegistry::default();

    // The cleared listener holds a burst it cannot hand on: no pipeline room, so its frame is 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;

    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"
    );

    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"
    );

    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 channel it is about to start, even when that channel is in the
/// frames list under the name — the state a superseded save leaves, its replace having moved past
/// the channel the earlier save built whose hand-over has not run yet. A stop would fire its cancel
/// token, so the listener started right after would return at once.
#[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 replaces `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"
    );
    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, it must not stay registered — sends would reach a listener 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 previous one built,
/// whose listener has not begun, while the frame that really holds events is still open. The wait
/// cannot live on the channels — whichever hand-over walks such a chain first consumes it, and the
/// next save would start a listener over a frame still holding undelivered events, reaching the
/// pipeline twice with the same content. It lives under the channel's name.
#[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(&[])]);
    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;

    let registry = crate::ChannelRegistry::default();

    // The listener every save replaces holds a burst it cannot hand on (no pipeline room).
    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;

    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 holding the event stays open, no later save's listener 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.
    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 was parked on that frame, so its end releases all.
    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 is not handed on again, it serves its 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 serve their own batches at most, never the event.
    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` after long silence, so a stale decision would swallow the new event.
#[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;

    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, which hands over and settles the burst. That is what lets a replacement take over
/// only what the cancelled listener 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;

    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 and settles the event; 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: since the frame ended only once what it
    // held was handed on and settled, nothing is left to take up and the event reaches the pipeline
    // once. The wait below is the collector's quiet 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 — task
/// dropped, or a panic — still leaves its frame open while its collector holds an undelivered
/// burst, so a replacement waits the hand-over out instead of starting on top of it. The cancelled
/// listener above ends the way a token change stops one; this end nobody 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 (a failed hand-over 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 pipeline has no room for the burst until the check makes it, so the
/// event sits in the record while the listener winds down, neither dropped nor left unhandled.
/// Serialized on the process-global drain flag, as that check 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: the burst 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 parse sent is the durable sign.
    // The record's hold is not (the collector clears it on hand-over), but with no pipeline room it
    // 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 is read: a later one asks otherwise.
    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, so an
    // await 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 dropped nothing 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 held burst 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;

    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 last because the frame
    // cannot end while the burst is unhanded, so the fetch-stop cannot be asserted 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 again.
#[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)]);
    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 position must move past what the record holds: the platform re-offers a batch until it
    // does, so a stalled position would re-offer the event forever. A stalled position pauses
    // before its next poll, hence the widened bound — the cheap one would time out on timing luck.
    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 it is offered forever.
    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:?}"
    );

    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, 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;
    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 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.
#[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, with a command at its end — the batch the
    // 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.
    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. A classification refusing 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, ending the backlog and leaving the
    // loop's own long poll fresh; the command is the batch after 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 handshake's batch, so the poll collecting it again is looking at events this run
/// already saw, 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: the handshake fetches the first copy with the record
    // unwritable, the loop's first poll the second, the poll after that the third.
    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;
    });

    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;
    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 its fate is durable, and the intake backs off instead of re-polling.
#[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
    // gives a poll an unrecordable event — 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, whose record
    // could not be written for. The poll after it is the one the backoff holds off, and it hangs
    // with nothing to serve (still logged on arrival). Waiting needs a bound past a pause — and
    // past two, the handshake pausing on the same batch — hence the widened one.
    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 asking for
    // offset 0, so the loop's polls are those above 0. The gap between the first two is the real
    // claim: a tight retry 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 asked for an offset at or below the offered event's
    // own id (`test_update` carries `update_id` 1), because what the record lacks stays 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 its slow forwarded attachment reach the assistant as one turn. Both arrive in one
/// poll with the comment first, so the group would be released after its quiet window while the
/// attachment is still fetched: the collector holds it on the preparation instead, past the group's
/// own ceiling since the fetch takes longer.
#[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)]);
    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 quiet window apart 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:?}"
    );

    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();
}

#[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)]);
    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 parsed: the fetch runs on the processor, so the
/// next `getUpdates` must arrive while it is 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 must arrive while that fetch still runs.
    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;
    });

    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 arriving after the group is gone is a turn of its own.
#[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, queued only
    // once the first turn is observed — queued up front it would 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");

    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 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, ending the backlog, and the loop's
    // own poll fetches the burst — a command of this run, 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"
    );

    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();
}