maris-ai-master / core-python /tests /test_runtime.py
MarisUK's picture
Maris AI model sync
f440f03 verified
"""Tests runtime konfigurācijai produkcijas izvietošanai."""
from __future__ import annotations
import os
import tomllib
from pathlib import Path
from maris_core.runtime import (
configure_huggingface_environment,
is_reload_enabled,
resolve_host,
resolve_port,
)
def test_resolve_port_prefers_platform_port(monkeypatch) -> None:
monkeypatch.delenv("PORT", raising=False)
assert resolve_port() == 8000
monkeypatch.setenv("PORT", "10000")
assert resolve_port() == 10000
def test_resolve_port_rejects_invalid_value(monkeypatch) -> None:
monkeypatch.setenv("PORT", "invalid")
try:
resolve_port()
except ValueError as exc:
assert str(exc) == "Invalid PORT value: 'invalid'"
else:
raise AssertionError("resolve_port() should reject invalid PORT values")
def test_resolve_host_defaults_to_ipv4_locally(monkeypatch) -> None:
monkeypatch.delenv("HOST", raising=False)
monkeypatch.delenv("RAILWAY_PRIVATE_DOMAIN", raising=False)
monkeypatch.delenv("RAILWAY_PUBLIC_DOMAIN", raising=False)
monkeypatch.delenv("RAILWAY_ENVIRONMENT_ID", raising=False)
assert resolve_host() == "0.0.0.0"
def test_resolve_host_prefers_explicit_host_env(monkeypatch) -> None:
monkeypatch.setenv("HOST", "127.0.0.1")
monkeypatch.setenv("RAILWAY_PRIVATE_DOMAIN", "maris-core-python.railway.internal")
assert resolve_host() == "127.0.0.1"
def test_resolve_host_keeps_ipv4_binding_on_railway(monkeypatch) -> None:
monkeypatch.delenv("HOST", raising=False)
monkeypatch.setenv("RAILWAY_PRIVATE_DOMAIN", "maris-core-python.railway.internal")
assert resolve_host() == "0.0.0.0"
def test_reload_disabled_by_default(monkeypatch) -> None:
monkeypatch.delenv("MARIS_RELOAD", raising=False)
assert is_reload_enabled() is False
def test_reload_can_be_enabled_from_env(monkeypatch) -> None:
monkeypatch.setenv("MARIS_RELOAD", "true")
assert is_reload_enabled() is True
def test_configure_huggingface_environment_sets_standard_runtime_aliases(monkeypatch) -> None:
monkeypatch.setenv("MARIS_REPO_TOKEN", "hf-secret")
monkeypatch.delenv("HF_TOKEN", raising=False)
monkeypatch.delenv("HUGGING_FACE_HUB_TOKEN", raising=False)
monkeypatch.delenv("HUGGINGFACEHUB_API_TOKEN", raising=False)
monkeypatch.delenv("HF_HUB_DISABLE_XET", raising=False)
configure_huggingface_environment()
assert os.environ["HF_TOKEN"] == "hf-secret"
assert os.environ["HUGGING_FACE_HUB_TOKEN"] == "hf-secret"
assert os.environ["HUGGINGFACEHUB_API_TOKEN"] == "hf-secret"
assert os.environ["HF_HUB_DISABLE_XET"] == "1"
def test_railway_healthcheck_uses_health_endpoint() -> None:
railway_config = tomllib.loads(
(Path(__file__).resolve().parents[1] / "railway.toml").read_text(encoding="utf-8")
)
assert railway_config["deploy"]["healthcheckPath"] == "/health"