// MaxEventsPerBlockTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth /// Verifies the configurable per-block scheduling cap governs the future-event /// look-ahead depth carried across block boundaries. @MainActor @Suite("maxEventsPerBlock look-ahead cap") struct MaxEventsPerBlockTests { private static let blockSize: Int = 64 // Fire target well past block 0 so every scheduled event lands in carry-over first. private static let targetBlock: Int = 8 private static let scheduledCount: Int = 300 /// Schedule `scheduledCount` synths at a single future time, then run blocks until it /// passes. Returns how many actually activated. private func activatedCount(maxEventsPerBlock: Int) throws -> Int { let engine: SynthesisEngine = makeTestEngine( blockSize: Self.blockSize, eventQueueCapacity: 1024, maxEventsPerBlock: maxEventsPerBlock ) try engine.start() defer { engine.stop() } let targetTime: SampleTime = SampleTime(Self.targetBlock * Self.blockSize) for _ in 0.. default cap 256 → only the cap survives carry-over. let activated: Int = try activatedCount(maxEventsPerBlock: lyrebirdDefaultMaxEventsPerBlock) #expect(activated == lyrebirdDefaultMaxEventsPerBlock) } @Test func raisedCapKeepsAllLookAhead() throws { // Cap 512 > 300 scheduled → every event survives carry-over and fires. let activated: Int = try activatedCount(maxEventsPerBlock: 512) #expect(activated == Self.scheduledCount) } @Test func exceedingCapIncrementsDropCounter() throws { let engine: SynthesisEngine = makeTestEngine( blockSize: Self.blockSize, eventQueueCapacity: 1024, maxEventsPerBlock: lyrebirdDefaultMaxEventsPerBlock ) try engine.start() defer { engine.stop() } #expect(engine.perBlockEventDropCount == 0) let targetTime: SampleTime = SampleTime(Self.targetBlock * Self.blockSize) for _ in 0..