333 lines
13 KiB
Python
333 lines
13 KiB
Python
"""Real stdio control acceptance, shared verbatim with installed-wheel acceptance."""
|
|
|
|
import asyncio
|
|
import hashlib
|
|
import json
|
|
import shutil
|
|
import sys
|
|
import uuid
|
|
import xml.etree.ElementTree as ET
|
|
from collections.abc import Iterator
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import anyio
|
|
import mido # type: ignore[import-untyped]
|
|
import pytest
|
|
from jsonschema import Draft202012Validator
|
|
from test_native_service import TEMPLATE, job, plan_for, validate_saved
|
|
from test_stdio import TIMEOUT, RawStdio, _environment
|
|
|
|
from riff_groove_builder.errors import InvalidRequestError, SerializationError
|
|
from riff_groove_builder.schemas import (
|
|
MAX_SCHEDULED_EVENTS,
|
|
GenerationError,
|
|
GenerationResult,
|
|
NoteOffEvent,
|
|
NoteOnEvent,
|
|
Part,
|
|
tool_input_schema,
|
|
tool_output_schema,
|
|
)
|
|
from riff_groove_builder.service import GenerationService
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
pytestmark = pytest.mark.integration
|
|
|
|
|
|
@pytest.fixture
|
|
def anyio_backend() -> str:
|
|
return "asyncio"
|
|
|
|
|
|
@pytest.fixture
|
|
def controls_workspace() -> Iterator[Path]:
|
|
path = ROOT / ".test-artifacts" / f"cc-controls-{uuid.uuid4().hex}"
|
|
path.mkdir(parents=True)
|
|
try:
|
|
yield path
|
|
finally:
|
|
shutil.rmtree(path)
|
|
|
|
|
|
def control(part: Part, tick: int, controller: int, value: int) -> dict[str, object]:
|
|
return {
|
|
"part": part,
|
|
"beat": {"numerator": tick, "denominator": 960},
|
|
"controller": controller,
|
|
"value": value,
|
|
}
|
|
|
|
|
|
PITCHED = [
|
|
control("lead", 0, 1, 11),
|
|
control("lead", 480, 1, 73),
|
|
control("lead", 6720, 1, 0),
|
|
control("bass", 0, 1, 21),
|
|
control("bass", 960, 1, 127),
|
|
control("bass", 6720, 1, 0),
|
|
]
|
|
DRUMS = [control("drums", 320, 4, 96), control("drums", 6720, 4, 0)]
|
|
OTHER = [control("lead", 320, 74, 127), control("lead", 6720, 74, 0)]
|
|
|
|
|
|
def independent_controls(
|
|
result: GenerationResult, expected: dict[Part, list[tuple[int, int, int]]]
|
|
) -> None:
|
|
"""Use raw Mido deltas/bytes and XML stair points, not the scheduled-plan helper."""
|
|
for artifact in result.artifacts:
|
|
if artifact.format == "mid":
|
|
midi = mido.MidiFile(artifact.path)
|
|
assert midi.ticks_per_beat == 960
|
|
for part, track in zip(result.resolved.parts, midi.tracks[1:], strict=True):
|
|
tick = 0
|
|
observed = []
|
|
channels = {"lead": 0, "bass": 1, "drums": 9}
|
|
absolute = []
|
|
for msg in track:
|
|
tick += msg.time
|
|
absolute.append((tick, msg.type))
|
|
if msg.type == "control_change":
|
|
assert msg.channel == channels[part]
|
|
assert msg.bytes() == [0xB0 | channels[part], msg.control, msg.value]
|
|
observed.append((tick, msg.control, msg.value))
|
|
assert observed == expected.get(part, [])
|
|
for onset, kind in absolute:
|
|
if kind == "control_change":
|
|
same_tick = [k for t, k in absolute if t == onset]
|
|
if "note_on" in same_tick:
|
|
assert same_tick.index("control_change") < same_tick.index("note_on")
|
|
assert tick == 6720
|
|
elif artifact.format in ("griff", "briff"):
|
|
xml = ET.parse(artifact.path).getroot()
|
|
assert xml.attrib["PPQBase"] == "480"
|
|
points = xml.findall("./cc-1.Mod/p")
|
|
assert [(int(p.attrib["tick"]), int(p.attrib["value"])) for p in points] == [
|
|
(tick // 2, value)
|
|
for tick, controller, value in expected.get(artifact.parts[0], [])
|
|
if controller == 1
|
|
]
|
|
assert all(p.attrib["curveType"] == "stair" for p in points)
|
|
|
|
|
|
async def exercise_control_stdio(
|
|
client: RawStdio, outputs: Path, template: Path, *, prefix: str
|
|
) -> list[str]:
|
|
listed = (await client.request(f"{prefix}-schemas", "tools/list"))["result"]["tools"]
|
|
schema = next(tool["inputSchema"] for tool in listed if tool["name"] == "generate_music")
|
|
assert schema == tool_input_schema("generate_music")
|
|
assert "cc_events" in schema["properties"] and "cc_events" not in schema["required"]
|
|
assert next(
|
|
tool["outputSchema"] for tool in listed if tool["name"] == "generate_music"
|
|
) == tool_output_schema("generate_music")
|
|
expected: dict[Part, list[tuple[int, int, int]]] = {
|
|
"lead": [(0, 1, 11), (480, 1, 73), (6720, 1, 0)],
|
|
"bass": [(0, 1, 21), (960, 1, 127), (6720, 1, 0)],
|
|
}
|
|
published = []
|
|
evidence = []
|
|
cases: list[tuple[str, dict[str, object], list[str], list[Part]]] = [
|
|
(
|
|
"pitched",
|
|
{"parts": ["lead", "bass"], "cc_events": PITCHED, "allow_midi_fallback": False},
|
|
["mid", "griff", "briff"],
|
|
[],
|
|
),
|
|
("drums", {"cc_events": [*PITCHED, *DRUMS]}, ["mid", "griff", "briff"], ["drums"]),
|
|
("other", {"cc_events": [*PITCHED, *OTHER]}, ["mid", "briff", "sd3p"], ["lead"]),
|
|
(
|
|
"midi",
|
|
{
|
|
"formats": ["mid"],
|
|
"cc_events": [*PITCHED, *DRUMS, *OTHER],
|
|
"allow_midi_fallback": False,
|
|
},
|
|
["mid"],
|
|
[],
|
|
),
|
|
("tick", {"formats": ["mid"], "cc_events": [control("lead", 1, 1, 99)]}, ["mid"], []),
|
|
("omitted", {}, ["mid", "griff", "briff", "sd3p"], []),
|
|
("empty", {"cc_events": []}, ["mid", "griff", "briff", "sd3p"], []),
|
|
]
|
|
no_controls: list[list[bytes]] = []
|
|
for label, options, formats, fallback in cases:
|
|
basename = f"{prefix}-{label}"
|
|
req = job(basename=basename, **options)
|
|
arguments = req.model_dump(mode="json", exclude_none=True)
|
|
if label == "omitted":
|
|
arguments.pop("cc_events")
|
|
Draft202012Validator(schema).validate(arguments)
|
|
response = await client.request(
|
|
basename, "tools/call", {"name": "generate_music", "arguments": arguments}
|
|
)
|
|
payload = response["result"]
|
|
assert payload["isError"] is False
|
|
result = GenerationResult.model_validate(payload["structuredContent"])
|
|
assert result.status == ("midi_fallback" if fallback else "complete")
|
|
assert [a.format for a in result.artifacts] == formats
|
|
assert result.artifacts[0].fallback_for == fallback
|
|
warnings = [w for w in result.warnings if w.code == "NATIVE_FORMAT_UNAVAILABLE"]
|
|
assert [w.part for w in warnings] == fallback
|
|
if warnings:
|
|
assert (
|
|
"sd3_native_controller_not_supported"
|
|
if label == "drums"
|
|
else "unsupported_native_controller"
|
|
) in warnings[0].message
|
|
validate_saved(result, req, template)
|
|
wanted = {**expected} if label not in ("tick", "omitted", "empty") else {}
|
|
if label in ("drums", "midi"):
|
|
wanted["drums"] = [(320, 4, 96), (6720, 4, 0)]
|
|
if label in ("other", "midi"):
|
|
wanted["lead"] = [(0, 1, 11), (320, 74, 127), (480, 1, 73), (6720, 1, 0), (6720, 74, 0)]
|
|
if label == "tick":
|
|
wanted["lead"] = [(1, 1, 99)]
|
|
independent_controls(result, wanted)
|
|
if label in ("omitted", "empty"):
|
|
no_controls.append([Path(a.path).read_bytes() for a in result.artifacts])
|
|
evidence.append({"case": label, "formats": formats, "fallback": fallback, "cc": wanted})
|
|
published.append(basename)
|
|
assert no_controls[0] == no_controls[1]
|
|
# Original fixture/request baseline recorded before cc_events existed (report_9_fix).
|
|
assert [hashlib.sha256(raw).hexdigest() for raw in no_controls[0]] == [
|
|
"8fc4cfbe66c0706f223ec33a61692ead046a56a9dba51c6566f967690cee186c",
|
|
"fe7a2a5c98d0ae16f6894e681e7f001a658c90d11b3f1beb313d5c98b6c810c9",
|
|
"5da9b05e92e6989366f41f8abf22a0f8b88ae14591bc8b544e4c7c0cf47827c4",
|
|
"8bee8a86a7e60320d02c4404e36d818639af1d8f4ebce27f6c23a25e7ebd0589",
|
|
]
|
|
failures: list[tuple[str, dict[str, object], str, str]] = [
|
|
(
|
|
"disabled-drums",
|
|
{"cc_events": DRUMS, "allow_midi_fallback": False},
|
|
"NATIVE_FORMAT_UNAVAILABLE",
|
|
"sd3_native_controller_not_supported",
|
|
),
|
|
(
|
|
"disabled-lead",
|
|
{"cc_events": OTHER, "allow_midi_fallback": False},
|
|
"NATIVE_FORMAT_UNAVAILABLE",
|
|
"unsupported_native_controller",
|
|
),
|
|
(
|
|
"bad-part",
|
|
{"description": "lead only", "cc_events": DRUMS},
|
|
"INVALID_REQUEST",
|
|
"resolved_part_association",
|
|
),
|
|
(
|
|
"past-end",
|
|
{"cc_events": [control("lead", 6721, 1, 1)]},
|
|
"INVALID_REQUEST",
|
|
"beat_after_musical_end",
|
|
),
|
|
]
|
|
for label, options, code, reason in failures:
|
|
before = sorted(outputs.iterdir())
|
|
req = job(basename=f"{prefix}-{label}", **options)
|
|
response = await client.request(
|
|
label,
|
|
"tools/call",
|
|
{"name": "generate_music", "arguments": req.model_dump(mode="json", exclude_none=True)},
|
|
)
|
|
assert response["result"]["isError"] is True
|
|
Draft202012Validator(tool_output_schema("generate_music")).validate(
|
|
response["result"]["structuredContent"]
|
|
)
|
|
error = GenerationError.model_validate(response["result"]["structuredContent"])
|
|
assert error.code == code and error.details["reason"] == reason
|
|
assert sorted(outputs.iterdir()) == before
|
|
assert not list(outputs.glob(".riff-stage-*"))
|
|
print(f"CC stdio evidence {prefix}: {json.dumps(evidence, sort_keys=True)}")
|
|
return published
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_real_stdio_exact_controls(controls_workspace: Path) -> None:
|
|
outputs = controls_workspace / "outputs"
|
|
outputs.mkdir()
|
|
outside = controls_workspace / "other-cwd"
|
|
outside.mkdir()
|
|
template = controls_workspace / "original-template.sd3p"
|
|
shutil.copyfile(TEMPLATE, template)
|
|
original, info = template.read_bytes(), template.stat()
|
|
process = await asyncio.wait_for(
|
|
asyncio.create_subprocess_exec(
|
|
sys.executable,
|
|
"-I",
|
|
"-B",
|
|
"-m",
|
|
"riff_groove_builder",
|
|
"--output-dir",
|
|
str(outputs),
|
|
"--sd3-template",
|
|
str(template),
|
|
cwd=outside,
|
|
env=_environment(),
|
|
stdin=asyncio.subprocess.PIPE,
|
|
stdout=asyncio.subprocess.PIPE,
|
|
stderr=asyncio.subprocess.PIPE,
|
|
),
|
|
TIMEOUT,
|
|
)
|
|
client = RawStdio(process)
|
|
try:
|
|
await client.initialize()
|
|
published = await exercise_control_stdio(client, outputs, template, prefix="cc")
|
|
finally:
|
|
with anyio.CancelScope(shield=True):
|
|
await client.close()
|
|
assert process.returncode == 0 and client.diagnostics == b""
|
|
assert len(client.lines) == 13
|
|
assert sorted(p.name for p in outputs.iterdir()) == sorted(published)
|
|
assert template.read_bytes() == original
|
|
assert (template.stat().st_ino, template.stat().st_mtime_ns) == (info.st_ino, info.st_mtime_ns)
|
|
|
|
|
|
@pytest.mark.parametrize("corruption", ["channel", "note", "role", "release"])
|
|
def test_service_corrupt_drums_with_cc_never_fallback(
|
|
controls_workspace: Path, corruption: str
|
|
) -> None:
|
|
req = job(cc_events=DRUMS)
|
|
base, _ = plan_for(job())
|
|
for event in base.tracks[-1].events:
|
|
if corruption == "channel":
|
|
event.channel = 8
|
|
if isinstance(event, NoteOnEvent | NoteOffEvent):
|
|
if corruption == "note":
|
|
event.note += 1
|
|
if corruption == "role":
|
|
event.role = "keyswitch"
|
|
if corruption == "release" and isinstance(event, NoteOffEvent):
|
|
event.velocity = 1
|
|
service = GenerationService(controls_workspace, sd3_template=TEMPLATE)
|
|
with patch("riff_groove_builder.service.compile_score", return_value=base):
|
|
with pytest.raises(SerializationError) as failure:
|
|
service.generate_music(req)
|
|
assert failure.value.details["reason"] in {
|
|
"unsupported_sd3_event_semantics",
|
|
"score_event_plan_mismatch",
|
|
}
|
|
assert list(controls_workspace.iterdir()) == []
|
|
|
|
|
|
def test_service_inferred_part_pre_stage(controls_workspace: Path) -> None:
|
|
service = GenerationService(controls_workspace)
|
|
with pytest.raises(InvalidRequestError) as failure:
|
|
service.generate_music(job(description="lead only", cc_events=DRUMS))
|
|
assert failure.value.details["reason"] == "resolved_part_association"
|
|
assert list(controls_workspace.iterdir()) == []
|
|
|
|
|
|
def test_service_combined_budget_pre_stage(controls_workspace: Path) -> None:
|
|
service = GenerationService(controls_workspace)
|
|
req = job(
|
|
description="lead only 256 bars quarter",
|
|
cc_events=[control("lead", tick, 1, 73) for tick in range(MAX_SCHEDULED_EVENTS)],
|
|
)
|
|
with patch.object(service, "_new_stage") as staging:
|
|
with pytest.raises(InvalidRequestError) as failure:
|
|
service.generate_music(req)
|
|
assert failure.value.details == {"field": "cc_events", "reason": "event_budget_exceeded"}
|
|
staging.assert_not_called()
|
|
assert list(controls_workspace.iterdir()) == []
|