// TupletNoteDurationTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable import LyrebirdSynth @MainActor @Suite("Tuplet noteDuration validation") struct TupletNoteDurationTests { // MARK: - Matching contents → no warning @Test func threeQuartersFillQuarterTriplet() { let kids: [ScoreElement] = [ Note(.c, 5, dur: .quarter), Note(.d, 5, dur: .quarter), Note(.e, 5, dur: .quarter) ] #expect(tupletContentsMismatchWarning( actual: 3, normal: 2, noteDuration: .quarter, children: kids ) == nil) } @Test func fiveSixteenthsFillSixteenthQuintuplet() { let kids: [ScoreElement] = (0..<5).map { _ in Note(.c, 5, dur: .sixteenth) } #expect(tupletContentsMismatchWarning( actual: 5, normal: 4, noteDuration: .sixteenth, children: kids ) == nil) } @Test func mixedRhythmThatTotalsCorrectlyIsOk() { // The bracket means "3 quarters' worth"; a half + quarter (2 + 1 = 3 // quarter-beats) fills it even though the rhythm is not 3 equal notes. let kids: [ScoreElement] = [ Note(.c, 5, dur: .half), Note(.d, 5, dur: .quarter) ] #expect(tupletContentsMismatchWarning( actual: 3, normal: 2, noteDuration: .quarter, children: kids ) == nil) } // MARK: - Mismatched contents → warning @Test func wholeNoteInQuarterTripletWarns() { // The exact authoring bug: a whole note inside a 3:2 quarter triplet // (contents total 1 + 4 + 1 = 6, but 3 quarters = 3) → over-fills. let kids: [ScoreElement] = [ Note(.c, 5, dur: .quarter), Note(.d, 5, dur: .whole), Note(.e, 5, dur: .quarter) ] let warning: String? = tupletContentsMismatchWarning( actual: 3, normal: 2, noteDuration: .quarter, children: kids ) #expect(warning != nil) } @Test func underfilledTripletWarns() { // Only 2 quarters where 3 are expected. let kids: [ScoreElement] = [ Note(.c, 5, dur: .quarter), Note(.d, 5, dur: .quarter) ] #expect(tupletContentsMismatchWarning( actual: 3, normal: 2, noteDuration: .quarter, children: kids ) != nil) } // MARK: - The DSL still builds a valid element on mismatch (warning, not trap) @Test func tupletStillBuildsOnMismatch() { let element: ScoreElement = Tuplet(3, inTimeOf: 2, noteDuration: .quarter) { Note(.c, 5, dur: .whole) // mismatched on purpose } if case .tuplet(let a, let n, _, _) = element { #expect(a == 3) #expect(n == 2) } else { Issue.record("Tuplet must still return a .tuplet element despite the warning") } } // MARK: - Nested tuplets count by their COMPRESSED span (no false positive) @Test func nestedTupletCountedByCompressedSpan() { // A nested eighth-triplet (3 eighths in the time of 2) occupies exactly one // quarter of the OUTER bracket. So [quarter, quarter, nested] totals 3 // outer-quarters → fills a quarter triplet exactly → no warning. let nested: ScoreElement = Tuplet(3, inTimeOf: 2) { Note(.c, 5, dur: .eighth) Note(.d, 5, dur: .eighth) Note(.e, 5, dur: .eighth) } let kids: [ScoreElement] = [ Note(.c, 5, dur: .quarter), Note(.d, 5, dur: .quarter), nested ] #expect(tupletContentsMismatchWarning( actual: 3, normal: 2, noteDuration: .quarter, children: kids ) == nil) } @Test func nestedTupletThatUnderfillsWarns() { // A nested quarter-triplet (3 quarters in the time of 2) occupies only TWO // outer-quarters — it does NOT fill a 3-quarter outer bracket → warning. let nested: ScoreElement = Tuplet(3, inTimeOf: 2) { Note(.c, 5, dur: .quarter) Note(.d, 5, dur: .quarter) Note(.e, 5, dur: .quarter) } #expect(tupletContentsMismatchWarning( actual: 3, normal: 2, noteDuration: .quarter, children: [nested] ) != nil) } // MARK: - Message distinguishes over-fill from under-fill @Test func messageReportsDirection() { let over: String? = tupletContentsMismatchWarning( actual: 3, normal: 2, noteDuration: .quarter, children: [Note(.c, 5, dur: .whole)] // 4 > 3 ) #expect(over?.contains("over-fill") == true) let under: String? = tupletContentsMismatchWarning( actual: 3, normal: 2, noteDuration: .quarter, children: [Note(.c, 5, dur: .quarter)] // 1 < 3 ) #expect(under?.contains("under-fill") == true) } // MARK: - Without noteDuration, no validation (flexible tuplets) @Test func noNoteDurationStillBuilds() { let element: ScoreElement = Tuplet(3, inTimeOf: 2) { Note(.c, 5, dur: .whole) // would warn IF a noteDuration were declared } if case .tuplet = element { } else { Issue.record("expected .tuplet") } } // MARK: - emission /// A half-note triplet authored with `noteDuration: .half` must emit /// `half` inside `` for every /// tuplet member. Three members → appears exactly 3 times in the XML. @Test func halfNoteTripletEmitsNormalType() { let score: Score = Score(bpm: 60.0) { Part("test") { Tuplet(3, inTimeOf: 2, noteDuration: .half) { Note(.c, 5, dur: .quarter) Note(.d, 5, dur: .whole) Note(.e, 5, dur: .quarter) } } } let xml: String = score.toMusicXMLString() // Each of the 3 members must carry half. let occurrences: Int = xml.components(separatedBy: "half").count - 1 #expect(occurrences == 3) // Sanity: actual- and normal-notes are also present (3 and 2). #expect(xml.contains("3")) #expect(xml.contains("2")) } /// A standard eighth-triplet WITHOUT `noteDuration:` must NOT emit ``. /// Byte-identical guard: the tag must be completely absent. @Test func standardTripletWithoutNoteDurationEmitsNoNormalType() { let score: Score = Score(bpm: 60.0) { Part("test") { Tuplet(3, inTimeOf: 2) { Note(.c, 5, dur: .eighth) Note(.d, 5, dur: .eighth) Note(.e, 5, dur: .eighth) } // Fill remainder of the 4/4 bar so the measure is valid. Note(.f, 5, dur: .eighth) } } let xml: String = score.toMusicXMLString() #expect(!xml.contains("")) } /// A dotted base value (`noteDuration: .quarter.dotted`) must emit /// `quarter` followed by ``. @Test func dottedNoteDurationEmitsNormalTypeAndDot() { let score: Score = Score(bpm: 60.0) { Part("test") { // 3 dotted-quarters in time of 2: 3 × 1.5 = 4.5 quarter-beats. // Place in a 9/8 bar (same total) or just test the XML content. Tuplet(3, inTimeOf: 2, noteDuration: .quarter.dotted) { Note(.c, 5, dur: .quarter.dotted) Note(.d, 5, dur: .quarter.dotted) Note(.e, 5, dur: .quarter.dotted) } } } let xml: String = score.toMusicXMLString() #expect(xml.contains("quarter")) #expect(xml.contains("")) } /// Flattening a tuplet with `noteDuration:` yields the same `ScoreEvent` list /// as flattening one without — the field is notation-only. @Test func normalTypeIsNotationOnly() { let scoreWith: Score = Score(bpm: 60.0) { Part("test") { Tuplet(3, inTimeOf: 2, noteDuration: .quarter) { Note(.c, 5, dur: .quarter) Note(.d, 5, dur: .quarter) Note(.e, 5, dur: .quarter) } } } let scoreWithout: Score = Score(bpm: 60.0) { Part("test") { Tuplet(3, inTimeOf: 2) { Note(.c, 5, dur: .quarter) Note(.d, 5, dur: .quarter) Note(.e, 5, dur: .quarter) } } } let eventsWithNormalType: [ScoreEvent] = scoreWith.parts[0].flatten() let eventsWithoutNormalType: [ScoreEvent] = scoreWithout.parts[0].flatten() #expect(eventsWithNormalType.count == eventsWithoutNormalType.count) // Sounding durations must be identical. for (a, b): (ScoreEvent, ScoreEvent) in zip(eventsWithNormalType, eventsWithoutNormalType) { #expect(a.duration == b.duration) } } /// Nested tuplets carry their OWN declared note value: the inner bracket's /// `noteDuration` reaches only the inner atoms, and the outer bracket (with no /// `noteDuration`) emits no `` at all — no bleed-through. @Test func nestedTupletNormalTypeIsInnerOnly() { let score: Score = Score(bpm: 60.0) { Part("test") { Tuplet(3, inTimeOf: 2) { // outer: no noteDuration Note(.c, 5, dur: .quarter) Note(.d, 5, dur: .quarter) Tuplet(3, inTimeOf: 2, noteDuration: .eighth) { // inner: eighth Note(.e, 5, dur: .eighth) Note(.f, 5, dur: .eighth) Note(.g, 5, dur: .eighth) } } } } let xml: String = score.toMusicXMLString() // Only the 3 inner atoms carry eighth. #expect(xml.components(separatedBy: "eighth").count - 1 == 3) // No other (the outer bracket declared none → no bleed). #expect(xml.components(separatedBy: "").count - 1 == 3) } }