PulseCanvas / app · pulsecanvas.ai / scripts / CompactLayoutSmoke.swift
| 1 | import AppKit |
| 2 | import MetalKit |
| 3 | import SwiftUI |
| 4 | |
| 5 | @main struct CompactLayoutSmoke { |
| 6 | @MainActor static func main() { |
| 7 | _ = NSApplication.shared |
| 8 | let defaults = UserDefaults(suiteName: "PulseCanvas.layout-test.\(UUID().uuidString)")! |
| 9 | let studio = StudioStore(defaults: defaults) |
| 10 | studio.isStudio = true |
| 11 | studio.segments = (0..<32).map { StudioSegment(start: Double($0 * 8), end: Double(($0 + 1) * 8), scene: $0 % 31, bars: 4) } |
| 12 | studio.detectedBPM = 128 |
| 13 | let render = RenderState() |
| 14 | let audio = AudioController() |
| 15 | let exporter = SocialExportController() |
| 16 | // The standalone test has its own defaults domain; retain its format too. |
| 17 | let previousFormat = UserDefaults.standard.data(forKey: "PulseCanvas.videoFormat.v1") |
| 18 | defer { |
| 19 | if let previousFormat { UserDefaults.standard.set(previousFormat, forKey: "PulseCanvas.videoFormat.v1") } |
| 20 | else { UserDefaults.standard.removeObject(forKey: "PulseCanvas.videoFormat.v1") } |
| 21 | } |
| 22 | let host = NSHostingView(rootView: CreatorView(render: render, audio: audio, radio: RadioController(), studio: studio, |
| 23 | exporter: exporter, presentation: StudioPresentationController(), lifecycle: AppLifecycle()).preferredColorScheme(.dark)) |
| 24 | for height in stride(from: 180, through: 600, by: 1) { |
| 25 | let layout = CanvasPreviewLayout(size: CGSize(width: 900, height: height), aspectRatio: 16.0 / 9) |
| 26 | let next = CanvasPreviewLayout(size: CGSize(width: 900, height: height + 1), aspectRatio: 16.0 / 9) |
| 27 | precondition(next.preview.height >= layout.preview.height, "Resizing larger must not collapse the image") |
| 28 | } |
| 29 | var previousHeights: [String: CGFloat] = [:] |
| 30 | for studioMode in [true, false] { |
| 31 | studio.isStudio = studioMode |
| 32 | let modeName = studioMode ? "Studio" : "Visualizers" |
| 33 | for size in [CGSize(width: 1080, height: 740), CGSize(width: 1440, height: 740), CGSize(width: 1440, height: 800), CGSize(width: 1440, height: 859), CGSize(width: 1440, height: 860), CGSize(width: 1440, height: 940)] { |
| 34 | for aspect in SocialAspect.allCases { |
| 35 | exporter.aspect = aspect |
| 36 | host.frame = CGRect(origin: .zero, size: size) |
| 37 | host.layoutSubtreeIfNeeded() |
| 38 | RunLoop.main.run(until: Date().addingTimeInterval(0.1)) |
| 39 | host.layoutSubtreeIfNeeded() |
| 40 | let metalViews = descendants(host).compactMap { $0 as? MTKView } |
| 41 | precondition(metalViews.count == 1, "Resize must retain one renderer") |
| 42 | let preview = metalViews[0].convert(metalViews[0].bounds, to: host) |
| 43 | precondition(preview.height >= 200, "Studio preview collapsed: \(preview)") |
| 44 | precondition(abs(preview.width / preview.height - aspect.aspectRatio) < 0.01, "Preview aspect changed") |
| 45 | precondition(host.bounds.contains(preview), "Preview escaped the window") |
| 46 | let key = "\(modeName)/\(size.width)/\(aspect.rawValue)" |
| 47 | if let previousHeight = previousHeights[key] { precondition(preview.height + 1 >= previousHeight, "Growing the window shrank the preview: \(key), window \(size.height), \(previousHeight) → \(preview.height)") } |
| 48 | previousHeights[key] = preview.height |
| 49 | print("PASS \(modeName) \(Int(size.width))×\(Int(size.height)) \(aspect.rawValue): preview \(Int(preview.width))×\(Int(preview.height))") |
| 50 | if studioMode && size.width == 1080 && aspect == .wide, let bitmap = host.bitmapImageRepForCachingDisplay(in: host.bounds) { |
| 51 | host.cacheDisplay(in: host.bounds, to: bitmap) |
| 52 | try! bitmap.representation(using: .png, properties: [:])!.write(to: URL(fileURLWithPath: ".build/validation/compact-studio-chrome.png")) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | print("PASS compact editor: all canvas shapes retain visible previews with transport and presentation controls; one renderer survives resizing") |
| 58 | let aspectPreference = "PulseCanvas.presentationMatchAspect.v1" |
| 59 | let previousAspectPreference = UserDefaults.standard.object(forKey: aspectPreference) |
| 60 | defer { |
| 61 | if let previousAspectPreference { UserDefaults.standard.set(previousAspectPreference, forKey: aspectPreference) } |
| 62 | else { UserDefaults.standard.removeObject(forKey: aspectPreference) } |
| 63 | } |
| 64 | for scene in [10, 31] { |
| 65 | render.scene = scene |
| 66 | for mode in [StudioPresentationController.Mode.studio, .visualizer] { |
| 67 | for aspect in SocialAspect.allCases { |
| 68 | let presentationHost = NSHostingView(rootView: StudioPresentationView( |
| 69 | render: render, audio: audio, studio: studio, mode: mode, aspect: aspect, exit: {})) |
| 70 | for size in [CGSize(width: 1440, height: 900), CGSize(width: 900, height: 1440)] { |
| 71 | presentationHost.frame = CGRect(origin: .zero, size: size) |
| 72 | var retainedRenderer: MTKView? |
| 73 | for fit in [false, true, false] { |
| 74 | UserDefaults.standard.set(fit, forKey: aspectPreference) |
| 75 | RunLoop.main.run(until: Date().addingTimeInterval(0.05)) |
| 76 | presentationHost.layoutSubtreeIfNeeded() |
| 77 | let views = descendants(presentationHost).compactMap { $0 as? MTKView } |
| 78 | precondition(views.count == 1, "Presentation must retain one renderer") |
| 79 | if let retainedRenderer { precondition(views[0] === retainedRenderer, "Toggling aspect must resize the current renderer without restarting it") } |
| 80 | retainedRenderer = views[0] |
| 81 | let frame = views[0].convert(views[0].bounds, to: presentationHost) |
| 82 | precondition(abs(frame.midX - size.width / 2) < 1 && abs(frame.midY - size.height / 2) < 1, "Presentation must stay centered") |
| 83 | precondition(frame.minX >= -1 && frame.minY >= -1 && frame.maxX <= size.width + 1 && frame.maxY <= size.height + 1) |
| 84 | if fit { |
| 85 | precondition(abs(frame.width / frame.height - aspect.aspectRatio) < 0.01, "Presentation must match the project aspect ratio") |
| 86 | precondition(abs(frame.width - size.width) < 1 || abs(frame.height - size.height) < 1, "Fitted presentation must use the largest uncropped frame") |
| 87 | } else { |
| 88 | precondition(abs(frame.width - size.width) < 1 && abs(frame.height - size.height) < 1, "Unchecked presentation must fill the display") |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | print("PASS presentation aspect: all five project shapes, both presentation modes, native/ProjectM renderers and wide/tall displays; centered fit, full-screen fill and renderer continuity verified") |
| 96 | } |
| 97 | |
| 98 | @MainActor static func descendants(_ view: NSView) -> [NSView] { |
| 99 | view.subviews.flatMap { [$0] + descendants($0) } |
| 100 | } |
| 101 | } |