File size: 4,656 Bytes
2e818da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from __future__ import annotations

from pathlib import Path

import pytest

from app.observability.config import get_observability_config

ENV_VARS = (
    "OTEL_ENABLED",
    "OTEL_MODE",
    "OTEL_EXPORTER_OTLP_ENDPOINT",
    "SIGNOZ_UI_URL",
    "OBSERVABILITY_ARTIFACT_ROOT",
    "EVALUATION_RUN",
    "DEPLOYMENT_ENV",
)


@pytest.fixture(autouse=True)
def clean_observability_env(monkeypatch):
    """Every test starts from a clean slate regardless of the host shell's env."""
    for name in ENV_VARS:
        monkeypatch.delenv(name, raising=False)
    yield


def test_defaults_when_env_unset():
    config = get_observability_config()

    assert config.enabled is False
    assert config.mode == "disabled"
    assert config.otlp_endpoint == "http://127.0.0.1:4318"
    assert config.signoz_ui_url == "http://127.0.0.1:8080"
    assert config.artifact_root == Path("~/.studybuddy/observability").expanduser()


def test_reads_all_env_vars(monkeypatch, tmp_path):
    monkeypatch.setenv("OTEL_ENABLED", "true")
    monkeypatch.setenv("OTEL_MODE", "local")
    monkeypatch.setenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://collector.internal:4318")
    monkeypatch.setenv("SIGNOZ_UI_URL", "http://signoz.internal:8080")
    monkeypatch.setenv("OBSERVABILITY_ARTIFACT_ROOT", str(tmp_path))

    config = get_observability_config()

    assert config.enabled is True
    assert config.mode == "local"
    assert config.otlp_endpoint == "http://collector.internal:4318"
    assert config.signoz_ui_url == "http://signoz.internal:8080"
    assert config.artifact_root == tmp_path


def test_demo_deployment_forces_observability_off(monkeypatch):
    monkeypatch.setenv("DEPLOYMENT_ENV", "demo")
    monkeypatch.setenv("OTEL_ENABLED", "true")
    monkeypatch.setenv("OTEL_MODE", "full")

    config = get_observability_config()

    assert config.enabled is False
    assert config.mode == "disabled"


def test_artifact_root_expands_user_home(monkeypatch):
    monkeypatch.setenv("OBSERVABILITY_ARTIFACT_ROOT", "~/.studybuddy/observability")

    config = get_observability_config()

    assert "~" not in str(config.artifact_root)
    assert config.artifact_root == Path("~/.studybuddy/observability").expanduser()


@pytest.mark.parametrize("raw,expected", [("true", True), ("1", True), ("yes", True), ("on", True), ("True", True)])
def test_otel_enabled_truthy_strings(monkeypatch, raw, expected):
    monkeypatch.setenv("OTEL_ENABLED", raw)
    assert get_observability_config().enabled is expected


@pytest.mark.parametrize("raw,expected", [("false", False), ("0", False), ("no", False), ("off", False), ("", False)])
def test_otel_enabled_falsy_strings(monkeypatch, raw, expected):
    monkeypatch.setenv("OTEL_ENABLED", raw)
    assert get_observability_config().enabled is expected


def test_invalid_mode_falls_back_to_disabled_in_ordinary_startup(monkeypatch):
    monkeypatch.setenv("OTEL_MODE", "bogus-mode")

    config = get_observability_config()

    assert config.mode == "disabled"


def test_invalid_mode_raises_descriptive_error_in_evaluation_run(monkeypatch):
    monkeypatch.setenv("OTEL_MODE", "bogus-mode")
    monkeypatch.setenv("EVALUATION_RUN", "true")

    with pytest.raises(ValueError, match="bogus-mode"):
        get_observability_config()


def test_missing_mode_defaults_to_disabled_in_evaluation_run(monkeypatch):
    # No OTEL_MODE set at all -> defaults to "disabled". A benchmark run
    # explicitly opting out of telemetry is a valid choice, not a
    # misconfiguration, so this must not raise.
    monkeypatch.setenv("EVALUATION_RUN", "true")

    config = get_observability_config()

    assert config.mode == "disabled"


def test_explicit_disabled_mode_does_not_raise_in_evaluation_run(monkeypatch):
    monkeypatch.setenv("OTEL_MODE", "disabled")
    monkeypatch.setenv("EVALUATION_RUN", "true")

    config = get_observability_config()

    assert config.mode == "disabled"


def test_bogus_mode_still_raises_in_evaluation_run(monkeypatch):
    monkeypatch.setenv("OTEL_MODE", "bogus-garbage")
    monkeypatch.setenv("EVALUATION_RUN", "true")

    with pytest.raises(ValueError, match="bogus-garbage"):
        get_observability_config()


def test_valid_mode_with_evaluation_run_succeeds(monkeypatch):
    monkeypatch.setenv("OTEL_MODE", "full")
    monkeypatch.setenv("EVALUATION_RUN", "true")

    config = get_observability_config()

    assert config.mode == "full"


def test_local_mode_with_evaluation_run_succeeds(monkeypatch):
    monkeypatch.setenv("OTEL_MODE", "local")
    monkeypatch.setenv("EVALUATION_RUN", "true")

    config = get_observability_config()

    assert config.mode == "local"