// GlissandoTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Glissando — authoring, encoding, and validation") struct GlissandoTests { // MARK: - Basic glissando emission @Test func glissandoDefaultEmitsWavyStartAndStop() { let score: Score = Score(bpm: 120) { Part("p") { Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando())]) Note(.e, 5, dur: .quarter, spanners: [.endGlissando]) } } let xml: String = score.toMusicXMLString() // Start: #expect(xml.contains("")) // Stop: #expect(xml.contains("")) // Exactly one start and one stop. let startCount: Int = xml.components(separatedBy: "? = xml.range(of: "? = xml.range(of: " = startRange, let t: Range = stopRange { #expect(s.lowerBound < t.lowerBound) } else { Issue.record("Expected both glissando start and stop to appear in XML") } } @Test func glissandoNoAnnotationProducesByteIdenticalOutput() { // A score without any glissando must not contain or . let score: Score = Score(bpm: 120) { Part("p") { Note(.c, 5, dur: .quarter) Note(.e, 5, dur: .quarter) } } let xml: String = score.toMusicXMLString() #expect(!xml.contains(", not . #expect(xml.contains("")) // Stop also uses . #expect(xml.contains("")) // No element present. #expect(!xml.contains("")) #expect(!xml.contains("")) #expect(!xml.contains("gliss.")) // The self-closing form must NOT appear for the start. let selfClosingStart: Int = xml.components( separatedBy: "" ).count - 1 #expect(selfClosingStart == 0) } @Test func textLabelNilEmitsSelfClosingElement() { let score: Score = Score(bpm: 120) { Part("p") { Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando(text: nil))]) Note(.e, 5, dur: .quarter, spanners: [.endGlissando]) } } let xml: String = score.toMusicXMLString() #expect(xml.contains("")) #expect(!xml.contains(">gliss.")) } @Test func textLabelWithXMLSpecialCharsIsEscaped() { let score: Score = Score(bpm: 120) { Part("p") { Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando(text: "A&"))]) Note(.e, 5, dur: .quarter, spanners: [.endGlissando]) } } let xml: String = score.toMusicXMLString() // The escape function should have converted & → &, < → <, > → > #expect(xml.contains("A&<B>")) } // MARK: - Unbalanced span handling @Test func orphanStopDroppedAndDiagnosticPosted() async { LyrebirdMessageCenter.shared.clear() let score: Score = Score(bpm: 120) { Part("p") { // Only an endGlissando with no matching beginGlissando. Note(.c, 5, dur: .quarter, spanners: [.endGlissando]) Note(.e, 5, dur: .quarter) } } let xml: String = score.toMusicXMLString() // No glissando elements should be emitted. #expect(!xml.contains("")) #expect(xml.contains("")) // Both slur and glissando stop must be present. #expect(xml.contains("")) #expect(xml.contains("")) } // MARK: - flatten unaffected @Test func glissandoDoesNotAffectFlatten() { // flatten() produces the same ScoreEvent list regardless of glissando spans // (glissando is notation-only — no effect on beat positions or durations). let partWithGlissando: ScorePart = Part("p") { Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando())]) Note(.e, 5, dur: .quarter, spanners: [.endGlissando]) } let partPlain: ScorePart = Part("p") { Note(.c, 5, dur: .quarter) Note(.e, 5, dur: .quarter) } let eventsWithGlissando: [ScoreEvent] = partWithGlissando.flatten() let eventsPlain: [ScoreEvent] = partPlain.flatten() #expect(eventsWithGlissando.count == eventsPlain.count) for (a, b): (ScoreEvent, ScoreEvent) in zip(eventsWithGlissando, eventsPlain) { #expect(a.beat == b.beat) #expect(a.duration == b.duration) } } // MARK: - GlissandoKind @Test func glissandoKindMusicXMLElementNames() { #expect(GlissandoKind.glissando.musicXMLElementName == "glissando") #expect(GlissandoKind.slide.musicXMLElementName == "slide") } // MARK: - GlissandoLineType @Test func glissandoLineTypeMusicXMLValues() { #expect(GlissandoLineType.solid.musicXMLValue == "solid") #expect(GlissandoLineType.dashed.musicXMLValue == "dashed") #expect(GlissandoLineType.dotted.musicXMLValue == "dotted") #expect(GlissandoLineType.wavy.musicXMLValue == "wavy") } // MARK: - Glissando defaults @Test func glissandoDefaultValues() { let g: Glissando = Glissando() #expect(g.kind == .glissando) #expect(g.lineType == .wavy) #expect(g.text == nil) } // MARK: - Multiple glissandos in sequence @Test func multipleGlissandosInSequenceEmitCorrectly() { let score: Score = Score(bpm: 120) { Part("p") { Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando())]) Note(.e, 5, dur: .quarter, spanners: [.endGlissando]) // Second glissando span Note(.g, 5, dur: .quarter, spanners: [.beginGlissando(Glissando(kind: .slide))]) Note(.b, 5, dur: .quarter, spanners: [.endGlissando]) } } let xml: String = score.toMusicXMLString() // Two glissando starts: one wavy, one slide. let glissandoStarts: Int = xml.components(separatedBy: "type=\"start\"").count - 1 // There should be 2 starts (one glissando, one slide). #expect(glissandoStarts >= 2) // First glissando stop uses , second uses . #expect(xml.contains("")) #expect(xml.contains("")) } @Test func glissandoStopSurvivesMultiVoiceBarlineSplit() { // The stop note crosses a barline on the multi-voice encoder path, which // reconstructs a leftover continuation atom. That reconstruction must // carry the glissando-stop forward (regression: it previously reset to // default, dropping the ). let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) { Part("p") { Voice(1) { Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando())]) // Stop note is a whole note starting on beat 2 → crosses the // barline (splits at the bar end). Note(.g, 5, dur: .whole, spanners: [.endGlissando]) } Voice(2) { Note(.c, 4, dur: .whole) Note(.c, 4, dur: .whole) } } } let xml: String = score.toMusicXMLString() #expect(xml.contains(""), "glissando stop must survive the multi-voice barline split") } @Test func singleAtomGlissandoDoesNotBreakNextSpanInVoice() { // A degenerate single-note glissando (begin+end on one note) must clear // the per-voice opener so a following real glissando span is not // mis-flagged as a double-open and dropped. let score: Score = Score(bpm: 120) { Part("p") { Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando()), .endGlissando]) Note(.e, 5, dur: .quarter, spanners: [.beginGlissando(Glissando(kind: .slide))]) Note(.g, 5, dur: .quarter, spanners: [.endGlissando]) } } let xml: String = score.toMusicXMLString() // The second (real, two-note) slide span must emit its start+stop. #expect(xml.contains(""), "the span after a single-atom glissando must still close") } }