Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b9b51b2
test: cover window screenshot socket output
austinywang Jul 28, 2026
26f31b7
test: tolerate background UI test launch
austinywang Jul 28, 2026
cdc7e50
fix: replace legacy window screenshot capture
austinywang Jul 28, 2026
ba63392
test: harden screenshot regression setup
austinywang Jul 28, 2026
e8d3600
test: match XCTest activation assertion
austinywang Jul 28, 2026
2c955f6
test: wait for rendered screenshot content
austinywang Jul 28, 2026
f584f09
Merge remote-tracking branch 'origin/main' into issue-9065-remove-cgw…
austinywang Jul 28, 2026
dbb34c1
fix: preserve composited window screenshots
austinywang Jul 28, 2026
25e8907
fix: type AppKit screenshot fallback
austinywang Jul 28, 2026
2b12a95
fix: bound composited screenshot routing
austinywang Jul 28, 2026
947f4d8
Merge remote-tracking branch 'origin/main' into issue-9065-remove-cgw…
austinywang Jul 28, 2026
8ed80bf
fix: bound composited screenshot failures
austinywang Jul 28, 2026
49bc794
Merge remote-tracking branch 'origin/main' into issue-9065-remove-cgw…
austinywang Jul 28, 2026
f1667ea
fix: latch compositor after capture timeout
austinywang Jul 28, 2026
527d503
test: cover invalid screenshot window numbers
austinywang Jul 29, 2026
03f37f6
fix: skip invalid screenshot window numbers
austinywang Jul 29, 2026
79f7124
Merge origin/main into issue-9065-remove-cgwindowlistcreateimage
Aug 1, 2026
cc9196c
fix: composite own window snapshots without capture permission
Aug 1, 2026
5670958
fix: bound permission-free screenshot compositing
Aug 1, 2026
a605f2e
fix: preserve screenshot fallback and overlay semantics
Aug 1, 2026
673bb9b
fix: serialize screenshot capture safely
Aug 1, 2026
d7a2c87
Merge remote-tracking branch 'origin/main' into issue-9065-remove-cgw…
Aug 1, 2026
45bbf1f
Merge remote-tracking branch 'origin/main' into issue-9065-remove-cgw…
Aug 7, 2026
471b0ac
fix: close screenshot capture review gaps
Aug 7, 2026
894b450
fix: preserve screenshot command contracts
Aug 7, 2026
0330d7a
fix: include native chrome in window captures
Aug 7, 2026
ac8082c
fix: recover and clip screenshot compositing
Aug 7, 2026
ea43159
refactor: give screenshot capture one lifetime lease
Aug 7, 2026
8d4788a
fix: isolate screenshot backend liveness
Aug 7, 2026
bbbf26c
Merge remote-tracking branch 'origin/main' into issue-9065-remove-cgw…
Aug 7, 2026
76a1d08
test: align OMP restore environment contract
Aug 7, 2026
9329825
fix: preserve efficient screenshot composition
Aug 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/GhosttyTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10695,8 +10695,8 @@ final class GhosttySurfaceScrollView: NSView {

/// Create a CGImage from the terminal's IOSurface-backed layer contents.
///
/// This avoids Screen Recording permissions (unlike CGWindowListCreateImage) and is therefore
/// suitable for debug socket tests running in headless/VM contexts.
/// This avoids the Screen Recording permission required by legacy window-list capture, making
/// it suitable for debug socket tests running in headless/VM contexts.
func debugCopyIOSurfaceCGImage() -> CGImage? {
guard let modelLayer = surfaceView.layer else { return nil }
let layer = modelLayer.presentation() ?? modelLayer
Expand Down
15 changes: 1 addition & 14 deletions Sources/TerminalController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13155,8 +13155,7 @@ class TerminalController {
return
}

guard let pngData = self.captureCompositedWindowPNGData(window)
?? self.captureAppKitWindowPNGData(window) else {
guard let pngData = self.captureAppKitWindowPNGData(window) else {
captureError = "Failed to create PNG data"
return
}
Expand All @@ -13176,18 +13175,6 @@ class TerminalController {
return "OK \(screenshotId) \(outputPath.path)"
}

private func captureCompositedWindowPNGData(_ window: NSWindow) -> Data? {
guard let cgImage = CGWindowListCreateImage(
.null,
.optionIncludingWindow,
CGWindowID(window.windowNumber),
[.boundsIgnoreFraming, .nominalResolution]
) else {
return nil
}
return NSBitmapImageRep(cgImage: cgImage).representation(using: .png, properties: [:])
}

private func captureAppKitWindowPNGData(_ window: NSWindow) -> Data? {
guard let contentView = window.contentView else {
return nil
Expand Down
190 changes: 190 additions & 0 deletions cmuxUITests/AutomationSocketUITests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import XCTest
import Foundation
import CoreGraphics
import ImageIO
import Darwin

final class AutomationSocketUITests: XCTestCase {
Expand Down Expand Up @@ -225,6 +227,194 @@ final class AutomationSocketUITests: XCTestCase {
XCTAssertEqual(typedTitles.first, "$iterate-pr")
}

func testWindowScreenshotCommandWritesNonBlankPNGWithTerminalContent() throws {
let app = configuredApp(mode: "allowAll")
app.launchArguments += [
"-AppleLanguages", "(en)",
"-AppleLocale", "en_US",
"-NSAppSleepDisabled", "YES",
]
defer { app.terminate() }

let activationOptions = XCTExpectedFailure.Options()
activationOptions.isStrict = false
activationOptions.issueMatcher = { issue in
let diagnostics = [
issue.compactDescription,
issue.detailedDescription,
issue.associatedError?.localizedDescription,
]
.compactMap { $0 }
.joined(separator: "\n")
return (issue.type == .system || issue.type == .assertionFailure) &&
diagnostics.contains("Failed to activate application") &&
diagnostics.contains("Running Background")
}
XCTExpectFailure("App activation may fail on headless CI runners", options: activationOptions) {
app.launch()
}
Comment thread
austinywang marked this conversation as resolved.
Resolved
XCTAssertTrue(
ensureRunningAfterLaunch(app, timeout: 12.0),
"Expected app to launch for the window screenshot test. state=\(app.state.rawValue)"
)
XCTAssertTrue(
waitForSocketPong(timeout: 12.0),
"Expected socket ping at \(socketPath). diagnostics=\(loadDiagnostics())"
)

let marker = "CMUX_SCREENSHOT_MARKER_9065"
let markerCommand = """
i=0; while [ $i -lt 8 ]; do printf '\\033[48;2;245;40;210m%-80s\\033[0m\\n' '\(marker)'; i=$((i+1)); done; tail -f /dev/null
"""
let workspace = try XCTUnwrap(
socketResult(
method: "workspace.create",
params: [
"title": "Window screenshot regression",
"initial_command": markerCommand,
"focus": true,
]
),
"Expected workspace.create to return the marker terminal"
)
let surfaceID = try XCTUnwrap(
workspace["surface_id"] as? String,
"Expected workspace.create to return a surface id"
)
XCTAssertTrue(
waitForTerminalText(marker, surfaceID: surfaceID, timeout: 12.0),
"Expected marker text to render before taking the screenshot"
)

XCTAssertTrue(
app.windows.firstMatch.waitForExistence(timeout: 5.0),
"Expected the main window to exist before taking the screenshot"
)

let screenshot = try XCTUnwrap(
socketResult(
method: "debug.window.screenshot",
params: ["label": "issue-9065-window"]
),
"Expected debug.window.screenshot to succeed"
)
let path = try XCTUnwrap(screenshot["path"] as? String)
let screenshotURL = URL(fileURLWithPath: path)
defer { try? FileManager.default.removeItem(at: screenshotURL) }

let pngData = try Data(contentsOf: screenshotURL)
XCTAssertGreaterThan(pngData.count, 1_024, "Expected a non-empty PNG file")
XCTAssertTrue(
pngData.starts(with: [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]),
"Expected the screenshot command to write PNG data"
)

let stats = try XCTUnwrap(windowScreenshotPixelStats(pngData))
XCTAssertGreaterThan(stats.width, 500, "Expected a main-window-sized screenshot")
XCTAssertGreaterThan(stats.height, 300, "Expected a main-window-sized screenshot")
XCTAssertGreaterThan(
stats.uniqueQuantizedColors,
8,
"Expected a non-blank, non-solid main window screenshot"
)
XCTAssertGreaterThan(
stats.markerPixels,
100,
"Expected the screenshot to include the terminal's magenta marker content"
)
}

private struct WindowScreenshotPixelStats {
let width: Int
let height: Int
let uniqueQuantizedColors: Int
let markerPixels: Int
}

private func waitForTerminalText(
_ expectedText: String,
surfaceID: String,
timeout: TimeInterval
) -> Bool {
let deadline = Date().addingTimeInterval(timeout)
repeat {
if let result = socketResult(
method: "surface.read_text",
params: ["surface_id": surfaceID]
),
let text = result["text"] as? String,
text.contains(expectedText) {
return true
}
RunLoop.current.run(until: Date().addingTimeInterval(0.1))
} while Date() < deadline
return false
}

private func windowScreenshotPixelStats(_ pngData: Data) -> WindowScreenshotPixelStats? {
guard let source = CGImageSourceCreateWithData(pngData as CFData, nil),
let image = CGImageSourceCreateImageAtIndex(source, 0, nil) else {
return nil
}

let width = image.width
let height = image.height
guard width > 0, height > 0 else { return nil }

let bytesPerPixel = 4
let bytesPerRow = width * bytesPerPixel
var pixels = [UInt8](repeating: 0, count: height * bytesPerRow)
let decoded = pixels.withUnsafeMutableBytes { rawBuffer -> Bool in
guard let baseAddress = rawBuffer.baseAddress,
let context = CGContext(
data: baseAddress,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: bytesPerRow,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGBitmapInfo.byteOrder32Big.rawValue
| CGImageAlphaInfo.premultipliedLast.rawValue
) else {
return false
}
context.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height))
return true
}
guard decoded else { return nil }

var uniqueQuantizedColors = Set<UInt16>()
var markerPixels = 0
let sampleStep = 2
for y in stride(from: 0, to: height, by: sampleStep) {
let row = y * bytesPerRow
for x in stride(from: 0, to: width, by: sampleStep) {
let index = row + x * bytesPerPixel
let red = pixels[index]
let green = pixels[index + 1]
let blue = pixels[index + 2]
let alpha = pixels[index + 3]

if alpha > 16 {
let key = (UInt16(red >> 4) << 8)
| (UInt16(green >> 4) << 4)
| UInt16(blue >> 4)
uniqueQuantizedColors.insert(key)
}
if red > 200, green < 110, blue > 150, alpha > 200 {
markerPixels += 1
}
}
}

return WindowScreenshotPixelStats(
width: width,
height: height,
uniqueQuantizedColors: uniqueQuantizedColors.count,
markerPixels: markerPixels
)
}

private func configuredApp(mode: String) -> XCUIApplication {
let app = XCUIApplication()
app.launchArguments += ["-\(modeKey)", mode]
Expand Down