364 lines
14 KiB
Python
364 lines
14 KiB
Python
"""Model/JSON Schema integration with synthetic in-memory data, never real outputs."""
|
|
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
from fractions import Fraction
|
|
|
|
import pytest
|
|
from jsonschema import Draft202012Validator
|
|
from pydantic import ValidationError
|
|
|
|
from riff_groove_builder.schemas import (
|
|
GENERATION_OUTPUT,
|
|
PROFILE_LIST_OUTPUT,
|
|
EventPlan,
|
|
GenerateMusicRequest,
|
|
GenerationError,
|
|
GenerationResult,
|
|
ListInstrumentProfilesRequest,
|
|
ProfileListResult,
|
|
ToolName,
|
|
tool_input_schema,
|
|
tool_output_schema,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("parts", "formats"),
|
|
[
|
|
(["lead"], ["mid", "agp"]),
|
|
(["bass"], ["agp"]),
|
|
(["drums"], ["sdp"]),
|
|
(["lead", "bass"], ["mid", "agp"]),
|
|
(["lead", "bass", "drums"], ["mid", "agp", "sdp"]),
|
|
],
|
|
)
|
|
def test_representative_requests(parts: list[str], formats: list[str]) -> None:
|
|
data = {
|
|
"description": "Synthetic contract example, not interpreted here.",
|
|
"basename": "contract-1",
|
|
"parts": parts,
|
|
"formats": formats,
|
|
"tempo_bpm": 120.5,
|
|
"profiles": {part: f"synthetic-{part}" for part in parts},
|
|
}
|
|
schema = tool_input_schema("generate_music")
|
|
Draft202012Validator(schema).validate(data)
|
|
model = GenerateMusicRequest.model_validate(data)
|
|
serialized = model.model_dump_json(exclude_unset=True)
|
|
Draft202012Validator(schema).validate(json.loads(serialized))
|
|
restored = GenerateMusicRequest.model_validate_json(serialized)
|
|
assert restored == model
|
|
assert restored.model_fields_set == model.model_fields_set
|
|
assert restored.bars is None and restored.key is None and restored.time_signature is None
|
|
assert "bars" not in json.loads(serialized)
|
|
|
|
|
|
@pytest.mark.parametrize("tool", ["list_instrument_profiles", "generate_music"])
|
|
def test_schemas_are_valid_independent_and_explicit(tool: ToolName) -> None:
|
|
input_schema = tool_input_schema(tool)
|
|
output_schema = tool_output_schema(tool)
|
|
Draft202012Validator.check_schema(input_schema)
|
|
Draft202012Validator.check_schema(output_schema)
|
|
assert input_schema["additionalProperties"] is False
|
|
assert output_schema["type"] == "object"
|
|
assert len(output_schema["oneOf"]) == 2
|
|
input_schema["additionalProperties"] = True
|
|
output_schema.clear()
|
|
assert tool_input_schema(tool)["additionalProperties"] is False
|
|
assert "oneOf" in tool_output_schema(tool)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"updates",
|
|
[
|
|
{"description": " "},
|
|
{"basename": "con"},
|
|
{"basename": "../bad"},
|
|
{"basename": "valid\n"},
|
|
{"parts": []},
|
|
{"parts": ["bass", "bass"]},
|
|
{"parts": ["guitar"]},
|
|
{"formats": []},
|
|
{"formats": ["mid", "mid"]},
|
|
{"formats": ["wav"]},
|
|
{"tempo_bpm": True},
|
|
{"tempo_bpm": 301},
|
|
{"bars": True},
|
|
{"bars": 0},
|
|
{"seed": -1},
|
|
{"seed": True},
|
|
{"allow_midi_fallback": 1},
|
|
{"time_signature": {"numerator": 4, "denominator": 3}},
|
|
{"key": "C major minor"},
|
|
{"key": "C major\n"},
|
|
{"profiles": {"guitar": "synthetic"}},
|
|
{"parts": ["drums"], "formats": ["agp"]},
|
|
{"parts": ["lead"], "formats": ["sdp"]},
|
|
{"parts": ["lead"], "profiles": {"bass": "synthetic"}},
|
|
{"extra": 1},
|
|
],
|
|
)
|
|
def test_request_schema_and_model_reject_representative_bad_payloads(
|
|
updates: dict[str, object],
|
|
) -> None:
|
|
data = {"description": "riff", "basename": "contract", **updates}
|
|
assert not Draft202012Validator(tool_input_schema("generate_music")).is_valid(data)
|
|
with pytest.raises(ValidationError):
|
|
GenerateMusicRequest.model_validate(data)
|
|
|
|
|
|
def test_empty_profile_contract_and_error_branch() -> None:
|
|
input_validator = Draft202012Validator(tool_input_schema("list_instrument_profiles"))
|
|
assert input_validator.is_valid({})
|
|
assert not input_validator.is_valid({"extra": 1})
|
|
assert not input_validator.is_valid([])
|
|
assert ListInstrumentProfilesRequest.model_validate_json("{}").model_dump() == {}
|
|
validator = Draft202012Validator(tool_output_schema("list_instrument_profiles"))
|
|
for model in (
|
|
ProfileListResult(profiles=[]),
|
|
GenerationError(code="PROFILE_REQUIRED", message="Select a verified profile.", details={}),
|
|
):
|
|
payload = model.model_dump_json()
|
|
validator.validate(json.loads(payload))
|
|
assert PROFILE_LIST_OUTPUT.validate_json(payload) == model
|
|
for data in ({}, {"profiles": [], "code": "INVALID_REQUEST", "message": "Bad", "details": {}}):
|
|
assert not validator.is_valid(data)
|
|
|
|
|
|
def complete_data() -> dict[str, object]:
|
|
return {
|
|
"status": "complete",
|
|
"resolved": {
|
|
"parts": ["lead", "bass", "drums"],
|
|
"tempo_bpm": 120.25,
|
|
"bars": 1,
|
|
"time_signature": {"numerator": 7, "denominator": 8},
|
|
"key": "Bb minor",
|
|
"seed": 42,
|
|
"profile_ids": {
|
|
"lead": "synthetic-lead",
|
|
"bass": "synthetic-bass",
|
|
"drums": "synthetic-drums",
|
|
},
|
|
"assumptions": [],
|
|
"overrides": ["Explicit key"],
|
|
"musical_start_tick": 30,
|
|
"musical_end_tick": 3390,
|
|
"ticks_per_quarter": 960,
|
|
},
|
|
"artifacts": [
|
|
{
|
|
"format": "mid",
|
|
"parts": ["lead", "bass", "drums"],
|
|
"path": r"C:\synthetic\contract.mid",
|
|
"size_bytes": 100,
|
|
"profile_ids": ["synthetic-lead", "synthetic-bass", "synthetic-drums"],
|
|
"fallback_for": [],
|
|
},
|
|
{
|
|
"format": "griff",
|
|
"parts": ["lead"],
|
|
"path": r"C:\synthetic\contract_lead.griff",
|
|
"size_bytes": 100,
|
|
"profile_ids": ["synthetic-lead"],
|
|
"fallback_for": [],
|
|
},
|
|
{
|
|
"format": "briff",
|
|
"parts": ["bass"],
|
|
"path": r"C:\synthetic\contract_bass.briff",
|
|
"size_bytes": 100,
|
|
"profile_ids": ["synthetic-bass"],
|
|
"fallback_for": [],
|
|
},
|
|
{
|
|
"format": "sd3p",
|
|
"parts": ["drums"],
|
|
"path": r"C:\synthetic\contract_drums.sd3p",
|
|
"size_bytes": 100,
|
|
"profile_ids": ["synthetic-drums"],
|
|
"fallback_for": [],
|
|
},
|
|
],
|
|
"warnings": [],
|
|
}
|
|
|
|
|
|
def test_success_fallback_and_error_output_schema_round_trip() -> None:
|
|
complete = GenerationResult.model_validate(complete_data())
|
|
data = complete.model_dump()
|
|
data["status"] = "midi_fallback"
|
|
data["artifacts"] = [data["artifacts"][0]]
|
|
data["artifacts"][0]["fallback_for"] = ["lead", "bass", "drums"]
|
|
data["warnings"] = [
|
|
{"code": "NATIVE_FORMAT_UNAVAILABLE", "part": part, "message": "Synthetic test limit"}
|
|
for part in ("lead", "bass", "drums")
|
|
]
|
|
validator = Draft202012Validator(tool_output_schema("generate_music"))
|
|
for model in (
|
|
complete,
|
|
GenerationResult.model_validate(data),
|
|
GenerationError(
|
|
code="UNSUPPORTED_DESCRIPTION",
|
|
message="Use supported instructions.",
|
|
details={"unsupported": ["synthetic-term"]},
|
|
),
|
|
):
|
|
serialized = model.model_dump_json()
|
|
validator.validate(json.loads(serialized))
|
|
assert GENERATION_OUTPUT.validate_json(serialized) == model
|
|
updates: list[dict[str, object]] = [
|
|
{"status": "unknown"},
|
|
{"artifacts": []},
|
|
{"warnings": [{"code": "NATIVE_FORMAT_UNAVAILABLE", "part": "lead", "message": "Test"}]},
|
|
{"status": "midi_fallback"},
|
|
{"code": "INVALID_REQUEST", "message": "Mixed branch", "details": {}},
|
|
]
|
|
for update in updates:
|
|
payload = {**complete.model_dump(), **update}
|
|
assert not validator.is_valid(payload)
|
|
with pytest.raises(ValidationError):
|
|
GenerationResult.model_validate(payload)
|
|
|
|
|
|
def test_profile_summary_model_schema_associations() -> None:
|
|
validator = Draft202012Validator(tool_output_schema("list_instrument_profiles"))
|
|
for part, instrument, native in (
|
|
("lead", "eclips", "griff"),
|
|
("bass", "ray", "briff"),
|
|
("drums", "sd3", "sd3p"),
|
|
):
|
|
summary = {
|
|
"id": f"synthetic-{part}",
|
|
"part": part,
|
|
"instrument": instrument,
|
|
"product_name": "Synthetic metadata only",
|
|
"product_version": "test",
|
|
"mapping_version": "test",
|
|
"midi_available": False,
|
|
"native_format": native,
|
|
"native_available": False,
|
|
"supported_articulations": [],
|
|
"limitations": ["Not a verified production profile"],
|
|
}
|
|
model = ProfileListResult.model_validate({"profiles": [summary]})
|
|
assert model.profiles[0].mapping_kind == "instrument"
|
|
assert validator.is_valid({"profiles": [summary]})
|
|
validator.validate(json.loads(model.model_dump_json()))
|
|
assert PROFILE_LIST_OUTPUT.validate_json(model.model_dump_json()) == model
|
|
updates: list[dict[str, object]] = [
|
|
{"instrument": "unknown"},
|
|
{"native_format": "mid"},
|
|
{"limitations": []},
|
|
]
|
|
for update in updates:
|
|
bad = {"profiles": [{**summary, **update}]}
|
|
assert not validator.is_valid(bad)
|
|
with pytest.raises(ValidationError):
|
|
ProfileListResult.model_validate(bad)
|
|
|
|
|
|
def test_standard_profile_summaries_model_and_schema() -> None:
|
|
from riff_groove_builder.profiles import load_profile_registry
|
|
|
|
validator = Draft202012Validator(tool_output_schema("list_instrument_profiles"))
|
|
model = ProfileListResult(profiles=load_profile_registry().summaries())
|
|
validator.validate(json.loads(model.model_dump_json()))
|
|
assert PROFILE_LIST_OUTPUT.validate_json(model.model_dump_json()) == model
|
|
for summary in model.profiles:
|
|
if summary.mapping_kind == "instrument":
|
|
assert summary.native_available and summary.limitations
|
|
continue
|
|
assert summary.mapping_kind == "standard_midi"
|
|
updates: list[dict[str, object]] = [
|
|
{"mapping_kind": "GM"},
|
|
{"native_available": True},
|
|
{"supported_articulations": ["palm_mute"]},
|
|
{"limitations": []},
|
|
]
|
|
for update in updates:
|
|
data = {"profiles": [{**summary.model_dump(), **update}]}
|
|
assert not validator.is_valid(data)
|
|
with pytest.raises(ValidationError):
|
|
ProfileListResult.model_validate(data)
|
|
|
|
|
|
def test_exact_rational_score_event_serialization_without_profile_map() -> None:
|
|
plan = EventPlan.model_validate(
|
|
{
|
|
"score": {
|
|
"intent": {
|
|
"description": "Synthetic timing test",
|
|
"parts": ["lead", "bass", "drums"],
|
|
"tempo_bpm": 120,
|
|
"bars": 1,
|
|
"time_signature": {"numerator": 1, "denominator": 4},
|
|
"key": "C major",
|
|
"seed": 0,
|
|
},
|
|
"start": {"numerator": 0},
|
|
"end": {"numerator": 1},
|
|
"tracks": [
|
|
{
|
|
"part": part,
|
|
"notes": [
|
|
{
|
|
"kind": "drum" if part == "drums" else "pitched",
|
|
"part": part,
|
|
**(
|
|
{"voice": "synthetic_voice"}
|
|
if part == "drums"
|
|
else {"pitch": 60}
|
|
),
|
|
"start": {"numerator": 2, "denominator": 6},
|
|
"duration": {"numerator": 1, "denominator": 7},
|
|
"intensity": 0.5,
|
|
"articulation": "synthetic",
|
|
}
|
|
],
|
|
}
|
|
for part in ("lead", "bass", "drums")
|
|
],
|
|
},
|
|
"musical_start_tick": 24,
|
|
"musical_end_tick": 984,
|
|
"end_tick": 1000,
|
|
"tracks": [{"part": part, "events": []} for part in ("lead", "bass", "drums")],
|
|
}
|
|
)
|
|
serialized = plan.model_dump_json()
|
|
Draft202012Validator.check_schema(EventPlan.model_json_schema())
|
|
Draft202012Validator(EventPlan.model_json_schema()).validate(json.loads(serialized))
|
|
restored = EventPlan.model_validate_json(serialized)
|
|
assert restored == plan
|
|
for track in restored.score.tracks:
|
|
assert track.notes[0].start.value == Fraction(1, 3)
|
|
assert track.notes[0].duration.value == Fraction(1, 7)
|
|
assert track.notes[0].start.value + track.notes[0].duration.value == Fraction(10, 21)
|
|
assert track.notes[0].start.model_dump() == {"numerator": 1, "denominator": 3}
|
|
# A sub-tick logical duration is retained, not silently rounded by these models.
|
|
assert restored.score.tracks[0].notes[0].duration.value * 960 == Fraction(960, 7)
|
|
|
|
|
|
def test_contract_imports_do_not_start_server_or_write_protocol_output() -> None:
|
|
result = subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"-I",
|
|
"-B",
|
|
"-c",
|
|
"import sys; import riff_groove_builder.schemas; "
|
|
"import riff_groove_builder.errors; "
|
|
"assert 'mcp' not in sys.modules; "
|
|
"assert 'riff_groove_builder.server' not in sys.modules",
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=15,
|
|
check=False,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
assert result.stdout == result.stderr == ""
|