// SPDX-License-Identifier: GPL-3.0-or-later import AVFoundation import Foundation import XCTest final class CoCoVideoBudgetTests: XCTestCase { private func directory() throws -> URL { let root = FileManager.default.temporaryDirectory.appendingPathComponent("CoCoVideoBudget-" + UUID().uuidString) try FileManager.default.createDirectory(at: root, withIntermediateDirectories: false) addTeardownBlock { try? FileManager.default.removeItem(at: root) } return root } private func frames(count: Int, audioFrames: Int = 735) -> [CoCoVideoFrame] { let pixels = Data(repeating: 0x40, count: 640 * 480 * 4) return (0.. = await withCheckedContinuation { c in encoder.finish { c.resume(returning: $0) } } if case .success = stopped { XCTFail("Writer failure must not be advertised as a completed movie") } XCTAssertTrue(encoder.writerIsTerminal) let drained: Result = await withCheckedContinuation { c in encoder.whenDrained { c.resume(returning: $0) } } if case .success = drained { XCTFail("Failed input cannot be acknowledged as a successful drain") } // The failure notification can precede AVFoundation's closure, but the // finish callback cannot. No polling/retry is permitted after finish. let duration = try await AVURLAsset(url: working).load(.duration).seconds XCTAssertGreaterThan(duration, 0.2, "A failed writer retains its preceding playable content") XCTAssertGreaterThan(try Data(contentsOf: working).count, 0) XCTAssertEqual(try Data(contentsOf: neighbor), original) let contents = try FileManager.default.contentsOfDirectory(at: root, includingPropertiesForKeys: nil) XCTAssertEqual(Set(contents.map(\.lastPathComponent)), ["Working.mov", "Existing.mp4"]) } }