|
| 1 | +from types import SimpleNamespace |
| 2 | +from unittest.mock import AsyncMock, MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from wildedge.integrations.openai import wrap_async_embeddings, wrap_sync_embeddings |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize("asynchronous", [False, True]) |
| 10 | +@pytest.mark.parametrize( |
| 11 | + ("value", "batch_size", "chars"), |
| 12 | + [ |
| 13 | + ("hello", 1, 5), |
| 14 | + (["hello", "world"], 2, 10), |
| 15 | + ([1, 2, 3], 1, None), |
| 16 | + ([[1, 2], [3]], 2, None), |
| 17 | + ], |
| 18 | +) |
| 19 | +async def test_metadata_and_passthrough(asynchronous, value, batch_size, chars): |
| 20 | + result = SimpleNamespace( |
| 21 | + model="resolved-model", |
| 22 | + usage=SimpleNamespace(prompt_tokens=7), |
| 23 | + data=[SimpleNamespace(embedding=[0.1, 0.2, 0.3])], |
| 24 | + ) |
| 25 | + resource = SimpleNamespace( |
| 26 | + create=(AsyncMock if asynchronous else MagicMock)(return_value=result) |
| 27 | + ) |
| 28 | + handle = MagicMock() |
| 29 | + client = SimpleNamespace( |
| 30 | + closed=False, register_model=MagicMock(return_value=handle) |
| 31 | + ) |
| 32 | + (wrap_async_embeddings if asynchronous else wrap_sync_embeddings)( |
| 33 | + resource, "openai", lambda: client |
| 34 | + ) |
| 35 | + call = resource.create(model="model", input=value) |
| 36 | + assert (await call if asynchronous else call) is result |
| 37 | + handle.track_inference.assert_called_once() |
| 38 | + event = handle.track_inference.call_args.kwargs |
| 39 | + assert event["batch_size"] == batch_size |
| 40 | + assert event["input_meta"].token_count == 7 |
| 41 | + assert event["input_meta"].char_count == chars |
| 42 | + assert event["output_meta"].tokens_in == 7 |
| 43 | + assert event["output_meta"].dimensions == 3 |
| 44 | + assert event["api_meta"].resolved_model_id == "resolved-model" |
| 45 | + assert "hello" not in str(event) |
| 46 | + client.closed = True |
| 47 | + call = resource.create(model="model", input=value) |
| 48 | + assert (await call if asynchronous else call) is result |
| 49 | + assert handle.track_inference.call_count == 1 |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("asynchronous", [False, True]) |
| 53 | +async def test_error_and_telemetry_failure(asynchronous): |
| 54 | + original = (AsyncMock if asynchronous else MagicMock)( |
| 55 | + side_effect=TimeoutError("timeout") |
| 56 | + ) |
| 57 | + resource = SimpleNamespace(create=original) |
| 58 | + handle = MagicMock() |
| 59 | + client = SimpleNamespace( |
| 60 | + closed=False, register_model=MagicMock(return_value=handle) |
| 61 | + ) |
| 62 | + (wrap_async_embeddings if asynchronous else wrap_sync_embeddings)( |
| 63 | + resource, "openai", lambda: client |
| 64 | + ) |
| 65 | + |
| 66 | + async def invoke(): |
| 67 | + call = resource.create(model="model", input="hello") |
| 68 | + return await call if asynchronous else call |
| 69 | + |
| 70 | + with pytest.raises(TimeoutError, match="timeout"): |
| 71 | + await invoke() |
| 72 | + handle.track_error.assert_called_once() |
| 73 | + handle.track_inference.assert_not_called() |
| 74 | + result = SimpleNamespace(model="model", usage=None, data=[]) |
| 75 | + original.side_effect = None |
| 76 | + original.return_value = result |
| 77 | + handle.track_inference.side_effect = RuntimeError("telemetry broken") |
| 78 | + assert await invoke() is result |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("asynchronous", [False, True]) |
| 82 | +async def test_input_iterator_is_not_consumed(asynchronous): |
| 83 | + def values(): |
| 84 | + raise AssertionError("instrumentation must not iterate inputs") |
| 85 | + yield "unreachable" |
| 86 | + |
| 87 | + result = SimpleNamespace(model="model", usage=None, data=[]) |
| 88 | + resource = SimpleNamespace( |
| 89 | + create=(AsyncMock if asynchronous else MagicMock)(return_value=result) |
| 90 | + ) |
| 91 | + handle = MagicMock() |
| 92 | + client = SimpleNamespace( |
| 93 | + closed=False, register_model=MagicMock(return_value=handle) |
| 94 | + ) |
| 95 | + (wrap_async_embeddings if asynchronous else wrap_sync_embeddings)( |
| 96 | + resource, "openai", lambda: client |
| 97 | + ) |
| 98 | + call = resource.create(model="model", input=values()) |
| 99 | + assert (await call if asynchronous else call) is result |
| 100 | + event = handle.track_inference.call_args.kwargs |
| 101 | + assert event["batch_size"] is None |
| 102 | + assert event["output_meta"].dimensions is None |
0 commit comments