Spaces:
Sleeping
Sleeping
File size: 9,871 Bytes
116524e | 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | """Tests for the ace.tracing wrapper."""
from __future__ import annotations
import os
from unittest.mock import patch
import pytest
@pytest.mark.unit
class TestConfigure:
"""Tests for ace.tracing.configure()."""
def test_configure_sets_tracking_uri_and_token(self) -> None:
with patch.dict(os.environ, {}, clear=False):
from kayba_tracing._wrapper import configure
with patch("kayba_tracing._wrapper.mlflow") as mock_mlflow:
configure(api_key="kb-test-key")
mock_mlflow.set_tracking_uri.assert_called_once_with(
"https://use.kayba.ai/api/mlflow"
)
assert os.environ["MLFLOW_TRACKING_TOKEN"] == "kb-test-key"
def test_configure_custom_base_url(self) -> None:
with patch.dict(os.environ, {}, clear=False):
from kayba_tracing._wrapper import configure
with patch("kayba_tracing._wrapper.mlflow") as mock_mlflow:
configure(
api_key="kb-test-key",
base_url="https://custom.example.com",
)
mock_mlflow.set_tracking_uri.assert_called_once_with(
"https://custom.example.com/api/mlflow"
)
def test_configure_strips_trailing_slash(self) -> None:
with patch.dict(os.environ, {}, clear=False):
from kayba_tracing._wrapper import configure
with patch("kayba_tracing._wrapper.mlflow") as mock_mlflow:
configure(
api_key="kb-test-key",
base_url="https://custom.example.com/",
)
mock_mlflow.set_tracking_uri.assert_called_once_with(
"https://custom.example.com/api/mlflow"
)
def test_configure_reads_api_key_from_env(self) -> None:
with patch.dict(os.environ, {"KAYBA_API_KEY": "kb-env-key"}, clear=False):
from kayba_tracing._wrapper import configure
with patch("kayba_tracing._wrapper.mlflow"):
configure()
assert os.environ["MLFLOW_TRACKING_TOKEN"] == "kb-env-key"
def test_configure_reads_base_url_from_env(self) -> None:
with patch.dict(
os.environ,
{
"KAYBA_API_KEY": "kb-key",
"KAYBA_API_URL": "https://env.example.com",
},
clear=False,
):
from kayba_tracing._wrapper import configure
with patch("kayba_tracing._wrapper.mlflow") as mock_mlflow:
configure()
mock_mlflow.set_tracking_uri.assert_called_once_with(
"https://env.example.com/api/mlflow"
)
def test_configure_raises_without_api_key(self) -> None:
with patch.dict(os.environ, {"KAYBA_API_KEY": ""}, clear=False):
from kayba_tracing._wrapper import configure
with pytest.raises(ValueError, match="No API key provided"):
configure()
def test_experiment_is_alias_for_folder(self) -> None:
import kayba_tracing._wrapper as w
with patch.dict(os.environ, {}, clear=False):
with patch("kayba_tracing._wrapper.mlflow"):
w.configure(api_key="kb-key", experiment="my-project")
assert w._folder == "my-project"
def test_folder_takes_precedence_over_experiment(self) -> None:
import kayba_tracing._wrapper as w
with patch.dict(os.environ, {}, clear=False):
with patch("kayba_tracing._wrapper.mlflow"):
w.configure(
api_key="kb-key",
experiment="from-experiment",
folder="from-folder",
)
assert w._folder == "from-folder"
def test_configure_sets_folder(self) -> None:
import kayba_tracing._wrapper as w
with patch.dict(os.environ, {}, clear=False):
with patch("kayba_tracing._wrapper.mlflow"):
w.configure(api_key="kb-key", folder="my-folder")
assert w._folder == "my-folder"
def test_configure_clears_folder_when_none(self) -> None:
import kayba_tracing._wrapper as w
with patch.dict(os.environ, {}, clear=False):
with patch("kayba_tracing._wrapper.mlflow"):
w.configure(api_key="kb-key", folder="old")
w.configure(api_key="kb-key")
assert w._folder is None
@pytest.mark.unit
class TestSanitizeFolder:
"""Tests for folder name sanitization."""
def test_strips_html_tags(self) -> None:
from kayba_tracing._wrapper import _sanitize_folder
assert _sanitize_folder('<script>alert("xss")</script>') == "alertxss"
def test_strips_control_characters(self) -> None:
from kayba_tracing._wrapper import _sanitize_folder
assert _sanitize_folder("folder\x00\x1f\nname") == "foldername"
def test_allows_safe_characters(self) -> None:
from kayba_tracing._wrapper import _sanitize_folder
assert _sanitize_folder("my-folder/sub_dir 2.0") == "my-folder/sub_dir 2.0"
def test_truncates_long_names(self) -> None:
from kayba_tracing._wrapper import _sanitize_folder
assert len(_sanitize_folder("a" * 500)) == 256
def test_strips_sql_injection_chars(self) -> None:
from kayba_tracing._wrapper import _sanitize_folder
assert _sanitize_folder("folder'; DROP TABLE--") == "folder DROP TABLE--"
def test_configure_sanitizes_folder(self) -> None:
import kayba_tracing._wrapper as w
with patch.dict(os.environ, {}, clear=False):
with patch("kayba_tracing._wrapper.mlflow"):
w.configure(api_key="kb-key", folder='<img onerror="xss">')
# Entire input is an HTML tag, stripped to empty string
assert w._folder is None
def test_set_folder_sanitizes(self) -> None:
import kayba_tracing._wrapper as w
w.set_folder("<b>bold</b>")
assert w.get_folder() == "bold"
@pytest.mark.unit
class TestFolder:
"""Tests for set_folder / get_folder."""
def test_set_and_get_folder(self) -> None:
import kayba_tracing._wrapper as w
w.set_folder("production")
assert w.get_folder() == "production"
def test_clear_folder(self) -> None:
import kayba_tracing._wrapper as w
w.set_folder("production")
w.set_folder(None)
assert w.get_folder() is None
def test_inject_folder_tag(self) -> None:
import kayba_tracing._wrapper as w
w._folder = "my-folder"
with patch("kayba_tracing._wrapper.mlflow") as mock_mlflow:
w._inject_folder_tag()
mock_mlflow.update_current_trace.assert_called_once_with(
tags={"kayba.folder": "my-folder"}
)
def test_inject_folder_tag_noop_when_none(self) -> None:
import kayba_tracing._wrapper as w
w._folder = None
with patch("kayba_tracing._wrapper.mlflow") as mock_mlflow:
w._inject_folder_tag()
mock_mlflow.update_current_trace.assert_not_called()
@pytest.mark.unit
class TestTraceDecorator:
"""Tests for the trace decorator wrapper."""
def test_trace_wraps_function(self) -> None:
import kayba_tracing._wrapper as w
w._folder = "test-folder"
with patch("kayba_tracing._wrapper.mlflow") as mock_mlflow:
# Make mlflow.trace return a passthrough decorator
mock_mlflow.trace.side_effect = lambda fn=None, **kw: (
fn if fn is not None else (lambda f: f)
)
@w.trace
def my_func(x: int) -> int:
return x + 1
result = my_func(5)
assert result == 6
mock_mlflow.update_current_trace.assert_called_with(
tags={"kayba.folder": "test-folder"}
)
def test_trace_with_params(self) -> None:
import kayba_tracing._wrapper as w
w._folder = None
with patch("kayba_tracing._wrapper.mlflow") as mock_mlflow:
mock_mlflow.trace.return_value = lambda fn: fn
@w.trace(name="custom", span_type="LLM")
def my_func() -> str:
return "ok"
result = my_func()
assert result == "ok"
mock_mlflow.trace.assert_called_once_with(name="custom", span_type="LLM")
# No folder set, so no tag injection
mock_mlflow.update_current_trace.assert_not_called()
@pytest.mark.unit
class TestReExports:
"""Verify utility re-exports."""
def test_enable_calls_mlflow(self) -> None:
from kayba_tracing._wrapper import enable
with patch("kayba_tracing._wrapper.mlflow.tracing.enable") as mock:
enable()
mock.assert_called_once()
def test_disable_calls_mlflow(self) -> None:
from kayba_tracing._wrapper import disable
with patch("kayba_tracing._wrapper.mlflow.tracing.disable") as mock:
disable()
mock.assert_called_once()
@pytest.mark.unit
class TestPackageInit:
"""Verify the public __init__ exports."""
def test_all_exports(self) -> None:
import ace.tracing
expected = {
"configure",
"disable",
"enable",
"get_folder",
"get_trace",
"search_traces",
"set_folder",
"start_span",
"trace",
}
assert set(ace.tracing.__all__) == expected
|