Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/LiveInputBoardTests.swift
T
2026-06-21 12:25:18 -07:00

132 lines
5.2 KiB
Swift

// LiveInputBoardTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
/// Tests for the `.liveInput` path in `ShoeGaze.addBoard()`.
///
/// The NRT driver does not supply real hardware input samples, so the pedal chain
/// reads silence. These tests assert correct setup: no thrown error, the expected
/// number of nodes added to the engine, and the pass-through board structure.
@MainActor
@Suite("LiveInputBoard")
struct LiveInputBoardTests {
/// `addBoard()` with `.liveInput` and `numInputChannels > 0` must not throw and
/// must add a `HardwareInputPassThrough` that is tracked and torn down correctly.
@Test func liveInputBoardBuildsWithoutThrowing() throws {
let numOutputChannels: Int = 2
let numInputChannels: Int = 1
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.1,
numOutputChannels: numOutputChannels,
numInputChannels: numInputChannels,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine, inputSource: .liveInput(channel: 0))
// addBoard() must succeed — no throw even though the NRT driver produces
// silence on the input bus.
let board: PedalBoard = try sg.addBoard()
// The board's head bus index must be distinct from the hardware input bus
// (the pass-through copies from numOutputChannels to board.headBus.index).
let hardwareInputBusIndex: Int = numOutputChannels
#expect(board.headBus.index != hardwareInputBusIndex,
"board head bus should be a user-space bus, not the raw hardware input bus")
// headInputNode must be set so PedalBoard.add(_:) can chain the first pedal
// after the pass-through automatically.
#expect(board.headInputNode != nil,
"headInputNode should be set to the HardwareInputPassThrough")
// Add a pedal and finalize to verify the full signal chain builds without error.
let pedal: OverdrivePedal = OverdrivePedal()
try board.add(pedal)
try sg.finalizeBoard(board)
try engine.start()
// Teardown must not crash or assert.
sg.teardown()
}
/// `addBoard()` with `.liveInput` and `numInputChannels == 0` must throw
/// `ShoeGazeError.liveInputRequiresInputChannels` rather than trapping.
@Test func liveInputBoardThrowsWhenNoInputChannels() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.1,
numInputChannels: 0
)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine, inputSource: .liveInput(channel: 0))
#expect(throws: ShoeGazeError.liveInputRequiresInputChannels) {
try sg.addBoard()
}
}
/// `liveInputBusIndex` maps a hardware input channel to bus `numOutputChannels + channel`.
@Test func liveInputBusIndexMapsChannelToBus() throws {
let numOutputChannels: Int = 2
let numInputChannels: Int = 4
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.05,
numOutputChannels: numOutputChannels,
numInputChannels: numInputChannels,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: url) }
let channel: Int = 2
let busIndex: Int = try liveInputBusIndex(channel: channel, engine: engine)
#expect(busIndex == numOutputChannels + channel,
"input channel \(channel) should map to bus \(numOutputChannels + channel)")
}
/// `addBoard()` with `.liveInput(channel:)` on a valid non-zero channel builds without throwing.
@Test func liveInputNonZeroChannelBuildsWithoutThrowing() throws {
let numInputChannels: Int = 4
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.1,
numInputChannels: numInputChannels,
numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine, inputSource: .liveInput(channel: 2))
let board: PedalBoard = try sg.addBoard()
try board.add(OverdrivePedal())
try sg.finalizeBoard(board)
try engine.start()
sg.teardown()
}
/// `addBoard()` with a `.liveInput` channel `>= numInputChannels` throws
/// `ShoeGazeError.inputChannelOutOfRange`.
@Test func liveInputChannelOutOfRangeThrows() throws {
let numInputChannels: Int = 1
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.05,
numInputChannels: numInputChannels
)
defer { try? FileManager.default.removeItem(at: url) }
let outOfRangeChannel: Int = 3
let sg: ShoeGaze = ShoeGaze(engine: engine, inputSource: .liveInput(channel: outOfRangeChannel))
#expect(throws: ShoeGazeError.inputChannelOutOfRange(requested: outOfRangeChannel, available: numInputChannels)) {
try sg.addBoard()
}
}
}