from app.core.data_source.runtime import (
    DataSourceRuntimeConfig,
    build_standalone_data_source_runtime,
)


def test_standalone_runtime_uses_explicit_dependencies(monkeypatch):
    created = {}

    class PlatformEngine:
        def dispose(self):
            created["platform_disposed"] = True

    class GraphDriver:
        def close(self):
            created["graph_closed"] = True

    def fake_engine(url, **kwargs):
        created["platform_url"] = url
        created["platform_engine_options"] = kwargs
        return PlatformEngine()

    def fake_graph_driver(uri, **kwargs):
        created["neo4j_uri"] = uri
        created["neo4j_options"] = kwargs
        return GraphDriver()

    monkeypatch.setattr("app.core.data_source.runtime.create_engine", fake_engine)
    monkeypatch.setattr(
        "app.core.data_source.runtime.GraphDatabase.driver",
        fake_graph_driver,
    )
    config = DataSourceRuntimeConfig.from_mapping(
        {
            "platform_database_url": "postgresql://platform",
            "neo4j_uri": "bolt://neo4j:7687",
            "neo4j_user": "neo4j",
            "neo4j_password": "test",
            "credential_master_key": (
                "MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY="
            ),
            "credential_key_version": "v1",
        }
    )

    runtime = build_standalone_data_source_runtime(config)
    runtime.close()

    assert created["platform_url"] == "postgresql://platform"
    assert created["neo4j_uri"] == "bolt://neo4j:7687"
    assert created["platform_disposed"] is True
    assert created["graph_closed"] is True
