// Which file a recording is filed as. // // The rule, from the walks of 15 and 16 Sep 2026, driven here without an // audio framework or an App Group: // // * a recording is CAPTURED as linear PCM in a Core Audio File and DELIVERED // as an AAC `.m4a` encoded from it, and a capture that will not export is // delivered as it is — because the crate accepts `.caf`, and a recording // nobody can file is the worse failure. // // **What the container and the format are actually worth was measured, not // assumed.** Writing 13 s and taking the bytes mid-write, with the writer still // open — which is what a killed process leaves behind: // // pcm in caf, killed 6.341 s, and the export to .m4a works // aac in caf, killed 0.000 s, 0 packets; the export FAILS (-11800) // aac in m4a, killed no player at all // // CAF writes its `data` chunk at -1 while recording either way; what AAC also // needs is the `pakt` packet table, and that is only written at `stop()`. PCM // has no packet table to lose. The whole table, and what a two-hour meeting // costs, is in Cotton note "The phone and Mac app — how a screen is built". import CottonKit import XCTest @testable import CottonUI // MARK: - The doubles /// An export that does exactly what it is told to. private struct ScriptedExport: MeetingAudioExporting { let refuses: Bool let seen = Seen() final class Seen: @unchecked Sendable { private let lock = NSLock() private(set) var asked: [String] = [] func note(_ path: String) { lock.lock() defer { lock.unlock() } asked.append(path) } } func export( _ captured: URL, as named: String, watching: @escaping @Sendable (Double) -> Void ) async throws -> URL { seen.note(captured.path(percentEncoded: false)) if refuses { throw MeetingAudioExportFailure.wouldNotExport } watching(0.5) watching(1) return captured.deletingLastPathComponent().appendingPathComponent(named) } } // MARK: - What gets filed final class MeetingAudioDeliveryTests: XCTestCase { func testACaptureIsFiledAsTheMpegFourEncodedFromIt() async { let exporter = ScriptedExport(refuses: false) let watched = Fractions() let delivered = await MeetingAudioDelivery.prepare( "/tmp/acts/a1/audio.caf", exporting: exporter, watching: { watched.note($0) }, log: { _ in }) XCTAssertEqual(delivered.path, "/tmp/acts/a1/audio.m4a") XCTAssertEqual(delivered.mime, "audio/mp4") XCTAssertEqual(exporter.seen.asked, ["/tmp/acts/a1/audio.caf"]) XCTAssertEqual( watched.seen, [0.5, 1], "and the fraction reaches the caller, which is what the screen draws") } /// **An export that fails is not a refusal.** The capture file is a /// container the crate takes, so it is filed as it is — and the type /// follows the file rather than claiming the one that was wanted. func testACaptureThatWillNotExportIsFiledAsRecorded() async { let said = SaidLines() let delivered = await MeetingAudioDelivery.prepare( "/tmp/acts/a1/audio.caf", exporting: ScriptedExport(refuses: true), log: { said.note($0) }) XCTAssertEqual(delivered.path, "/tmp/acts/a1/audio.caf") XCTAssertEqual(delivered.mime, "audio/x-caf") XCTAssertEqual(said.lines.count, 1, "and the reason goes to the log rather than nowhere") } /// A marker written by a build that recorded straight into MPEG-4, or any /// other path that is already the delivered container: nothing is encoded /// and nothing is claimed. func testAFileThatIsAlreadyTheDeliveredContainerIsLeftAlone() async { let exporter = ScriptedExport(refuses: false) let delivered = await MeetingAudioDelivery.prepare( "/tmp/acts/a1/audio.m4a", exporting: exporter, log: { _ in }) XCTAssertEqual(delivered.path, "/tmp/acts/a1/audio.m4a") XCTAssertEqual(delivered.mime, "audio/mp4") XCTAssertEqual(exporter.seen.asked, [], "nothing was asked to encode an m4a into an m4a") } func testNoPathIsNoFileAndNoClaim() async { let delivered = await MeetingAudioDelivery.prepare( "", exporting: ScriptedExport(refuses: false), log: { _ in }) XCTAssertEqual(delivered.path, "") XCTAssertNil(delivered.mime) } } /// The fractions the caller was told, in memory. private final class Fractions: @unchecked Sendable { private let lock = NSLock() private var held: [Double] = [] func note(_ fraction: Double) { lock.lock() defer { lock.unlock() } held.append(fraction) } var seen: [Double] { lock.lock() defer { lock.unlock() } return held } } /// The log, in memory. private final class SaidLines: @unchecked Sendable { private let lock = NSLock() private var held: [String] = [] func note(_ line: String) { lock.lock() defer { lock.unlock() } held.append(line) } var lines: [String] { lock.lock() defer { lock.unlock() } return held } }