"""Tests Maris agent workspace FastAPI aplikācijai.""" from __future__ import annotations import importlib import json import sys import tomllib from pathlib import Path from fastapi.testclient import TestClient REPO_ROOT = Path(__file__).resolve().parents[2] if str(REPO_ROOT) not in sys.path: sys.path.insert(0, str(REPO_ROOT)) space_app = importlib.import_module("huggingface_space.app") class DummyProcess: def __init__(self, pid: int = 4242) -> None: self.pid = pid def poll(self) -> None: return None def test_status_endpoint_includes_progress_metadata() -> None: client = TestClient(space_app.app) with space_app.STATE_LOCK: original_state = dict(space_app.TRAINING_STATE) space_app.TRAINING_STATE.update( { "process": None, "log_path": "", "log_handle": None, "started_at": None, "finished_at": None, "request": {"num_epochs": 3}, "stop_requested": False, } ) try: response = client.get("/status") finally: with space_app.STATE_LOCK: space_app.TRAINING_STATE.update(original_state) assert response.status_code == 200 body = response.json() assert "progress" in body assert body["progress"]["stage"] == "queued" assert "history" in body def test_maybe_start_automatic_training_starts_with_space_defaults( monkeypatch, tmp_path: Path ) -> None: calls: list[dict[str, object]] = [] monkeypatch.setenv("MARIS_SPACE_AUTO_TRAIN", "true") monkeypatch.setattr(space_app, "PERSISTENT_DIR", str(tmp_path)) monkeypatch.setattr(space_app, "has_completed_training_artifacts", lambda output_dir: False) monkeypatch.setattr( space_app, "_start_training_process", lambda request: ( calls.append(request.model_dump()) or {"pid": 99, "log_path": "/tmp/train.log"} ), ) space_app._maybe_start_automatic_training() assert len(calls) == 1 assert calls[0]["dataset_repo"] == space_app.AGENT_RUNTIME.dataset_repo assert calls[0]["model_repo"] == space_app.AGENT_RUNTIME.model_repo assert calls[0]["model_preset"] == "balanced" assert calls[0]["continue_from_latest_artifact"] is True def test_maybe_start_automatic_training_skips_when_completed_artifacts_exist( monkeypatch, tmp_path: Path ) -> None: monkeypatch.setenv("MARIS_SPACE_AUTO_TRAIN", "true") monkeypatch.setattr(space_app, "PERSISTENT_DIR", str(tmp_path)) monkeypatch.setattr(space_app, "has_completed_training_artifacts", lambda output_dir: True) monkeypatch.setattr( space_app, "_start_training_process", lambda request: (_ for _ in ()).throw(AssertionError("auto training should be skipped")), ) space_app._maybe_start_automatic_training() def test_index_endpoint_defaults_to_balanced_preset() -> None: client = TestClient(space_app.app) response = client.get("/") assert response.status_code == 200 assert '