import AVFoundationimport XCTest@testable importCottonUI/// The tap→first audio buffer figure, cold and warm, on this Mac's own/// on-device model through the one recogniser that ships (a recording plays in/// where the microphone would). A measurement, not a guard: it runs only when/// `COTTON_LISTEN_PROBE` is set, because a machine with no dictation model has/// nothing to measure.@MainActorfinalclassListenLatencyProbe: XCTestCase {func testTapToFirstAudioBufferColdAndWarm() asyncthrows {guardProcessInfo.processInfo.environment["COTTON_LISTEN_PROBE"] != nilelse {throw XCTSkip("set COTTON_LISTEN_PROBE to measure") }let file = FileManager.default.temporaryDirectory.appendingPathComponent("listen-probe.caf")let format = AVAudioFormat(standardFormatWithSampleRate: 16_000, channels: 1)!let out = try AVAudioFile(forWriting: file, settings: format.settings)let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 16_000)! buffer.frameLength = 16_000try out.write(from: buffer)let hints = DictationHints(language: "en")let cold = DictationModel.live(audio: .file(file))let coldMs = tryawait firstBuffer(cold, hints: hints) cold.release()let warm = DictationModel.live(audio: .file(file)) warm.prepare(hints: hints)tryawaitTask.sleep(for: .seconds(8))let warmMs = tryawait firstBuffer(warm, hints: hints) warm.release()// A second tap on the same recogniser, warmed again after the first.tryawaitTask.sleep(for: .seconds(8))let againMs = tryawait firstBuffer(warm, hints: hints) warm.release() print("listen-probe: tap→first audio buffer cold \(coldMs) ms, warm \(warmMs) ms, second tap \(againMs) ms") }privatefunc firstBuffer(_ model: DictationModel, hints: DictationHints) asyncthrows -> Int {let tapped = Date()iflet refusal = await model.start(prefix: "", hints: hints) { throw XCTSkip("refused: \(refusal)") }while model.waveform.isEmpty {tryawaitTask.sleep(for: .milliseconds(2))guardDate().timeIntervalSince(tapped) < 120else { throw XCTSkip("no model on this Mac") } }returnInt(Date().timeIntervalSince(tapped) * 1000) }}