// TempoTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation import LyrebirdSynth @testable import LyrebirdShoeGaze @MainActor @Suite("ShoeGaze Tempo") struct TempoTests { private func makeShoeGaze(duration: Double = 0.1) -> (ShoeGaze, URL) { let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: duration, numAudioBuses: 64) let shoeGaze: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop) return (shoeGaze, url) } @Test func clockDefaultsToStandardTempo() throws { let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze() defer { try? FileManager.default.removeItem(at: url) } #expect(shoeGaze.clock.bpm == ShoeGaze.defaultTempoBpm) } @Test func tapTempoAtOneSecondIntervalsSetsSixtyBpm() throws { let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze() defer { try? FileManager.default.removeItem(at: url) } // Simulate taps exactly 1s apart by manipulating the tap history directly — // real taps come from wall-clock `Date()`, so back-date synthetic timestamps // instead of sleeping the test for real seconds. let now: Date = Date() shoeGaze.tapTimestamps = [ now.addingTimeInterval(-3.0), now.addingTimeInterval(-2.0), now.addingTimeInterval(-1.0), ] shoeGaze.tapTempo() // landing "now", completing four 1s-spaced taps #expect(abs(shoeGaze.clock.bpm - 60.0) < 0.5) } @Test func singleTapDoesNotSetTempo() throws { let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze() defer { try? FileManager.default.removeItem(at: url) } let startingBpm: Double = shoeGaze.clock.bpm shoeGaze.tapTempo() #expect(shoeGaze.clock.bpm == startingBpm, "A single tap has no interval to measure — tempo must not change") } @Test func longPauseResetsTapHistory() throws { let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze() defer { try? FileManager.default.removeItem(at: url) } let now: Date = Date() // Two taps 1s apart, then a long pause before the next tap. shoeGaze.tapTimestamps = [now.addingTimeInterval(-10.0), now.addingTimeInterval(-9.0)] shoeGaze.tapTempo() #expect(shoeGaze.tapTimestamps.count == 1, "A >2s gap should discard prior taps and start a fresh sequence") } @Test func tapTempoRespectsBpmRangeViaClockPrecondition() throws { let (shoeGaze, url): (ShoeGaze, URL) = makeShoeGaze() defer { try? FileManager.default.removeItem(at: url) } let now: Date = Date() // Very fast taps (well under minTapInterval == 0.2s) are filtered out of the average. shoeGaze.tapTimestamps = [ now.addingTimeInterval(-0.15), now.addingTimeInterval(-0.05), ] let startingBpm: Double = shoeGaze.clock.bpm shoeGaze.tapTempo() // The sub-threshold interval is filtered; with no valid interval left the tempo // is left unchanged rather than jumping to an implausible value. #expect(shoeGaze.clock.bpm == startingBpm) } }