import AppKit import CoreGraphics import Foundation import ParcelCore import Testing @testable import ParcelTestSupport @Suite(.serialized) @MainActor struct AppKitTestSuite {} enum MediaScene: String, CaseIterable { case icon, hero, layouts, stacks, shortcuts, accessibility, tour case snapUndo = "snap-undo" static let layoutSequence: [Layout] = [.grid, .columns, .side, .tree, .accordion] func outputs(appearance: String) -> [String] { switch self { case .icon: ["icon-readme.png"] case .hero: ["hero-\(appearance).png"] case .layouts: ["layouts.gif", "layouts-reduced.png"] case .stacks: ["stacks.gif", "stacks-reduced.png"] case .snapUndo: ["snap-undo.gif", "snap-undo-reduced.png"] case .shortcuts: ["settings-shortcuts-\(appearance).png"] case .accessibility: ["settings-accessibility.png"] case .tour: ["tour.png"] } } static func selection(_ name: String, appearance: String) throws -> [MediaScene] { guard ["light", "dark"].contains(appearance) else { throw E2EError(description: "Appearance must be light or dark") } if name == "all" { return appearance == "light" ? [.hero, .layouts, .stacks, .shortcuts, .tour] : [.hero, .snapUndo, .shortcuts] } guard let scene = MediaScene(rawValue: name) else { throw E2EError(description: "Unknown media scene: \(name)") } if [.layouts, .stacks, .tour].contains(scene), appearance != "light" { throw E2EError(description: "\(name) uses the light appearance") } if scene == .snapUndo, appearance != "dark" { throw E2EError(description: "snap-undo uses the dark appearance") } return [scene] } } struct MediaShortcuts: Decodable { struct Properties: Decodable { struct Bindings: Decodable { let defaults: [String: String] private enum CodingKeys: String, CodingKey { case defaults = "default" } } let keybindings: Bindings } let properties: Properties func label(_ command: String, title: String) throws -> String { guard let chord = properties.keybindings.defaults[command] else { throw E2EError(description: "No default shortcut for the captured command: \(command)") } return "\(title) · \(chord)" } } @MainActor final class MediaRecorder { let output: URL let appearance: String let ffmpeg: URL private var label: NSPanel? init(output: URL, appearance: String, ffmpeg: URL) { self.output = output self.appearance = appearance self.ffmpeg = ffmpeg } func confirm(_ message: String) throws { try FileHandle.standardError.write( contentsOf: Data("\n\(message)\nPress Return when ready, or type anything to cancel: ".utf8)) let terminal = try FileHandle(forReadingFrom: URL(fileURLWithPath: "/dev/tty")) defer { do { try terminal.close() } catch { Issue.record(error) } } guard let data = try terminal.read(upToCount: 256), data == Data([10]) else { throw E2EError(description: "Capture cancelled") } } func run(_ executable: URL, _ arguments: [String], directory: URL) async throws { let result = try await runProcess( executable: executable, arguments: arguments, directory: directory, timeout: .seconds(60)) guard result.status == 0 else { throw E2EError(description: "\(executable.lastPathComponent) failed: \(result.errors)") } } func showLabel(_ text: String, frame: Rect) throws { let screen = try #require(NSScreen.screens.first, "Capture needs a display") label?.orderOut(nil) let panel = NSPanel( contentRect: NSRect( x: frame.minX + 24, y: screen.frame.maxY - frame.maxY + 24, width: 340, height: 44), styleMask: [.borderless, .nonactivatingPanel], backing: .buffered, defer: false) panel.level = .floating panel.backgroundColor = .windowBackgroundColor panel.ignoresMouseEvents = true let field = NSTextField(labelWithString: text) field.font = .systemFont(ofSize: 17, weight: .semibold) field.frame = NSRect(x: 12, y: 10, width: 316, height: 24) panel.contentView = NSView(frame: NSRect(x: 0, y: 0, width: 340, height: 44)) panel.contentView?.addSubview(field) panel.orderFrontRegardless() label = panel } func still(_ name: String, frame: Rect, directory: URL, width: Int = 1440, height: Int = 900) async throws { let raw = directory.appendingPathComponent(UUID().uuidString + ".png") try await run( URL(fileURLWithPath: "/usr/sbin/screencapture"), ["-x", "-t", "png", region(frame), raw.path], directory: directory) try await run( ffmpeg, [ "-v", "error", "-n", "-i", raw.path, "-vf", "scale=\(width):\(height):force_original_aspect_ratio=decrease,pad=\(width):\(height):(ow-iw)/2:(oh-ih)/2", "-frames:v", "1", directory.appendingPathComponent(name).path, ], directory: directory) } private func region(_ frame: Rect) -> String { "-R\(Int(frame.minX)),\(Int(frame.minY)),\(Int(frame.size.width)),\(Int(frame.size.height))" } func recordMovie(frame: Rect, directory: URL, actions: @MainActor () async throws -> Void) async throws -> URL { let movie = directory.appendingPathComponent("scene.mov") async let capture: Void = run( URL(fileURLWithPath: "/usr/sbin/screencapture"), ["-x", "-v", "-V", "6", region(frame), movie.path], directory: directory) let started = ContinuousClock.now try await Task.sleep(for: .milliseconds(300)) try await actions() try #require(started.duration(to: .now) < .milliseconds(5800), "Scene ran past the GIF’s export window") try await capture return movie } func gif(_ name: String, movie: URL, directory: URL) async throws { let destination = directory.appendingPathComponent(name) try await run( ffmpeg, [ "-v", "error", "-n", "-i", movie.path, "-t", "5.8", "-filter_complex", "fps=12,scale=720:450:force_original_aspect_ratio=decrease,pad=720:450:(ow-iw)/2:(oh-ih)/2,split[a][b];[a]palettegen[p];[b][p]paletteuse", "-loop", "0", destination.path, ], directory: directory) let attributes = try FileManager.default.attributesOfItem(atPath: destination.path) guard let size = attributes[.size] as? NSNumber, size.intValue <= 3_000_000 else { throw E2EError(description: "\(name) exceeds 3 MB; shorten the scene before publishing") } } func capture(_ scene: MediaScene) async throws { if scene == .icon { let source = try #require(ProcessInfo.processInfo.environment["PARCEL_MEDIA_ICON"]) let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) defer { do { try FileManager.default.removeItem(at: directory) } catch { Issue.record(error) } } try await run( URL(fileURLWithPath: "/usr/bin/sips"), ["-z", "400", "400", source, "--out", directory.appendingPathComponent("icon-readme.png").path], directory: directory) try publish(scene, directory: directory) return } try #require(CGPreflightScreenCaptureAccess(), "Grant the capture host Screen Recording before recording") try E2EConsoleSession.requireUnlockedConsole() let expected: NSAppearance.Name = appearance == "dark" ? .darkAqua : .aqua try #require( NSApplication.shared.effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) == expected, "Switch macOS to \(appearance) appearance first") var configuration = E2EConfiguration() configuration.layout = .grid configuration.innerGap = 8 configuration.outerGap = 8 configuration.focusBorder = true configuration.motion = true configuration.transition = "glide" configuration.windowCap = scene == .stacks ? 3 : 4 try await withSession(configuration: configuration) { session in let count = scene == .stacks ? 3 : 4 let (fixture, _) = try await session.addFixture(arguments: [ "--windows", String(count), "--titles", count == 3 ? "[\"Editor\",\"Build\",\"Notes\"]" : "[\"Editor\",\"Build\",\"Notes\",\"Preview\"]", ]) try await session.startApp() let windows = try await session.settledWindows(for: fixture, count: count) let workspace = try await session.workspace(for: windows) let frame = try E2EScreenGeometry.fullFrame(displayID: workspace.id.display) let shortcuts = try JSONDecoder().decode(MediaShortcuts.self, from: try await session.cli(["schema"])) defer { self.label?.orderOut(nil); self.label = nil } switch scene { case .hero: try self.confirm("Open Parcel’s menu showing Grid and the tile cap. Keep the fixture windows visible.") try await self.still( "hero-\(self.appearance).png", frame: frame, directory: session.directory, width: 1720, height: 1076 ) case .layouts: let movie = try await self.recordMovie(frame: frame, directory: session.directory) { for layout in MediaScene.layoutSequence { try self.showLabel( shortcuts.label("layout-\(layout.rawValue)", title: layout.rawValue.capitalized), frame: frame) try await session.command("set-layout", layout.rawValue) let settled = try await session.settledWindows(for: fixture, count: 4) try await session.expectLayout(settled, fixture: fixture, layout: layout) try await Task.sleep(for: .milliseconds(900)) } } try await self.gif("layouts.gif", movie: movie, directory: session.directory) for (index, layout) in MediaScene.layoutSequence.enumerated() { try self.showLabel( shortcuts.label("layout-\(layout.rawValue)", title: layout.rawValue.capitalized), frame: frame) try await session.command("set-layout", layout.rawValue) _ = try await session.settledWindows(for: fixture, count: 4) try await self.still( String(format: "layout-%02d.png", index), frame: frame, directory: session.directory) } try await self.run( self.ffmpeg, [ "-v", "error", "-n", "-i", session.directory.appendingPathComponent("layout-%02d.png").path, "-vf", "scale=480:300,tile=3x2,pad=1440:900:0:150", "-frames:v", "1", session.directory.appendingPathComponent("layouts-reduced.png").path, ], directory: session.directory) case .stacks: let movie = try await self.recordMovie(frame: frame, directory: session.directory) { for (offset, title) in ["Preview", "Inbox"].enumerated() { try self.showLabel("Three tiles · \(offset + 4) windows", frame: frame) _ = try await fixture.control(FixtureControl(action: "create", title: title)) _ = try await session.settledWindows(for: fixture, count: offset + 4) try await Task.sleep(for: .milliseconds(750)) } try self.showLabel(shortcuts.label("stack-next", title: "Next stack member"), frame: frame) try await session.command("stack-next") _ = try await session.settledWindows(for: fixture, count: 5) try await self.still("stacks-reduced.png", frame: frame, directory: session.directory) } try await self.gif("stacks.gif", movie: movie, directory: session.directory) case .snapUndo: let original = try await fixture.snapshot() let movie = try await self.recordMovie(frame: frame, directory: session.directory) { for (window, command, title) in [ (4, "snap-left-half", "Snap left half"), (3, "make-primary", "Make primary"), (3, "undo", "Undo primary"), (4, "undo", "Undo snap"), ] { _ = try await fixture.control(FixtureControl(action: "focus", window: window)) try self.showLabel(shortcuts.label(command, title: title), frame: frame) try await session.command(command) _ = try await session.settledWindows(for: fixture, count: 4) try await Task.sleep(for: .milliseconds(700)) } } let restored = try await fixture.snapshot() try #require(original.windows.count == restored.windows.count) for window in original.windows { let actual = try #require(restored.windows.first { $0.number == window.number }) try #require(framesMatch(window.frame, actual.frame), "Undo did not restore the fixture layout") } try await self.still("snap-undo-reduced.png", frame: frame, directory: session.directory) try await self.gif("snap-undo.gif", movie: movie, directory: session.directory) case .shortcuts, .accessibility, .tour: let instruction: String switch scene { case .shortcuts: instruction = "Open Settings → Shortcuts. Show a conflict warning and start recording another binding." case .accessibility: instruction = "Enable Increase Contrast in macOS. Keep a fixture window focused so its stronger border is visible." case .tour: try await session.command("e2e-tour", "begin") instruction = "Keep the setup Welcome screen visible." default: preconditionFailure("Scene grouping disagrees") } try self.confirm(instruction + " Select only this scene’s public UI in the next capture.") let raw = session.directory.appendingPathComponent("selected.png") try await self.run( URL(fileURLWithPath: "/usr/sbin/screencapture"), ["-x", "-i", "-t", "png", raw.path], directory: session.directory) let name = try #require(scene.outputs(appearance: self.appearance).first) try await self.run( self.ffmpeg, [ "-v", "error", "-n", "-i", raw.path, "-vf", "scale=\(scene == .tour ? 1040 : 1440):-2", "-frames:v", "1", session.directory.appendingPathComponent(name).path, ], directory: session.directory) case .icon: preconditionFailure("Icon scene does not open windows") } try await session.quit() try self.publish(scene, directory: session.directory) } } func publish(_ scene: MediaScene, directory: URL) throws { let names = scene.outputs(appearance: appearance) for name in names { let data = try Data(contentsOf: directory.appendingPathComponent(name)) let validHeader = name.hasSuffix(".gif") ? data.starts(with: Data("GIF89a".utf8)) || data.starts(with: Data("GIF87a".utf8)) : data.starts(with: [137, 80, 78, 71, 13, 10, 26, 10]) guard data.count > 100, validHeader else { throw E2EError(description: "Missing or invalid media: \(name)") } guard !FileManager.default.fileExists(atPath: output.appendingPathComponent(name).path) else { throw E2EError(description: "Move existing capture aside: \(name)") } } for name in names { try FileManager.default.copyItem( at: directory.appendingPathComponent(name), to: output.appendingPathComponent(name)) } } } extension RealAppEndToEndTests { @Test(.enabled(if: ProcessInfo.processInfo.environment["PARCEL_CAPTURE_MEDIA"] == "1")) func captureRequestedScenes() async throws { let environment = ProcessInfo.processInfo.environment let appearance = try #require(environment["PARCEL_MEDIA_APPEARANCE"]) let scenes = try MediaScene.selection(try #require(environment["PARCEL_MEDIA_SCENE"]), appearance: appearance) let recorder = MediaRecorder( output: URL(fileURLWithPath: try #require(environment["PARCEL_MEDIA_OUTPUT"])), appearance: appearance, ffmpeg: URL(fileURLWithPath: try #require(environment["PARCEL_MEDIA_FFMPEG"]))) for scene in scenes { for name in scene.outputs(appearance: appearance) { try #require( !FileManager.default.fileExists(atPath: recorder.output.appendingPathComponent(name).path), "Move existing capture aside: \(name)") } } try recorder.confirm( "Use an empty desktop at 2× with the default wallpaper. Captures include the display. Quit your own Parcel first." ) for scene in scenes { try await recorder.capture(scene) } } } @Suite struct MediaShortcutTests { @Test func labelsUseTheSchemaDefaults() throws { let schema = Data(#"{"properties":{"keybindings":{"default":{"undo":"fixture-shortcut"}}}}"#.utf8) let shortcuts = try JSONDecoder().decode(MediaShortcuts.self, from: schema) #expect(try shortcuts.label("undo", title: "Undo") == "Undo · fixture-shortcut") } @Test func missingBindingsAreNotInvented() throws { let schema = Data(#"{"properties":{"keybindings":{"default":{}}}}"#.utf8) let shortcuts = try JSONDecoder().decode(MediaShortcuts.self, from: schema) #expect(throws: E2EError.self) { try shortcuts.label("undo", title: "Undo") } } @Test func malformedDefaultsAreRejected() { let schema = Data(#"{"properties":{"keybindings":{"default":{"undo":false}}}}"#.utf8) #expect(throws: DecodingError.self) { try JSONDecoder().decode(MediaShortcuts.self, from: schema) } } } @Suite @MainActor struct MediaPublicationTests { private func withDirectories(_ body: (URL, URL) throws -> Void) throws { let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) let source = root.appendingPathComponent("source") let output = root.appendingPathComponent("output") try FileManager.default.createDirectory(at: source, withIntermediateDirectories: true) try FileManager.default.createDirectory(at: output, withIntermediateDirectories: false) defer { do { try FileManager.default.removeItem(at: root) } catch { Issue.record(error) } } try body(source, output) } @Test func publishesNamedMediaWithoutLaunchingTools() throws { try withDirectories { source, output in let data = Data([137, 80, 78, 71, 13, 10, 26, 10]) + Data(repeating: 0, count: 120) try data.write(to: source.appendingPathComponent("hero-light.png")) let recorder = MediaRecorder(output: output, appearance: "light", ffmpeg: URL(fileURLWithPath: "/unused")) try recorder.publish(.hero, directory: source) #expect(try Data(contentsOf: output.appendingPathComponent("hero-light.png")) == data) } } @Test func preservesExistingMedia() throws { try withDirectories { source, output in let old = Data("Keep this reviewed image".utf8) let destination = output.appendingPathComponent("hero-light.png") try old.write(to: destination) try (Data([137, 80, 78, 71, 13, 10, 26, 10]) + Data(repeating: 0, count: 120)) .write(to: source.appendingPathComponent("hero-light.png")) let recorder = MediaRecorder(output: output, appearance: "light", ffmpeg: URL(fileURLWithPath: "/unused")) #expect(throws: E2EError.self) { try recorder.publish(.hero, directory: source) } #expect(try Data(contentsOf: destination) == old) } } @Test func rejectsInvalidMediaBeforePublishingAnyFile() throws { try withDirectories { source, output in try (Data("GIF89a".utf8) + Data(repeating: 0, count: 120)).write( to: source.appendingPathComponent("layouts.gif")) try Data(repeating: 0, count: 120).write(to: source.appendingPathComponent("layouts-reduced.png")) let recorder = MediaRecorder(output: output, appearance: "light", ffmpeg: URL(fileURLWithPath: "/unused")) #expect(throws: E2EError.self) { try recorder.publish(.layouts, directory: source) } #expect(try FileManager.default.contentsOfDirectory(atPath: output.path).isEmpty) } } } @Suite struct MediaSceneTests { @Test func capturesEveryRequestedLayout() { #expect(MediaScene.layoutSequence == [.grid, .columns, .side, .tree, .accordion]) } @Test(arguments: [MediaScene.layouts, .stacks, .snapUndo]) func everyAnimationHasAStill(scene: MediaScene) { let outputs = scene.outputs(appearance: "light") #expect(outputs.count == 2) #expect(outputs[0].hasSuffix(".gif")) #expect(outputs[1].hasSuffix("-reduced.png")) } @Test func appearanceBatchesDoNotOverwriteEachOther() throws { let light = try MediaScene.selection("all", appearance: "light").flatMap { $0.outputs(appearance: "light") } let dark = try MediaScene.selection("all", appearance: "dark").flatMap { $0.outputs(appearance: "dark") } #expect(Set(light).isDisjoint(with: dark)) #expect(Set(light).count == light.count) #expect(Set(dark).count == dark.count) } @Test(arguments: [("unknown", "light"), ("all", "auto"), ("layouts", "dark"), ("snap-undo", "light")]) func rejectsInvalidRequests(scene: String, appearance: String) { #expect(throws: E2EError.self) { try MediaScene.selection(scene, appearance: appearance) } } @Test func optionalScenesHaveNamedOutputs() { #expect(MediaScene.accessibility.outputs(appearance: "light") == ["settings-accessibility.png"]) #expect(MediaScene.tour.outputs(appearance: "light") == ["tour.png"]) #expect(MediaScene.icon.outputs(appearance: "light") == ["icon-readme.png"]) } }