// FlowAdaptiveDownsamplerTests.swift // OSGKeyboard · Tests // // `AVAudioEngine.installTap(format:)` raises an UNCATCHABLE NSException when // the format handed to it no longer matches the input node's live format, and // that kills the whole app. The fix everywhere is the same: install with // `format: nil` and let this resampler absorb whatever rate actually arrives. // // These tests pin the behaviour that makes `format: nil` safe — converting // across a mid-stream route change — plus the numeric clamp that keeps a // degenerate route from trapping on the realtime audio thread. import AVFoundation @testable import OSGKeyboardHostSupport import XCTest final class FlowAdaptiveDownsamplerTests: XCTestCase { private let targetFormat = AVAudioFormat( commonFormat: .pcmFormatFloat32, sampleRate: 16_000, channels: 1, interleaved: false ) private func makeBuffer(sampleRate: Double, frames: AVAudioFrameCount) -> AVAudioPCMBuffer? { guard let format = AVAudioFormat( commonFormat: .pcmFormatFloat32, sampleRate: sampleRate, channels: 1, interleaved: false ), let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: frames) else { return nil } buffer.frameLength = frames // A quiet ramp: silence would also convert, but non-zero samples prove // the converter actually ran rather than handing back an empty scratch. if let channel = buffer.floatChannelData?[0] { for index in 0..