From df95e7034df683c38fde67890430ab4c2abfa4ba Mon Sep 17 00:00:00 2001 From: historia Date: Wed, 26 Aug 2026 18:39:33 -0400 Subject: feat: in-place toggle field, updated transcription tui to use new field --- app/tests/test_backends_audiocpp.py | 128 +++++++++++++++++++++++++++++------- app/tests/test_backends_faster.py | 94 +++++++++++--------------- app/tests/test_tui.py | 52 +++++++++++++++ 3 files changed, 195 insertions(+), 79 deletions(-) (limited to 'app/tests') diff --git a/app/tests/test_backends_audiocpp.py b/app/tests/test_backends_audiocpp.py index 37745f3..d364c06 100644 --- a/app/tests/test_backends_audiocpp.py +++ b/app/tests/test_backends_audiocpp.py @@ -792,31 +792,109 @@ class InstallModelsTests(unittest.TestCase): self.checkout, [{"path": "models/higgs"}])) -class TranscriptionChoicesTests(unittest.TestCase): - """_transcription_choices: the renamed voice-transcripts options.""" - - def test_fresh_directory_offers_the_renamed_all(self): - choices, default = make_server.wizard._transcription_choices( - [], {}, prompt_exists=False) - self.assertEqual(default, "all") - self.assertEqual(choices, [("Re-transcribe all", "all")]) - - def test_existing_transcripts_offer_new_only_and_all(self): - wavs = [Path("/x/narrator.wav"), Path("/x/new.wav")] - choices, default = make_server.wizard._transcription_choices( - wavs, {"narrator": "old transcript"}, prompt_exists=True) - self.assertEqual(default, "missing") - self.assertEqual([label for label, _mode in choices], - ["Only transcribe new voices", "Re-transcribe all"]) - - def test_complete_transcripts_offer_keep_and_all(self): - wavs = [Path("/x/narrator.wav")] - choices, default = make_server.wizard._transcription_choices( - wavs, {"narrator": "old transcript"}, prompt_exists=True) - self.assertEqual(default, "keep") - self.assertEqual([label for label, _mode in choices], - ["Keep the existing transcripts", - "Re-transcribe all"]) +class ConfigFormTranscriptionToggleTests(unittest.TestCase): + """The combined form's Voice transcripts row: one fixed two-way toggle. + + The row is always visible whenever a clone-capable family is hosted + (it must not hide itself just because the picked wav directory has no + .wavs yet), and the plan it produces follows the toggled mode. + """ + + def _checkout(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + return _make_checkout(Path(tmp.name)) + + def _empty_dir(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + return Path(tmp.name) + + def _run(self, checkout, voices_dir, picked_family=None, override=None): + """Drive _wizard on CHECKOUT; return (form call capture, settings).""" + catalog = make_server.catalog.load_model_catalog(checkout) + family = picked_family or "qwen3_tts" + index = next(i for i, entry in enumerate(catalog) + if entry["family"] == family) + target = catalog[index]["packages"][0]["target_directory"] + captured = {} + + def fake_form(stdscr, title, fields, **kwargs): + captured.update(kwargs) + captured["fields"] = fields + result = {f["key"]: f["value"] for f in fields} + if override: + result.update(override) + return result + + with patch.object(make_server.build, "find_local_checkout", + return_value=checkout), \ + patch.object(tui, "checkbox_tree", + return_value=[(index, target)]), \ + patch.object(tui, "form", side_effect=fake_form), \ + patch.object(make_server.wizard, "VOICES_DIR", voices_dir): + settings = make_server.wizard._wizard( + None, make_server.wizard.build_parser().parse_args([]), + make_server.wizard.build_parser()) + return captured, settings + + def test_row_is_a_fixed_toggle_defaulting_to_new_voices(self): + checkout = self._checkout() + captured, _settings = self._run(checkout, self._empty_dir()) + by_key = {f["key"]: f for f in captured["fields"]} + row = by_key["transcription"] + self.assertEqual(row["kind"], "toggle") + self.assertEqual(row["value"], "missing") + self.assertEqual( + row["choices"], + [("Transcribe new voices", "missing"), + ("Re-transcribe all voices", "all")]) + + def test_row_always_visible_when_clone_capable(self): + # Regression: the row used to hide itself until the wav directory + # contained .wavs; a clone-capable pick must always offer it. + checkout = self._checkout() + voices = self._empty_dir() + captured, _settings = self._run(checkout, voices) + by_key = {f["key"]: f for f in captured["fields"]} + self.assertTrue(by_key["transcription"]["visible"](captured["fields"])) + self.assertEqual(_settings["plan"]["mode"], "missing") + self.assertEqual(_settings["plan"]["missing"], []) + + def test_row_hidden_without_a_clone_capable_pick(self): + checkout = self._checkout() + captured, _settings = self._run(checkout, self._empty_dir(), + picked_family="supertonic") + by_key = {f["key"]: f for f in captured["fields"]} + self.assertFalse(by_key["transcription"]["visible"](captured["fields"])) + self.assertIsNone(_settings["plan"]) + + def test_form_opens_on_the_continue_button(self): + checkout = self._checkout() + captured, _settings = self._run(checkout, self._empty_dir()) + self.assertTrue(captured.get("start_on_buttons")) + + def test_new_voices_plan_carries_only_untranscribed_wavs(self): + checkout = self._checkout() + voices = self._empty_dir() + (voices / "extra.wav").write_bytes(b"x") + (voices / "narrator.wav").write_bytes(b"x") + common.write_prompt_text(voices, {"narrator": "Old words."}) + _captured, settings = self._run(checkout, voices) + self.assertEqual(settings["plan"]["mode"], "missing") + self.assertEqual([w.name for w in settings["plan"]["missing"]], + ["extra.wav"]) + self.assertEqual(settings["plan"]["existing"], + {"narrator": "Old words."}) + + def test_toggled_all_retranscribes_everything(self): + checkout = self._checkout() + voices = self._empty_dir() + (voices / "narrator.wav").write_bytes(b"x") + common.write_prompt_text(voices, {"narrator": "Old words."}) + _captured, settings = self._run(checkout, voices, + override={"transcription": "all"}) + self.assertEqual(settings["plan"]["mode"], "all") class TranscribeWavDirTests(unittest.TestCase): diff --git a/app/tests/test_backends_faster.py b/app/tests/test_backends_faster.py index c03f031..a093fbe 100644 --- a/app/tests/test_backends_faster.py +++ b/app/tests/test_backends_faster.py @@ -110,8 +110,8 @@ class LoadVoicesTests(unittest.TestCase): self.assertEqual(make_voices.load_voices(self.path), {}) -class DecideFasterTranscriptionTests(unittest.TestCase): - """_decide_faster_transcription: the re-transcribe plan questions.""" +class PlanForTests(unittest.TestCase): + """_plan_for: execution plans for the wizard's transcription toggle.""" def setUp(self): self._tmp = tempfile.TemporaryDirectory() @@ -124,59 +124,18 @@ class DecideFasterTranscriptionTests(unittest.TestCase): def tearDown(self): self._tmp.cleanup() - def test_new_voices_default_to_missing_mode(self): - choices, default = make_voices._decide_faster_transcription( - [self.narrator, self.new_voice], - {"narrator": {"ref_text": "old"}}) - self.assertEqual(default, "missing") - modes = [mode for _label, mode in choices] - self.assertIn("missing", modes) - self.assertIn("all", modes) - missing = [w for w in (self.narrator, self.new_voice) - if w.stem not in {"narrator"}] + def test_missing_plan_carries_only_unlisted_wavs(self): plan = make_voices._plan_for("missing", self.folder, {"narrator": {"ref_text": "old"}}) self.assertEqual(plan["mode"], "missing") - self.assertEqual([w.name for w in plan["missing"]], - [w.name for w in missing]) - - def test_declining_new_voices_transcribes_all(self): - choices, default = make_voices._decide_faster_transcription( - [self.narrator, self.new_voice], - {"narrator": {"ref_text": "old"}}) - # Re-transcribing everything stays available alongside new-only. - modes = [mode for _label, mode in choices] - self.assertIn("all", modes) - plan = make_voices._plan_for("all", self.folder, - {"narrator": {"ref_text": "old"}}) - self.assertEqual(plan["mode"], "all") - - def test_no_new_voices_offers_retranscribe_default_keep(self): - choices, default = make_voices._decide_faster_transcription( - [self.narrator], {"narrator": {"ref_text": "old"}}) - self.assertEqual(default, "keep") - modes = [mode for _label, mode in choices] - self.assertEqual(modes, ["keep", "all"]) + self.assertEqual([w.name for w in plan["missing"]], ["new.wav"]) + self.assertEqual(plan["existing"], {"narrator": {"ref_text": "old"}}) - def test_no_new_voices_can_retranscribe_all(self): - _choices, _default = make_voices._decide_faster_transcription( - [self.narrator], {"narrator": {"ref_text": "old"}}) + def test_all_plan_names_the_mode(self): plan = make_voices._plan_for("all", self.folder, {"narrator": {"ref_text": "old"}}) self.assertEqual(plan["mode"], "all") - - def test_choice_labels_are_the_renamed_ones(self): - # The option names shown for the Voice-transcripts choice. - with_new, _ = make_voices._decide_faster_transcription( - [self.narrator, self.new_voice], - {"narrator": {"ref_text": "old"}}) - self.assertEqual([label for label, _mode in with_new], - ["Only transcribe new voices", "Re-transcribe all"]) - without_new, _ = make_voices._decide_faster_transcription( - [self.narrator], {"narrator": {"ref_text": "old"}}) - self.assertEqual([label for label, _mode in without_new], - ["Keep the existing voices.json", - "Re-transcribe all"]) + self.assertEqual(plan["missing"], []) class MainTests(unittest.TestCase): @@ -333,7 +292,7 @@ class WizardFormTests(unittest.TestCase): # no keep/new-only choice exists. self.assertEqual(settings["plan"]["mode"], "all") - def test_modify_run_offers_transcription_modes(self): + def test_modify_run_offers_transcription_toggle(self): folder = self._wavs() (folder / "new.wav").write_bytes(b"x") # a voice not in voices.json output = folder / "voices.json" @@ -347,11 +306,14 @@ class WizardFormTests(unittest.TestCase): captured["keys"] = [f["key"] for f in fields] by_key = {f["key"]: f for f in fields} self.assertIn("transcription", by_key) - modes = [mode for _label, mode in by_key[ - "transcription"]["choices"](fields)] - # New .wavs exist, so both transcribing only those and - # re-transcribing everything are offered. - self.assertEqual(modes, ["missing", "all"]) + # A plain in-place toggle (no popup): fixed choices, new-only. + row = by_key["transcription"] + self.assertEqual(row["kind"], "toggle") + self.assertEqual(row["value"], "missing") + self.assertEqual( + row["choices"], + [("Transcribe new voices", "missing"), + ("Re-transcribe all voices", "all")]) result = {f["key"]: f["value"] for f in fields} result["transcription"] = "missing" return result @@ -372,6 +334,30 @@ class WizardFormTests(unittest.TestCase): ["new.wav"]) self.assertEqual(settings["wav_dir"], folder) + def test_modify_run_toggled_all_retranscribes_everything(self): + folder = self._wavs() + (folder / "new.wav").write_bytes(b"x") + output = folder / "voices.json" + existing = {"narrator": { + "ref_audio": str(folder / "narrator.wav"), + "ref_text": "old transcript", "language": "English"}} + output.write_text(json.dumps(existing), encoding="utf-8") + + def fake_form(stdscr, title, fields, **kwargs): + result = {f["key"]: f["value"] for f in fields} + result["transcription"] = "all" + return result + + with patch.object(make_voices, "_is_installed", return_value=True), \ + patch.object(make_voices, "_is_cloned", return_value=True), \ + patch.object(make_voices.tui, "form", + side_effect=fake_form): + settings = make_voices._wizard( + None, self._args("--output", str(output), + "--skip-install", "--skip-clone")) + self.assertIsNotNone(settings) + self.assertEqual(settings["plan"]["mode"], "all") + def test_cancel_aborts(self): with patch.object(make_voices, "_is_installed", return_value=True), \ patch.object(make_voices, "_is_cloned", return_value=True), \ diff --git a/app/tests/test_tui.py b/app/tests/test_tui.py index b839a71..7821ebb 100644 --- a/app/tests/test_tui.py +++ b/app/tests/test_tui.py @@ -684,6 +684,58 @@ class FormTests(TuiTestCase): result = tui.form(screen, "Settings", fields) self.assertEqual(result, {"combine": True}) + def test_toggle_field_cycles_with_enter(self): + # Enter steps the toggle to its next choice in place (no menu + # opens), Tab -> button, Enter saves. + fields = [{"key": "transcripts", "label": "Transcripts", + "kind": "toggle", "value": "missing", + "choices": [("New voices", "missing"), + ("All voices", "all")]}] + screen = FakeScreen(keys=[10, 9, 10]) + result = tui.form(screen, "Settings", fields) + self.assertEqual(result, {"transcripts": "all"}) + + def test_toggle_field_cycles_with_space(self): + fields = [{"key": "transcripts", "label": "Transcripts", + "kind": "toggle", "value": "missing", + "choices": [("New voices", "missing"), + ("All voices", "all")]}] + screen = FakeScreen(keys=[ord(" "), 9, 10]) + result = tui.form(screen, "Settings", fields) + self.assertEqual(result, {"transcripts": "all"}) + + def test_toggle_field_last_choice_wraps_to_the_first(self): + fields = [{"key": "transcripts", "label": "Transcripts", + "kind": "toggle", "value": "all", + "choices": [("New voices", "missing"), + ("All voices", "all")]}] + screen = FakeScreen(keys=[10, 9, 10]) + result = tui.form(screen, "Settings", fields) + self.assertEqual(result, {"transcripts": "missing"}) + + def test_toggle_field_renders_the_matching_label(self): + fields = [{"key": "transcripts", "label": "Transcripts", + "kind": "toggle", "value": "missing", + "choices": [("New voices", "missing"), + ("All voices", "all")]}] + screen = FakeScreen(keys=[9, 10]) # straight to Save + tui.form(screen, "Settings", fields) + texts = [text for _, _, text, _ in screen.strings] + self.assertTrue(any("New voices" in text for text in texts)) + self.assertFalse(any("All voices" in text for text in texts)) + + def test_toggle_field_fires_on_change(self): + calls = [] + fields = [{"key": "transcripts", "label": "Transcripts", + "kind": "toggle", "value": "missing", + "choices": [("New voices", "missing"), + ("All voices", "all")], + "on_change": lambda fs: calls.append(1)}] + screen = FakeScreen(keys=[10, 9, 10]) + result = tui.form(screen, "Settings", fields) + self.assertEqual(calls, [1]) + self.assertEqual(result, {"transcripts": "all"}) + def test_hidden_field_keeps_value_and_is_not_drawn(self): fields = self._fields() fields.append({"key": "hidden", "label": "Secret", "kind": "text", -- cgit v1.2.3