// StaffPedalTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Staff and Pedal notation — DSL, flatten, and MusicXML encode") struct StaffPedalTests { // MARK: - PedalKind @Test func pedalKindMusicXMLType() { #expect(PedalKind.start.musicXMLType == "start") #expect(PedalKind.stop.musicXMLType == "stop") } // MARK: - Staff DSL @Test func staffDSLBuildsStaffElement() { let element: ScoreElement = Staff(2, clef: .bass) { Note(.c, 3, dur: .whole, dyn: .mp) } guard case .staff(let number, let clef, let children) = element else { Issue.record("Expected .staff case") return } #expect(number == 2) #expect(clef == .bass) #expect(children.count == 1) } @Test func staffDSLWithNoClefStoresNil() { let element: ScoreElement = Staff(1) { Note(.c, 4, dur: .quarter) } guard case .staff(let number, let clef, _) = element else { Issue.record("Expected .staff case") return } #expect(number == 1) #expect(clef == nil) } @Test func staffPreconditionMinimumOneIsEnforced() { // Verify that number >= 1 is the accepted lower bound by testing valid values. // (Precondition trap on 0 is a runtime-only test; we confirm the happy path.) let element: ScoreElement = Staff(1) { Rest(dur: .quarter) } if case .staff(let n, _, _) = element { #expect(n == 1) } } // MARK: - Pedal DSL @Test func pedalDSLBuildsStartElement() { let element: ScoreElement = Pedal(.start) guard case .pedal(let kind) = element else { Issue.record("Expected .pedal case") return } #expect(kind == .start) } @Test func pedalDSLBuildsStopElement() { let element: ScoreElement = Pedal(.stop) guard case .pedal(let kind) = element else { Issue.record("Expected .pedal case") return } #expect(kind == .stop) } // MARK: - flatten() — Staff produces ScoreEvents @Test func staffFlattenYieldsNotesFromChildren() { let part: ScorePart = ScorePart(name: "piano", elements: [ Staff(1) { Voice(1) { Note(.c, 5, dur: .quarter) } }, Staff(2, clef: .bass) { Voice(2) { Note(.c, 3, dur: .whole) } }, ]) let events: [ScoreEvent] = part.flatten() #expect(events.count == 2) let midis: [Int] = events.compactMap { $0.pitch.midiNote }.sorted() // C3 = MIDI 48, C5 = MIDI 72 #expect(midis.contains(48)) #expect(midis.contains(72)) } @Test func staffFlattenDoesNotAdvanceOuterCursor() { // Two Staff blocks — both start at beat 0. let part: ScorePart = ScorePart(name: "p", elements: [ Staff(1) { Note(.e, 5, dur: .quarter, voice: 1) Note(.d, 5, dur: .quarter, voice: 1) }, Staff(2, clef: .bass) { Note(.c, 3, dur: .half, voice: 2) }, ]) let events: [ScoreEvent] = part.flatten() #expect(events.count == 3) // Staff 2 whole note starts at beat 0. let staff2Events: [ScoreEvent] = events.filter { $0.voice == 2 } #expect(staff2Events.count == 1) #expect(staff2Events[0].beat == 0.0) #expect(staff2Events[0].duration == 2.0) } // MARK: - flatten() — Pedal is skipped @Test func pedalIsSkippedByFlatten() { let part: ScorePart = ScorePart(name: "p", elements: [ Pedal(.start), Note(.c, 4, dur: .quarter), Note(.d, 4, dur: .quarter), Pedal(.stop), ]) let events: [ScoreEvent] = part.flatten() // Only the two notes should appear; pedal marks contribute no events. #expect(events.count == 2) #expect(events[0].beat == 0.0) #expect(events[1].beat == 1.0) } // MARK: - Single-staff encode — byte-identical regression guard @Test func singleStaffPartEmitsNoByteDifference() { // A part with no Staff(...) block should emit exactly the same XML as before. let scoreA: Score = Score(bpm: 120) { Part("piano") { Note(.c, 4, dur: .quarter) Note(.d, 4, dur: .quarter) } } let xmlA: String = scoreA.toMusicXMLString() #expect(!xmlA.contains("")) #expect(!xmlA.contains("")) } // MARK: - Multi-staff encode @Test func multiStaffPartEmitsStavesElement() { let score: Score = Score(bpm: 120) { Part("piano", clef: .treble) { Staff(1) { Voice(1) { Note(.c, 5, dur: .whole) } } Staff(2, clef: .bass) { Voice(2) { Note(.c, 3, dur: .whole) } } } } let xml: String = score.toMusicXMLString() #expect(xml.contains("2")) } @Test func multiStaffPartEmitsPerStaffClef() { let score: Score = Score(bpm: 120) { Part("piano", clef: .treble) { Staff(1) { Voice(1) { Note(.c, 5, dur: .whole) } } Staff(2, clef: .bass) { Voice(2) { Note(.c, 3, dur: .whole) } } } } let xml: String = score.toMusicXMLString() #expect(xml.contains("")) #expect(xml.contains("")) // Staff-1 clef: treble (G, line 2) // Staff-2 clef: bass (F, line 4) #expect(xml.contains("G")) #expect(xml.contains("F")) // Should NOT contain a bare (no number attribute) in attributes. // The bare form only appears in the mid-measure block. // Quick sanity: verify . let clefCount: Int = xml.components(separatedBy: "1")) #expect(xml.contains("2")) } // MARK: - Pedal encode — single-staff (no child in direction) @Test func pedalEmitsSingleStaffDirection() { let score: Score = Score(bpm: 120) { Part("piano") { Pedal(.start) Note(.c, 4, dur: .whole) Pedal(.stop) } } let xml: String = score.toMusicXMLString() #expect(xml.contains("")) #expect(xml.contains("")) // Single-staff: no inside the direction. let directionBlocks: [String] = xml.components(separatedBy: "") // There should be at least the dynamic direction + 2 pedal directions. // Each pedal direction block should NOT contain . let pedalStartBlock: Bool = directionBlocks.contains { $0.contains("pedal type=\"start\"") && !$0.contains("") } #expect(pedalStartBlock) } @Test func pedalEmitsMultiStaffDirectionWithStaff() { let score: Score = Score(bpm: 120) { Part("piano", clef: .treble) { Staff(1) { Voice(1) { Note(.c, 5, dur: .quarter) } } Staff(2, clef: .bass) { Voice(2) { Pedal(.start) Note(.c, 3, dur: .whole) Pedal(.stop) } } } } let xml: String = score.toMusicXMLString() #expect(xml.contains("")) #expect(xml.contains("")) // Multi-staff: the pedal direction authored under Staff(2) should carry 2. // Check that the pedal start direction contains the staff element. // The direction block is: // // // // // 2 ← present in multi-staff // let hasPedalWithStaff2: Bool = xml.contains("pedal type=\"start\"") && xml.contains("2") #expect(hasPedalWithStaff2) } @Test("Staff block contributes zero span to a ScoreTimeline fill") func staffContributesZeroSpanToTimelineFill() throws { // A Staff block overlays from beat 0, so it must NOT advance the timeline // cursor. Placing a whole-note Staff at beat 1 and a quarter note at beat 2 // must resolve without an overlap trap — which WOULD fire if the staff's // four-beat span advanced the cursor past the second placement. let filled: [ScoreElement] = try ScoreTimeline.fill( [ (.beat(1.0), Staff(1) { Note(.named(.c, 4), dur: .whole) }), (.beat(2.0), Note(.named(.d, 4), dur: .quarter)), ], meter: MeterMap(.fourFour) ) let staffCount: Int = filled.filter { if case .staff = $0 { return true } else { return false } }.count let restCount: Int = filled.filter { if case .rest = $0 { return true } else { return false } }.count let noteCount: Int = filled.filter { if case .note = $0 { return true } else { return false } }.count // One Staff, one gap-bridging rest (beat 1 → beat 2), one note — the staff // consumed zero timeline space. #expect(staffCount == 1) #expect(noteCount == 1) #expect(restCount == 1) } }