"""P6: ensure the orphan-team cron job is registered alongside the existing
Restate cron jobs and points at a real CleanupService handler.

The job entry is consumed by `app/main.py:_register_cron_jobs` at startup,
which materialises Restate `CronJob` invocations. A typo or missing handler
name would silently mean orphan teams accumulate forever, so we lock the
contract in a test.
"""

from __future__ import annotations

from app.cron_jobs import CRON_JOBS


def test_orphan_team_cleanup_cron_is_registered() -> None:
    entry = next((j for j in CRON_JOBS if j["key"] == "cleanup-orphan-teams"), None)
    assert entry is not None, "cleanup-orphan-teams cron job is missing"
    assert entry["target_service"] == "CleanupService"
    assert entry["target_handler"] == "cleanup_orphan_teams"
    assert entry["payload"] is None
    # Daily schedule; field-of-five cron expression.
    assert len(entry["cron_expression"].split()) == 5


def test_orphan_team_cron_handler_exists_on_restate_service() -> None:
    """Lock the symbol — if someone renames the handler in cleanup.py without
    updating cron_jobs.py, this test fails."""
    import cleanup  # ty: ignore[unresolved-import]  # provided via pyproject pythonpath

    assert hasattr(cleanup.cleanup_service, "handlers") or hasattr(
        cleanup, "cleanup_orphan_teams"
    ), "cleanup.cleanup_orphan_teams is missing"


def test_no_duplicate_cron_keys() -> None:
    keys = [job["key"] for job in CRON_JOBS]
    assert len(keys) == len(set(keys)), f"Duplicate cron keys: {keys}"
