test: cover attachment-only rtfd image paste fallback

This commit is contained in:
Lawrence Chen 2026-03-13 05:08:49 -07:00
parent adcd0be0f7
commit b7de225364

View file

@ -956,6 +956,59 @@ final class GhosttyPasteboardHelperTests: XCTestCase {
XCTAssertTrue(imagePath.hasSuffix(".jpeg"))
XCTAssertTrue(FileManager.default.fileExists(atPath: imagePath))
}
func testAttachmentOnlyRTFDClipboardFallsBackToImagePath() throws {
let pasteboard = NSPasteboard(name: .init("cmux-test-rtfd-attachment-\(UUID().uuidString)"))
pasteboard.clearContents()
let image = NSImage(size: NSSize(width: 1, height: 1))
image.lockFocus()
NSColor.orange.setFill()
NSRect(x: 0, y: 0, width: 1, height: 1).fill()
image.unlockFocus()
let attachment = NSTextAttachment()
attachment.image = image
let attributed = NSAttributedString(attachment: attachment)
let data = try attributed.data(
from: NSRange(location: 0, length: attributed.length),
documentAttributes: [.documentType: NSAttributedString.DocumentType.rtfd]
)
pasteboard.setData(data, forType: .rtfd)
XCTAssertNil(cmuxPasteboardStringContentsForTesting(pasteboard))
let imagePath = try XCTUnwrap(cmuxPasteboardImagePathForTesting(pasteboard))
defer { try? FileManager.default.removeItem(atPath: imagePath) }
XCTAssertTrue(imagePath.hasSuffix(".tiff"))
XCTAssertTrue(FileManager.default.fileExists(atPath: imagePath))
}
func testRTFDClipboardWithVisibleTextPrefersText() throws {
let pasteboard = NSPasteboard(name: .init("cmux-test-rtfd-text-\(UUID().uuidString)"))
pasteboard.clearContents()
let image = NSImage(size: NSSize(width: 1, height: 1))
image.lockFocus()
NSColor.purple.setFill()
NSRect(x: 0, y: 0, width: 1, height: 1).fill()
image.unlockFocus()
let attachment = NSTextAttachment()
attachment.image = image
let attributed = NSMutableAttributedString(string: "Hello ")
attributed.append(NSAttributedString(attachment: attachment))
let data = try attributed.data(
from: NSRange(location: 0, length: attributed.length),
documentAttributes: [.documentType: NSAttributedString.DocumentType.rtfd]
)
pasteboard.setData(data, forType: .rtfd)
XCTAssertEqual(cmuxPasteboardStringContentsForTesting(pasteboard), "Hello")
XCTAssertNil(cmuxPasteboardImagePathForTesting(pasteboard))
}
}
@MainActor