import httpx import pytest import respx from plexreaper.sources.plex import PlexClient from plexreaper.sources.tautulli import TautulliDB from tests.conftest import NOW def test_tautulli_reads_sessions_and_admins(tautulli_db): db = TautulliDB("main", tautulli_db) assert db.admin_ids() == {1} sessions = db.sessions() assert len(sessions) == 4 by_guid = {s.guid: s for s in sessions} assert by_guid["plex://episode/e2"].season is None and by_guid["plex://episode/e2"].episode is None assert by_guid["plex://movie/aaa"].percent == 100 assert by_guid["plex://movie/bbb"].is_admin and by_guid["plex://movie/bbb"].added_at is not None ep = by_guid["plex://episode/e1"] assert (ep.season, ep.episode) == (1, 2) info = db.check() assert info["sessions"] == 4 and info["last"].date() == NOW.date() def test_tautulli_missing_file(tmp_path): with pytest.raises(FileNotFoundError): TautulliDB("x", tmp_path / "nope.db").sessions() def _mc(metadata, total=None): return {"MediaContainer": {"size": len(metadata), "totalSize": total or len(metadata), "Metadata": metadata}} MOVIE = { "ratingKey": "10", "guid": "plex://movie/m1", "title": "Cats", "year": 2019, "addedAt": 1_600_000_000, "audienceRating": 3.2, "duration": 6_600_000, "Genre": [{"tag": "Comedy"}], "Collection": [{"tag": "Bad Musicals"}], "Guid": [{"id": "tmdb://568124"}, {"id": "imdb://tt5697572"}], "Media": [{"Part": [{"size": 4_000_000_000}]}, {"Part": [{"size": 1_000_000_000}]}], } SHOW = {"ratingKey": "20", "guid": "plex://show/s1", "title": "Procedural", "year": 2014, "addedAt": 1_500_000_000, "childCount": 3, "leafCount": 2} EPISODES = [ {"ratingKey": "21", "guid": "plex://episode/e1", "grandparentRatingKey": "20", "parentIndex": 1, "index": 1, "duration": 2_400_000, "Media": [{"Part": [{"size": 700_000_000}]}]}, {"ratingKey": "22", "guid": "plex://episode/e2", "grandparentRatingKey": "20", "parentIndex": 2, "index": 1, "duration": 2_400_000, "Media": [{"Part": [{"size": 800_000_000}]}]}, {"ratingKey": "99", "guid": "plex://episode/orphan", "grandparentRatingKey": "404", "Media": []}, ] @respx.mock(base_url="http://plex.test") async def test_plex_inventory_and_history(respx_mock): respx_mock.get("/library/sections").respond(json={"MediaContainer": {"Directory": [ {"key": "2", "title": "Movies", "type": "movie"}, {"key": "3", "title": "TV", "type": "show"}, {"key": "8", "title": "Music", "type": "artist"}, ]}}) respx_mock.get("/library/sections/2/all", params__contains={"type": "1"}).respond(json=_mc([MOVIE])) respx_mock.get("/library/sections/3/all", params__contains={"type": "2"}).respond(json=_mc([SHOW])) # episodes paginate: two pages of one, then the orphan def episodes(request): start = int(request.headers["X-Plex-Container-Start"]) page = EPISODES[start : start + 1] return httpx.Response(200, json=_mc(page, total=len(EPISODES))) respx_mock.get("/library/sections/3/all", params__contains={"type": "4"}).mock(side_effect=episodes) respx_mock.get("/status/sessions/history/all").respond(json=_mc([ {"type": "movie", "ratingKey": "10", "accountID": 1, "viewedAt": 1_650_000_000}, {"type": "movie", "ratingKey": "10", "accountID": 2, "viewedAt": 1_700_000_000}, {"type": "episode", "ratingKey": "21", "grandparentKey": "/library/metadata/20", "viewedAt": 1_600_000_000}, {"type": "track", "ratingKey": "5"}, ])) async with PlexClient("http://plex.test", "tok", page_size=1) as plex: inv = await plex.inventory() hist = await plex.history() by = {t.guid: t for t in inv.titles} m = by["plex://movie/m1"] assert m.size_bytes == 5_000_000_000 and m.external_ids == {"tmdb": "568124", "imdb": "tt5697572"} assert m.collections == ["Bad Musicals"] and m.genres == ["Comedy"] s = by["plex://show/s1"] assert s.size_bytes == 1_500_000_000 and s.episode_count == 2 and s.season_count == 2 assert inv.episode_to_show == {"plex://episode/e1": "plex://show/s1", "plex://episode/e2": "plex://show/s1"} assert hist[10][0] == 2 and hist[10][1].timestamp() == 1_700_000_000 assert hist[20][0] == 1 @respx.mock(base_url="http://plex.test") async def test_plex_library_filter_and_errors(respx_mock): respx_mock.get("/library/sections").respond(json={"MediaContainer": {"Directory": [ {"key": "2", "title": "Movies", "type": "movie"}, {"key": "6", "title": "Obscure", "type": "movie"}]}}) respx_mock.get("/library/sections/6/all").respond(json=_mc([MOVIE])) async with PlexClient("http://plex.test", "tok") as plex: inv = await plex.inventory([6]) assert [t.library_name for t in inv.titles] == ["Obscure"] respx_mock.get("/identity").respond(401) async with PlexClient("http://plex.test", "bad") as plex: with pytest.raises(httpx.HTTPStatusError): await plex.identity()