File size: 2,399 Bytes
0f9caed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1a62a8
 
0f9caed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Sdílené fixtures — testy běží bez GPU, vLLM nahrazuje tests/mock_vllm.py."""

import os
import socket
import stat
import sys
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

MOCK_VLLM = ROOT / "tests" / "mock_vllm.py"


def free_port() -> int:
    with socket.socket() as s:
        s.bind(("127.0.0.1", 0))
        return s.getsockname()[1]


@pytest.fixture(autouse=True)
def _ensure_mock_executable():
    mode = MOCK_VLLM.stat().st_mode
    MOCK_VLLM.chmod(mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)


@pytest.fixture()
def env(tmp_path, monkeypatch):
    """Izolované prostředí: mock vLLM, tmp persistence, volný port."""
    port = free_port()
    monkeypatch.setenv("VLLM_BINARY", str(MOCK_VLLM))
    monkeypatch.setenv("ENGINE_PORT", str(port))
    monkeypatch.setenv("ENGINE_LOG", str(tmp_path / "engine.log"))
    monkeypatch.setenv("ENGINE_STARTUP_TIMEOUT", "30")
    monkeypatch.setenv("SETTINGS_DIR", str(tmp_path))
    monkeypatch.setenv("MODEL_DOWNLOAD_DIR", str(tmp_path / "downloads"))
    monkeypatch.setenv("LOCAL_MODEL_DIR", str(tmp_path / "local-models"))
    monkeypatch.setenv("PRELOAD_MODEL", "0")
    monkeypatch.setenv("AGENT_API_TOKEN", "test-token")
    monkeypatch.setenv("LOG_FILE", str(tmp_path / "app.log"))
    monkeypatch.setenv("MOCK_VLLM_STARTUP_DELAY", "0.4")
    monkeypatch.delenv("MOCK_VLLM_FAIL", raising=False)
    monkeypatch.delenv("MODEL_PRIMARY", raising=False)
    monkeypatch.delenv("LOCAL_RUNNER_URL", raising=False)
    return {"port": port, "tmp": tmp_path}


def _fresh_import(names=("presets", "settings", "context", "toolservers",
                         "engine", "app")):
    for name in names:
        sys.modules.pop(name, None)
    import importlib
    return {name: importlib.import_module(name) for name in names}


@pytest.fixture()
def app_module(env):
    """Čerstvě naimportovaná aplikace s izolovaným prostředím."""
    mods = _fresh_import()
    app = mods["app"]
    yield app
    app.ENGINE.stop()


@pytest.fixture()
def engine_module(env):
    mods = _fresh_import(("presets", "settings", "engine"))
    yield mods["engine"]


@pytest.fixture()
def settings_module(env):
    mods = _fresh_import(("presets", "settings"))
    yield mods["settings"]


@pytest.fixture()
def auth():
    return {"Authorization": "Bearer test-token"}