137 lines
5.1 KiB
Python
137 lines
5.1 KiB
Python
"""Real registry/capability/MIDI boundary checks, not native application acceptance."""
|
|
|
|
import shutil
|
|
import uuid
|
|
from collections.abc import Iterator
|
|
from dataclasses import asdict
|
|
from pathlib import Path
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from riff_groove_builder.articulations import compile_score
|
|
from riff_groove_builder.compose import compose_score
|
|
from riff_groove_builder.description import resolve_description
|
|
from riff_groove_builder.errors import NativeFormatUnavailableError
|
|
from riff_groove_builder.formats.agp import check_agp_capability, require_agp_capability
|
|
from riff_groove_builder.midi import validate_midi_file, write_midi
|
|
from riff_groove_builder.profiles import InvalidProfileError, load_profile_registry
|
|
from riff_groove_builder.schemas import GenerateMusicRequest, Part, ProfileSelection
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
@pytest.fixture
|
|
def workspace() -> Iterator[Path]:
|
|
path = ROOT / ".test-artifacts" / f"agp-capability-{uuid.uuid4().hex}"
|
|
path.mkdir(parents=True)
|
|
try:
|
|
yield path
|
|
finally:
|
|
shutil.rmtree(path)
|
|
|
|
|
|
@pytest.mark.parametrize("part", ["lead", "bass"])
|
|
def test_checks_and_require_perform_no_output_io(
|
|
workspace: Path, monkeypatch: pytest.MonkeyPatch, part: Part
|
|
) -> None:
|
|
profile = load_profile_registry().select(
|
|
[part], ProfileSelection(**{part: f"standard.{part}.v1"})
|
|
)[part]
|
|
sentinel = workspace / f"existing_{part}.agp"
|
|
sentinel.write_bytes(b"operator-owned sentinel; not an AGP fixture")
|
|
before = sentinel.stat()
|
|
missing_parent = workspace / "never-created"
|
|
io = Mock(side_effect=AssertionError("Capability boundary attempted filesystem IO"))
|
|
with monkeypatch.context() as guard:
|
|
for method in (
|
|
"open",
|
|
"mkdir",
|
|
"touch",
|
|
"write_bytes",
|
|
"write_text",
|
|
"read_bytes",
|
|
"read_text",
|
|
"stat",
|
|
"rename",
|
|
"replace",
|
|
"unlink",
|
|
):
|
|
guard.setattr(Path, method, io)
|
|
guard.setattr("builtins.open", io)
|
|
guard.setattr("os.open", io)
|
|
capability = check_agp_capability(part, profile)
|
|
with pytest.raises(NativeFormatUnavailableError) as caught:
|
|
require_agp_capability(part, profile)
|
|
io.assert_not_called()
|
|
assert capability.available is False
|
|
assert set(asdict(capability)) == {"part", "format", "available", "reason"}
|
|
assert caught.value.code == "NATIVE_FORMAT_UNAVAILABLE"
|
|
assert caught.value.details == {
|
|
"format": "agp",
|
|
"part": part,
|
|
"reason": capability.reason,
|
|
}
|
|
assert not missing_parent.exists()
|
|
assert list(workspace.iterdir()) == [sentinel]
|
|
assert sentinel.read_bytes() == b"operator-owned sentinel; not an AGP fixture"
|
|
after = sentinel.stat()
|
|
assert (after.st_ino, after.st_size, after.st_mtime_ns) == (
|
|
before.st_ino,
|
|
before.st_size,
|
|
before.st_mtime_ns,
|
|
)
|
|
|
|
|
|
def test_lead_and_bass_native_preflight_leaves_empty_output(workspace: Path) -> None:
|
|
selected = load_profile_registry().select(
|
|
["lead", "bass"], ProfileSelection(lead="standard.lead.v1", bass="standard.bass.v1")
|
|
)
|
|
for part, profile in selected.items():
|
|
with pytest.raises(NativeFormatUnavailableError):
|
|
require_agp_capability(part, profile)
|
|
assert list(workspace.iterdir()) == []
|
|
|
|
|
|
@pytest.mark.parametrize("part", ["lead", "bass"])
|
|
def test_explicit_independent_midi_remains_available_without_fake_agp(
|
|
workspace: Path, part: Part
|
|
) -> None:
|
|
request = GenerateMusicRequest(
|
|
description=f"{part} only 1 bar quarter",
|
|
basename="explicit-midi",
|
|
formats=["mid"],
|
|
)
|
|
resolved = resolve_description(request)
|
|
selected = load_profile_registry().select(
|
|
resolved.intent.parts, ProfileSelection(**{part: f"standard.{part}.v1"})
|
|
)
|
|
with pytest.raises(NativeFormatUnavailableError) as caught:
|
|
require_agp_capability(part, selected[part])
|
|
assert caught.value.details["part"] == part
|
|
assert list(workspace.iterdir()) == []
|
|
score = compose_score(resolved.intent, selected)
|
|
plan = compile_score(score, selected)
|
|
midi = workspace / "explicit-midi.mid"
|
|
assert write_midi(midi, plan) == midi
|
|
validate_midi_file(midi, plan)
|
|
assert midi.read_bytes().startswith(b"MThd")
|
|
assert list(workspace.iterdir()) == [midi]
|
|
assert not list(workspace.rglob("*.agp"))
|
|
assert check_agp_capability(part, selected[part]).available is False
|
|
|
|
|
|
def test_corrupted_profile_leaves_existing_output_untouched(workspace: Path) -> None:
|
|
sentinel = workspace / "existing_lead.agp"
|
|
sentinel.write_bytes(b"existing operator data")
|
|
profile = load_profile_registry().select(["lead"], ProfileSelection(lead="standard.lead.v1"))[
|
|
"lead"
|
|
]
|
|
profile.summary.native_available = True
|
|
with pytest.raises(InvalidProfileError) as caught:
|
|
require_agp_capability("lead", profile)
|
|
assert caught.value.code == "SERIALIZATION_ERROR"
|
|
assert sentinel.read_bytes() == b"existing operator data"
|
|
assert list(workspace.iterdir()) == [sentinel]
|