wrolpi/modules/ai/conftest.py
Roland 861e183bd2
ai: cache the models manifest, skip it offline, show a loading state
The Manage tab fetched the CDN manifest and its signature on every
catalog request with a 30s timeout each, and the tab polls every 30s.
On a black-holed or offline network every poll paid up to 60s before
falling back to the bundled list, the frontend's 60s limit threw the
response away, and the tab showed a blank page the whole time.

get_models_catalog now skips the CDN while the have_internet flag is
explicitly clear, reuses the last attempt (success or failure) for an
hour, and uses a 5s timeout for the 2KB manifest. A skipped attempt is
not cached so the CDN is tried as soon as internet comes up. The cache
is per process and is cleared between tests by an autouse fixture.

The Manage tab shows "Loading the model catalog" while the first
response is pending, and on failure shows the error with a Retry
button and a note that the API may still be starting, instead of
rendering nothing.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-24 18:28:11 -06:00

26 lines
918 B
Python

# AI module test fixtures. The docs conftest is not star-imported by the root conftest, but the
# AI endpoints cover docs; import its fixtures so AI tests can use doc_factory.
import pytest
from modules.ai.config import ai_config_context
from modules.docs.conftest import * # noqa
from wrolpi.api_utils import api_app
@pytest.fixture
def test_ai_config(test_directory, async_client):
"""An isolated ai.yaml config for one test."""
(test_directory / 'config').mkdir(exist_ok=True)
config_path = test_directory / 'config/ai.yaml'
with ai_config_context() as config:
config.initialize(api_app.shared_ctx.ai_config)
yield config_path
@pytest.fixture(autouse=True)
def _clear_models_catalog_cache():
"""Each test starts with an empty manifest cache."""
from modules.ai import catalog
catalog.clear_models_catalog_cache()
yield
catalog.clear_models_catalog_cache()