from __future__ import annotations from typing import Any import pytest from activity_core import api @pytest.mark.asyncio async def test_admin_sync_definitions_only_does_not_require_temporal( monkeypatch, ) -> None: seen: dict[str, Any] = {} async def fake_run_sync(**kwargs: Any) -> dict[str, Any]: seen.update(kwargs) return {"ok": True, "ran": {"definitions": True}} monkeypatch.setattr(api, "_session_factory", object()) monkeypatch.setattr(api, "_temporal_client", None) monkeypatch.setattr(api, "run_sync", fake_run_sync) result = await api.admin_sync( definitions=True, schedules=False, event_types=False, ) assert result == {"ok": True, "ran": {"definitions": True}} assert seen["session_factory"] is api._session_factory assert seen["temporal_client"] is None assert seen["definitions"] is True assert seen["schedules"] is False assert seen["event_types"] is False @pytest.mark.asyncio async def test_admin_sync_schedules_only_passes_temporal(monkeypatch) -> None: temporal = object() seen: dict[str, Any] = {} async def fake_run_sync(**kwargs: Any) -> dict[str, Any]: seen.update(kwargs) return { "ok": True, "schedules": { "upserted": 1, "paused": 0, "deleted_orphans": 0, }, } monkeypatch.setattr(api, "_session_factory", object()) monkeypatch.setattr(api, "_temporal_client", temporal) monkeypatch.setattr(api, "run_sync", fake_run_sync) result = await api.admin_sync( definitions=False, schedules=True, event_types=False, ) assert result["schedules"]["upserted"] == 1 assert seen["temporal_client"] is temporal assert seen["definitions"] is False assert seen["schedules"] is True assert seen["event_types"] is False @pytest.mark.asyncio async def test_admin_sync_all_sync_returns_failure_result(monkeypatch) -> None: async def fake_run_sync(**kwargs: Any) -> dict[str, Any]: return { "ok": False, "ran": { "definitions": kwargs["definitions"], "schedules": kwargs["schedules"], "event_types": kwargs["event_types"], }, "errors": [ { "stage": "event_types", "type": "RuntimeError", "message": "bad event type", } ], } monkeypatch.setattr(api, "_session_factory", object()) monkeypatch.setattr(api, "_temporal_client", object()) monkeypatch.setattr(api, "run_sync", fake_run_sync) result = await api.admin_sync( definitions=True, schedules=True, event_types=True, ) assert result == { "ok": False, "ran": { "definitions": True, "schedules": True, "event_types": True, }, "errors": [ { "stage": "event_types", "type": "RuntimeError", "message": "bad event type", } ], }