forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
74 lines (56 loc) · 2.44 KB
/
Copy pathconftest.py
File metadata and controls
74 lines (56 loc) · 2.44 KB
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
"""Pytest fixtures for the omi-cli test suite.
Each test gets:
* An isolated config dir under a temp path (via the ``OMI_CONFIG`` env var).
* A pre-seeded "default" profile with a fake API key.
* A respx mock router pointing at the fake API base URL so HTTP calls are
intercepted and asserted against, rather than going to the real Omi API.
"""
from __future__ import annotations
import os
from pathlib import Path
from typing import Iterator
import pytest
import respx
from typer.testing import CliRunner
from omi_cli import config as cfg
from omi_cli.auth.store import store_api_key
FAKE_API_BASE = "https://api.test.omi.local"
FAKE_API_KEY = "omi_dev_" + ("x" * 32)
@pytest.fixture
def config_path(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Isolate the config file to ``tmp_path`` for each test."""
target = tmp_path / "config.toml"
monkeypatch.setenv(cfg.ENV_CONFIG_PATH, str(target))
# Clean up any inherited credentials from the user's real env.
for var in (cfg.ENV_API_KEY, cfg.ENV_API_BASE, cfg.ENV_LOCAL_API_URL, cfg.ENV_LOCAL_TOKEN, cfg.ENV_PROFILE):
monkeypatch.delenv(var, raising=False)
monkeypatch.delenv("NO_COLOR", raising=False)
monkeypatch.delenv("OMI_NO_COLOR", raising=False)
return target
@pytest.fixture
def authed_profile(config_path: Path) -> cfg.Profile:
"""Create a default profile pre-loaded with a fake API key + custom api_base."""
return store_api_key("default", FAKE_API_KEY, api_base=FAKE_API_BASE)
@pytest.fixture
def respx_mock() -> Iterator[respx.MockRouter]:
"""A respx router scoped to the fake API base URL."""
with respx.mock(base_url=FAKE_API_BASE, assert_all_called=False) as router:
yield router
@pytest.fixture
def cli_runner() -> CliRunner:
"""Typer's CliRunner — used to invoke the root ``app`` in tests.
Click 8.2+ separates stderr from stdout by default (the old ``mix_stderr``
argument was removed). Tests can read ``result.stderr`` directly to assert
the JSON-mode agent contract.
"""
try:
return CliRunner(mix_stderr=False)
except TypeError:
# Click 8.2+ removed mix_stderr and always captures stderr separately.
pass
return CliRunner()
@pytest.fixture(autouse=True)
def _reset_typer_state(monkeypatch: pytest.MonkeyPatch) -> None:
"""Stop Typer/Rich from picking up the real terminal width during tests."""
monkeypatch.setenv("TERM", "dumb")
monkeypatch.setenv("COLUMNS", "200")