Branchwell

PulseCanvas / app · pulsecanvas.ai / scripts / VisionVideoSaveSmoke.swift

Raw 3.0 KB on 87dd19288bbd69b69b6ac6e250fb5d18bfebb877
1import Foundation
2
3@main struct VisionVideoSaveSmoke {
4 static func main() throws {
5 let root = FileManager.default.temporaryDirectory.appendingPathComponent("Vision-save-test-\(UUID().uuidString)")
6 defer { try? FileManager.default.removeItem(at: root) }
7 try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
8 let cache = root.appendingPathComponent("Cache")
9 let work = root.appendingPathComponent("Work")
10 try FileManager.default.createDirectory(at: work, withIntermediateDirectories: true)
11 let source = work.appendingPathComponent("PulseCanvas Video.mp4")
12 let bytes = Data(repeating: 0x71, count: 2_000_000)
13 try bytes.write(to: source)
14 let retained = try VisionVideoFile.retain(source, in: cache)
15 try FileManager.default.removeItem(at: work)
16 // The original render workspace can disappear while the picker is open.
17 precondition(VisionVideoFile.recover(in: cache) == retained && (try? Data(contentsOf: retained)) == bytes)
18 let sameFile = try VisionVideoFile.retain(retained, in: cache)
19 precondition(sameFile == retained)
20 let replacement = root.appendingPathComponent("Replacement.mp4")
21 let second = Data(repeating: 0x62, count: 300_000)
22 try second.write(to: replacement)
23 _ = try VisionVideoFile.retain(replacement, in: cache)
24 precondition((try? Data(contentsOf: retained)) == second)
25 let empty = root.appendingPathComponent("Empty.mp4"); try Data().write(to: empty)
26 do { _ = try VisionVideoFile.retain(empty, in: cache); fatalError("Empty movie accepted") } catch {}
27 precondition((try? Data(contentsOf: retained)) == second, "Failed render must not replace the last completed movie")
28 do { try VisionVideoFile.validate(root.appendingPathComponent("Missing.mp4")); fatalError("Missing movie accepted") } catch {}
29 do { try VisionVideoFile.validate(root); fatalError("Directory accepted") } catch {}
30 let legacy = root.appendingPathComponent("Legacy")
31 let oldWork = legacy.appendingPathComponent("Vision-video-previous-build")
32 try FileManager.default.createDirectory(at: oldWork, withIntermediateDirectories: true)
33 try bytes.write(to: oldWork.appendingPathComponent("PulseCanvas Video.mp4"))
34 let unrelated = legacy.appendingPathComponent("Unrelated.mp4"); try second.write(to: unrelated)
35 let recoveredCache = root.appendingPathComponent("Recovered")
36 let recovered = VisionVideoFile.recoverPreviousRender(in: legacy, cache: recoveredCache)!
37 precondition((try? Data(contentsOf: recovered)) == bytes)
38 precondition(FileManager.default.fileExists(atPath: unrelated.path))
39 precondition(VisionVideoFile.recoverPreviousRender(in: legacy, cache: recoveredCache) == recovered)
40 print("PASS save handoff: on-disk movie survives workspace dismissal, cancellation/retry and recovery; replacement is atomic; missing/empty inputs preserve completed movie; build-4 recovery is scoped")
41 }
42}