import CoreGraphics import Foundation import ShotdeckCore import Testing // Which window a capture takes, for each scope — pure logic over the window list the // window server reports, so it is testable without a display session. private func window( id: UInt32, bundle: String?, pid: Int32 = 100, frame: CGRect = CGRect(x: 100, y: 100, width: 800, height: 600), layer: Int = 0 ) -> WindowCandidate { WindowCandidate( windowID: id, ownerBundleID: bundle, ownerPID: pid, frame: frame, layer: layer) } @Test func windowScopeTakesTheFrontWindowOfTheFrontApp() throws { let candidates = [ window(id: 1, bundle: "com.apple.Safari", pid: 10), window(id: 2, bundle: "com.apple.Terminal", pid: 20), ] let target = try CaptureTargetResolver.resolve( scope: .window, savedArea: nil, frontmostPID: 20, candidates: candidates) guard case .window(let id, _, _) = target else { Issue.record("expected a window target, got \(target)") return } #expect(id == 2) } @Test func windowScopeFallsBackToTheFrontmostWindowOfAnyApp() throws { let candidates = [ window(id: 1, bundle: "com.apple.Safari", pid: 10), window(id: 2, bundle: "com.apple.Terminal", pid: 20), ] let target = try CaptureTargetResolver.resolve( scope: .window, savedArea: nil, frontmostPID: 99, candidates: candidates) guard case .window(let id, _, _) = target else { Issue.record("expected a window target, got \(target)") return } #expect(id == 1) } @Test func redlineNeverCapturesItself() throws { let candidates = [ window(id: 1, bundle: CaptureTargetResolver.ownBundleID, pid: 5), window(id: 2, bundle: "com.apple.Safari", pid: 10), ] let target = try CaptureTargetResolver.resolve( scope: .window, savedArea: nil, frontmostPID: 5, candidates: candidates) guard case .window(let id, _, _) = target else { Issue.record("expected a window target, got \(target)") return } #expect(id == 2) } @Test func chromeAndDesktopWindowsAreNotCandidates() throws { let candidates = [ window(id: 1, bundle: "com.apple.dock", layer: 20), window(id: 2, bundle: "com.apple.WindowManager", layer: 8), window(id: 3, bundle: "com.apple.Safari"), ] let target = try CaptureTargetResolver.resolve( scope: .window, savedArea: nil, frontmostPID: 0, candidates: candidates) guard case .window(let id, _, _) = target else { Issue.record("expected a window target, got \(target)") return } #expect(id == 3) } @Test func tinyWindowsAreIgnored() throws { let candidates = [ window(id: 1, bundle: "com.apple.Safari", frame: CGRect(x: 0, y: 0, width: 20, height: 20)), window(id: 2, bundle: "com.apple.Terminal"), ] let target = try CaptureTargetResolver.resolve( scope: .window, savedArea: nil, frontmostPID: 0, candidates: candidates) guard case .window(let id, _, _) = target else { Issue.record("expected a window target, got \(target)") return } #expect(id == 2) } @Test func browserScopeTakesTheFrontBrowserWindow() throws { let candidates = [ window(id: 1, bundle: "com.apple.Terminal", pid: 10), window(id: 2, bundle: "com.google.Chrome", pid: 11), window(id: 3, bundle: "com.apple.Safari", pid: 12), ] let target = try CaptureTargetResolver.resolve( scope: .browser, savedArea: nil, frontmostPID: 10, candidates: candidates) guard case .window(let id, _, _) = target else { Issue.record("expected a window target, got \(target)") return } #expect(id == 2) } @Test func browserScopeSaysSoWhenNoBrowserIsOpen() { let candidates = [window(id: 1, bundle: "com.apple.Terminal")] #expect(throws: ShotdeckError.self) { _ = try CaptureTargetResolver.resolve( scope: .browser, savedArea: nil, frontmostPID: 10, candidates: candidates) } } @Test func windowScopeSaysSoWhenThereIsNoWindow() { #expect(throws: ShotdeckError.self) { _ = try CaptureTargetResolver.resolve( scope: .window, savedArea: nil, frontmostPID: 10, candidates: [window(id: 1, bundle: "com.apple.dock", layer: 20)]) } } @Test func areaScopeUsesTheSavedAreaAndSaysWhenThereIsNone() throws { let area = CaptureRegion( displayID: CGMainDisplayID(), rect: CGRect(x: 10, y: 20, width: 300, height: 200), capturedScale: 2) let target = try CaptureTargetResolver.resolve( scope: .area, savedArea: area, frontmostPID: 0, candidates: []) #expect(target == .region(area)) #expect(throws: ShotdeckError.self) { _ = try CaptureTargetResolver.resolve( scope: .area, savedArea: nil, frontmostPID: 0, candidates: []) } } @Test func screenScopeUsesTheAreaDisplay() throws { let area = CaptureRegion( displayID: CGMainDisplayID(), rect: CGRect(x: 10, y: 20, width: 300, height: 200), capturedScale: 2) let target = try CaptureTargetResolver.resolve( scope: .screen, savedArea: area, frontmostPID: 0, candidates: []) #expect(target == .display(displayID: area.displayID)) } @Test func captureScopeDescriptionsNameWhatWillBeTaken() { let area = CaptureRegion( displayID: CGMainDisplayID(), rect: CGRect(x: 0, y: 0, width: 800, height: 600), capturedScale: 2) #expect(CaptureScope.area.targetDescription(savedArea: area).contains("800 × 600")) #expect(CaptureScope.area.targetDescription(savedArea: nil).contains("No area")) #expect(CaptureScope.window.targetDescription(savedArea: area).contains("frontmost window")) #expect(CaptureScope.browser.targetDescription(savedArea: area).contains("browser")) #expect(CaptureScope.screen.targetDescription(savedArea: area).contains("Display")) }