// NoteDisplayTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("NoteDisplay — per-note notation overrides") struct NoteDisplayTests { // MARK: - 1. Nil / no-override path — byte-identical output @Test func nilDisplayProducesNoByteDifference() throws { // A note with no display parameter must produce byte-identical MusicXML // to the same note with display: nil (the default). let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter) } } let scoreWithNil: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: nil) } } let xml: String = try score.toMusicXMLString() let xmlNil: String = try scoreWithNil.toMusicXMLString() #expect(xml == xmlNil) // Also confirm none of the display elements are present. #expect(!xml.contains("")) #expect(!xml.contains("")) #expect(!xml.contains("size=\"")) } @Test func allDefaultsNoteDisplayProducesNoByteDifference() throws { // NoteDisplay() with all defaults is a no-op when hasOverrides == false. let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter) } } let scoreWithDefault: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay()) } } let xml: String = try score.toMusicXMLString() let xmlDefault: String = try scoreWithDefault.toMusicXMLString() #expect(xml == xmlDefault) } // MARK: - 2. Notehead shapes → VALUE @Test func noteheadXEmitsCorrectElement() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x)) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("x")) } @Test func noteheadDiamondEmitsCorrectElement() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .diamond)) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("diamond")) } @Test func noteheadNoneEmitsHiddenNotehead() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .hidden)) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("none")) } @Test func noteheadCircleXEmitsHyphenatedValue() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .circleX)) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("circle-x")) } @Test func noteheadOtherClusterPassesThroughVerbatim() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .other("cluster"))) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("cluster")) } @Test func noteheadOtherIsXMLEscaped() throws { // .other with reserved XML characters should be escaped. let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .other(""))) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("<test>")) #expect(!xml.contains("")) } // MARK: - 3. NoteSize → @Test func sizeNilProducesNoByteDifference() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter) } } let scoreWithNilSize: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(size: nil)) } } let xml: String = try score.toMusicXMLString() let xmlNilSize: String = try scoreWithNilSize.toMusicXMLString() #expect(xml == xmlNilSize) #expect(!xml.contains("size=")) } @Test func sizeNormalProducesNoByteDifference() throws { // size: .normal is the same as nil — musicXMLTypeSize returns nil, so no size= attribute. // NoteDisplay(size: .normal).hasOverrides must be false. let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter) } } let scoreWithNormal: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(size: .normal)) } } let displayNormal: NoteDisplay = NoteDisplay(size: .normal) #expect(!displayNormal.hasOverrides) let xml: String = try score.toMusicXMLString() let xmlNormal: String = try scoreWithNormal.toMusicXMLString() #expect(xml == xmlNormal) } @Test func sizeCueEmitsSizeAttributeOnType() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .eighth, display: NoteDisplay(size: .cue)) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("eighth")) #expect(!xml.contains("eighth")) } @Test func sizeLargeEmitsSizeAttributeOnType() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(size: .large)) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("quarter")) } // MARK: - 4. hideStem → none @Test func hideStemEmitsStemNone() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(hideStem: true)) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("none")) } @Test func hideStemFalseProducesNoByteDifference() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter) } } let scoreNoStem: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(hideStem: false)) } } let xml: String = try score.toMusicXMLString() let xmlNoStem: String = try scoreNoStem.toMusicXMLString() #expect(xml == xmlNoStem) #expect(!xml.contains("")) } @Test func hideStemAppearsAfterTimeModificationBeforeStaff() throws { // Verify the MusicXML child-element order: time-modification → stem → notehead → staff. // Use a quarter note (no time-modification) to keep the test simple — we verify // that appears BEFORE any and the two appear within the note block. let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x, hideStem: true)) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("none")) #expect(xml.contains("x")) // stem must appear before notehead in the output string. let stemRange: Range? = xml.range(of: "none") let noteheadRange: Range? = xml.range(of: "x") #expect(stemRange != nil) #expect(noteheadRange != nil) if let stemR: Range = stemRange, let nhR: Range = noteheadRange { #expect(stemR.lowerBound < nhR.lowerBound) } } // MARK: - 5. hideBeam — breaks a beam run at the marked note @Test func hideBeamBreaksBeamRun() throws { // Four eighth notes in a row normally form one beam group (begin/continue/continue/end). // Marking the SECOND note with hideBeam should break the run so the second note // carries no element and the surrounding run is split. let scoreBeamed: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .eighth) Note(.d, 5, dur: .eighth) Note(.e, 5, dur: .eighth) Note(.f, 5, dur: .eighth) } } let scoreHideBeam: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .eighth) Note(.d, 5, dur: .eighth, display: NoteDisplay(hideBeam: true)) Note(.e, 5, dur: .eighth) Note(.f, 5, dur: .eighth) } } let xmlBeamed: String = try scoreBeamed.toMusicXMLString() let xmlHide: String = try scoreHideBeam.toMusicXMLString() // The beamed version has exactly 4 elements (begin/continue/continue/end). let beamedCount: Int = xmlBeamed.components(separatedBy: "").count - 1 #expect(beamedCount == 4) // The hide-beam version has fewer elements because the run broke. let hideCount: Int = xmlHide.components(separatedBy: "").count - 1 #expect(hideCount < beamedCount) // The hide-beam version should still contain some beams for the intact sub-runs. #expect(hideCount > 0) } @Test func hideBeamFalseProducesNoByteDifference() throws { let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .eighth) Note(.d, 5, dur: .eighth) } } let scoreNoHide: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .eighth) Note(.d, 5, dur: .eighth, display: NoteDisplay(hideBeam: false)) } } let xml: String = try score.toMusicXMLString() let xmlNoHide: String = try scoreNoHide.toMusicXMLString() #expect(xml == xmlNoHide) } // MARK: - 6. display composes with spanners @Test func displayComposesWithSpanners() throws { // A note with BOTH spanners: and display: should emit both the notehead // override AND the slur markup. let score: Score = Score(bpm: 120) { Part("test") { Note(.c, 5, dur: .quarter, spanners: [.beginSlur], display: NoteDisplay(notehead: .diamond)) Note(.e, 5, dur: .quarter, spanners: [.endSlur]) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("diamond")) #expect(xml.contains("")) #expect(xml.contains("")) } @Test func displayComposesWithSlurBlock() throws { // A note wrapped in Slur{} AND a display should emit both. let score: Score = Score(bpm: 120) { Part("test") { Slur { Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x)) Note(.e, 5, dur: .quarter) } } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("x")) #expect(xml.contains("")) } // MARK: - 7. flatten transparency @Test func displayedFlattenIsTransparent() { // A .displayed note must produce the SAME ScoreEvent list as the unwrapped note. let part: ScorePart = ScorePart( name: "test", elements: [ Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x, hideStem: true)) ] ) let plainPart: ScorePart = ScorePart( name: "test", elements: [ Note(.c, 5, dur: .quarter) ] ) let events: [ScoreEvent] = part.flatten() let plainEvents: [ScoreEvent] = plainPart.flatten() #expect(events.count == plainEvents.count) #expect(events.count == 1) #expect(events[0].beat == plainEvents[0].beat) #expect(events[0].duration == plainEvents[0].duration) #expect(events[0].pitch.midiNote == plainEvents[0].pitch.midiNote) } @Test func displayedChordFlattenIsTransparent() { // A .displayed chord must produce the same ScoreEvent list as the unwrapped chord. let part: ScorePart = ScorePart( name: "test", elements: [ Chord([(.c, 5), (.e, 5)], dur: .half, display: NoteDisplay(notehead: .diamond)) ] ) let plainPart: ScorePart = ScorePart( name: "test", elements: [ Chord([(.c, 5), (.e, 5)], dur: .half) ] ) let events: [ScoreEvent] = part.flatten() let plainEvents: [ScoreEvent] = plainPart.flatten() #expect(events.count == plainEvents.count) #expect(events.count == 2) } // MARK: - 8. Rests ignore display @Test func restIgnoresNoteheadAndStem() throws { // A rest followed by a note with display. Verify the rest carries no or . let score: Score = Score(bpm: 120) { Part("test") { Rest(dur: .quarter) Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x, hideStem: true)) } } let xml: String = try score.toMusicXMLString() // The note has the overrides; the rest should not. #expect(xml.contains("x")) #expect(xml.contains("none")) // Verify: a plain Rest does not produce even when the part contains // an atom with a notehead override. The rest's own XML block must not contain these. // Split by blocks and check the rest block specifically. let noteBlocks: [Substring] = xml.split(separator: "\n").filter { $0.contains("x").count - 1 #expect(count == 2) } @Test func chordHideStemEmitsStemNone() throws { let score: Score = Score(bpm: 120) { Part("test") { Chord([(.c, 5), (.e, 5)], dur: .quarter, display: NoteDisplay(hideStem: true)) } } let xml: String = try score.toMusicXMLString() #expect(xml.contains("none")) } // MARK: - 11. Barline-split inherits display @Test func barlineSplitNoteInheritsDisplay() throws { // A note that crosses a barline is split into two tied pieces; both pieces // should carry the display override (e.g. x). let score: Score = Score(bpm: 120, timeSignature: TimeSignature(3, 4)) { Part("test") { // A dotted-half that fits in one bar, plus a quarter that wraps into bar 2. // Use a whole note which crosses the 3/4 barline: // 4 beats in a 3/4 bar → splits into dotted-half + quarter. Note(.c, 5, dur: .whole, display: NoteDisplay(notehead: .diamond)) } } let xml: String = try score.toMusicXMLString() // The whole note splits into exactly two tied pieces (dotted-half + quarter); // both must carry the diamond notehead. let count: Int = xml.components(separatedBy: "diamond").count - 1 #expect(count == 2) } @Test func barlineSplitDisplayInheritedOnMultiVoicePath() throws { // The multi-voice encoder path reconstructs a leftover continuation atom // for the part of a note past the barline. That reconstruction must carry // the display override forward (regression: it previously reset to default, // dropping the notehead on the continuation piece). let score: Score = Score(bpm: 120, timeSignature: TimeSignature(3, 4)) { Part("test") { Voice(1) { Note(.c, 5, dur: .whole, display: NoteDisplay(notehead: .diamond)) } Voice(2) { Note(.e, 4, dur: .whole) } } } let xml: String = try score.toMusicXMLString() // Voice 1's whole note splits into two tied pieces across the 3/4 barline; // both pieces must keep the diamond notehead. let count: Int = xml.components(separatedBy: "diamond").count - 1 #expect(count == 2) } }