// ALSAMIDITests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT // Linux-only: the ALSA-sequencer `MIDI` backend exists where CALSA does and // CoreMIDI does not. These run headless — a CI container may or may not expose // the kernel sequencer — so they cover the parts that need no controller: the // handler registry, the not-started guards, and safe teardown, plus a tolerant // start/stop that accepts either outcome. Receiving from real hardware is the // deferred acceptance check (see LINUX_PLAN.md). The file also guards that the // backend is actually compiled in: if CALSA stops resolving, it vanishes and // every other test still passes. #if !canImport(CoreMIDI) && canImport(CALSA) import Testing @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("ALSA MIDI (headless)") struct ALSAMIDITests { private func makeEngine() -> SynthesisEngine { SynthesisEngine( blockSize: 64, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0, driver: OfflineDriver() ) } @Test func addHandlerReturnsDistinctTokensAndRemoveIsSafe() { let midi: MIDI = MIDI(engine: makeEngine()) let a: MIDIHandlerToken = midi.addHandler(name: "a") { _, _ in } let b: MIDIHandlerToken = midi.addHandler(name: "b") { _, _ in } #expect(a != b) midi.removeHandler(a) midi.removeHandler(b) midi.removeHandler(a) // removing twice is a no-op, not a crash } @Test func sendBeforeStartThrowsNotStarted() { let midi: MIDI = MIDI(engine: makeEngine()) let dest: MIDIDestination = MIDIDestination(name: "x", client: 128, port: 0) #expect(throws: MIDIError.notStarted) { try midi.send(.noteOn(channel: 0, note: 60, velocity: 100), to: dest) } #expect(throws: MIDIError.notStarted) { try midi.sendVirtual(.noteOn(channel: 0, note: 60, velocity: 100)) } #expect(throws: MIDIError.notStarted) { try midi.createVirtualOutput(name: "out") } } @Test func stopBeforeStartAndRepeatedStopAreSafe() { let midi: MIDI = MIDI(engine: makeEngine()) midi.stop() midi.stop() } @Test func sourcesAreEmptyBeforeStart() { let midi: MIDI = MIDI(engine: makeEngine()) #expect(midi.sources.isEmpty) #expect(midi.destinations.isEmpty) } @Test func startThenStopEitherOpensOrFailsCleanly() { // The kernel sequencer may be absent in a container. Success must leave // a startable/stoppable client; failure must be a setup error, not a // crash, and must leave the client re-startable. let midi: MIDI = MIDI(engine: makeEngine()) do { try midi.start() // Opened: a second start is rejected, and stop tears down cleanly. #expect(throws: MIDIError.alreadyStarted) { try midi.start() } midi.stop() // After a clean stop it can start again (or fail cleanly again). do { try midi.start(); midi.stop() } catch is MIDIError {} catch { Issue.record("unexpected \(error)") } } catch let error as MIDIError { guard case .clientCreateFailed = error else { Issue.record("expected clientCreateFailed when the sequencer is absent, got \(error)") return } // A failed start must not have latched isStarted — retry is allowed. #expect(throws: MIDIError.self) { try midi.start() } } catch { Issue.record("unexpected error type \(error)") } } } #endif