PulseCanvas / app · pulsecanvas.ai / scripts / ExportFeedbackSmoke.swift
| 1 | import AppKit |
| 2 | @preconcurrency import AVFoundation |
| 3 | import Combine |
| 4 | |
| 5 | @main struct ExportFeedbackSmoke { |
| 6 | @MainActor static func main() async throws { |
| 7 | _ = NSApplication.shared |
| 8 | let root = URL(fileURLWithPath: ".build/export-feedback-validation") |
| 9 | try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) |
| 10 | let audio = root.appendingPathComponent("tone.wav") |
| 11 | let format = AVAudioFormat(standardFormatWithSampleRate: 44_100, channels: 2)! |
| 12 | let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 44_100)! |
| 13 | buffer.frameLength = 44_100 |
| 14 | for i in 0..<44_100 { |
| 15 | let sample = Float(sin(Double(i) * 2 * .pi * 440 / 44_100) * 0.2) |
| 16 | buffer.floatChannelData![0][i] = sample; buffer.floatChannelData![1][i] = sample |
| 17 | } |
| 18 | var settings = format.settings; settings[AVLinearPCMIsNonInterleaved] = false |
| 19 | var file: AVAudioFile? = try AVAudioFile(forWriting: audio, settings: settings) |
| 20 | try file?.write(from: buffer) |
| 21 | file = nil |
| 22 | let state = RenderState(); state.scene = 22 |
| 23 | let exporter = SocialExportController() |
| 24 | exporter.fps = 24 |
| 25 | var previews = 0, counts: [Int] = [], statuses: [String] = [] |
| 26 | let previewObserver = exporter.$framePreview.sink { image in |
| 27 | if let image { precondition(max(image.width, image.height) <= 480 && min(image.width, image.height) > 0); previews += 1 } |
| 28 | } |
| 29 | let countObserver = exporter.$renderedFrames.sink { counts.append($0) } |
| 30 | let statusObserver = exporter.$status.sink { statuses.append($0) } |
| 31 | var successfulSaveIDs = Set<UUID>() |
| 32 | for (aspect, resolution) in [(SocialAspect.wide,720),(.phone,2160),(.wide,2160)] { |
| 33 | exporter.aspect = aspect; exporter.resolution = resolution |
| 34 | previews = 0; counts = []; statuses = [] |
| 35 | if aspect == .wide && resolution == 2160 { state.scene = 23 } |
| 36 | let began = ProcessInfo.processInfo.systemUptime |
| 37 | var estimates: [Double] = [] |
| 38 | let estimateObserver = exporter.$estimatedSecondsRemaining.sink { if let value = $0 { precondition(value.isFinite && value > 0); estimates.append(value) } } |
| 39 | let output = root.appendingPathComponent("\(resolution)-\(aspect.rawValue).mp4") |
| 40 | await exporter.export(audioURL: audio, render: state, to: output) |
| 41 | precondition(exporter.exportedURL == output && !exporter.isExporting && exporter.progress == 1, |
| 42 | "Expected an export at \(output.path). Got \(exporter.status).") |
| 43 | precondition(exporter.successfulSaveID != nil && successfulSaveIDs.insert(exporter.successfulSaveID!).inserted) |
| 44 | precondition(exporter.renderedFrames == 24 && exporter.totalFrames == 24 && previews >= 1) |
| 45 | precondition(counts.first == 0 && counts.last == 24 && zip(counts,counts.dropFirst()).allSatisfy { $0 <= $1 }) |
| 46 | precondition(statuses.contains("Preparing export…") && statuses.contains("Finishing video…") && statuses.contains("Adding soundtrack…")) |
| 47 | let duration = try await AVURLAsset(url: output).load(.duration).seconds |
| 48 | precondition(abs(duration - 1) < 0.01) |
| 49 | estimateObserver.cancel() |
| 50 | precondition(exporter.estimatedSecondsRemaining == nil) |
| 51 | print("TIMING: \(resolution) \(aspect.rawValue) scene \(state.scene): \(ProcessInfo.processInfo.systemUptime - began)s, \(estimates.count) estimates") |
| 52 | print("PASS: \(resolution) \(aspect.rawValue) exposes actual small frame previews, exact 24-frame progress, finishing/audio stages, saved success and complete video") |
| 53 | } |
| 54 | let existing = root.appendingPathComponent("cancelled.mp4") |
| 55 | let contents = Data("preserve existing file".utf8) |
| 56 | try contents.write(to: existing) |
| 57 | exporter.resolution = 720 |
| 58 | var requestedCancel = false |
| 59 | let cancellationObserver = exporter.$progress.sink { progress in |
| 60 | if exporter.isExporting && progress > 0.02 && !requestedCancel { requestedCancel = true; exporter.cancel() } |
| 61 | } |
| 62 | await exporter.export(audioURL: nil, render: state, to: existing) |
| 63 | precondition(successfulSaveIDs.contains(exporter.successfulSaveID!)) |
| 64 | let retained = try Data(contentsOf: existing) |
| 65 | precondition(requestedCancel && exporter.exportedURL == nil && exporter.status == "Export cancelled" && !exporter.isExporting && retained == contents) |
| 66 | precondition(exporter.totalFrames == 360 && exporter.renderedFrames < 360) |
| 67 | cancellationObserver.cancel() |
| 68 | await exporter.export(audioURL: root.appendingPathComponent("missing.wav"), render: state, to: root.appendingPathComponent("failed.mp4")) |
| 69 | precondition(successfulSaveIDs.contains(exporter.successfulSaveID!)) |
| 70 | precondition(exporter.exportedURL == nil && exporter.renderedFrames == 0 && exporter.totalFrames == 0 && exporter.framePreview == nil && exporter.status.hasPrefix("Export failed:")) |
| 71 | withExtendedLifetime([previewObserver,countObserver,statusObserver]) {} |
| 72 | print("PASS: cancelled/failed exports reset feedback and never signal success; cancellation preserves the destination") |
| 73 | } |
| 74 | } |