822 lines
26 KiB
Python
822 lines
26 KiB
Python
"""Synthetic contracts only: no instrument map, file publication, or music generation."""
|
|
|
|
import math
|
|
from fractions import Fraction
|
|
|
|
import pytest
|
|
from jsonschema import Draft202012Validator
|
|
from pydantic import BaseModel, TypeAdapter, ValidationError
|
|
|
|
from riff_groove_builder.schemas import (
|
|
MAX_SCHEDULED_EVENTS,
|
|
Artifact,
|
|
ControlChangeEvent,
|
|
DrumNote,
|
|
EventPlan,
|
|
EventTrack,
|
|
GenerateMusicRequest,
|
|
GenerationResult,
|
|
ListInstrumentProfilesRequest,
|
|
MusicalIntent,
|
|
NoteOffEvent,
|
|
NoteOnEvent,
|
|
PitchedNote,
|
|
PositiveBeat,
|
|
ProfileListResult,
|
|
ProfileSelection,
|
|
ProfileSummary,
|
|
RationalBeat,
|
|
ResolvedGeneration,
|
|
ScheduledEvent,
|
|
Score,
|
|
ScoreTrack,
|
|
TimeSignature,
|
|
tool_input_schema,
|
|
)
|
|
|
|
|
|
def request(**updates: object) -> GenerateMusicRequest:
|
|
return GenerateMusicRequest.model_validate(
|
|
{"description": "A supported riff", "basename": "test_riff-1", **updates}
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("field", "value"),
|
|
[
|
|
("description", "x"),
|
|
("description", "x" * 4000),
|
|
("basename", "a"),
|
|
("basename", "A" * 64),
|
|
("parts", ["lead"]),
|
|
("parts", ["bass"]),
|
|
("parts", ["drums"]),
|
|
("parts", ["lead", "bass", "drums"]),
|
|
("formats", ["mid"]),
|
|
("formats", ["agp"]),
|
|
("formats", ["sdp"]),
|
|
("formats", ["mid", "agp", "sdp"]),
|
|
("tempo_bpm", 20),
|
|
("tempo_bpm", 300),
|
|
("tempo_bpm", 120.125),
|
|
("bars", 1),
|
|
("bars", 256),
|
|
("time_signature", {"numerator": 1, "denominator": 1}),
|
|
("time_signature", {"numerator": 16, "denominator": 16}),
|
|
("key", "E minor"),
|
|
("profiles", {}),
|
|
(
|
|
"profiles",
|
|
{"lead": "synthetic-lead", "bass": "synthetic-bass", "drums": "synthetic-drums"},
|
|
),
|
|
("seed", 0),
|
|
("seed", 2_147_483_647),
|
|
("allow_midi_fallback", False),
|
|
("allow_midi_fallback", True),
|
|
],
|
|
)
|
|
def test_request_valid_boundaries(field: str, value: object) -> None:
|
|
model = request(**{field: value})
|
|
assert GenerateMusicRequest.model_validate_json(model.model_dump_json()) == model
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("field", "value"),
|
|
[
|
|
("description", ""),
|
|
("description", " \t\n"),
|
|
("description", "x" * 4001),
|
|
("description", 10),
|
|
("description", None),
|
|
("basename", ""),
|
|
("basename", "a" * 65),
|
|
("basename", 1),
|
|
("basename", None),
|
|
("parts", []),
|
|
("parts", ["lead", "lead"]),
|
|
("parts", ["guitar"]),
|
|
("parts", "lead"),
|
|
("formats", []),
|
|
("formats", ["mid", "mid"]),
|
|
("formats", ["wav"]),
|
|
("formats", "mid"),
|
|
("tempo_bpm", 19.999),
|
|
("tempo_bpm", 300.001),
|
|
("tempo_bpm", True),
|
|
("tempo_bpm", False),
|
|
("tempo_bpm", "120"),
|
|
("tempo_bpm", math.nan),
|
|
("tempo_bpm", math.inf),
|
|
("tempo_bpm", -math.inf),
|
|
("bars", 0),
|
|
("bars", 257),
|
|
("bars", True),
|
|
("bars", 4.0),
|
|
("bars", "4"),
|
|
("bars", math.nan),
|
|
("bars", math.inf),
|
|
("time_signature", {"numerator": 0, "denominator": 4}),
|
|
("time_signature", {"numerator": 17, "denominator": 4}),
|
|
("time_signature", {"numerator": True, "denominator": 4}),
|
|
("time_signature", {"numerator": 4.0, "denominator": 4}),
|
|
("time_signature", {"numerator": 4, "denominator": 3}),
|
|
("time_signature", {"numerator": 4, "denominator": True}),
|
|
("time_signature", {"numerator": 4, "denominator": 4.0}),
|
|
("time_signature", {"numerator": 4, "denominator": "4"}),
|
|
("time_signature", {"numerator": 4}),
|
|
("time_signature", {"numerator": 4, "denominator": 4, "extra": 1}),
|
|
("profiles", {"lead": ""}),
|
|
("profiles", {"bass": " "}),
|
|
("profiles", {"drums": 1}),
|
|
("profiles", {"guitar": "unknown"}),
|
|
("profiles", []),
|
|
("seed", -1),
|
|
("seed", 2_147_483_648),
|
|
("seed", True),
|
|
("seed", 0.0),
|
|
("seed", "0"),
|
|
("seed", None),
|
|
("allow_midi_fallback", 0),
|
|
("allow_midi_fallback", "true"),
|
|
("allow_midi_fallback", None),
|
|
("output_root", r"C:\private"),
|
|
],
|
|
)
|
|
def test_request_invalid_boundaries(field: str, value: object) -> None:
|
|
with pytest.raises(ValidationError):
|
|
request(**{field: value})
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"basename",
|
|
[
|
|
"CON",
|
|
"con",
|
|
"CoN",
|
|
"PRN",
|
|
"aux",
|
|
"NUL",
|
|
"COM1",
|
|
"com9",
|
|
"LPT1",
|
|
"lPt9",
|
|
"../escape",
|
|
r"..\escape",
|
|
"/absolute",
|
|
r"C:\absolute",
|
|
r"\\host\share",
|
|
"a/b",
|
|
"a\\b",
|
|
".",
|
|
"..",
|
|
"a.",
|
|
"a ",
|
|
" a",
|
|
"a..b",
|
|
"a:b",
|
|
"a\n",
|
|
"_riff",
|
|
"-riff",
|
|
"é",
|
|
"CON.txt",
|
|
"CONIN$",
|
|
"COM¹",
|
|
"a\x00",
|
|
],
|
|
)
|
|
def test_unsafe_basenames(basename: str) -> None:
|
|
with pytest.raises(ValidationError):
|
|
request(basename=basename)
|
|
|
|
|
|
@pytest.mark.parametrize("basename", ["CON_1", "con-riff", "COM0", "COM10", "LPT0", "NULx", "9"])
|
|
def test_safe_reserved_name_neighbors(basename: str) -> None:
|
|
assert request(basename=basename).basename == basename
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("raw", "normalized"),
|
|
[
|
|
("C major", "C major"),
|
|
(" db MINOR ", "C# minor"),
|
|
("Bb major", "A# major"),
|
|
("f# mAjOr", "F# major"),
|
|
("Cb minor", "B minor"),
|
|
("B# major", "C major"),
|
|
("E# minor", "F minor"),
|
|
("Fb major", "E major"),
|
|
("gb major", "F# major"),
|
|
("Ab minor", "G# minor"),
|
|
("E minor", "E minor"),
|
|
("E Minor", "E minor"),
|
|
("Em", "E minor"),
|
|
("em", "E minor"),
|
|
(" F#m ", "F# minor"),
|
|
("Bbm", "A# minor"),
|
|
("Am", "A minor"),
|
|
("Cbm", "B minor"),
|
|
],
|
|
)
|
|
def test_key_normalization(raw: str, normalized: str) -> None:
|
|
Draft202012Validator(tool_input_schema("generate_music")).validate(
|
|
{"description": "riff", "basename": "x", "key": raw}
|
|
)
|
|
assert request(key=raw).key == normalized
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"key",
|
|
[
|
|
"C",
|
|
"EM",
|
|
"E m",
|
|
"Emin",
|
|
"Em major",
|
|
"F##m",
|
|
"BBm",
|
|
"H major",
|
|
"C dorian",
|
|
"C major minor",
|
|
"C major D minor",
|
|
"C## major",
|
|
"Dbb minor",
|
|
"C#b major",
|
|
"DB major",
|
|
"D♭ major",
|
|
"C\tmajor",
|
|
"C\nmajor",
|
|
"Cmajor",
|
|
"C major\n",
|
|
],
|
|
)
|
|
def test_invalid_keys(key: str) -> None:
|
|
assert not Draft202012Validator(tool_input_schema("generate_music")).is_valid(
|
|
{"description": "riff", "basename": "x", "key": key}
|
|
)
|
|
with pytest.raises(ValidationError):
|
|
request(key=key)
|
|
|
|
|
|
def test_advertised_key_examples_match_schema_and_runtime() -> None:
|
|
from riff_groove_builder.schemas import KEY_PATTERN
|
|
|
|
schema = tool_input_schema("generate_music")
|
|
key_schema = schema["properties"]["key"]["anyOf"][0]
|
|
assert key_schema["pattern"] == KEY_PATTERN
|
|
for example in key_schema["examples"]:
|
|
assert request(key=example).key is not None
|
|
Draft202012Validator(schema).validate(
|
|
{"description": "riff", "basename": "x", "key": example}
|
|
)
|
|
|
|
|
|
def test_omissions_do_not_resolve_intent_or_choose_profiles() -> None:
|
|
minimal = request()
|
|
assert minimal.model_fields_set == {"description", "basename"}
|
|
assert minimal.model_dump(exclude_unset=True) == {
|
|
"description": "A supported riff",
|
|
"basename": "test_riff-1",
|
|
}
|
|
for field in ("parts", "formats", "tempo_bpm", "bars", "time_signature", "key", "profiles"):
|
|
assert getattr(minimal, field) is None
|
|
assert getattr(request(**{field: None}), field) is None
|
|
assert minimal.seed == 0
|
|
assert minimal.allow_midi_fallback is True
|
|
assert ProfileSelection().selected() == {}
|
|
assert request(formats=["sdp"]).parts is None
|
|
assert request(profiles={"drums": "synthetic"}).parts is None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"updates",
|
|
[
|
|
{"parts": ["drums"], "formats": ["agp"]},
|
|
{"parts": ["bass"], "formats": ["sdp"]},
|
|
{"parts": ["lead"], "profiles": {"bass": "synthetic"}},
|
|
{"parts": ["bass", "drums"], "profiles": {"lead": "synthetic"}},
|
|
],
|
|
)
|
|
def test_explicit_incompatible_requests(updates: dict[str, object]) -> None:
|
|
with pytest.raises(ValidationError, match="requires|selected parts"):
|
|
request(**updates)
|
|
|
|
|
|
@pytest.mark.parametrize("denominator", [1, 2, 4, 8, 16])
|
|
def test_exact_meter(denominator: int) -> None:
|
|
assert TimeSignature.model_validate(
|
|
{"numerator": 7, "denominator": denominator}
|
|
).beats_per_bar == Fraction(28, denominator)
|
|
|
|
|
|
def summary(**updates: object) -> ProfileSummary:
|
|
return ProfileSummary.model_validate(
|
|
{
|
|
"id": "synthetic-only",
|
|
"part": "lead",
|
|
"instrument": "eclips",
|
|
"product_name": "Synthetic test metadata",
|
|
"product_version": "test",
|
|
"mapping_version": "test",
|
|
"midi_available": False,
|
|
"native_format": "griff",
|
|
"native_available": False,
|
|
"supported_articulations": [],
|
|
"limitations": ["No verified map or native capability."],
|
|
**updates,
|
|
}
|
|
)
|
|
|
|
|
|
def test_profile_list_empty_and_unavailable_summaries() -> None:
|
|
assert ListInstrumentProfilesRequest.model_validate({}).model_dump() == {}
|
|
assert ProfileListResult(profiles=[]).model_dump() == {"profiles": []}
|
|
unavailable = summary()
|
|
assert unavailable.mapping_kind == "instrument"
|
|
assert unavailable.model_dump()["mapping_kind"] == "instrument"
|
|
assert ProfileSummary.model_json_schema()["properties"]["mapping_kind"]["default"] == (
|
|
"instrument"
|
|
)
|
|
available = summary(
|
|
id="other-synthetic",
|
|
midi_available=True,
|
|
native_available=True,
|
|
supported_articulations=["sustain"],
|
|
limitations=[],
|
|
)
|
|
assert len(ProfileListResult(profiles=[unavailable, available]).profiles) == 2
|
|
with pytest.raises(ValidationError, match="unique"):
|
|
ProfileListResult(profiles=[unavailable, unavailable])
|
|
with pytest.raises(ValidationError):
|
|
ListInstrumentProfilesRequest.model_validate({"extra": 1})
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"updates",
|
|
[
|
|
{"part": "bass"},
|
|
{"instrument": "sd3"},
|
|
{"native_format": "sdp"},
|
|
{"limitations": []},
|
|
{"midi_available": 1},
|
|
{"native_available": "false"},
|
|
{"id": " "},
|
|
{"supported_articulations": ["sustain", "sustain"]},
|
|
{"limitations": ["same", "same"]},
|
|
{"private_path": "must not be exposed"},
|
|
{"mapping_kind": "gm"},
|
|
{"mapping_kind": True},
|
|
{"mapping_kind": "standard_midi", "supported_articulations": ["slide"]},
|
|
{
|
|
"mapping_kind": "standard_midi",
|
|
"supported_articulations": ["normal"],
|
|
"native_available": True,
|
|
},
|
|
],
|
|
)
|
|
def test_invalid_profile_summaries(updates: dict[str, object]) -> None:
|
|
with pytest.raises(ValidationError):
|
|
summary(**updates)
|
|
|
|
|
|
def intent() -> MusicalIntent:
|
|
return MusicalIntent.model_validate(
|
|
{
|
|
"description": "Synthetic score only",
|
|
"parts": ["lead"],
|
|
"tempo_bpm": 120,
|
|
"bars": 1,
|
|
"time_signature": {"numerator": 7, "denominator": 8},
|
|
"key": "E minor",
|
|
"seed": 0,
|
|
}
|
|
)
|
|
|
|
|
|
def note(**updates: object) -> PitchedNote:
|
|
return PitchedNote.model_validate(
|
|
{
|
|
"part": "lead",
|
|
"pitch": 60,
|
|
"start": {"numerator": 1, "denominator": 3},
|
|
"duration": {"numerator": 1, "denominator": 3},
|
|
"intensity": 0.5,
|
|
"articulation": "synthetic",
|
|
**updates,
|
|
}
|
|
)
|
|
|
|
|
|
def score() -> Score:
|
|
return Score(
|
|
intent=intent(),
|
|
start=RationalBeat(numerator=0),
|
|
end=RationalBeat(numerator=7, denominator=2),
|
|
tracks=[ScoreTrack(part="lead", notes=[note()])],
|
|
)
|
|
|
|
|
|
def test_rational_beats_and_score_round_trip() -> None:
|
|
assert RationalBeat(numerator=2, denominator=6).model_dump() == {
|
|
"numerator": 1,
|
|
"denominator": 3,
|
|
}
|
|
assert RationalBeat(numerator=0, denominator=17).value == 0
|
|
model = score()
|
|
assert model.intent.duration_beats == Fraction(7, 2)
|
|
restored = Score.model_validate_json(model.model_dump_json())
|
|
assert restored == model
|
|
logical = restored.tracks[0].notes[0]
|
|
assert logical.start.value + logical.duration.value == Fraction(2, 3)
|
|
assert sum((logical.duration.value for _ in range(300)), Fraction()) == 100
|
|
assert (
|
|
DrumNote.model_validate(
|
|
{
|
|
"voice": "synthetic_voice",
|
|
"start": {"numerator": 0},
|
|
"duration": {"numerator": 1},
|
|
"intensity": 1,
|
|
"articulation": "synthetic",
|
|
}
|
|
).part
|
|
== "drums"
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("model", "payload"),
|
|
[
|
|
(RationalBeat, {"numerator": -1}),
|
|
(RationalBeat, {"numerator": True}),
|
|
(RationalBeat, {"numerator": 1.0}),
|
|
(RationalBeat, {"numerator": 1, "denominator": 0}),
|
|
(RationalBeat, {"numerator": 1, "denominator": -3}),
|
|
(RationalBeat, {"numerator": 1, "denominator": False}),
|
|
(RationalBeat, {"numerator": 1, "denominator": 3.0}),
|
|
(RationalBeat, {"value": "1/3"}),
|
|
(PositiveBeat, {"numerator": 0}),
|
|
],
|
|
)
|
|
def test_invalid_rational_timing(model: type[BaseModel], payload: dict[str, object]) -> None:
|
|
with pytest.raises(ValidationError):
|
|
model.model_validate(payload)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"updates",
|
|
[
|
|
{"pitch": -1},
|
|
{"pitch": 128},
|
|
{"pitch": True},
|
|
{"pitch": 60.0},
|
|
{"part": "drums"},
|
|
{"voice": "kick"},
|
|
{"articulation": " "},
|
|
{"start": 0.5},
|
|
{"duration": {"numerator": 0}},
|
|
{"intensity": -0.01},
|
|
{"intensity": 1.01},
|
|
{"intensity": True},
|
|
{"intensity": math.nan},
|
|
{"intensity": math.inf},
|
|
{"intensity": -math.inf},
|
|
],
|
|
)
|
|
def test_invalid_logical_notes(updates: dict[str, object]) -> None:
|
|
with pytest.raises(ValidationError):
|
|
note(**updates)
|
|
|
|
|
|
@pytest.mark.parametrize("pitch", [0, 127])
|
|
@pytest.mark.parametrize("intensity", [0, 1])
|
|
def test_logical_midi_range_not_instrument_certification(pitch: int, intensity: int) -> None:
|
|
assert note(pitch=pitch, intensity=intensity).pitch == pitch
|
|
|
|
|
|
def test_invalid_score_invariants() -> None:
|
|
data = score().model_dump()
|
|
updates: list[dict[str, object]] = [
|
|
{"tracks": []},
|
|
{"tracks": [score().tracks[0].model_dump()] * 2},
|
|
{"end": {"numerator": 0}},
|
|
{"end": {"numerator": 4}},
|
|
{"start": {"numerator": 1}},
|
|
{"tracks": [{"part": "bass", "notes": [note().model_dump()]}]},
|
|
{"tracks": [{"part": "lead", "notes": [note(start={"numerator": 4}).model_dump()]}]},
|
|
]
|
|
for update in updates:
|
|
with pytest.raises(ValidationError):
|
|
Score.model_validate({**data, **update})
|
|
with pytest.raises(ValidationError):
|
|
MusicalIntent.model_validate({**intent().model_dump(), "parts": []})
|
|
|
|
|
|
def event(**updates: object) -> NoteOnEvent:
|
|
return NoteOnEvent.model_validate(
|
|
{
|
|
"tick": 0,
|
|
"part": "lead",
|
|
"channel": 0,
|
|
"priority": 2,
|
|
"sequence": 0,
|
|
"role": "musical_note",
|
|
"note": 60,
|
|
"velocity": 100,
|
|
**updates,
|
|
}
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"updates",
|
|
[
|
|
{"tick": -1},
|
|
{"tick": True},
|
|
{"tick": 0.0},
|
|
{"channel": -1},
|
|
{"channel": 16},
|
|
{"channel": True},
|
|
{"channel": 1.0},
|
|
{"priority": -1},
|
|
{"priority": False},
|
|
{"sequence": -1},
|
|
{"note": -1},
|
|
{"note": 128},
|
|
{"note": True},
|
|
{"velocity": 0},
|
|
{"velocity": 128},
|
|
{"velocity": True},
|
|
{"velocity": 1.0},
|
|
{"role": "articulation_control"},
|
|
{"kind": "control_change"},
|
|
{"controller": 1},
|
|
{"value": 2},
|
|
{"part": "guitar"},
|
|
],
|
|
)
|
|
def test_invalid_note_on_events(updates: dict[str, object]) -> None:
|
|
with pytest.raises(ValidationError):
|
|
event(**updates)
|
|
|
|
|
|
def test_discriminated_events_and_ranges() -> None:
|
|
adapter = TypeAdapter(ScheduledEvent)
|
|
for channel in (0, 15):
|
|
for number in (0, 127):
|
|
on = event(channel=channel, note=number, velocity=127, role="keyswitch")
|
|
assert adapter.validate_json(on.model_dump_json()) == on
|
|
base = {"tick": 0, "part": "lead", "channel": 0, "priority": 0, "sequence": 1}
|
|
off = NoteOffEvent.model_validate({**base, "role": "keyswitch", "note": 0})
|
|
assert off.velocity == 0
|
|
for number in (0, 127):
|
|
cc = ControlChangeEvent.model_validate({**base, "controller": number, "value": number})
|
|
assert adapter.validate_json(cc.model_dump_json()) == cc
|
|
for update in (
|
|
{"controller": -1},
|
|
{"controller": 128},
|
|
{"controller": True},
|
|
{"value": -1},
|
|
{"value": 128},
|
|
{"value": True},
|
|
{"note": 2},
|
|
{"velocity": 1},
|
|
{"role": "keyswitch"},
|
|
):
|
|
with pytest.raises(ValidationError):
|
|
ControlChangeEvent.model_validate({**cc.model_dump(), **update})
|
|
for velocity in (-1, 128, True, 0.0):
|
|
with pytest.raises(ValidationError):
|
|
NoteOffEvent.model_validate({**off.model_dump(), "velocity": velocity})
|
|
with pytest.raises(ValidationError):
|
|
adapter.validate_python({**event().model_dump(), "kind": "unknown"})
|
|
with pytest.raises(ValidationError):
|
|
EventTrack(part="bass", events=[event()])
|
|
with pytest.raises(ValidationError, match="ordering keys"):
|
|
EventTrack(part="lead", events=[event(), event()])
|
|
|
|
|
|
def plan(**updates: object) -> EventPlan:
|
|
return EventPlan.model_validate(
|
|
{
|
|
"score": score(),
|
|
"musical_start_tick": 24,
|
|
"musical_end_tick": 3384,
|
|
"end_tick": 3400,
|
|
"tracks": [
|
|
{
|
|
"part": "lead",
|
|
"events": [
|
|
event(tick=0, role="keyswitch").model_dump(),
|
|
event(tick=24, sequence=1).model_dump(),
|
|
],
|
|
}
|
|
],
|
|
**updates,
|
|
}
|
|
)
|
|
|
|
|
|
def test_event_plan_metadata_not_a_scheduler() -> None:
|
|
model = plan()
|
|
assert model.tracks[0].events[0].ordering_key == (0, 2, 0)
|
|
assert model.musical_end_tick - model.musical_start_tick == 3360
|
|
assert EventPlan.model_validate_json(model.model_dump_json()) == model
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"updates",
|
|
[
|
|
{"musical_start_tick": -1},
|
|
{"musical_end_tick": 20},
|
|
{"musical_end_tick": 3385},
|
|
{"end_tick": 3383},
|
|
{"end_tick": True},
|
|
{"ticks_per_quarter": 0},
|
|
{"ticks_per_quarter": True},
|
|
{"tracks": []},
|
|
{"tracks": [{"part": "bass", "events": []}]},
|
|
{"tracks": [{"part": "lead", "events": [event(tick=3401).model_dump()]}]},
|
|
{"tracks": [{"part": "lead", "events": [event(tick=0).model_dump()]}]},
|
|
{"tracks": [{"part": "lead", "events": [event(tick=3384).model_dump()]}]},
|
|
],
|
|
)
|
|
def test_invalid_event_plans(updates: dict[str, object]) -> None:
|
|
with pytest.raises(ValidationError):
|
|
plan(**updates)
|
|
|
|
|
|
def test_event_budget_boundary() -> None:
|
|
payload = plan().model_dump()
|
|
events = [event(tick=24, sequence=index).model_dump() for index in range(MAX_SCHEDULED_EVENTS)]
|
|
tracks = [{"part": "lead", "events": events}]
|
|
assert len(EventPlan.model_validate({**payload, "tracks": tracks}).tracks[0].events) == 100_000
|
|
events.append(event(tick=24, sequence=MAX_SCHEDULED_EVENTS).model_dump())
|
|
with pytest.raises(ValidationError, match="100,000"):
|
|
EventPlan.model_validate({**payload, "tracks": tracks})
|
|
|
|
|
|
def result_data() -> dict[str, object]:
|
|
return {
|
|
"status": "complete",
|
|
"resolved": {
|
|
"parts": ["lead"],
|
|
"tempo_bpm": 120,
|
|
"bars": 1,
|
|
"time_signature": {"numerator": 4, "denominator": 4},
|
|
"key": "E minor",
|
|
"seed": 0,
|
|
"profile_ids": {"lead": "synthetic-only"},
|
|
"assumptions": [],
|
|
"overrides": [],
|
|
"musical_start_tick": 0,
|
|
"musical_end_tick": 3840,
|
|
"ticks_per_quarter": 960,
|
|
},
|
|
"artifacts": [
|
|
{
|
|
"format": "mid",
|
|
"parts": ["lead"],
|
|
"path": r"C:\synthetic\test.mid",
|
|
"size_bytes": 64,
|
|
"profile_ids": ["synthetic-only"],
|
|
"fallback_for": [],
|
|
}
|
|
],
|
|
"warnings": [],
|
|
}
|
|
|
|
|
|
def test_success_and_fallback_result_invariants() -> None:
|
|
complete = GenerationResult.model_validate(result_data())
|
|
assert GenerationResult.model_validate_json(complete.model_dump_json()) == complete
|
|
data = complete.model_dump()
|
|
data["status"] = "midi_fallback"
|
|
data["artifacts"][0]["fallback_for"] = ["lead"]
|
|
data["warnings"] = [
|
|
{"code": "NATIVE_FORMAT_UNAVAILABLE", "part": "lead", "message": "Synthetic limitation."}
|
|
]
|
|
fallback = GenerationResult.model_validate(data)
|
|
assert GenerationResult.model_validate_json(fallback.model_dump_json()) == fallback
|
|
assert (
|
|
GenerationResult.model_validate(
|
|
{
|
|
**complete.model_dump(),
|
|
"warnings": [{"code": "ASSUMPTION", "part": None, "message": "Test"}],
|
|
}
|
|
).status
|
|
== "complete"
|
|
)
|
|
assert Artifact.model_validate(
|
|
{
|
|
**complete.artifacts[0].model_dump(),
|
|
"format": "griff",
|
|
"path": r"C:\synthetic\test.griff",
|
|
}
|
|
).parts == ["lead"]
|
|
assert Artifact.model_validate(
|
|
{
|
|
**complete.artifacts[0].model_dump(),
|
|
"format": "sd3p",
|
|
"path": r"C:\synthetic\test.sd3p",
|
|
"parts": ["drums"],
|
|
"profile_ids": ["synthetic-drums"],
|
|
}
|
|
).parts == ["drums"]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"updates",
|
|
[
|
|
{"format": "wav"},
|
|
{"size_bytes": 0},
|
|
{"size_bytes": -1},
|
|
{"size_bytes": True},
|
|
{"size_bytes": 64.0},
|
|
{"parts": []},
|
|
{"parts": ["lead", "lead"]},
|
|
{"format": "agp", "parts": ["lead", "bass"]},
|
|
{"format": "agp", "parts": ["drums"]},
|
|
{"format": "sdp", "parts": ["lead"]},
|
|
{"format": "agp", "fallback_for": ["lead"]},
|
|
{"fallback_for": ["drums"]},
|
|
{"fallback_for": ["lead", "lead"]},
|
|
{"path": "relative.mid"},
|
|
{"path": r"C:relative.mid"},
|
|
{"path": r"\relative.mid"},
|
|
{"path": "C:\\file\n.mid"},
|
|
{"profile_ids": []},
|
|
{"profile_ids": ["synthetic-only", "synthetic-only"]},
|
|
],
|
|
)
|
|
def test_invalid_artifacts(updates: dict[str, object]) -> None:
|
|
artifact = GenerationResult.model_validate(result_data()).artifacts[0]
|
|
with pytest.raises(ValidationError):
|
|
Artifact.model_validate({**artifact.model_dump(), **updates})
|
|
|
|
|
|
def test_invalid_claimed_results() -> None:
|
|
good = GenerationResult.model_validate(result_data())
|
|
artifact = good.artifacts[0].model_dump()
|
|
resolved = good.resolved.model_dump()
|
|
warning = {"code": "NATIVE_FORMAT_UNAVAILABLE", "part": "lead", "message": "Synthetic"}
|
|
updates: list[dict[str, object]] = [
|
|
{"status": "success"},
|
|
{"artifacts": []},
|
|
{"artifacts": [artifact, artifact]},
|
|
{"artifacts": [{**artifact, "profile_ids": ["wrong"]}]},
|
|
{"artifacts": [{**artifact, "parts": ["bass"]}]},
|
|
{"artifacts": [{**artifact, "fallback_for": ["lead"]}]},
|
|
{"warnings": [warning]},
|
|
{"warnings": [{**warning, "part": "bass", "code": "OTHER"}]},
|
|
{"status": "midi_fallback"},
|
|
{"status": "midi_fallback", "warnings": [warning]},
|
|
{"status": "midi_fallback", "artifacts": [{**artifact, "fallback_for": ["lead"]}]},
|
|
{"resolved": {**resolved, "musical_end_tick": -1}},
|
|
{"resolved": {**resolved, "musical_start_tick": 5000}},
|
|
{"resolved": {**resolved, "profile_ids": {}}},
|
|
{"resolved": {**resolved, "profile_ids": {"lead": "synthetic-only", "bass": "other"}}},
|
|
{
|
|
"resolved": {
|
|
**resolved,
|
|
"parts": ["lead", "bass"],
|
|
"profile_ids": {"lead": "synthetic-only", "bass": "other"},
|
|
}
|
|
},
|
|
{
|
|
"status": "midi_fallback",
|
|
"warnings": [warning],
|
|
"artifacts": [{**artifact, "fallback_for": ["lead"]}, {**artifact, "format": "agp"}],
|
|
},
|
|
{
|
|
"status": "midi_fallback",
|
|
"warnings": [{**warning, "part": None}],
|
|
"artifacts": [{**artifact, "fallback_for": ["lead"]}],
|
|
},
|
|
]
|
|
for update in updates:
|
|
with pytest.raises(ValidationError):
|
|
GenerationResult.model_validate({**good.model_dump(), **update})
|
|
assert (
|
|
ResolvedGeneration.model_validate({**resolved, "musical_start_tick": 3840}).musical_end_tick
|
|
== 3840
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model",
|
|
[
|
|
request(),
|
|
summary(),
|
|
score(),
|
|
note(),
|
|
plan(),
|
|
event(),
|
|
TimeSignature(numerator=4, denominator=4),
|
|
ProfileSelection(),
|
|
ProfileListResult(profiles=[]),
|
|
GenerationResult.model_validate(result_data()),
|
|
],
|
|
)
|
|
def test_unknown_fields_forbidden_on_all_contract_layers(model: BaseModel) -> None:
|
|
with pytest.raises(ValidationError, match="Extra inputs"):
|
|
type(model).model_validate({**model.model_dump(), "unsupported": True})
|