Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions tests/test_load_from_plex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import asyncio
import types
import httpx

from mcp_plex import loader
from mcp_plex.types import TMDBShow


def test_load_from_plex(monkeypatch):
movie = types.SimpleNamespace(
ratingKey="1",
guid="g1",
type="movie",
title="Movie",
guids=[
types.SimpleNamespace(id="imdb://ttm"),
types.SimpleNamespace(id="tmdb://1"),
],
)

ep1 = types.SimpleNamespace(
ratingKey="2",
guid="g2",
type="episode",
title="Ep1",
guids=[
types.SimpleNamespace(id="imdb://tt1"),
types.SimpleNamespace(id="tmdb://2"),
],
)
ep2 = types.SimpleNamespace(
ratingKey="3",
guid="g3",
type="episode",
title="Ep2",
guids=[types.SimpleNamespace(id="imdb://tt2")],
)

show = types.SimpleNamespace(
guids=[types.SimpleNamespace(id="tmdb://3")],
episodes=lambda: [ep1, ep2],
)

movie_section = types.SimpleNamespace(all=lambda: [movie])
show_section = types.SimpleNamespace(all=lambda: [show])
library = types.SimpleNamespace(
section=lambda name: movie_section if name == "Movies" else show_section
)
server = types.SimpleNamespace(library=library)

async def handler(request):
url = str(request.url)
if "imdbapi" in url:
return httpx.Response(
200, json={"id": "tt", "type": "movie", "primaryTitle": "IMDb"}
)
if "/movie/1" in url:
return httpx.Response(200, json={"id": 1, "title": "TMDB Movie"})
if "/tv/3" in url:
return httpx.Response(200, json={"id": 3, "name": "TMDB Show"})
if "/episode/2" in url:
return httpx.Response(200, json={"id": 2, "name": "TMDB Ep"})
return httpx.Response(404)

transport = httpx.MockTransport(handler)
orig_client = httpx.AsyncClient
monkeypatch.setattr(
loader.httpx,
"AsyncClient",
lambda *args, **kwargs: orig_client(transport=transport),
)

items = asyncio.run(loader._load_from_plex(server, "key"))
assert len(items) == 3
assert items[0].tmdb and items[0].tmdb.id == 1
assert items[1].tmdb and items[1].tmdb.id == 2
assert isinstance(items[2].tmdb, TMDBShow)
assert items[2].tmdb.id == 3
37 changes: 37 additions & 0 deletions tests/test_loader_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ async def upsert(self, collection_name: str, points):
self.upserted.extend(points)


class TrackingQdrantClient(DummyQdrantClient):
"""Qdrant client that starts with a mismatched collection size."""

def __init__(self, url: str, api_key: str | None = None):
super().__init__(url, api_key)
# Pre-create a collection with the wrong vector size to force recreation
wrong_params = SimpleNamespace(
vectors={
"dense": models.VectorParams(size=99, distance=models.Distance.COSINE)
}
)
self.collections["media-items"] = SimpleNamespace(
config=SimpleNamespace(params=wrong_params)
)
self.deleted = False

async def delete_collection(self, name: str):
self.deleted = True
await super().delete_collection(name)


async def _run_loader(sample_dir: Path):
await loader.run(None, None, None, sample_dir, None, None)

Expand All @@ -79,3 +100,19 @@ def test_run_writes_points(monkeypatch):
assert len(client.upserted) == 2
payloads = [p.payload for p in client.upserted]
assert all("title" in p and "type" in p for p in payloads)


def test_run_recreates_mismatched_collection(monkeypatch):
monkeypatch.setattr(loader, "TextEmbedding", DummyTextEmbedding)
monkeypatch.setattr(loader, "SparseTextEmbedding", DummySparseEmbedding)
monkeypatch.setattr(loader, "AsyncQdrantClient", TrackingQdrantClient)
sample_dir = Path(__file__).resolve().parents[1] / "sample-data"
asyncio.run(_run_loader(sample_dir))
client = TrackingQdrantClient.instance
assert client is not None
# The pre-created collection should have been deleted and recreated
assert client.deleted is True
assert (
client.collections["media-items"].config.params.vectors["dense"].size
== 3
)