from app import views


def test_gpg_status_pass():
    repo = {"repo_config": {"gpgcheck": 1, "repo-gpgcheck": 1}}
    assert views.gpg_status(repo)["level"] == "pass"


def test_gpg_status_warn_when_partial():
    repo = {"repo_config": {"gpgcheck": 1, "repo-gpgcheck": 0}}
    assert views.gpg_status(repo)["level"] == "warn"


def test_gpg_status_unset_when_no_config():
    assert views.gpg_status({})["level"] == "unset"
    assert views.gpg_status({"repo_config": {}})["level"] == "unset"


def test_version_number_parses_trailing_int():
    assert views.version_number("/pulp/api/v3/repositories/rpm/rpm/x/versions/7/") == 7
    assert views.version_number(None) is None
    assert views.version_number("/no/number/here/") is None


def test_package_count_sums_present_counts():
    version = {
        "content_summary": {
            "present": {
                "rpm.package": {"count": 1500},
                "rpm.advisory": {"count": 42},
            }
        }
    }
    assert views.package_count(version) == 1542
    assert views.package_count(None) is None
    assert views.package_count({}) == 0


def test_build_repo_view_combines_repo_and_version():
    repo = {
        "pulp_href": "/pulp/api/v3/repositories/rpm/rpm/abc-123/",
        "name": "rocky9-baseos",
        "latest_version_href": "/pulp/api/v3/repositories/rpm/rpm/abc-123/versions/2/",
        "repo_config": {"gpgcheck": 1, "repo-gpgcheck": 1},
    }
    version = {
        "pulp_created": "2026-06-01T00:00:00Z",
        "content_summary": {"present": {"rpm.package": {"count": 1500}}},
    }
    view = views.build_repo_view(repo, version)
    assert view["name"] == "rocky9-baseos"
    assert view["uuid"] == "abc-123"
    assert view["latest_version"] == 2
    assert view["package_count"] == 1500
    assert view["last_sync"] == "2026-06-01T00:00:00Z"
    assert view["gpg"]["level"] == "pass"
    assert view["deployed_version"] is None  # 단계1 미구현


def test_summarize_counts():
    repo_views = [
        {"gpg": {"level": "pass"}, "package_count": 100},
        {"gpg": {"level": "warn"}, "package_count": 50},
        {"gpg": {"level": "pass"}, "package_count": None},
    ]
    summary = views.summarize(repo_views)
    assert summary == {
        "total": 3,
        "syncing": 0,
        "gpg_pass": 2,
        "total_packages": 150,
    }
