from app.core.data_source.runtime import DataSourceRuntimeConfig


def test_runner_pool_configuration_is_explicit_and_connection_budgeted():
    config = DataSourceRuntimeConfig.from_mapping(
        {
            "platform_database_url": "postgresql://platform",
            "neo4j_uri": "bolt://neo4j:7687",
            "neo4j_user": "neo4j",
            "neo4j_password": "test",
            "credential_master_key": "key",
            "credential_key_version": "v1",
            "pool_size": 1,
            "max_overflow": 1,
            "max_idle_pools": 4,
            "worker_count": 2,
        }
    )

    assert config.max_business_connections == 16
    assert config.pool_settings()["pool_size"] == 1
    assert "platform_database_url" not in repr(config)
    assert "neo4j_password" not in repr(config)


def test_runner_connection_budget_rejects_unsafe_configuration():
    import pytest

    with pytest.raises(ValueError, match="connection budget"):
        DataSourceRuntimeConfig.from_mapping(
            {
                "platform_database_url": "postgresql://platform",
                "neo4j_uri": "bolt://neo4j:7687",
                "neo4j_user": "neo4j",
                "neo4j_password": "test",
                "credential_master_key": "key",
                "credential_key_version": "v1",
                "pool_size": 2,
                "max_overflow": 3,
                "max_idle_pools": 20,
                "worker_count": 4,
                "connection_budget": 50,
            }
        )
