Cubase-MCP-GAJ3T.MusicGen.R.../tests/integration/test_text_feedback.py

218 lines
8.5 KiB
Python

"""Text-only recovery through real stdio, also reused by the installed wheel."""
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 typing import Any
import mido # type: ignore[import-untyped]
import pytest
from jsonschema import Draft202012Validator
from test_stdio import TIMEOUT, RawStdio, _environment
from riff_groove_builder.schemas import ToolName, tool_output_schema
ROOT = Path(__file__).resolve().parents[2]
@pytest.fixture
def anyio_backend() -> str:
return "asyncio"
@pytest.fixture
def feedback_workspace() -> Iterator[Path]:
path = ROOT / ".test-artifacts" / f"text-feedback-{uuid.uuid4().hex}"
path.mkdir(parents=True)
try:
yield path
finally:
shutil.rmtree(path)
async def exercise_text_feedback(client: RawStdio, outputs: Path, *, label: str) -> dict[str, Any]:
"""Make recovery decisions only from decoded TextContent, never structuredContent."""
async def call(name: ToolName, arguments: dict[str, object]) -> dict[str, Any]:
response = await client.request(
"text-feedback", "tools/call", {"name": name, "arguments": arguments}
)
result = response["result"]
assert len(result["content"]) == 1 and result["content"][0]["type"] == "text"
public = json.loads(result["content"][0]["text"])
assert isinstance(public, dict)
assert public == result["structuredContent"]
assert result["isError"] == ("code" in public)
Draft202012Validator(tool_output_schema(name)).validate(public)
return public
listed = await call("list_instrument_profiles", {})
profiles = listed["profiles"]
lead = next(
profile for profile in profiles if profile["part"] == "lead" and profile["native_available"]
)
assert lead["native_format"] == "griff"
before = sorted(outputs.iterdir())
bad: dict[str, object] = {
"description": "lead only 1 bar quarter",
"basename": "text-recovered",
"parts": ["lead"],
"formats": ["sd3p", "mid"],
"key": "E minor",
"profiles": {"lead": "aggressive_rock"},
"cc_events": [{"part": "lead", "beat": {"numerator": 0}, "controller": 91, "value": 64}],
}
wrong_format = await call("generate_music", bad)
assert wrong_format["code"] == "INVALID_REQUEST"
issue = wrong_format["details"]["issues"][0]
assert issue["field"] == "formats" and issue["reason"] == "format_parts"
assert "sd3p/sdp requires drums" in issue["correction"]
assert "Guitar lead uses griff/agp or mid" in issue["correction"]
assert "Omit formats" in issue["correction"]
assert sorted(outputs.iterdir()) == before
# CC91 was not user-requested; remove it rather than silently generating a vendor effect.
repaired = {key: value for key, value in bad.items() if key not in ("formats", "cc_events")}
wrong_profile = await call("generate_music", repaired)
assert wrong_profile["code"] == "PROFILE_REQUIRED"
assert "list_instrument_profiles({})" in wrong_profile["message"]
assert "omit profiles for defaults" in wrong_profile["message"]
assert "aggressive_rock" not in json.dumps(wrong_profile)
assert sorted(outputs.iterdir()) == before
unsupported = await call(
"generate_music",
{"description": "lead only detailed eighth triplet melody", "basename": "unsupported"},
)
assert unsupported["code"] == "UNSUPPORTED_DESCRIPTION"
assert "Arbitrary triplet patterns" in unsupported["message"]
assert sorted(outputs.iterdir()) == before
generated = await call(
"generate_music", {**repaired, "profiles": {"lead": lead["id"]}, "key": "Em"}
)
assert generated["status"] == "complete"
assert generated["resolved"]["key"] == "E minor"
assert generated["resolved"]["profile_ids"]["lead"] == lead["id"]
assert generated["resolved"]["musical_end_tick"] == 3840
assert [artifact["format"] for artifact in generated["artifacts"]] == ["mid", "griff"]
assert generated["warnings"]
artifacts = []
for artifact in generated["artifacts"]:
path = Path(artifact["path"])
assert path.parent == outputs / "text-recovered"
data = path.read_bytes()
assert len(data) == artifact["size_bytes"] > 0
# Step 11's independently recorded baseline remains byte-identical.
assert (
hashlib.sha256(data).hexdigest()
== {
"mid": "922d4c30e4618b6cce61a2ecebb36193773ca3884e56db80f7c18fe18e813f26",
"griff": "213adde1c4785dccd2289e6888328c16bca720fb8d2786edb4df34f3863d1c2f",
}[artifact["format"]]
)
assert artifact["fallback_for"] == []
if artifact["format"] == "mid":
midi = mido.MidiFile(path)
assert midi.type == 1 and midi.ticks_per_beat == 960
assert [track.name for track in midi.tracks] == ["Conductor", "Lead"]
assert [sum(message.time for message in track) for track in midi.tracks] == [3840, 3840]
note_ons = [
message.note
for track in midi.tracks
for message in track
if message.type == "note_on" and message.velocity > 0
]
assert len(note_ons) == 5 and note_ons.count(24) == 1
assert len([note for note in note_ons if note >= 36]) == 4
assert not any(
message.type == "control_change" and message.control == 91
for track in midi.tracks
for message in track
)
else:
root = ET.fromstring(data)
assert root.tag == "Riff" and len(root.findall("./String/Note")) == 4
assert not any(node.tag.startswith("cc-") for node in root)
artifacts.append(
{
"format": artifact["format"],
"size_bytes": len(data),
"sha256": hashlib.sha256(data).hexdigest(),
}
)
assert sorted(outputs.iterdir()) == sorted([*before, outputs / "text-recovered"])
assert not list(outputs.glob(".riff-stage-*"))
evidence = {
"visible_ids": [profile["id"] for profile in profiles],
"wrong_format": wrong_format,
"wrong_profile": wrong_profile,
"unsupported_code": unsupported["code"],
"recovered": {
"status": generated["status"],
"key": generated["resolved"]["key"],
"profile": generated["resolved"]["profile_ids"]["lead"],
"artifacts": artifacts,
"warning_codes": [w["code"] for w in generated["warnings"]],
"midi_note_ons": 5,
"midi_musical_note_ons": 4,
"midi_keyswitch_note_ons": 1,
"native_notes": 4,
"cc91_events": 0,
},
}
print("Text-only recovery evidence: " + json.dumps(evidence, sort_keys=True))
retained = ROOT / ".test-artifacts" / "step16" / label / "text-recovered"
retained.mkdir(parents=True, exist_ok=True)
for artifact in (outputs / "text-recovered").iterdir():
shutil.copyfile(artifact, retained / artifact.name)
(retained / "evidence.json").write_text(json.dumps(evidence, indent=2), encoding="utf-8")
return evidence
@pytest.mark.anyio
async def test_real_stdio_text_only_wrong_call_to_native_midi(feedback_workspace: Path) -> None:
process = await asyncio.wait_for(
asyncio.create_subprocess_exec(
sys.executable,
"-I",
"-B",
"-m",
"riff_groove_builder",
"--output-dir",
str(feedback_workspace),
cwd=ROOT,
env=_environment(),
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
),
TIMEOUT,
)
client = RawStdio(process)
try:
handshake = await client.initialize()
await exercise_text_feedback(client, feedback_workspace, label="source")
finally:
await client.close()
assert process.returncode == 0 and client.diagnostics == b""
retained = ROOT / ".test-artifacts" / "step16" / "source"
(retained / "text-transport.json").write_text(
json.dumps(
{
"handshake": handshake["result"],
"exit_code": process.returncode,
"stdout_lines": len(client.lines),
"stderr": client.diagnostics.decode("utf-8"),
},
indent=2,
),
encoding="utf-8",
)