aboutsummaryrefslogtreecommitdiff
path: root/app/tests/test_hub.py
blob: d6340daf066ecdb85eaabf751b528fd84b5ecbde (plain)
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
"""Tests for the TUI hub (ui/hub.py) menu and helpers.

The hub drives the same curses widgets as ui/tui.py, so these tests reuse
the fake curses/screen from test_tui to run the menu without a terminal.
"""

import json
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch

from backends import BackendInfo, BackendStatus, ServerSpec
from tests.test_tui import FakeCurses, FakeScreen
from ui import hub, tui


class _ScriptedTUI:
    """Stand-in for the tui widget module: answers each menu/line_edit/
    confirm/form call from a scripted answer list and records every prompt."""

    def __init__(self):
        self.script = []
        self.prompts = []
        self.options_seen = []
        self.flashes = []
        self.form_script = []   # values dicts / None / sentinel for tui.form
        self.forms_seen = []    # (title, fields, kwargs) for tui.form

    def _next(self, prompt, options=None):
        self.prompts.append(prompt)
        if options is not None:
            self.options_seen.append(options)
        return self.script.pop(0)

    def menu(self, stdscr, title, options, **kwargs):
        return self._next(title, options)

    def line_edit(self, stdscr, title, default, **kwargs):
        self.prompts.append(f"{title} [default: {default!r}]")
        return self.script.pop(0)

    def confirm(self, stdscr, question, **kwargs):
        return self._next(question)

    def form(self, stdscr, title, fields, **kwargs):
        self.forms_seen.append((title, fields, kwargs))
        return self.form_script.pop(0)

    def flash(self, stdscr, text, kind="warn"):
        self.flashes.append(text)


class HubHelperTests(unittest.TestCase):
    """Pure helpers in hub.py (no curses)."""

    def test_is_float(self):
        self.assertTrue(hub._is_float("1.0"))
        self.assertTrue(hub._is_float("2"))
        self.assertFalse(hub._is_float("abc"))
        self.assertFalse(hub._is_float(""))

    def test_list_voices_from_dir(self):
        with __import__("tempfile").TemporaryDirectory() as td:
            d = Path(td)
            (d / "Narrator.wav").write_bytes(b"x")
            (d / "Alpha.WAV").write_bytes(b"x")
            (d / "notes.txt").write_bytes(b"x")
            voices = hub._list_voices(str(d))
        # Stems preserve case; sorting is case-insensitive.
        self.assertEqual(voices, ["Alpha", "Narrator"])

    def test_list_voices_missing_dir(self):
        self.assertEqual(hub._list_voices("/no/such/dir"), [])

    def test_status_mark(self):
        from backends import BackendStatus
        local = BackendStatus("k", "l", installed=True, configured=True,
                              running=True, managed=True)
        remote = BackendStatus("k", "l", installed=True, configured=True,
                               running=True, remote=True)
        both = BackendStatus("k", "l", installed=True, configured=True,
                             running=True, managed=True, remote=True)
        models = BackendStatus("k", "l", installed=True, configured=True,
                               running=True, managed=True,
                               running_models=["Base", "CustomVoice"])
        remote_models = BackendStatus("k", "l", installed=True,
                                      configured=True, running=True,
                                      remote=True, running_models=["Base"])
        installed = BackendStatus("k", "l", installed=True,
                                  configured=False)
        none = BackendStatus("k", "l", installed=False, configured=False)
        # running beats installed (a server is up even if not configured);
        # only a backend that is neither installed nor running is dimmed.
        self.assertEqual(hub._status_mark(local),
                         ("running [local]", "ok", "body"))
        # A server this tool did not start, found at the remote URL.
        self.assertEqual(hub._status_mark(remote),
                         ("running [remote]", "ok", "body"))
        self.assertEqual(hub._status_mark(both),
                         ("running [local, remote]", "ok", "body"))
        # Multi-model backends name the models that answered.
        self.assertEqual(hub._status_mark(models),
                         ("running [local] (Base, CustomVoice)", "ok", "body"))
        self.assertEqual(hub._status_mark(remote_models),
                         ("running [remote] (Base)", "ok", "body"))
        self.assertEqual(hub._status_mark(installed),
                         ("installed", "warn", "body"))
        self.assertEqual(hub._status_mark(none),
                         ("unavailable", "err", "dim"))
        self.assertEqual(hub._status_mark(None),
                         ("unavailable", "err", "dim"))


class HubMenuTests(unittest.TestCase):
    """Drive the hub's screen stack with a fake screen (no terminal)."""

    def setUp(self):
        tui._THEME.clear()
        self.curses = FakeCurses()
        from unittest.mock import patch as _patch
        self._patcher = _patch.dict("sys.modules", {"curses": self.curses})
        self._patcher.start()
        self.addCleanup(self._patcher.stop)
        self.addCleanup(tui._THEME.clear)

    def _none_status(self, key="k", label="l"):
        from backends import BackendStatus
        return BackendStatus(key, label, installed=False, configured=False)

    def test_quit_returns_none_when_no_backend(self):
        # No backends installed/running: menu is [Configure backends,
        # Settings, Quit]. Quit is the 3rd option (Down twice) then Enter.
        screen = FakeScreen(keys=[FakeCurses.KEY_DOWN, FakeCurses.KEY_DOWN, 10])
        with patch.object(hub, "detect_all", return_value=[]):
            result = hub._Hub(screen).run()
        self.assertIsNone(result)

    def test_menu_has_only_configure_settings_and_quit_without_backends(self):
        # Capture the options handed to tui.menu: with nothing installed or
        # running, Convert/Server must be absent.
        captured = {}

        def fake_menu(stdscr, title, options, **kwargs):
            captured["options"] = options
            return "quit"

        screen = FakeScreen()
        with patch.object(hub.tui, "menu", fake_menu), \
                patch.object(hub, "detect_all", return_value=[]):
            hub._Hub(screen).run()
        labels = [label for label, _ in captured["options"]]
        self.assertEqual(labels, ["Configure backends", "Settings", "Quit"])

    def test_menu_has_all_five_when_one_installed(self):
        captured = {}

        def fake_menu(stdscr, title, options, **kwargs):
            captured["options"] = options
            captured["rows"] = kwargs.get("table_rows")
            return "quit"

        screen = FakeScreen()
        st = self._none_status("qwen", "qwen-tts")
        st.installed = True
        with patch.object(hub.tui, "menu", fake_menu), \
                patch.object(hub, "detect_all", return_value=[st]):
            hub._Hub(screen).run()
        labels = [label for label, _ in captured["options"]]
        self.assertEqual(
            labels,
            ["Convert books", "Configure backends",
             "Start/Stop Backend Servers", "Settings", "Quit"])
        # The status table is passed through, one row per backend.
        self.assertEqual(captured["rows"],
                         [("qwen-tts", "installed", "warn", "body")])

    def test_table_dims_name_when_not_installed_and_not_running(self):
        captured = {}

        def fake_menu(stdscr, title, options, **kwargs):
            captured["rows"] = kwargs.get("table_rows")
            return "quit"

        screen = FakeScreen()
        dead = self._none_status("audiocpp", "audio.cpp")
        external = self._none_status("qwen", "qwen-tts")
        external.running = True
        external.remote = True
        with patch.object(hub.tui, "menu", fake_menu), \
                patch.object(hub, "detect_all",
                             return_value=[dead, external]):
            hub._Hub(screen).run()
        # Unusable backend: dim name. Running-but-not-installed stays
        # bright and is tagged remote (found at its remote URL).
        self.assertEqual(
            captured["rows"],
            [("audio.cpp", "unavailable", "err", "dim"),
             ("qwen-tts", "running [remote]", "ok", "body")])

    def test_menu_hides_server_when_only_running(self):
        # Running but not installed (an external server) still unlocks
        # Convert — but Start/Stop needs the backend on this machine.
        captured = {}

        def fake_menu(stdscr, title, options, **kwargs):
            captured["options"] = options
            return "quit"

        screen = FakeScreen()
        st = self._none_status("qwen", "qwen-tts")
        st.running = True
        with patch.object(hub.tui, "menu", fake_menu), \
                patch.object(hub, "detect_all", return_value=[st]):
            hub._Hub(screen).run()
        labels = [label for label, _ in captured["options"]]
        self.assertEqual(
            labels,
            ["Convert books", "Configure backends", "Settings", "Quit"])

    def test_ffmpeg_warning_shown_when_missing(self):
        # ffmpeg not on PATH → a red notice is passed above the table.
        captured = {}

        def fake_menu(stdscr, title, options, **kwargs):
            captured["notice_lines"] = kwargs.get("notice_lines")
            return "quit"

        screen = FakeScreen()
        with patch.object(hub.tui, "menu", fake_menu), \
                patch.object(hub, "detect_all", return_value=[]), \
                patch.object(hub.shutil, "which", return_value=None):
            hub._Hub(screen).run()
        self.assertEqual(captured["notice_lines"],
                         [("Warning: ffmpeg not installed!", "err")])

    def test_ffmpeg_warning_hidden_when_installed(self):
        # ffmpeg on PATH → no notice is passed at all.
        captured = {}

        def fake_menu(stdscr, title, options, **kwargs):
            captured["notice_lines"] = kwargs.get("notice_lines")
            return "quit"

        screen = FakeScreen()
        with patch.object(hub.tui, "menu", fake_menu), \
                patch.object(hub, "detect_all", return_value=[]), \
                patch.object(hub.shutil, "which", return_value="/usr/bin/ffmpeg"):
            hub._Hub(screen).run()
        self.assertIsNone(captured["notice_lines"])

    def test_convert_with_no_available_backend_flashes(self):
        # Installed-but-not-ready backends → Convert is offered, but the
        # convert flow has nothing to list: it flashes a hint (no "Configure
        # backends" detour anymore) and returns to the main menu. Then
        # quit: 5 main-menu options, Quit is the 5th (Down x4).
        from backends import BackendStatus
        statuses = [BackendStatus("audiocpp", "audio.cpp", installed=True,
                                  configured=False),
                    BackendStatus("qwen", "qwen-tts", installed=True,
                                  configured=False),
                    BackendStatus("faster", "faster", installed=True,
                                  configured=False)]
        flashed = []

        def fake_flash(stdscr, text, kind="warn"):
            flashed.append(text)

        with patch.object(hub, "detect_all", return_value=statuses), \
                patch.object(hub.tui, "flash", fake_flash):
            # Convert(Enter) → flash → main menu; Down x4 -> Quit, Enter.
            screen = FakeScreen(keys=[10,
                                      FakeCurses.KEY_DOWN, FakeCurses.KEY_DOWN,
                                      FakeCurses.KEY_DOWN, FakeCurses.KEY_DOWN,
                                      10])
            result = hub._Hub(screen).run()
        self.assertIsNone(result)
        self.assertEqual(len(flashed), 1)
        self.assertIn("No backend is ready", flashed[0])


class SubmenuStatusTableTests(unittest.TestCase):
    """First picker screen of every flow repeats the backend status table.

    Entries themselves stay clean: the configure-backends menu lists flat
    actions, and the Start/Stop menu offers only installed backends.
    """

    def _capture_menu(self, captured):
        def fake_menu(stdscr, title, options, **kwargs):
            captured["title"] = title
            captured["options"] = options
            captured.update(kwargs)
            return tui.Wizard.BACK  # Esc: back out immediately

        return fake_menu

    def _capture_form(self, captured):
        def fake_form(stdscr, title, fields, **kwargs):
            captured["title"] = title
            captured["fields"] = fields
            captured.update(kwargs)
            return tui.Wizard.BACK  # Cancel: back out immediately

        return fake_form

    def test_configure_backends_menu_lists_actions_and_status_table(self):
        captured = {}
        infos = [BackendInfo("qwen", "qwen-tts", lambda: None, lambda: 0),
                 BackendInfo("faster", "faster-qwen3-tts", lambda: None,
                             lambda: 0)]
        statuses = [
            BackendStatus("qwen", "qwen-tts", installed=True,
                          configured=True),
            BackendStatus("faster", "faster-qwen3-tts", installed=False,
                          configured=False, running=True, remote=True),
        ]
        with patch.object(hub, "REGISTRY", infos), \
                patch.object(hub, "detect_all", return_value=statuses), \
                patch.object(hub.tui, "menu",
                             self._capture_menu(captured)), \
                patch.object(hub.shutil, "which",
                             return_value="/usr/bin/ffmpeg"):
            result = hub._Hub(None).screen_configure()
        self.assertIs(result, tui.Wizard.BACK)
        # Configure (qwen installed), Install (faster uninstalled), then
        # Uninstall (qwen installed); no audio.cpp means no model actions.
        self.assertEqual([label for label, _ in captured["options"]],
                         ["Configure qwen-tts", "Install Backend",
                          "Uninstall Backend"])
        # ...the shared status table carries the states instead.
        self.assertEqual(captured["table_title"], "Backend status")
        self.assertEqual(
            captured["table_rows"],
            [("qwen-tts", "installed", "warn", "body"),
             ("faster-qwen3-tts", "running [remote]", "ok", "body")])
        self.assertIsNone(captured["notice_lines"])

    def test_configure_backends_menu_install_only_when_nothing_installed(self):
        captured = {}
        infos = [BackendInfo("qwen", "qwen-tts", lambda: None, lambda: 0)]
        statuses = [BackendStatus("qwen", "qwen-tts", installed=False,
                                  configured=False)]
        with patch.object(hub, "REGISTRY", infos), \
                patch.object(hub, "detect_all", return_value=statuses), \
                patch.object(hub.tui, "menu",
                             self._capture_menu(captured)), \
                patch.object(hub.shutil, "which", return_value="/x"):
            result = hub._Hub(None).screen_configure()
        self.assertIs(result, tui.Wizard.BACK)
        # Nothing installed: only the install entry is offered.
        self.assertEqual([label for label, _ in captured["options"]],
                         ["Install Backend"])

    def test_configure_backends_menu_audiocpp_model_actions(self):
        captured = {}
        infos = [BackendInfo("audiocpp", "audio.cpp", lambda: None, lambda: 0)]
        statuses = [BackendStatus("audiocpp", "audio.cpp", installed=True,
                                  configured=True)]
        with tempfile.TemporaryDirectory() as td:
            checkout = Path(td)
            (checkout / "server.json").write_text(json.dumps({
                "models": [{"id": "present", "path": "models/present"},
                           {"id": "absent", "path": "models/absent"}],
            }), encoding="utf-8")
            (checkout / "models" / "present").mkdir(parents=True)
            (checkout / "models" / "present" / "m.gguf").write_bytes(b"x")
            with patch.object(hub, "REGISTRY", infos), \
                    patch.object(hub, "detect_all", return_value=statuses), \
                    patch.object(hub.tui, "menu",
                                 self._capture_menu(captured)), \
                    patch.object(hub.audiocpp_backend, "find_local_checkout",
                                 return_value=checkout), \
                    patch.object(hub.shutil, "which", return_value="/x"):
                result = hub._Hub(None).screen_configure()
        self.assertIs(result, tui.Wizard.BACK)
        labels = [label for label, _ in captured["options"]]
        # A model is missing (download), plus the installed backend's
        # configure + uninstall entries. Deleting unused models now lives
        # inside the "Configure audio.cpp" wizard, not here.
        self.assertEqual(
            labels,
            ["Configure audio.cpp", "Download Missing Models (audio.cpp)",
             "Uninstall Backend"])

    def test_convert_menu_builds_one_form_with_backend_field(self):
        captured = {}
        st = BackendStatus("qwen", "qwen-tts", installed=True,
                           configured=True)
        with patch.object(hub.tui, "form",
                          self._capture_form(captured)), \
                patch.object(hub, "detect_all", return_value=[st]), \
                patch.object(hub.shutil, "which", return_value="/x"):
            result = hub._Hub(None).screen_convert()
        self.assertIs(result, tui.Wizard.BACK)
        self.assertEqual(captured["title"], "Convert books")
        # One form, no picker menu: the first field is the Backend picker,
        # and only convertible backends are offered in it.
        self.assertEqual(captured["fields"][0]["key"], "backend")
        self.assertEqual(captured["fields"][0]["choices"],
                         [("qwen-tts", "qwen")])
        self.assertEqual(captured["buttons"], ("Generate!", "Cancel"))
        self.assertTrue(captured["start_on_buttons"])

    def test_convert_with_nothing_ready_flashes_instead_of_menu(self):
        # Installed-but-unconfigured → nothing convertible: a hint flash
        # replaces the old fallback menu entirely.
        flashed = []
        menus = []

        def fake_menu(*args, **kwargs):
            menus.append((args, kwargs))
            return tui.Wizard.BACK

        def fake_flash(stdscr, text, kind="warn"):
            flashed.append(text)

        st = BackendStatus("qwen", "qwen-tts", installed=True,
                           configured=False)
        with patch.object(hub.tui, "menu", fake_menu), \
                patch.object(hub.tui, "flash", fake_flash), \
                patch.object(hub, "detect_all", return_value=[st]):
            result = hub._Hub(None).screen_convert()
        self.assertIs(result, tui.Wizard.BACK)
        self.assertEqual(menus, [])
        self.assertIn("No backend is ready", flashed[0])

    def test_configure_backends_menu_shows_status_table(self):
        captured = {}
        infos = [BackendInfo("qwen", "qwen-tts", lambda: None, lambda: 0)]
        statuses = [BackendStatus("qwen", "qwen-tts", installed=True,
                                  configured=True)]
        with patch.object(hub, "REGISTRY", infos), \
                patch.object(hub, "detect_all", return_value=statuses), \
                patch.object(hub.tui, "menu",
                             self._capture_menu(captured)), \
                patch.object(hub.shutil, "which", return_value="/x"):
            result = hub._Hub(None).screen_configure()
        self.assertIs(result, tui.Wizard.BACK)
        self.assertEqual(captured["table_title"], "Backend status")
        self.assertEqual(
            captured["table_rows"], [("qwen-tts", "installed", "warn",
                                      "body")])

    def test_server_menu_lists_only_installed_backends(self):
        captured = {}
        installed = BackendStatus("audiocpp", "audio.cpp", installed=True,
                                  configured=True)
        remote = BackendStatus("qwen", "qwen-tts", installed=False,
                               configured=False, running=True)
        gone = BackendStatus("faster", "faster-qwen3-tts", installed=False,
                             configured=False)
        with patch.object(hub.tui, "menu",
                          self._capture_menu(captured)), \
                patch.object(hub, "detect_all",
                             return_value=[installed, remote, gone]), \
                patch.object(hub.shutil, "which", return_value="/x"):
            result = hub._Hub(None).screen_server()
        self.assertIs(result, tui.Wizard.BACK)
        # Only the installed backend is offered; a running external server
        # (remote) can't be stopped from here and must not appear.
        self.assertEqual([label for label, _ in captured["options"]],
                         ["audio.cpp"])
        # The status table still shows all three, states included.
        self.assertEqual([row[0] for row in captured["table_rows"]],
                         ["audio.cpp", "qwen-tts", "faster-qwen3-tts"])

    def test_server_menu_flashes_when_nothing_installed(self):
        flashed = []

        def fake_flash(stdscr, text, kind="warn"):
            flashed.append(text)

        remote = BackendStatus("qwen", "qwen-tts", installed=False,
                               configured=False, running=True)
        with patch.object(hub.tui, "flash", fake_flash), \
                patch.object(hub, "detect_all", return_value=[remote]):
            result = hub._Hub(None).screen_server()
        self.assertIs(result, tui.Wizard.BACK)
        self.assertEqual(len(flashed), 1)
        self.assertIn("No backend is installed", flashed[0])

    def test_submenu_repeats_ffmpeg_warning(self):
        captured = {}
        infos = [BackendInfo("qwen", "qwen-tts", lambda: None, lambda: 0)]
        statuses = [BackendStatus("qwen", "qwen-tts", installed=True,
                                  configured=True)]
        with patch.object(hub, "REGISTRY", infos), \
                patch.object(hub, "detect_all", return_value=statuses), \
                patch.object(hub.tui, "menu",
                             self._capture_menu(captured)), \
                patch.object(hub.shutil, "which", return_value=None):
            hub._Hub(None).screen_configure()
        self.assertEqual(captured["notice_lines"],
                         [("Warning: ffmpeg not installed!", "err")])


class ConvertFlowTests(unittest.TestCase):
    """_convert_form / screen_convert: one form whose first field is the
    Backend picker, followed by that backend's options (local config or live
    remote queries)."""

    def setUp(self):
        self.tui = _ScriptedTUI()
        for name in ("menu", "line_edit", "confirm", "form", "flash"):
            patcher = patch.object(hub.tui, name, getattr(self.tui, name))
            patcher.start()
            self.addCleanup(patcher.stop)

    def _form_values(self, **overrides):
        """A fully-populated form result, with sensible defaults."""
        values = {"output_format": "m4b", "speed": "1.0",
                  "single_file": False, "debug": False}
        values.update(overrides)
        return values

    def _answer_form(self, **overrides):
        self.tui.form_script.append(self._form_values(**overrides))

    def _field(self, key, form_index=-1):
        _, fields, _ = self.tui.forms_seen[form_index]
        return next(f for f in fields if f["key"] == key)

    def _ready(self, key, label):
        """A backend status that is ready to convert with."""
        return BackendStatus(key, label, installed=True, configured=True)

    def _convert(self, stdscr, statuses):
        """Run the convert flow with STATUSES, returning the command tuple.

        ``_run_conversion`` is stubbed so the accepted command is captured
        instead of launching the run view; None is returned when the flow
        aborts before reaching a conversion (nothing ready, a flash).
        """
        captured = {}

        def fake_run_conversion(self_, backend, kwargs):
            captured["backend"] = backend
            captured["kwargs"] = kwargs

        with patch.object(hub, "detect_all", return_value=statuses), \
                patch.object(hub._Hub, "_run_conversion",
                             fake_run_conversion):
            hub._Hub(None).screen_convert()
        if "backend" not in captured:
            return None
        return ("convert", captured["backend"], captured["kwargs"])

    # ------------------------------------------------------------------
    # audio.cpp: remote server (no local checkout / server.json)
    # ------------------------------------------------------------------

    def _patch_remote(self, models, voices=None):
        """Fetch helpers return MODELS/VOICES for a remote audio.cpp server."""
        fetched_models = patch.object(hub.audiocpp_backend,
                                      "fetch_server_models",
                                      lambda url: models)
        fetched_voices = patch.object(hub.audiocpp_backend,
                                      "fetch_server_voices",
                                      lambda url, model_id: voices)
        for patcher in (fetched_models, fetched_voices):
            patcher.start()
            self.addCleanup(patcher.stop)

    def _remote(self, key, label, spec_name=None, url=None,
                remote_urls=None, remote_models=None):
        """A running remote backend status (not installed on this machine)."""
        if remote_urls is None:
            remote_urls = {spec_name or key:
                           url or f"http://{key}.local:8080"}
        return BackendStatus(key, label, installed=False, configured=False,
                             running=True, remote=True,
                             remote_urls=remote_urls,
                             remote_models=list(remote_models or []))

    def test_audiocpp_remote_builds_one_form(self):
        self._patch_remote(
            [{"id": "higgs", "family": "higgs_audio_tts", "task": "tts"}],
            voices=["narrator"])
        with patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
            self._answer_form(backend="audiocpp-remote", model_id="higgs",
                              audiocpp_voice="narrator", instructions="",
                              speed="1.5")
            cmd = self._convert(
                None, [self._remote("audiocpp", "audio.cpp")])
        self.assertEqual(cmd[0], "convert")
        self.assertEqual(cmd[1], hub.BACKEND_AUDIOCPP)
        kwargs = cmd[2]
        self.assertEqual(kwargs["model_id"], "higgs")
        self.assertEqual(kwargs["voice"], "narrator")
        self.assertIsNone(kwargs["instructions"])
        self.assertEqual(kwargs["api_url"], "http://audiocpp.local:8080")
        self.assertEqual(kwargs["output_format"], "m4b")
        self.assertEqual(kwargs["speed"], 1.5)
        self.assertFalse(kwargs["single_file"])
        self.assertFalse(kwargs["debug"])
        # One form, not a cascade of menus/editors.
        self.assertEqual(len(self.tui.forms_seen), 1)
        title, fields, form_kwargs = self.tui.forms_seen[0]
        self.assertEqual(title, "Convert books")
        self.assertEqual([f["key"] for f in fields],
                         ["backend", "model_id", "audiocpp_voice",
                          "instructions", "output_format", "speed",
                          "single_file", "debug"])
        self.assertEqual(form_kwargs["buttons"], ("Generate!", "Cancel"))
        self.assertTrue(form_kwargs["start_on_buttons"])
        # The backend field offers the remote entry under a [remote] label.
        self.assertEqual(fields[0]["choices"],
                         [("audio.cpp [remote]", "audiocpp-remote")])
        # The model menu was fed from the live query (label, id).
        self.assertEqual(self._field("model_id")["choices"],
                         [("higgs  (higgs_audio_tts, tts)", "higgs")])

    def test_audiocpp_qwen3_tts_voice_choices_lead_with_builtin_speaker(self):
        self._patch_remote(
            [{"id": "qwen", "family": "qwen3_tts", "task": "tts"}],
            voices=["narrator"])
        with patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
            self._answer_form(backend="audiocpp-remote", model_id="qwen",
                              audiocpp_voice="(built-in speaker)",
                              instructions="")
            cmd = self._convert(
                None, [self._remote("audiocpp", "audio.cpp")])
        # The sentinel maps to "no voice" (built-in speaker).
        self.assertIsNone(cmd[2]["voice"])
        fields = self.tui.forms_seen[0][1]
        voice_field = self._field("audiocpp_voice")
        choices = voice_field["choices"](fields)
        self.assertEqual(choices,
                         [("(built-in speaker)", "(built-in speaker)"),
                          ("narrator", "narrator")])

    def test_audiocpp_remote_missing_family_treated_as_qwen3_tts(self):
        # Legacy servers omit family/task; the converter defaults them to
        # qwen3_tts/tts and so must the form (voice optional).
        self._patch_remote([{"id": "legacy", "family": "", "task": ""}],
                           voices=[])
        with patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
            self._answer_form(backend="audiocpp-remote", model_id="legacy",
                              audiocpp_voice="(built-in speaker)",
                              instructions="")
            cmd = self._convert(
                None, [self._remote("audiocpp", "audio.cpp")])
        self.assertIsNotNone(cmd)
        self.assertIsNone(cmd[2]["voice"])

    def test_audiocpp_vdes_hides_voice_and_requires_instructions(self):
        self._patch_remote(
            [{"id": "design", "family": "qwen3_tts", "task": "vdes"}])
        with patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
            self._answer_form(backend="audiocpp-remote", model_id="design",
                              audiocpp_voice=None,
                              instructions="A warm British narrator")
            cmd = self._convert(
                None, [self._remote("audiocpp", "audio.cpp")])
        self.assertIsNone(cmd[2]["voice"])
        self.assertEqual(cmd[2]["instructions"], "A warm British narrator")
        fields = self.tui.forms_seen[0][1]
        voice_field = self._field("audiocpp_voice")
        self.assertFalse(voice_field["visible"](fields))
        instr = self._field("instructions")
        self.assertIsNotNone(instr["validate"](""))
        self.assertIsNone(instr["validate"]("describe me"))

    def test_audiocpp_required_voice_validates(self):
        # A non-qwen3_tts family needs a --voice; a blank value refuses.
        self._patch_remote(
            [{"id": "higgs", "family": "higgs_audio_tts", "task": "tts"}],
            voices=["narrator"])
        with patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
            self._answer_form(backend="audiocpp-remote", model_id="higgs",
                              audiocpp_voice="narrator", instructions="")
            self._convert(None,
                              [self._remote("audiocpp", "audio.cpp")])
        voice_field = self._field("audiocpp_voice")
        self.assertIsNotNone(voice_field["validate"](""))
        self.assertIsNone(voice_field["validate"]("narrator"))

    def test_audiocpp_remote_unreachable_models_flash_and_abort(self):
        self._patch_remote(None)  # endpoint did not answer valid JSON
        cmd = self._convert(
            None, [self._remote("audiocpp", "audio.cpp")])
        self.assertIsNone(cmd)
        self.assertIn("Could not list models", self.tui.flashes[0])

    def test_audiocpp_remote_empty_models_flash_and_abort(self):
        self._patch_remote([])
        cmd = self._convert(
            None, [self._remote("audiocpp", "audio.cpp")])
        self.assertIsNone(cmd)
        self.assertIn("hosts no model entries", self.tui.flashes[0])

    def test_audiocpp_remote_no_server_voices_leaves_voice_blank(self):
        # No voices listed for a required-voice model: the form still opens
        # with an empty Voice field (Generate-time validation reports it).
        self._patch_remote(
            [{"id": "higgs", "family": "higgs_audio_tts", "task": "tts"}],
            voices=[])
        with patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
            self._answer_form(backend="audiocpp-remote", model_id="higgs",
                              audiocpp_voice="", instructions="")
            cmd = self._convert(
                None, [self._remote("audiocpp", "audio.cpp")])
        self.assertIsNotNone(cmd)
        self.assertIsNone(cmd[2]["voice"])
        fields = self.tui.forms_seen[0][1]
        voice_field = self._field("audiocpp_voice")
        self.assertEqual(voice_field["choices"](fields), [])

    # ------------------------------------------------------------------
    # audio.cpp: local managed setup keeps reading its server.json
    # ------------------------------------------------------------------

    def test_audiocpp_local_still_reads_server_json(self):
        queried = []

        def must_not_query(url):
            queried.append(url)
            raise AssertionError("live query on the local path")

        with tempfile.TemporaryDirectory() as td:
            root = Path(td)
            (root / "server.json").write_text(json.dumps({
                "models": [{"id": "qwen", "family": "qwen3_tts",
                            "task": "tts"}],
                "voice_dir": str(root),
            }), encoding="utf-8")
            (root / "Narrator.wav").write_bytes(b"x")
            with patch.object(hub.audiocpp_backend, "find_local_checkout",
                              return_value=root), \
                    patch.object(hub.audiocpp_backend, "fetch_server_models",
                                 must_not_query), \
                    patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
                self._answer_form(backend="audiocpp", model_id="qwen",
                                  audiocpp_voice="Narrator", instructions="")
                cmd = self._convert(None,
                                        [self._ready("audiocpp", "audio.cpp")])
        self.assertEqual(queried, [])
        self.assertIsNotNone(cmd)
        self.assertEqual(cmd[2]["model_id"], "qwen")
        self.assertEqual(cmd[2]["voice"], "Narrator")

    def test_managed_and_remote_both_offered(self):
        # A ready managed audio.cpp (server.json) AND a running remote
        # audio.cpp: both entries appear. The managed entry reads server.json
        # (no api_url), the remote entry live-queries (api_url set).
        self._patch_remote(
            [{"id": "higgs", "family": "higgs_audio_tts", "task": "tts"}],
            voices=["narrator"])
        with tempfile.TemporaryDirectory() as td:
            root = Path(td)
            (root / "server.json").write_text(json.dumps({
                "models": [{"id": "qwen", "family": "qwen3_tts",
                            "task": "tts"}],
            }), encoding="utf-8")
            with patch.object(hub.audiocpp_backend, "find_local_checkout",
                              return_value=root), \
                    patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
                self._answer_form(backend="audiocpp", model_id="qwen",
                                  audiocpp_voice="(built-in speaker)",
                                  instructions="")
                cmd = self._convert(None, [
                    self._ready("audiocpp", "audio.cpp"),
                    self._remote("audiocpp", "audio.cpp")])
        self.assertEqual(cmd[0], "convert")
        self.assertEqual(cmd[1], hub.BACKEND_AUDIOCPP)
        # Managed entry: no api_url override (uses the configured local URL).
        self.assertNotIn("api_url", cmd[2])
        self.assertEqual(cmd[2]["model_id"], "qwen")
        fields = self.tui.forms_seen[0][1]
        self.assertEqual(fields[0]["choices"],
                         [("audio.cpp", "audiocpp"),
                          ("audio.cpp [remote]", "audiocpp-remote")])

    def test_audiocpp_remote_mapper_adds_api_url(self):
        self._patch_remote(
            [{"id": "higgs", "family": "higgs_audio_tts", "task": "tts"}],
            voices=["narrator"])
        with patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
            self._answer_form(backend="audiocpp-remote", model_id="higgs",
                              audiocpp_voice="narrator", instructions="")
            cmd = self._convert(
                None, [self._remote("audiocpp", "audio.cpp",
                                    url="http://10.0.0.5:8080")])
        self.assertEqual(cmd[2]["api_url"], "http://10.0.0.5:8080")

    # ------------------------------------------------------------------
    # common fields: output format, speed, single-file, debug
    # ------------------------------------------------------------------

    def test_common_fields_hide_combine_for_m4b(self):
        fields = hub._common_fields()
        fmt = next(f for f in fields if f["key"] == "output_format")
        single = next(f for f in fields if f["key"] == "single_file")
        fmt["value"] = "m4b"
        self.assertFalse(single["visible"](fields))
        fmt["value"] = "mp3"
        self.assertTrue(single["visible"](fields))

    # ------------------------------------------------------------------
    # qwen: speaker or clone
    # ------------------------------------------------------------------

    def test_qwen_builds_speaker_and_clone_form(self):
        with patch.object(hub.qwen_backend, "QWEN_SPEAKERS",
                          ["Vivian", "Serena"]), \
                patch.object(hub.config, "SPEAKER", "Vivian"):
            self._answer_form(backend="qwen", mode="custom", speaker="Serena",
                              clone="")
            with patch.object(hub.common, "update_config_value") as mk_update:
                cmd = self._convert(None,
                                        [self._ready("qwen", "qwen-tts")])
            speaker_in_memory = hub.config.SPEAKER
        self.assertEqual(cmd[0], "convert")
        self.assertEqual(cmd[1], hub.BACKEND_QWEN)
        self.assertIsNone(cmd[2]["clone"])
        # The speaker choice is persisted for future runs too.
        mk_update.assert_called_once_with("SPEAKER", "Serena")
        self.assertEqual(speaker_in_memory, "Serena")
        fields = self.tui.forms_seen[0][1]
        self.assertEqual([f["key"] for f in fields],
                         ["backend", "mode", "speaker", "clone",
                          "output_format", "speed", "single_file", "debug"])
        mode_field = self._field("mode")
        self.assertEqual(mode_field["choices"],
                         [("Built-in speaker", "custom"),
                          ("Clone from a .wav file", "clone")])
        speaker_field = self._field("speaker")
        clone_field = self._field("clone")
        # Speaker shows in custom mode; the .wav path shows in clone mode.
        self.assertTrue(speaker_field["visible"](fields))
        self.assertFalse(clone_field["visible"](fields))
        mode_field["value"] = "clone"
        self.assertFalse(speaker_field["visible"](fields))
        self.assertTrue(clone_field["visible"](fields))

    def test_qwen_clone_mode_passes_path_and_keeps_speaker(self):
        with patch.object(hub.qwen_backend, "QWEN_SPEAKERS", ["Vivian"]), \
                patch.object(hub.config, "SPEAKER", "Vivian"):
            self._answer_form(backend="qwen", mode="clone", speaker="Vivian",
                              clone="/tmp/ref.wav")
            with patch.object(hub.common, "update_config_value") as mk_update:
                cmd = self._convert(None,
                                        [self._ready("qwen", "qwen-tts")])
        self.assertEqual(cmd[2]["clone"], "/tmp/ref.wav")
        # Clone mode does not touch the global speaker.
        mk_update.assert_not_called()

    # ------------------------------------------------------------------
    # faster: remote server (no local voices.json)
    # ------------------------------------------------------------------

    def test_faster_remote_uses_a_text_voice_field(self):
        with tempfile.TemporaryDirectory() as td:
            with patch.object(hub.faster_backend, "_checkout",
                              return_value=Path(td)):
                self._answer_form(backend="faster", faster_voice="obama")
                cmd = self._convert(
                    None, [self._ready("faster", "faster-qwen3-tts")])
        self.assertEqual(cmd[0], "convert")
        self.assertEqual(cmd[1], "faster")
        self.assertEqual(cmd[2]["voice"], "obama")
        self.assertEqual(self._field("faster_voice")["kind"], "text")

    def test_faster_local_still_lists_voices_json(self):
        with tempfile.TemporaryDirectory() as td:
            checkout = Path(td)
            (checkout / "voices.json").write_text(
                json.dumps({"default": {}, "obama": {}}), encoding="utf-8")
            with patch.object(hub.faster_backend, "_checkout",
                              return_value=checkout):
                self._answer_form(backend="faster", faster_voice="obama")
                cmd = self._convert(
                    None, [self._ready("faster", "faster-qwen3-tts")])
        self.assertEqual(cmd[2]["voice"], "obama")
        voice_field = self._field("faster_voice")
        self.assertEqual(voice_field["kind"], "choice")
        self.assertEqual(voice_field["choices"],
                         [("default", "default"), ("obama", "obama")])

    def test_faster_remote_uses_text_voice_and_api_url(self):
        st = self._remote("faster", "faster-qwen3-tts",
                          url="http://10.0.0.5:8000")
        self._answer_form(backend="faster-remote", faster_voice="obama")
        cmd = self._convert(None, [st])
        self.assertEqual(cmd[0], "convert")
        self.assertEqual(cmd[1], "faster")
        self.assertEqual(cmd[2]["voice"], "obama")
        self.assertEqual(cmd[2]["api_url"], "http://10.0.0.5:8000")
        self.assertEqual(self._field("faster_voice")["kind"], "text")

    def test_qwen_remote_limited_modes_and_api_url(self):
        # A remote qwen with only the Base (clone) demo answering: the form
        # offers only clone mode and targets the clone remote URL.
        st = self._remote(
            "qwen", "qwen-tts",
            remote_urls={"qwen-clone": "http://10.0.0.5:7861"},
            remote_models=["Base"])
        with patch.object(hub.qwen_backend, "QWEN_SPEAKERS", ["Vivian"]), \
                patch.object(hub.config, "SPEAKER", "Vivian"):
            self._answer_form(backend="qwen-remote", mode="clone",
                              speaker="Vivian", clone="/tmp/ref.wav")
            cmd = self._convert(None, [st])
        self.assertEqual(cmd[1], hub.BACKEND_QWEN)
        self.assertEqual(cmd[2]["clone"], "/tmp/ref.wav")
        self.assertEqual(cmd[2]["api_url"], "http://10.0.0.5:7861")
        fields = self.tui.forms_seen[0][1]
        self.assertEqual(self._field("mode")["choices"],
                         [("Clone from a .wav file", "clone")])

    # ------------------------------------------------------------------
    # multiple backends: the Backend picker gates which options show
    # ------------------------------------------------------------------

    def test_multiple_backends_gate_options_on_backend_value(self):
        # Two ready backends: the form leads with a Backend picker and the
        # per-backend fields are hidden/shown by its value.
        with tempfile.TemporaryDirectory() as td:
            root = Path(td)
            (root / "server.json").write_text(json.dumps({
                "models": [{"id": "higgs", "family": "higgs_audio_tts",
                            "task": "tts"}],
            }), encoding="utf-8")
            with patch.object(hub.audiocpp_backend, "find_local_checkout",
                              return_value=root), \
                    patch.object(hub.config, "AUDIOCPP_INSTRUCTIONS", ""):
                self._answer_form(backend="qwen", mode="custom",
                                  speaker="Vivian", clone="")
                cmd = self._convert(None, [
                    self._ready("audiocpp", "audio.cpp"),
                    self._ready("qwen", "qwen-tts")])
        self.assertEqual(cmd[0], "convert")
        self.assertEqual(cmd[1], hub.BACKEND_QWEN)
        fields = self.tui.forms_seen[0][1]
        self.assertEqual(fields[0]["key"], "backend")
        self.assertEqual(fields[0]["choices"],
                         [("audio.cpp", "audiocpp"), ("qwen-tts", "qwen")])
        self.assertEqual(
            [f["key"] for f in fields],
            ["backend", "model_id", "audiocpp_voice", "instructions",
             "mode", "speaker", "clone", "output_format", "speed",
             "single_file", "debug"])
        # The form opens on the configured default (audio.cpp): its fields
        # show, the other backend's hide.
        for key in ("model_id", "audiocpp_voice", "instructions"):
            self.assertTrue(self._field(key)["visible"](fields))
        for key in ("mode", "speaker", "clone"):
            self.assertFalse(self._field(key)["visible"](fields))
        # Picking qwen in the Backend field swaps which options show.
        fields[0]["value"] = "qwen"
        self.assertTrue(self._field("mode")["visible"](fields))
        self.assertTrue(self._field("speaker")["visible"](fields))
        self.assertFalse(self._field("clone")["visible"](fields))
        # qwen's clone mode hides the speaker and shows the .wav path.
        self._field("mode")["value"] = "clone"
        self.assertFalse(self._field("speaker")["visible"](fields))
        self.assertTrue(self._field("clone")["visible"](fields))
        for key in ("model_id", "audiocpp_voice", "instructions"):
            self.assertFalse(self._field(key)["visible"](fields))
        # And back to audio.cpp.
        fields[0]["value"] = "audiocpp"
        for key in ("model_id", "audiocpp_voice", "instructions"):
            self.assertTrue(self._field(key)["visible"](fields))
        for key in ("mode", "speaker", "clone"):
            self.assertFalse(self._field(key)["visible"](fields))


class SelectSpecTests(unittest.TestCase):
    """_select_spec: mode-aware server selection (qwen has two servers)."""

    def _qwen_status(self):
        return BackendStatus(
            "qwen", "qwen-tts", installed=True, configured=True,
            servers=[ServerSpec("qwen-custom", "http://127.0.0.1:7860", []),
                     ServerSpec("qwen-clone", "http://127.0.0.1:7861", [])])

    def test_qwen_custom_mode(self):
        spec = hub._select_spec(self._qwen_status(), {"clone": None})
        self.assertEqual(spec.name, "qwen-custom")

    def test_qwen_clone_mode(self):
        spec = hub._select_spec(self._qwen_status(), {"clone": "ref.wav"})
        self.assertEqual(spec.name, "qwen-clone")

    def test_audiocpp_returns_single_spec(self):
        st = BackendStatus("audiocpp", "audio.cpp", installed=True,
                           configured=True,
                           servers=[ServerSpec("audiocpp", "http://x", [])])
        spec = hub._select_spec(st, {})
        self.assertEqual(spec.name, "audiocpp")

    def test_none_when_no_servers(self):
        st = BackendStatus("qwen", "qwen-tts", installed=False,
                           configured=False)
        self.assertIsNone(hub._select_spec(st, {}))


class PrepareRunConfigTests(unittest.TestCase):
    """_prepare_run_config: the run view's inputs from the accepted form."""

    def _spec(self, name="qwen-custom", url="http://127.0.0.1:7860"):
        return ServerSpec(name, url, ["x"])

    def test_remote_targets_the_api_url(self):
        with patch.object(hub, "detect_all", return_value=[]) as mk_detect:
            cfg = hub._prepare_run_config(
                "audiocpp", {"api_url": "http://10.0.0.5:8080"})
        self.assertEqual(cfg.server_url, "http://10.0.0.5:8080")
        self.assertIsNone(cfg.autostart_spec)
        self.assertEqual(cfg.server_identity, "audiocpp")
        self.assertIn("remote", cfg.backend_label)
        # The remote path never re-detects or touches managed-instance state.
        mk_detect.assert_not_called()

    def test_autostart_sets_the_spec_and_pops_the_flag(self):
        spec = self._spec()
        kwargs = {"autostart": "qwen-custom"}
        with patch.object(hub, "detect_all", return_value=[]), \
                patch.object(hub, "_find_spec", return_value=spec):
            cfg = hub._prepare_run_config("qwen", kwargs)
        self.assertIs(cfg.autostart_spec, spec)
        self.assertEqual(cfg.server_name, "qwen-custom")
        self.assertNotIn("autostart", kwargs)

    def test_autostart_with_missing_spec_continues_with_notice(self):
        kwargs = {"autostart": "gone"}
        with patch.object(hub, "detect_all", return_value=[]), \
                patch.object(hub, "_find_spec", return_value=None):
            cfg = hub._prepare_run_config("qwen", kwargs)
        self.assertIsNone(cfg.autostart_spec)
        self.assertIn("gone", cfg.notice)

    def test_managed_conversion_flags_foreign_server_on_port(self):
        spec = ServerSpec("audiocpp", "http://127.0.0.1:8080", ["x"])
        status = BackendStatus("audiocpp", "audio.cpp", installed=True,
                               configured=True, servers=[spec])
        with patch.object(hub, "detect_all", return_value=[status]), \
                patch("backends.common.server_running", return_value=True), \
                patch.object(hub.servers, "alive", return_value=False):
            cfg = hub._prepare_run_config("audiocpp", {})
        self.assertEqual(cfg.server_url, spec.url)
        self.assertIsNone(cfg.autostart_spec)
        self.assertIn("did not start", cfg.notice)

    def test_managed_not_running_sets_url_without_autostart(self):
        spec = self._spec()
        status = BackendStatus("qwen", "qwen-tts", installed=True,
                               configured=True, servers=[spec])
        with patch.object(hub, "detect_all", return_value=[status]), \
                patch("backends.common.server_running", return_value=False):
            cfg = hub._prepare_run_config("qwen", {"clone": None})
        self.assertEqual(cfg.server_url, spec.url)
        self.assertIsNone(cfg.autostart_spec)


class PreflightTests(unittest.TestCase):
    """_preflight: overwrite prompts run in the TUI, plan stashed in kwargs."""

    def _cmd(self):
        return ("convert", "qwen", {"clone": None, "output_format": "mp3"})

    def test_books_stashed_on_kwargs(self):
        stdscr = object()
        cmd = self._cmd()
        with patch.object(hub.AudiobookConverter, "preflight_overwrites",
                          return_value=(["book.txt"], [("book.txt", "x")])) \
                as mk_pre:
            self.assertTrue(hub._preflight(stdscr, cmd))
        self.assertEqual(cmd[2]["book_files"], ["book.txt"])
        self.assertEqual(cmd[2]["planned"], [("book.txt", "x")])
        # The confirm callback passed to preflight is a TUI yes/no.
        confirm = mk_pre.call_args.kwargs["confirm"]
        with patch.object(hub.tui, "confirm", return_value=True) as mk_confirm:
            self.assertTrue(confirm("overwrite?", True))
        mk_confirm.assert_called_once()

    def test_nothing_to_convert_flashes_and_returns_false(self):
        stdscr = object()
        with patch.object(hub.AudiobookConverter, "preflight_overwrites",
                          return_value=([], [])), \
                patch.object(hub.tui, "flash") as mk_flash:
            self.assertFalse(hub._preflight(stdscr, self._cmd()))
        mk_flash.assert_called_once()

    def test_all_skipped_flashes_and_returns_false(self):
        stdscr = object()
        with patch.object(hub.AudiobookConverter, "preflight_overwrites",
                          return_value=(["book.txt"], [])), \
                patch.object(hub.tui, "flash") as mk_flash:
            self.assertFalse(hub._preflight(stdscr, self._cmd()))
        mk_flash.assert_called_once()

    def test_confirm_esc_raises_back_to_form(self):
        # Esc on an overwrite confirm backs out to the form (one screen),
        # not "No" — which could dump the user on the main menu.
        stdscr = object()
        with patch.object(hub.AudiobookConverter, "preflight_overwrites",
                          return_value=(["book.txt"], [("book.txt", "x")])) \
                as mk_pre:
            hub._preflight(stdscr, self._cmd())
        confirm = mk_pre.call_args.kwargs["confirm"]
        with patch.object(hub.tui, "confirm", return_value=hub._CANCEL):
            with self.assertRaises(hub._BackToForm):
                confirm("overwrite?", True)


class DispatchConversionTests(unittest.TestCase):
    """_Hub._run_conversion: builds the config and runs the run view."""

    def test_runs_run_view_on_the_hub_screen(self):
        timeouts = []

        class Screen:
            def timeout(self, ms):
                timeouts.append(ms)

        class FakeView:
            def __init__(self, scr, config):
                self.config = config
                self.scr = scr
            def run(self):
                pass

        screen = Screen()
        with patch.object(hub, "_prepare_run_config",
                          return_value=hub.runview.RunConfig(
                              backend="qwen", backend_label="qwen-tts",
                              kwargs={}, book_files=[], planned=[])) as mk_cfg, \
                patch.object(hub.runview, "RunView", FakeView):
            hub._Hub(screen)._run_conversion("qwen", {})
        mk_cfg.assert_called_once()
        # The run view leaves a timed getch behind; it is reset so the hub
        # menus block for keys again.
        self.assertEqual(timeouts, [-1])

    def test_no_run_config_skips_the_view(self):
        with patch.object(hub, "_prepare_run_config", return_value=None) as mk_cfg, \
                patch.object(hub.runview, "RunView") as mk_view:
            hub._Hub(None)._run_conversion("qwen", {})
        mk_cfg.assert_called_once()
        mk_view.assert_not_called()


class AddAutostartTests(unittest.TestCase):
    """_add_autostart: always starts the server when it isn't running."""

    def _status(self):
        spec = ServerSpec("qwen-custom", "http://127.0.0.1:7860", ["x"])
        return BackendStatus("qwen", "qwen-tts", installed=True,
                             configured=True, running=False,
                             servers=[spec])

    def test_sets_autostart_when_not_running(self):
        cmd = ("convert", "qwen", {"clone": None})
        with patch.object(hub, "detect_all", return_value=[self._status()]), \
                patch("backends.common.server_running", return_value=False):
            hub._add_autostart(cmd, [self._status()])
        self.assertEqual(cmd[2]["autostart"], "qwen-custom")

    def test_no_autostart_when_server_already_running(self):
        cmd = ("convert", "qwen", {"clone": None})
        with patch.object(hub, "detect_all", return_value=[self._status()]), \
                patch("backends.common.server_running", return_value=True):
            hub._add_autostart(cmd, [self._status()])
        self.assertNotIn("autostart", cmd[2])

    def test_no_autostart_for_remote_conversion(self):
        # A remote conversion (api_url set) never autostarts: the server is
        # external to this tool, so there is nothing to start/stop here.
        cmd = ("convert", "audiocpp", {"api_url": "http://10.0.0.5:8080"})
        hub._add_autostart(cmd, [])
        self.assertNotIn("autostart", cmd[2])


class SettingsTests(unittest.TestCase):
    """Settings menu: field collection, validation, config.py writing."""

    def test_write_config_preserves_comments_and_other_lines(self):
        import tempfile
        with tempfile.TemporaryDirectory() as td:
            path = Path(td) / "config.py"
            path.write_text(
                "# Default output options\n"
                'AUDIO_FORMAT = "m4b"\n'
                'AUDIO_BITRATE = "128k"\n'
                'LANGUAGE = "English"\n'
                "\n"
                "CHUNK_SIZE = 250  # words per request\n",
                encoding="utf-8")
            with patch.object(hub.config, "__file__", str(path)):
                hub._write_config({"AUDIO_FORMAT": "mp3",
                                   "AUDIO_BITRATE": "192k",
                                   "LANGUAGE": "Japanese",
                                   "CHUNK_SIZE": 300})
            text = path.read_text(encoding="utf-8")
        self.assertEqual(
            text,
            "# Default output options\n"
            'AUDIO_FORMAT = "mp3"\n'
            'AUDIO_BITRATE = "192k"\n'
            'LANGUAGE = "Japanese"\n'
            "\n"
            "CHUNK_SIZE = 300  # words per request\n")

    def test_write_config_missing_key_raises(self):
        import tempfile
        with tempfile.TemporaryDirectory() as td:
            path = Path(td) / "config.py"
            path.write_text("X = 1\n", encoding="utf-8")
            with patch.object(hub.config, "__file__", str(path)):
                with self.assertRaises(ValueError):
                    hub._write_config({"AUDIO_FORMAT": "mp3"})

    def test_apply_settings_writes_and_reloads_in_memory(self):
        written = {}

        def fake_write(updates):
            written.update(updates)

        original = {name: getattr(hub.config, name) for name in
                    ("AUDIO_FORMAT", "AUDIO_BITRATE", "LANGUAGE",
                     "CHUNK_SIZE", "AUDIOCPP_UNLOAD_MODELS",
                     "QWEN_API_URL", "CLONE_API_URL",
                     "FASTER_API_URL", "AUDIOCPP_API_URL",
                     "QWEN_REMOTE_URL", "CLONE_REMOTE_URL",
                     "FASTER_REMOTE_URL", "AUDIOCPP_REMOTE_URL")}
        self.addCleanup(lambda: [setattr(hub.config, name, value)
                                 for name, value in original.items()])
        values = {"audio_format": "ogg", "audio_bitrate": " 192k ",
                  "language": "en", "chunk_size": "300",
                  "unload_models": True,
                  "qwen_custom_port": "7862", "qwen_clone_port": "7863",
                  "faster_port": "8001", "audiocpp_port": "8081",
                  "audiocpp_remote_url": "10.0.0.5:8080",
                  "faster_remote_url": "http://10.0.0.6:8000",
                  "qwen_custom_remote_url": "",
                  "qwen_clone_remote_url": ""}
        with patch.object(hub, "_write_config", fake_write), \
                patch.object(hub, "_sync_audiocpp_server_port"):
            hub._apply_settings(values)
        # Values are trimmed and language normalized to a display name;
        # remote URLs are normalized to full http(s) URLs (empty = off).
        self.assertEqual(written, {"AUDIO_FORMAT": "ogg",
                                   "AUDIO_BITRATE": "192k",
                                   "LANGUAGE": "English",
                                   "CHUNK_SIZE": 300,
                                   "AUDIOCPP_UNLOAD_MODELS": True,
                                   "QWEN_API_URL": "http://127.0.0.1:7862",
                                   "CLONE_API_URL": "http://127.0.0.1:7863",
                                   "FASTER_API_URL": "http://127.0.0.1:8001",
                                   "AUDIOCPP_API_URL":
                                       "http://127.0.0.1:8081",
                                   "QWEN_REMOTE_URL": "",
                                   "CLONE_REMOTE_URL": "",
                                   "FASTER_REMOTE_URL":
                                       "http://10.0.0.6:8000",
                                   "AUDIOCPP_REMOTE_URL":
                                       "http://10.0.0.5:8080"})
        # In-memory config is reloaded so this session sees the change.
        self.assertEqual(hub.config.AUDIO_FORMAT, "ogg")
        self.assertEqual(hub.config.AUDIO_BITRATE, "192k")
        self.assertEqual(hub.config.LANGUAGE, "English")
        self.assertEqual(hub.config.CHUNK_SIZE, 300)
        self.assertEqual(hub.config.AUDIOCPP_UNLOAD_MODELS, True)
        self.assertEqual(hub.config.QWEN_API_URL, "http://127.0.0.1:7862")
        self.assertEqual(hub.config.FASTER_API_URL, "http://127.0.0.1:8001")
        self.assertEqual(hub.config.AUDIOCPP_REMOTE_URL,
                         "http://10.0.0.5:8080")

    def test_apply_settings_rejects_bad_values(self):
        original = {name: getattr(hub.config, name) for name in
                    ("AUDIO_FORMAT", "AUDIO_BITRATE", "LANGUAGE",
                     "CHUNK_SIZE", "AUDIOCPP_UNLOAD_MODELS",
                     "QWEN_API_URL", "CLONE_API_URL",
                     "FASTER_API_URL", "AUDIOCPP_API_URL",
                     "QWEN_REMOTE_URL", "CLONE_REMOTE_URL",
                     "FASTER_REMOTE_URL", "AUDIOCPP_REMOTE_URL")}
        self.addCleanup(lambda: [setattr(hub.config, name, value)
                                 for name, value in original.items()])
        base = {"audio_format": "m4b", "audio_bitrate": "128k",
                "language": "English", "chunk_size": "250",
                "unload_models": True,
                "qwen_custom_port": "7860", "qwen_clone_port": "7861",
                "faster_port": "8000", "audiocpp_port": "8080"}
        with patch.object(hub, "_write_config") as mk_write:
            with self.assertRaises(ValueError):
                hub._apply_settings({**base, "language": "Klingon"})
            with self.assertRaises(ValueError):
                hub._apply_settings({**base, "chunk_size": "0"})
            with self.assertRaises(ValueError):
                hub._apply_settings({**base, "audiocpp_port": "70000"})
            with self.assertRaises(ValueError):
                hub._apply_settings({**base,
                                     "audiocpp_remote_url": "not a url"})
            mk_write.assert_not_called()

    def test_field_validators(self):
        self.assertIsNone(hub._validate_bitrate("128k"))
        self.assertIsNotNone(hub._validate_bitrate("   "))
        self.assertIsNone(hub._validate_language("English"))
        self.assertIsNone(hub._validate_language("en"))
        self.assertIsNotNone(hub._validate_language("Klingon"))
        self.assertIsNone(hub._validate_chunk_size("250"))
        self.assertIsNotNone(hub._validate_chunk_size("abc"))
        self.assertIsNotNone(hub._validate_chunk_size("0"))
        self.assertIsNone(hub._validate_port("8080"))
        self.assertIsNone(hub._validate_port("1"))
        self.assertIsNone(hub._validate_port("65535"))
        self.assertIsNotNone(hub._validate_port("0"))
        self.assertIsNotNone(hub._validate_port("70000"))
        self.assertIsNotNone(hub._validate_port("abc"))

    def test_settings_menu_builds_form_and_saves(self):
        captured = {}

        def fake_form(stdscr, title, fields, back_value=None):
            captured["fields"] = fields
            return {"audio_format": "ogg", "audio_bitrate": "192k",
                    "language": "English", "chunk_size": "300",
                    "unload_models": True,
                    "qwen_custom_port": "7860", "qwen_clone_port": "7861",
                    "faster_port": "8000", "audiocpp_port": "8080"}

        applied = []

        def fake_apply(values):
            applied.append(values)

        def fake_flash(stdscr, text, kind="warn"):
            captured["flash"] = (text, kind)

        with patch.object(hub.tui, "form", fake_form), \
                patch.object(hub, "_apply_settings", fake_apply), \
                patch.object(hub.tui, "flash", fake_flash):
            hub._Hub(None).screen_settings()
        self.assertEqual([f["key"] for f in captured["fields"]],
                         ["audio_format", "audio_bitrate", "language",
                          "chunk_size", "unload_models", "audiocpp_port",
                          "faster_port", "qwen_custom_port",
                          "qwen_clone_port", "audiocpp_remote_url",
                          "faster_remote_url", "qwen_custom_remote_url",
                          "qwen_clone_remote_url"])
        kinds = {f["key"]: f["kind"] for f in captured["fields"]}
        self.assertEqual(kinds["audio_format"], "choice")
        self.assertEqual(kinds["audio_bitrate"], "text")
        self.assertEqual(kinds["audiocpp_port"], "text")
        self.assertEqual(kinds["unload_models"], "bool")
        self.assertEqual(kinds["audiocpp_remote_url"], "text")
        labels = {f["key"]: f["label"] for f in captured["fields"]}
        self.assertEqual(labels["qwen_clone_port"], "qwen-tts Base port")
        self.assertEqual(labels["audiocpp_remote_url"],
                         "audio.cpp remote URL")
        self.assertNotIn("(clone)", " ".join(labels.values()))
        # The ports section note hangs off the first port field, the remote
        # section note off the first remote URL field.
        notes = {f["key"]: f.get("note") for f in captured["fields"]}
        self.assertTrue(notes["audiocpp_port"])
        self.assertTrue(notes["audiocpp_remote_url"])
        self.assertIsNone(notes["audio_format"])
        self.assertIsNone(notes["qwen_custom_port"])
        self.assertEqual(applied, [{"audio_format": "ogg",
                                    "audio_bitrate": "192k",
                                    "language": "English",
                                    "chunk_size": "300",
                                    "unload_models": True,
                                    "qwen_custom_port": "7860",
                                    "qwen_clone_port": "7861",
                                    "faster_port": "8000",
                                    "audiocpp_port": "8080"}])
        self.assertEqual(captured["flash"], ("Settings saved.", "ok"))

    def test_settings_menu_cancel_does_not_apply(self):
        def fake_form(stdscr, title, fields, back_value=None):
            return back_value  # user pressed Cancel

        applied = []

        def fake_apply(values):
            applied.append(values)

        with patch.object(hub.tui, "form", fake_form), \
                patch.object(hub, "_apply_settings", fake_apply):
            hub._Hub(None).screen_settings()
        self.assertEqual(applied, [])

    def test_settings_menu_writes_config_end_to_end(self):
        import tempfile
        tui._THEME.clear()
        self.addCleanup(tui._THEME.clear)
        curses = FakeCurses()
        patcher = patch.dict("sys.modules", {"curses": curses})
        patcher.start()
        self.addCleanup(patcher.stop)

        original = {name: getattr(hub.config, name) for name in
                    ("AUDIO_FORMAT", "AUDIO_BITRATE", "LANGUAGE",
                     "CHUNK_SIZE", "AUDIOCPP_UNLOAD_MODELS",
                     "QWEN_API_URL", "CLONE_API_URL",
                     "FASTER_API_URL", "AUDIOCPP_API_URL",
                     "QWEN_REMOTE_URL", "CLONE_REMOTE_URL",
                     "FASTER_REMOTE_URL", "AUDIOCPP_REMOTE_URL")}
        self.addCleanup(lambda: [setattr(hub.config, name, value)
                                 for name, value in original.items()])

        with tempfile.TemporaryDirectory() as td:
            path = Path(td) / "config.py"
            path.write_text(
                "# Default output options\n"
                'AUDIO_FORMAT = "m4b"\n'
                'AUDIO_BITRATE = "128k"\n'
                'LANGUAGE = "English"\n'
                "\n"
                "CHUNK_SIZE = 250\n"
                "AUDIOCPP_UNLOAD_MODELS = True\n"
                'QWEN_API_URL = "http://127.0.0.1:7860"\n'
                'CLONE_API_URL = "http://127.0.0.1:7861"\n'
                'FASTER_API_URL = "http://127.0.0.1:8000"\n'
                'AUDIOCPP_API_URL = "http://127.0.0.1:8080"\n'
                'QWEN_REMOTE_URL = "http://127.0.0.1:7860"\n'
                'CLONE_REMOTE_URL = "http://127.0.0.1:7861"\n'
                'FASTER_REMOTE_URL = "http://127.0.0.1:8000"\n'
                'AUDIOCPP_REMOTE_URL = "http://127.0.0.1:8080"\n',
                encoding="utf-8")
            with patch.object(hub.config, "__file__", str(path)):
                # Down to Chunk size, Enter -> editor, Ctrl-U + '300',
                # Enter; Tab -> Save, Enter; a key dismisses the flash.
                screen = FakeScreen(keys=[
                    FakeCurses.KEY_DOWN, FakeCurses.KEY_DOWN,
                    FakeCurses.KEY_DOWN, 10, 21, ord("3"), ord("0"),
                    ord("0"), 10, 9, 10, 10])
                hub._Hub(screen).screen_settings()
            text = path.read_text(encoding="utf-8")
        self.assertIn('AUDIO_FORMAT = "m4b"', text)
        self.assertIn("CHUNK_SIZE = 300", text)
        # The running session also picked up the change in-memory.
        self.assertEqual(hub.config.CHUNK_SIZE, 300)

    def test_settings_menu_updates_backend_ports(self):
        import tempfile
        original = {name: getattr(hub.config, name) for name in
                    ("QWEN_API_URL", "CLONE_API_URL",
                     "FASTER_API_URL", "AUDIOCPP_API_URL")}
        self.addCleanup(lambda: [setattr(hub.config, name, value)
                                 for name, value in original.items()])
        with tempfile.TemporaryDirectory() as td:
            path = Path(td) / "config.py"
            path.write_text(
                'QWEN_API_URL = "http://127.0.0.1:7860"\n'
                'CLONE_API_URL = "http://127.0.0.1:7861"\n'
                'FASTER_API_URL = "http://127.0.0.1:8000"\n'
                'AUDIOCPP_API_URL = "http://127.0.0.1:8080"\n',
                encoding="utf-8")
            with patch.object(hub.config, "__file__", str(path)), \
                    patch.object(hub, "_sync_audiocpp_server_port"):
                hub._write_config({
                    "QWEN_API_URL": "http://127.0.0.1:7862",
                    "CLONE_API_URL": "http://127.0.0.1:7863",
                    "FASTER_API_URL": "http://127.0.0.1:8001",
                    "AUDIOCPP_API_URL": "http://127.0.0.1:8081",
                })
            text = path.read_text(encoding="utf-8")
        self.assertIn('QWEN_API_URL = "http://127.0.0.1:7862"', text)
        self.assertIn('CLONE_API_URL = "http://127.0.0.1:7863"', text)
        self.assertIn('FASTER_API_URL = "http://127.0.0.1:8001"', text)
        self.assertIn('AUDIOCPP_API_URL = "http://127.0.0.1:8081"', text)


class AudiocppServerConfigTests(unittest.TestCase):
    """update_server_config_port: rewriting the checkout's server.json."""

    def _make_checkout(self, td, port=8080):
        from backends import audiocpp as audiocpp_backend
        import json
        checkout = Path(td) / "audio.cpp"
        checkout.mkdir()
        server_json = checkout / "server.json"
        server_json.write_text(
            json.dumps({"host": "127.0.0.1", "port": port,
                        "models": [{"id": "qwen"}]}, indent=2),
            encoding="utf-8")
        return audiocpp_backend, checkout, server_json

    def test_rewrites_existing_server_json_port(self):
        import tempfile
        import json
        with tempfile.TemporaryDirectory() as td:
            mod, checkout, server_json = self._make_checkout(td, port=8080)
            with patch.object(mod, "find_local_checkout",
                              return_value=checkout):
                self.assertTrue(mod.update_server_config_port(9090))
            data = json.loads(server_json.read_text(encoding="utf-8"))
        self.assertEqual(data["port"], 9090)
        # Other keys are preserved.
        self.assertEqual(data["host"], "127.0.0.1")
        self.assertEqual(data["models"], [{"id": "qwen"}])

    def test_noop_when_port_unchanged(self):
        import tempfile
        with tempfile.TemporaryDirectory() as td:
            mod, checkout, server_json = self._make_checkout(td, port=8080)
            before = server_json.read_text(encoding="utf-8")
            with patch.object(mod, "find_local_checkout",
                              return_value=checkout):
                self.assertTrue(mod.update_server_config_port(8080))
            self.assertEqual(server_json.read_text(encoding="utf-8"), before)

    def test_false_when_no_checkout(self):
        from backends import audiocpp as audiocpp_backend
        with patch.object(audiocpp_backend, "find_local_checkout",
                          return_value=None):
            self.assertFalse(audiocpp_backend.update_server_config_port(9090))


class ConfigureBackendsDispatchTests(unittest.TestCase):
    """The configure-backends screens dispatch their backend actions."""

    def test_setup_screen_runs_the_backend_wizard_and_goes_back(self):
        info = BackendInfo("qwen", "qwen-tts", lambda: None, lambda: 0)
        with patch.object(info, "setup_screen") as mk_setup:
            result = hub._Hub(None).screen_setup(info)()
        mk_setup.assert_called_once_with(None)
        self.assertIs(result, tui.Wizard.BACK)

    def test_setup_screen_flashes_on_crash_and_goes_back(self):
        info = BackendInfo("qwen", "qwen-tts", lambda: None, lambda: 0)
        flashes = []

        def boom(scr):
            raise RuntimeError("kaboom")

        with patch.object(info, "setup_screen", boom), \
                patch.object(hub.tui, "flash",
                             lambda scr, text, kind="warn":
                             flashes.append(text)):
            result = hub._Hub(None).screen_setup(info)()
        self.assertIs(result, tui.Wizard.BACK)
        self.assertEqual(flashes, ["kaboom"])

    def test_screen_uninstall_runs_uninstall_and_goes_back(self):
        import contextlib
        info = BackendInfo("qwen", "qwen-tts", lambda: None, lambda: 0)
        with patch.object(hub._Hub, "_pick_backend", return_value=info), \
                patch.object(info, "uninstall") as mk_uninstall, \
                patch.object(hub.tui, "suspend", contextlib.nullcontext):
            result = hub._Hub(None).screen_uninstall()
        mk_uninstall.assert_called_once_with()
        self.assertIs(result, tui.Wizard.BACK)

    def _capture_flashes(self):
        flashes = []

        def fake_flash(stdscr, text, kind="warn"):
            flashes.append((text, kind))

        return patch.object(hub.tui, "flash", fake_flash), flashes

    def test_download_models_action_flashes_hand_install_guidance(self):
        with tempfile.TemporaryDirectory() as td:
            checkout = Path(td)
            (checkout / "server.json").write_text(json.dumps({"models": []}),
                                                  encoding="utf-8")
            missing = [{"id": "qwen",
                        "rel": "models/Qwen3-TTS-12Hz-0.6B-Base-GGUF"}]
            patch_flash, flashes = self._capture_flashes()
            with patch.object(hub.audiocpp_backend, "find_local_checkout",
                              return_value=checkout), \
                    patch.object(hub.audiocpp_backend, "missing_model_entries",
                                 return_value=missing), \
                    patch.object(hub.audiocpp_backend,
                                 "missing_model_install_guidance",
                                 return_value=[]), \
                    patch.object(hub.audiocpp_backend, "hand_install_guidance",
                                 return_value="do it by hand") as mk_hand, \
                    patch_flash:
                hub._download_models_action(None)
        self.assertEqual(flashes, [("do it by hand", "err")])
        mk_hand.assert_called_once()

    def test_download_models_action_flashes_ok_when_nothing_missing(self):
        with tempfile.TemporaryDirectory() as td:
            checkout = Path(td)
            (checkout / "server.json").write_text(json.dumps({"models": []}),
                                                  encoding="utf-8")
            patch_flash, flashes = self._capture_flashes()
            with patch.object(hub.audiocpp_backend, "find_local_checkout",
                              return_value=checkout), \
                    patch.object(hub.audiocpp_backend, "missing_model_entries",
                                 return_value=[]), \
                    patch_flash:
                hub._download_models_action(None)
        self.assertEqual(len(flashes), 1)
        self.assertEqual(flashes[0][1], "ok")

    def test_download_models_action_suspends_and_installs(self):
        import contextlib

        @contextlib.contextmanager
        def fake_suspend(scr):
            yield

        with tempfile.TemporaryDirectory() as td:
            checkout = Path(td)
            (checkout / "server.json").write_text(json.dumps({"models": []}),
                                                  encoding="utf-8")
            missing = [{"id": "qwen", "rel": "models/q"}]
            guidance = [("qwen", "qwen3_tts_0_6b_base_q8_0")]
            patch_flash, flashes = self._capture_flashes()
            with patch.object(hub.audiocpp_backend, "find_local_checkout",
                              return_value=checkout), \
                    patch.object(hub.audiocpp_backend, "missing_model_entries",
                                 return_value=missing), \
                    patch.object(hub.audiocpp_backend,
                                 "missing_model_install_guidance",
                                 return_value=guidance), \
                    patch.object(hub.tui, "suspend", fake_suspend), \
                    patch.object(hub.audiocpp_backend, "install_models") as mk, \
                    patch_flash:
                hub._download_models_action(None)
        mk.assert_called_once_with(checkout, guidance)
        self.assertEqual(len(flashes), 1)
        self.assertEqual(flashes[0][1], "ok")

    def test_download_models_action_flashes_error_when_no_checkout(self):
        patch_flash, flashes = self._capture_flashes()
        with patch.object(hub.audiocpp_backend, "find_local_checkout",
                          return_value=None), patch_flash:
            hub._download_models_action(None)
        self.assertEqual(len(flashes), 1)
        self.assertEqual(flashes[0][1], "err")

    def test_pick_backend_install_lists_uninstalled_only(self):
        captured = {}

        def fake_menu(stdscr, title, options, **kwargs):
            captured["options"] = options
            return tui.Wizard.BACK

        infos = [BackendInfo("qwen", "qwen-tts", lambda: None, lambda: 0),
                 BackendInfo("faster", "faster-qwen3-tts", lambda: None,
                             lambda: 0)]
        statuses = [BackendStatus("qwen", "qwen-tts", installed=True,
                                  configured=True),
                    BackendStatus("faster", "faster-qwen3-tts",
                                  installed=False, configured=False)]
        with patch.object(hub, "REGISTRY", infos), \
                patch.object(hub, "detect_all", return_value=statuses), \
                patch.object(hub.tui, "menu", fake_menu):
            result = hub._Hub(None)._pick_backend(installed_only=False)
        self.assertIsNone(result)
        self.assertEqual([label for label, _ in captured["options"]],
                         ["faster-qwen3-tts"])

    def test_pick_backend_uninstall_lists_installed_only(self):
        captured = {}

        def fake_menu(stdscr, title, options, **kwargs):
            captured["options"] = options
            return "qwen"

        infos = [BackendInfo("qwen", "qwen-tts", lambda: None, lambda: 0),
                 BackendInfo("faster", "faster-qwen3-tts", lambda: None,
                             lambda: 0)]
        statuses = [BackendStatus("qwen", "qwen-tts", installed=True,
                                  configured=True),
                    BackendStatus("faster", "faster-qwen3-tts",
                                  installed=False, configured=False)]
        with patch.object(hub, "REGISTRY", infos), \
                patch.object(hub, "detect_all", return_value=statuses), \
                patch.object(hub.tui, "menu", fake_menu), \
                patch.object(hub, "get", return_value=infos[0]) as mk_get:
            result = hub._Hub(None)._pick_backend(installed_only=True)
        self.assertEqual(result, infos[0])
        mk_get.assert_called_once_with("qwen")
        self.assertEqual([label for label, _ in captured["options"]],
                         ["qwen-tts"])


class HubNavigationTests(unittest.TestCase):
    """Esc (and q) steps back exactly one screen across the whole hub."""

    def setUp(self):
        tui._THEME.clear()
        self.addCleanup(tui._THEME.clear)

    def _info(self, key="audiocpp", label="audio.cpp"):
        return BackendInfo(key, label, lambda: None, lambda: 0)

    def _status(self, key="audiocpp", label="audio.cpp"):
        return BackendStatus(key, label, installed=True, configured=True)

    def _drive(self, script, statuses, registry):
        """Run the hub, feeding SCRIPT (one value per menu) to tui.menu.

        Records the title of every menu shown, in order. ``tui.Wizard.BACK``
        in the script simulates Esc on that menu.
        """
        titles = []

        def menu(stdscr, title, options, **kwargs):
            titles.append(title)
            return script.pop(0)

        def get(key):
            return next((i for i in registry if i.key == key), None)

        with patch.object(hub, "REGISTRY", registry), \
                patch.object(hub, "detect_all", return_value=statuses), \
                patch.object(hub, "get", side_effect=get), \
                patch.object(hub.tui, "menu", menu), \
                patch.object(hub.audiocpp_backend, "find_local_checkout",
                             return_value=None):
            hub._Hub(None).run()
        return titles

    def test_esc_on_wizard_first_screen_returns_to_configure(self):
        # The reported bug: Esc on the audio.cpp "Select TTS model
        # families" tree (the wizard's first screen) must land back on
        # "Configure backends", not the main menu.
        info = self._info()
        with patch.object(info, "setup_screen", return_value=1):
            titles = self._drive(
                ["configure_backends", ("configure", "audiocpp"),
                 tui.Wizard.BACK, tui.Wizard.BACK],
                [self._status()], [info])
        self.assertEqual(
            titles,
            ["tts-audiobook-generator", "Configure backends",
             "Configure backends", "tts-audiobook-generator"])

    def test_esc_on_install_picker_returns_to_configure(self):
        registry = [self._info("audiocpp", "audio.cpp"),
                    self._info("qwen", "qwen-tts")]
        statuses = [self._status("audiocpp", "audio.cpp"),
                    BackendStatus("qwen", "qwen-tts", installed=False,
                                  configured=False)]
        titles = self._drive(
            ["configure_backends", "install", tui.Wizard.BACK,
             tui.Wizard.BACK, tui.Wizard.BACK],
            statuses, registry)
        self.assertEqual(
            titles,
            ["tts-audiobook-generator", "Configure backends",
             "Install Backend", "Configure backends",
             "tts-audiobook-generator"])

    def test_esc_on_server_action_returns_one_screen_at_a_time(self):
        specs = [ServerSpec("qwen-custom", "http://127.0.0.1:7860", []),
                 ServerSpec("qwen-clone", "http://127.0.0.1:7861", [])]
        status = BackendStatus("qwen", "qwen-tts", installed=True,
                               configured=True, servers=specs)
        registry = [self._info("qwen", "qwen-tts")]
        with patch.object(hub.common, "server_running", return_value=False):
            titles = self._drive(
                ["server", "qwen", "qwen-clone", tui.Wizard.BACK,
                 tui.Wizard.BACK, tui.Wizard.BACK, tui.Wizard.BACK],
                [status], registry)
        self.assertEqual(
            titles,
            ["tts-audiobook-generator", "Start / Stop a server",
             "qwen-tts server", "qwen-clone  (stopped)",
             "qwen-tts server", "Start / Stop a server",
             "tts-audiobook-generator"])

    def test_esc_on_main_menu_quits(self):
        titles = self._drive([tui.Wizard.BACK], [], [])
        self.assertEqual(titles, ["tts-audiobook-generator"])


if __name__ == "__main__":
    unittest.main()