|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from types import SimpleNamespace |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +import wildedge |
| 9 | +from wildedge import constants |
| 10 | +from wildedge.defaults import set_default_client |
| 11 | + |
| 12 | + |
| 13 | +class FakeHandle: |
| 14 | + def __init__(self): |
| 15 | + self.inferences: list[dict] = [] |
| 16 | + self.errors: list[dict] = [] |
| 17 | + |
| 18 | + def track_inference(self, **kwargs): |
| 19 | + self.inferences.append(kwargs) |
| 20 | + |
| 21 | + def track_error(self, error_code=None, *, error_message=None, **kwargs): |
| 22 | + self.errors.append({"error_code": error_code, "error_message": error_message}) |
| 23 | + |
| 24 | + |
| 25 | +class FakeClient: |
| 26 | + def __init__(self): |
| 27 | + self.handle = FakeHandle() |
| 28 | + self.registered: list[dict] = [] |
| 29 | + |
| 30 | + def register_model(self, model_obj, **kwargs): |
| 31 | + self.registered.append({"model_obj": model_obj, **kwargs}) |
| 32 | + return self.handle |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def client(): |
| 37 | + fake = FakeClient() |
| 38 | + set_default_client(fake) |
| 39 | + return fake |
| 40 | + |
| 41 | + |
| 42 | +def test_openai_shape_usage(client): |
| 43 | + with wildedge.llm_api(model="openai/gpt-4o-mini", provider="openrouter") as call: |
| 44 | + call.usage( |
| 45 | + { |
| 46 | + "prompt_tokens": 100, |
| 47 | + "completion_tokens": 50, |
| 48 | + "prompt_tokens_details": {"cached_tokens": 25}, |
| 49 | + "completion_tokens_details": {"reasoning_tokens": 10}, |
| 50 | + } |
| 51 | + ) |
| 52 | + call.stop_reason = "stop" |
| 53 | + |
| 54 | + assert client.registered[0]["model_id"] == "openai/gpt-4o-mini" |
| 55 | + assert client.registered[0]["source"] == "openrouter" |
| 56 | + event = client.handle.inferences[0] |
| 57 | + assert event["input_modality"] == "text" |
| 58 | + assert event["output_modality"] == "generation" |
| 59 | + assert event["success"] is True |
| 60 | + meta = event["output_meta"] |
| 61 | + assert meta.tokens_in == 100 |
| 62 | + assert meta.tokens_out == 50 |
| 63 | + assert meta.cached_input_tokens == 25 |
| 64 | + assert meta.reasoning_tokens_out == 10 |
| 65 | + assert meta.stop_reason == "stop" |
| 66 | + |
| 67 | + |
| 68 | +def test_anthropic_shape_usage(client): |
| 69 | + with wildedge.llm_api(model="claude-sonnet-4-5", provider="anthropic") as call: |
| 70 | + call.usage( |
| 71 | + {"input_tokens": 10, "output_tokens": 5, "cache_read_input_tokens": 3} |
| 72 | + ) |
| 73 | + |
| 74 | + meta = client.handle.inferences[0]["output_meta"] |
| 75 | + assert meta.tokens_in == 10 |
| 76 | + assert meta.tokens_out == 5 |
| 77 | + assert meta.cached_input_tokens == 3 |
| 78 | + |
| 79 | + |
| 80 | +def test_attribute_object_usage(client): |
| 81 | + usage = SimpleNamespace( |
| 82 | + prompt_tokens=7, |
| 83 | + completion_tokens=3, |
| 84 | + prompt_tokens_details=SimpleNamespace(cached_tokens=2), |
| 85 | + completion_tokens_details=None, |
| 86 | + ) |
| 87 | + with wildedge.llm_api(model="m", provider="p") as call: |
| 88 | + call.usage(usage) |
| 89 | + |
| 90 | + meta = client.handle.inferences[0]["output_meta"] |
| 91 | + assert meta.tokens_in == 7 |
| 92 | + assert meta.tokens_out == 3 |
| 93 | + assert meta.cached_input_tokens == 2 |
| 94 | + |
| 95 | + |
| 96 | +def test_explicit_field_usage_wins_over_payload(client): |
| 97 | + with wildedge.llm_api(model="m", provider="p") as call: |
| 98 | + call.usage({"prompt_tokens": 1}, tokens_in=11, tokens_out=22) |
| 99 | + |
| 100 | + meta = client.handle.inferences[0]["output_meta"] |
| 101 | + assert meta.tokens_in == 11 |
| 102 | + assert meta.tokens_out == 22 |
| 103 | + |
| 104 | + |
| 105 | +def test_response_openai_shape(client): |
| 106 | + with wildedge.llm_api(model="m", provider="p") as call: |
| 107 | + call.response( |
| 108 | + { |
| 109 | + "model": "gpt-4o-mini-2024-07-18", |
| 110 | + "system_fingerprint": "fp_1", |
| 111 | + "usage": {"prompt_tokens": 4, "completion_tokens": 2}, |
| 112 | + "choices": [{"finish_reason": "length", "message": {}}], |
| 113 | + } |
| 114 | + ) |
| 115 | + |
| 116 | + event = client.handle.inferences[0] |
| 117 | + assert event["output_meta"].stop_reason == "length" |
| 118 | + assert event["output_meta"].tokens_in == 4 |
| 119 | + assert event["api_meta"].resolved_model_id == "gpt-4o-mini-2024-07-18" |
| 120 | + assert event["api_meta"].system_fingerprint == "fp_1" |
| 121 | + |
| 122 | + |
| 123 | +def test_response_anthropic_shape(client): |
| 124 | + with wildedge.llm_api(model="m", provider="anthropic") as call: |
| 125 | + call.response( |
| 126 | + { |
| 127 | + "model": "claude-sonnet-4-5", |
| 128 | + "stop_reason": "end_turn", |
| 129 | + "usage": {"input_tokens": 9, "output_tokens": 1}, |
| 130 | + } |
| 131 | + ) |
| 132 | + |
| 133 | + meta = client.handle.inferences[0]["output_meta"] |
| 134 | + assert meta.stop_reason == "end_turn" |
| 135 | + assert meta.tokens_in == 9 |
| 136 | + |
| 137 | + |
| 138 | +def test_exception_records_error_not_inference(client): |
| 139 | + with pytest.raises(ValueError): |
| 140 | + with wildedge.llm_api(model="m", provider="p"): |
| 141 | + raise ValueError("bad payload") |
| 142 | + |
| 143 | + assert client.handle.inferences == [] |
| 144 | + assert client.handle.errors == [ |
| 145 | + {"error_code": "ValueError", "error_message": "bad payload"} |
| 146 | + ] |
| 147 | + |
| 148 | + |
| 149 | +def test_prompt_input_meta(client): |
| 150 | + with wildedge.llm_api( |
| 151 | + model="m", provider="p", prompt="a knight with two swords" |
| 152 | + ) as call: |
| 153 | + call.usage(tokens_in=6) |
| 154 | + |
| 155 | + meta = client.handle.inferences[0]["input_meta"] |
| 156 | + assert meta.char_count == len("a knight with two swords") |
| 157 | + assert meta.word_count == 5 |
| 158 | + assert meta.token_count == 6 |
| 159 | + assert meta.prompt_type == "chat" |
| 160 | + |
| 161 | + |
| 162 | +def test_messages_input_meta(client): |
| 163 | + messages = [ |
| 164 | + {"role": "system", "content": "be brief"}, |
| 165 | + {"role": "user", "content": "hello there"}, |
| 166 | + ] |
| 167 | + with wildedge.llm_api(model="m", provider="p", messages=messages) as call: |
| 168 | + call.usage(tokens_in=3) |
| 169 | + |
| 170 | + meta = client.handle.inferences[0]["input_meta"] |
| 171 | + assert meta.char_count == len("hello there") |
| 172 | + assert meta.word_count == 2 |
| 173 | + |
| 174 | + |
| 175 | +def test_first_token_sets_ttft(client): |
| 176 | + with wildedge.llm_api(model="m", provider="p") as call: |
| 177 | + call.first_token() |
| 178 | + call.usage(tokens_out=1) |
| 179 | + |
| 180 | + assert client.handle.inferences[0]["output_meta"].time_to_first_token_ms is not None |
| 181 | + |
| 182 | + |
| 183 | +def test_async_context_manager(client): |
| 184 | + async def run(): |
| 185 | + async with wildedge.llm_api(model="m", provider="p") as call: |
| 186 | + call.usage(tokens_in=1) |
| 187 | + |
| 188 | + asyncio.run(run()) |
| 189 | + assert len(client.handle.inferences) == 1 |
| 190 | + |
| 191 | + |
| 192 | +def test_source_derived_from_base_url(client): |
| 193 | + with wildedge.llm_api(model="m", base_url="https://openrouter.ai/api/v1"): |
| 194 | + pass |
| 195 | + |
| 196 | + assert client.registered[0]["source"] == "openrouter" |
| 197 | + |
| 198 | + |
| 199 | +def test_success_flag_recorded(client): |
| 200 | + with wildedge.llm_api(model="m", provider="p") as call: |
| 201 | + call.usage(tokens_out=1) |
| 202 | + call.success = False |
| 203 | + |
| 204 | + assert client.handle.inferences[0]["success"] is False |
| 205 | + |
| 206 | + |
| 207 | +def test_no_output_meta_when_nothing_known(client): |
| 208 | + with wildedge.llm_api(model="m", provider="p"): |
| 209 | + pass |
| 210 | + |
| 211 | + event = client.handle.inferences[0] |
| 212 | + assert event["output_meta"] is None |
| 213 | + assert event["api_meta"] is None |
| 214 | + |
| 215 | + |
| 216 | +def test_noop_without_dsn_end_to_end(monkeypatch): |
| 217 | + monkeypatch.delenv(constants.ENV_DSN, raising=False) |
| 218 | + monkeypatch.delenv(constants.ENV_INTEGRATIONS, raising=False) |
| 219 | + |
| 220 | + with wildedge.llm_api(model="m", provider="p") as call: |
| 221 | + call.usage(tokens_in=1, tokens_out=2) |
| 222 | + |
| 223 | + assert wildedge.get_client().noop is True |
0 commit comments