Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

7.3 KB
Raw Blame
"""Tests for backend.cli helpers: the shared WAV cache and Studio port resolution."""

import argparse
import json
import os
import sys
import tempfile
import unittest
from unittest import mock

ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
BACKEND_ROOT = os.path.join(ROOT, "backend")
if BACKEND_ROOT not in sys.path:
    sys.path.insert(0, BACKEND_ROOT)

import cli as cli_mod
from services import audio_extract


class SharedWavTests(unittest.TestCase):
    def test_successful_extraction_is_reused(self):
        fd, wav_path = tempfile.mkstemp(suffix=".wav")
        os.close(fd)
        try:
            with mock.patch.object(
                audio_extract, "extract_wav_16k_mono", return_value=wav_path
            ) as extract:
                shared = cli_mod._SharedWav("/video.mp4")
                self.assertEqual(shared.get(), wav_path)
                self.assertEqual(shared.get(), wav_path)
            self.assertEqual(extract.call_count, 1)
        finally:
            if os.path.exists(wav_path):
                os.unlink(wav_path)

    def test_failed_extraction_is_attempted_once(self):
        with mock.patch.object(
            audio_extract, "extract_wav_16k_mono", side_effect=RuntimeError("decode failed")
        ) as extract:
            shared = cli_mod._SharedWav("/video.mp4")
            for _ in range(4):
                self.assertIsNone(shared.get())

        self.assertEqual(extract.call_count, 1)
        self.assertIsInstance(shared.error, RuntimeError)

    def test_empty_result_is_treated_as_failure(self):
        with mock.patch.object(
            audio_extract, "extract_wav_16k_mono", return_value=None
        ) as extract:
            shared = cli_mod._SharedWav("/video.mp4")
            self.assertIsNone(shared.get())
            self.assertIsNone(shared.get())

        self.assertEqual(extract.call_count, 1)


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


class SelectionSignatureTests(unittest.TestCase):
    """The suggestions cache is only allowed to replay picks made under the
    same rules."""

    def _sig(self, config, kb="kb-a"):
        with mock.patch("services.claude_suggest.kb_signature", return_value=kb):
            return cli_mod._selection_signature(config)

    def test_editing_the_knowledge_base_invalidates_an_ai_session(self):
        cfg = {"ai_select": True}
        self.assertNotEqual(self._sig(cfg, kb="kb-a"), self._sig(cfg, kb="kb-b"))

    def test_editing_the_knowledge_base_leaves_a_saliency_session_alone(self):
        cfg = {"ai_select": False}
        self.assertEqual(self._sig(cfg, kb="kb-a"), self._sig(cfg, kb="kb-b"))

    def test_changing_profile_invalidates(self):
        a = self._sig({"ai_select": True, "profile": "party"})
        b = self._sig({"ai_select": True})
        self.assertNotEqual(a, b)

    def test_turning_energy_off_invalidates(self):
        a = self._sig({"ai_select": True, "energy_boost": False})
        b = self._sig({"ai_select": True, "energy_boost": True})
        self.assertNotEqual(a, b)

    def test_an_unreadable_knowledge_base_does_not_raise(self):
        with mock.patch("services.claude_suggest.kb_signature", side_effect=OSError("boom")):
            self.assertIn("kb-unavailable", cli_mod._selection_signature({"ai_select": True}))

    def test_two_failed_lookups_never_compare_equal(self):
        # Otherwise a session saved during one failure replays during the next,
        # after the knowledge base has changed underneath it.
        with mock.patch("services.claude_suggest.kb_signature", side_effect=OSError("boom")):
            a = cli_mod._selection_signature({"ai_select": True})
            b = cli_mod._selection_signature({"ai_select": True})
        self.assertNotEqual(a, b)


class ExplicitClipBoundsTests(unittest.TestCase):
    """config is seeded from DEFAULT_PRESET, which carries the vertical numbers.
    Forwarding those unconditionally would make --format horizontal a no-op."""

    def test_untouched_preset_values_do_not_override_the_format(self):
        from presets import DEFAULT_PRESET

        config = {**DEFAULT_PRESET, "format": "horizontal"}
        self.assertEqual(cli_mod._explicit_clip_bounds(config), (None, None))

    def test_a_value_the_user_changed_is_forwarded(self):
        from presets import DEFAULT_PRESET

        config = {**DEFAULT_PRESET, "min_clip_duration": 30}
        self.assertEqual(cli_mod._explicit_clip_bounds(config), (30, None))

    def test_horizontal_selection_uses_the_format_window(self):
        from presets import DEFAULT_PRESET
        from services.claude_suggest import ClipBounds

        config = {**DEFAULT_PRESET, "format": "horizontal"}
        bounds = ClipBounds.of(config["format"], *cli_mod._explicit_clip_bounds(config))
        self.assertEqual((bounds.dur_min, bounds.dur_max), (60, 300))


class JsonObjectArgTests(unittest.TestCase):
    """`--style` is validated up front: malformed input stops the run rather
    than rendering with a theme nobody asked for."""

    def test_missing_value_is_none(self):
        self.assertIsNone(cli_mod._json_object_arg(None, "--style"))

    def test_a_valid_object_passes_through(self):
        parsed = cli_mod._json_object_arg('{"pack": "collage"}', "--style")
        self.assertEqual(parsed, {"pack": "collage"})

    def test_malformed_json_exits(self):
        with self.assertRaises(SystemExit):
            cli_mod._json_object_arg("not json", "--style")

    def test_a_json_array_is_not_an_object(self):
        with self.assertRaises(SystemExit):
            cli_mod._json_object_arg("[1, 2]", "--style")


class AgentOutputHelpersTests(unittest.TestCase):
    def test_no_color_disables_color(self):
        with mock.patch.dict(os.environ, {"NO_COLOR": "1"}, clear=False):
            self.assertFalse(cli_mod._use_color())

    def test_parse_content_text(self):
        raw = (
            "TITLES\n1. First title\n2. Second title\n"
            "DESCRIPTION\nA short blurb about the clip.\n"
            "TAGS\nalpha, beta\n"
            "HASHTAGS\n#one #two\n"
        )
        parsed = cli_mod._parse_content_text(raw)
        self.assertEqual(parsed["titles"], ["First title", "Second title"])
        self.assertIn("blurb", parsed["description"])
        self.assertEqual(parsed["tags"], ["alpha", "beta"])
        self.assertEqual(parsed["hashtags"], ["#one", "#two"])

    def test_want_spinner_off_when_quiet(self):
        with mock.patch.dict(os.environ, {"PODCLI_QUIET": "1"}, clear=False):
            self.assertFalse(cli_mod._want_spinner())

    def test_non_interactive_yes_flag(self):
        ns = argparse.Namespace(yes=True, json=False)
        self.assertTrue(cli_mod._non_interactive(ns))

    def test_emit_process_json_writes_one_object(self):
        import io

        buf = io.StringIO()
        cli_mod._emit_process_json(
            json_out=buf,
            video_path="/ep.mp4",
            output_dir="/out",
            quality="high",
            clips=[{"title": "A", "start_second": 1, "end_second": 12, "score": 9}],
            results=[{"output_path": "/out/a.mp4", "duration": 11, "status": "ok"}],
            interrupted=False,
        )
        payload = json.loads(buf.getvalue())
        self.assertTrue(payload["ok"])
        self.assertEqual(payload["quality"], "high")
        self.assertEqual(payload["clips"][0]["title"], "A")
        self.assertEqual(payload["clips"][0]["path"], "/out/a.mp4")