Spaces:
Sleeping
Sleeping
| """Ephemeral store path must not block backend init unless KINK_FAIL_ON_EPHEMERAL_STORE is set.""" | |
| from __future__ import annotations | |
| import os | |
| import sys | |
| from pathlib import Path | |
| import pytest | |
| from backend import Backend | |
| def _ephemeral_db_path(name: str) -> Path: | |
| root = Path("/tmp") / "kink_cli_ephemeral_tests" | |
| root.mkdir(parents=True, exist_ok=True) | |
| return root / name | |
| def _ensure_store_passthrough(target: Path) -> Path: | |
| resolved = Path(target).resolve() | |
| assert resolved.is_file(), "fixture must supply a non-empty SQLite file" | |
| return resolved | |
| def valid_sqlite_under_tmp() -> Path: | |
| """Real DB under /tmp so _store_path_is_ephemeral is true (Linux HF Spaces default).""" | |
| p = _ephemeral_db_path(f"ephemeral_guard_{os.getpid()}.db") | |
| if p.is_file(): | |
| p.unlink() | |
| Backend(p) | |
| return p.resolve() | |
| def _fresh_api() -> object: | |
| sys.modules.pop("api", None) | |
| return __import__("api", fromlist=["app"]) | |
| def test_space_require_full_catalog_ephemeral_store_still_initializes_backend( | |
| monkeypatch: pytest.MonkeyPatch, | |
| valid_sqlite_under_tmp: Path, | |
| ) -> None: | |
| """Regression: SPACE_ID + require-full + /tmp used to raise before Backend() (live Space 500s).""" | |
| monkeypatch.setenv("KINK_STORE_PATH", str(valid_sqlite_under_tmp)) | |
| monkeypatch.setenv("SPACE_ID", "hf-test") | |
| monkeypatch.setenv("KINK_HF_REQUIRE_FULL_CATALOG", "1") | |
| monkeypatch.setenv("KINK_SKIP_HEAVY_WARM", "1") | |
| api_mod = _fresh_api() | |
| monkeypatch.setattr(api_mod, "ensure_store_db", _ensure_store_passthrough) | |
| api_mod._backend_impl = None | |
| b = api_mod._get_backend() | |
| assert b is not None | |
| assert api_mod._backend_impl is b | |
| def test_fail_on_ephemeral_store_env_still_aborts(monkeypatch: pytest.MonkeyPatch, valid_sqlite_under_tmp: Path) -> None: | |
| monkeypatch.setenv("KINK_STORE_PATH", str(valid_sqlite_under_tmp)) | |
| monkeypatch.setenv("SPACE_ID", "hf-test") | |
| monkeypatch.setenv("KINK_HF_REQUIRE_FULL_CATALOG", "1") | |
| monkeypatch.setenv("KINK_SKIP_HEAVY_WARM", "1") | |
| monkeypatch.setenv("KINK_FAIL_ON_EPHEMERAL_STORE", "1") | |
| api_mod = _fresh_api() | |
| monkeypatch.setattr(api_mod, "ensure_store_db", _ensure_store_passthrough) | |
| api_mod._backend_impl = None | |
| with pytest.raises(RuntimeError, match="ephemeral"): | |
| api_mod._get_backend() | |