Branchwell

PulseCanvas / app / visionOS / VisionLaunchOptions.swift

1import Foundation
2
3/// Launch hooks for UI tests and recorded demos. Every hook is opt-in through launch arguments, so an ordinary launch
4/// (App Store, TestFlight, Xcode Run) behaves exactly as before.
5///
6/// -PCUITesting fresh, isolated Studio and Radio storage in a temporary folder (no autosave restore,
7/// the bundled demo soundtrack, a new timeline); the user's own files are never touched
8/// -PCAutoplay start playback once the soundtrack is ready
9/// -PCOpenImmersive open the immersive viewing space once playback starts (implies -PCAutoplay)
10/// -PCHideRoom YES|NO set the "Hide room" switch before opening (default: the app's own default)
11/// -PCRadio start in Radio mode (needs the network)
12/// -PCVisualizer <n|name> put this visualizer on the first timeline card (index into NativeEffects.names, or a
13/// case-insensitive name)
14/// -PCSeek <seconds> seek the soundtrack before playing
15/// -PCCaptureAudio <file> debug UI-test capture only: import a local fixture through the real audio path
16struct VisionLaunchOptions: Equatable {
17 var uiTesting = false
18 var autoplay = false
19 var openImmersive = false
20 var hideRoom: Bool?
21 var radio = false
22 var visualizer: String?
23 var seek: Double?
24 /// Debug-only recorded-demo sequence; applied only with isolated UI-test storage.
25 var captureScenes: [Int] = []
26 /// Optional short local soundtrack for an isolated debug capture of real export/save UI.
27 var captureAudio: URL?
28
29 static let current = VisionLaunchOptions(arguments: ProcessInfo.processInfo.arguments)
30
31 init(arguments: [String]) {
32 func value(_ flag: String) -> String? {
33 guard let index = arguments.firstIndex(of: flag), arguments.indices.contains(index + 1) else { return nil }
34 let next = arguments[index + 1]
35 return next.hasPrefix("-PC") ? nil : next
36 }
37 func flag(_ name: String) -> Bool {
38 guard arguments.contains(name) else { return false }
39 guard let next = value(name) else { return true }
40 return !["NO", "no", "false", "0"].contains(next)
41 }
42 uiTesting = flag("-PCUITesting")
43 openImmersive = flag("-PCOpenImmersive")
44 autoplay = flag("-PCAutoplay") || openImmersive
45 radio = flag("-PCRadio")
46 if let room = value("-PCHideRoom") { hideRoom = !["NO", "no", "false", "0"].contains(room) }
47 visualizer = value("-PCVisualizer")
48 seek = value("-PCSeek").flatMap(Double.init)
49 #if DEBUG
50 if uiTesting { captureScenes = value("-PCCaptureScenes")?.split(separator: ",").compactMap { Int($0) } ?? [] }
51 if uiTesting, let path = value("-PCCaptureAudio") {
52 if path.hasPrefix("/") { captureAudio = URL(fileURLWithPath: path) }
53 else if !path.isEmpty, path != ".", path != "..", !path.contains("/"), !path.contains("\\") {
54 // Simulator installation can relocate the container between capture runs.
55 captureAudio = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent(path)
56 }
57 }
58 #endif
59 }
60
61 /// The index of the requested visualizer in `names`, by number or by case-insensitive name.
62 func visualizerIndex(in names: [String]) -> Int? {
63 guard let visualizer else { return nil }
64 if let index = Int(visualizer), names.indices.contains(index) { return index }
65 return names.firstIndex { $0.compare(visualizer, options: .caseInsensitive) == .orderedSame }
66 }
67
68 /// A per-launch temporary folder for `-PCUITesting`, so tests start clean and never read or write user data.
69 static func isolatedDirectory(_ name: String) -> URL {
70 let url = FileManager.default.temporaryDirectory
71 .appendingPathComponent("PulseCanvasUITesting-\(ProcessInfo.processInfo.processIdentifier)", isDirectory: true)
72 .appendingPathComponent(name, isDirectory: true)
73 try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
74 return url
75 }
76}
77
78extension VisionSession {
79 /// The app's session: normal storage, or fresh isolated storage under `-PCUITesting`.
80 static func forLaunch(_ launch: VisionLaunchOptions = .current) -> VisionSession {
81 guard launch.uiTesting else { return VisionSession() }
82 let defaults = UserDefaults(suiteName: "PulseCanvasUITesting-\(ProcessInfo.processInfo.processIdentifier)")!
83 return VisionSession(editor: VisionStudio(directory: VisionLaunchOptions.isolatedDirectory("Studio")),
84 radio: VisionRadio(directory: VisionLaunchOptions.isolatedDirectory("Radio"), defaults: defaults))
85 }
86}
87
88@MainActor extension VisionLaunchOptions {
89 /// Use the actual Studio assignment/settings paths for reproducible simulator recordings.
90 /// Ordinary launches and release builds never read or apply this capture-only sequence.
91 func applyCaptureSequence(to studio: VisionStudio) {
92 #if DEBUG
93 guard uiTesting, !captureScenes.isEmpty, captureScenes.count <= 64,
94 captureScenes.allSatisfy({ NativeEffects.activeIDs.contains($0) }) else { return }
95 studio.applyCommonSettings(VisionCommonSettings(bars: 1, chaos: 0.85, zoomSpeed: 2,
96 motionSpeed: 1.3, strobeEnabled: false))
97 for (index, clip) in studio.project.clips.enumerated() {
98 studio.assign(captureScenes[index % captureScenes.count], to: clip.id)
99 }
100 for segment in studio.segments {
101 NSLog("CAPTURE-SEGMENT %.3f %.3f %d %@", segment.start, segment.end, segment.scene, NativeEffects.names[segment.scene])
102 }
103 #endif
104 }
105}