Merge pull request #792 from christianlouis/copilot/enable-sentry-client-side
feat(ui): Add Sentry Browser SDK for client-side error tracking
This commit is contained in:
@@ -210,6 +210,141 @@ class TestGetAppVersion:
|
||||
assert _get_app_version() is None
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestSentryJsTemplateContext:
|
||||
"""Test that Sentry Browser SDK config is injected into the template context."""
|
||||
|
||||
def test_sentry_dsn_exposed_when_configured(self, mocker):
|
||||
"""sentry_dsn is set in the template context when SENTRY_DSN is configured."""
|
||||
mock_settings = mocker.patch("app.views.base.settings")
|
||||
mock_settings.sentry_dsn = "https://key@o1.ingest.sentry.io/1"
|
||||
mock_settings.sentry_environment = "production"
|
||||
mock_settings.sentry_js_traces_sample_rate = 0.1
|
||||
mock_settings.sentry_js_replay_session_sample_rate = 0.0
|
||||
mock_settings.sentry_js_replay_on_error_sample_rate = 0.1
|
||||
|
||||
from app.views.base import _inject_global_context
|
||||
|
||||
ctx: dict = {}
|
||||
_inject_global_context(ctx)
|
||||
|
||||
assert ctx["sentry_dsn"] == "https://key@o1.ingest.sentry.io/1"
|
||||
|
||||
def test_sentry_dsn_none_when_not_configured(self, mocker):
|
||||
"""sentry_dsn is None in the template context when SENTRY_DSN is unset."""
|
||||
mock_settings = mocker.patch("app.views.base.settings")
|
||||
mock_settings.sentry_dsn = None
|
||||
mock_settings.sentry_environment = "production"
|
||||
mock_settings.sentry_js_traces_sample_rate = 0.0
|
||||
mock_settings.sentry_js_replay_session_sample_rate = 0.0
|
||||
mock_settings.sentry_js_replay_on_error_sample_rate = 0.1
|
||||
|
||||
from app.views.base import _inject_global_context
|
||||
|
||||
ctx: dict = {}
|
||||
_inject_global_context(ctx)
|
||||
|
||||
assert ctx["sentry_dsn"] is None
|
||||
|
||||
def test_sentry_dsn_empty_string_becomes_none(self, mocker):
|
||||
"""An empty-string SENTRY_DSN is normalised to None in the template context."""
|
||||
mock_settings = mocker.patch("app.views.base.settings")
|
||||
mock_settings.sentry_dsn = ""
|
||||
mock_settings.sentry_environment = "production"
|
||||
mock_settings.sentry_js_traces_sample_rate = 0.0
|
||||
mock_settings.sentry_js_replay_session_sample_rate = 0.0
|
||||
mock_settings.sentry_js_replay_on_error_sample_rate = 0.1
|
||||
|
||||
from app.views.base import _inject_global_context
|
||||
|
||||
ctx: dict = {}
|
||||
_inject_global_context(ctx)
|
||||
|
||||
assert ctx["sentry_dsn"] is None
|
||||
|
||||
def test_js_sample_rates_exposed_in_context(self, mocker):
|
||||
"""Browser SDK sample rates are passed through to the template context."""
|
||||
mock_settings = mocker.patch("app.views.base.settings")
|
||||
mock_settings.sentry_dsn = "https://key@o1.ingest.sentry.io/1"
|
||||
mock_settings.sentry_environment = "staging"
|
||||
mock_settings.sentry_js_traces_sample_rate = 0.5
|
||||
mock_settings.sentry_js_replay_session_sample_rate = 0.2
|
||||
mock_settings.sentry_js_replay_on_error_sample_rate = 0.8
|
||||
|
||||
from app.views.base import _inject_global_context
|
||||
|
||||
ctx: dict = {}
|
||||
_inject_global_context(ctx)
|
||||
|
||||
assert ctx["sentry_environment"] == "staging"
|
||||
assert ctx["sentry_js_traces_sample_rate"] == 0.5
|
||||
assert ctx["sentry_js_replay_session_sample_rate"] == 0.2
|
||||
assert ctx["sentry_js_replay_on_error_sample_rate"] == 0.8
|
||||
|
||||
def test_js_sample_rates_default_values(self, mocker):
|
||||
"""Browser SDK sample rates fall back to safe defaults when attrs are absent."""
|
||||
mock_settings = mocker.patch("app.views.base.settings")
|
||||
# Simulate settings object without the new JS attributes
|
||||
del mock_settings.sentry_js_traces_sample_rate
|
||||
del mock_settings.sentry_js_replay_session_sample_rate
|
||||
del mock_settings.sentry_js_replay_on_error_sample_rate
|
||||
mock_settings.sentry_dsn = None
|
||||
mock_settings.sentry_environment = "production"
|
||||
|
||||
from app.views.base import _inject_global_context
|
||||
|
||||
ctx: dict = {}
|
||||
_inject_global_context(ctx)
|
||||
|
||||
assert ctx["sentry_js_traces_sample_rate"] == 0.0
|
||||
assert ctx["sentry_js_replay_session_sample_rate"] == 0.0
|
||||
assert ctx["sentry_js_replay_on_error_sample_rate"] == 0.1
|
||||
|
||||
|
||||
_MINIMAL_SETTINGS_KWARGS = {
|
||||
"database_url": "sqlite:///./test.db",
|
||||
"redis_url": "redis://localhost:6379",
|
||||
"openai_api_key": "test",
|
||||
"azure_ai_key": "test",
|
||||
"azure_region": "test",
|
||||
"azure_endpoint": "https://test.example.com",
|
||||
"gotenberg_url": "http://localhost:3000",
|
||||
"workdir": "/tmp",
|
||||
"auth_enabled": False,
|
||||
"session_secret": "a-test-secret-that-is-at-least-32-chars-long!",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestSentryJsConfig:
|
||||
"""Test the new JS-specific Settings fields."""
|
||||
|
||||
def test_js_traces_sample_rate_default(self):
|
||||
"""SENTRY_JS_TRACES_SAMPLE_RATE defaults to 0.0."""
|
||||
from app.config import settings
|
||||
|
||||
assert settings.sentry_js_traces_sample_rate == 0.0
|
||||
|
||||
def test_js_replay_session_sample_rate_default(self):
|
||||
"""SENTRY_JS_REPLAY_SESSION_SAMPLE_RATE defaults to 0.0."""
|
||||
from app.config import settings
|
||||
|
||||
assert settings.sentry_js_replay_session_sample_rate == 0.0
|
||||
|
||||
def test_js_replay_on_error_sample_rate_default(self):
|
||||
"""SENTRY_JS_REPLAY_ON_ERROR_SAMPLE_RATE defaults to 0.1."""
|
||||
from app.config import settings
|
||||
|
||||
assert settings.sentry_js_replay_on_error_sample_rate == 0.1
|
||||
|
||||
def test_js_traces_sample_rate_can_be_set(self):
|
||||
"""SENTRY_JS_TRACES_SAMPLE_RATE can be set directly via constructor."""
|
||||
from app.config import Settings
|
||||
|
||||
s = Settings(**_MINIMAL_SETTINGS_KWARGS, sentry_js_traces_sample_rate=0.5)
|
||||
assert s.sentry_js_traces_sample_rate == 0.5
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user