Cubase-MCP-GAJ3T.MusicGen.R.../tests/unit/test_errors.py

89 lines
3.1 KiB
Python

"""Stable, deliberately public failure information."""
import json
import math
import pytest
from pydantic import ValidationError
from riff_groove_builder.errors import (
DomainError,
InvalidRequestError,
NativeFormatUnavailableError,
OutputExistsError,
OutputIOError,
ProfileNotFoundError,
ProfileRequiredError,
SerializationError,
UnsupportedArticulationError,
UnsupportedDescriptionError,
)
from riff_groove_builder.schemas import GenerationError
@pytest.mark.parametrize(
("error_type", "code", "action"),
[
(InvalidRequestError, "INVALID_REQUEST", "Correct"),
(UnsupportedDescriptionError, "UNSUPPORTED_DESCRIPTION", "supported"),
(ProfileRequiredError, "PROFILE_REQUIRED", "Select"),
(ProfileNotFoundError, "PROFILE_REQUIRED", "list profiles"),
(UnsupportedArticulationError, "UNSUPPORTED_ARTICULATION", "Choose"),
(NativeFormatUnavailableError, "NATIVE_FORMAT_UNAVAILABLE", "fallback"),
(OutputExistsError, "OUTPUT_EXISTS", "basename"),
(OutputIOError, "OUTPUT_IO_ERROR", "permissions"),
(SerializationError, "SERIALIZATION_ERROR", "profile"),
],
)
def test_stable_public_domain_errors(error_type: type[DomainError], code: str, action: str) -> None:
error = error_type(details={"part": "lead", "choices": ["synthetic-only"]})
assert isinstance(error, DomainError)
assert error.code == code
assert action in str(error)
result = GenerationError.from_domain(error)
assert result.code == code
assert result.details == {"part": "lead", "choices": ["synthetic-only"]}
assert GenerationError.model_validate_json(result.model_dump_json()) == result
def test_private_exception_cause_is_never_automatically_exposed() -> None:
try:
try:
raise OSError(r"C:\private\licensed-content: confidential")
except OSError as cause:
raise OutputIOError(details={"operation": "publish"}) from cause
except OutputIOError as error:
payload = GenerationError.from_domain(error).model_dump_json()
assert "private" not in payload
assert "confidential" not in payload
assert "Traceback" not in payload
assert json.loads(payload)["details"] == {"operation": "publish"}
assert InvalidRequestError().details == {}
@pytest.mark.parametrize("value", [math.nan, math.inf, -math.inf])
def test_error_details_require_finite_json(value: float) -> None:
with pytest.raises(ValidationError):
InvalidRequestError(details={"value": value})
with pytest.raises(ValidationError):
GenerationError(
code="INVALID_REQUEST", message="Correct the field.", details={"value": value}
)
@pytest.mark.parametrize(
"updates",
[
{"code": "UNEXPECTED"},
{"message": ""},
{"message": " "},
{"details": []},
{"traceback": "private"},
],
)
def test_malformed_public_errors(updates: dict[str, object]) -> None:
with pytest.raises(ValidationError):
GenerationError.model_validate(
{"code": "INVALID_REQUEST", "message": "Correct the field.", "details": {}, **updates}
)