Files
chemenu-ng/tests/ports/test_llm_port.py
torbenandClaude Sonnet 5.5 140014f8bb Add StepProfile and LLM port with errors
Domain enum for step profiles and the LlmPort protocol with request,
response, usage and the LlmError hierarchy. Step names are validated
because they later become part of file paths.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-04 22:06:58 +01:00

54 lines
1.5 KiB
Python

import pytest
from pydantic import BaseModel
from chemenu.domain.step_profile import StepProfile
from chemenu.ports import llm
from chemenu.ports.llm import LlmError, LlmRequest, Usage
class _Out(BaseModel):
x: int
def _request(step: str) -> LlmRequest:
return LlmRequest(step=step, profile=StepProfile.ANALYSE, prompt="p", output_model=_Out)
@pytest.mark.parametrize("step", ["", "Note", "note_inventory", "../x", "a/b", "note-inventory\n"])
def test_invalid_step_name_is_rejected(step: str) -> None:
with pytest.raises(ValueError, match="invalid step name"):
_request(step)
def test_valid_step_name_is_accepted() -> None:
assert _request("note-inventory2").step == "note-inventory2"
def test_error_carries_usage() -> None:
usage = Usage(input_tokens=3, output_tokens=4)
error = llm.TruncatedError("cut off", usage)
assert error.usage == usage
assert str(error) == "cut off"
assert LlmError("plain").usage is None
assert usage.cache_read_tokens == 0
assert usage.cache_write_tokens == 0
@pytest.mark.parametrize(
"name",
[
"ProfileNotConfiguredError",
"MissingApiKeyError",
"MissingRecordingError",
"RefusedError",
"TruncatedError",
"ModelMismatchError",
"LlmTimeoutError",
"ProviderError",
],
)
def test_all_errors_are_llm_errors(name: str) -> None:
cls = getattr(llm, name)
assert issubclass(cls, LlmError)
assert str(cls("msg")) == "msg"