@testable import BaseKit import Foundation import XCTest #if os(macOS) import AppKit #elseif os(iOS) import UIKit #endif /// A rich paste becomes the chat's Markdown (Paul, 2026-11-26). The HTML /// samples are the web's too (internal/browsertest/testdata/paste.json, /// TestAskPasteMarkdown): both converters must agree on every one. final class PasteMarkdownTests: XCTestCase { struct Sample: Decodable { let name: String; let html: String?; let rtf: String?; let markdown: String? } func testTheSharedSamples() throws { let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent() .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() let data = try Data(contentsOf: root.appendingPathComponent("internal/browsertest/testdata/paste.json")) let samples = try JSONDecoder().decode([Sample].self, from: data) XCTAssertGreaterThan(samples.count, 5) for sample in samples { if let html = sample.html { XCTAssertEqual(PasteMarkdown.fromHTML(html), sample.markdown, sample.name) } if let rtf = sample.rtf { XCTAssertEqual(PasteMarkdown.fromRTF(Data(rtf.utf8)), sample.markdown, sample.name) } } } /// Fable's S5: a hostile clipboard (a page's copy handler can put any /// HTML there) never crashes or stalls the paste, even on the iPhone's /// 512 KB item-provider thread: nesting is flattened past 32 levels /// and the walk stops at the field's 16,000 characters. func testHostileClipboardsOnASmallStack() { func onSmallStack(_ work: @escaping @Sendable () -> Void) { let done = DispatchSemaphore(value: 0) let thread = Thread { work(); done.signal() } thread.stackSize = 512 * 1024 thread.start() XCTAssertEqual(done.wait(timeout: .now() + 20), .success, "the paste stalled") } onSmallStack { let deep = String(repeating: "
Just words, 2*3 and *stars*
Two lines
")) XCTAssertNil(PasteMarkdown.fromHTML("")) XCTAssertNil(PasteMarkdown.fromHTML("not bold")) } /// RTF (Mail, TextEdit, Notes, Safari): bold and italic faces, links, /// and text lists. func testRTF() throws { #if os(macOS) let body = NSFont(name: "Helvetica", size: 13)! let bold = NSFontManager.shared.convert(body, toHaveTrait: .boldFontMask) let italic = NSFontManager.shared.convert(body, toHaveTrait: .italicFontMask) #else let body = UIFont(name: "Helvetica", size: 13)! let bold = UIFont(descriptor: body.fontDescriptor.withSymbolicTraits(.traitBold)!, size: 13) let italic = UIFont(descriptor: body.fontDescriptor.withSymbolicTraits(.traitItalic)!, size: 13) #endif let text = NSMutableAttributedString() func add(_ s: String, _ font: Any = body, link: String? = nil, list: NSTextList? = nil) { var attributes: [NSAttributedString.Key: Any] = [.font: font] if let link { attributes[.link] = URL(string: link)! } if let list { let style = NSMutableParagraphStyle() style.textLists = [list] attributes[.paragraphStyle] = style } text.append(NSAttributedString(string: s, attributes: attributes)) } add("The "); add("flour", bold); add(" comes "); add("Thursday", italic); add(", see "); add("the order", link: "https://fennel.example/order"); add(".\n") add("\n") let bullets = NSTextList(markerFormat: .disc, options: 0) add("\t•\tRye\n", list: bullets) add("\t•\tSpelt\n", list: bullets) add("Thanks") let rtf = try text.data(from: NSRange(location: 0, length: text.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf]) XCTAssertEqual(PasteMarkdown.fromRTF(rtf), "The **flour** comes *Thursday*, see [the order](https://fennel.example/order).\n\n- Rye\n- Spelt\n\nThanks") XCTAssertNil(PasteMarkdown.fromRTF(try NSAttributedString(string: "Plain\nlines", attributes: [.font: body]) .data(from: NSRange(location: 0, length: 11), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf]))) } }