// NoteDurationTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable import LyrebirdSynth
@MainActor
@Suite("NoteDuration — double whole + British names")
struct NoteDurationTests {
@Test func doubleWholeIsEightBeats() {
#expect(NoteDuration.doubleWhole.beats == 8.0)
#expect(NoteDuration.doubleWhole.asFraction == Fraction(num: 8, den: 1))
}
@Test func britishNamesAliasAmericanDurations() {
#expect(NoteDuration.breve == .doubleWhole)
#expect(NoteDuration.semibreve == .whole)
#expect(NoteDuration.minim == .half)
#expect(NoteDuration.crotchet == .quarter)
#expect(NoteDuration.quaver == .eighth)
#expect(NoteDuration.semiquaver == .sixteenth)
#expect(NoteDuration.demisemiquaver == .thirtySecond)
#expect(NoteDuration.hemidemisemiquaver == .sixtyFourth)
}
@Test func britishNamesCarryCorrectBeats() {
#expect(NoteDuration.breve.beats == 8.0)
#expect(NoteDuration.semibreve.beats == 4.0)
#expect(NoteDuration.minim.beats == 2.0)
#expect(NoteDuration.crotchet.beats == 1.0)
#expect(NoteDuration.quaver.beats == 0.5)
#expect(NoteDuration.semiquaver.beats == 0.25)
#expect(NoteDuration.demisemiquaver.beats == 0.125)
#expect(NoteDuration.hemidemisemiquaver.beats == 0.0625)
}
@Test func doubleWholeEncodesAsBreveType() {
// An 8-beat bar (8/4) holds a whole breve, which the encoder maps to
// breve.
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(8, 4)) {
Part("p") {
Note(.named(.c, 4), dur: .doubleWhole)
}
}
#expect(score.toMusicXMLString().contains("breve"))
}
@Test func doubleWholeInFourFourSplitsToTiedWholes() {
// A breve (8 beats) does not fit a 4/4 bar — it splits into two tied
// wholes. Each tied piece must read whole, never breve.
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
Part("p") {
Note(.named(.c, 4), dur: .doubleWhole)
}
}
let xml: String = score.toMusicXMLString()
#expect(xml.components(separatedBy: "whole").count - 1 == 2)
#expect(!xml.contains("breve"))
}
@Test func doubleWholeMultiVoiceSplitsToTiedWholes() {
// Multi-voice path: each voice's breve splits into two tied wholes. The
// barline continuation piece must be typed by its leftover (whole), not by
// the original note value (breve) — a regression guard for the multi-voice
// leftover-atom rawDuration fix.
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
Part("p") {
Voice(1) { Note(.named(.c, 5), dur: .doubleWhole) }
Voice(2) { Note(.named(.c, 4), dur: .doubleWhole) }
}
}
let xml: String = score.toMusicXMLString()
#expect(xml.components(separatedBy: "whole").count - 1 == 4)
#expect(!xml.contains("breve"))
}
@Test func doubleWholeRoundTripsByDuration() throws {
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(8, 4)) {
Part("p") {
Note(.named(.c, 4), dur: .doubleWhole)
}
}
let rt: Score = try Score.fromMusicXML(data: score.toMusicXMLData())
// One sounding event of 8 beats survives the round-trip.
let events: [ScoreEvent] = rt.allEvents.filter { !$0.isRest }
#expect(events.count == 1)
#expect(events.first?.duration == 8.0)
}
}