@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: "
", count: 100_000) + "deep" + String(repeating: "
", count: 100_000) XCTAssertEqual(PasteMarkdown.fromHTML(deep), "**deep**") } onSmallStack { let lists = String(repeating: "