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

353 lines
14 KiB
Python

"""Independent numeric MIDI/native checks over real source and installed MCP stdio."""
import asyncio
import hashlib
import json
import shutil
import sys
import uuid
import xml.etree.ElementTree as ET
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 tool_input_schema, tool_output_schema
ROOT = Path(__file__).resolve().parents[2]
CANONICAL = """Lead guitar, 4/4, 120 BPM, 2 bars.
Bar 1:
Counts: 1 & 2 & 3 & [4 trip let]
Events: x x x x x x [x x ^]
Chords: F#2 F#2 D3 D3 Bb2 Bb2 - - -
Notes: - - - - - - C4 E4 G4
Hold the final G4 through the end of bar 2 without retriggering."""
E2 = """Lead guitar in E minor, 4/4, 120 BPM, 2 bars.
Bar 1:
Counts: 1 & 2 & 3 & [4 trip let]
Events: x x x x x x [x x ^]
Notes: E2 E2 E2 E2 E2 E2 E2 E2 E2
Hold the final E2 through the end of bar 2 without retriggering."""
STARTS = [0, 480, 960, 1440, 1920, 2400, 2880, 3200, 3520]
ENDS = [480, 960, 1440, 1920, 2400, 2880, 3200, 3520, 7680]
VELOCITIES = [80, 80, 64, 80, 96, 80, 80, 80, 80]
@pytest.fixture
def anyio_backend() -> str:
return "asyncio"
def inspect(
public: dict[str, Any],
pitches: list[list[int]],
starts: list[int],
ends: list[int],
techniques: list[str],
*,
native: bool = True,
velocities: list[int] | None = None,
held: list[bool] | None = None,
) -> dict[str, Any]:
assert public["status"] == ("complete" if native else "midi_fallback")
assert [a["format"] for a in public["artifacts"]] == (["mid", "griff"] if native else ["mid"])
rows = public["resolved"]["interpretation"]
assert len(rows) == len(pitches)
for index, (row, notes, start, end, technique) in enumerate(
zip(rows, pitches, starts, ends, techniques, strict=True)
):
assert row["pitches"] == notes and row["technique"] == technique and row["part"] == "lead"
assert row["start"]["numerator"] * 960 == start * row["start"]["denominator"]
assert row["duration"]["numerator"] * 960 == (end - start) * row["duration"]["denominator"]
assert row["held"] == (held[index] if held is not None else end - start > 960)
musical: list[tuple[int, int, int, int]] = []
switches: list[tuple[int, int, int, int]] = []
midi = mido.MidiFile(public["artifacts"][0]["path"])
assert midi.type == 1 and midi.ticks_per_beat == 960
total = public["resolved"]["musical_end_tick"]
assert [sum(m.time for m in track) for track in midi.tracks] == [total, total]
assert not any(m.type in ("control_change", "program_change") for t in midi.tracks for m in t)
active: dict[int, tuple[int, int]] = {}
tick = 0
for message in midi.tracks[1]:
tick += message.time
if message.type == "note_on" and message.velocity:
assert message.channel == 0 and message.note not in active
active[message.note] = tick, message.velocity
elif message.type == "note_off":
start, velocity = active.pop(message.note)
(switches if message.note in (24, 26, 31) else musical).append(
(start, tick, message.note, velocity)
)
assert not active
expected = sorted(
(start, end, pitch)
for notes, start, end in zip(pitches, starts, ends, strict=True)
for pitch in notes
)
assert sorted(n[:3] for n in musical) == expected
if velocities:
assert sorted(musical) == sorted(
(start, end, pitch, velocity)
for notes, start, end, velocity in zip(pitches, starts, ends, velocities, strict=True)
for pitch in notes
)
expected_switches = []
previous = None
for start, technique in zip(starts, techniques, strict=True):
if technique != previous:
expected_switches.append(
(
start,
start + 30,
{"normal": 24, "palm_mute": 26, "pinch_harmonic": 31}[technique],
100,
)
)
previous = technique
assert switches == expected_switches
native_rows = []
if native:
root = ET.parse(public["artifacts"][1]["path"]).getroot()
assert root.tag == "Riff" and root.get("PPQBase") == "480"
assert int(root.attrib["loopEnd"]) * 2 == total
assert root.get("Measures") == str(public["resolved"]["bars"])
assert root.get("Time-Signature") == "4/4"
if public["resolved"]["key"] == "E minor":
assert root.get("Key") == "Em"
assert not any(node.tag.startswith("cc-") for node in root)
tuning = {6: 36, 5: 43, 4: 48, 3: 53, 2: 57, 1: 62}
for string in root.findall("String"):
string_id = int(string.attrib["ID"])
occupied = 0
for note in string.findall("Note"):
start, end = int(note.attrib["Begin"]), int(note.attrib["End"])
pitch, fret = int(note.attrib["NoteID"]), int(note.attrib["Fret"])
assert pitch == tuning[string_id] + fret and 0 <= fret <= 22
assert start >= occupied and end > start
occupied = end
index = starts.index(start * 2)
assert (
note.attrib["Articulation"]
== {"normal": "Sus", "palm_mute": "PM", "pinch_harmonic": "PH"}[
techniques[index]
]
)
native_rows.append(
(start * 2, end * 2, pitch, int(note.attrib["Velocity"]), string_id, fret)
)
assert sorted(n[:4] for n in native_rows) == sorted(musical)
if pitches == [[40]] * 9:
assert all(row[4:] == (6, 4) for row in native_rows)
hashes = {
a["format"]: hashlib.sha256(Path(a["path"]).read_bytes()).hexdigest()
for a in public["artifacts"]
}
return {
"musical": sorted(musical),
"switches": switches,
"native": sorted(native_rows),
"hashes": hashes,
"resolved": public["resolved"],
}
async def exercise_hybrid_stdio(client: RawStdio, outputs: Path, *, label: str) -> list[str]:
retained = ROOT / ".test-artifacts" / "step16" / label
retained.mkdir(parents=True, exist_ok=True)
evidence: dict[str, Any] = {}
names = []
async def call(name: str, text: str, *, fallback: bool | None = False) -> dict[str, Any]:
arguments: dict[str, Any] = {"description": text, "basename": name}
if fallback is not None:
arguments["allow_midi_fallback"] = fallback
Draft202012Validator(tool_input_schema("generate_music")).validate(arguments)
response = await client.request(
name,
"tools/call",
{
"name": "generate_music",
"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"]
Draft202012Validator(tool_output_schema("generate_music")).validate(public)
if not result.get("isError"):
names.append(name)
destination = retained / name
if destination.exists():
shutil.rmtree(destination)
shutil.copytree(outputs / name, destination)
evidence[name + "-transport"] = {"arguments": arguments, "result": result}
return public
pitches = [[42, 46, 49]] * 2 + [[50, 54, 57]] * 2 + [[46, 50, 53]] * 2 + [[60], [64], [67]]
techniques = ["palm_mute"] * 8 + ["pinch_harmonic"]
for name, text, expected, native in [
("hybrid-d3", CANONICAL, pitches, True),
(
"hybrid-compact",
CANONICAL.replace("x x x x x x [x x ^]", "xxxxxx[xx^]")
.replace("F#2 F#2 D3 D3 Bb2 Bb2 - - -", "F#2F#2 D3D3 Bb2Bb2 ---")
.replace("- - - - - - C4 E4 G4", "-- -- -- C4E4G4"),
pitches,
True,
),
(
"hybrid-d2-fallback",
CANONICAL.replace("D3 D3", "D2 D2"),
pitches[:2] + [[38, 42, 45]] * 2 + pitches[4:],
False,
),
("hybrid-e2", E2, [[40]] * 9, True),
(
"hybrid-e2-compact",
E2.replace("1 & 2 & 3 &", "1& 2& 3&")
.replace("x x x x x x [x x ^]", "xxxxxx[xx^]")
.replace("E2 E2 E2 E2 E2 E2 E2 E2 E2", "E2E2E2E2E2E2E2E2E2"),
[[40]] * 9,
True,
),
(
"hybrid-e2-tilde",
E2[: E2.index("Hold")] + "\nBar 2:\nCounts: 1 2 3 4\nEvents: ~~~~\nNotes: ----",
[[40]] * 9,
True,
),
(
"hybrid-e2-continue",
E2[: E2.index("Hold")] + "\nBar 2: hold previous note to bar end",
[[40]] * 9,
True,
),
]:
public = await call(name, text, fallback=None if name == "hybrid-e2" else not native)
evidence[name] = inspect(
public, expected, STARTS, ENDS, techniques, native=native, velocities=VELOCITIES
)
assert len(evidence[name]["musical"]) == (9 if "e2" in name else 21)
if not native:
assert public["artifacts"][0]["fallback_for"] == ["lead"]
assert any("no_physical_string_assignment" in w["message"] for w in public["warnings"])
assert (
evidence[name]["hashes"]["mid"]
== "4ce995ca751590b32cd85c72c3a2d8dad7f478cdf34d3abb12616d121719c3b4"
)
assert evidence["hybrid-d3"]["hashes"] == evidence["hybrid-compact"]["hashes"]
assert (
evidence["hybrid-e2"]["hashes"]
== evidence["hybrid-e2-compact"]["hashes"]
== evidence["hybrid-e2-tilde"]["hashes"]
== evidence["hybrid-e2-continue"]["hashes"]
== {
"mid": "c1967e698a50b8c22a22e458c3e2dd02d04407d1ad1d61f8a462d790b0810e62",
"griff": "e8195e74c60ac93b1a4c92967a0834a64b2a2ebd74f453d6d3640ea465c4104c",
}
)
for index, symbol in enumerate(
("Em4OverB2", "Em4/B2", "Em4/B", "Em4OverB", "[C3 E3 G3]", "Em4(add4)", "F#")
):
name = f"hybrid-voicing-{index}"
public = await call(name, f"guitar, D minor, 1 bar\nCounts: 1\nEvents: o\nChords: {symbol}")
expected_notes = (
[47, 64, 67, 71]
if index < 2
else [59, 64, 67, 71]
if index < 4
else [48, 52, 55]
if index == 4
else [64, 67, 69, 71]
if index == 5
else [42, 46, 49]
)
evidence[name] = inspect(public, [expected_notes], [0], [960], ["normal"])
assert public["resolved"]["key"] == "D minor"
if index in (2, 3, 6):
assert "MIDI" in "\n".join(public["resolved"]["assumptions"])
public = await call("hybrid-rest", "guitar, 1 bar\nCounts: 1& 2&\nEvents: o-o~\nNotes: E2-E2-")
evidence["hybrid-rest"] = inspect(
public, [[40], [40]], [0, 960], [480, 1920], ["normal"] * 2, held=[False, True]
)
for name, text, code in [
("hybrid-d2-rejected", CANONICAL.replace("D3 D3", "D2 D2"), "NATIVE_FORMAT_UNAVAILABLE"),
(
"hybrid-ambiguous",
"guitar\nCounts: 1&a\nEvents: xxx\nNotes: E2E2E2",
"UNSUPPORTED_DESCRIPTION",
),
("hybrid-mismatch", E2.replace("x x x x x x [x x ^]", "xx"), "UNSUPPORTED_DESCRIPTION"),
(
"hybrid-slide",
E2.replace("x x x x x x [x x ^]", "xx xx xx x/^"),
"UNSUPPORTED_DESCRIPTION",
),
(
"hybrid-hold-conflict",
E2 + "\nBar 2:\nCounts: 1\nEvents: -\nNotes: -",
"UNSUPPORTED_DESCRIPTION",
),
("hybrid-private", E2 + "\nPRIVATE_PATH_AND_SECRET_DO_NOT_ECHO", "UNSUPPORTED_DESCRIPTION"),
]:
before = sorted(outputs.iterdir())
public = await call(name, text)
assert public["code"] == code
assert sorted(outputs.iterdir()) == before and not (outputs / name).exists()
assert "PRIVATE_PATH_AND_SECRET_DO_NOT_ECHO" not in json.dumps(public)
evidence[name] = public
assert not list(outputs.glob(".riff-stage-*"))
(retained / "evidence.json").write_text(json.dumps(evidence, indent=2), encoding="utf-8")
return names
@pytest.mark.anyio
async def test_source_hybrid_real_stdio() -> None:
outputs = ROOT / ".test-artifacts" / f"hybrid-source-{uuid.uuid4().hex}"
outputs.mkdir()
process = await asyncio.wait_for(
asyncio.create_subprocess_exec(
sys.executable,
"-I",
"-B",
"-m",
"riff_groove_builder",
"--output-dir",
str(outputs),
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()
assert handshake["result"]["serverInfo"]["version"] == "0.2.0"
await exercise_hybrid_stdio(client, outputs, label="source")
finally:
await client.close()
shutil.rmtree(outputs)
assert process.returncode == 0 and client.diagnostics == b""
(ROOT / ".test-artifacts" / "step16" / "source" / "hybrid-transport.json").write_text(
json.dumps(
{
"handshake": handshake["result"],
"exit_code": process.returncode,
"stdout_lines": len(client.lines),
"stderr": client.diagnostics.decode("utf-8"),
"sd3_template": None,
},
indent=2,
),
encoding="utf-8",
)