|
1 | 1 | """Tests for SDK-local ingestion models (Logged variants and content blocks).""" |
2 | 2 |
|
3 | | -import builtins |
4 | | -import importlib |
5 | | - |
6 | 3 | import pytest |
7 | 4 | from pydantic import ValidationError |
8 | 5 |
|
9 | | -import splunk_ao.logger.control as control_module |
10 | | -import splunk_ao.logger.logger as logger_module |
11 | | -import splunk_ao.schema.logged as logged_module |
12 | 6 | from galileo_core.schemas.logging.llm import MessageRole |
13 | 7 | from galileo_core.schemas.logging.span import AgentSpan, LlmSpan, RetrieverSpan, ToolSpan, WorkflowSpan |
14 | 8 | from galileo_core.schemas.logging.trace import Trace |
@@ -341,76 +335,20 @@ def test_full_ingest_request_roundtrip(self) -> None: |
341 | 335 | assert type(tool) is ToolSpan |
342 | 336 | assert tool.output == "4" |
343 | 337 |
|
344 | | - def test_logged_trace_roundtrip_with_fallback_control_span(self, monkeypatch: pytest.MonkeyPatch) -> None: |
345 | | - # Given: splunk_ao.logger.control is reloaded without native ControlSpan support |
346 | | - if not control_module.HAS_NATIVE_CONTROL_SPAN: |
347 | | - control_payload = control_module.ControlSpan(input="selected text").model_dump(mode="python") |
348 | | - trace = logged_module.LoggedTrace(input="query", spans=[control_payload]) |
349 | | - restored = logged_module.LoggedTrace.model_validate(trace.model_dump(mode="json")) |
350 | | - |
351 | | - assert restored.spans[0].type == "control" |
352 | | - assert restored.spans[0].__class__.__name__ == "LoggedControlSpan" |
353 | | - assert restored.spans[0].model_dump(mode="json")["input"] == "selected text" |
354 | | - return |
355 | | - |
356 | | - original_import = builtins.__import__ |
357 | | - |
358 | | - def force_fallback_import(name, globals=None, locals=None, fromlist=(), level=0): |
359 | | - if name == "galileo_core.schemas.logging.control": |
360 | | - raise ImportError("forced fallback for test") |
361 | | - return original_import(name, globals, locals, fromlist, level) |
362 | | - |
363 | | - try: |
364 | | - with monkeypatch.context() as context: |
365 | | - context.setattr(builtins, "__import__", force_fallback_import) |
366 | | - importlib.reload(control_module) |
367 | | - importlib.reload(logged_module) |
368 | | - |
369 | | - # When: constructing and validating a LoggedTrace containing the fallback ControlSpan payload |
370 | | - control_payload = control_module.ControlSpan(input="selected text").model_dump(mode="python") |
371 | | - trace = logged_module.LoggedTrace(input="query", spans=[control_payload]) |
372 | | - restored = logged_module.LoggedTrace.model_validate(trace.model_dump(mode="json")) |
373 | | - |
374 | | - # Then: the discriminated union resolves the fallback ControlSpan cleanly |
375 | | - assert control_module.HAS_NATIVE_CONTROL_SPAN is False |
376 | | - assert restored.spans[0].type == "control" |
377 | | - assert restored.spans[0].__class__.__name__ == "LoggedControlSpan" |
378 | | - assert restored.spans[0].model_dump(mode="json")["input"] == "selected text" |
379 | | - finally: |
380 | | - importlib.reload(control_module) |
381 | | - importlib.reload(logged_module) |
382 | | - logger_module.LoggedControlSpan = logged_module.LoggedControlSpan |
| 338 | + def test_logged_trace_roundtrip_with_control_span(self) -> None: |
| 339 | + from splunk_ao.logger.control import ControlSpan |
| 340 | + control_payload = ControlSpan(input="selected text").model_dump(mode="python") |
| 341 | + trace = LoggedTrace(input="query", spans=[control_payload]) |
| 342 | + restored = LoggedTrace.model_validate(trace.model_dump(mode="json")) |
| 343 | + assert restored.spans[0].type == "control" |
| 344 | + assert type(restored.spans[0]) is LoggedControlSpan |
| 345 | + assert restored.spans[0].model_dump(mode="json")["input"] == "selected text" |
383 | 346 |
|
384 | 347 | @pytest.mark.parametrize("field_name", ["id", "session_id", "trace_id", "parent_id"]) |
385 | | - def test_fallback_control_span_rejects_non_uuidish_id_fields( |
386 | | - self, field_name: str, monkeypatch: pytest.MonkeyPatch |
387 | | - ) -> None: |
388 | | - # Given: splunk_ao.logger.control is reloaded without native ControlSpan support |
389 | | - if not control_module.HAS_NATIVE_CONTROL_SPAN: |
390 | | - with pytest.raises(ValidationError): |
391 | | - control_module.ControlSpan(input="selected text", **{field_name: 123}) |
392 | | - return |
393 | | - |
394 | | - original_import = builtins.__import__ |
395 | | - |
396 | | - def force_fallback_import(name, globals=None, locals=None, fromlist=(), level=0): |
397 | | - if name == "galileo_core.schemas.logging.control": |
398 | | - raise ImportError("forced fallback for test") |
399 | | - return original_import(name, globals, locals, fromlist, level) |
400 | | - |
401 | | - try: |
402 | | - with monkeypatch.context() as context: |
403 | | - context.setattr(builtins, "__import__", force_fallback_import) |
404 | | - importlib.reload(control_module) |
405 | | - |
406 | | - # When/Then: non-UUID-ish id fields are rejected by the fallback schema |
407 | | - assert control_module.HAS_NATIVE_CONTROL_SPAN is False |
408 | | - with pytest.raises(ValidationError): |
409 | | - control_module.ControlSpan(input="selected text", **{field_name: 123}) |
410 | | - finally: |
411 | | - importlib.reload(control_module) |
412 | | - importlib.reload(logged_module) |
413 | | - logger_module.LoggedControlSpan = logged_module.LoggedControlSpan |
| 348 | + def test_control_span_rejects_non_uuidish_id_fields(self, field_name: str) -> None: |
| 349 | + from splunk_ao.logger.control import ControlSpan |
| 350 | + with pytest.raises(ValidationError): |
| 351 | + ControlSpan(input="selected text", **{field_name: 123}) |
414 | 352 |
|
415 | 353 |
|
416 | 354 | class TestLoggedAndCoreParity: |
|
0 commit comments