"""Real description-to-disk publication, failure injection, competition and junctions.""" import os import shutil import subprocess import uuid from collections.abc import Callable, Iterator from concurrent.futures import ThreadPoolExecutor from pathlib import Path from threading import Barrier from unittest.mock import patch import mido # type: ignore[import-untyped] import pytest from jsonschema import Draft202012Validator from pydantic import ValidationError from riff_groove_builder.articulations import compile_score from riff_groove_builder.compose import compose_score from riff_groove_builder.description import resolve_description from riff_groove_builder.errors import ( DomainError, NativeFormatUnavailableError, OutputExistsError, OutputIOError, SerializationError, ) from riff_groove_builder.midi import validate_midi_file, write_midi from riff_groove_builder.profiles import load_profile_registry from riff_groove_builder.schemas import ( EventPlan, GenerateMusicRequest, GenerationResult, Part, ProfileSelection, tool_output_schema, ) from riff_groove_builder.service import GenerationService, _Stage ROOT = Path(__file__).resolve().parents[2] pytestmark = pytest.mark.integration @pytest.fixture def workspace() -> Iterator[Path]: path = ROOT / ".test-artifacts" / f"generation-{uuid.uuid4().hex}" path.mkdir(parents=True) try: yield path finally: shutil.rmtree(path) def request(**updates: object) -> GenerateMusicRequest: job = GenerateMusicRequest.model_validate( { "description": "lead bass drums 137.5 BPM 2 bars 7/8 Bb minor quarter rests", "basename": "song", "seed": 42, **updates, } ) if job.profiles is None: job.profiles = ProfileSelection( **{ part: "standard.gm-drums.v1" if part == "drums" else f"standard.{part}.v1" for part in resolve_description(job).intent.parts } ) return job @pytest.mark.parametrize( "parts", [["lead"], ["bass"], ["drums"], ["lead", "bass", "drums"], ["drums", "bass"]] ) def test_actual_saved_midi_and_resolved_schema(workspace: Path, parts: list[Part]) -> None: job = request(parts=parts) result = GenerationService(workspace).generate_music(job) Draft202012Validator(tool_output_schema("generate_music")).validate( result.model_dump(mode="json") ) assert GenerationResult.model_validate_json(result.model_dump_json()) == result resolved = result.resolved assert resolved.parts == parts assert resolved.tempo_bpm == 137.5 assert resolved.bars == 2 assert resolved.time_signature.model_dump() == {"numerator": 7, "denominator": 8} assert resolved.key == "A# minor" assert resolved.seed == 42 assert resolved.overrides and resolved.assumptions assert resolved.musical_start_tick == 0 and resolved.musical_end_tick == 6720 artifact = result.artifacts[0] assert artifact.parts == artifact.fallback_for == parts path = Path(artifact.path) assert path.is_absolute() and path == workspace / "song" / "song.mid" assert artifact.size_bytes == path.stat().st_size == len(path.read_bytes()) assert path.read_bytes()[:4] == b"MThd" midi = mido.MidiFile(path) assert midi.type == 1 and midi.ticks_per_beat == 960 assert [track.name for track in midi.tracks] == [ "Conductor", *(part.capitalize() for part in parts), ] assert [sum(message.time for message in track) for track in midi.tracks] == [6720] * ( len(parts) + 1 ) conductor = midi.tracks[0] assert next(msg.tempo for msg in conductor if msg.type == "set_tempo") == round( 60_000_000 / 137.5 ) meter = next(msg for msg in conductor if msg.type == "time_signature") assert (meter.numerator, meter.denominator) == (7, 8) for part, track in zip(parts, midi.tracks[1:], strict=True): channel = {"lead": 0, "bass": 1, "drums": 9}[part] notes = [message for message in track if message.type == "note_on"] assert notes and all( note.channel == channel and 1 <= note.velocity <= 127 for note in notes ) assert all(message.is_meta or message.type in ("note_on", "note_off") for message in track) if part == "drums": assert {note.note for note in notes} == {36, 38, 42} resolution = resolve_description(job) assert job.profiles is not None selected = load_profile_registry().select(parts, job.profiles) validate_midi_file(path, compile_score(compose_score(resolution.intent, selected), selected)) assert list(workspace.iterdir()) == [path.parent] assert list(path.parent.iterdir()) == [path] assert "description" not in result.model_dump_json() def test_midi_only_is_complete_and_native_only_substitutes_all_parts(workspace: Path) -> None: service = GenerationService(workspace) direct = service.generate_music(request(basename="direct", formats=["mid"])) native = service.generate_music(request(basename="native", formats=["agp", "sdp"])) assert direct.status == "complete" and direct.artifacts[0].fallback_for == [] assert native.status == "midi_fallback" assert native.artifacts[0].fallback_for == ["lead", "bass", "drums"] assert len(direct.warnings) == 3 and len(native.warnings) == 6 assert ( Path(direct.artifacts[0].path).read_bytes() == Path(native.artifacts[0].path).read_bytes() ) assert not list(workspace.rglob("*.agp")) and not list(workspace.rglob("*.sdp")) with pytest.raises(NativeFormatUnavailableError): service.generate_music(request(basename="disabled", allow_midi_fallback=False)) assert not (workspace / "disabled").exists() assert not list(workspace.glob(".riff-stage-*")) @pytest.mark.parametrize( ("operation", "failure", "code"), [ ( "write_midi", SerializationError(details={"reason": "serializer_failed"}), "SERIALIZATION_ERROR", ), ("write_midi", OutputIOError(details={"reason": "midi_write_failed"}), "OUTPUT_IO_ERROR"), ( "validate_midi_file", SerializationError(details={"reason": "smf_content_mismatch"}), "SERIALIZATION_ERROR", ), ( "validate_midi_file", OutputIOError(details={"reason": "midi_read_failed"}), "OUTPUT_IO_ERROR", ), ("validate_midi_file", PermissionError("private-path"), "OUTPUT_IO_ERROR"), ], ) def test_write_read_failures_leave_no_partial_final_or_stage( workspace: Path, operation: str, failure: Exception, code: str ) -> None: sentinel = workspace / "sentinel" sentinel.write_bytes(b"unrelated operator data") before = sentinel.stat() with patch(f"riff_groove_builder.service.{operation}", side_effect=failure): with pytest.raises(DomainError) as captured: GenerationService(workspace).generate_music(request()) assert captured.value.code == code assert list(workspace.iterdir()) == [sentinel] assert sentinel.read_bytes() == b"unrelated operator data" assert (sentinel.stat().st_ino, sentinel.stat().st_mtime_ns) == ( before.st_ino, before.st_mtime_ns, ) def test_partial_write_is_cleaned_and_not_reported_as_fallback(workspace: Path) -> None: def failed_write( path: Path, _plan: EventPlan, *, on_created: Callable[[os.stat_result], None] ) -> Path: with path.open("xb") as stream: on_created(os.fstat(stream.fileno())) stream.write(b"partial bytes") raise OutputIOError(details={"reason": "midi_write_failed"}) with ( patch("riff_groove_builder.service.write_midi", side_effect=failed_write), pytest.raises(OutputIOError) as captured, ): GenerationService(workspace).generate_music(request()) assert captured.value.details["reason"] == "midi_write_failed" assert not list(workspace.iterdir()) def test_actual_corruption_fails_readback_and_cleans(workspace: Path) -> None: def corrupt( path: Path, plan: EventPlan, *, on_created: Callable[[os.stat_result], None] ) -> Path: write_midi(path, plan, on_created=on_created) path.write_bytes(b"not MIDI") return path with ( patch("riff_groove_builder.service.write_midi", side_effect=corrupt), pytest.raises(SerializationError), ): GenerationService(workspace).generate_music(request()) assert not list(workspace.iterdir()) def test_result_model_failure_precedes_publication(workspace: Path) -> None: with pytest.raises(ValidationError) as captured: GenerationResult.model_validate({}) with ( patch( "riff_groove_builder.service.GenerationResult.model_validate", side_effect=captured.value, ), pytest.raises(SerializationError) as error, ): GenerationService(workspace).generate_music(request()) assert error.value.details["reason"] == "invalid_generation_result" assert not list(workspace.iterdir()) def test_unexpected_bug_propagates_but_owned_stage_is_cleaned(workspace: Path) -> None: with ( patch("riff_groove_builder.service.validate_midi_file", side_effect=RuntimeError("bug")), pytest.raises(RuntimeError, match="bug"), ): GenerationService(workspace).generate_music(request()) assert not list(workspace.iterdir()) def test_file_change_during_readback_is_not_published(workspace: Path) -> None: def change(path: Path, plan: EventPlan) -> None: validate_midi_file(path, plan) with path.open("ab") as handle: handle.write(b"changed after readback") with ( patch("riff_groove_builder.service.validate_midi_file", side_effect=change), pytest.raises(OutputIOError) as captured, ): GenerationService(workspace).generate_music(request()) assert captured.value.details == {"reason": "staged_content_changed"} assert not list(workspace.iterdir()) def test_root_change_mid_generation_refuses_unsafe_cleanup(workspace: Path) -> None: root = workspace / "outputs" root.mkdir() service = GenerationService(root) moved = workspace / "original-root" def change_root( path: Path, plan: EventPlan, *, on_created: Callable[[os.stat_result], None] ) -> Path: write_midi(path, plan, on_created=on_created) root.rename(moved) root.mkdir() (root / "sentinel").write_bytes(b"replacement data") return path with ( patch("riff_groove_builder.service.write_midi", side_effect=change_root), pytest.raises(OutputIOError) as captured, ): service.generate_music(request()) assert captured.value.details == { "reason": "stage_cleanup_failed", "original_code": "OUTPUT_IO_ERROR", } assert list(root.iterdir()) == [root / "sentinel"] assert (root / "sentinel").read_bytes() == b"replacement data" assert len(list(moved.glob(".riff-stage-*"))) == 1 assert not (root / "song").exists() and not (moved / "song").exists() def test_publication_failure_is_io_and_cleans_complete_stage(workspace: Path) -> None: with patch.object(Path, "rename", side_effect=PermissionError("private-path")): with pytest.raises(OutputIOError) as captured: GenerationService(workspace).generate_music(request()) assert captured.value.details == {"reason": "publication_failed"} assert not list(workspace.iterdir()) def test_cleanup_failure_is_explicit_and_does_not_delete_other_files(workspace: Path) -> None: sentinel = workspace / "sentinel" sentinel.write_bytes(b"preserved") with ( patch.object(Path, "rename", side_effect=PermissionError("private-path")), patch.object(Path, "unlink", side_effect=PermissionError("private-cleanup")), pytest.raises(OutputIOError) as captured, ): GenerationService(workspace).generate_music(request()) assert captured.value.details == { "reason": "stage_cleanup_failed", "original_code": "OUTPUT_IO_ERROR", } assert not (workspace / "song").exists() stages = list(workspace.glob(".riff-stage-*")) assert len(stages) == 1 and (stages[0] / "song.mid").read_bytes().startswith(b"MThd") assert sentinel.read_bytes() == b"preserved" shutil.rmtree(stages[0]) def test_concurrent_same_name_has_exactly_one_winner(workspace: Path) -> None: barrier = Barrier(2) publish = GenerationService._publish def synchronized(self: GenerationService, stage: _Stage, destination: Path) -> None: barrier.wait(timeout=15) publish(self, stage, destination) def run(service: GenerationService) -> GenerationResult | DomainError: try: return service.generate_music(request()) except DomainError as error: return error services = [GenerationService(workspace), GenerationService(workspace)] with patch.object(GenerationService, "_publish", synchronized), ThreadPoolExecutor(2) as pool: results = list(pool.map(run, services)) assert len([result for result in results if isinstance(result, GenerationResult)]) == 1 losers = [result for result in results if isinstance(result, DomainError)] assert len(losers) == 1 and isinstance(losers[0], OutputExistsError) assert list(workspace.iterdir()) == [workspace / "song"] assert list((workspace / "song").iterdir()) == [workspace / "song" / "song.mid"] assert mido.MidiFile(workspace / "song" / "song.mid").type == 1 def junction(link: Path, target: Path) -> None: result = subprocess.run( [os.environ["COMSPEC"], "/d", "/c", "mklink", "/J", str(link), str(target)], capture_output=True, timeout=15, check=False, ) if result.returncode: pytest.skip("Windows junction creation unavailable in this environment") assert link.lstat().st_file_attributes & 0x400 @pytest.mark.skipif(os.name != "nt", reason="Real Windows junction/reparse checks") @pytest.mark.parametrize("location", ["root", "ancestor", "pinned-root", "destination", "stage"]) def test_real_windows_junctions_cannot_redirect_generation(workspace: Path, location: str) -> None: outside = workspace / "outside" outside.mkdir() sentinel = outside / "song.mid" sentinel.write_bytes(b"outside sentinel") root = workspace / "outputs" link = workspace / "link" if location in ("root", "ancestor"): (outside / "nested").mkdir() junction(link, outside) try: with pytest.raises(ValueError, match="existing local directory"): GenerationService(link if location == "root" else link / "nested") finally: link.rmdir() else: root.mkdir() service = GenerationService(root) if location == "pinned-root": root.rename(workspace / "original-root") junction(root, outside) try: with pytest.raises(OutputIOError) as captured: service.generate_music(request()) assert captured.value.details["reason"] == "output_root_changed" finally: root.rmdir() elif location == "destination": link = root / "song" junction(link, outside) try: with pytest.raises(OutputExistsError): service.generate_music(request()) finally: link.rmdir() else: links: list[Path] = [] def redirect( path: Path, plan: EventPlan, *, on_created: Callable[[os.stat_result], None] ) -> Path: write_midi(path, plan, on_created=on_created) path.parent.rename(root / "moved-stage") junction(path.parent, outside) links.append(path.parent) return path try: with ( patch("riff_groove_builder.service.write_midi", side_effect=redirect), pytest.raises(OutputIOError) as error, ): service.generate_music(request()) assert error.value.details == { "reason": "stage_cleanup_failed", "original_code": "OUTPUT_IO_ERROR", } assert not (root / "song").exists() finally: for item in links: item.rmdir() assert sentinel.read_bytes() == b"outside sentinel" assert not list(outside.glob("*.agp")) and not list(outside.glob("*.sdp"))