234 lines
8.7 KiB
Python
234 lines
8.7 KiB
Python
"""AGP capability contracts only; synthetic mapping data is not a native fixture."""
|
|
|
|
from collections.abc import Callable
|
|
from dataclasses import FrozenInstanceError, asdict
|
|
from pathlib import Path
|
|
from typing import cast
|
|
|
|
import pytest
|
|
|
|
from riff_groove_builder.errors import (
|
|
DomainError,
|
|
InvalidRequestError,
|
|
NativeFormatUnavailableError,
|
|
)
|
|
from riff_groove_builder.formats import NativeCapability
|
|
from riff_groove_builder.formats.agp import check_agp_capability, require_agp_capability
|
|
from riff_groove_builder.profiles import (
|
|
InstrumentProfile,
|
|
InvalidProfileError,
|
|
ProfilePartMismatchError,
|
|
load_profile_registry,
|
|
parse_profile,
|
|
)
|
|
from riff_groove_builder.schemas import Part, ProfileSelection
|
|
|
|
Boundary = Callable[[Part, InstrumentProfile], NativeCapability]
|
|
BOUNDARIES: list[Boundary] = [check_agp_capability, require_agp_capability]
|
|
FIXTURES = Path(__file__).resolve().parents[1] / "fixtures" / "profiles"
|
|
|
|
|
|
def standard(part: Part) -> InstrumentProfile:
|
|
identifier = "standard.gm-drums.v1" if part == "drums" else f"standard.{part}.v1"
|
|
return load_profile_registry().select([part], ProfileSelection(**{part: identifier}))[part]
|
|
|
|
|
|
@pytest.mark.parametrize("part", ["lead", "bass"])
|
|
def test_standard_profiles_return_explicit_typed_unavailable(part: Part) -> None:
|
|
profile = standard(part)
|
|
before = profile.model_dump()
|
|
capability: NativeCapability = check_agp_capability(part, profile)
|
|
assert type(capability) is NativeCapability
|
|
assert asdict(capability) == {
|
|
"part": part,
|
|
"format": "agp",
|
|
"available": False,
|
|
"reason": "riffer_profile_not_supported",
|
|
}
|
|
assert profile.summary.mapping_kind == "standard_midi"
|
|
assert profile.summary.native_available is False
|
|
assert profile.model_dump() == before
|
|
assert check_agp_capability(part, profile) == capability
|
|
for field, value in (
|
|
("part", "drums"),
|
|
("format", "sdp"),
|
|
("available", True),
|
|
("reason", "forged"),
|
|
):
|
|
with pytest.raises(FrozenInstanceError):
|
|
setattr(capability, field, value)
|
|
|
|
|
|
@pytest.mark.parametrize("part", ["lead", "bass"])
|
|
def test_instrument_mapping_does_not_establish_a_codec(part: Part) -> None:
|
|
profile = parse_profile((FIXTURES / f"synthetic-{part}.json").read_bytes())
|
|
assert profile.summary.mapping_kind == "instrument"
|
|
capability = check_agp_capability(part, profile)
|
|
assert capability.available is False
|
|
assert capability.reason == "riffer_profile_not_supported"
|
|
assert profile.provenance.status == "test_only"
|
|
|
|
|
|
@pytest.mark.parametrize("part", ["lead", "bass"])
|
|
@pytest.mark.parametrize("boundary", BOUNDARIES)
|
|
def test_native_metadata_alone_cannot_enable_instrument_support(
|
|
part: Part, boundary: Boundary
|
|
) -> None:
|
|
profile = parse_profile((FIXTURES / f"synthetic-{part}.json").read_bytes())
|
|
profile.summary.native_available = True
|
|
with pytest.raises(InvalidProfileError) as caught:
|
|
boundary(part, profile)
|
|
assert caught.value.details == {"reason": "invalid_profile_model"}
|
|
|
|
|
|
def test_mapping_attestation_and_product_version_are_not_native_evidence() -> None:
|
|
data = parse_profile((FIXTURES / "synthetic-lead.json").read_bytes()).model_dump()
|
|
data["id"] = data["summary"]["id"] = "harness.mapping.only"
|
|
data["summary"]["product_name"] = "TEST HARNESS mapping attestation, NOT vendor evidence"
|
|
data["summary"]["product_version"] = data["provenance"]["product_version"] = "unknown-build"
|
|
data["provenance"]["status"] = "verified"
|
|
data["provenance"]["sources"] = [
|
|
{
|
|
"kind": "lawful_local",
|
|
"reference": "TEST-HARNESS-NOT-VENDOR-EVIDENCE",
|
|
"aspects": [
|
|
"identity",
|
|
"range_or_voices",
|
|
"channels",
|
|
"actions",
|
|
"state_reset",
|
|
"timing",
|
|
"velocity",
|
|
],
|
|
}
|
|
]
|
|
profile = InstrumentProfile.model_validate(data)
|
|
assert check_agp_capability("lead", profile).available is False
|
|
with pytest.raises(NativeFormatUnavailableError) as caught:
|
|
require_agp_capability("lead", profile)
|
|
assert caught.value.details["reason"] == "riffer_profile_not_supported"
|
|
|
|
|
|
@pytest.mark.parametrize("part", ["lead", "bass"])
|
|
def test_require_raises_named_error_with_safe_complete_context(part: Part) -> None:
|
|
with pytest.raises(NativeFormatUnavailableError) as caught:
|
|
require_agp_capability(part, standard(part))
|
|
assert caught.value.code == "NATIVE_FORMAT_UNAVAILABLE"
|
|
assert caught.value.details == {
|
|
"format": "agp",
|
|
"part": part,
|
|
"reason": "riffer_profile_not_supported",
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("boundary", BOUNDARIES)
|
|
@pytest.mark.parametrize("invalid", ["drums", "ray", "LEAD", "", None, 1, True, [], {}])
|
|
def test_invalid_parts_are_bad_requests_not_unavailable(
|
|
boundary: Boundary, invalid: object
|
|
) -> None:
|
|
with pytest.raises(InvalidRequestError) as caught:
|
|
boundary(cast(Part, invalid), standard("lead"))
|
|
assert caught.value.code == "INVALID_REQUEST"
|
|
assert caught.value.details == {"format": "agp", "reason": "unsupported_agp_part"}
|
|
|
|
|
|
@pytest.mark.parametrize("boundary", BOUNDARIES)
|
|
@pytest.mark.parametrize(
|
|
("part", "profile_part"),
|
|
[("lead", "bass"), ("bass", "lead"), ("lead", "drums"), ("bass", "drums")],
|
|
)
|
|
def test_valid_wrong_part_profiles_are_selection_errors(
|
|
boundary: Boundary, part: Part, profile_part: Part
|
|
) -> None:
|
|
with pytest.raises(ProfilePartMismatchError) as caught:
|
|
boundary(part, standard(profile_part))
|
|
assert caught.value.code == "PROFILE_REQUIRED"
|
|
assert caught.value.details == {"format": "agp", "part": part, "reason": "wrong_part"}
|
|
|
|
|
|
@pytest.mark.parametrize("boundary", BOUNDARIES)
|
|
@pytest.mark.parametrize(
|
|
"mutation",
|
|
[
|
|
"instrument",
|
|
"part",
|
|
"format",
|
|
"native_flag",
|
|
"channel",
|
|
"range",
|
|
"aliases",
|
|
"provenance",
|
|
"schema",
|
|
"unchecked_copy",
|
|
"unchecked_construct",
|
|
],
|
|
)
|
|
def test_mutated_profiles_are_invalid_not_native_unavailable(
|
|
boundary: Boundary, mutation: str
|
|
) -> None:
|
|
profile = standard("lead")
|
|
if mutation == "instrument":
|
|
profile.summary.instrument = "ray"
|
|
elif mutation == "part":
|
|
profile.summary.part = "bass"
|
|
elif mutation == "format":
|
|
profile.summary.native_format = "sd3p"
|
|
elif mutation == "native_flag":
|
|
profile.summary.native_available = True
|
|
elif mutation == "channel":
|
|
profile.channel = 128
|
|
elif mutation == "range":
|
|
assert profile.playable_range is not None
|
|
profile.playable_range.low = 128
|
|
elif mutation == "aliases":
|
|
profile.articulations[0].aliases.append("slide")
|
|
elif mutation == "provenance":
|
|
profile.provenance.sources.clear()
|
|
elif mutation == "schema":
|
|
profile = profile.model_copy(update={"schema_version": "unknown-private-value"})
|
|
elif mutation == "unchecked_copy":
|
|
profile = profile.model_copy(update={"channel": "private-invalid-value"})
|
|
else:
|
|
profile = InstrumentProfile.model_construct(id="private-incomplete-profile")
|
|
with pytest.raises(InvalidProfileError) as caught:
|
|
boundary("lead", profile)
|
|
assert caught.value.code == "SERIALIZATION_ERROR"
|
|
assert caught.value.details == {"reason": "invalid_profile_model"}
|
|
assert "private" not in str(caught.value)
|
|
|
|
|
|
@pytest.mark.parametrize("boundary", BOUNDARIES)
|
|
@pytest.mark.parametrize("invalid", [None, {}, "not a profile"])
|
|
def test_wrong_profile_model_type_is_corrupted_input(boundary: Boundary, invalid: object) -> None:
|
|
with pytest.raises(InvalidProfileError) as caught:
|
|
boundary("lead", cast(InstrumentProfile, invalid))
|
|
assert caught.value.code == "SERIALIZATION_ERROR"
|
|
|
|
|
|
def test_mutating_a_profile_after_check_cannot_authorize_require() -> None:
|
|
profile = standard("lead")
|
|
capability = check_agp_capability("lead", profile)
|
|
profile.summary.native_available = True
|
|
with pytest.raises(InvalidProfileError):
|
|
require_agp_capability("lead", profile)
|
|
assert capability.available is False
|
|
|
|
|
|
def test_unexpected_validation_errors_are_not_fallback(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
profile = standard("lead")
|
|
|
|
def broken_check(profile: InstrumentProfile) -> InstrumentProfile:
|
|
raise RuntimeError("test-only unexpected programming failure")
|
|
|
|
monkeypatch.setattr("riff_groove_builder.formats.agp.checked_profile", broken_check)
|
|
with pytest.raises(RuntimeError):
|
|
require_agp_capability("lead", profile)
|
|
|
|
|
|
def test_invalid_request_takes_precedence_over_invalid_profile() -> None:
|
|
with pytest.raises(DomainError) as caught:
|
|
require_agp_capability("drums", cast(InstrumentProfile, None))
|
|
assert type(caught.value) is InvalidRequestError
|