"""The application context owns the process-wide instances (ARCH-A4). Importing `core.shared.deps` used to build a DataManager (which opens DuckDB and parses the calendar/metadata tables), four provider objects, an HFManager and two thread pools. The names are now handles that resolve through the lifespan-managed `AppContext`. """ import asyncio import pytest from core.shared import deps, http_client _HANDLES = ( deps.data_manager, deps.finviz, deps.hf_manager, deps.SCAN_EXECUTOR, deps.PROVIDER_EXECUTOR, deps.RENDER_EXECUTOR, ) @pytest.fixture(autouse=True) def _isolated_handles() -> None: """No resolver, no instance carried over from an earlier test, and the provider registry wired. `AppContext.data_manager` builds a `DataManager`, which resolves the `alpaca` provider through `core.ports`. In the app that registration is done by the lifespan (`app/main.py` calls `register_providers()`); a bare `AppContext()` in a test therefore has to be a composition root itself, or it only passes when another test happened to register first. """ from app.context import register_providers register_providers() deps.set_shared_resolver(None) for handle in _HANDLES: handle._value = None yield deps.set_shared_resolver(None) for handle in _HANDLES: handle._value = None @pytest.fixture def _no_resolver() -> None: deps.set_shared_resolver(None) yield deps.set_shared_resolver(None) def test_importing_deps_builds_nothing() -> None: # Every name below is a handle whose _value is the private instance it # built. None has been built, because nothing has asked for one. for handle in _HANDLES: assert handle._value is None def test_handle_forwards_attributes_to_one_instance(_no_resolver: None) -> None: first = deps.data_manager.metadata_db_path assert deps.data_manager._value is not None assert deps.data_manager.metadata_db_path == first def test_handle_forwards_writes_to_the_instance(_no_resolver: None) -> None: deps.hf_manager.repo_id = "someone/else" try: assert deps.hf_manager._value.repo_id == "someone/else" # type: ignore[union-attr] finally: deps.hf_manager._value.repo_id = "back" # type: ignore[union-attr] def test_context_resolver_supplies_the_instances() -> None: from app.context import AppContext context = AppContext() deps.set_shared_resolver(context.resolve) try: assert deps.data_manager._resolve() is context.data_manager assert deps.SCAN_EXECUTOR._resolve() is context.scan_executor # Not cached locally, so a context swap is picked up. assert deps.data_manager._value is None finally: deps.set_shared_resolver(None) def test_context_resolve_lowercases_the_screaming_case_names() -> None: from app.context import AppContext context = AppContext() assert context.resolve("PROVIDER_EXECUTOR") is context.provider_executor assert context.resolve("data_manager") is context.data_manager def test_every_shared_handle_resolves_through_the_context() -> None: """A name the context cannot resolve is an AttributeError at first use. RENDER_EXECUTOR was missing from `_SHARED_NAMES`, so the calendar PDF export -- the one caller -- raised `'AppContext' object has no attribute 'render_executor'` once the lifespan had published the resolver. """ from app.context import AppContext context = AppContext() deps.set_shared_resolver(context.resolve) try: for name in context.shared_names(): assert context.resolve(name) is getattr(context, name.lower()) assert deps.RENDER_EXECUTOR._resolve() is context.render_executor assert deps.RENDER_EXECUTOR.submit(lambda: 42).result() == 42 finally: deps.set_shared_resolver(None) def test_resolve_rejects_a_name_the_context_does_not_own() -> None: from app.context import AppContext context = AppContext() with pytest.raises(KeyError): context.resolve("NOT_A_SHARED_NAME") def test_context_builds_each_shared_instance_once() -> None: from app.context import AppContext context = AppContext() assert context.data_manager is context.data_manager assert context.finviz is context.finviz def test_aclose_closes_the_yahoo_pool_and_is_idempotent() -> None: from app.context import AppContext context = AppContext() pool = context.yahoo_client_pool assert pool.get() is pool.get() assert pool.is_open asyncio.run(context.aclose()) assert not pool.is_open asyncio.run(context.aclose()) def test_closed_context_rejects_reconstruction_instead_of_leaking_instances() -> None: from app.context import AppContext context = AppContext() asyncio.run(context.aclose()) with pytest.raises(RuntimeError, match="closed"): _ = context.data_manager with pytest.raises(RuntimeError, match="closed"): _ = context.scan_executor assert context._instances == {} def test_active_pool_is_the_one_yahoo_client_resolves() -> None: from app.context import AppContext context = AppContext() http_client.set_active_pool(context.yahoo_client_pool) try: assert http_client.yahoo_client() is context.yahoo_client_pool.get() finally: http_client.set_active_pool(None) # Unset falls back to the module's own pool, not the context's. assert http_client.yahoo_client() is not context.yahoo_client_pool.get() def test_lifespan_publishes_and_releases_the_context() -> None: from fastapi.testclient import TestClient from app.main import app with TestClient(app) as client: assert client.get("/api/health").status_code in (200, 404) context = app.state.context assert deps.data_manager._resolve() is context.data_manager assert http_client._active_pool is context.yahoo_client_pool assert app.state.context is context assert deps._shared_resolver is None assert http_client._active_pool is None