from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def read(path: str) -> str:
    return (ROOT / path).read_text(encoding="utf-8")


def test_v493_jasmin_sync_respects_days_and_global_limit():
    service = read("app/external_reconciliation_sync.py")
    script = read("scripts/sync_external_reconciliation.py")

    assert "sync_jasmin_reconciliation_candidates(*, limit: int = 100, days: int = 3)" in service
    assert "since =" in service
    assert "_within_days(record, days=days)" in service
    assert "staged.sort" in service
    assert "staged[:limit]" in service
    assert "sync_jasmin_reconciliation_candidates(limit=args.limit, days=args.days)" in script


def test_v493_jasmin_client_supports_odata_filter_and_orderby():
    client = read("app/jasmin_client.py")

    assert "filter: Optional[str] = None" in client
    assert "orderby: Optional[str] = None" in client
    assert 'params["$filter"] = filter' in client
    assert 'params["$orderby"] = orderby' in client


def test_v493_stale_reconciliation_cleanup_script_exists():
    script = read("scripts/cleanup_stale_reconciliation_items.py")

    assert "--jasmin" in script
    assert "--days" in script
    assert "--apply" in script
    assert "cleanup_reconciliation_outside_window" in script
    assert "Dry-run" in script
