from datetime import datetime from pathlib import Path import respx from typer.testing import CliRunner from plexreaper.cli import app from plexreaper.ledger import Ledger from plexreaper.models import Inventory from plexreaper.pipeline import assemble, fetch_sessions, save_cached_inventory from plexreaper.tui.app import ReaperApp from tests.conftest import NOW, make_title def _inventory(): return Inventory( titles=[ make_title("plex://movie/aaa", rating_key=1, title="Watched"), make_title("plex://movie/bbb", rating_key=2, title="Big Unwatched", size_gb=40), make_title("plex://movie/sport", rating_key=4, title="Match", genres=["Sport"]), ], episode_to_show={}, scanned_at=datetime.now(), # must be fresh, or the CLI tests see an expired cache and call Plex ) async def test_tui_marks_persist(cfg, tmp_path): sessions, admins = fetch_sessions(cfg) ledger = Ledger(cfg.ledger_path) result = assemble(cfg, _inventory(), sessions, admins, {}, now=NOW) export = tmp_path / "out.json" app_ = ReaperApp(result, ledger, export_path=export) async with app_.run_test() as pilot: await pilot.pause() assert app_.current().title.title == "Big Unwatched" await pilot.press("space") # reap, cursor moves on await pilot.press("k") # keep the next one (Watched) await pilot.press("x") await pilot.press("e") await pilot.press("escape") await pilot.press("slash") await pilot.press(*"match") await pilot.press("enter") await pilot.pause() assert app_.current().title.title == "Match" await pilot.press("space") # protected: refused assert ledger.decisions() == {"plex://movie/bbb": "reap", "plex://movie/aaa": "keep"} assert export.is_file() and "Big Unwatched" in export.read_text() def test_cli_scan_json_from_cache_and_decisions(cfg, tmp_path, monkeypatch): save_cached_inventory(cfg.cache_path, _inventory()) toml = tmp_path / "plex-reaper.toml" toml.write_text( f'[plex]\nurl = "http://plex.test"\ntoken = "tok"\n' f'[[tautulli]]\nname = "main"\ndb = "{cfg.tautulli[0].db}"\n' f'[state]\nledger = "{cfg.state.ledger}"\ncache = "{cfg.state.cache}"\n' ) monkeypatch.delenv("PLEX_TOKEN", raising=False) Ledger(cfg.ledger_path).set("plex://movie/bbb", "reap", kind="movie", title="Big Unwatched", score=70) runner = CliRunner() r = runner.invoke(app, ["-c", str(toml), "scan", "--json", "--limit", "0"]) assert r.exit_code == 0, r.output assert '"Big Unwatched"' in r.output and '"decision": "reap"' in r.output r = runner.invoke(app, ["-c", str(toml), "decisions"]) assert r.exit_code == 0 and "Big Unwatched" in r.output out = tmp_path / "list.json" r = runner.invoke(app, ["-c", str(toml), "export", str(out)]) assert r.exit_code == 0 and "wrote 1 items" in r.output and out.is_file() r = runner.invoke(app, ["-c", str(toml), "check"]) assert r.exit_code == 0 and "tautulli" in r.output assert Path(cfg.state.cache).is_file() @respx.mock async def test_tui_review_plans_and_executes(cfg, tmp_path, respx_mock): from tests.test_execute import cfg_with_arr, mock_arrs writes = mock_arrs(respx_mock) arr_cfg = cfg_with_arr(tmp_path) cfg.arr = arr_cfg.arr inv = _inventory() inv.titles[1].external_ids = {"tmdb": "1372"} # Big Unwatched resolves to Blood Diamond's radarr entry sessions, admins = fetch_sessions(cfg) ledger = Ledger(cfg.ledger_path) ledger.set("plex://movie/bbb", "reap", kind="movie", title="Big Unwatched") result = assemble(cfg, inv, sessions, admins, ledger.decisions(), now=NOW) app_ = ReaperApp(result, ledger, cfg=cfg, export_path=tmp_path / "o.json") async with app_.run_test() as pilot: await pilot.pause() await app_.workers.wait_for_complete() assert app_.arr_info["plex://movie/bbb"]["profile"] == "HD-1080p" await pilot.press("x") await pilot.pause() screen = app_.screen await screen.workers.wait_for_complete() await pilot.pause() assert screen.plan and screen.plan[0].ok and screen.plan[0].arr_id == 11 assert not writes["del_movie"].called await pilot.press("X") await pilot.pause() await pilot.press("y") await pilot.pause() await screen.workers.wait_for_complete() await pilot.pause() assert writes["del_movie"].called and writes["put_movie"].called assert ledger.decisions() == {} assert [r["title"] for r in ledger.executions()] == ["Big Unwatched"] def test_cli_execute_dry_run(cfg, tmp_path, monkeypatch): from tests.test_execute import mock_arrs inv = _inventory() inv.titles[1].external_ids = {"tmdb": "1372"} save_cached_inventory(cfg.cache_path, inv) toml = tmp_path / "plex-reaper.toml" toml.write_text( f'[plex]\nurl = "http://plex.test"\ntoken = "tok"\n' f'[[tautulli]]\nname = "main"\ndb = "{cfg.tautulli[0].db}"\n' '[[arr]]\nname = "radarr"\ntype = "radarr"\nurl = "http://radarr.test"\nlibraries = [2]\n' f'[state]\nledger = "{cfg.state.ledger}"\ncache = "{cfg.state.cache}"\n' ) monkeypatch.setenv("RADARR_API_KEY", "k") Ledger(cfg.ledger_path).set("plex://movie/bbb", "reap", kind="movie", title="Big Unwatched") with respx.mock as respx_mock: writes = mock_arrs(respx_mock) r = CliRunner().invoke(app, ["-c", str(toml), "execute"]) assert r.exit_code == 0, r.output assert "dry run" in r.output and "1 of 1 executable" in r.output assert not writes["del_movie"].called r = CliRunner().invoke(app, ["-c", str(toml), "execute", "--no-dry-run"], input="n\n") assert r.exit_code == 0 and "aborted" in r.output and not writes["del_movie"].called r = CliRunner().invoke(app, ["-c", str(toml), "execute", "--no-dry-run", "--yes"]) assert r.exit_code == 0, r.output assert writes["del_movie"].called and "done" in r.output r = CliRunner().invoke(app, ["-c", str(toml), "executions"]) assert "Big Unwatched" in r.output r = CliRunner().invoke(app, ["-c", str(toml), "check"]) assert "radarr" in r.output and "HD-1080p" in r.output def test_cli_retarget_and_mark_4k(cfg, tmp_path, monkeypatch): from tests.test_execute import mock_arrs inv = _inventory() inv.titles.append(make_title("plex://show/ww", rating_key=7, kind="show", title="Westworld", library_id=3, library_name="TV Shows")) inv.titles[-1].external_ids = {"tvdb": "296762"} save_cached_inventory(cfg.cache_path, inv) toml = tmp_path / "plex-reaper.toml" toml.write_text( f'[plex]\nurl = "http://plex.test"\ntoken = "tok"\n' f'[[tautulli]]\nname = "main"\ndb = "{cfg.tautulli[0].db}"\n' '[[arr]]\nname = "sonarr"\ntype = "sonarr"\nurl = "http://sonarr.test"\nlibraries = [3]\n' '[arr.downgrade]\n"Ultra-HD" = "HD-1080p"\n' f'[state]\nledger = "{cfg.state.ledger}"\ncache = "{cfg.state.cache}"\n' ) monkeypatch.setenv("SONARR_API_KEY", "k") monkeypatch.setenv("COLUMNS", "250") # keep rich from wrapping table cells runner = CliRunner() with respx.mock as respx_mock: writes = mock_arrs(respx_mock) r = runner.invoke(app, ["-c", str(toml), "retarget", "--arr", "sonarr", "--from", "Ultra-HD", "--to", "HD-1080p"]) assert r.exit_code == 0, r.output assert "1 titles on 'Ultra-HD'" in r.output and not writes["editor_s"].called r = runner.invoke(app, ["-c", str(toml), "retarget", "--arr", "sonarr", "--from", "Ultra-HD", "--to", "HD-1080p", "--ended-only", "--no-dry-run", "--yes"]) assert r.exit_code == 0, r.output assert "retargeted 1" in r.output and writes["editor_s"].called r = runner.invoke(app, ["-c", str(toml), "retarget", "--arr", "sonarr", "--from", "Nope", "--to", "HD-1080p"]) assert r.exit_code != 0 and "not found" in r.output r = runner.invoke(app, ["-c", str(toml), "mark-4k", "--arr", "sonarr", "--min-gb", "10"]) assert r.exit_code == 0, r.output assert "Westworld" in r.output and "would mark" in r.output and "37G" in r.output assert Ledger(cfg.ledger_path).decisions() == {} r = runner.invoke(app, ["-c", str(toml), "mark-4k", "--arr", "sonarr", "--min-gb", "10", "--no-dry-run"]) assert r.exit_code == 0, r.output assert Ledger(cfg.ledger_path).decisions() == {"plex://show/ww": "downgrade"} r = runner.invoke(app, ["-c", str(toml), "execute", "--only", "downgrade", "--limit", "1"]) assert r.exit_code == 0, r.output assert "keep 1 at/below target" in r.output and "dry run" in r.output