126 lines
5.3 KiB
Python
126 lines
5.3 KiB
Python
"""Keep the public JSON examples executable against the current typed contracts."""
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from jsonschema import Draft202012Validator
|
|
|
|
from riff_groove_builder.articulations import apply_scheduled_controls, compile_score
|
|
from riff_groove_builder.compose import compose_score
|
|
from riff_groove_builder.description import resolve_description
|
|
from riff_groove_builder.profiles import load_profile_registry
|
|
from riff_groove_builder.schemas import (
|
|
GenerateMusicRequest,
|
|
GenerationError,
|
|
GenerationResult,
|
|
ProfileSelection,
|
|
tool_input_schema,
|
|
tool_output_schema,
|
|
)
|
|
|
|
README = Path(__file__).resolve().parents[2] / "README.md"
|
|
EXAMPLES: list[dict[str, Any]] = [
|
|
json.loads(block)
|
|
for block in re.findall(r"```json\n(.*?)\n```", README.read_text("utf-8"), re.S)
|
|
]
|
|
|
|
|
|
def test_readme_json_example_inventory() -> None:
|
|
assert len(EXAMPLES) == 7
|
|
assert sum("description" in example for example in EXAMPLES) == 4
|
|
|
|
|
|
def test_restart_prompt_exact_original_hybrid_description() -> None:
|
|
text = README.read_text("utf-8")
|
|
expected = (
|
|
"Lead guitar in E minor, 4/4, 120 BPM, 2 bars.\n"
|
|
"Bar 1:\nCounts: 1 & 2 & 3 & [4 trip let]\n"
|
|
"Events: x x x x x x [x x ^]\n"
|
|
"Notes: E2 E2 E2 E2 E2 E2 E2 E2 E2\n"
|
|
"Hold the final E2 through the end of bar 2 without retriggering."
|
|
)
|
|
description = re.search(r"\nBEGIN DESCRIPTION\n(.*?)\nEND DESCRIPTION\n", text, re.S)
|
|
assert description is not None and description.group(1) == expected
|
|
assert f"```text\n{expected}\n```" in text
|
|
arguments = {
|
|
"description": description.group(1),
|
|
"basename": "em_e2_hybrid_20261020_fresh_chat_01",
|
|
"allow_midi_fallback": False,
|
|
}
|
|
Draft202012Validator(tool_input_schema("generate_music")).validate(arguments)
|
|
request = GenerateMusicRequest.model_validate(arguments)
|
|
resolution = resolve_description(request)
|
|
assert resolution.intent.parts == ["lead"] and resolution.intent.key == "E minor"
|
|
assert request.seed == 0 and request.cc_events == [] and request.formats is None
|
|
|
|
|
|
def test_current_documentation_local_links() -> None:
|
|
root = README.parent
|
|
for path in [README, *(root / "docs").rglob("*.md")]:
|
|
for link in re.findall(r"(?<!!)\[[^\]]+\]\(([^ )]+)\)", path.read_text("utf-8")):
|
|
if re.match(r"^[a-z]+:", link):
|
|
continue
|
|
target, _, anchor = link.partition("#")
|
|
destination = (path.parent / target.replace("/", os.sep)).resolve() if target else path
|
|
assert destination.is_file(), (path, link)
|
|
if anchor:
|
|
content = destination.read_text("utf-8")
|
|
anchors = set(re.findall(r'<a\s+id="([^"]+)"', content))
|
|
anchors.update(
|
|
re.sub(r"[^\w -]", "", heading.lower()).replace(" ", "-")
|
|
for heading in re.findall(r"(?m)^#{1,6}\s+(.+)$", content)
|
|
)
|
|
assert anchor in anchors, (path, link)
|
|
|
|
|
|
@pytest.mark.parametrize("example", EXAMPLES)
|
|
def test_readme_json_contracts(example: dict[str, Any]) -> None:
|
|
if "mcpServers" in example:
|
|
assert set(example) == {"mcpServers"}
|
|
server = example["mcpServers"]["riff-groove-builder"]
|
|
assert set(server) == {"command", "args"}
|
|
assert server["args"][:3] == ["-m", "riff_groove_builder", "--output-dir"]
|
|
assert all(isinstance(arg, str) for arg in [server["command"], *server["args"]])
|
|
assert Path(server["command"]).is_absolute()
|
|
assert Path(server["args"][3]).is_absolute()
|
|
elif "description" in example:
|
|
Draft202012Validator(tool_input_schema("generate_music")).validate(example)
|
|
request = GenerateMusicRequest.model_validate(example)
|
|
resolution = resolve_description(request)
|
|
profiles = load_profile_registry().select(
|
|
resolution.intent.parts, request.profiles or ProfileSelection()
|
|
)
|
|
plan = apply_scheduled_controls(
|
|
compile_score(compose_score(resolution.intent, profiles), profiles),
|
|
request.cc_events,
|
|
profiles,
|
|
)
|
|
assert plan.musical_start_tick == 0
|
|
if request.basename == "phrase-original":
|
|
assert set(example) == {"description", "basename"}
|
|
assert request.description == (
|
|
"Please produce a 4/4 riff in Em. With Chugging palm muting "
|
|
"8th notes from 1 to 3&;\n"
|
|
"and triplets on 4&A with the A as a pinch harmonic "
|
|
"that carries over a whole note\n"
|
|
"in the 2nd measure."
|
|
)
|
|
assert resolution.intent.parts == ["lead"]
|
|
assert profiles["lead"].id == "ample.ame.4.0.1.riffer.v1"
|
|
assert plan.musical_end_tick == 7680
|
|
assert request.cc_events == [] and request.formats is None
|
|
else:
|
|
assert plan.musical_end_tick == 6720
|
|
else:
|
|
payload = example.get("structuredContent", example)
|
|
Draft202012Validator(tool_output_schema("generate_music")).validate(payload)
|
|
if "code" in payload:
|
|
assert GenerationError.model_validate(payload).code == "NATIVE_FORMAT_UNAVAILABLE"
|
|
else:
|
|
result = GenerationResult.model_validate(payload)
|
|
assert result.status == "complete" and result.artifacts[0].size_bytes == 324
|