PulseCanvas / app · pulsecanvas.ai / scripts / VisionProjectSmoke.swift
| 1 | import Foundation |
| 2 | |
| 3 | @main struct VisionProjectSmoke { |
| 4 | static func main() throws { |
| 5 | let scenes = NativeEffects.activeIDs |
| 6 | precondition(scenes.count == 342) |
| 7 | for scene in scenes { |
| 8 | precondition(VisionLook(scene: scene).isValid, "Invalid defaults: \(scene)") |
| 9 | precondition(NativeEffects.category(for: scene) != "Other native") |
| 10 | } |
| 11 | var project = VisionProject() |
| 12 | project.clips = [VisionClip(scene: scenes[0], bars: 4), VisionClip(scene: scenes[1], bars: 1), |
| 13 | VisionClip(scene: scenes[0], bars: 2)] |
| 14 | project.looks[scenes[0]] = VisionLook(scene: scenes[0]) |
| 15 | project.looks[scenes[0]]?.chaos = 0.82 |
| 16 | project.beatBoundaries = (0...48).map { Double($0) * 0.5 } |
| 17 | let saved = try JSONEncoder().encode(project.validated()) |
| 18 | let loaded = try JSONDecoder().decode(VisionProject.self, from: saved).validated() |
| 19 | precondition(loaded.clips == project.clips && loaded.looks == project.looks) |
| 20 | precondition(loaded.loopPlayback) |
| 21 | var legacy = try JSONSerialization.jsonObject(with: saved) as! [String: Any] |
| 22 | legacy.removeValue(forKey: "loopPlayback") |
| 23 | let migrated = try JSONDecoder().decode(VisionProject.self, from: JSONSerialization.data(withJSONObject: legacy)).validated() |
| 24 | precondition(migrated.loopPlayback && migrated.clips == project.clips && migrated.looks == project.looks) |
| 25 | project.loopPlayback = false |
| 26 | let nonlooping = try JSONDecoder().decode(VisionProject.self, from: JSONEncoder().encode(project)) |
| 27 | precondition(!nonlooping.loopPlayback) |
| 28 | let segments = loaded.segments(duration: 24) |
| 29 | precondition(segments.map(\.start) == [0, 8, 10] && segments.map(\.end) == [8, 10, 14]) |
| 30 | precondition(StudioTimeline.valid(segments, sceneCount: NativeEffects.all.count)) |
| 31 | precondition(segments[0].scene == segments[2].scene && loaded.looks[segments[2].scene]?.chaos == 0.82) |
| 32 | project.beatBoundaries = nil; project.bpm = 120 |
| 33 | precondition(project.segments(duration: 11).map(\.end) == [8, 10, 11]) |
| 34 | for bpm in [120.0, 150, 180] { |
| 35 | var timed = project |
| 36 | timed.bpm = bpm |
| 37 | timed.barBoundaries = (0...24).map { Double($0) * 240 / bpm } |
| 38 | timed.clips = [VisionClip(scene: scenes[0], bars: 4), VisionClip(scene: scenes[1], bars: 1)] |
| 39 | let ids = timed.clips.map(\.id) |
| 40 | timed.fitTimeline(duration: 24 * 240 / bpm) |
| 41 | precondition(abs(timed.segments(duration: 24 * 240 / bpm)[0].end - 960 / bpm) < 0.00001) |
| 42 | precondition(Array(timed.clips.prefix(2)).map(\.id) == ids) |
| 43 | for i in timed.clips.indices { timed.clips[i].bars = 1 } |
| 44 | timed.fitTimeline(duration: 24 * 240 / bpm) |
| 45 | let cuts = timed.segments(duration: 24 * 240 / bpm) |
| 46 | precondition(cuts.count == 24 && cuts.last?.end == 24 * 240 / bpm) |
| 47 | precondition(cuts.allSatisfy { abs(($0.end - $0.start) - 240 / bpm) < 0.00001 }) |
| 48 | precondition(Set(timed.clips.map(\.id)).count == timed.clips.count) |
| 49 | } |
| 50 | var oldLook = try JSONSerialization.jsonObject(with: JSONEncoder().encode(VisionLook(scene: scenes[0]))) as! [String: Any] |
| 51 | for key in ["strobeEnabled", "strobeSensitivity", "strobeStrength", "rotation", "motionBlur", "haloCount"] { oldLook.removeValue(forKey: key) } |
| 52 | let migratedLook = try JSONDecoder().decode(VisionLook.self, from: JSONSerialization.data(withJSONObject: oldLook)) |
| 53 | precondition(!migratedLook.strobeEnabled && migratedLook.strobeSensitivity == 1 && migratedLook.isValid) |
| 54 | print("PASS 120/150/180 BPM: four bars = 16 beats, one-bar retiming fills song, unique card IDs and appearance migration") |
| 55 | var padded = project |
| 56 | padded.barBoundaries = [0, 2, 4, 6, 8, 10] |
| 57 | padded.clips = [VisionClip(scene: scenes[0], bars: 1), VisionClip(scene: scenes[1], bars: 1)] |
| 58 | padded.fitTimeline(duration: 10.05) |
| 59 | precondition(padded.segments(duration: 10.05).last?.end == 10.05 |
| 60 | && StudioTimeline.valid(padded.segments(duration: 10.05), sceneCount: NativeEffects.all.count)) |
| 61 | var randomBars = project |
| 62 | randomBars.barBoundaries = Array(stride(from: 0.0, through: 96.0, by: 2.0)) |
| 63 | let originalIDs = randomBars.clips.map(\.id) |
| 64 | let originalScenes = randomBars.clips.map(\.scene) |
| 65 | let originalLooks = randomBars.looks |
| 66 | var choice = 0 |
| 67 | precondition(randomBars.randomizeBars(duration: 96.05) { choice += 1; return (choice - 1) % 8 + 1 }) |
| 68 | let randomizedCuts = randomBars.segments(duration: 96.05) |
| 69 | precondition(randomizedCuts.last?.end == 96.05 && StudioTimeline.valid(randomizedCuts, sceneCount: NativeEffects.all.count)) |
| 70 | precondition(randomBars.clips.map(\.bars) == (0..<randomBars.clips.count).map { $0 % 8 + 1 }) |
| 71 | precondition(Array(randomBars.clips.prefix(originalIDs.count)).map(\.id) == originalIDs |
| 72 | && Array(randomBars.clips.prefix(originalScenes.count)).map(\.scene) == originalScenes) |
| 73 | precondition(Set(randomBars.clips.map(\.id)).count == randomBars.clips.count && randomBars.looks == originalLooks) |
| 74 | let beforeInvalidBars = randomBars.clips |
| 75 | precondition(!randomBars.randomizeBars(duration: 96.05, chooseLength: { 0 }) && randomBars.clips == beforeInvalidBars) |
| 76 | var largeSize = VisionLook(scene: scenes[0]); largeSize.size = 6 |
| 77 | let restoredLargeSize = try JSONDecoder().decode(VisionLook.self, from: JSONEncoder().encode(largeSize)) |
| 78 | precondition(largeSize.isValid && restoredLargeSize.size == 6) |
| 79 | print("PASS random bar lengths: every new card randomized, beat-grid coverage, decoder padding, IDs/order/looks retained, atomic invalid-input rejection and Mac size range") |
| 80 | func rejects(_ mutate: (inout VisionProject) -> Void) { |
| 81 | var invalid = project; mutate(&invalid) |
| 82 | do { _ = try invalid.validated(); fatalError("Invalid project accepted") } catch {} |
| 83 | } |
| 84 | rejects { $0.version = 99 } |
| 85 | rejects { $0.clips = [] } |
| 86 | rejects { $0.clips[1].id = $0.clips[0].id } |
| 87 | rejects { $0.clips[0].bars = 0 } |
| 88 | rejects { $0.clips[0].scene = Int.max } |
| 89 | rejects { $0.audioFile = "../private.mp3" } |
| 90 | rejects { $0.bpm = .nan } |
| 91 | rejects { $0.beatBoundaries = [0, 1, 0.5] } |
| 92 | rejects { $0.beatBoundaries = [1, 2] } |
| 93 | rejects { $0.barBoundaries = [0] } |
| 94 | rejects { $0.looks[scenes[0]]?.speed = .infinity } |
| 95 | rejects { $0.looks[scenes[0]]?.palette = 99 } |
| 96 | for retired in NativeEffects.all.indices where !scenes.contains(retired) { |
| 97 | var legacy = project; legacy.clips[0].scene = retired |
| 98 | let result = try legacy.validated() |
| 99 | precondition(scenes.contains(result.clips[0].scene)) |
| 100 | } |
| 101 | print("PASS Vision project: \(scenes.count) defaults/categories, JSON round-trip, shared looks, mixed bar lengths, beat grid, clipping, invalid files and retired IDs") |
| 102 | } |
| 103 | } |