tauri-plugin-background-service 1.0.1

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

//! # tauri-plugin-background-service
//!
//! A [Tauri](https://tauri.app) v2 plugin that manages long-lived background service
//! lifecycle across **Android**, **iOS**, and **Desktop**.
//!
//! Users implement the [`BackgroundService`] trait; the plugin handles OS-specific
//! keepalive (Android foreground service, iOS `BGTaskScheduler`), cancellation via
//! [`CancellationToken`](tokio_util::sync::CancellationToken), and state management
//! through an actor pattern.
//!
//! ## Quick Start
//!
//! ```rust,ignore
//! use tauri_plugin_background_service::{
//!     BackgroundService, ServiceContext, ServiceError, init_with_service,
//! };
//!
//! struct MyService;
//!
//! #[async_trait::async_trait]
//! impl<R: tauri::Runtime> BackgroundService<R> for MyService {
//!     async fn init(&mut self, _ctx: &ServiceContext<R>) -> Result<(), ServiceError> {
//!         Ok(())
//!     }
//!
//!     async fn run(&mut self, ctx: &ServiceContext<R>) -> Result<(), ServiceError> {
//!         tokio::select! {
//!             _ = ctx.shutdown.cancelled() => Ok(()),
//!             _ = do_work(ctx) => Ok(()),
//!         }
//!     }
//! }
//!
//! tauri::Builder::default()
//!     .plugin(init_with_service(|| MyService))
//! ```
//!
//! ## Platform Behavior
//!
//! | Platform | Keepalive Mechanism | Auto-restart |
//! |----------|-------------------|-------------|
//! | Android | Foreground service with persistent notification (`START_STICKY`) | Yes |
//! | iOS | `BGTaskScheduler` with expiration handler | No |
//! | Desktop | Plain `tokio::spawn` | No |
//!
//! ## iOS Setup
//!
//! Add the following entries to your app's `Info.plist`:
//!
//! ```xml
//! <key>BGTaskSchedulerPermittedIdentifiers</key>
//! <array>
//!     <string>$(BUNDLE_ID).bg-refresh</string>
//!     <string>$(BUNDLE_ID).bg-processing</string>
//! </array>
//!
//! <key>UIBackgroundModes</key>
//! <array>
//!     <string>processing</string>
//!     <string>fetch</string>
//! </array>
//! ```
//!
//! Replace `$(BUNDLE_ID)` with your app's bundle identifier.
//! Without these entries, `BGTaskScheduler.shared.submit(_:)` will throw at runtime.
//!
//! See the [project repository](https://github.com/dardourimohamed/tauri-background-service)
//! for detailed platform guides and API documentation.

pub mod capabilities;
pub mod desired_state;
pub mod error;
pub mod manager;
pub mod models;
pub mod notifier;
pub mod service_trait;
pub mod validator;

#[cfg(mobile)]
pub mod mobile;

#[cfg(feature = "desktop-service")]
pub mod desktop;

// ─── Public API Surface ──────────────────────────────────────────────────────

pub use error::ServiceError;
#[doc(hidden)]
pub use manager::{manager_loop, OnCompleteCallback, ServiceFactory, ServiceManagerHandle};
pub use models::{
    IOSSchedulingStatus, LifecycleState, LifecycleStatus, PendingTaskInfo, Platform,
    PlatformCapabilities, PluginConfig, PluginEvent, ServiceContext, ServiceState, ServiceStatus,
    SetupIssue, SetupValidationReport, StartConfig, ValidationIssue,
};
pub use notifier::{Notifier, NotifierPolicy, NotifySink};
pub use service_trait::BackgroundService;

#[cfg(all(feature = "desktop-service", unix))]
pub use desktop::headless::{headless_main, headless_main_with_desired_state};

// ─── Internal Imports ────────────────────────────────────────────────────────

use tauri::{
    plugin::{Builder, TauriPlugin},
    AppHandle, Manager, Runtime,
};

use crate::manager::ManagerCommand;

#[cfg(mobile)]
use crate::manager::MobileKeepalive;

// `MobileLifecycle` is referenced from the iOS plugin bindings below AND from the
// android-active notification-permission commands (NTF-09), so it must be in
// scope on every mobile target (Android + iOS), not just iOS.
#[cfg(mobile)]
use mobile::MobileLifecycle;

use std::sync::Arc;

// ─── iOS Plugin Binding ──────────────────────────────────────────────────────
// Must be at module level. Referenced by mobile::init() when registering
// the iOS plugin. Only compiled when targeting iOS.

#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_background_service);

// ─── iOS Lifecycle Helpers ────────────────────────────────────────────────────

/// Set the on_complete callback so iOS `completeBgTask` fires when `run()` finishes.
///
/// Sends `SetOnComplete` to the actor. Must be called **before** `Start` because
/// `handle_start` captures the callback via `take()` at spawn time.
#[cfg(target_os = "ios")]
async fn ios_set_on_complete_callback<R: Runtime>(app: &AppHandle<R>) -> Result<(), String> {
    let mobile = app.state::<Arc<MobileLifecycle<R>>>();
    let mobile_handle = mobile.handle.clone();
    let manager = app.state::<ServiceManagerHandle<R>>();

    let mob_for_complete = MobileLifecycle {
        handle: mobile_handle,
    };
    manager
        .cmd_tx
        .send(ManagerCommand::SetOnComplete {
            callback: Box::new(move |success| {
                let _ = mob_for_complete.complete_bg_task(success);
            }),
        })
        .await
        .map_err(|e| e.to_string())
}

#[cfg(not(target_os = "ios"))]
async fn ios_set_on_complete_callback<R: Runtime>(_app: &AppHandle<R>) -> Result<(), String> {
    Ok(())
}

/// Spawn a blocking thread that waits for the iOS expiration signal (`waitForCancel`).
///
/// Must be called **after** `Start` succeeds so the service is running when the
/// cancel listener begins waiting. Sends `Stop` to the actor when cancelled.
///
/// Three outcomes:
/// 1. **Resolved invoke** (safety timer / expiration) → `Ok(())` → send `StopWithReason(PlatformExpiration)`.
/// 2. **Timeout** (default: 4h) → call `cancel_cancel_listener` to unblock the
///    thread, then send `StopWithReason(PlatformTimeout)`.
/// 3. **Rejected invoke** (explicit stop / natural completion) → `Err` → no action.
///
/// Core cancel listener logic, extracted for testability.
///
/// - `wait_fn`: blocking function simulating `wait_for_cancel` (returns `Ok(())` on resolve,
///   `Err` on reject).
/// - `cancel_fn`: called on timeout to unblock the `wait_fn` thread.
/// - `cmd_tx`: channel to send `StopWithReason` command on resolve/timeout.
/// - `timeout_secs`: how long to wait before treating the listener as timed out.
///
/// Returns `true` if a `StopWithReason` was sent, `false` otherwise.
#[allow(dead_code)] // used on iOS + in tests
async fn run_cancel_listener<R: Runtime>(
    wait_fn: Box<dyn FnOnce() -> Result<(), ServiceError> + Send>,
    cancel_fn: Box<dyn FnOnce() + Send>,
    cmd_tx: tokio::sync::mpsc::Sender<ManagerCommand<R>>,
    timeout_secs: u64,
) -> bool {
    let handle = tokio::task::spawn_blocking(wait_fn);
    let result = tokio::time::timeout(std::time::Duration::from_secs(timeout_secs), handle).await;
    match result {
        // Resolved invoke (safety timer or expiration) → graceful shutdown
        Ok(Ok(Ok(()))) => {
            let (tx, rx) = tokio::sync::oneshot::channel();
            let _ = cmd_tx
                .send(ManagerCommand::StopWithReason {
                    reason: crate::models::StopReason::PlatformExpiration,
                    reply: tx,
                })
                .await;
            let _ = rx.await;
            true
        }
        // Timeout → unblock the spawn_blocking thread, then graceful shutdown
        Err(_) => {
            cancel_fn();
            let (tx, rx) = tokio::sync::oneshot::channel();
            let _ = cmd_tx
                .send(ManagerCommand::StopWithReason {
                    reason: crate::models::StopReason::PlatformTimeout,
                    reply: tx,
                })
                .await;
            let _ = rx.await;
            true
        }
        // Rejected invoke (explicit stop or natural completion) → no action
        _ => false,
    }
}

#[cfg(target_os = "ios")]
fn ios_spawn_cancel_listener<R: Runtime>(app: &AppHandle<R>, timeout_secs: u64) {
    let mobile = app.state::<Arc<MobileLifecycle<R>>>();
    let mobile_handle = mobile.handle.clone();
    let mobile_handle_for_cancel = mobile.handle.clone();
    let manager = app.state::<ServiceManagerHandle<R>>();
    let cmd_tx = manager.cmd_tx.clone();

    tokio::spawn(async move {
        let wait_fn = Box::new(move || {
            let mob = MobileLifecycle {
                handle: mobile_handle,
            };
            mob.wait_for_cancel()
        });
        let cancel_fn = Box::new(move || {
            let cancel_mob = MobileLifecycle {
                handle: mobile_handle_for_cancel,
            };
            let _ = cancel_mob.cancel_cancel_listener();
        });
        // Ignore result — the listener fires-and-forgets.
        let _ = run_cancel_listener(wait_fn, cancel_fn, cmd_tx, timeout_secs).await;
    });
}

#[cfg(not(target_os = "ios"))]
fn ios_spawn_cancel_listener<R: Runtime>(_app: &AppHandle<R>, _timeout_secs: u64) {}

/// Spawn the iOS **cold BGTask auto-start** probe after plugin setup returns.
///
/// `run_mobile_plugin` is synchronous on the Rust side. Calling it directly from
/// plugin setup can deadlock startup on iOS: setup runs on the main thread, while
/// the Swift command handlers marshal their work back onto `DispatchQueue.main`.
/// Spawning the probe lets Tauri finish building the app and keeps the native
/// bridge calls off the main thread.
#[cfg(target_os = "ios")]
fn ios_spawn_cold_auto_start<R: Runtime>(app: &AppHandle<R>) {
    let app = app.app_handle().clone();
    tauri::async_runtime::spawn(async move {
        ios_handle_cold_auto_start(&app).await;
    });
}

/// Handle iOS cold auto-start when the process was launched for a pending BGTask.
#[cfg(target_os = "ios")]
async fn ios_handle_cold_auto_start<R: Runtime>(app: &AppHandle<R>) {
    let mobile = app.state::<Arc<MobileLifecycle<R>>>().inner().clone();

    let pending = match tokio::task::spawn_blocking({
        let mobile = mobile.clone();
        move || mobile.get_pending_bg_task()
    })
    .await
    {
        Ok(Ok(Some(pending))) => pending,
        Ok(Ok(None)) => {
            // No pending BGTask — normal launch.
            return;
        }
        Ok(Err(e)) => {
            log::warn!("iOS: failed to get pending BGTask: {e}");
            return;
        }
        Err(e) => {
            log::warn!("iOS: failed to join pending BGTask query: {e}");
            return;
        }
    };
    let _ = pending;

    // Read desired_running + last_start_config from the typed DTO (no untyped
    // JSON reads), exactly as the warm auto-start path does.
    let should_start = match tokio::task::spawn_blocking({
        let mobile = mobile.clone();
        move || mobile.get_desired_state_status()
    })
    .await
    {
        Ok(Ok(status)) => status.and_then(|status| {
            let config_str = status.last_start_config?;
            Some((status.desired_running, config_str))
        }),
        Ok(Err(e)) => {
            log::warn!("iOS: failed to get desired-state status: {e}");
            None
        }
        Err(e) => {
            log::warn!("iOS: failed to join desired-state query: {e}");
            None
        }
    };

    let Some((true, config_str)) = should_start else {
        log::info!(
            "iOS: skipped auto-start: desired_running=false — clearing stale pending BGTask"
        );
        let _ = tokio::task::spawn_blocking({
            let mobile = mobile.clone();
            move || mobile.clear_pending_bg_task()
        })
        .await;
        return;
    };

    let Ok(config) = serde_json::from_str::<StartConfig>(&config_str) else {
        log::warn!(
            "iOS: failed to parse stored start config — preserving pending task info for diagnostics"
        );
        return;
    };

    let manager = app.state::<ServiceManagerHandle<R>>();
    let cmd_tx = manager.cmd_tx.clone();
    let app_clone = app.app_handle().clone();
    let timeout_secs = app.state::<PluginConfig>().ios_cancel_listener_timeout_secs;

    // Set on_complete callback for iOS completeBgTask before Start so the actor
    // captures the callback at spawn time.
    let mob_handle = mobile.handle.clone();
    if let Err(e) = cmd_tx
        .send(ManagerCommand::SetOnComplete {
            callback: Box::new(move |success| {
                let ml = MobileLifecycle {
                    handle: mob_handle.clone(),
                };
                let _ = ml.complete_bg_task(success);
            }),
        })
        .await
    {
        log::warn!("iOS: auto-start preserved pending BGTask after failure: {e}");
        let _ = tokio::task::spawn_blocking(move || mobile.record_failed_pending()).await;
        return;
    }

    // H3: clear the pending BGTask only after Start succeeds; on failure
    // preserve it + record a failure marker. The clear lives inside
    // `run_auto_start`, gated on `rx.await == Ok(Ok(()))`.
    let mobile_for_success = mobile.clone();
    let mobile_for_failure = mobile.clone();
    let app_for_listener = app_clone.clone();

    log::info!("iOS: auto-starting service for pending BGTask");
    let on_success = Box::new(move || {
        let _ = mobile_for_success.clear_pending_bg_task();
        ios_spawn_cancel_listener(&app_for_listener, timeout_secs);
    });
    let on_failure = Box::new(move || {
        let _ = mobile_for_failure.record_failed_pending();
    });
    run_auto_start(config, app_clone, cmd_tx, on_success, on_failure).await;
}

/// Spawn the iOS **warm BGTask-delivery** listener (H14).
///
/// A BGTask delivered to a warm/idle process never re-runs the cold auto-start
/// block (that runs once at setup), so without this the process would only
/// persist the pending record and wait. This listener blocks on the Swift
/// `waitForBgTask` Pending Invoke; each time `handleBackgroundTask`/
/// `handleProcessingTask` resolves it, Rust drives [`run_warm_start`] — mirroring
/// the cold sequence (re-send `SetOnComplete` → `Start` → consume on success),
/// while a delivery to an already-running actor is a clean no-op.
///
/// The loop re-blocks after each delivery and exits when the invoke is rejected
/// (`cancel_warm_listener`) or the blocking thread fails.
#[cfg(target_os = "ios")]
fn ios_spawn_warm_listener<R: Runtime>(app: &AppHandle<R>) {
    let app = app.app_handle().clone();
    tauri::async_runtime::spawn(async move {
        loop {
            // Block until iOS delivers a BGTask to the warm process.
            let mobile_handle = app.state::<Arc<MobileLifecycle<R>>>().handle.clone();
            let wait = tokio::task::spawn_blocking(move || {
                MobileLifecycle {
                    handle: mobile_handle,
                }
                .wait_for_bg_task()
            })
            .await;

            match wait {
                Ok(Ok(())) => {
                    ios_handle_warm_delivery(&app).await;
                }
                // Rejected invoke (teardown) or join error → stop listening.
                _ => {
                    log::info!("iOS: warm BGTask listener stopped");
                    break;
                }
            }
        }
    });
}

/// Handle a single warm BGTask delivery: read the typed pending + desired state
/// and drive [`run_warm_start`], mirroring the cold auto-start block.
#[cfg(target_os = "ios")]
async fn ios_handle_warm_delivery<R: Runtime>(app: &AppHandle<R>) {
    // Clone owned values out of managed state so no `State` borrow is held
    // across the `run_warm_start` await.
    let mobile = app.state::<Arc<MobileLifecycle<R>>>().inner().clone();

    let pending = match mobile.get_pending_bg_task() {
        Ok(Some(p)) => p,
        Ok(None) => {
            log::debug!("iOS: warm delivery signalled with no pending BGTask");
            return;
        }
        Err(e) => {
            log::warn!("iOS: warm delivery — failed to get pending BGTask: {e}");
            return;
        }
    };
    let _ = pending;

    // Read desired_running + last_start_config from the typed DTO (no untyped
    // JSON reads), exactly as the cold auto-start path does.
    let should_start = mobile
        .get_desired_state_status()
        .ok()
        .flatten()
        .and_then(|status| {
            let config_str = status.last_start_config?;
            Some((status.desired_running, config_str))
        });

    let Some((true, config_str)) = should_start else {
        log::info!("iOS: warm delivery skipped: desired_running=false");
        return;
    };

    let Ok(config) = serde_json::from_str::<StartConfig>(&config_str) else {
        log::warn!(
            "iOS: warm delivery — failed to parse stored start config; preserving pending task info"
        );
        return;
    };

    let manager = app.state::<ServiceManagerHandle<R>>();
    let cmd_tx = manager.cmd_tx.clone();
    let app_clone = app.app_handle().clone();
    let timeout_secs = app.state::<PluginConfig>().ios_cancel_listener_timeout_secs;

    // Re-send SetOnComplete so completeBgTask fires (take()n at spawn).
    let mob_handle = mobile.handle.clone();
    let on_complete: OnCompleteCallback = Box::new(move |success| {
        let ml = MobileLifecycle {
            handle: mob_handle.clone(),
        };
        let _ = ml.complete_bg_task(success);
    });

    // On success: consume the pending record (M14 part 2) + spawn the cancel
    // listener. On genuine failure: preserve the evidence + record a marker.
    let mobile_for_success = mobile.clone();
    let mobile_for_failure = mobile.clone();
    let app_for_listener = app_clone.clone();
    let on_success = Box::new(move || {
        let _ = mobile_for_success.clear_pending_bg_task();
        ios_spawn_cancel_listener(&app_for_listener, timeout_secs);
    });
    let on_failure = Box::new(move || {
        let _ = mobile_for_failure.record_failed_pending();
    });

    log::info!("iOS: warm-starting service for delivered BGTask");
    run_warm_start(
        config,
        app_clone,
        cmd_tx,
        on_complete,
        on_success,
        on_failure,
    )
    .await;
}

#[cfg(not(target_os = "ios"))]
#[allow(dead_code)]
fn ios_spawn_warm_listener<R: Runtime>(_app: &AppHandle<R>) {}

/// Drive the iOS cold auto-start sequence and consume the pending BGTask
/// **exactly once, only on success** (H3).
///
/// The pending record is the evidence that iOS launched us for a background
/// task. Clearing it before `Start` actually succeeds means a failed start
/// silently discards that evidence — the task never reruns and we can't tell
/// it failed. So the clear is gated on the actor replying `Ok(Ok(()))`:
/// - **success** → `on_success` (clear pending + spawn the cancel listener),
///   logged "consumed after success".
/// - **failure** (command channel closed, reply dropped, or `Start` errored) →
///   `on_failure` (record a failure marker; the pending record is preserved
///   because it is *not* cleared), logged "preserved after failure".
///
/// Extracted for host testability — mirrors [`run_cancel_listener`]. The iOS
/// wiring injects the mobile side-effects (clear / record-failure / cancel
/// listener) as closures so this core gating logic runs on the macOS test gate.
///
/// Returns `true` if `Start` succeeded (pending consumed), `false` otherwise.
#[allow(dead_code)] // used on iOS + in tests
async fn run_auto_start<R: Runtime>(
    config: StartConfig,
    app: AppHandle<R>,
    cmd_tx: tokio::sync::mpsc::Sender<ManagerCommand<R>>,
    on_success: Box<dyn FnOnce() + Send>,
    on_failure: Box<dyn FnOnce() + Send>,
) -> bool {
    let (tx, rx) = tokio::sync::oneshot::channel();
    if cmd_tx
        .send(ManagerCommand::Start {
            config,
            reply: tx,
            app,
        })
        .await
        .is_err()
    {
        log::warn!(
            "iOS: auto-start preserved pending BGTask after failure (command channel closed)"
        );
        on_failure();
        return false;
    }

    match rx.await {
        Ok(Ok(())) => {
            log::info!("iOS: auto-start consumed pending BGTask after success");
            on_success();
            true
        }
        Ok(Err(e)) => {
            log::warn!("iOS: auto-start preserved pending BGTask after failure: {e}");
            on_failure();
            false
        }
        Err(e) => {
            log::warn!(
                "iOS: auto-start preserved pending BGTask after failure (reply dropped: {e})"
            );
            on_failure();
            false
        }
    }
}

/// Drive the iOS **warm** BGTask-delivery start sequence (H14, M14 part 2).
///
/// A BGTask delivered to a warm/idle process must actually start the Rust
/// service, not merely persist pending state and wait. This mirrors
/// [`run_auto_start`] (the *cold* launch path) but runs while the process is
/// already alive, so it must be a clean no-op when the actor is already running:
///
/// 1. **Guard `AlreadyRunning`** — pre-check `is_running`. A warm delivery to a
///    running actor returns `false` without arming a stale `on_complete` or
///    consuming the pending record (M14 part 2: it cannot re-arm a cold
///    auto-start).
/// 2. **Re-send `SetOnComplete`** — the actor `take()`s the callback at spawn,
///    so a fresh one is required for each start; this is how `completeBgTask`
///    fires (not the iOS safety timer).
/// 3. **`Start`** → on success consume the pending record (`on_success`, which
///    also spawns the cancel listener in production); on a genuine failure
///    preserve the evidence and record a marker (`on_failure`).
///
/// Extracted for host testability — the iOS wiring injects the mobile
/// side-effects (clear / record-failure / cancel listener) as closures so this
/// core gating logic runs on the macOS test gate.
///
/// Returns `true` if a warm `Start` succeeded (pending consumed), `false`
/// otherwise (no-op while running, or preserved-on-failure).
#[allow(dead_code)] // used on iOS + in tests
async fn run_warm_start<R: Runtime>(
    config: StartConfig,
    app: AppHandle<R>,
    cmd_tx: tokio::sync::mpsc::Sender<ManagerCommand<R>>,
    on_complete: OnCompleteCallback,
    on_success: Box<dyn FnOnce() + Send>,
    on_failure: Box<dyn FnOnce() + Send>,
) -> bool {
    // Guard AlreadyRunning: a warm delivery to a running actor is a clean no-op.
    // Pre-checking `is_running` (rather than reacting to a `Start` rejection)
    // avoids arming a stale `on_complete`, which would otherwise fire for the
    // wrong BGTask on the next legitimate start. The pending record is left for
    // the running service to consume on its own completion.
    let (run_tx, run_rx) = tokio::sync::oneshot::channel();
    if cmd_tx
        .send(ManagerCommand::IsRunning { reply: run_tx })
        .await
        .is_err()
    {
        log::warn!(
            "iOS: warm start preserved pending BGTask after failure (command channel closed)"
        );
        on_failure();
        return false;
    }
    if run_rx.await.unwrap_or(false) {
        log::info!("iOS: warm BGTask delivery while already running — no-op");
        return false;
    }

    // Re-send SetOnComplete: the actor `take()`s it at spawn, so a fresh callback
    // is required for each start so `completeBgTask` fires (not the safety timer).
    if cmd_tx
        .send(ManagerCommand::SetOnComplete {
            callback: on_complete,
        })
        .await
        .is_err()
    {
        log::warn!("iOS: warm start preserved pending BGTask after failure (channel closed)");
        on_failure();
        return false;
    }

    let (tx, rx) = tokio::sync::oneshot::channel();
    if cmd_tx
        .send(ManagerCommand::Start {
            config,
            reply: tx,
            app,
        })
        .await
        .is_err()
    {
        log::warn!(
            "iOS: warm start preserved pending BGTask after failure (command channel closed)"
        );
        on_failure();
        return false;
    }

    match rx.await {
        Ok(Ok(())) => {
            log::info!("iOS: warm BGTask delivery started service; consumed pending BGTask");
            on_success();
            true
        }
        // Lost the race after the IsRunning pre-check — still a clean no-op, not
        // a failure: the actor became running between the check and Start.
        Ok(Err(ServiceError::AlreadyRunning)) => {
            log::info!("iOS: warm BGTask delivery raced a running actor — no-op");
            false
        }
        Ok(Err(e)) => {
            log::warn!("iOS: warm start preserved pending BGTask after failure: {e}");
            on_failure();
            false
        }
        Err(e) => {
            log::warn!(
                "iOS: warm start preserved pending BGTask after failure (reply dropped: {e})"
            );
            on_failure();
            false
        }
    }
}

// ─── Tauri Commands ──────────────────────────────────────────────────────────

#[tauri::command]
async fn start<R: Runtime>(app: AppHandle<R>, config: StartConfig) -> Result<(), String> {
    // OS service mode: route through persistent IPC client.
    #[cfg(all(feature = "desktop-service", unix))]
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        // Check if IPC is connected before sending the start request.
        if ipc_state.client.is_connected() {
            return ipc_state
                .client
                .start(config)
                .await
                .map_err(|e| e.to_string());
        }

        // IPC is disconnected. Check if auto-start is enabled.
        let plugin_config = app.state::<PluginConfig>();
        if !plugin_config.desktop_start_service_if_missing {
            return Err(ServiceError::Ipc("ipcUnavailable".into()).to_string());
        }

        // Try to start the OS service and wait for IPC readiness.
        let socket_path = ipc_state.client.socket_path().display().to_string();
        let timeout =
            std::time::Duration::from_millis(plugin_config.desktop_service_start_timeout_ms);

        use desktop::service_manager::{derive_service_label, DesktopServiceManager};
        let label = derive_service_label(&app, plugin_config.desktop_service_label.as_deref());
        let exec_path = std::env::current_exe().map_err(|e| e.to_string())?;
        {
            let mgr = DesktopServiceManager::new(&label, exec_path).map_err(|e| e.to_string())?;
            mgr.start().map_err(|e| e.to_string())?;
        }

        ipc_state.client.nudge_reconnect();
        let connected = ipc_state
            .client
            .wait_for_connected(timeout)
            .await
            .map_err(|e| e.to_string())?;

        if !connected {
            return Err(
                ServiceError::Ipc(format!("ipcUnavailable: socket {socket_path}")).to_string(),
            );
        }

        // IPC is now connected — send the start command.
        return ipc_state
            .client
            .start(config)
            .await
            .map_err(|e| e.to_string());
    }

    // In-process mode (default).
    // iOS: send SetOnComplete before Start so the callback is captured at spawn time.
    ios_set_on_complete_callback(&app).await?;

    // Mobile keepalive is now handled by the actor (Step 5).
    // The actor calls start_keepalive AFTER the AlreadyRunning check.

    let manager = app.state::<ServiceManagerHandle<R>>();
    let (tx, rx) = tokio::sync::oneshot::channel();
    manager
        .cmd_tx
        .send(ManagerCommand::Start {
            config,
            reply: tx,
            app: app.clone(),
        })
        .await
        .map_err(|e| e.to_string())?;

    rx.await
        .map_err(|e| e.to_string())?
        .map_err(|e| e.to_string())?;

    // iOS: spawn cancel listener after Start succeeds.
    let plugin_config = app.state::<PluginConfig>();
    ios_spawn_cancel_listener(&app, plugin_config.ios_cancel_listener_timeout_secs);

    Ok(())
}

#[tauri::command]
async fn stop<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
    // OS service mode: route through persistent IPC client.
    #[cfg(all(feature = "desktop-service", unix))]
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        return ipc_state.client.stop().await.map_err(|e| e.to_string());
    }

    // In-process mode (default).
    let manager = app.state::<ServiceManagerHandle<R>>();
    let (tx, rx) = tokio::sync::oneshot::channel();
    manager
        .cmd_tx
        .send(ManagerCommand::Stop { reply: tx })
        .await
        .map_err(|e| e.to_string())?;

    rx.await
        .map_err(|e| e.to_string())?
        .map_err(|e| e.to_string())
}

#[tauri::command]
async fn is_running<R: Runtime>(app: AppHandle<R>) -> bool {
    // OS service mode: route through persistent IPC client.
    #[cfg(all(feature = "desktop-service", unix))]
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        return ipc_state.client.is_running().await.unwrap_or(false);
    }

    // In-process mode (default).
    let manager = app.state::<ServiceManagerHandle<R>>();
    let (tx, rx) = tokio::sync::oneshot::channel();
    if manager
        .cmd_tx
        .send(ManagerCommand::IsRunning { reply: tx })
        .await
        .is_err()
    {
        return false;
    }
    rx.await.unwrap_or(false)
}

#[tauri::command]
async fn get_service_state<R: Runtime>(app: AppHandle<R>) -> Result<models::ServiceStatus, String> {
    // OS service mode: route through persistent IPC client.
    #[cfg(all(feature = "desktop-service", unix))]
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        return ipc_state
            .client
            .get_state()
            .await
            .map_err(|e| e.to_string());
    }

    // In-process mode (default).
    let manager = app.state::<ServiceManagerHandle<R>>();
    Ok(manager.get_state().await)
}

#[tauri::command]
#[allow(unused_variables)]
async fn get_platform_capabilities<R: Runtime>(
    app: AppHandle<R>,
) -> Result<models::PlatformCapabilities, String> {
    #[cfg(feature = "desktop-service")]
    let plugin_config = app.state::<PluginConfig>();

    #[cfg(feature = "desktop-service")]
    let desktop_mode = Some(plugin_config.desktop_service_mode.as_str());
    #[cfg(not(feature = "desktop-service"))]
    let desktop_mode: Option<&str> = None;

    let (platform, lifecycle_mode) =
        capabilities::CapabilityProvider::detect_platform(desktop_mode);

    #[cfg(all(feature = "desktop-service", unix))]
    let os_service_installed = if matches!(lifecycle_mode, models::LifecycleMode::DesktopOsService)
    {
        use desktop::service_manager::{derive_service_label, DesktopServiceManager};
        let label = derive_service_label(&app, plugin_config.desktop_service_label.as_deref());
        let exec = std::env::current_exe().unwrap_or_default();
        DesktopServiceManager::new(&label, exec)
            .map(|_| true)
            .unwrap_or(false)
    } else {
        false
    };

    #[cfg(not(all(feature = "desktop-service", unix)))]
    let os_service_installed = false;

    Ok(capabilities::CapabilityProvider::capabilities(
        platform,
        lifecycle_mode,
        os_service_installed,
    ))
}

/// Query the iOS scheduling status from the native layer.
///
/// Returns `IOSSchedulingStatus` on iOS with scheduling results and desired state.
/// Returns a default status (not scheduled) on non-iOS platforms.
#[tauri::command]
async fn get_scheduling_status<R: Runtime>(
    app: AppHandle<R>,
) -> Result<models::IOSSchedulingStatus, String> {
    #[cfg(target_os = "ios")]
    {
        let mobile = app.state::<Arc<MobileLifecycle<R>>>();
        mobile
            .get_scheduling_status()
            .map_err(|e| e.to_string())
            .and_then(|opt| opt.ok_or_else(|| "no scheduling status available".to_string()))
    }
    #[cfg(not(target_os = "ios"))]
    {
        let _ = app;
        Ok(models::IOSSchedulingStatus {
            refresh_scheduled: false,
            processing_scheduled: false,
            refresh_error: None,
            processing_error: None,
        })
    }
}

/// Request the Android battery-optimization (Doze) exemption (BGS-22, doc-08
/// Step 14).
///
/// Forwards to the Kotlin `requestBatteryExemption` @Command, which fires
/// `startActivity(Intent(ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
/// "package:<app>"))` to surface the system Doze-exemption dialog. The
/// `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` permission is declared in the plugin
/// `android/src/main/AndroidManifest.xml` but was previously never requested —
/// this wires the honest user-granted flow (preferring the flow over dropping
/// the permission). No-op on non-Android targets (the Kotlin @Command does not
/// exist; iOS/desktop have no Doze analogue). There is no Rust-side status
/// mirror: the exemption is OS-only and not queryable through this plugin.
#[tauri::command]
async fn request_battery_exemption<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
    #[cfg(target_os = "android")]
    {
        let mobile = app.state::<Arc<MobileLifecycle<R>>>();
        mobile
            .request_battery_exemption()
            .map_err(|e| e.to_string())
    }
    #[cfg(not(target_os = "android"))]
    {
        let _ = app;
        Ok(())
    }
}

/// Query the persisted iOS desired-state status from the native layer.
///
/// Returns `IOSDesiredStateStatus` on iOS with the persisted desired state.
/// Returns a default status (not desired) on non-iOS platforms.
#[tauri::command]
async fn get_desired_state_status<R: Runtime>(
    app: AppHandle<R>,
) -> Result<models::IOSDesiredStateStatus, String> {
    #[cfg(target_os = "ios")]
    {
        let mobile = app.state::<Arc<MobileLifecycle<R>>>();
        mobile
            .get_desired_state_status()
            .map_err(|e| e.to_string())
            .and_then(|opt| opt.ok_or_else(|| "no desired-state status available".to_string()))
    }
    #[cfg(not(target_os = "ios"))]
    {
        let _ = app;
        Ok(models::IOSDesiredStateStatus {
            desired_running: false,
            last_start_config: None,
            last_task_kind: None,
            last_task_started_at: None,
            last_task_completed_at: None,
            last_schedule_error: None,
            last_completion_reason: None,
            notification_granted: None,
        })
    }
}

/// Query the pending iOS background task info.
///
/// Returns `Some(PendingTaskInfo)` on iOS if the app was launched by iOS for
/// a background task and the info hasn't been cleared yet.
/// Returns `None` on non-iOS platforms or when no pending task exists.
#[tauri::command]
async fn get_pending_bg_task<R: Runtime>(
    app: AppHandle<R>,
) -> Result<Option<models::PendingTaskInfo>, String> {
    #[cfg(target_os = "ios")]
    {
        let mobile = app.state::<Arc<MobileLifecycle<R>>>();
        mobile.get_pending_bg_task().map_err(|e| e.to_string())
    }
    #[cfg(not(target_os = "ios"))]
    {
        let _ = app;
        Ok(None)
    }
}

/// Query the POST_NOTIFICATIONS permission status (NTF-09).
///
/// Android returns the current status (`granted` | `notDetermined` | `denied`)
/// via the Kotlin `getNotificationPermissionStatus` command, which resolves
/// immediately — so the mobile call is made directly (mirroring
/// `get_scheduling_status`). Non-Android returns a default `"granted"` so the
/// command is callable cross-platform. (The iOS UN-prompt half of NTF-09 is
/// Step 10c, not this command.)
///
/// WIRE-01: the public JS contract is a SCALAR string union
/// (`NotificationPermissionStatus = 'granted' | 'denied' | 'notDetermined'`),
/// so the command returns the inner `String`, not the `{status}` object the
/// Kotlin side emits. The struct stays internal to mobile.rs for
/// deserialization.
#[tauri::command]
async fn get_notification_permission_status<R: Runtime>(
    app: AppHandle<R>,
) -> Result<String, String> {
    #[cfg(target_os = "android")]
    {
        let mobile = app.state::<Arc<MobileLifecycle<R>>>();
        mobile
            .get_notification_permission_status()
            .map_err(|e| e.to_string())
    }
    #[cfg(not(target_os = "android"))]
    {
        let _ = app;
        Ok("granted".to_string())
    }
}

/// Request POST_NOTIFICATIONS permission (NTF-09).
///
/// Android runs the Kotlin `requestNotificationPermission` command, which on
/// API 33+ defers resolution to the OS permission dialog via the
/// `@PermissionCallback` (Step 10a). That call blocks `run_mobile_plugin`'s
/// `rx.recv()` for the dialog duration, so it is wrapped in
/// `tokio::task::spawn_blocking` to avoid blocking the async runtime (the
/// `wait_for_cancel` class). Non-Android returns a default `"granted"`.
///
/// WIRE-01: the public JS contract is a SCALAR string union, so the command
/// returns the inner `String`, not the `{status}` object. The requester now
/// resolves with the same `NotificationPermissionStatus` type as the getter
/// (previously it returned `Promise<void>` and discarded the result).
#[tauri::command]
async fn request_notification_permission<R: Runtime>(app: AppHandle<R>) -> Result<String, String> {
    #[cfg(target_os = "android")]
    {
        let mobile = app.state::<Arc<MobileLifecycle<R>>>().inner().clone();
        tokio::task::spawn_blocking(move || mobile.request_notification_permission())
            .await
            .map_err(|e| e.to_string())?
            .map_err(|e| e.to_string())
    }
    #[cfg(not(target_os = "android"))]
    {
        let _ = app;
        Ok("granted".to_string())
    }
}

/// Whether the app may post a full-screen intent (NTF-16, Step 12c).
///
/// Android queries `IncomingCallNotifier.canUseFullScreenIntent` via the Kotlin
/// `canUseFullScreenIntent` command, which resolves immediately — so the mobile
/// call is made directly (mirroring `get_notification_permission_status`). Non-
/// Android defaults to `canUse: true` so the re-grant affordance never shows
/// (FSI is irrelevant off-Android).
///
/// IPC SHAPE CONTRACT: returns a `serde_json::Value` OBJECT `{ "canUse": bool }`
/// (NOT a bare `bool`) so it matches the TS `invoke<{ canUse: boolean }>` wrapper
/// and the UI consumer `result.canUse`. A bare `Result<bool, String>` would
/// serde-serialize to a bare JSON boolean; the TS generic is only a compile-time
/// cast, so `result.canUse` would be `undefined` at runtime and the `=== false`
/// re-grant gate would NEVER fire — silently killing NTF-16 on Android (the only
/// platform where it matters). The Kotlin layer resolves `{canUse: bool}` either
/// way; this command re-wraps the typed bool the mobile bridge extracts so the
/// object shape flows end-to-end. The fully-mocked vitest cannot reach this Rust
/// serde shape, so it is pinned statically by the
/// `ntf16_full_screen_intent_wire_is_present_and_unique` integration test.
#[tauri::command]
async fn can_use_full_screen_intent<R: Runtime>(
    app: AppHandle<R>,
) -> Result<serde_json::Value, String> {
    #[cfg(target_os = "android")]
    {
        let mobile = app.state::<Arc<MobileLifecycle<R>>>();
        let can_use = mobile
            .can_use_full_screen_intent()
            .map_err(|e| e.to_string())?;
        Ok(serde_json::json!({ "canUse": can_use }))
    }
    #[cfg(not(target_os = "android"))]
    {
        let _ = app;
        Ok(serde_json::json!({ "canUse": true }))
    }
}

/// Open the OS settings page to re-grant USE_FULL_SCREEN_INTENT (NTF-16).
///
/// Android runs the Kotlin `openFullScreenIntentSettings` command, which
/// resolves immediately (startActivity) — NO `spawn_blocking` (unlike
/// `request_notification_permission`, this is not a deferred @PermissionCallback
/// flow). Non-Android is a no-op (the affordance never shows there).
#[tauri::command]
async fn open_full_screen_intent_settings<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
    #[cfg(target_os = "android")]
    {
        let mobile = app.state::<Arc<MobileLifecycle<R>>>();
        mobile
            .open_full_screen_intent_settings()
            .map_err(|e| e.to_string())
    }
    #[cfg(not(target_os = "android"))]
    {
        let _ = app;
        Ok(())
    }
}

/// Enable auto-restart for the background service.
///
/// Persists `desired_running=true` with an optional start config WITHOUT
/// starting the service. This sets the intent for recovery after process
/// kill or device reboot. The platform recovery mechanisms will use this
/// to automatically restart the service when conditions allow.
#[tauri::command]
async fn enable_auto_restart<R: Runtime>(
    app: AppHandle<R>,
    config: Option<StartConfig>,
) -> Result<(), String> {
    // OS service mode: route through persistent IPC client.
    #[cfg(all(feature = "desktop-service", unix))]
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        return ipc_state
            .client
            .enable_auto_restart(config)
            .await
            .map_err(|e| e.to_string());
    }

    let manager = app.state::<ServiceManagerHandle<R>>();
    let (tx, rx) = tokio::sync::oneshot::channel();
    manager
        .cmd_tx
        .send(ManagerCommand::EnableAutoRestart { config, reply: tx })
        .await
        .map_err(|e| e.to_string())?;
    rx.await
        .map_err(|e| e.to_string())?
        .map_err(|e| e.to_string())
}

/// Disable auto-restart for the background service.
///
/// Persists `desired_running=false` and clears recovery fields WITHOUT
/// stopping the service if it is currently running. After calling this,
/// the platform recovery mechanisms will no longer attempt to restart the
/// service after process kill or device reboot.
#[tauri::command]
async fn disable_auto_restart<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
    // OS service mode: route through persistent IPC client.
    #[cfg(all(feature = "desktop-service", unix))]
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        return ipc_state
            .client
            .disable_auto_restart()
            .await
            .map_err(|e| e.to_string());
    }

    let manager = app.state::<ServiceManagerHandle<R>>();
    let (tx, rx) = tokio::sync::oneshot::channel();
    manager
        .cmd_tx
        .send(ManagerCommand::DisableAutoRestart { reply: tx })
        .await
        .map_err(|e| e.to_string())?;
    rx.await
        .map_err(|e| e.to_string())?
        .map_err(|e| e.to_string())
}

/// Get the persisted desired-state for the background service.
///
/// Returns `Some(DesiredState)` with the current recovery intent and metadata,
/// or `None` if no persistence backend is configured on the current platform.
#[tauri::command]
async fn get_desired_service_state<R: Runtime>(
    app: AppHandle<R>,
) -> Result<Option<desired_state::DesiredState>, String> {
    // OS service mode: route through persistent IPC client.
    #[cfg(all(feature = "desktop-service", unix))]
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        return ipc_state
            .client
            .get_desired_state()
            .await
            .map_err(|e| e.to_string());
    }

    let manager = app.state::<ServiceManagerHandle<R>>();
    let (tx, rx) = tokio::sync::oneshot::channel();
    manager
        .cmd_tx
        .send(ManagerCommand::GetDesiredState { reply: tx })
        .await
        .map_err(|e| e.to_string())?;
    rx.await.map_err(|e| e.to_string())
}

/// Notify the Rust actor of a native platform lifecycle event.
///
/// Called from the native layer (Kotlin/Swift) when the OS triggers a
/// lifecycle action that the Rust actor must handle — e.g. the user pressed
/// "Stop" on the Android foreground notification, or Android timed out the
/// foreground service.
///
/// The actor maps each [`NativeLifecycleEvent`] variant to the appropriate
/// [`StopReason`](models::StopReason) and dispatches through
/// [`handle_stop_with_reason`](manager::handle_stop_with_reason).
///
/// This command is not intended for end-user consumption — it is called by
/// the native plugin code.
#[tauri::command]
async fn native_lifecycle_event<R: Runtime>(
    app: AppHandle<R>,
    event: models::NativeLifecycleEvent,
) -> Result<(), String> {
    let manager = app.state::<ServiceManagerHandle<R>>();
    manager
        .send_native_lifecycle_event(event)
        .await
        .map_err(|e| e.to_string())
}

/// Validate the background service setup for the current platform.
///
/// Returns a [`SetupValidationReport`] with errors (blocking) and warnings
/// (non-blocking) about platform-specific prerequisites.
#[tauri::command]
#[allow(unused_variables)]
async fn validate_setup<R: Runtime>(
    app: AppHandle<R>,
) -> Result<models::SetupValidationReport, String> {
    // OS service mode: route through persistent IPC client.
    #[cfg(all(feature = "desktop-service", unix))]
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        return ipc_state
            .client
            .validate_setup()
            .await
            .map_err(|e| e.to_string());
    }

    #[cfg(feature = "desktop-service")]
    let plugin_config = app.state::<PluginConfig>();

    #[cfg(feature = "desktop-service")]
    let desktop_mode = Some(plugin_config.desktop_service_mode.as_str());
    #[cfg(not(feature = "desktop-service"))]
    let desktop_mode: Option<&str> = None;

    let (platform, _) = capabilities::CapabilityProvider::detect_platform(desktop_mode);
    Ok(validator::SetupValidator::validate(platform))
}

/// Get the complete lifecycle status of the background service.
///
/// Returns a [`LifecycleStatus`] snapshot with current state, desired state,
/// recovery status, platform capabilities, and validation issues.
#[tauri::command]
async fn get_lifecycle_status<R: Runtime>(
    app: AppHandle<R>,
) -> Result<models::LifecycleStatus, String> {
    // OS service mode: route through persistent IPC client.
    #[cfg(all(feature = "desktop-service", unix))]
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        return ipc_state
            .client
            .get_lifecycle_status()
            .await
            .map_err(|e| e.to_string());
    }

    #[cfg(feature = "desktop-service")]
    let plugin_config = app.state::<PluginConfig>();

    #[cfg(feature = "desktop-service")]
    let desktop_mode = Some(plugin_config.desktop_service_mode.as_str());
    #[cfg(not(feature = "desktop-service"))]
    let desktop_mode: Option<&str> = None;

    let manager = app.state::<ServiceManagerHandle<R>>();
    let (tx, rx) = tokio::sync::oneshot::channel();
    manager
        .cmd_tx
        .send(ManagerCommand::GetLifecycleStatus {
            desktop_mode: desktop_mode.map(|s| s.to_string()),
            reply: tx,
        })
        .await
        .map_err(|e| e.to_string())?;

    rx.await.map_err(|e| e.to_string())
}

/// Configure recovery (auto-restart) for the background service.
///
/// When `enabled` is `true`, persists `desired_running=true` with an optional
/// start config (for recovery after process kill or device reboot).
/// When `enabled` is `false`, clears the recovery intent.
#[tauri::command]
async fn configure_recovery<R: Runtime>(
    app: AppHandle<R>,
    enabled: bool,
    config: Option<StartConfig>,
) -> Result<(), String> {
    if enabled {
        enable_auto_restart(app, config).await
    } else {
        disable_auto_restart(app).await
    }
}

// ─── Desktop OS Service State & Commands ──────────────────────────────────────

/// Managed state indicating OS service mode via IPC.
///
/// When present as managed state, the `start`/`stop`/`is_running` commands
/// route through the persistent IPC client instead of the in-process actor loop.
#[cfg(all(feature = "desktop-service", unix))]
struct DesktopIpcState {
    client: desktop::ipc_client::PersistentIpcClientHandle,
}

/// Set up OS-service mode: spawn the persistent IPC client, manage
/// [`DesktopIpcState`], and kick off auto-provisioning.
///
/// Called from plugin setup when `desktopServiceMode` is `"osService"` on a
/// desktop platform. After this returns, commands route through the IPC
/// client (the in-process `cmd_rx` is intentionally unused in this mode).
#[cfg(all(feature = "desktop-service", unix, not(mobile)))]
fn setup_os_service_ipc<R: Runtime>(
    app: &AppHandle<R>,
    config: &PluginConfig,
) -> Result<(), ServiceError> {
    let label = desktop::service_manager::derive_service_label(
        app,
        config.desktop_service_label.as_deref(),
    );
    let socket_path = desktop::ipc::socket_path(&label)?;
    let client = desktop::ipc_client::PersistentIpcClientHandle::spawn(
        socket_path,
        app.app_handle().clone(),
    );
    app.manage(DesktopIpcState { client });

    // BGS-12: auto-provision ONLY when the user has consented to the
    // background service. Consent OFF ⇒ no auto-provision ⇒ a manual
    // `systemctl --user disable` is never reverted on the next launch (the
    // systemd unit re-install / re-enable / start inside
    // `spawn_os_service_auto_provision` → `install_service_inner` is skipped
    // entirely). The consent record is read LIVE from disk (the plugin
    // cannot import the app crate, so it reads the stable plain-JSON
    // `background-service-consent.json` contract directly).
    let consent_dir = app.path().app_data_dir().ok().map(|d| d.join("data"));
    let allow = consent_dir
        .as_deref()
        .map(|d| should_auto_provision(d, config.desktop_start_service_if_missing))
        .unwrap_or(false);
    if allow {
        spawn_os_service_auto_provision(app.app_handle());
    } else {
        log::info!(
            "Background service: OS-service auto-provision skipped \
             (consent off or desktopStartServiceIfMissing disabled)"
        );
    }
    Ok(())
}

/// The on-disk background-service consent filename (the stable plain-JSON
/// contract owned by the app's `consent` module; the plugin reads it directly
/// because it cannot import the app crate without a circular dependency).
#[cfg(feature = "desktop-service")]
const DESKTOP_CONSENT_FILENAME: &str = "background-service-consent.json";

/// Minimal mirror of the app's `BackgroundServiceConsent` on-disk record.
///
/// Only the `enabled` bool is read; serde ignores the record's other fields
/// (`auto_unlock`, `updated_at`) by default. Default-off on a missing/corrupt
/// record, matching the app's `load()` semantics.
#[cfg(feature = "desktop-service")]
#[derive(Debug, Default, serde::Deserialize)]
struct ProvisioningConsent {
    #[serde(default)]
    enabled: bool,
}

/// Whether the persisted consent record allows the OS service to be
/// auto-provisioned (BGS-12). Provisioning gates on the master service
/// consent (`enabled`); the `auto_unlock` sub-consent governs credential
/// auto-unlock (F3 / `perform_run`), a separate concern. Default-off on a
/// missing/corrupt record ⇒ provisioning blocked until consent is recorded.
#[cfg(feature = "desktop-service")]
fn desktop_consent_allows_provisioning(data_dir: &std::path::Path) -> bool {
    let path = data_dir.join(DESKTOP_CONSENT_FILENAME);
    let record = std::fs::read_to_string(&path)
        .ok()
        .and_then(|text| serde_json::from_str::<ProvisioningConsent>(&text).ok())
        .unwrap_or_default();
    record.enabled
}

/// The full auto-provision decision (BGS-12): the config must opt in
/// (`desktop_start_service_if_missing`) AND the user must have consented
/// (`enabled`). Both gates are load-bearing — the config gate alone left a
/// silent install with no consent; the consent gate alone ignores a host's
/// `desktopStartServiceIfMissing=false`.
#[cfg(feature = "desktop-service")]
fn should_auto_provision(data_dir: &std::path::Path, start_if_missing: bool) -> bool {
    start_if_missing && desktop_consent_allows_provisioning(data_dir)
}

#[cfg(feature = "desktop-service")]
#[tauri::command]
async fn install_service<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
    install_service_inner(&app).await
}

// PRODUCT DECISION: daemon crash-loop + restart cadence (doc 08, BGS-05 Leg C).
// `RestartSec` (5s between restarts) + `StartLimitBurst`/`StartLimitIntervalSec`
// (5 restarts per 60s ⇒ systemd stops the crash-loop). `InstallOptions::default()`
// stays None for all three so tests + non-prod callers opt out; only this prod
// caller opts in. systemd-native; launchd ignores the StartLimit fields.
//
// BGS-05 re-fix (Critic Blocker 1, cfg-attribute displacement): EACH const AND
// `install_service_inner` carries its OWN `#[cfg(feature = "desktop-service")]`.
// The Step-6 original had a single cfg above the FIRST const; because `//`
// comments are trivia, the attribute bound ONLY to that first const and SILENTLY
// left `install_service_inner` + the other two consts ungated — breaking the
// default-features build (their bodies reference the cfg-gated `desktop` module
// + feature-gated `PluginConfig` fields). One cfg per item is displacement-proof.
#[cfg(feature = "desktop-service")]
const DEFAULT_RESTART_DELAY_SECS: u32 = 5;
#[cfg(feature = "desktop-service")]
const DEFAULT_START_LIMIT_BURST: u32 = 5;
#[cfg(feature = "desktop-service")]
const DEFAULT_START_LIMIT_INTERVAL_SECS: u32 = 60;

#[cfg(feature = "desktop-service")]
async fn install_service_inner<R: Runtime>(app: &AppHandle<R>) -> Result<(), String> {
    use desktop::service_manager::{derive_service_label, DesktopServiceManager};
    let plugin_config = app.state::<PluginConfig>();
    let label = derive_service_label(app, plugin_config.desktop_service_label.as_deref());
    let exec_path = std::env::current_exe().map_err(|e| e.to_string())?;

    // Validate that the executable exists and is executable.
    if !exec_path.exists() {
        return Err(format!(
            "Current executable does not exist at {}: cannot install OS service",
            exec_path.display()
        ));
    }

    // Verify the binary supports --service-label by spawning it with the flag
    // and checking for a specific exit behavior. We use a timeout to avoid
    // hanging if the binary starts a GUI.
    let validate_result = tokio::time::timeout(
        std::time::Duration::from_secs(5),
        tokio::process::Command::new(&exec_path)
            .arg("--service-label")
            .arg(&label)
            .arg("--validate-service-install")
            .output(),
    )
    .await;

    match validate_result {
        Ok(Ok(output)) => {
            let stdout = String::from_utf8_lossy(&output.stdout);
            if !stdout.trim().contains("ok") {
                return Err("Binary does not handle --validate-service-install. \
                     Ensure headless_main() is called from your app's main()."
                    .into());
            }
        }
        Ok(Err(e)) => {
            return Err(format!(
                "Failed to validate executable for --service-label: {e}"
            ));
        }
        Err(_) => {
            // Timed out — the binary probably started the GUI instead of handling
            // the service flag. Warn but don't block installation.
            log::warn!(
                "Timeout validating --service-label support. \
                 Ensure your app's main() handles the --service-label argument \
                 and calls headless_main()."
            );
        }
    }

    {
        let mgr = DesktopServiceManager::new(&label, exec_path).map_err(|e| e.to_string())?;
        use desktop::service_manager::InstallOptions;
        let options = InstallOptions {
            autostart: plugin_config.desktop_service_autostart,
            // BGS-05 Leg C: the prod daemon autostarts with a real restart cadence
            // (RestartSec) + a crash-loop cap (StartLimitBurst/Interval). The
            // struct default remains None for all three.
            restart_delay_secs: Some(DEFAULT_RESTART_DELAY_SECS),
            journal_output: true,
            log_path: None,
            start_limit_burst: Some(DEFAULT_START_LIMIT_BURST),
            start_limit_interval_secs: Some(DEFAULT_START_LIMIT_INTERVAL_SECS),
        };
        mgr.install(&options).map_err(|e| e.to_string())?;
    }

    // Nudge the persistent IPC client to skip backoff and reconnect.
    if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
        ipc_state.client.nudge_reconnect();
        let timeout =
            std::time::Duration::from_millis(plugin_config.desktop_service_start_timeout_ms);
        ipc_state.client.wait_for_connected(timeout).await.ok();
    }

    Ok(())
}

/// Auto-provision the OS service at app startup (install + start + IPC wait).
///
/// Spawned from plugin setup in OS-service mode when
/// `desktopStartServiceIfMissing` is enabled. If the daemon's IPC socket does
/// not become reachable within a short grace period, the service unit is
/// installed (idempotent) and started, then the persistent IPC client is
/// nudged to reconnect. Failures are logged; the host app keeps whatever
/// in-process fallback it set up.
#[cfg(all(feature = "desktop-service", unix, not(mobile)))]
fn spawn_os_service_auto_provision<R: Runtime>(app: &AppHandle<R>) {
    let app = app.clone();
    tauri::async_runtime::spawn(async move {
        let (start_if_missing, start_timeout_ms) = {
            let plugin_config = app.state::<PluginConfig>();
            (
                plugin_config.desktop_start_service_if_missing,
                plugin_config.desktop_service_start_timeout_ms,
            )
        };
        if !start_if_missing {
            return;
        }

        // Grace period: an already-running daemon connects almost immediately.
        if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
            match ipc_state
                .client
                .wait_for_connected(std::time::Duration::from_secs(3))
                .await
            {
                Ok(true) => {
                    log::info!("OS service already running — IPC connected");
                    return;
                }
                Ok(false) => {}
                Err(e) => log::warn!("IPC wait failed during auto-provision: {e}"),
            }
        }

        log::info!("OS service IPC unavailable — auto-provisioning (install + start)");
        if let Err(e) = install_service_inner(&app).await {
            log::warn!(
                "OS service auto-install failed: {e}; \
                 app continues with in-process fallback"
            );
            return;
        }

        // Explicitly start the unit (install may only enable autostart).
        {
            use desktop::service_manager::{derive_service_label, DesktopServiceManager};
            let plugin_config = app.state::<PluginConfig>();
            let label = derive_service_label(&app, plugin_config.desktop_service_label.as_deref());
            let exec_path = match std::env::current_exe() {
                Ok(p) => p,
                Err(e) => {
                    log::warn!("OS service auto-start failed: cannot resolve current exe: {e}");
                    return;
                }
            };
            match DesktopServiceManager::new(&label, exec_path) {
                Ok(mgr) => {
                    if let Err(e) = mgr.start() {
                        log::warn!("OS service auto-start failed: {e}");
                        return;
                    }
                }
                Err(e) => {
                    log::warn!("OS service manager unavailable: {e}");
                    return;
                }
            }
        }

        if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
            ipc_state.client.nudge_reconnect();
            let timeout = std::time::Duration::from_millis(start_timeout_ms);
            match ipc_state.client.wait_for_connected(timeout).await {
                Ok(true) => log::info!("OS service auto-provision complete — IPC connected"),
                Ok(false) => log::warn!(
                    "OS service installed and started but IPC did not connect within {}ms",
                    timeout.as_millis()
                ),
                Err(e) => log::warn!("IPC wait failed after auto-provision: {e}"),
            }
        }
    });
}

#[cfg(feature = "desktop-service")]
#[tauri::command]
async fn uninstall_service<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
    use desktop::service_manager::{derive_service_label, DesktopServiceManager};
    let plugin_config = app.state::<PluginConfig>();
    let label = derive_service_label(&app, plugin_config.desktop_service_label.as_deref());
    let exec_path = std::env::current_exe().map_err(|e| e.to_string())?;
    let mgr = DesktopServiceManager::new(&label, exec_path).map_err(|e| e.to_string())?;
    mgr.uninstall().map_err(|e| e.to_string())
}

// ─── Desktop OS Service Start/Stop/Status Commands ────────────────────────────

/// Build an [`OsServiceStatus`] from available information.
///
/// Gathers the service label, mode string, IPC connection state, socket path,
/// and optional last error into a status snapshot.
#[cfg(all(feature = "desktop-service", unix))]
fn build_os_service_status(
    label: &str,
    ipc_connected: bool,
    socket_path: Option<String>,
    last_error: Option<String>,
    native_status: Option<service_manager::ServiceStatus>,
) -> models::OsServiceStatus {
    let mode = if cfg!(target_os = "macos") {
        "launchd"
    } else {
        // DESK-01: Windows daemon was removed; this fn is now Unix-only
        // (cfg(all(feature = "desktop-service", unix))). The only other
        // supported Unix target is Linux.
        "systemd"
    };

    // DESK-02: prefer the OS-native status when available so the caller can
    // distinguish NotInstalled / Stopped / Running. Fall back to the
    // IPC-derived signal when the daemon binary cannot be constructed or
    // the status query itself failed — IPC connectivity remains a separate
    // axis reported in `ipc_connected`.
    let installed = match native_status {
        Some(service_manager::ServiceStatus::Running) => models::OsServiceInstallState::Running,
        Some(service_manager::ServiceStatus::Stopped(_)) => {
            models::OsServiceInstallState::Installed
        }
        Some(service_manager::ServiceStatus::NotInstalled) => {
            models::OsServiceInstallState::NotInstalled
        }
        None => {
            // Native status unavailable — best-effort fallback from IPC.
            if ipc_connected {
                models::OsServiceInstallState::Running
            } else {
                models::OsServiceInstallState::Installed
            }
        }
    };

    models::OsServiceStatus {
        label: label.to_string(),
        mode: mode.to_string(),
        installed,
        ipc_connected,
        socket_path,
        last_error,
    }
}

/// Start the OS-level background service (desktop only).
///
/// Delegates to [`DesktopServiceManager::start()`] (systemd, launchd, or
/// Windows SCM), then nudges the persistent IPC client to reconnect.
#[cfg(feature = "desktop-service")]
#[tauri::command]
async fn start_os_service<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
    #[cfg(unix)]
    {
        use desktop::service_manager::{derive_service_label, DesktopServiceManager};
        let plugin_config = app.state::<PluginConfig>();
        let label = derive_service_label(&app, plugin_config.desktop_service_label.as_deref());
        let exec_path = std::env::current_exe().map_err(|e| e.to_string())?;
        {
            let mgr = DesktopServiceManager::new(&label, exec_path).map_err(|e| e.to_string())?;
            mgr.start().map_err(|e| e.to_string())?;
        }

        // Nudge the persistent IPC client to skip backoff and reconnect.
        if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
            ipc_state.client.nudge_reconnect();
            let timeout =
                std::time::Duration::from_millis(plugin_config.desktop_service_start_timeout_ms);
            ipc_state.client.wait_for_connected(timeout).await.ok();
        }

        Ok(())
    }
    #[cfg(not(unix))]
    {
        let _ = app;
        Err(os_service_unsupported_platform())
    }
}

/// Stop the OS-level background service (desktop only).
///
/// Delegates to [`DesktopServiceManager::stop()`] (systemd, launchd, or
/// Windows SCM).
#[cfg(feature = "desktop-service")]
#[tauri::command]
async fn stop_os_service<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
    #[cfg(unix)]
    {
        use desktop::service_manager::{derive_service_label, DesktopServiceManager};
        let plugin_config = app.state::<PluginConfig>();
        let label = derive_service_label(&app, plugin_config.desktop_service_label.as_deref());
        let exec_path = std::env::current_exe().map_err(|e| e.to_string())?;
        let mgr = DesktopServiceManager::new(&label, exec_path).map_err(|e| e.to_string())?;
        mgr.stop().map_err(|e| e.to_string())
    }
    #[cfg(not(unix))]
    {
        let _ = app;
        Err(os_service_unsupported_platform())
    }
}

/// Restart the OS-level background service (desktop only).
///
/// Calls stop (best-effort) then start via the platform service manager.
#[cfg(feature = "desktop-service")]
#[tauri::command]
async fn restart_os_service<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
    #[cfg(unix)]
    {
        use desktop::service_manager::{derive_service_label, DesktopServiceManager};
        let plugin_config = app.state::<PluginConfig>();
        let label = derive_service_label(&app, plugin_config.desktop_service_label.as_deref());
        let exec_path = std::env::current_exe().map_err(|e| e.to_string())?;

        // DESK-05: the previous restart silently swallowed real stop errors
        // stop errors and immediately raced start against the still-
        // tearing-down previous instance. Propagate stop errors, then wait
        // boundedly for the IPC client to observe a disconnect, before
        // issuing start.
        {
            let mgr =
                DesktopServiceManager::new(&label, exec_path.clone()).map_err(|e| e.to_string())?;
            mgr.stop().map_err(|e| e.to_string())?;
        }

        // Bounded wait for the IPC client to observe the disconnect. The
        // manager itself is not Send (Box<dyn ServiceManager>), so it is
        // dropped above and we use only the Send-safe IPC client here.
        let timeout =
            std::time::Duration::from_millis(plugin_config.desktop_service_start_timeout_ms);
        if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
            ipc_state.client.nudge_reconnect();
            let deadline = std::time::Instant::now() + timeout;
            loop {
                if !ipc_state.client.is_connected() || std::time::Instant::now() >= deadline {
                    break;
                }
                tokio::time::sleep(std::time::Duration::from_millis(50)).await;
            }
        }

        // Now issue start (scoped so the non-Send manager is dropped before
        // any subsequent await keeps the future Send).
        {
            let mgr = DesktopServiceManager::new(&label, exec_path).map_err(|e| e.to_string())?;
            mgr.start().map_err(|e| e.to_string())?;
        }
        // Nudge the persistent client to skip backoff and reconnect.
        if let Some(ipc_state) = app.try_state::<DesktopIpcState>() {
            ipc_state.client.nudge_reconnect();
            ipc_state.client.wait_for_connected(timeout).await.ok();
        }

        Ok(())
    }
    #[cfg(not(unix))]
    {
        let _ = app;
        Err(os_service_unsupported_platform())
    }
}

/// Get the status of the OS-level background service (desktop only).
///
/// Returns [`OsServiceStatus`] with label, mode (systemd / launchd / scm),
/// IPC state, and socket or pipe path.
#[cfg(feature = "desktop-service")]
#[tauri::command]
async fn get_os_service_status<R: Runtime>(
    app: AppHandle<R>,
) -> Result<models::OsServiceStatus, String> {
    #[cfg(unix)]
    {
        use desktop::service_manager::{derive_service_label, DesktopServiceManager};
        let plugin_config = app.state::<PluginConfig>();
        let label = derive_service_label(&app, plugin_config.desktop_service_label.as_deref());

        let ipc_connected = app
            .try_state::<DesktopIpcState>()
            .map(|s| s.client.is_connected())
            .unwrap_or(false);

        let socket_path = desktop::ipc::socket_path(&label)
            .ok()
            .map(|p| p.to_string_lossy().to_string());

        // DESK-02: query the OS-native install/run status instead of
        // guessing `Installed` when IPC is disconnected. A status-query
        // failure is non-fatal — fall back to the IPC-only signal so the
        // command remains callable even if the daemon binary is gone.
        let native_status = std::env::current_exe()
            .and_then(|exec_path| {
                DesktopServiceManager::new(&label, exec_path)
                    .map_err(|e| std::io::Error::other(e.to_string()))
            })
            .and_then(|mgr| {
                mgr.status()
                    .map_err(|e| std::io::Error::other(e.to_string()))
            })
            .ok();

        Ok(build_os_service_status(
            &label,
            ipc_connected,
            socket_path,
            None,
            native_status,
        ))
    }
    #[cfg(not(unix))]
    {
        let _ = app;
        Err(os_service_unsupported_platform())
    }
}

/// Error string for OS-service commands on platforms with no IPC transport
/// (neither Unix domain sockets nor Windows named pipes).
#[cfg(all(feature = "desktop-service", not(unix)))]
fn os_service_unsupported_platform() -> String {
    ServiceError::Platform("OS-service mode is not supported on this platform".into()).to_string()
}

// ─── Plugin Builder ──────────────────────────────────────────────────────────

/// Create the Tauri plugin with your service factory.
///
/// ```rust,ignore
/// // MyService must implement BackgroundService<R>
/// tauri::Builder::default()
///     .plugin(tauri_plugin_background_service::init_with_service(|| MyService::new()))
/// ```
pub fn init_with_service<R, S, F>(factory: F) -> TauriPlugin<R, PluginConfig>
where
    R: Runtime,
    S: BackgroundService<R>,
    F: Fn() -> S + Send + Sync + 'static,
{
    let boxed_factory: ServiceFactory<R> = Box::new(move || Box::new(factory()));

    Builder::<R, PluginConfig>::new("background-service")
        .invoke_handler(tauri::generate_handler![
            start,
            stop,
            is_running,
            get_service_state,
            get_platform_capabilities,
            get_scheduling_status,
            get_desired_state_status,
            get_pending_bg_task,
            get_notification_permission_status,
            request_notification_permission,
            can_use_full_screen_intent,
            open_full_screen_intent_settings,
            enable_auto_restart,
            disable_auto_restart,
            get_desired_service_state,
            native_lifecycle_event,
            validate_setup,
            get_lifecycle_status,
            configure_recovery,
            request_battery_exemption,
            #[cfg(feature = "desktop-service")]
            install_service,
            #[cfg(feature = "desktop-service")]
            uninstall_service,
            #[cfg(feature = "desktop-service")]
            start_os_service,
            #[cfg(feature = "desktop-service")]
            stop_os_service,
            #[cfg(feature = "desktop-service")]
            restart_os_service,
            #[cfg(feature = "desktop-service")]
            get_os_service_status,
        ])
        .setup(move |app, api| {
            let config = api.config().clone();
            // CORE-03: validate the config once before any consumer touches
            // it. `mpsc::channel(0)` panics, and invalid Android ids / modes
            // / iOS timers reach native setup and produce confusing late
            // failures. The setup hook is the single chokepoint.
            if let Err(e) = config.validate() {
                let msg = e.to_string();
                log::error!("invalid background-service plugin config: {msg}");
                return Err(msg.into());
            }
            let (cmd_tx, cmd_rx) = tokio::sync::mpsc::channel(config.channel_capacity);
            #[cfg(mobile)]
            let mobile_cmd_tx = cmd_tx.clone();
            let handle = ServiceManagerHandle::new(cmd_tx);
            app.manage(handle);

            app.manage(config.clone());

            let ios_safety_timeout_secs = config.ios_safety_timeout_secs;
            let ios_processing_safety_timeout_secs = config.ios_processing_safety_timeout_secs;
            let ios_earliest_refresh_begin_minutes = config.ios_earliest_refresh_begin_minutes;
            let ios_earliest_processing_begin_minutes =
                config.ios_earliest_processing_begin_minutes;
            let ios_requires_external_power = config.ios_requires_external_power;
            let ios_requires_network_connectivity = config.ios_requires_network_connectivity;
            let ios_processing_ceiling_multiplier = config.ios_processing_ceiling_multiplier;
            let android_fg_service_types = config.android_foreground_service_types.clone();
            let android_validate_fg_type = config.android_validate_foreground_service_type;

            // D1: lifecycle-notification policy, derived once at spawn.
            // Defaults keep every notification off; DEC-002 suppresses the
            // Android paths already covered by native Kotlin notifications.
            let notifier_policy = NotifierPolicy::derive(&config, cfg!(target_os = "android"));
            let notify_sink: Option<Arc<dyn NotifySink>> = Some(Arc::new(Notifier {
                app: app.app_handle().clone(),
            }));

            // One authoritative Rust-backed desired-state model on every platform
            // (H4 / D1). The mobile arm was previously hardcoded `None`, so iOS
            // status lied (`desired_running` always false) and the recovery
            // commands silently no-op'd. With a `Some(...)` backend the actor
            // persists desired state in an app-data-dir file, `build_lifecycle_status`
            // reports the real desired state, and the recovery commands mirror
            // their effect into Swift `UserDefaults` + BGTask scheduling
            // (see `manager::mirror_desired_to_native` / `MobileLifecycle::mirror_desired_state`).
            let desired_state_backend: Option<Arc<dyn desired_state::DesiredStateBackend>> = {
                match app.path().app_data_dir() {
                    Ok(data_dir) => Some(Arc::new(desired_state::FileDesiredStateBackend::new(
                        data_dir,
                    ))),
                    Err(e) => {
                        log::warn!("Failed to get app data dir for desired-state persistence: {e}");
                        None
                    }
                }
            };

            // Mode dispatch: spawn in-process actor or configure IPC for OS service.
            //
            // OS-service mode is strictly a DESKTOP concept. Android and iOS are
            // `unix` targets, so the cfg below must exclude mobile: otherwise a
            // consumer that enables `desktop-service` unconditionally and sets
            // `desktopServiceMode: "osService"` would route mobile into the
            // desktop IPC path, never spawn the actor loop, drop `cmd_rx`, and
            // every ManagerCommand (including Start) would fail on a closed
            // channel — the native foreground service would never start.
            #[cfg(all(feature = "desktop-service", unix, not(mobile)))]
            if config.desktop_service_mode == "osService" {
                // OS service mode: spawn persistent IPC client.
                setup_os_service_ipc(app, &config)?;
            } else {
                // In-process mode (default): spawn the actor loop.
                let factory = boxed_factory;
                tauri::async_runtime::spawn(manager_loop(
                    cmd_rx,
                    factory,
                    ios_safety_timeout_secs,
                    ios_processing_safety_timeout_secs,
                    ios_earliest_refresh_begin_minutes,
                    ios_earliest_processing_begin_minutes,
                    ios_requires_external_power,
                    ios_requires_network_connectivity,
                    ios_processing_ceiling_multiplier,
                    desired_state_backend,
                    android_fg_service_types.clone(),
                    android_validate_fg_type,
                    notifier_policy,
                    notify_sink,
                    None,
                    false,
                ));
            }

            // Mobile: ALWAYS spawn the in-process actor, regardless of any
            // desktop-service configuration. `DesktopIpcState` is never managed
            // on mobile, so the command-level IPC early-returns are inert.
            #[cfg(all(feature = "desktop-service", unix, mobile))]
            {
                if config.desktop_service_mode == "osService" {
                    log::warn!(
                        "desktopServiceMode=osService is ignored on mobile; \
                         using the in-process service actor"
                    );
                }
                let factory = boxed_factory;
                tauri::async_runtime::spawn(manager_loop(
                    cmd_rx,
                    factory,
                    ios_safety_timeout_secs,
                    ios_processing_safety_timeout_secs,
                    ios_earliest_refresh_begin_minutes,
                    ios_earliest_processing_begin_minutes,
                    ios_requires_external_power,
                    ios_requires_network_connectivity,
                    ios_processing_ceiling_multiplier,
                    desired_state_backend,
                    android_fg_service_types.clone(),
                    android_validate_fg_type,
                    notifier_policy,
                    notify_sink,
                    None,
                    false,
                ));
            }

            // Unknown desktop platform class (neither unix nor windows): no
            // IPC transport exists, so osService mode cannot be honored.
            #[cfg(all(feature = "desktop-service", not(unix)))]
            {
                if config.desktop_service_mode == "osService" {
                    log::warn!(
                        "Desktop OS-service mode is not supported on this platform; \
                         background-service commands will fail instead of running in-process"
                    );
                    drop(cmd_rx);
                } else {
                    // On non-Unix platforms, only explicit in-process mode is available.
                    let factory = boxed_factory;
                    tauri::async_runtime::spawn(manager_loop(
                        cmd_rx,
                        factory,
                        ios_safety_timeout_secs,
                        ios_processing_safety_timeout_secs,
                        ios_earliest_refresh_begin_minutes,
                        ios_earliest_processing_begin_minutes,
                        ios_requires_external_power,
                        ios_requires_network_connectivity,
                        ios_processing_ceiling_multiplier,
                        desired_state_backend,
                        android_fg_service_types.clone(),
                        android_validate_fg_type,
                        notifier_policy,
                        notify_sink,
                        None,
                        false,
                    ));
                }
            }

            #[cfg(not(feature = "desktop-service"))]
            {
                let factory = boxed_factory;
                tauri::async_runtime::spawn(manager_loop(
                    cmd_rx,
                    factory,
                    ios_safety_timeout_secs,
                    ios_processing_safety_timeout_secs,
                    ios_earliest_refresh_begin_minutes,
                    ios_earliest_processing_begin_minutes,
                    ios_requires_external_power,
                    ios_requires_network_connectivity,
                    ios_processing_ceiling_multiplier,
                    desired_state_backend,
                    android_fg_service_types,
                    android_validate_fg_type,
                    notifier_policy,
                    notify_sink,
                    None,
                    false,
                ));
            }

            #[cfg(mobile)]
            {
                let lifecycle = mobile::init(app, api)?;
                let lifecycle_arc = Arc::new(lifecycle);

                // Send SetMobile to actor so keepalive is managed by the actor.
                let mobile_trait: Arc<dyn MobileKeepalive> = lifecycle_arc.clone();
                if let Err(e) = mobile_cmd_tx.try_send(ManagerCommand::SetMobile {
                    mobile: mobile_trait,
                }) {
                    log::error!("Failed to send SetMobile command: {e}");
                }

                // Store for iOS callbacks and Android auto-start helpers.
                app.manage(lifecycle_arc);
            }

            // iOS: auto-start when launched by OS for a pending BGTask.
            // The native bridge calls are spawned after setup returns so Swift
            // can service their main-queue work while Tauri continues startup.
            #[cfg(target_os = "ios")]
            {
                ios_spawn_cold_auto_start(app);

                // H14: listen for BGTasks delivered to the warm process and
                // start the Rust service for each (the cold block above only
                // runs once at launch).
                ios_spawn_warm_listener(app);
            }

            Ok(())
        })
        .on_event(|app, event| {
            if let tauri::RunEvent::Exit = event {
                // Android foreground service mode owns its lifecycle outside the
                // Activity. Closing the UI must not stop the background Core.
                #[cfg(target_os = "android")]
                {
                    let _ = app;
                    return;
                }

                #[cfg(not(target_os = "android"))]
                {
                    // In OS service mode, the service runs in a separate process — skip.
                    #[cfg(all(feature = "desktop-service", unix))]
                    if app.try_state::<DesktopIpcState>().is_some() {
                        return;
                    }
                    let manager = app.state::<ServiceManagerHandle<R>>();
                    // H2: on iOS, app `Exit` is OS-driven backgrounding/termination,
                    // not a user stop. Route it through `ProcessExit` so desired
                    // state + the BGTask schedule survive (recovery resumes
                    // delivery). On desktop, closing the app is a genuine stop.
                    #[cfg(target_os = "ios")]
                    let stop_result =
                        manager.stop_blocking_with_reason(crate::models::StopReason::ProcessExit);
                    #[cfg(not(target_os = "ios"))]
                    let stop_result = manager.stop_blocking();
                    if let Err(e) = stop_result {
                        log::warn!("Failed to stop background service on app exit: {e}");
                    }
                }
            }
        })
        .build()
}

#[cfg(test)]
#[allow(clippy::field_reassign_with_default)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    /// Minimal service for testing type compatibility.
    struct DummyService;

    #[async_trait]
    impl BackgroundService<tauri::Wry> for DummyService {
        async fn init(&mut self, _ctx: &ServiceContext<tauri::Wry>) -> Result<(), ServiceError> {
            Ok(())
        }

        async fn run(&mut self, _ctx: &ServiceContext<tauri::Wry>) -> Result<(), ServiceError> {
            Ok(())
        }
    }

    // ── Construction Tests ───────────────────────────────────────────────

    #[test]
    fn service_manager_handle_constructs() {
        let (cmd_tx, _cmd_rx) = tokio::sync::mpsc::channel(16);
        let _handle: ServiceManagerHandle<tauri::Wry> = ServiceManagerHandle::new(cmd_tx);
    }

    #[test]
    fn factory_produces_boxed_service() {
        let factory: ServiceFactory<tauri::Wry> = Box::new(|| Box::new(DummyService));
        let _service: Box<dyn BackgroundService<tauri::Wry>> = factory();
    }

    #[test]
    fn handle_factory_creates_fresh_instances() {
        let count = Arc::new(AtomicUsize::new(0));
        let count_clone = count.clone();

        let factory: ServiceFactory<tauri::Wry> = Box::new(move || {
            count_clone.fetch_add(1, Ordering::SeqCst);
            Box::new(DummyService)
        });

        let _ = (factory)();
        let _ = (factory)();

        assert_eq!(count.load(Ordering::SeqCst), 2);
    }

    // ── Compile-time Tests ───────────────────────────────────────────────

    /// Verify `init_with_service` returns `TauriPlugin<R>`.
    #[allow(dead_code)]
    fn init_with_service_returns_tauri_plugin<R: Runtime, S, F>(
        factory: F,
    ) -> TauriPlugin<R, PluginConfig>
    where
        S: BackgroundService<R>,
        F: Fn() -> S + Send + Sync + 'static,
    {
        init_with_service(factory)
    }

    /// Verify `start` command signature is generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn start_command_signature<R: Runtime>(
        app: AppHandle<R>,
        config: StartConfig,
    ) -> Result<(), String> {
        start(app, config).await
    }

    /// Verify `stop` command signature is generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn stop_command_signature<R: Runtime>(app: AppHandle<R>) -> Result<(), String> {
        stop(app).await
    }

    /// Verify `is_running` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn is_running_command_signature<R: Runtime>(app: AppHandle<R>) -> bool {
        is_running(app).await
    }

    /// Verify `get_service_state` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn get_service_state_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<models::ServiceStatus, String> {
        get_service_state(app).await
    }

    /// Verify `get_scheduling_status` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn get_scheduling_status_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<models::IOSSchedulingStatus, String> {
        get_scheduling_status(app).await
    }

    /// Verify `get_desired_state_status` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn get_desired_state_status_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<models::IOSDesiredStateStatus, String> {
        get_desired_state_status(app).await
    }

    /// Verify `get_pending_bg_task` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn get_pending_bg_task_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<Option<models::PendingTaskInfo>, String> {
        get_pending_bg_task(app).await
    }

    /// Verify `get_notification_permission_status` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn get_notification_permission_status_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<String, String> {
        get_notification_permission_status(app).await
    }

    /// Verify `request_notification_permission` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn request_notification_permission_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<String, String> {
        request_notification_permission(app).await
    }

    /// Verify `can_use_full_screen_intent` command signature is async and generic over `R: Runtime`.
    /// Return type mirrors the command (`Result<serde_json::Value, String>` — the `{canUse: bool}`
    /// object shape; see the command's IPC SHAPE CONTRACT doc).
    #[allow(dead_code)]
    async fn can_use_full_screen_intent_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<serde_json::Value, String> {
        can_use_full_screen_intent(app).await
    }

    /// Verify `open_full_screen_intent_settings` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn open_full_screen_intent_settings_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<(), String> {
        open_full_screen_intent_settings(app).await
    }

    /// Verify `enable_auto_restart` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn enable_auto_restart_command_signature<R: Runtime>(
        app: AppHandle<R>,
        config: Option<StartConfig>,
    ) -> Result<(), String> {
        enable_auto_restart(app, config).await
    }

    /// Verify `disable_auto_restart` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn disable_auto_restart_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<(), String> {
        disable_auto_restart(app).await
    }

    /// Verify `get_desired_service_state` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn get_desired_service_state_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<Option<desired_state::DesiredState>, String> {
        get_desired_service_state(app).await
    }

    /// Verify `validate_setup` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn validate_setup_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<models::SetupValidationReport, String> {
        validate_setup(app).await
    }

    /// Verify `native_lifecycle_event` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn native_lifecycle_event_command_signature<R: Runtime>(
        app: AppHandle<R>,
        event: models::NativeLifecycleEvent,
    ) -> Result<(), String> {
        native_lifecycle_event(app, event).await
    }

    /// Verify `get_lifecycle_status` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn get_lifecycle_status_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<models::LifecycleStatus, String> {
        get_lifecycle_status(app).await
    }

    /// Verify `configure_recovery` command signature is async and generic over `R: Runtime`.
    #[allow(dead_code)]
    async fn configure_recovery_command_signature<R: Runtime>(
        app: AppHandle<R>,
        enabled: bool,
        config: Option<StartConfig>,
    ) -> Result<(), String> {
        configure_recovery(app, enabled, config).await
    }

    // ── Desktop IPC State Tests ─────────────────────────────────────────

    /// Verify PersistentIpcClientHandle can be constructed.
    #[cfg(all(feature = "desktop-service", unix))]
    #[tokio::test]
    async fn desktop_ipc_state_with_persistent_client() {
        use desktop::ipc_client::PersistentIpcClientHandle;
        let app = tauri::test::mock_app();
        let path = std::path::PathBuf::from("/tmp/test-persistent-client.sock");
        let client = PersistentIpcClientHandle::spawn(path, app.handle().clone());
        // The client is spawned but may not be connected yet — that's fine.
        // Just verify we can construct the state.
        let _state = DesktopIpcState { client };
    }

    /// AC2: a config with `desktopServiceMode: "osService"` on the current
    /// platform constructs the IPC-client state — `DesktopIpcState` is managed
    /// so commands route through IPC instead of failing on a closed channel.
    #[cfg(all(feature = "desktop-service", unix, not(mobile)))]
    #[tokio::test]
    async fn os_service_mode_constructs_ipc_state() {
        let app = tauri::test::mock_app();
        let handle = app.handle();
        let config = PluginConfig {
            desktop_service_mode: "osService".into(),
            ..Default::default()
        };
        // The auto-provision task reads PluginConfig from managed state.
        handle.manage(config.clone());

        setup_os_service_ipc(handle, &config).expect("osService IPC setup should succeed");

        assert!(
            handle.try_state::<DesktopIpcState>().is_some(),
            "DesktopIpcState must be managed in osService mode"
        );
    }

    // ── BGS-12: consent gate on OS-service auto-provisioning ──────────────

    /// The OS service auto-provisions ONLY when the config opts in AND the
    /// user has consented to the background service. This is the AC decision
    /// predicate; the spawn itself is undrivable in a unit test (real IPC +
    /// service-manager), so it is pinned by the include_str static gate below.
    #[cfg(feature = "desktop-service")]
    #[test]
    fn bgs12_no_autoprovision_without_consent() {
        let temp = tempfile::tempdir().unwrap();
        let dir = temp.path();

        // No consent record ⇒ default off ⇒ NO auto-provision, even when the
        // config opts in. NV-MUT (neuter the helper to return `true`) → RED.
        assert!(
            !should_auto_provision(dir, true),
            "consent off (no record): must NOT auto-provision even if start_if_missing=true"
        );

        // Consent ON (enabled) ⇒ auto-provision allowed when config opts in.
        std::fs::write(
            dir.join(DESKTOP_CONSENT_FILENAME),
            serde_json::json!({"enabled": true, "auto_unlock": true, "updated_at": 1}).to_string(),
        )
        .unwrap();
        assert!(
            should_auto_provision(dir, true),
            "consent on + start_if_missing: auto-provision allowed"
        );

        // start_if_missing=false ⇒ NO auto-provision even with consent (a
        // host config that opts out is respected; a manual disable sticks).
        assert!(
            !should_auto_provision(dir, false),
            "start_if_missing=false: must NOT auto-provision"
        );
    }

    /// Provisioning gates on `enabled` (the master service consent), NOT on
    /// `auto_unlock` (the credential auto-unlock sub-consent — a separate
    /// concern owned by the F3 / `perform_run` gates). A corrupt record
    /// defaults off.
    #[cfg(feature = "desktop-service")]
    #[test]
    fn bgs12_provisioning_gates_on_enabled_not_auto_unlock() {
        let temp = tempfile::tempdir().unwrap();
        let dir = temp.path();
        // enabled=true, auto_unlock=false ⇒ service consented ⇒ provision OK.
        std::fs::write(
            dir.join(DESKTOP_CONSENT_FILENAME),
            serde_json::json!({"enabled": true, "auto_unlock": false}).to_string(),
        )
        .unwrap();
        assert!(
            should_auto_provision(dir, true),
            "enabled alone (service consent) must allow provisioning"
        );

        // Corrupt record ⇒ default off ⇒ no provisioning.
        std::fs::write(dir.join(DESKTOP_CONSENT_FILENAME), b"not json {{{").unwrap();
        assert!(
            !should_auto_provision(dir, true),
            "corrupt consent record must default off (no provisioning)"
        );
    }

    /// include_str! static gate (mem-1783121224-667 pattern): pin that
    /// `setup_os_service_ipc` wires the consent gate before the
    /// auto-provision spawn. Runtime concat so the asserted token never
    /// appears verbatim on this line (defeats self-referential false-pinning).
    #[cfg(feature = "desktop-service")]
    #[test]
    fn bgs12_setup_os_service_ipc_wires_consent_gate() {
        let src = include_str!("lib.rs");
        let call = ["should_auto", "_provision("].concat();
        assert!(
            src.contains(&call[..]),
            "setup_os_service_ipc must call the consent-gate helper before spawning auto-provision"
        );
        // Pin the else branch (the skip). The literal is split so it never
        // appears verbatim in this test source (defeats self-referential
        // false-pinning against `include_str!("lib.rs")`).
        let skip = ["OS-service auto-", "provision skipped"].concat();
        assert!(
            src.contains(&skip[..]),
            "setup_os_service_ipc must skip the spawn when consent is off (the else branch)"
        );
    }

    /// BGS-21 (doc-08 Step 12) Rust-side reachability pin (mem-1783277823-8dae):
    /// the two notification-permission commands must be (1) declared in build.rs
    /// `COMMANDS` (drives permission-token autogeneration), (2) registered in
    /// `generate_handler!` (JS reachability — Tauri v2 JS invokes resolve only
    /// against registered Rust commands), and (3) bridged in mobile.rs via
    /// `run_mobile_plugin` with the EXACT camelCase Kotlin @Command names.
    /// Dropping any single axis silently ships a dead JS binding that fails at
    /// runtime (command-not-found / capability denial) — the mem-1783371281-d310
    /// class. NV-MUT: drop a build.rs entry / a generate_handler! registration /
    /// change a run_mobile_plugin name ⇒ this test REDs on the matching axis.
    /// The same-file (lib.rs) registration token is built by runtime concat so
    /// the asserted string never appears verbatim on this line (mem-1783121224-667
    /// self-reference guard); the build.rs + mobile.rs axes are cross-file ⇒ a
    /// direct `contains` is safe.
    #[test]
    fn bgs21_notification_permission_bridge_registered_and_wired() {
        // (1) build.rs COMMANDS entries (cross-file ⇒ direct contains is safe).
        let build_rs = include_str!("../build.rs");
        assert!(
            build_rs.contains("\"get_notification_permission_status\""),
            "get_notification_permission_status must be listed in build.rs COMMANDS"
        );
        assert!(
            build_rs.contains("\"request_notification_permission\""),
            "request_notification_permission must be listed in build.rs COMMANDS"
        );

        // (2) generate_handler! registration (SAME file ⇒ runtime concat so the
        // asserted name+comma token does not appear verbatim in this source).
        let src = include_str!("lib.rs");
        let get_reg = ["get_notification_permission", "_status,"].concat();
        let req_reg = ["request_notification_permiss", "ion,"].concat();
        assert!(
            src.contains(&get_reg[..]),
            "get_notification_permission_status must be registered in generate_handler!"
        );
        assert!(
            src.contains(&req_reg[..]),
            "request_notification_permission must be registered in generate_handler!"
        );

        // (3) mobile.rs bridges via run_mobile_plugin with the exact camelCase
        // Kotlin @Command names (cross-file ⇒ direct contains is safe).
        let mobile_rs = include_str!("mobile.rs");
        assert!(
            mobile_rs.contains("\"getNotificationPermissionStatus\""),
            "mobile.rs must bridge getNotificationPermissionStatus via run_mobile_plugin"
        );
        assert!(
            mobile_rs.contains("\"requestNotificationPermission\""),
            "mobile.rs must bridge requestNotificationPermission via run_mobile_plugin"
        );
    }

    /// BGS-22 (doc-08 Step 14): the `request_battery_exemption` JS binding is
    /// reachable on ALL FOUR production axes (mem-1783371281-d310): (1) declared
    /// in build.rs COMMANDS, (2) registered in `generate_handler!`, (3) bridged
    /// in mobile.rs via `run_mobile_plugin` with the EXACT camelCase Kotlin
    /// `requestBatteryExemption` name, and (4) covered by an `allow-request-
    /// battery-exemption` permission token in both the autogenerated command
    /// table and the default permission set. Dropping any single axis silently
    /// ships a dead JS binding that fails at runtime (command-not-found /
    /// capability denial). NV-MUT: drop a build.rs entry / a generate_handler!
    /// registration / change a run_mobile_plugin name / remove the permission
    /// token ⇒ this test REDs on the matching axis. The same-file (lib.rs)
    /// registration token is built by runtime concat so the asserted string
    /// never appears verbatim on this line (mem-1783121224-667 self-reference
    /// guard); the cross-file axes (build.rs + mobile.rs + permissions) ⇒ a
    /// direct `contains` is safe.
    #[test]
    fn bgs22_battery_exemption_bridge_registered_and_wired() {
        // (1) build.rs COMMANDS entry (cross-file ⇒ direct contains is safe).
        let build_rs = include_str!("../build.rs");
        assert!(
            build_rs.contains("\"request_battery_exemption\""),
            "request_battery_exemption must be listed in build.rs COMMANDS"
        );

        // (2) generate_handler! registration (SAME file ⇒ runtime concat so the
        // asserted name+comma token does not appear verbatim in this source).
        let src = include_str!("lib.rs");
        let reg = ["request_battery_exempt", "ion,"].concat();
        assert!(
            src.contains(&reg[..]),
            "request_battery_exemption must be registered in generate_handler!"
        );

        // (3) mobile.rs bridges via run_mobile_plugin with the exact camelCase
        // Kotlin @Command name (cross-file ⇒ direct contains is safe).
        let mobile_rs = include_str!("mobile.rs");
        assert!(
            mobile_rs.contains("\"requestBatteryExemption\""),
            "mobile.rs must bridge requestBatteryExemption via run_mobile_plugin"
        );

        // (4) permission token: an autogenerated allow-request-battery-exemption
        // command token exists AND is in the default permission set (cross-file
        // ⇒ direct contains is safe). Without both, the JS invoke is capability-
        // denied at runtime even though the command compiles + is registered.
        let default_toml = include_str!("../permissions/default.toml");
        assert!(
            default_toml.contains("\"allow-request-battery-exemption\""),
            "allow-request-battery-exemption must be in permissions/default.toml"
        );
        let cmd_toml =
            include_str!("../permissions/autogenerated/commands/request_battery_exemption.toml");
        assert!(
            cmd_toml.contains("\"request_battery_exemption\""),
            "permissions/autogenerated/commands/request_battery_exemption.toml must allow request_battery_exemption"
        );
    }
    /// ACL-01: `get_desired_state_status` (iOS desired-state mirror) must be
    /// reachable on all four production axes (mem-1783371281-d310): (1) declared
    /// in build.rs COMMANDS, (2) registered in `generate_handler!`, (3) bridged
    /// in mobile.rs via `run_mobile_plugin` with the EXACT camelCase Swift/Kotlin
    /// `getDesiredStateStatus` name, and (4) covered by an
    /// `allow-get-desired-state-status` permission token in both the
    /// autogenerated command table and the default permission set. Dropping any
    /// single axis silently ships a dead JS binding that fails at runtime.
    #[test]
    fn acl01_get_desired_state_status_reachable_on_all_four_axes() {
        // (1) build.rs COMMANDS entry (cross-file ⇒ direct contains is safe).
        let build_rs = include_str!("../build.rs");
        assert!(
            build_rs.contains("\"get_desired_state_status\""),
            "get_desired_state_status must be listed in build.rs COMMANDS"
        );

        // (2) generate_handler! registration (SAME file ⇒ runtime concat so
        // the asserted name+comma token does not appear verbatim here).
        let src = include_str!("lib.rs");
        let reg = ["get_desired_state_stat", "us,"].concat();
        assert!(
            src.contains(&reg[..]),
            "get_desired_state_status must be registered in generate_handler!"
        );

        // (3) mobile.rs bridges via run_mobile_plugin with the exact camelCase
        // name (cross-file ⇒ direct contains is safe).
        let mobile_rs = include_str!("mobile.rs");
        assert!(
            mobile_rs.contains("\"getDesiredStateStatus\""),
            "mobile.rs must bridge getDesiredStateStatus via run_mobile_plugin"
        );

        // (4) permission token: autogenerated allow-get-desired-state-status
        // command token AND default permission entry (cross-file ⇒ direct
        // contains is safe). Without both, the JS invoke is capability-denied
        // at runtime even though the command compiles + is registered.
        let default_toml = include_str!("../permissions/default.toml");
        assert!(
            default_toml.contains("\"allow-get-desired-state-status\""),
            "allow-get-desired-state-status must be in permissions/default.toml"
        );
        let cmd_toml =
            include_str!("../permissions/autogenerated/commands/get_desired_state_status.toml");
        assert!(
            cmd_toml.contains("\"get_desired_state_status\""),
            "permissions/autogenerated/commands/get_desired_state_status.toml must allow get_desired_state_status"
        );
    }

    // ── Desktop Command Compile-time Tests ────────────────────────────────

    /// Verify `install_service` command signature is generic over `R: Runtime`.
    #[cfg(feature = "desktop-service")]
    #[allow(dead_code)]
    async fn install_service_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<(), String> {
        install_service(app).await
    }

    /// Verify `uninstall_service` command signature is generic over `R: Runtime`.
    #[cfg(feature = "desktop-service")]
    #[allow(dead_code)]
    async fn uninstall_service_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<(), String> {
        uninstall_service(app).await
    }

    /// Verify `start_os_service` command signature is generic over `R: Runtime`.
    #[cfg(feature = "desktop-service")]
    #[allow(dead_code)]
    async fn start_os_service_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<(), String> {
        start_os_service(app).await
    }

    /// Verify `stop_os_service` command signature is generic over `R: Runtime`.
    #[cfg(feature = "desktop-service")]
    #[allow(dead_code)]
    async fn stop_os_service_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<(), String> {
        stop_os_service(app).await
    }

    /// Verify `restart_os_service` command signature is generic over `R: Runtime`.
    #[cfg(feature = "desktop-service")]
    #[allow(dead_code)]
    async fn restart_os_service_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<(), String> {
        restart_os_service(app).await
    }

    // ── DESK-05: restart propagates stop errors and waits before start ──

    #[cfg(all(feature = "desktop-service", unix))]
    #[test]
    fn desk05_restart_does_not_swallow_stop_error() {
        // The old restart swallowed stop errors and raced start — both
        // now. `stop()` must propagate via `?` and a bounded wait must
        // separate stop from start.
        let src = include_str!("lib.rs");
        let restart_body = src
            .split("async fn restart_os_service")
            .nth(1)
            .and_then(|rest| rest.split("\n}").next())
            .expect("restart_os_service body must be present");
        assert!(
            !restart_body.contains("mgr.stop().ok()"),
            "DESK-05: restart must not swallow stop errors with .ok()"
        );
        assert!(
            restart_body.contains("mgr.stop().map_err"),
            "DESK-05: restart must propagate stop errors via ?"
        );
        assert!(
            restart_body.contains("deadline") || restart_body.contains("wait_for_connected"),
            "DESK-05: restart must wait boundedly between stop and start"
        );
    }

    #[cfg(feature = "desktop-service")]
    #[allow(dead_code)]
    async fn get_os_service_status_command_signature<R: Runtime>(
        app: AppHandle<R>,
    ) -> Result<models::OsServiceStatus, String> {
        get_os_service_status(app).await
    }

    // ── Desktop OS Service Command Routing Tests ──────────────────────────

    /// Test that `build_os_service_status` produces a valid OsServiceStatus
    /// with the correct fields populated.
    #[cfg(all(feature = "desktop-service", unix))]
    #[test]
    fn build_os_service_status_populates_fields() {
        let status = build_os_service_status(
            "com.example.bg-service",
            true,
            Some("/tmp/test.sock".to_string()),
            None,
            None,
        );
        assert_eq!(status.label, "com.example.bg-service");
        assert!(status.ipc_connected);
        assert_eq!(status.socket_path.as_deref(), Some("/tmp/test.sock"));
        assert!(status.last_error.is_none());
    }

    /// Test that `build_os_service_status` includes the correct mode string.
    #[cfg(all(feature = "desktop-service", unix))]
    #[test]
    fn build_os_service_status_mode_is_correct() {
        let status = build_os_service_status("test", false, None, None, None);
        #[cfg(target_os = "linux")]
        assert_eq!(status.mode, "systemd");
        #[cfg(target_os = "macos")]
        assert_eq!(status.mode, "launchd");
        // DESK-01: no Windows mode branch anymore.
    }

    // ── DESK-02: native ServiceStatus mapped into OsServiceInstallState ──

    #[cfg(all(feature = "desktop-service", unix))]
    #[test]
    fn desk02_native_running_maps_to_running() {
        let status = build_os_service_status(
            "test",
            false,
            None,
            None,
            Some(service_manager::ServiceStatus::Running),
        );
        assert_eq!(status.installed, models::OsServiceInstallState::Running);
    }

    #[cfg(all(feature = "desktop-service", unix))]
    #[test]
    fn desk02_native_stopped_maps_to_installed() {
        // Installed but not running — the daemon binary is registered with
        // the OS service manager but not currently executing. The caller
        // can see ipc_connected=false to confirm it's not responding.
        let status = build_os_service_status(
            "test",
            false,
            None,
            None,
            Some(service_manager::ServiceStatus::Stopped(None)),
        );
        assert_eq!(status.installed, models::OsServiceInstallState::Installed);
    }

    #[cfg(all(feature = "desktop-service", unix))]
    #[test]
    fn desk02_native_not_installed_is_reachable() {
        // DESK-02 headline: NotInstalled is no longer unreachable. Previously
        // `build_os_service_status` always returned Installed/Running; now the
        // native status is the source of truth when available.
        let status = build_os_service_status(
            "test",
            false,
            None,
            None,
            Some(service_manager::ServiceStatus::NotInstalled),
        );
        assert_eq!(
            status.installed,
            models::OsServiceInstallState::NotInstalled,
            "NotInstalled must surface when the native manager reports it"
        );
    }

    #[cfg(all(feature = "desktop-service", unix))]
    #[test]
    fn desk02_native_unavailable_falls_back_to_ipc_signal() {
        // When the daemon binary cannot be constructed or the status query
        // fails, fall back to the IPC-derived signal.
        let running = build_os_service_status("test", true, None, None, None);
        assert_eq!(running.installed, models::OsServiceInstallState::Running);
        let stopped = build_os_service_status("test", false, None, None, None);
        assert_eq!(stopped.installed, models::OsServiceInstallState::Installed);
    }

    // ── On-Event Shutdown Compile-time Test ─────────────────────────────────

    /// Verify the on_event closure accessing ServiceManagerHandle<R> from managed
    /// state type-checks. Ensures the generic R is properly threaded through in
    /// the on_event context where stop_blocking() is called synchronously.
    #[allow(dead_code)]
    fn on_event_shutdown_closure_type_checks<R: Runtime>(_app: &AppHandle<R>) {
        let _closure = |_app: &AppHandle<R>, event: &tauri::RunEvent| {
            if let tauri::RunEvent::Exit = event {
                let manager = _app.state::<ServiceManagerHandle<R>>();
                if let Err(_e) = manager.stop_blocking() {
                    log::warn!("bg service shutdown on exit failed: {_e}");
                }
            }
        };
    }

    // ── Cancel Listener Tests ───────────────────────────────────────────────

    use crate::manager::ManagerCommand;
    use std::sync::atomic::AtomicBool;

    /// Helper: spawn a background task that accepts one StopWithReason command and replies Ok(()).
    /// Returns a oneshot receiver that yields Some(reason) if StopWithReason was received.
    fn spawn_stop_drain(
        mut cmd_rx: tokio::sync::mpsc::Receiver<ManagerCommand<tauri::test::MockRuntime>>,
    ) -> tokio::sync::oneshot::Receiver<Option<crate::models::StopReason>> {
        let (seen_tx, seen_rx) =
            tokio::sync::oneshot::channel::<Option<crate::models::StopReason>>();
        tokio::spawn(async move {
            let result =
                tokio::time::timeout(std::time::Duration::from_secs(2), cmd_rx.recv()).await;
            match result {
                Ok(Some(ManagerCommand::StopWithReason { reason, reply })) => {
                    let _ = reply.send(Ok(()));
                    let _ = seen_tx.send(Some(reason));
                }
                _ => {
                    let _ = seen_tx.send(None);
                }
            }
        });
        seen_rx
    }

    #[tokio::test]
    async fn cancel_listener_resolved_invoke_sends_stop_with_reason() {
        let (cmd_tx, cmd_rx) = tokio::sync::mpsc::channel(16);
        let seen = spawn_stop_drain(cmd_rx);

        // wait_fn returns Ok(()) → simulates resolved invoke (safety timer / expiration)
        let stop_sent = run_cancel_listener(
            Box::new(|| Ok(())),
            Box::new(|| {}),
            cmd_tx,
            5, // timeout, shouldn't matter since wait_fn returns immediately
        )
        .await;

        assert!(stop_sent, "resolved invoke should return true");
        let reason = seen.await.unwrap();
        assert_eq!(
            reason,
            Some(crate::models::StopReason::PlatformExpiration),
            "StopWithReason(PlatformExpiration) should be sent on resolved invoke"
        );
    }

    #[tokio::test]
    async fn cancel_listener_rejected_invoke_no_stop() {
        let (cmd_tx, cmd_rx) = tokio::sync::mpsc::channel(16);
        let seen = spawn_stop_drain(cmd_rx);

        // wait_fn returns Err → simulates rejected invoke (explicit stop / completion)
        let stop_sent = run_cancel_listener(
            Box::new(|| Err(ServiceError::Platform("rejected".into()))),
            Box::new(|| {}),
            cmd_tx,
            5,
        )
        .await;

        assert!(!stop_sent, "rejected invoke should return false");
        assert_eq!(
            seen.await.unwrap(),
            None,
            "StopWithReason should NOT be sent on rejected invoke"
        );
    }

    #[tokio::test]
    async fn cancel_listener_timeout_sends_stop_with_reason() {
        let (cmd_tx, cmd_rx) = tokio::sync::mpsc::channel(16);
        let cancel_called = Arc::new(AtomicBool::new(false));
        let cancel_called_clone = cancel_called.clone();
        let seen = spawn_stop_drain(cmd_rx);

        // Use a channel to unblock the wait_fn when cancel_fn is called,
        // simulating how the real cancelCancelListener rejects the invoke.
        let (unblock_tx, unblock_rx) = std::sync::mpsc::channel::<()>();

        let stop_sent = run_cancel_listener(
            Box::new(move || {
                // Block until cancel_fn signals us (simulates wait_for_cancel blocking)
                let _ = unblock_rx.recv();
                Ok(())
            }),
            Box::new(move || {
                cancel_called_clone.store(true, Ordering::SeqCst);
                let _ = unblock_tx.send(());
            }),
            cmd_tx,
            0, // immediate timeout
        )
        .await;

        assert!(stop_sent, "timeout should return true");
        assert!(
            cancel_called.load(Ordering::SeqCst),
            "cancel_fn should be called on timeout"
        );
        let reason = seen.await.unwrap();
        assert_eq!(
            reason,
            Some(crate::models::StopReason::PlatformTimeout),
            "StopWithReason(PlatformTimeout) should be sent on timeout"
        );
    }

    #[tokio::test]
    async fn cancel_listener_join_error_no_stop() {
        let (cmd_tx, cmd_rx) = tokio::sync::mpsc::channel(16);
        let seen = spawn_stop_drain(cmd_rx);

        // wait_fn panics → simulates JoinError from spawn_blocking
        let stop_sent = run_cancel_listener(
            Box::new(|| panic!("simulated panic in wait_for_cancel")),
            Box::new(|| {}),
            cmd_tx,
            5,
        )
        .await;

        // JoinError is Ok(Err(_)) which falls into the `_ => false` branch
        assert!(!stop_sent, "join error should return false (no stop sent)");
        assert_eq!(
            seen.await.unwrap(),
            None,
            "StopWithReason should NOT be sent on join error"
        );
    }

    // ── iOS Cold Auto-Start Tests (H3) ──────────────────────────────────────

    /// Helper: spawn a background task that accepts one `Start` command and
    /// replies with `reply_with`. Returns a receiver that yields `true` if a
    /// `Start` command was observed.
    fn spawn_start_drain(
        mut cmd_rx: tokio::sync::mpsc::Receiver<ManagerCommand<tauri::test::MockRuntime>>,
        reply_with: Result<(), ServiceError>,
    ) -> tokio::sync::oneshot::Receiver<bool> {
        let (seen_tx, seen_rx) = tokio::sync::oneshot::channel::<bool>();
        tokio::spawn(async move {
            let result =
                tokio::time::timeout(std::time::Duration::from_secs(2), cmd_rx.recv()).await;
            match result {
                Ok(Some(ManagerCommand::Start { reply, .. })) => {
                    let _ = reply.send(reply_with);
                    let _ = seen_tx.send(true);
                }
                _ => {
                    let _ = seen_tx.send(false);
                }
            }
        });
        seen_rx
    }

    /// AC2 (H3): a successful auto-start consumes the pending BGTask exactly once
    /// and never records a failure marker.
    #[tokio::test]
    async fn auto_start_success_consumes_pending_exactly_once() {
        let app = tauri::test::mock_app();
        let (cmd_tx, cmd_rx) = tokio::sync::mpsc::channel(16);
        let seen = spawn_start_drain(cmd_rx, Ok(()));

        let cleared = Arc::new(AtomicUsize::new(0));
        let failed = Arc::new(AtomicUsize::new(0));
        let cleared_c = cleared.clone();
        let failed_c = failed.clone();

        let started = run_auto_start(
            StartConfig::default(),
            app.handle().clone(),
            cmd_tx,
            Box::new(move || {
                cleared_c.fetch_add(1, Ordering::SeqCst);
            }),
            Box::new(move || {
                failed_c.fetch_add(1, Ordering::SeqCst);
            }),
        )
        .await;

        assert!(started, "successful Start should return true");
        assert!(seen.await.unwrap(), "Start command should be received");
        assert_eq!(
            cleared.load(Ordering::SeqCst),
            1,
            "pending must be consumed exactly once on success"
        );
        assert_eq!(
            failed.load(Ordering::SeqCst),
            0,
            "no failure marker on success"
        );
    }

    /// AC1 (H3): a forced `Start` failure preserves the pending evidence — the
    /// clear is NOT called — and records a failure marker.
    #[tokio::test]
    async fn auto_start_failure_preserves_pending_and_marks_failure() {
        let app = tauri::test::mock_app();
        let (cmd_tx, cmd_rx) = tokio::sync::mpsc::channel(16);
        let seen = spawn_start_drain(
            cmd_rx,
            Err(ServiceError::Platform("forced start failure".into())),
        );

        let cleared = Arc::new(AtomicUsize::new(0));
        let failed = Arc::new(AtomicUsize::new(0));
        let cleared_c = cleared.clone();
        let failed_c = failed.clone();

        let started = run_auto_start(
            StartConfig::default(),
            app.handle().clone(),
            cmd_tx,
            Box::new(move || {
                cleared_c.fetch_add(1, Ordering::SeqCst);
            }),
            Box::new(move || {
                failed_c.fetch_add(1, Ordering::SeqCst);
            }),
        )
        .await;

        assert!(!started, "failed Start should return false");
        assert!(seen.await.unwrap(), "Start command should be received");
        assert_eq!(
            cleared.load(Ordering::SeqCst),
            0,
            "pending must be PRESERVED on failure (clear not called)"
        );
        assert_eq!(
            failed.load(Ordering::SeqCst),
            1,
            "failure marker must be recorded exactly once on failure"
        );
    }

    /// H3 edge: if the actor channel is closed before `Start` is delivered, the
    /// pending evidence is preserved (failure path), not silently consumed.
    #[tokio::test]
    async fn auto_start_channel_closed_preserves_pending() {
        let app = tauri::test::mock_app();
        let (cmd_tx, cmd_rx) = tokio::sync::mpsc::channel::<ManagerCommand<_>>(16);
        // Drop the receiver so the send fails immediately.
        drop(cmd_rx);

        let cleared = Arc::new(AtomicUsize::new(0));
        let failed = Arc::new(AtomicUsize::new(0));
        let cleared_c = cleared.clone();
        let failed_c = failed.clone();

        let started = run_auto_start(
            StartConfig::default(),
            app.handle().clone(),
            cmd_tx,
            Box::new(move || {
                cleared_c.fetch_add(1, Ordering::SeqCst);
            }),
            Box::new(move || {
                failed_c.fetch_add(1, Ordering::SeqCst);
            }),
        )
        .await;

        assert!(!started, "closed channel should return false");
        assert_eq!(
            cleared.load(Ordering::SeqCst),
            0,
            "pending must be preserved when the command never sends"
        );
        assert_eq!(
            failed.load(Ordering::SeqCst),
            1,
            "failure marker recorded when the command channel is closed"
        );
    }

    // ── iOS Warm Auto-Start Tests (H14, M14 part 2) ─────────────────────────
    //
    // A BGTask delivered to a *warm/idle* process must actually start the Rust
    // service (H14), mirroring the cold auto-start sequence: pre-check
    // AlreadyRunning → re-send SetOnComplete → Start → consume pending on
    // success. A warm delivery to an already-running actor is a clean no-op
    // (no double-start, no failure marker, pending NOT consumed) (M14 part 2).

    /// Service whose `run()` blocks until cancelled — keeps `is_running` true so
    /// the "warm delivery while running" no-op path can be exercised.
    struct WarmBlockingService;

    #[async_trait]
    impl BackgroundService<tauri::test::MockRuntime> for WarmBlockingService {
        async fn init(
            &mut self,
            _ctx: &ServiceContext<tauri::test::MockRuntime>,
        ) -> Result<(), ServiceError> {
            Ok(())
        }
        async fn run(
            &mut self,
            ctx: &ServiceContext<tauri::test::MockRuntime>,
        ) -> Result<(), ServiceError> {
            ctx.shutdown.cancelled().await;
            Ok(())
        }
    }

    /// Service that completes `run()` immediately with success — used to prove the
    /// captured `on_complete` callback fires (vs the iOS safety timer).
    struct WarmQuickService;

    #[async_trait]
    impl BackgroundService<tauri::test::MockRuntime> for WarmQuickService {
        async fn init(
            &mut self,
            _ctx: &ServiceContext<tauri::test::MockRuntime>,
        ) -> Result<(), ServiceError> {
            Ok(())
        }
        async fn run(
            &mut self,
            _ctx: &ServiceContext<tauri::test::MockRuntime>,
        ) -> Result<(), ServiceError> {
            Ok(())
        }
    }

    /// Spawn a real manager actor (mirrors `manager::tests::setup_manager`) so the
    /// warm-start `AlreadyRunning` pre-check and `on_complete` arming run against
    /// genuine actor state rather than a fake drain.
    fn spawn_real_manager(
        factory: crate::manager::ServiceFactory<tauri::test::MockRuntime>,
    ) -> tokio::sync::mpsc::Sender<ManagerCommand<tauri::test::MockRuntime>> {
        let (cmd_tx, cmd_rx) = tokio::sync::mpsc::channel(16);
        tokio::spawn(manager_loop(
            cmd_rx,
            factory,
            28.0,
            0.0,
            15.0,
            15.0,
            false,
            false,
            4.0,
            None,
            vec!["remoteMessaging".into()],
            true,
            NotifierPolicy::default(),
            None,
            None,
            false,
        ));
        cmd_tx
    }

    async fn warm_is_running(
        cmd_tx: &tokio::sync::mpsc::Sender<ManagerCommand<tauri::test::MockRuntime>>,
    ) -> bool {
        let (tx, rx) = tokio::sync::oneshot::channel();
        cmd_tx
            .send(ManagerCommand::IsRunning { reply: tx })
            .await
            .unwrap();
        rx.await.unwrap()
    }

    fn noop_on_complete() -> OnCompleteCallback {
        Box::new(|_success| {})
    }

    /// AC1 (H14): a warm BGTask delivered to an idle actor with desired_running
    /// starts the service (`is_running` flips true) and consumes the pending
    /// record exactly once.
    #[tokio::test]
    async fn warm_start_idle_starts_actor_and_consumes_pending() {
        let app = tauri::test::mock_app();
        let cmd_tx = spawn_real_manager(Box::new(|| Box::new(WarmBlockingService)));

        let consumed = Arc::new(AtomicUsize::new(0));
        let failed = Arc::new(AtomicUsize::new(0));
        let consumed_c = consumed.clone();
        let failed_c = failed.clone();

        let started = run_warm_start(
            StartConfig::default(),
            app.handle().clone(),
            cmd_tx.clone(),
            noop_on_complete(),
            Box::new(move || {
                consumed_c.fetch_add(1, Ordering::SeqCst);
            }),
            Box::new(move || {
                failed_c.fetch_add(1, Ordering::SeqCst);
            }),
        )
        .await;

        assert!(
            started,
            "warm delivery to idle actor should start the service"
        );
        assert!(
            warm_is_running(&cmd_tx).await,
            "is_running should flip true after warm start"
        );
        assert_eq!(
            consumed.load(Ordering::SeqCst),
            1,
            "pending must be consumed exactly once on warm success"
        );
        assert_eq!(
            failed.load(Ordering::SeqCst),
            0,
            "no failure marker on success"
        );
    }

    /// AC2 (M14 part 2): a warm delivery to an already-running actor is a clean
    /// no-op — it does NOT double-start, does NOT record a failure marker, and
    /// does NOT consume the pending record a second time.
    #[tokio::test]
    async fn warm_start_while_running_is_noop() {
        let app = tauri::test::mock_app();
        let cmd_tx = spawn_real_manager(Box::new(|| Box::new(WarmBlockingService)));

        let consumed = Arc::new(AtomicUsize::new(0));
        let failed = Arc::new(AtomicUsize::new(0));

        // First warm delivery starts the service.
        let consumed_1 = consumed.clone();
        let failed_1 = failed.clone();
        let first = run_warm_start(
            StartConfig::default(),
            app.handle().clone(),
            cmd_tx.clone(),
            noop_on_complete(),
            Box::new(move || {
                consumed_1.fetch_add(1, Ordering::SeqCst);
            }),
            Box::new(move || {
                failed_1.fetch_add(1, Ordering::SeqCst);
            }),
        )
        .await;
        assert!(first, "first warm delivery should start the service");

        // Second warm delivery, while running, must be a clean no-op.
        let consumed_2 = consumed.clone();
        let failed_2 = failed.clone();
        let second = run_warm_start(
            StartConfig::default(),
            app.handle().clone(),
            cmd_tx.clone(),
            noop_on_complete(),
            Box::new(move || {
                consumed_2.fetch_add(1, Ordering::SeqCst);
            }),
            Box::new(move || {
                failed_2.fetch_add(1, Ordering::SeqCst);
            }),
        )
        .await;

        assert!(
            !second,
            "warm delivery while running should be a no-op (false)"
        );
        assert!(
            warm_is_running(&cmd_tx).await,
            "service should still be running after the no-op warm delivery"
        );
        assert_eq!(
            consumed.load(Ordering::SeqCst),
            1,
            "pending consumed exactly once across both deliveries"
        );
        assert_eq!(
            failed.load(Ordering::SeqCst),
            0,
            "a no-op warm delivery must NOT record a failure marker"
        );
    }

    /// AC1 (H14): after a warm start the captured `on_complete` callback fires
    /// (proving SetOnComplete was re-sent and captured at spawn), not the iOS
    /// safety timer.
    #[tokio::test]
    async fn warm_start_arms_captured_on_complete_callback() {
        let app = tauri::test::mock_app();
        let cmd_tx = spawn_real_manager(Box::new(|| Box::new(WarmQuickService)));

        let fired = Arc::new(AtomicBool::new(false));
        let fired_cb = fired.clone();
        let on_complete: OnCompleteCallback = Box::new(move |success| {
            if success {
                fired_cb.store(true, Ordering::SeqCst);
            }
        });

        let started = run_warm_start(
            StartConfig::default(),
            app.handle().clone(),
            cmd_tx.clone(),
            on_complete,
            Box::new(|| {}),
            Box::new(|| {}),
        )
        .await;
        assert!(started, "warm start should initiate the service");

        // Wait for the immediately-completing service to fire the captured callback.
        let mut armed = false;
        for _ in 0..50 {
            if fired.load(Ordering::SeqCst) {
                armed = true;
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        }
        assert!(
            armed,
            "captured on_complete callback must fire after warm start (SetOnComplete re-sent)"
        );
    }

    /// H3/M14: a genuine `Start` failure on the warm path preserves the pending
    /// evidence (clear not called) and records a failure marker — distinct from
    /// the AlreadyRunning no-op.
    #[tokio::test]
    async fn warm_start_failure_preserves_pending_and_marks_failure() {
        let app = tauri::test::mock_app();
        let (cmd_tx, cmd_rx) =
            tokio::sync::mpsc::channel::<ManagerCommand<tauri::test::MockRuntime>>(16);

        // Drain: reply IsRunning=false, swallow SetOnComplete, fail the Start.
        tokio::spawn(async move {
            let mut rx = cmd_rx;
            while let Ok(Some(cmd)) =
                tokio::time::timeout(std::time::Duration::from_secs(2), rx.recv()).await
            {
                match cmd {
                    ManagerCommand::IsRunning { reply } => {
                        let _ = reply.send(false);
                    }
                    ManagerCommand::SetOnComplete { .. } => {}
                    ManagerCommand::Start { reply, .. } => {
                        let _ = reply.send(Err(ServiceError::Platform("forced".into())));
                        break;
                    }
                    _ => {}
                }
            }
        });

        let consumed = Arc::new(AtomicUsize::new(0));
        let failed = Arc::new(AtomicUsize::new(0));
        let consumed_c = consumed.clone();
        let failed_c = failed.clone();

        let started = run_warm_start(
            StartConfig::default(),
            app.handle().clone(),
            cmd_tx,
            noop_on_complete(),
            Box::new(move || {
                consumed_c.fetch_add(1, Ordering::SeqCst);
            }),
            Box::new(move || {
                failed_c.fetch_add(1, Ordering::SeqCst);
            }),
        )
        .await;

        assert!(!started, "forced warm start failure should return false");
        assert_eq!(
            consumed.load(Ordering::SeqCst),
            0,
            "pending must be PRESERVED on genuine failure (clear not called)"
        );
        assert_eq!(
            failed.load(Ordering::SeqCst),
            1,
            "failure marker recorded exactly once on genuine failure"
        );
    }

    // ═══════════════════════════════════════════════════════════════════════
    //  IPC AUTO-START RECOVERY TESTS (Step 12)
    // ═══════════════════════════════════════════════════════════════════════

    // Unix-only: these tests bind real Unix domain sockets via `test_helpers`.
    #[cfg(all(feature = "desktop-service", unix))]
    mod ipc_auto_start_tests {
        use super::*;
        use crate::desktop::ipc_client::PersistentIpcClientHandle;
        use crate::desktop::test_helpers::setup_server;
        use std::time::Duration;

        /// Verify that `wait_for_connected` returns `false` when the timeout
        /// expires without a server, and that the error message includes
        /// the socket path.
        #[tokio::test]
        async fn wait_for_connected_timeout_returns_false() {
            let app = tauri::test::mock_app();
            let path = crate::desktop::test_helpers::unique_socket_path();
            let handle = PersistentIpcClientHandle::spawn(path.clone(), app.handle().clone());

            let connected = handle
                .wait_for_connected(Duration::from_millis(200))
                .await
                .unwrap();
            assert!(!connected, "should return false on timeout");

            let _ = std::fs::remove_file(&path);
        }

        /// Verify that `wait_for_connected` returns `true` once a server
        /// appears and the persistent client connects.
        #[tokio::test]
        async fn wait_for_connected_succeeds_with_server() {
            let (path, shutdown, _event_tx) = setup_server();
            let app = tauri::test::mock_app();
            let handle = PersistentIpcClientHandle::spawn(path, app.handle().clone());

            let connected = handle
                .wait_for_connected(Duration::from_secs(5))
                .await
                .unwrap();
            assert!(connected, "should connect within timeout");

            shutdown.cancel();
        }

        /// Verify that `socket_path()` returns the path the handle was
        /// spawned with.
        #[tokio::test]
        async fn socket_path_accessor() {
            let app = tauri::test::mock_app();
            let path = crate::desktop::test_helpers::unique_socket_path();
            let handle = PersistentIpcClientHandle::spawn(path.clone(), app.handle().clone());
            assert_eq!(
                handle.socket_path(),
                &path,
                "socket_path() should return the path passed to spawn"
            );
            let _ = std::fs::remove_file(&path);
        }

        /// Verify the disconnected path with `desktop_start_service_if_missing=false`
        /// returns an IPC error containing "ipcUnavailable".
        ///
        /// This tests the `start` command handler's disconnected branch
        /// by directly checking the error construction logic.
        #[tokio::test]
        async fn start_disconnected_without_auto_start_returns_ipc_error() {
            let err = ServiceError::Ipc("ipcUnavailable".into());
            let msg = err.to_string();
            assert!(
                msg.contains("ipcUnavailable"),
                "error should contain 'ipcUnavailable': {msg}"
            );
        }

        /// Verify the timeout error includes the socket path for diagnostics.
        #[tokio::test]
        async fn start_timeout_error_includes_socket_path() {
            let socket = "/tmp/test-socket-path.sock";
            let err = ServiceError::Ipc(format!("ipcUnavailable: socket {socket}"));
            let msg = err.to_string();
            assert!(
                msg.contains(socket),
                "error should contain socket path: {msg}"
            );
        }
    }
}