"""Integration tests for the offline-first sync endpoints. Drives POST /sync/push (idempotent append of like/play events) and GET /sync/changes (delta pull with a server-clock cursor). Requires a reachable Postgres; skips otherwise. """ import asyncio import os import uuid from collections.abc import AsyncIterator from pathlib import Path import pytest from app.core.config import get_settings from app.infrastructure.db import Base, dispose_engine, get_engine, session_scope from app.infrastructure.db.repositories import ( SqlAlchemyArtistRepository, SqlAlchemyRefreshTokenRepository, SqlAlchemyTrackRepository, SqlAlchemyUserRepository, ) from asgi_lifespan import LifespanManager from httpx import ASGITransport, AsyncClient pytestmark = pytest.mark.asyncio _db_reachable_cache: bool | None = None async def _db_reachable() -> bool: global _db_reachable_cache if _db_reachable_cache is not None: return _db_reachable_cache from sqlalchemy import text try: async with asyncio.timeout(3): async with get_engine().connect() as conn: await conn.execute(text("SELECT 1")) _db_reachable_cache = True except Exception: _db_reachable_cache = False return _db_reachable_cache async def _seed_track(source_id: str = "sync-1") -> uuid.UUID: async with session_scope() as session: artist = await SqlAlchemyArtistRepository(session).get_or_create("Sync Artist") tid = uuid.uuid4() await SqlAlchemyTrackRepository(session).add( id=tid, title="Sync Track", artist_id=artist.id, storage_uri=f"tracks/zz/{source_id}.mp3", file_format="mp3", file_size=10, source="upload", source_id=source_id, metadata_status="enriched", added_by=None, ) return tid @pytest.fixture async def api(tmp_path: Path) -> AsyncIterator[AsyncClient]: if not await _db_reachable(): pytest.skip("Postgres not reachable — integration test skipped.") os.environ["MEDIA_PATH"] = str(tmp_path) get_settings.cache_clear() try: async with get_engine().begin() as conn: await conn.run_sync(Base.metadata.drop_all) await conn.run_sync(Base.metadata.create_all) from app.application.user_service import UserService from app.core.security import Argon2PasswordHasher async with session_scope() as session: await UserService( users=SqlAlchemyUserRepository(session), refresh_tokens=SqlAlchemyRefreshTokenRepository(session), hasher=Argon2PasswordHasher(), ).create_user(username="syncer", password="testpass1", is_superuser=False) from app.main import create_app app = create_app() async with LifespanManager(app): transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: yield client async with get_engine().begin() as conn: await conn.run_sync(Base.metadata.drop_all) await dispose_engine() finally: os.environ.pop("MEDIA_PATH", None) get_settings.cache_clear() async def _auth(api: AsyncClient) -> dict[str, str]: resp = await api.post( "/api/v1/auth/login", json={"username": "syncer", "password": "testpass1"} ) assert resp.status_code == 200, resp.text return {"Authorization": f"Bearer {resp.json()['access_token']}"} async def test_push_is_idempotent_and_pull_returns_events(api: AsyncClient) -> None: headers = await _auth(api) track_id = await _seed_track() like_id, play_id = str(uuid.uuid4()), str(uuid.uuid4()) push_body = { "likes": [ { "id": like_id, "track_id": str(track_id), "value": "like", "created_at": "2020-01-01T00:00:00+00:00", } ], "plays": [ { "id": play_id, "track_id": str(track_id), "played_at": "2020-01-01T00:05:00+00:00", "play_duration_seconds": 180, "completed": True, } ], } first = await api.post("/api/v1/sync/push", json=push_body, headers=headers) assert first.status_code == 200, first.text assert first.json()["accepted_likes"] == 1 assert first.json()["accepted_plays"] == 1 # Replaying the exact same events must store nothing (idempotent by id). replay = await api.post("/api/v1/sync/push", json=push_body, headers=headers) assert replay.json()["accepted_likes"] == 0 assert replay.json()["accepted_plays"] == 0 # Full pull returns the events + the catalogue track, with a cursor. changes = await api.get("/api/v1/sync/changes", headers=headers) assert changes.status_code == 200, changes.text body = changes.json() assert [lk["id"] for lk in body["likes"]] == [like_id] assert body["likes"][0]["created_at"].startswith("2020-01-01") # event time preserved assert [p["id"] for p in body["plays"]] == [play_id] assert str(track_id) in [t["id"] for t in body["tracks"]] assert body["cursor"] async def test_cursor_advances_and_empty_delta(api: AsyncClient) -> None: headers = await _auth(api) track_id = await _seed_track() await api.post( "/api/v1/sync/push", json={ "likes": [ { "id": str(uuid.uuid4()), "track_id": str(track_id), "value": "like", "created_at": "2024-01-01T00:00:00+00:00", } ] }, headers=headers, ) cursor = (await api.get("/api/v1/sync/changes", headers=headers)).json()["cursor"] # Nothing changed since the cursor → empty delta (but a fresh cursor). delta = await api.get("/api/v1/sync/changes", params={"since": cursor}, headers=headers) assert delta.status_code == 200, delta.text assert delta.json()["likes"] == [] assert delta.json()["plays"] == [] async def test_offline_event_with_old_time_still_syncs(api: AsyncClient) -> None: # The delta is keyed by server-ingestion time, not the client event time, so # an event pushed now with an old created_at still surfaces for a cursor that # sits *after* that old event time. headers = await _auth(api) track_id = await _seed_track() baseline = (await api.get("/api/v1/sync/changes", headers=headers)).json()["cursor"] await api.post( "/api/v1/sync/push", json={ "likes": [ { "id": str(uuid.uuid4()), "track_id": str(track_id), "value": "like", "created_at": "2019-06-01T00:00:00+00:00", # long before `baseline` } ] }, headers=headers, ) delta = await api.get("/api/v1/sync/changes", params={"since": baseline}, headers=headers) assert len(delta.json()["likes"]) == 1 # surfaced despite the old event time async def test_push_skips_unknown_track(api: AsyncClient) -> None: headers = await _auth(api) resp = await api.post( "/api/v1/sync/push", json={ "likes": [ { "id": str(uuid.uuid4()), "track_id": str(uuid.uuid4()), # not in the catalogue "value": "like", "created_at": "2024-01-01T00:00:00+00:00", } ] }, headers=headers, ) assert resp.status_code == 200, resp.text assert resp.json()["accepted_likes"] == 0 async def test_pull_includes_changed_playlists_with_track_ids(api: AsyncClient) -> None: headers = await _auth(api) track_id = await _seed_track() created = await api.post("/api/v1/playlists", json={"name": "Sync Mix"}, headers=headers) playlist_id = created.json()["id"] await api.post( f"/api/v1/playlists/{playlist_id}/tracks", json={"track_id": str(track_id)}, headers=headers, ) body = (await api.get("/api/v1/sync/changes", headers=headers)).json() playlists = {p["id"]: p for p in body["playlists"]} assert playlist_id in playlists assert playlists[playlist_id]["track_ids"] == [str(track_id)] async def test_sync_requires_auth(api: AsyncClient) -> None: assert (await api.get("/api/v1/sync/changes")).status_code == 401 assert (await api.post("/api/v1/sync/push", json={})).status_code == 401