// SlotReleaseTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if os(macOS) || os(Linux) import Testing import LyrebirdSynth @testable import LyrebirdLiveCompile @MainActor @Suite("LiveSession slot release") struct SlotReleaseTests { /// A synth whose graceful `release` succeeds (records the call) — stands in /// for a gated `EnvGen` voice. private final class GatedSynth: SynthesizerBase, @unchecked Sendable { var released: Bool = false override func build() { addOutputGenerator(Out.Audio(bus: 0, source: 0.1)) } override func release(engine: SynthesisEngine) throws { released = true } } /// A synth with no gate — inherits the default `release` that throws `.noGate`. private final class GatelessSynth: SynthesizerBase, @unchecked Sendable { override func build() { addOutputGenerator(Out.Audio(bus: 0, source: 0.1)) } } private func makeStartedSession() throws -> LiveSession { let engine: SynthesisEngine = SynthesisEngine( blockSize: 64, controlBlockSize: 16, sampleRate: 48_000, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 16, numControlBuses: 16, eventQueueCapacity: lyrebirdDefaultEventQueueCapacity, driver: OfflineDriver() ) let session: LiveSession = LiveSession(engine: engine) try engine.start() return session } @Test func releaseGatedSlotTriggersReleaseAndDropsHandle() throws { let session: LiveSession = try makeStartedSession() let voice: GatedSynth = GatedSynth() try session.swapSlot("pad", synth: voice) #expect(session.slotNames.contains("pad")) let released: Bool = session.releaseSlot("pad") #expect(released) #expect(voice.released) // graceful release fired #expect(session.slotNames.contains("pad") == false) // handle dropped, name free // Safety net: the released voice moves to the tracked list so it stays // reclaimable by resetAll() / Clear Synths even if it never self-frees. #expect(session.trackedSynthCount == 1) session.resetAll() #expect(session.trackedSynthCount == 0) session.engine.stop() } @Test func nameIsReusableAfterRelease() throws { let session: LiveSession = try makeStartedSession() try session.swapSlot("pad", synth: GatedSynth()) #expect(session.releaseSlot("pad")) // The name is free again — a fresh voice claims the slot cleanly. try session.swapSlot("pad", synth: GatedSynth()) #expect(session.slotNames.contains("pad")) #expect(session.slotCount == 1) session.engine.stop() } @Test func releaseGatelessSlotFailsAndKeepsHandle() throws { let session: LiveSession = try makeStartedSession() try session.swapSlot("bass", synth: GatelessSynth()) let released: Bool = session.releaseSlot("bass") #expect(released == false) // Not orphaned — still in the slot, still stoppable with a hard clear. #expect(session.slotNames.contains("bass")) #expect(session.clearSlot("bass")) session.engine.stop() } @Test func releaseEmptySlotReturnsFalse() throws { let session: LiveSession = try makeStartedSession() #expect(session.releaseSlot("nothing-here") == false) session.engine.stop() } } #endif