Files
Lyrebird/Tests/LyrebirdTests/StaffPedalTests.swift
T

282 lines
10 KiB
Swift

// 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("<staves>"))
#expect(!xmlA.contains("<clef number="))
#expect(!xmlA.contains("<staff>"))
}
// 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("<staves>2</staves>"))
}
@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("<clef number=\"1\">"))
#expect(xml.contains("<clef number=\"2\">"))
// Staff-1 clef: treble (G, line 2)
// Staff-2 clef: bass (F, line 4)
#expect(xml.contains("<sign>G</sign>"))
#expect(xml.contains("<sign>F</sign>"))
// Should NOT contain a bare <clef> (no number attribute) in attributes.
// The bare form only appears in the mid-measure <attributes><clef> block.
// Quick sanity: verify <clef number= appears, not just <clef>.
let clefCount: Int = xml.components(separatedBy: "<clef number=").count - 1
#expect(clefCount == 2)
}
@Test func multiStaffPartEmitsStaffElementOnNotes() {
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("<staff>1</staff>"))
#expect(xml.contains("<staff>2</staff>"))
}
// MARK: - Pedal encode — single-staff (no <staff> 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("<pedal type=\"start\" line=\"yes\"/>"))
#expect(xml.contains("<pedal type=\"stop\" line=\"yes\"/>"))
// Single-staff: no <staff> inside the direction.
let directionBlocks: [String] = xml.components(separatedBy: "<direction placement=\"below\">")
// There should be at least the dynamic direction + 2 pedal directions.
// Each pedal direction block should NOT contain <staff>.
let pedalStartBlock: Bool = directionBlocks.contains { $0.contains("pedal type=\"start\"") && !$0.contains("<staff>") }
#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("<pedal type=\"start\" line=\"yes\"/>"))
#expect(xml.contains("<pedal type=\"stop\" line=\"yes\"/>"))
// Multi-staff: the pedal direction authored under Staff(2) should carry <staff>2</staff>.
// Check that the pedal start direction contains the staff element.
// The direction block is:
// <direction placement="below">
// <direction-type>
// <pedal type="start" line="yes"/>
// </direction-type>
// <staff>2</staff> ← present in multi-staff
// </direction>
let hasPedalWithStaff2: Bool = xml.contains("pedal type=\"start\"") && xml.contains("<staff>2</staff>")
#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)
}
}