podlib-gui 0.1.0

GUI for managing music libraries on USB-connected iPods (Nano 7G and classic click-wheel), built with egui/eframe
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
//! Library worker: the persistent thread that owns the library backend.
//!
//! WHY a worker thread instead of keeping the backend in UI state: the
//! UI thread must never block, and `podlib::open_library` returns a
//! `Box<dyn LibraryBackend>`, which is not `Send`. Such a value can
//! neither cross threads nor be wrapped in `Mutex<..>` inside UI state
//! (`Mutex<T>` is `Send` only when `T: Send`), and reopening the library
//! on every mutation would re-parse the whole iTunesDB twice per
//! operation — too slow for large libraries. The minimal `Send`-safe
//! design is therefore one long-lived worker thread that owns the
//! backend: the UI sends plain-data commands over an mpsc channel, each
//! with its own reply channel, and drains replies with `try_recv`.

use std::any::Any;
use std::path::PathBuf;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread::JoinHandle;

use podlib::{
    assign_album_track_numbers, BackendError, BatchEditOutcome, LibraryBackend, Playlist,
    PlaylistDraft, PlaylistId, Track, TrackChanges, TrackDraft, TrackId,
};

/// One command to the library worker; plain data, so it is `Send` even
/// though the backend it drives is not.
#[derive(Debug, Clone)]
pub enum LibraryCommand {
    /// Full re-read of tracks and playlists.
    Refresh,
    /// Adds host-side audio files already mapped into drafts.
    AddTracks(Vec<TrackDraft>),
    RemoveTrack { id: TrackId, purge_media: bool },
    EditTrack { id: TrackId, changes: TrackChanges },
    /// Batch tag edit: assigns one artist to every listed track.
    /// Per-id failures do not stop the batch — the honest tail in the
    /// summary reports each one (id/title + reason).
    SetArtist { track_ids: Vec<TrackId>, artist: String },
    CreatePlaylist { name: String },
    DeletePlaylist { id: PlaylistId },
    /// Renames a user playlist in place (id and membership untouched).
    RenamePlaylist { id: PlaylistId, name: String },
    AddToPlaylist { playlist: PlaylistId, track_ids: Vec<TrackId> },
    RemoveFromPlaylist { playlist: PlaylistId, track_ids: Vec<TrackId> },
    /// Drops the backend so the DB is closed before the device unmounts.
    Close,
}

/// Full library listing; a fresh copy is taken after every operation
/// (KISS: full re-list instead of tracking deltas).
#[derive(Debug, Clone, Default)]
pub struct LibrarySnapshot {
    pub tracks: Vec<Track>,
    pub playlists: Vec<Playlist>,
}

impl LibrarySnapshot {
    pub fn track(&self, id: TrackId) -> Option<&Track> {
        self.tracks.iter().find(|track| track.id == id)
    }

    pub fn playlist(&self, id: PlaylistId) -> Option<&Playlist> {
        self.playlists.iter().find(|playlist| playlist.id == id)
    }
}

/// Worker → UI reply for one command (or for the initial open).
#[derive(Debug)]
pub enum LibraryOutcome {
    /// The backend opened and the initial listing succeeded.
    Loaded(LibrarySnapshot),
    /// An operation succeeded; `summary` is a ready-made status notice
    /// (for `edit_track` it surfaces the id change required by contract:
    /// edits are remove-and-re-add, so the id may rotate).
    Applied {
        snapshot: LibrarySnapshot,
        summary: String,
    },
    /// `Close` executed: the backend is dropped and the worker exits.
    Closed,
    Failed {
        message: String,
        hint: Option<String>,
    },
}

type LibraryReply = Sender<LibraryOutcome>;

/// Handle to the worker thread. `Clone`-free on purpose: the UI holds
/// exactly one, and eject moves it into the eject thread.
pub struct LibraryWorker {
    requests: Option<Sender<(LibraryCommand, LibraryReply)>>,
    handle: Option<JoinHandle<()>>,
}

impl LibraryWorker {
    /// Spawns the worker; the backend is opened inside the spawned
    /// thread (`Box<dyn LibraryBackend>` is not `Send`, so it must never
    /// exist on the UI thread). The returned receiver yields exactly one
    /// message: `Loaded` or `Failed` for the initial open.
    pub fn spawn(mount: PathBuf) -> (Self, Receiver<LibraryOutcome>) {
        let (requests, incoming) = channel::<(LibraryCommand, LibraryReply)>();
        let (opened_tx, opened_rx) = channel();
        // No `.expect()` here (round-3 rule: none outside tests): a
        // failed thread spawn surfaces as an honest Failed outcome and
        // a dead worker whose `request()` reports "не активен".
        // Clone the open sender: the closure owns one copy, the error
        // arm below still has the original to report the failure.
        let spawned = std::thread::Builder::new()
            .name("podlib-library".to_owned())
            .spawn({
                let opened = opened_tx.clone();
                move || worker_loop(mount, incoming, opened)
            });
        match spawned {
            Ok(handle) => (
                Self {
                    requests: Some(requests),
                    handle: Some(handle),
                },
                opened_rx,
            ),
            Err(error) => {
                let _ = opened_tx.send(LibraryOutcome::Failed {
                    message: format!("Не удалось запустить поток библиотеки: {error}"),
                    hint: Some("Подключите устройство заново.".to_owned()),
                });
                (Self {
                    requests: None,
                    handle: None,
                }, opened_rx)
            }
        }
    }

    /// Test-only twin of [`Self::spawn`]: runs the command loop over a
    /// backend built inside the spawned thread (a `Box<dyn
    /// LibraryBackend>` is not `Send`, so — exactly like in production —
    /// it must never cross a thread boundary). Lets tests drive
    /// behaviour a real device fixture cannot reproduce (a backend
    /// panicking mid-command).
    #[cfg(test)]
    fn spawn_with_backend(
        make_backend: impl FnOnce() -> Box<dyn LibraryBackend> + Send + 'static,
    ) -> (Self, Receiver<LibraryOutcome>) {
        let (requests, incoming) = channel::<(LibraryCommand, LibraryReply)>();
        // No open outcome is produced here; the sender dies at once and
        // the receiver stays unread (tests drive commands directly).
        let (_, opened_rx) = channel();
        let handle = std::thread::Builder::new()
            .name("podlib-library-test".to_owned())
            .spawn(move || serve_loop(make_backend(), incoming))
            .expect("spawn test library worker thread");
        (
            Self {
                requests: Some(requests),
                handle: Some(handle),
            },
            opened_rx,
        )
    }

    /// Sends one command and returns the reply channel for it. Fails
    /// when the worker is gone (open failed earlier, or it exited).
    pub fn request(
        &self,
        command: LibraryCommand,
    ) -> Result<Receiver<LibraryOutcome>, String> {
        let requests = self
            .requests
            .as_ref()
            .ok_or_else(|| "воркер библиотеки не активен".to_owned())?;
        let (reply_tx, reply_rx) = channel();
        requests
            .send((command, reply_tx))
            .map(|_| reply_rx)
            .map_err(|_| "воркер библиотеки не активен".to_owned())
    }

    /// Blocking close and join. Used by the eject thread: the backend
    /// must be closed while the device is still mounted, so `Close` is
    /// awaited before the unmount sequence starts.
    pub fn close_and_join(mut self) {
        if let Some(requests) = self.requests.take() {
            let (reply_tx, reply_rx) = channel();
            let _ = requests.send((LibraryCommand::Close, reply_tx));
            let _ = reply_rx.recv();
        }
        if let Some(handle) = self.handle.take() {
            let _ = handle.join();
        }
    }
}

impl Drop for LibraryWorker {
    fn drop(&mut self) {
        // Dropping the request sender makes the worker's recv() fail, so
        // it finishes the in-flight command (mutations persist before
        // returning) and drops the backend. No blocking join: the UI
        // thread must not wait on a potentially slow persist.
        self.requests.take();
    }
}

/// Worker body: open → initial listing → command loop.
fn worker_loop(
    mount: PathBuf,
    incoming: Receiver<(LibraryCommand, LibraryReply)>,
    opened: Sender<LibraryOutcome>,
) {
    let backend = match podlib::open_library(&mount) {
        Ok(backend) => backend,
        Err(error) => {
            let _ = opened.send(LibraryOutcome::Failed {
                message: format!("Том смонтирован, но библиотека не открылась: {error}"),
                hint: Some(
                    "Проверьте, что на устройстве есть библиотека iTunes, и подключите заново."
                        .to_owned(),
                ),
            });
            return; // the request channel closes: the UI sees a dead worker
        }
    };
    let first = match list_snapshot(backend.as_ref()) {
        Ok(snapshot) => LibraryOutcome::Loaded(snapshot),
        Err(error) => {
            let (message, hint) = failure_from(error);
            let _ = opened.send(LibraryOutcome::Failed { message, hint });
            return;
        }
    };
    if opened.send(first).is_err() {
        return; // UI went away before the open finished
    }
    serve_loop(backend, incoming);
}

/// Command loop against an already-open backend. Split from
/// `worker_loop` so tests can inject a mock backend (a backend panicking
/// mid-command cannot be reproduced through a real device fixture).
fn serve_loop(
    mut backend: Box<dyn LibraryBackend>,
    incoming: Receiver<(LibraryCommand, LibraryReply)>,
) {
    while let Ok((command, reply)) = incoming.recv() {
        // Round 5.3 diagnostics (permanent): every command and outcome
        // lands in podlib-gui.log — the «тихо ничего не сделал» class
        // of reports must be answerable from the log alone.
        eprintln!("[worker] cmd={command:?}");
        if matches!(command, LibraryCommand::Close) {
            eprintln!("[worker] outcome=closed");
            drop(backend);
            let _ = reply.send(LibraryOutcome::Closed);
            return;
        }
        // catch_unwind: a panicking backend must still produce a reply,
        // otherwise the UI would wait forever. A panic leaves the backend
        // in unknown state (a persist may have been cut in half), so the
        // panic reply is the last thing this worker serves: dropping the
        // backend closes its files, and later requests fail on the dead
        // channel, which the UI reports as a disconnected worker.
        let outcome = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            dispatch(backend.as_mut(), command)
        })) {
            Ok(outcome) => outcome,
            Err(payload) => {
                // Stop serving honestly (see the WHY above): no further
                // command may reach a backend of unknown state.
                let _ = reply.send(LibraryOutcome::Failed {
                    message: format!(
                        "Внутренняя ошибка библиотеки: {}",
                        panic_message(payload)
                    ),
                    hint: Some(
                        "Состояние библиотеки после сбоя неизвестно; отключите и подключите \
                         устройство заново."
                            .to_owned(),
                    ),
                });
                drop(backend);
                return;
            }
        };
        match &outcome {
            LibraryOutcome::Applied { summary, .. } => {
                eprintln!("[worker] outcome=applied summary={summary:?}");
            }
            LibraryOutcome::Loaded(snapshot) => {
                eprintln!(
                    "[worker] outcome=loaded tracks={} playlists={}",
                    snapshot.tracks.len(),
                    snapshot.playlists.len()
                );
            }
            LibraryOutcome::Failed { message, .. } => {
                eprintln!("[worker] outcome=failed message={message:?}");
            }
            LibraryOutcome::Closed => eprintln!("[worker] outcome=closed"),
        }
        if reply.send(outcome).is_err() {
            return; // UI closed
        }
    }
}

/// Reads the whole library (both listings share one snapshot).
fn list_snapshot(backend: &dyn LibraryBackend) -> Result<LibrarySnapshot, BackendError> {
    Ok(LibrarySnapshot {
        tracks: backend.tracks()?,
        playlists: backend.playlists()?,
    })
}

/// Executes one command against the backend and produces the reply.
/// Mutations re-list the library on success so the UI always renders the
/// state the backend persisted.
fn dispatch(backend: &mut dyn LibraryBackend, command: LibraryCommand) -> LibraryOutcome {
    match command {
        LibraryCommand::Refresh => match list_snapshot(backend) {
            Ok(snapshot) => LibraryOutcome::Loaded(snapshot),
            Err(error) => failure_outcome(error),
        },
        LibraryCommand::AddTracks(drafts) => add_tracks_command(backend, drafts),
        LibraryCommand::RemoveTrack { id, purge_media } => match backend.remove_track(id, purge_media) {
            Ok(_) => finish(backend, "Трек удалён".to_owned()),
            Err(error) => failure_outcome(error),
        },
        LibraryCommand::EditTrack { id, changes } => edit_track_command(backend, id, changes),
        LibraryCommand::SetArtist {
            track_ids,
            artist,
        } => set_artist_command(backend, track_ids, artist),
        LibraryCommand::CreatePlaylist { name } => match PlaylistDraft::new(&name, Vec::new()) {
            Ok(draft) => match backend.create_playlist(&draft) {
                Ok(_) => finish(backend, format!("Создан плейлист «{name}»")),
                Err(error) => failure_outcome(error),
            },
            Err(error) => failure_outcome(error.into()),
        },
        LibraryCommand::DeletePlaylist { id } => match backend.delete_playlist(id) {
            Ok(_) => finish(backend, "Плейлист удалён".to_owned()),
            Err(error) => failure_outcome(error),
        },
        LibraryCommand::RenamePlaylist { id, name } => match backend.rename_playlist(id, &name) {
            // The id never rotates (rename is in place), so the notice
            // needs no id-change tail.
            Ok(_) => finish(backend, format!("Плейлист переименован в «{name}»")),
            Err(error) => failure_outcome(error),
        },
        LibraryCommand::AddToPlaylist {
            playlist,
            track_ids,
        } => match backend.add_to_playlist(playlist, &track_ids) {
            // Duplicates are ignored by contract, so the notice speaks of
            // requested rows, not of stored ones.
            Ok(_) => finish(
                backend,
                format!(
                    "Добавлено в плейлист: {} {}",
                    track_ids.len(),
                    plural(track_ids.len(), "трек", "трека", "треков")
                ),
            ),
            Err(error) => failure_outcome(error),
        },
        LibraryCommand::RemoveFromPlaylist {
            playlist,
            track_ids,
        } => match backend.remove_from_playlist(playlist, &track_ids) {
            Ok(_) => finish(backend, "Убрано из плейлиста".to_owned()),
            Err(error) => failure_outcome(error),
        },
        LibraryCommand::Close => LibraryOutcome::Closed, // handled in the loop
    }
}

/// Adds a batch of drafts; firmware-unplayable formats are transcoded
/// first (round 5.2), passthrough drafts get their real duration
/// probed (round 6.1 B1), and a success re-lists the library.
fn add_tracks_command(
    backend: &mut dyn LibraryBackend,
    drafts: Vec<TrackDraft>,
) -> LibraryOutcome {
    let mut drafts = drafts;
    // Round 6.1 B2: drafts resolving to one device album (chapters of
    // a book) get 1..N numbering and the shared count before writing.
    assign_album_track_numbers(&mut drafts);
    let total = drafts.len();
    // Round 5.2: firmware-unplayable formats (flac/ogg/oga) are
    // transcoded to AAC in this worker thread; the batch tempdir
    // lives until the batch ends and cleans itself up on drop.
    let transcode_dir = match tempfile::tempdir() {
        Ok(dir) => Some(dir),
        Err(error) => {
            return failure_outcome(BackendError::Io(std::io::Error::other(error)));
        }
    };
    let mut added = 0usize;
    let mut stopped = None;
    let mut add_failures: Vec<String> = Vec::new();
    for (index, draft) in drafts.iter().enumerate() {
        let prepared;
        if needs_transcode(&path_extension(&draft.source_path)) {
            if let Some(dir) = transcode_dir.as_ref().map(|dir| dir.path()) {
                match transcode_draft(draft, dir, index) {
                    Ok(mut transcoded) => {
                        // Round 6.1 B1 on the transcode path: the
                        // transcoded file is fresh ffmpeg output, so
                        // its duration is still the DnD zero — probe
                        // the real one here. Same gate as passthrough
                        // (a caller-supplied duration is trusted) and
                        // the same honest skip on a failed probe.
                        if transcoded.duration_ms == 0 {
                            match probe_duration_ms(&transcoded.source_path) {
                                Ok(duration_ms) => transcoded.duration_ms = duration_ms,
                                Err(reason) => {
                                    add_failures
                                        .push(format!("«{}»: {reason}", draft.title));
                                    continue;
                                }
                            }
                        }
                        prepared = Some(transcoded);
                    }
                    Err(reason) => {
                        // The batch continues: the failed file is
                        // reported honestly in the summary tail.
                        add_failures.push(format!("«{}»: {reason}", draft.title));
                        continue;
                    }
                }
            } else {
                // Unreachable while the batch tempdir owns the path, but
                // the draft is skipped honestly instead of panicking.
                add_failures.push(format!("«{}»: нет временной папки транскода", draft.title));
                continue;
            }
        } else if draft.duration_ms == 0 {
            // Round 6.1 B1: a passthrough file's duration comes from
            // nowhere else — probe it here. The iPod refuses to play a
            // track whose total_time_ms stayed 0 (the live mp3-book
            // failure), so a failed probe skips the draft honestly
            // instead of adding a silent zero.
            match probe_duration_ms(&draft.source_path) {
                Ok(duration_ms) => {
                    let mut probed = draft.clone();
                    probed.duration_ms = duration_ms;
                    prepared = Some(probed);
                }
                Err(reason) => {
                    add_failures.push(format!("«{}»: {reason}", draft.title));
                    continue;
                }
            }
        } else {
            // The caller already supplied a duration — trust it and
            // skip the probe.
            prepared = None;
        }
        let target = prepared.as_ref().unwrap_or(draft);
        match backend.add_track(target) {
            Ok(_) => added += 1,
            Err(error) => {
                stopped = Some(error);
                break;
            }
        }
    }
    let failures_tail = add_failures_summary(&add_failures);
    match stopped {
        None if added == total && add_failures.is_empty() => {
            finish(backend, added_tracks_summary(added))
        }
        // Honest partial success without a backend error: some
        // drafts failed their transcode or their duration probe
        // and were skipped.
        None => {
            let mut summary =
                format!("{} из {total}", added_tracks_summary(added));
            summary.push_str(&failures_tail);
            finish(backend, summary)
        }
        Some(error) if added == 0 && add_failures.is_empty() => {
            failure_outcome(error)
        }
        // Partial success: show honestly how far it got.
        Some(error) => match list_snapshot(backend) {
            Ok(snapshot) => {
                let mut summary = format!(
                    "{} из {total}; остановлено: {error}",
                    added_tracks_summary(added)
                );
                summary.push_str(&failures_tail);
                LibraryOutcome::Applied { snapshot, summary }
            }
            Err(_) => failure_outcome(error),
        },
    }
}

/// Applies tag edits to one track and re-lists on success.
fn edit_track_command(
    backend: &mut dyn LibraryBackend,
    id: TrackId,
    changes: TrackChanges,
) -> LibraryOutcome {
    match backend.edit_track(id, &changes) {
        Ok(track) => {
            // Contract: the resulting id is authoritative; edits are
            // remove-and-re-add, so it usually changes — surface it.
            let summary = if track.id == id {
                format!("Обновлено: «{}»", track.title)
            } else {
                format!("Обновлено: «{}» (новый id: {})", track.title, track.id)
            };
            finish(backend, summary)
        }
        Err(error) => failure_outcome(error),
    }
}

/// Batch «Задать исполнителя»: one backend batch call (on libopod
/// backends a single edit session — N×replace_track, one stage+install,
/// instead of N full device installs). The backend itself skips tracks
/// that already carry the target artist ([`BatchEditOutcome::Unchanged`]
/// — pid does not rotate, re-runs are free), so the worker only counts
/// the buckets and reports each honestly. Titles for the failure tail
/// come from the pre-edit listing: per-id backend failures name opaque
/// ids only.
fn set_artist_command(
    backend: &mut dyn LibraryBackend,
    track_ids: Vec<TrackId>,
    artist: String,
) -> LibraryOutcome {
    let total = track_ids.len();
    let titles: std::collections::BTreeMap<TrackId, String> = match list_snapshot(backend) {
        Ok(snapshot) => snapshot
            .tracks
            .into_iter()
            .map(|track| (track.id, track.title))
            .collect(),
        Err(error) => return failure_outcome(error),
    };
    let outcomes = match backend.set_artist_batch(&track_ids, &artist) {
        Ok(outcomes) => outcomes,
        Err(error) => return failure_outcome(error),
    };
    let mut updated = 0_usize;
    let mut unchanged = 0_usize;
    let mut failures: Vec<String> = Vec::new();
    for (id, outcome) in &outcomes {
        match outcome {
            BatchEditOutcome::Updated(_) => updated += 1,
            BatchEditOutcome::Unchanged => unchanged += 1,
            BatchEditOutcome::Failed(reason) => {
                let title =
                    titles.get(id).map_or_else(|| id.to_string(), |title| format!("«{title}»"));
                failures.push(format!("{title}: {reason}"));
            }
        }
    }
    if updated == 0 && !failures.is_empty() {
        let (message, hint) = if failures.len() == 1 {
            (failures[0].clone(), None)
        } else {
            (
                format!(
                    "Задать исполнителя не удалось ни одному треку ({} из {total}): {}",
                    failures.len(),
                    failures.join("; ")
                ),
                Some("Список мог устареть — нажмите «Обновить».".to_owned()),
            )
        };
        return LibraryOutcome::Failed { message, hint };
    }
    let unchanged_tail =
        if unchanged > 0 { format!("; уже с исполнителем: {unchanged}") } else { String::new() };
    let summary = if updated == 0 {
        // Everything already carried the artist: an honest «уже задан»
        // instead of a fabricated success count (failures are handled
        // above, so `unchanged` covers the whole batch).
        format!(
            "Исполнитель уже задан: {} {}",
            unchanged,
            plural(unchanged, "трек", "трека", "треков")
        )
    } else if updated == total {
        format!(
            "Исполнитель задан: {} {}{unchanged_tail}",
            total,
            plural(total, "трек", "трека", "треков")
        )
    } else {
        let mut summary = format!("Исполнитель задан: {updated} из {total}{unchanged_tail}");
        if !failures.is_empty() {
            summary.push_str("; задать не удалось: ");
            summary.push_str(&failures.join("; "));
        }
        summary
    };
    finish(backend, summary)
}

/// Re-lists after a successful mutation and wraps the fresh snapshot.
fn finish(backend: &mut dyn LibraryBackend, summary: String) -> LibraryOutcome {
    match list_snapshot(backend) {
        Ok(snapshot) => LibraryOutcome::Applied { snapshot, summary },
        Err(error) => failure_outcome(error),
    }
}

/// Maps a backend error to a UI-facing message plus optional advice.
fn failure_from(error: BackendError) -> (String, Option<String>) {
    match &error {
        BackendError::MasterPlaylistImmutable { id } => (
            format!("Плейлист {id} — встроенный, его нельзя изменять"),
            Some(
                "Мастер-плейлист библиотеки доступен только для чтения; добавляйте треки \
                 в пользовательские плейлисты."
                    .to_owned(),
            ),
        ),
        BackendError::TrackNotFound(_) | BackendError::PlaylistNotFound(_) => (
            error.to_string(),
            Some("Список мог устареть — нажмите «Обновить».".to_owned()),
        ),
        BackendError::Io(_) => (
            format!("Ошибка ввода-вывода: {error}"),
            Some(
                "Проверьте, что устройство смонтировано и на нём есть свободное место."
                    .to_owned(),
            ),
        ),
        _ => (error.to_string(), None),
    }
}

fn failure_outcome(error: BackendError) -> LibraryOutcome {
    let (message, hint) = failure_from(error);
    LibraryOutcome::Failed { message, hint }
}

/// Russian plural form for `n` items: «1 трек», «2 трека», «5 треков».
/// Rules: `n % 10 == 1 && n % 100 != 11` → one; `n % 10` in 2..=4 and
/// `n % 100` not in 12..=14 → few; otherwise (including 0) → many.
pub(crate) fn plural<'a>(n: usize, one: &'a str, few: &'a str, many: &'a str) -> &'a str {
    let units = n % 10;
    let teens = n % 100;
    if units == 1 && teens != 11 {
        one
    } else if (2..=4).contains(&units) && !(12..=14).contains(&teens) {
        few
    } else {
        many
    }
}

/// «Добавлен 1 трек» / «Добавлено 2 трека» / «Добавлено 5 треков»: the
/// passive participle agrees with the count exactly like the noun does.
fn added_tracks_summary(added: usize) -> String {
    format!(
        "{} {} {}",
        plural(added, "Добавлен", "Добавлено", "Добавлено"),
        added,
        plural(added, "трек", "трека", "треков")
    )
}

/// Extracts a human-readable message from a panic payload. Takes the box
/// by value: it downcasts `&str`, then `String`, and falls back to a
/// placeholder for anything else; tests cover all three paths.
pub(crate) fn panic_message(payload: Box<dyn Any + Send>) -> String {
    if let Some(text) = payload.downcast_ref::<&str>() {
        (*text).to_owned()
    } else if let Some(text) = payload.downcast_ref::<String>() {
        text.clone()
    } else {
        "причина не сообщена".to_owned()
    }
}

/// File extensions accepted from drag-and-drop (case-insensitive).
/// Native Nano 7G firmware formats per the Apple spec pass through
/// untouched: mp3, AAC-family (m4a/m4b/aac), WAV (since 2026-11-21),
/// AIFF (aiff/aif) and Audible (aa/aax). `m4b`/`aa`/`aax` are
/// audiobook containers → drafts get audiobook bits (round 5.1 and
/// 2026-11-21). flac/ogg/oga have no firmware decoder and are
/// transcoded in the worker (round 5.2).
///
/// The DB write is format-agnostic: vendored libopod hardcodes the
/// `'MP3 '` fourcc for every track, and the actual file keeps its
/// own extension. Playback of passthrough formats other than mp3
/// still needs the live device check (release gate) — the vendor
/// media gate accepted mp3 sources only until 2026-11-21.
pub const AUDIO_EXTENSIONS: [&str; 12] = [
    "mp3", "m4a", "m4b", "aac", "wav", "aiff", "aif", "aa", "aax", "flac", "ogg", "oga",
];

/// Extensions that mark an audiobook file (bits 8): the AAC audiobook
/// container and the native Audible formats.
const AUDIOBOOK_EXTENSIONS: [&str; 3] = ["m4b", "aa", "aax"];

/// Raw iTunesDB media-kind bit of audiobooks (round 5.1). Checks use
/// the semantic bit test (`bits & AUDIOBOOK_BIT != 0`) — an exact
/// `== 8` breaks for combined bits (round 6 tail nit-6).
pub(crate) const AUDIOBOOK_BIT: u32 = 8;

/// Lowercased extension of a path, or an empty string when absent.
pub(crate) fn path_extension(path: &std::path::Path) -> String {
    path.extension()
        .and_then(|extension| extension.to_str())
        .map(|extension| extension.to_ascii_lowercase())
        .unwrap_or_default()
}

/// True for formats the Nano 7G firmware cannot play (flac/ogg/oga
/// have no firmware decoder): the worker must transcode them before
/// add_track. WAV joined the passthrough on 2026-11-21 — the
/// spec lists it as native (round 5.2 had bundled it with flac/ogg).
pub(crate) fn needs_transcode(extension: &str) -> bool {
    matches!(extension, "flac" | "ogg" | "oga")
}

/// Codec routing of the transcode by source extension: FLAC is
/// lossless, so it re-wraps into ALAC bit-perfectly (the e2e PCM test
/// proves it); Ogg sources are lossy — a zero-additional-loss path
/// does not exist, so they re-encode to VBR AAC (owner decision
/// 2026-11-21: compact wins over size).
fn transcode_codec(extension: &str) -> &'static str {
    match extension {
        "flac" => "alac",
        _ => "aac", // ogg, oga
    }
}

/// Target container extension of a transcode: audiobook bits get the
/// book container, everything else the music one.
fn transcode_target_extension(media_kind_bits: u32) -> &'static str {
    if media_kind_bits & AUDIOBOOK_BIT != 0 {
        "m4b"
    } else {
        "m4a"
    }
}

/// Exact argument list of the transcode command: silence the banner,
/// drop any cover video stream, keep chapter metadata, encode with the
/// routed codec (ALAC for lossless sources, VBR AAC otherwise) and mux
/// into the iPod container.
fn ffmpeg_transcode_args(
    source: &std::path::Path,
    target: &std::path::Path,
    codec: &str,
) -> Vec<String> {
    let mut args = vec![
        "-hide_banner".to_owned(),
        "-loglevel".to_owned(),
        "error".to_owned(),
        "-y".to_owned(),
        "-i".to_owned(),
        source.display().to_string(),
        "-vn".to_owned(),
        "-map_chapters".to_owned(),
        "0".to_owned(),
        "-c:a".to_owned(),
        codec.to_owned(),
    ];
    if codec == "aac" {
        // Native AAC VBR quality 5 (~250k average).
        args.push("-q:a".to_owned());
        args.push("5".to_owned());
    }
    args.push("-f".to_owned());
    args.push("ipod".to_owned());
    args.push(target.display().to_string());
    args
}

/// Honest batch tail for transcoding failures, appended to the summary.
/// The honest failures tail of an add summary: transcode or duration
/// probe problems, one line per skipped draft.
fn add_failures_summary(failures: &[String]) -> String {
    if failures.is_empty() {
        String::new()
    } else {
        format!("; добавление не удалось: {}", failures.join("; "))
    }
}

/// Recursively collects audio files under `directory` (std fs walk):
/// hidden entries (leading dot) and non-audio files are skipped.
fn collect_audio_files(directory: &std::path::Path, out: &mut Vec<PathBuf>) {
    let Ok(entries) = std::fs::read_dir(directory) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        let hidden = path
            .file_name()
            .and_then(|name| name.to_str())
            .is_some_and(|name| name.starts_with('.'));
        if hidden {
            continue;
        }
        if path.is_dir() {
            collect_audio_files(&path, out);
        } else if AUDIO_EXTENSIONS.contains(&path_extension(&path).as_str()) {
            out.push(path);
        }
    }
}

/// Maps dropped file paths into track drafts. Returns the drafts plus
/// human-readable names of skipped paths (not an audio extension, or a
/// stem that cannot serve as a title) so the UI can show what was
/// ignored. `.m4b`/`.aa`/`.aax` drafts carry audiobook media-kind
/// bits. A dropped
/// DIRECTORY expands recursively (sorted by full path; hidden entries
/// and non-audio files skipped) and every audio file inside becomes an
/// audiobook (bits 8 — the owner's rule: directory = book); a
/// directory with no audio lands in `skipped` by its full name.
/// Provenance is tracked PER PATH (round 6 tail minor-4): in a mixed
/// drop the directly dropped files keep their extension-based kind —
/// only the directory expansion is forced to bits 8.
pub fn drafts_from_paths(paths: &[PathBuf]) -> (Vec<TrackDraft>, Vec<String>) {
    let mut drafts = Vec::new();
    let mut skipped = Vec::new();
    let mut expanded: Vec<(PathBuf, bool)> = Vec::new(); // (path, from a dropped directory)
    for path in paths {
        if path.is_dir() {
            let mut collected = Vec::new();
            collect_audio_files(path, &mut collected);
            collected.sort();
            if collected.is_empty() {
                // «имя директории» per spec; the display fallback covers
                // a root-like path without a file name.
                skipped.push(
                    path.file_name()
                        .and_then(|name| name.to_str())
                        .map(str::to_owned)
                        .unwrap_or_else(|| path.display().to_string()),
                );
            } else {
                expanded.extend(collected.into_iter().map(|path| (path, true)));
            }
        } else {
            expanded.push((path.clone(), false));
        }
    }
    for (path, from_directory) in &expanded {
        let name = match path.file_name().and_then(|name| name.to_str()) {
            Some(name) => name.to_owned(),
            None => path.display().to_string(),
        };
        let extension = path_extension(path);
        let is_audio = !extension.is_empty()
            && AUDIO_EXTENSIONS.contains(&extension.as_str());
        let stem = path
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or_default()
            .trim()
            .to_owned();
        match TrackDraft::new(path.clone(), stem) {
            // A blank stem fails title validation and lands in `skipped`.
            Ok(draft) if is_audio => {
                // A file expanded from a dropped directory is always a
                // part of the dropped book; a single dropped file keeps
                // its own extension-based kind (round 6 tail minor-4:
                // per-path provenance, not a global directory flag).
                let draft =
                    if *from_directory || AUDIOBOOK_EXTENSIONS.contains(&extension.as_str()) {
                        draft.with_media_kind_bits(8)
                    } else {
                        draft
                };
                drafts.push(draft);
            }
            _ => skipped.push(name.to_owned()),
        }
    }
    (drafts, skipped)
}

/// Runs ffmpeg for one draft into the batch tempdir and returns a
/// draft pointing at the transcoded file (the source draft and its
/// path stay untouched). Failures surface as one-line honest reasons:
/// a missing binary, or a non-zero exit with the first stderr line.
fn transcode_draft(
    draft: &TrackDraft,
    batch_dir: &std::path::Path,
    index: usize,
) -> Result<TrackDraft, String> {
    let target = batch_dir.join(format!(
        "podlib-transcode-{index}.{}",
        transcode_target_extension(draft.media_kind_bits)
    ));
    let codec = transcode_codec(&path_extension(&draft.source_path));
    let output = std::process::Command::new("ffmpeg")
        .args(ffmpeg_transcode_args(&draft.source_path, &target, codec))
        .output()
        .map_err(|_| "ffmpeg не найден".to_owned())?;
    if !output.status.success() {
        let reason = String::from_utf8_lossy(&output.stderr)
            .lines()
            .next()
            .unwrap_or("без вывода ffmpeg")
            .to_owned();
        return Err(format!("ffmpeg: {reason}"));
    }
    if !target.is_file() {
        return Err("ffmpeg не создал целевой файл".to_owned());
    }
    let mut prepared = draft.clone();
    prepared.source_path = target;
    Ok(prepared)
}

/// ffprobe arguments for the duration probe: print only the container
/// duration, as a bare float line («873.958000») on stdout.
fn ffprobe_duration_args(path: &std::path::Path) -> Vec<String> {
    vec![
        "-v".to_owned(),
        "error".to_owned(),
        "-show_entries".to_owned(),
        "format=duration".to_owned(),
        "-of".to_owned(),
        "default=noprint_wrappers=1:nokey=1".to_owned(),
        path.display().to_string(),
    ]
}

/// Probes the real duration of a media file with `ffprobe` — the same
/// CLI pattern as the ffmpeg transcode step (round 6.1 B1: passthrough
/// drafts have no other source for `duration_ms`, and the iPod refuses
/// to play a track whose total_time_ms stayed 0). Returns milliseconds;
/// an honest one-line reason on failure: a missing binary, a non-zero
/// exit, or output that is not a positive duration. `program` is
/// injectable so tests can exercise the missing-binary path.
fn probe_duration_ms_with(program: &str, path: &std::path::Path) -> Result<u32, String> {
    let output = std::process::Command::new(program)
        .args(ffprobe_duration_args(path))
        .output()
        .map_err(|_| format!("{program} не найден"))?;
    if !output.status.success() {
        let reason = String::from_utf8_lossy(&output.stderr)
            .lines()
            .next()
            .unwrap_or("без вывода ffprobe")
            .to_owned();
        return Err(format!("ffprobe: {reason}"));
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    let seconds: f64 = stdout
        .trim()
        .parse()
        .map_err(|_| format!("ffprobe: нечисловой вывод «{}»", stdout.trim()))?;
    if !seconds.is_finite() || seconds <= 0.0 {
        return Err(format!("ffprobe: бессмысленная длительность {seconds}"));
    }
    Ok((seconds * 1000.0).round() as u32)
}

/// Probes the duration with the real ffprobe.
fn probe_duration_ms(path: &std::path::Path) -> Result<u32, String> {
    probe_duration_ms_with("ffprobe", path)
}

/// Payload dragged from the track table: the ids of the dragged rows. A
/// `Vec` (one id today) so multi-row drag needs no payload type change.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DraggedTracks(pub Vec<TrackId>);

/// Guards a playlist mutation (add/remove members, rename, delete)
/// before a command is sent.
///
/// WHY the UI guards instead of trusting the backend: only the 7G
/// (libopod) adapter rejects master-member mutations. The classic
/// iTunesDB backend deliberately allows them, mirroring the firmware —
/// but they are pointless there as well: the master playlist is the
/// mirror of the whole library, and adding to it is a no-op (duplicate
/// members are ignored by contract). One consistent UI rule for both
/// device kinds, explained up front without a round trip to the device.
/// Firmware-owned views (`distinguished_kind != 0`: Аудиокниги, Видео
/// and friends) are system property on every device kind — rename and
/// delete are refused alongside membership changes.
pub fn guard_playlist_mutation(playlist: &Playlist) -> Result<(), String> {
    if playlist.is_master {
        Err(format!(
            "«{}» — встроенный мастер-плейлист: в него нельзя ничего добавить или убрать",
            playlist.name
        ))
    } else if playlist.distinguished_kind != 0 {
        Err(format!(
            "«{}» — системный плейлист устройства: его нельзя изменять",
            playlist.name
        ))
    } else {
        Ok(())
    }
}

/// Text fields of the edit dialog, exactly as shown on screen.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct EditDialogFields {
    pub title: String,
    pub artist: String,
    pub album: String,
    pub album_artist: String,
    pub track_number: String,
    pub disc_number: String,
}

impl EditDialogFields {
    pub fn from_track(track: &Track) -> Self {
        Self {
            title: track.title.clone(),
            artist: track.artist.clone().unwrap_or_default(),
            album: track.album.clone().unwrap_or_default(),
            album_artist: track.album_artist.clone().unwrap_or_default(),
            track_number: track.track_number.to_string(),
            disc_number: track.disc_number.to_string(),
        }
    }
}

/// Builds a sparse change set from the dialog: unchanged fields stay
/// `None` (no-op → the UI closes the dialog without a request), text
/// changed to empty clears the field (backend contract), numbers must
/// parse. NOTE: no genre field by design — libopod stores it on add but
/// never returns it on read, so an edit field would silently drop it.
pub fn changes_from_track(
    current: &Track,
    fields: &EditDialogFields,
) -> Result<TrackChanges, String> {
    let title = fields.title.trim();
    if title.is_empty() {
        return Err("Название не может быть пустым".to_owned());
    }
    let mut changes = TrackChanges::default();
    if title != current.title {
        changes.title = Some(title.to_owned());
    }
    if fields.artist != current.artist.clone().unwrap_or_default() {
        changes.artist = Some(fields.artist.clone());
    }
    if fields.album != current.album.clone().unwrap_or_default() {
        changes.album = Some(fields.album.clone());
    }
    if fields.album_artist != current.album_artist.clone().unwrap_or_default() {
        changes.album_artist = Some(fields.album_artist.clone());
    }
    changes.track_number = number_change(fields.track_number.trim(), current.track_number)?;
    changes.disc_number = number_change(fields.disc_number.trim(), current.disc_number)?;
    Ok(changes)
}

/// Blank means "keep current"; otherwise the text must parse and differ.
fn number_change(text: &str, current: u32) -> Result<Option<u32>, String> {
    if text.is_empty() {
        return Ok(None);
    }
    let parsed: u32 = text
        .parse()
        .map_err(|_| format!("«{text}» — не целое число"))?;
    if parsed == current {
        Ok(None)
    } else {
        Ok(Some(parsed))
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::sync::mpsc::RecvTimeoutError;
    use std::time::Duration;

    use podlib::{build_nano2g_fixture, FixturePlaylist, FixtureTrack, PlaylistDraft};
    use tempfile::TempDir;

    use super::*;

    // 30s: the ceiling only matters under heavy parallel load (the suite
    // spawns real ffmpeg fixtures); workers normally reply in milliseconds.
    const TIMEOUT: Duration = Duration::from_secs(30);

    fn fixture_track(title: &str) -> FixtureTrack {
        FixtureTrack {
            title: title.to_owned(),
            artist: Some("Artist".to_owned()),
            album: Some("Album".to_owned()),
            genre: None,
            file_name: format!("{title}.mp3"),
            size_bytes: 1024,
            duration_ms: 60_000,
            track_number: 1,
            disc_number: 1,
        }
    }

    /// A mounted classic-device fixture with the given tracks/playlists.
    fn fixture_mount(tracks: &[FixtureTrack], playlists: &[FixturePlaylist]) -> TempDir {
        let dir = TempDir::new().unwrap();
        build_nano2g_fixture(dir.path(), tracks, playlists).unwrap();
        dir
    }

    fn opened_worker(mount: &Path) -> (LibraryWorker, LibrarySnapshot) {
        let (worker, rx) = LibraryWorker::spawn(mount.to_owned());
        match rx.recv_timeout(TIMEOUT) {
            Ok(LibraryOutcome::Loaded(snapshot)) => (worker, snapshot),
            other => panic!("expected Loaded, got {other:?}"),
        }
    }

    fn run(worker: &LibraryWorker, command: LibraryCommand) -> LibraryOutcome {
        let rx = worker.request(command).expect("worker must be alive");
        rx.recv_timeout(TIMEOUT).expect("worker must reply")
    }

    /// Polls until the worker refuses commands, returning the refusal
    /// message. The worker thread exits right AFTER its final reply
    /// (`Failed` outcome / `Closed`), and `request()` turns Err only
    /// once that thread has actually dropped the receiver — polling
    /// instead of racing the exit keeps these tests deterministic.
    fn wait_until_refusing(worker: &LibraryWorker) -> Option<String> {
        let deadline = std::time::Instant::now() + TIMEOUT;
        loop {
            match worker.request(LibraryCommand::Refresh) {
                Err(message) => return Some(message),
                Ok(_) if std::time::Instant::now() >= deadline => return None,
                Ok(_) => std::thread::sleep(Duration::from_millis(2)),
            }
        }
    }

    fn applied_snapshot(outcome: LibraryOutcome) -> LibrarySnapshot {
        match outcome {
            LibraryOutcome::Applied { snapshot, .. } => snapshot,
            other => panic!("expected Applied, got {other:?}"),
        }
    }

    fn failure_of(outcome: LibraryOutcome) -> (String, Option<String>) {
        match outcome {
            LibraryOutcome::Failed { message, hint } => (message, hint),
            other => panic!("expected Failed, got {other:?}"),
        }
    }

    // ---- Russian plural forms (P1) ----

    #[test]
    fn plural_picks_one_few_many_on_boundaries() {
        let cases = [
            (0, "треков"),   // ноль → родительный множественного
            (1, "трек"),     // один
            (2, "трека"),    // два-четыре
            (5, "треков"),   // пять и больше
            (11, "треков"),  // 11-14 — всегда «ов»
            (12, "треков"),
            (21, "трек"),    // ...1, кроме 11
            (22, "трека"),   // ...2, кроме 12-14
            (25, "треков"),
            (100, "треков"),
            (101, "трек"),   // круглое число + 1
            (111, "треков"), // 11 в конце — исключение из правила выше
        ];
        for (n, expected) in cases {
            assert_eq!(
                plural(n, "трек", "трека", "треков"),
                expected,
                "n = {n}"
            );
        }
    }

    #[test]
    fn added_tracks_summary_agrees_with_the_count() {
        assert_eq!(added_tracks_summary(1), "Добавлен 1 трек");
        assert_eq!(added_tracks_summary(2), "Добавлено 2 трека");
        assert_eq!(added_tracks_summary(5), "Добавлено 5 треков");
        assert_eq!(added_tracks_summary(11), "Добавлено 11 треков");
        assert_eq!(added_tracks_summary(21), "Добавлен 21 трек");
    }

    // ---- drafts_from_paths ----

    fn write_file(dir: &Path, name: &str) -> PathBuf {
        let path = dir.join(name);
        std::fs::write(&path, b"x").unwrap();
        path
    }

    /// Real probeable passthrough audio (AAC in an .m4a container)
    /// generated by the installed ffmpeg. Round 6.1 B1: garbage bytes
    /// no longer reach `add_track` — the duration probe rejects them —
    /// so add-flow tests need genuine media files.
    fn real_passthrough_file(dir: &Path, name: &str) -> PathBuf {
        let path = dir.join(format!("{name}.m4a"));
        let generated = std::process::Command::new("ffmpeg")
            .args([
                "-f",
                "lavfi",
                "-i",
                "sine=frequency=440:duration=0.2",
                "-c:a",
                "aac",
                &path.display().to_string(),
            ])
            .output()
            .expect("run ffmpeg");
        assert!(
            generated.status.success(),
            "fixture ffmpeg failed: {}",
            String::from_utf8_lossy(&generated.stderr)
        );
        path
    }

    #[test]
    fn drafts_from_paths_maps_audio_files_to_stem_titled_drafts() {
        let dir = TempDir::new().unwrap();
        let (drafts, skipped) = drafts_from_paths(&[
            write_file(dir.path(), "Song One.mp3"),
            write_file(dir.path(), "UPPER.M4A"),
            write_file(dir.path(), "notes.txt"),
            write_file(dir.path(), "noext"),
            dir.path().join("missing.flac"),
        ]);
        assert_eq!(drafts.len(), 3, "mp3 + uppercase m4a + missing flac pass");
        assert_eq!(drafts[0].title, "Song One");
        assert_eq!(drafts[0].artist, None, "tags are not parsed");
        assert_eq!(drafts[0].duration_ms, 0);
        assert_eq!(drafts[0].media_kind_bits, 1, "songs default to song bits");
        assert_eq!(drafts[1].title, "UPPER", "extension match is case-insensitive");
        assert_eq!(drafts[2].source_path, dir.path().join("missing.flac"));
        assert_eq!(skipped, vec!["notes.txt", "noext"]);
    }

    #[test]
    fn m4b_drafts_carry_audiobook_bits_case_insensitively() {
        // Round 5.1: .m4b is the audiobook container — the draft must
        // land in the device's «Аудиокниги» view, not in «Музыка».
        let dir = TempDir::new().unwrap();
        let (drafts, skipped) = drafts_from_paths(&[
            write_file(dir.path(), "Song.m4b"),
            write_file(dir.path(), "Book.S.M4B"),
            write_file(dir.path(), "Plain.mp3"),
        ]);
        assert!(skipped.is_empty(), "all three are audio files");
        assert_eq!(drafts.len(), 3);
        assert_eq!(drafts[0].title, "Song");
        assert_eq!(drafts[0].media_kind_bits, 8, "lowercase .m4b");
        assert_eq!(drafts[1].media_kind_bits, 8, "uppercase .M4B");
        assert_eq!(drafts[2].media_kind_bits, 1, "plain .mp3 stays a song");
    }

    // ---- 2026-11-21: full iPod audio-format support ----

    /// AIFF plays natively on Nano 7G per the Apple spec; the drafts
    /// pass through as songs (round 5.2 had skipped both extensions).
    #[test]
    fn aiff_drafts_pass_through_as_songs() {
        let dir = TempDir::new().unwrap();
        let (drafts, skipped) = drafts_from_paths(&[
            write_file(dir.path(), "Track.aiff"),
            write_file(dir.path(), "Short.AIF"),
            write_file(dir.path(), "cover.txt"),
        ]);
        assert_eq!(skipped, vec!["cover.txt"], "only non-audio is skipped");
        assert_eq!(drafts.len(), 2);
        assert_eq!(drafts[0].title, "Track");
        assert_eq!(drafts[0].media_kind_bits, 1, ".aiff is a song");
        assert_eq!(drafts[1].title, "Short", "extension match is case-insensitive");
        assert_eq!(drafts[1].media_kind_bits, 1, ".AIF is a song");
    }

    /// Audible formats 2/3/4 (.aa) and Enhanced/AAX/AAX+ (.aax) play
    /// natively; they are audiobooks — the drafts carry bits 8 like
    /// .m4b, case-insensitively.
    #[test]
    fn audible_drafts_carry_audiobook_bits() {
        let dir = TempDir::new().unwrap();
        let (drafts, skipped) = drafts_from_paths(&[
            write_file(dir.path(), "Book.aa"),
            write_file(dir.path(), "Novel.AAX"),
            write_file(dir.path(), "Plain.mp3"),
        ]);
        assert!(skipped.is_empty(), "aa/aax are audio files");
        assert_eq!(drafts.len(), 3);
        assert_eq!(drafts[0].title, "Book");
        assert_eq!(drafts[0].media_kind_bits, 8, ".aa is an audiobook");
        assert_eq!(drafts[1].title, "Novel");
        assert_eq!(drafts[1].media_kind_bits, 8, "uppercase .AAX");
        assert_eq!(drafts[2].media_kind_bits, 1, "plain .mp3 stays a song");
    }

    // ---- round 5.2: directory drops + transcode ----

    /// Owner's decision: a dropped DIRECTORY is one audiobook — every
    /// audio file inside gets audiobook bits (8), regardless of its own
    /// extension. Nested folders are walked, hidden entries and
    /// non-audio files are skipped, paths sort deterministically.
    #[test]
    fn dropped_directory_expands_recursively_with_audiobook_bits() {
        let dir = TempDir::new().unwrap();
        std::fs::create_dir_all(dir.path().join("book/cd2")).unwrap();
        std::fs::create_dir_all(dir.path().join("book/.hidden")).unwrap();
        write_file(dir.path(), "book/01.flac");
        write_file(dir.path(), "book/cd2/02.ogg");
        write_file(dir.path(), "book/.hidden/secret.mp3");
        write_file(dir.path(), "book/cover.png");

        let (drafts, skipped) = drafts_from_paths(&[dir.path().join("book")]);
        assert!(skipped.is_empty());
        let full_paths: Vec<String> = drafts
            .iter()
            .map(|draft| draft.source_path.display().to_string())
            .collect();
        let mut sorted = full_paths.clone();
        sorted.sort();
        assert_eq!(full_paths, sorted, "paths are sorted by full path");
        assert_eq!(drafts.len(), 2, "hidden dir and cover.png are skipped");
        for draft in &drafts {
            assert_eq!(
                draft.media_kind_bits, 8,
                "files from a dropped directory are audiobook bits"
            );
        }
        assert_eq!(drafts[0].title, "01");
        assert!(drafts[0].source_path.ends_with("01.flac"));
        assert_eq!(drafts[1].title, "02");
        assert!(drafts[1].source_path.ends_with("02.ogg"));
    }

    // ---- round 6 tail minor-4: per-path provenance ----

    /// A MIXED drop (directory + individual files in one gesture) is
    /// provenance-tracked per path: files expanded from the directory
    /// are the book (bits 8), the directly dropped files keep their
    /// own extension-based kind. The old global `directory_drop` flag
    /// wrongly made everything an audiobook.
    #[test]
    fn mixed_drop_keeps_direct_files_by_extension() {
        let dir = TempDir::new().unwrap();
        std::fs::create_dir_all(dir.path().join("book")).unwrap();
        write_file(dir.path(), "book/01.mp3");
        write_file(dir.path(), "book/02.mp3");
        let single = write_file(dir.path(), "top.mp3");
        let m4b = write_file(dir.path(), "audio.m4b");
        let audible = write_file(dir.path(), "book-direct.aa");

        for order in 0..2 {
            // Both orders prove the per-path (not first-seen) logic.
            let paths: Vec<PathBuf> = if order == 0 {
                vec![
                    dir.path().join("book"),
                    single.clone(),
                    m4b.clone(),
                    audible.clone(),
                ]
            } else {
                vec![
                    single.clone(),
                    m4b.clone(),
                    audible.clone(),
                    dir.path().join("book"),
                ]
            };
            let (drafts, skipped) = drafts_from_paths(&paths);
            assert!(skipped.is_empty());
            assert_eq!(drafts.len(), 5, "2 book files + 3 direct files: {order}");
            for draft in &drafts {
                let bits = draft.media_kind_bits;
                if draft.source_path.starts_with(dir.path().join("book")) {
                    assert_eq!(bits, 8, "directory expansion is a book: {order}");
                } else if draft.source_path == m4b {
                    assert_eq!(bits, 8, ".m4b is a book by extension: {order}");
                } else if draft.source_path == audible {
                    assert_eq!(bits, 8, ".aa is a book by extension: {order}");
                } else {
                    assert_eq!(bits, 1, "direct mp3 stays a song: {order}");
                }
            }
        }
    }

    #[test]
    fn directory_without_audio_lands_in_skipped() {
        let dir = TempDir::new().unwrap();
        write_file(dir.path(), "booklet.pdf");
        let (drafts, skipped) = drafts_from_paths(&[dir.path().join("book")]);
        assert!(drafts.is_empty());
        assert_eq!(skipped, vec!["book".to_owned()], "the directory NAME lands in skipped");
    }

    #[test]
    fn needs_transcode_matches_firmware_unplayable_formats() {
        // Nano 7G firmware formats per the Apple spec (SP112039): mp3,
        // AAC-family (m4a/m4b/aac), WAV, AIFF and Audible play as-is;
        // flac/ogg have no firmware decoder and are transcoded to AAC
        // (round 5.2). WAV joined passthrough on 2026-11-21 (owner's
        // decision: round 5.2 had bundled it with flac/ogg by mistake).
        for playable in ["mp3", "m4a", "m4b", "aac", "wav", "aiff", "aif", "aa", "aax"] {
            assert!(!needs_transcode(playable), "{playable} passes through");
        }
        for unplayable in ["flac", "ogg", "oga"] {
            assert!(needs_transcode(unplayable), "{unplayable} needs transcode");
        }
        // Unknown extensions never reach the worker, but stay honest.
        assert!(!needs_transcode("txt"));
    }

    #[test]
    fn transcode_target_extension_follows_media_kind() {
        assert_eq!(transcode_target_extension(8), "m4b", "audiobook container");
        assert_eq!(transcode_target_extension(1), "m4a", "music container");
        assert_eq!(transcode_target_extension(4), "m4a");
        // nit-6: the check is the semantic BIT, not an exact == 8.
        assert_eq!(transcode_target_extension(9), "m4b", "audiobook bit wins");
    }

    #[test]
    fn transcode_codec_follows_source_losslessness() {
        assert_eq!(transcode_codec("flac"), "alac", "lossless → lossless");
        assert_eq!(transcode_codec("ogg"), "aac");
        assert_eq!(transcode_codec("oga"), "aac");
    }

    #[test]
    fn ffmpeg_transcode_args_are_exact() {
        // FLAC is a lossless source: wrap into ALAC, no rate control.
        let args = ffmpeg_transcode_args(
            std::path::Path::new("/src/Skazka.flac"),
            std::path::Path::new("/tmp/book.m4b"),
            transcode_codec("flac"),
        );
        assert_eq!(
            args,
            [
                "-hide_banner",
                "-loglevel",
                "error",
                "-y",
                "-i",
                "/src/Skazka.flac",
                "-vn",
                "-map_chapters",
                "0",
                "-c:a",
                "alac",
                "-f",
                "ipod",
                "/tmp/book.m4b",
            ]
        );
        // Ogg is lossy: VBR AAC quality 5 — compact wins over zero
        // additional loss (owner decision 2026-11-21).
        let args = ffmpeg_transcode_args(
            std::path::Path::new("/src/Mix.ogg"),
            std::path::Path::new("/tmp/mix.m4a"),
            transcode_codec("ogg"),
        );
        assert_eq!(
            args,
            [
                "-hide_banner",
                "-loglevel",
                "error",
                "-y",
                "-i",
                "/src/Mix.ogg",
                "-vn",
                "-map_chapters",
                "0",
                "-c:a",
                "aac",
                "-q:a",
                "5",
                "-f",
                "ipod",
                "/tmp/mix.m4a",
            ]
        );
    }

    #[test]
    fn add_failures_summary_appends_an_honest_tail() {
        assert_eq!(add_failures_summary(&[]), "");
        assert_eq!(
            add_failures_summary(&["«Книга»: ffmpeg не найден".to_owned()]),
            "; добавление не удалось: «Книга»: ffmpeg не найден"
        );
        assert_eq!(
            add_failures_summary(&["a".to_owned(), "b".to_owned()]),
            "; добавление не удалось: a; b"
        );
    }

    /// End-to-end lossless path: a real FLAC fixture (generated by the
    /// installed ffmpeg) is transcoded into ALAC-in-m4a and the decoded
    /// PCM must stay bit-identical — the whole point of ALAC routing.
    #[test]
    fn transcode_draft_wraps_flac_into_alac_losslessly() {
        let dir = TempDir::new().unwrap();
        let source = dir.path().join("song.flac");
        let generated = std::process::Command::new("ffmpeg")
            .args([
                "-hide_banner".to_owned(),
                "-loglevel".to_owned(),
                "error".to_owned(),
                "-y".to_owned(),
                "-f".to_owned(),
                "lavfi".to_owned(),
                "-i".to_owned(),
                "sine=frequency=440:duration=2".to_owned(),
                "-c:a".to_owned(),
                "flac".to_owned(),
                source.display().to_string(),
            ])
            .output()
            .expect("run ffmpeg");
        assert!(
            generated.status.success(),
            "fixture ffmpeg failed: {}",
            String::from_utf8_lossy(&generated.stderr)
        );

        let draft = TrackDraft::new(source.clone(), "Трек").unwrap();
        let transcoded = transcode_draft(&draft, dir.path(), 0).expect("ffmpeg transcode");
        assert_eq!(
            transcoded.source_path.extension().and_then(|e| e.to_str()),
            Some("m4a"),
            "music draft lands in the m4a container"
        );
        assert_ne!(transcoded.source_path, draft.source_path, "in batch tempdir");
        assert_eq!(
            probe_stream_codec(&transcoded.source_path).as_deref(),
            Some("alac"),
            "flac must wrap into ALAC"
        );

        // Bit-perfect: decode source and result to PCM and compare.
        let decode_pcm = |input: &std::path::Path, output: &std::path::Path| {
            let decoded = std::process::Command::new("ffmpeg")
                .args([
                    "-hide_banner".to_owned(),
                    "-loglevel".to_owned(),
                    "error".to_owned(),
                    "-y".to_owned(),
                    "-i".to_owned(),
                    input.display().to_string(),
                    "-c:a".to_owned(),
                    "pcm_s16le".to_owned(),
                    "-f".to_owned(),
                    "wav".to_owned(),
                    output.display().to_string(),
                ])
                .output()
                .expect("run ffmpeg");
            assert!(
                decoded.status.success(),
                "decode {input:?} failed: {}",
                String::from_utf8_lossy(&decoded.stderr)
            );
        };
        let source_pcm = dir.path().join("source.wav");
        let result_pcm = dir.path().join("result.wav");
        decode_pcm(&source, &source_pcm);
        decode_pcm(&transcoded.source_path, &result_pcm);
        assert_eq!(
            std::fs::read(&source_pcm).unwrap(),
            std::fs::read(&result_pcm).unwrap(),
            "FLAC→ALAC must keep the PCM bit-identical"
        );
    }

    /// The lossy path: an Ogg Vorbis fixture re-encodes into VBR AAC
    /// (quality 5) — the accepted second generation (owner decision
    /// 2026-11-21). Audiobook bits still land in the .m4b container.
    #[test]
    fn transcode_draft_reencodes_ogg_to_vbr_aac() {
        let dir = TempDir::new().unwrap();
        let source = dir.path().join("mix.ogg");
        let generated = std::process::Command::new("ffmpeg")
            .args([
                "-hide_banner".to_owned(),
                "-loglevel".to_owned(),
                "error".to_owned(),
                "-y".to_owned(),
                "-f".to_owned(),
                "lavfi".to_owned(),
                "-i".to_owned(),
                "sine=frequency=330:duration=2".to_owned(),
                "-c:a".to_owned(),
                "libvorbis".to_owned(),
                source.display().to_string(),
            ])
            .output()
            .expect("run ffmpeg");
        assert!(
            generated.status.success(),
            "fixture ffmpeg failed: {}",
            String::from_utf8_lossy(&generated.stderr)
        );

        let draft = TrackDraft::new(source, "Глава 1")
            .unwrap()
            .with_media_kind_bits(8);
        let transcoded = transcode_draft(&draft, dir.path(), 0).expect("ffmpeg transcode");
        assert_eq!(
            transcoded.source_path.extension().and_then(|e| e.to_str()),
            Some(transcode_target_extension(8)),
            "audiobook container .m4b"
        );
        assert_eq!(transcoded.media_kind_bits, 8);
        assert_eq!(transcoded.title, "Глава 1");
        assert_eq!(
            probe_stream_codec(&transcoded.source_path).as_deref(),
            Some("aac"),
            "ogg must re-encode to AAC"
        );
    }

    /// Reads the audio codec name of a file via the installed ffprobe
    /// (`None` when the tool or the stream is missing).
    fn probe_stream_codec(path: &std::path::Path) -> Option<String> {
        let output = std::process::Command::new("ffprobe")
            .args([
                "-v".to_owned(),
                "error".to_owned(),
                "-select_streams".to_owned(),
                "a:0".to_owned(),
                "-show_entries".to_owned(),
                "stream=codec_name".to_owned(),
                "-of".to_owned(),
                "default=noprint_wrappers=1:nokey=1".to_owned(),
                path.display().to_string(),
            ])
            .output()
            .ok()?;
        let text = String::from_utf8_lossy(&output.stdout).trim().to_owned();
        if text.is_empty() {
            None
        } else {
            Some(text)
        }
    }

    #[test]
    fn transcode_draft_reports_failure_honestly() {
        let dir = TempDir::new().unwrap();
        let draft = TrackDraft::new(dir.path().join("garbage.flac"), "Book").unwrap();
        // The garbage file exists but is not decodable: ffmpeg exits
        // non-zero and the reason surfaces as a one-line honest error.
        std::fs::write(&draft.source_path, b"not audio at all").unwrap();
        let error = transcode_draft(&draft, dir.path(), 0).unwrap_err();
        assert!(error.contains("ffmpeg"), "got: {error}");
    }

    // ---- round 6.1 B1: passthrough duration probe ----

    #[test]
    fn ffprobe_duration_args_are_exact() {
        let args = ffprobe_duration_args(Path::new("/media/book/ch.mp3"));
        assert_eq!(
            args,
            vec![
                "-v".to_owned(),
                "error".to_owned(),
                "-show_entries".to_owned(),
                "format=duration".to_owned(),
                "-of".to_owned(),
                "default=noprint_wrappers=1:nokey=1".to_owned(),
                "/media/book/ch.mp3".to_owned(),
            ]
        );
    }

    #[test]
    fn probe_reports_a_missing_tool_with_clear_text() {
        let dir = TempDir::new().unwrap();
        let source = dir.path().join("chapter.mp3");
        std::fs::write(&source, b"not really audio").unwrap();
        let error = probe_duration_ms_with("podlib-no-such-tool-xyz", &source).unwrap_err();
        assert!(
            error.contains("podlib-no-such-tool-xyz не найден"),
            "got: {error}"
        );
    }

    #[test]
    fn probe_parses_a_real_file_duration() {
        // Minimal 8 kHz mono 16-bit PCM WAV, 1.0 s of silence: real
        // bytes ffprobe can measure without ffmpeg.
        let dir = TempDir::new().unwrap();
        let data_size = 16_000u32;
        let mut wav = Vec::new();
        wav.extend_from_slice(b"RIFF");
        wav.extend_from_slice(&36u32.to_le_bytes());
        wav.extend_from_slice(b"WAVE");
        wav.extend_from_slice(b"fmt ");
        wav.extend_from_slice(&16u32.to_le_bytes());
        wav.extend_from_slice(&1u16.to_le_bytes()); // PCM
        wav.extend_from_slice(&1u16.to_le_bytes()); // mono
        wav.extend_from_slice(&8000u32.to_le_bytes());
        wav.extend_from_slice(&16000u32.to_le_bytes()); // byte rate
        wav.extend_from_slice(&2u16.to_le_bytes()); // block align
        wav.extend_from_slice(&16u16.to_le_bytes()); // bits
        wav.extend_from_slice(b"data");
        wav.extend_from_slice(&data_size.to_le_bytes());
        wav.extend(std::iter::repeat_n(0u8, data_size as usize));
        let source = dir.path().join("chapter.wav");
        std::fs::write(&source, &wav).unwrap();
        let duration_ms = probe_duration_ms(&source).expect("real ffprobe");
        assert!(
            (950..=1050).contains(&duration_ms),
            "1 s of PCM must probe to ~1000 ms, got {duration_ms}"
        );
    }

    /// Mock backend recording every draft handed to `add_track` — lets
    /// the B1/B2 passthrough branch be asserted end to end.
    #[derive(Default)]
    struct RecordingBackend {
        added: Vec<TrackDraft>,
    }

    impl LibraryBackend for RecordingBackend {
        fn tracks(&self) -> Result<Vec<Track>, BackendError> {
            Ok(Vec::new())
        }

        fn playlists(&self) -> Result<Vec<Playlist>, BackendError> {
            Ok(Vec::new())
        }

        fn add_track(&mut self, draft: &TrackDraft) -> Result<TrackId, BackendError> {
            self.added.push(draft.clone());
            Ok(TrackId::from_bits(self.added.len() as u64))
        }

        fn remove_track(&mut self, _id: TrackId, _purge_media: bool) -> Result<bool, BackendError> {
            Ok(true)
        }

        fn edit_track(
            &mut self,
            id: TrackId,
            _changes: &TrackChanges,
        ) -> Result<Track, BackendError> {
            Err(BackendError::TrackNotFound(id))
        }

        fn create_playlist(&mut self, _draft: &PlaylistDraft) -> Result<PlaylistId, BackendError> {
            Err(BackendError::PlaylistNotFound(PlaylistId::from_bits(0)))
        }

        fn delete_playlist(&mut self, _id: PlaylistId) -> Result<bool, BackendError> {
            Ok(false)
        }

        fn rename_playlist(&mut self, id: PlaylistId, _name: &str) -> Result<(), BackendError> {
            Err(BackendError::PlaylistNotFound(id))
        }

        fn add_to_playlist(
            &mut self,
            _playlist: PlaylistId,
            _track_ids: &[TrackId],
        ) -> Result<(), BackendError> {
            Ok(())
        }

        fn remove_from_playlist(
            &mut self,
            _playlist: PlaylistId,
            _track_ids: &[TrackId],
        ) -> Result<(), BackendError> {
            Ok(())
        }
    }

    /// End-to-end passthrough: two m4a chapters (real files generated
    /// by the installed ffmpeg) must reach the backend with a probed
    /// duration AND 1/2 album numbering (B1+B2 together).
    #[test]
    fn add_tracks_probes_passthrough_duration_and_numbers_album_chapters() {
        let dir = TempDir::new().unwrap();
        let mut drafts = Vec::new();
        for name in ["Глава 1", "Глава 2"] {
            let source = dir.path().join(format!("{name}.m4a"));
            let generated = std::process::Command::new("ffmpeg")
                .args([
                    "-f",
                    "lavfi",
                    "-i",
                    "sine=frequency=440:duration=0.5",
                    "-c:a",
                    "aac",
                    &source.display().to_string(),
                ])
                .output()
                .expect("run ffmpeg");
            assert!(
                generated.status.success(),
                "fixture ffmpeg failed: {}",
                String::from_utf8_lossy(&generated.stderr)
            );
            drafts.push(TrackDraft::new(source, name).unwrap());
        }
        let mut backend = RecordingBackend::default();
        let outcome = add_tracks_command(&mut backend, drafts);
        assert!(
            matches!(outcome, LibraryOutcome::Applied { .. }),
            "both chapters must add cleanly"
        );
        assert_eq!(backend.added.len(), 2);
        for draft in &backend.added {
            assert!(draft.duration_ms > 0, "B1: probed duration must be filled");
            assert_eq!(draft.track_count, 2, "B2: chapters share one album");
        }
        assert_eq!(backend.added[0].track_number, 1);
        assert_eq!(backend.added[1].track_number, 2);
    }

    #[test]
    fn add_tracks_reports_probe_failure_instead_of_silent_zero() {
        // A .mp3 that is not audio: the probe fails honestly, the draft
        // never reaches the backend, and the summary carries the reason
        // — no silent total_time_ms = 0 on the device.
        let dir = TempDir::new().unwrap();
        let source = dir.path().join("мусор.mp3");
        std::fs::write(&source, b"definitely not audio").unwrap();
        let mut backend = RecordingBackend::default();
        let outcome =
            add_tracks_command(&mut backend, vec![TrackDraft::new(source, "Глава").unwrap()]);
        match outcome {
            LibraryOutcome::Applied { summary, .. } => {
                assert!(summary.contains("0 треков из 1"), "got: {summary}");
                assert!(summary.contains("ffprobe"), "got: {summary}");
            }
            other => panic!("expected an honest Applied tail, got {other:?}"),
        }
        assert!(backend.added.is_empty(), "the failed draft must be skipped");
    }

    /// End-to-end transcode: a real FLAC fixture must reach the backend
    /// with a probed duration — the transcoded draft inherits the DnD
    /// zero, and a silent `total_time_ms = 0` makes the firmware refuse
    /// to play the track (live F40/DBWI.m4a failure). Same contract as
    /// the passthrough probe (B1).
    #[test]
    fn add_tracks_probes_transcoded_duration() {
        let dir = TempDir::new().unwrap();
        let source = dir.path().join("song.flac");
        let generated = std::process::Command::new("ffmpeg")
            .args([
                "-f",
                "lavfi",
                "-i",
                "sine=frequency=440:duration=0.5",
                "-c:a",
                "flac",
                &source.display().to_string(),
            ])
            .output()
            .expect("run ffmpeg");
        assert!(
            generated.status.success(),
            "fixture ffmpeg failed: {}",
            String::from_utf8_lossy(&generated.stderr)
        );
        let mut backend = RecordingBackend::default();
        let outcome =
            add_tracks_command(&mut backend, vec![TrackDraft::new(source, "Трек").unwrap()]);
        assert!(
            matches!(outcome, LibraryOutcome::Applied { .. }),
            "the transcoded draft must add cleanly"
        );
        assert_eq!(backend.added.len(), 1);
        assert!(
            backend.added[0].duration_ms > 0,
            "B1: transcoded duration must be probed, not zero"
        );
    }

    #[test]
    fn drafts_from_paths_rejects_blank_stems() {
        let dir = TempDir::new().unwrap();
        let (drafts, skipped) = drafts_from_paths(&[write_file(dir.path(), ".mp3")]);
        assert!(drafts.is_empty(), "empty stem must not become a draft");
        assert_eq!(skipped, vec![".mp3"]);
    }

    // ---- changes_from_track (edit dialog → TrackChanges) ----

    fn sample_track() -> Track {
        Track {
            id: TrackId::from_bits(42),
            title: "Alpha".to_owned(),
            artist: Some("Artist".to_owned()),
            album: None,
            album_artist: None,
            duration_ms: 60_000,
            track_number: 1,
            disc_number: 2,
            size_bytes: 1024,
            has_artwork: false,
            media_kind: 0,
        }
    }

    #[test]
    fn unchanged_dialog_fields_produce_empty_changes() {
        let track = sample_track();
        let fields = EditDialogFields::from_track(&track);
        let changes = changes_from_track(&track, &fields).unwrap();
        assert!(changes.is_empty(), "no-op dialog must send nothing");
    }

    #[test]
    fn dialog_diff_maps_edits_and_clearing() {
        let track = sample_track();
        let fields = EditDialogFields {
            title: "Beta".to_owned(),
            artist: String::new(), // clears the field (contract)
            album: "New Album".to_owned(),
            album_artist: String::new(),
            track_number: String::new(), // blank keeps current
            disc_number: "5".to_owned(),
        };
        let changes = changes_from_track(&track, &fields).unwrap();
        assert_eq!(changes.title.as_deref(), Some("Beta"));
        assert_eq!(changes.artist.as_deref(), Some(""));
        assert_eq!(changes.album.as_deref(), Some("New Album"));
        assert_eq!(changes.album_artist, None, "was None, stays None");
        assert_eq!(changes.track_number, None, "blank keeps current");
        assert_eq!(changes.disc_number, Some(5));
    }

    #[test]
    fn dialog_rejects_empty_title_and_bad_numbers() {
        let track = sample_track();
        let mut fields = EditDialogFields::from_track(&track);
        fields.title = "   ".to_owned();
        assert!(changes_from_track(&track, &fields).is_err());
        let mut fields = EditDialogFields::from_track(&track);
        fields.track_number = "abc".to_owned();
        let error = changes_from_track(&track, &fields).unwrap_err();
        assert!(error.contains("abc"));
    }

    // ---- DnD payload mapping ----

    #[test]
    fn dragged_tracks_payload_roundtrips() {
        let payload: Box<dyn Any + Send + Sync> = Box::new(DraggedTracks(vec![
            TrackId::from_bits(7),
            TrackId::from_bits(9),
        ]));
        let ids = payload.downcast_ref::<DraggedTracks>().map(|dragged| dragged.0.clone());
        assert_eq!(ids, Some(vec![TrackId::from_bits(7), TrackId::from_bits(9)]));
    }

    #[test]
    fn foreign_payload_does_not_downcast_to_dragged_tracks() {
        let payload: Box<dyn Any + Send + Sync> = Box::new(123u32);
        assert!(payload.downcast_ref::<DraggedTracks>().is_none());
    }

    // ---- master-playlist guard ----

    #[test]
    fn guard_rejects_master_system_and_allows_user_playlist() {
        let master = Playlist {
            id: PlaylistId::from_bits(1),
            name: "Медиатека".to_owned(),
            track_ids: Vec::new(),
            is_master: true,
            is_smart: false,
            distinguished_kind: 0,
        };
        let user = Playlist {
            id: PlaylistId::from_bits(2),
            name: "Mix".to_owned(),
            track_ids: Vec::new(),
            is_master: false,
            is_smart: false,
            distinguished_kind: 0,
        };
        let error = guard_playlist_mutation(&master).unwrap_err();
        assert!(error.contains("встроенный"));
        assert!(guard_playlist_mutation(&user).is_ok());
        // A firmware-owned view (Аудиокниги, Видео…) is system property:
        // no rename/delete/add/remove may reach the worker for it.
        let view = Playlist {
            id: PlaylistId::from_bits(3),
            name: "Аудиокниги".to_owned(),
            track_ids: Vec::new(),
            is_master: false,
            is_smart: false,
            distinguished_kind: 8,
        };
        let error = guard_playlist_mutation(&view).unwrap_err();
        assert!(error.contains("системн"), "got: {error}");
    }

    // ---- panic_message (moved from app.rs round 1) ----

    #[test]
    fn panic_message_handles_common_payloads() {
        assert_eq!(panic_message(Box::new("str panic")), "str panic");
        assert_eq!(
            panic_message(Box::new(String::from("boxed panic"))),
            "boxed panic"
        );
        assert_eq!(panic_message(Box::new(7u32)), "причина не сообщена");
    }

    // ---- worker end-to-end against the classic fixture backend ----

    #[test]
    fn worker_open_lists_fixture_library() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, snapshot) = opened_worker(dir.path());
        assert_eq!(snapshot.tracks.len(), 1);
        assert_eq!(snapshot.tracks[0].title, "Alpha");
        let master = snapshot
            .playlists
            .iter()
            .find(|playlist| playlist.is_master)
            .expect("fixture library must expose the master playlist");
        assert_eq!(master.track_ids.len(), 1);
        worker.close_and_join();
    }

    #[test]
    fn worker_add_tracks_applies_and_reports() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, _snapshot) = opened_worker(dir.path());
        let host = TempDir::new().unwrap();
        let paths = vec![
            real_passthrough_file(host.path(), "One"),
            real_passthrough_file(host.path(), "Two"),
        ];
        let (drafts, skipped) = drafts_from_paths(&paths);
        assert!(skipped.is_empty());
        let outcome = run(&worker, LibraryCommand::AddTracks(drafts));
        let snapshot = applied_snapshot(outcome);
        assert_eq!(snapshot.tracks.len(), 3);
        worker.close_and_join();
    }

    #[test]
    fn worker_add_tracks_partial_reports_how_far_it_got() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, _snapshot) = opened_worker(dir.path());
        let host = TempDir::new().unwrap();
        let paths = vec![
            real_passthrough_file(host.path(), "Good"),
            // Second source does not exist: the B1 probe fails on it,
            // and the draft is skipped with an honest failure line.
            dir.path().join("nope.mp3"),
        ];
        let (drafts, _) = drafts_from_paths(&paths);
        let outcome = run(&worker, LibraryCommand::AddTracks(drafts));
        let snapshot = applied_snapshot(outcome);
        assert_eq!(snapshot.tracks.len(), 2, "only the first file lands");
        worker.close_and_join();
    }

    #[test]
    fn worker_edit_updates_title_and_reports_id() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, snapshot) = opened_worker(dir.path());
        let id = snapshot.tracks[0].id;
        let changes = TrackChanges {
            title: Some("Beta".to_owned()),
            ..TrackChanges::default()
        };
        match run(&worker, LibraryCommand::EditTrack { id, changes }) {
            LibraryOutcome::Applied { snapshot, summary } => {
                assert_eq!(snapshot.tracks[0].title, "Beta");
                // Remove-and-re-add may keep or rotate the id; the summary
                // must stay truthful either way.
                let rotated = snapshot.tracks.iter().all(|track| track.id != id);
                if rotated {
                    assert!(summary.contains("новый id"), "got: {summary}");
                } else {
                    assert!(summary.contains("Обновлено"), "got: {summary}");
                    assert!(!summary.contains("новый id"));
                }
            }
            other => panic!("expected Applied, got {other:?}"),
        }
        worker.close_and_join();
    }

    #[test]
    fn worker_playlist_lifecycle_and_members() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, snapshot) = opened_worker(dir.path());
        let track_id = snapshot.tracks[0].id;

        let snapshot = applied_snapshot(run(
            &worker,
            LibraryCommand::CreatePlaylist {
                name: "Mix".to_owned(),
            },
        ));
        assert_eq!(snapshot.playlists.len(), 2, "master + the new one");
        let playlist = snapshot
            .playlists
            .iter()
            .find(|playlist| !playlist.is_master)
            .expect("user playlist must appear");
        let playlist_id = playlist.id;

        let snapshot = applied_snapshot(run(
            &worker,
            LibraryCommand::AddToPlaylist {
                playlist: playlist_id,
                track_ids: vec![track_id],
            },
        ));
        let playlist = snapshot.playlist(playlist_id).unwrap();
        assert_eq!(playlist.track_ids, vec![track_id]);

        let snapshot = applied_snapshot(run(
            &worker,
            LibraryCommand::RemoveFromPlaylist {
                playlist: playlist_id,
                track_ids: vec![track_id],
            },
        ));
        assert!(snapshot.playlist(playlist_id).unwrap().track_ids.is_empty());

        let snapshot = applied_snapshot(run(
            &worker,
            LibraryCommand::DeletePlaylist {
                id: playlist_id,
            },
        ));
        assert_eq!(snapshot.playlists.len(), 1, "only master remains");
        worker.close_and_join();
    }

    #[test]
    fn worker_renames_playlist_and_keeps_members() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, snapshot) = opened_worker(dir.path());
        let track_id = snapshot.tracks[0].id;

        let snapshot = applied_snapshot(run(
            &worker,
            LibraryCommand::CreatePlaylist {
                name: "Mix".to_owned(),
            },
        ));
        let playlist_id = snapshot
            .playlists
            .iter()
            .find(|playlist| !playlist.is_master)
            .expect("user playlist must appear")
            .id;
        applied_snapshot(run(
            &worker,
            LibraryCommand::AddToPlaylist {
                playlist: playlist_id,
                track_ids: vec![track_id],
            },
        ));

        let snapshot = applied_snapshot(run(
            &worker,
            LibraryCommand::RenamePlaylist {
                id: playlist_id,
                name: "Renamed".to_owned(),
            },
        ));
        let playlist = snapshot
            .playlist(playlist_id)
            .expect("the renamed playlist must stay");
        assert_eq!(playlist.name, "Renamed");
        // The id and the membership are untouched by the rename.
        assert_eq!(playlist.track_ids, vec![track_id]);
        worker.close_and_join();
    }

    #[test]
    fn worker_refuses_master_playlist_rename() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, snapshot) = opened_worker(dir.path());
        let master_id = snapshot
            .playlists
            .iter()
            .find(|playlist| playlist.is_master)
            .expect("fixture exposes the master")
            .id;
        match run(
            &worker,
            LibraryCommand::RenamePlaylist {
                id: master_id,
                name: "Nope".to_owned(),
            },
        ) {
            LibraryOutcome::Failed { message, .. } => {
                assert!(message.contains("встроенный"), "got: {message}");
            }
            other => panic!("expected Failed, got {other:?}"),
        }
        worker.close_and_join();
    }

    #[test]
    fn failure_from_maps_master_immutable_error_with_hint() {
        let (message, hint) = failure_from(BackendError::MasterPlaylistImmutable {
            id: PlaylistId::from_bits(16),
        });
        assert!(message.contains("встроенный"), "got: {message}");
        assert!(hint.is_some());
    }

    #[test]
    fn classic_fixture_allows_master_members_like_the_firmware() {
        // The classic iTunesDB backend mirrors firmware behaviour: the
        // master playlist CAN hold members. The 7G (libopod) adapter is
        // the one rejecting master mutations; the UI guards master
        // anyway so the experience is consistent on both device kinds.
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, snapshot) = opened_worker(dir.path());
        let master = snapshot.playlists.iter().find(|p| p.is_master).unwrap();
        let snapshot = applied_snapshot(run(
            &worker,
            LibraryCommand::AddToPlaylist {
                playlist: master.id,
                track_ids: vec![snapshot.tracks[0].id],
            },
        ));
        assert_eq!(snapshot.playlist(master.id).unwrap().track_ids.len(), 1);
        worker.close_and_join();
    }

    #[test]
    fn worker_remove_track_shrinks_snapshot() {
        let dir = fixture_mount(&[fixture_track("Alpha"), fixture_track("Beta")], &[]);
        let (worker, snapshot) = opened_worker(dir.path());
        let id = snapshot.tracks[0].id;
        let snapshot = applied_snapshot(run(
            &worker,
            LibraryCommand::RemoveTrack {
                id,
                purge_media: false,
            },
        ));
        assert_eq!(snapshot.tracks.len(), 1);
        assert!(snapshot.track(id).is_none());
        worker.close_and_join();
    }

    #[test]
    fn worker_refresh_reloads_snapshot() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, snapshot) = opened_worker(dir.path());
        match run(&worker, LibraryCommand::Refresh) {
            LibraryOutcome::Loaded(again) => assert_eq!(again.tracks.len(), snapshot.tracks.len()),
            other => panic!("expected Loaded, got {other:?}"),
        }
        worker.close_and_join();
    }

    #[test]
    fn worker_close_replies_closed_and_shuts_down() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, _snapshot) = opened_worker(dir.path());
        match run(&worker, LibraryCommand::Close) {
            LibraryOutcome::Closed => {}
            other => panic!("expected Closed, got {other:?}"),
        }
        assert!(
            matches!(
                wait_until_refusing(&worker),
                Some(message) if message.contains("не активен")
            ),
            "worker must refuse commands after Close"
        );
        worker.close_and_join(); // already exiting; must not hang
    }

    #[test]
    fn worker_open_failure_reports_failed_and_shuts_down() {
        let dir = TempDir::new().unwrap(); // no library inside
        let (worker, rx) = LibraryWorker::spawn(dir.path().to_owned());
        match rx.recv_timeout(TIMEOUT) {
            Ok(LibraryOutcome::Failed { message, .. }) => {
                assert!(message.contains("не открылась"), "got: {message}");
            }
            other => panic!("expected Failed, got {other:?}"),
        }
        assert!(
            wait_until_refusing(&worker).is_some(),
            "worker must refuse commands after a failed open"
        );
    }

    // ---- panic isolation (mock backend; F1) ----

    /// Mock backend: honest on reads, panics on every mutation. Drives
    /// the worker's panic-isolation path, which a real fixture cannot.
    struct PanickingBackend;

    impl LibraryBackend for PanickingBackend {
        fn tracks(&self) -> Result<Vec<Track>, BackendError> {
            Ok(Vec::new())
        }

        fn playlists(&self) -> Result<Vec<Playlist>, BackendError> {
            Ok(Vec::new())
        }

        fn add_track(&mut self, _draft: &TrackDraft) -> Result<TrackId, BackendError> {
            panic!("backend exploded mid-write")
        }

        fn remove_track(&mut self, _id: TrackId, _purge_media: bool) -> Result<bool, BackendError> {
            panic!("backend exploded mid-write")
        }

        fn edit_track(
            &mut self,
            _id: TrackId,
            _changes: &TrackChanges,
        ) -> Result<Track, BackendError> {
            panic!("backend exploded mid-write")
        }

        fn create_playlist(&mut self, _draft: &PlaylistDraft) -> Result<PlaylistId, BackendError> {
            panic!("backend exploded mid-write")
        }

        fn delete_playlist(&mut self, _id: PlaylistId) -> Result<bool, BackendError> {
            panic!("backend exploded mid-write")
        }

        fn rename_playlist(&mut self, _id: PlaylistId, _name: &str) -> Result<(), BackendError> {
            panic!("backend exploded mid-write")
        }

        fn add_to_playlist(
            &mut self,
            _playlist: PlaylistId,
            _track_ids: &[TrackId],
        ) -> Result<(), BackendError> {
            panic!("backend exploded mid-write")
        }

        fn remove_from_playlist(
            &mut self,
            _playlist: PlaylistId,
            _track_ids: &[TrackId],
        ) -> Result<(), BackendError> {
            panic!("backend exploded mid-write")
        }
    }

    #[test]
    fn worker_stops_serving_after_backend_panic() {
        // The backend is built inside the worker thread (not `Send`).
        let (worker, _opened) =
            LibraryWorker::spawn_with_backend(|| Box::new(PanickingBackend));
        // A real mutation (non-empty drafts), so the backend actually
        // panics inside `add_track`; the worker must still answer. The
        // draft carries its own duration, so the B1 probe (which would
        // reject the nonexistent source first) is skipped.
        let mut draft =
            TrackDraft::new(PathBuf::from("/tmp/nope.mp3"), "X").expect("draft must be valid");
        draft.duration_ms = 60_000;
        let rx = worker
            .request(LibraryCommand::AddTracks(vec![draft]))
            .expect("worker must accept commands before the panic");
        match rx.recv_timeout(TIMEOUT) {
            Ok(LibraryOutcome::Failed { message, hint }) => {
                assert!(
                    message.contains("Внутренняя ошибка библиотеки"),
                    "got: {message}"
                );
                assert!(hint.is_some(), "panic reply must advise reconnecting");
            }
            other => panic!("expected Failed, got {other:?}"),
        }
        // The worker is gone: later requests fail on the dead channel
        // instead of reaching a backend of unknown state.
        assert!(
            matches!(
                worker.request(LibraryCommand::Refresh),
                Err(message) if message.contains("не активен")
            ),
            "worker must stop serving after a backend panic"
        );
        worker.close_and_join(); // must not hang on a dead worker
    }

    #[test]
    fn worker_survives_noop_request_while_alive() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, _snapshot) = opened_worker(dir.path());
        // Requests keep working while the worker lives; the dead-worker
        // path is covered by the open-failure test above.
        assert!(worker.request(LibraryCommand::Refresh).is_ok());
        worker.close_and_join();
    }

    #[test]
    fn timeout_helper_reports_disconnected_worker() {
        // Documents the UI-side drain contract: Disconnected after the
        // worker died mid-operation, Empty while it is still working.
        let (tx, rx) = channel::<LibraryOutcome>();
        drop(tx);
        assert!(matches!(rx.try_recv(), Err(std::sync::mpsc::TryRecvError::Disconnected)));
        let (tx, rx) = channel::<LibraryOutcome>();
        let _tx = tx; // keep alive
        assert!(matches!(rx.try_recv(), Err(std::sync::mpsc::TryRecvError::Empty)));
        assert!(matches!(
            rx.recv_timeout(Duration::from_millis(1)),
            Err(RecvTimeoutError::Timeout)
        ));
    }

    #[test]
    fn empty_playlist_name_is_rejected_by_worker() {
        let dir = fixture_mount(&[fixture_track("Alpha")], &[]);
        let (worker, _snapshot) = opened_worker(dir.path());
        let (message, _hint) = failure_of(run(
            &worker,
            LibraryCommand::CreatePlaylist {
                name: "   ".to_owned(),
            },
        ));
        assert!(!message.is_empty());
        worker.close_and_join();
    }

    #[test]
    fn playlist_draft_is_send_across_threads() {
        // Guards the core design constraint: commands must stay Send.
        fn assert_send<T: Send>() {}
        assert_send::<LibraryCommand>();
        assert_send::<LibrarySnapshot>();
        let draft = PlaylistDraft::new("x", Vec::new()).unwrap();
        let command = LibraryCommand::CreatePlaylist {
            name: draft.name.clone(),
        };
        let (tx, rx) = channel();
        std::thread::spawn(move || tx.send(command).unwrap()).join().unwrap();
        assert!(rx.recv_timeout(TIMEOUT).is_ok());
    }

    /// Two-track fake for the batch «Задать исполнителя» command: ids in
    /// `fail_ids` refuse the edit (stale-selection shape), the rest apply
    /// the artist in memory like a real backend would. `edit_calls`
    /// counts edit_track invocations — the idempotency guard is proven
    /// by skipped edits, not just by the summary text.
    struct ArtistBackend {
        tracks: Vec<Track>,
        fail_ids: Vec<TrackId>,
        edit_calls: usize,
    }

    impl ArtistBackend {
        fn two_tracks() -> Self {
            Self {
                tracks: vec![track_for_artist_test(1, "Track One"), track_for_artist_test(2, "Track Two")],
                fail_ids: Vec::new(),
                edit_calls: 0,
            }
        }
    }

    fn track_for_artist_test(bits: u64, title: &str) -> Track {
        Track {
            id: TrackId::from_bits(bits),
            title: title.to_owned(),
            artist: None,
            album: None,
            album_artist: None,
            duration_ms: 90_000,
            track_number: 1,
            disc_number: 1,
            size_bytes: 1024,
            has_artwork: false,
            media_kind: 1,
        }
    }

    impl LibraryBackend for ArtistBackend {
        fn tracks(&self) -> Result<Vec<Track>, BackendError> {
            Ok(self.tracks.clone())
        }

        fn playlists(&self) -> Result<Vec<Playlist>, BackendError> {
            Ok(Vec::new())
        }

        fn add_track(&mut self, _draft: &TrackDraft) -> Result<TrackId, BackendError> {
            Err(BackendError::TrackNotFound(TrackId::from_bits(0)))
        }

        fn remove_track(&mut self, _id: TrackId, _purge_media: bool) -> Result<bool, BackendError> {
            Ok(true)
        }

        fn edit_track(
            &mut self,
            id: TrackId,
            changes: &TrackChanges,
        ) -> Result<Track, BackendError> {
            self.edit_calls += 1;
            if self.fail_ids.contains(&id) {
                return Err(BackendError::TrackNotFound(id));
            }
            let track = self
                .tracks
                .iter_mut()
                .find(|track| track.id == id)
                .ok_or(BackendError::TrackNotFound(id))?;
            if let Some(artist) = &changes.artist {
                track.artist = Some(artist.clone());
            }
            Ok(track.clone())
        }

        fn create_playlist(&mut self, _draft: &PlaylistDraft) -> Result<PlaylistId, BackendError> {
            Err(BackendError::PlaylistNotFound(PlaylistId::from_bits(0)))
        }

        fn delete_playlist(&mut self, _id: PlaylistId) -> Result<bool, BackendError> {
            Ok(false)
        }

        fn rename_playlist(&mut self, id: PlaylistId, _name: &str) -> Result<(), BackendError> {
            Err(BackendError::PlaylistNotFound(id))
        }

        fn add_to_playlist(
            &mut self,
            _playlist: PlaylistId,
            _track_ids: &[TrackId],
        ) -> Result<(), BackendError> {
            Err(BackendError::PlaylistNotFound(PlaylistId::from_bits(0)))
        }

        fn remove_from_playlist(
            &mut self,
            _playlist: PlaylistId,
            _track_ids: &[TrackId],
        ) -> Result<(), BackendError> {
            Err(BackendError::PlaylistNotFound(PlaylistId::from_bits(0)))
        }
    }

    #[test]
    fn set_artist_updates_every_selected_track() {
        let mut backend = ArtistBackend::two_tracks();
        let outcome = set_artist_command(
            &mut backend,
            vec![TrackId::from_bits(1), TrackId::from_bits(2)],
            "Котэ".to_owned(),
        );
        match outcome {
            LibraryOutcome::Applied { snapshot, summary } => {
                assert!(summary.contains("Исполнитель задан"), "got: {summary}");
                for track in &snapshot.tracks {
                    assert_eq!(track.artist.as_deref(), Some("Котэ"));
                }
            }
            other => panic!("expected Applied, got {other:?}"),
        }
    }

    #[test]
    fn set_artist_reports_an_honest_failure_tail() {
        let mut backend = ArtistBackend::two_tracks();
        backend.fail_ids = vec![TrackId::from_bits(2)];
        let outcome = set_artist_command(
            &mut backend,
            vec![TrackId::from_bits(1), TrackId::from_bits(2)],
            "Котэ".to_owned(),
        );
        match outcome {
            LibraryOutcome::Applied { summary, .. } => {
                assert!(summary.contains("1 из 2"), "got: {summary}");
                assert!(summary.contains("«Track Two»"), "got: {summary}");
                assert!(summary.contains("не удалось"), "got: {summary}");
            }
            other => panic!("expected Applied with an honest tail, got {other:?}"),
        }
    }

    #[test]
    fn set_artist_with_no_successes_fails_honestly() {
        let mut backend = ArtistBackend::two_tracks();
        backend.fail_ids = vec![TrackId::from_bits(1), TrackId::from_bits(2)];
        let outcome = set_artist_command(
            &mut backend,
            vec![TrackId::from_bits(1), TrackId::from_bits(2)],
            "Котэ".to_owned(),
        );
        match outcome {
            LibraryOutcome::Failed { message, .. } => {
                assert!(message.contains("не удалось"), "got: {message}");
                assert!(message.contains("«Track One»"), "got: {message}");
            }
            other => panic!("expected Failed, got {other:?}"),
        }
    }

    /// N5 (idempotency): a track that already carries the target artist
    /// must be skipped WITHOUT an edit (its pid must not rotate) and the
    /// summary must say so explicitly.
    #[test]
    fn set_artist_skips_tracks_that_already_have_the_artist() {
        let mut backend = ArtistBackend::two_tracks();
        backend.tracks[0].artist = Some("Котэ".to_owned());
        let outcome = set_artist_command(
            &mut backend,
            vec![TrackId::from_bits(1), TrackId::from_bits(2)],
            "Котэ".to_owned(),
        );
        match outcome {
            LibraryOutcome::Applied { snapshot, summary } => {
                assert!(summary.contains("уже с исполнителем: 1"), "got: {summary}");
                assert_eq!(backend.edit_calls, 1, "unchanged track must not be edited");
                assert_eq!(snapshot.track(TrackId::from_bits(1)).unwrap().artist.as_deref(), Some("Котэ"));
                assert_eq!(snapshot.track(TrackId::from_bits(2)).unwrap().artist.as_deref(), Some("Котэ"));
            }
            other => panic!("expected Applied, got {other:?}"),
        }
    }

    /// All tracks already carry the artist: no edits at all, and the
    /// outcome is an honest Applied «уже задан», not a fake success
    /// count or an error.
    #[test]
    fn set_artist_when_everything_is_unchanged_reports_already_set() {
        let mut backend = ArtistBackend::two_tracks();
        for track in &mut backend.tracks {
            track.artist = Some("Котэ".to_owned());
        }
        let outcome = set_artist_command(
            &mut backend,
            vec![TrackId::from_bits(1), TrackId::from_bits(2)],
            "Котэ".to_owned(),
        );
        match outcome {
            LibraryOutcome::Applied { summary, .. } => {
                assert!(summary.contains("уже задан"), "got: {summary}");
                assert_eq!(backend.edit_calls, 0, "nothing to edit");
            }
            other => panic!("expected Applied «уже задан», got {other:?}"),
        }
    }
}