oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1@testable import BaseKit
2import Foundation
3import XCTest
4#if os(macOS)
5import AppKit
6#elseif os(iOS)
7import UIKit
8#endif
9
10/// A rich paste becomes the chat's Markdown (Paul, 2026-11-26). The HTML
11/// samples are the web's too (internal/browsertest/testdata/paste.json,
12/// TestAskPasteMarkdown): both converters must agree on every one.
13final class PasteMarkdownTests: XCTestCase {
14 struct Sample: Decodable { let name: String; let html: String?; let rtf: String?; let markdown: String? }
15
16 func testTheSharedSamples() throws {
17 let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()
18 .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
19 let data = try Data(contentsOf: root.appendingPathComponent("internal/browsertest/testdata/paste.json"))
20 let samples = try JSONDecoder().decode([Sample].self, from: data)
21 XCTAssertGreaterThan(samples.count, 5)
22 for sample in samples {
23 if let html = sample.html { XCTAssertEqual(PasteMarkdown.fromHTML(html), sample.markdown, sample.name) }
24 if let rtf = sample.rtf { XCTAssertEqual(PasteMarkdown.fromRTF(Data(rtf.utf8)), sample.markdown, sample.name) }
25 }
26 }
27
28 /// Fable's S5: a hostile clipboard (a page's copy handler can put any
29 /// HTML there) never crashes or stalls the paste, even on the iPhone's
30 /// 512 KB item-provider thread: nesting is flattened past 32 levels
31 /// and the walk stops at the field's 16,000 characters.
32 func testHostileClipboardsOnASmallStack() {
33 func onSmallStack(_ work: @escaping @Sendable () -> Void) {
34 let done = DispatchSemaphore(value: 0)
35 let thread = Thread { work(); done.signal() }
36 thread.stackSize = 512 * 1024
37 thread.start()
38 XCTAssertEqual(done.wait(timeout: .now() + 20), .success, "the paste stalled")
39 }
40 onSmallStack {
41 let deep = String(repeating: "<div><b>", count: 100_000) + "deep" + String(repeating: "</b></div>", count: 100_000)
42 XCTAssertEqual(PasteMarkdown.fromHTML(deep), "**deep**")
43 }
44 onSmallStack {
45 let lists = String(repeating: "<ul><li>item", count: 2_000)
46 let out = PasteMarkdown.fromHTML(lists) ?? ""
47 XCTAssertTrue(out.hasPrefix("- item\n - item"), String(out.prefix(40)))
48 XCTAssertLessThan(out.utf16.count, 16_000 + 200, "bounded")
49 }
50 onSmallStack {
51 let start = Date()
52 let stray = String(repeating: "<b>", count: 20_000) + "x" + String(repeating: "</i>", count: 20_000)
53 XCTAssertEqual(PasteMarkdown.fromHTML(stray), "**x**")
54 XCTAssertLessThan(Date().timeIntervalSince(start), 2)
55 }
56 let huge = "<b>x</b>" + String(repeating: "y", count: PasteMarkdown.maxHTML)
57 XCTAssertNil(PasteMarkdown.fromHTML(huge), "over 2 MB of HTML is pasted plain")
58 }
59
60 func testPlainHTMLIsLeftAlone() {
61 XCTAssertNil(PasteMarkdown.fromHTML("<p>Just words, 2*3 and *stars*</p><p>Two lines</p>"))
62 XCTAssertNil(PasteMarkdown.fromHTML(""))
63 XCTAssertNil(PasteMarkdown.fromHTML("<b style=\"font-weight:normal\">not bold</b>"))
64 }
65
66 /// RTF (Mail, TextEdit, Notes, Safari): bold and italic faces, links,
67 /// and text lists.
68 func testRTF() throws {
69 #if os(macOS)
70 let body = NSFont(name: "Helvetica", size: 13)!
71 let bold = NSFontManager.shared.convert(body, toHaveTrait: .boldFontMask)
72 let italic = NSFontManager.shared.convert(body, toHaveTrait: .italicFontMask)
73 #else
74 let body = UIFont(name: "Helvetica", size: 13)!
75 let bold = UIFont(descriptor: body.fontDescriptor.withSymbolicTraits(.traitBold)!, size: 13)
76 let italic = UIFont(descriptor: body.fontDescriptor.withSymbolicTraits(.traitItalic)!, size: 13)
77 #endif
78 let text = NSMutableAttributedString()
79 func add(_ s: String, _ font: Any = body, link: String? = nil, list: NSTextList? = nil) {
80 var attributes: [NSAttributedString.Key: Any] = [.font: font]
81 if let link { attributes[.link] = URL(string: link)! }
82 if let list {
83 let style = NSMutableParagraphStyle()
84 style.textLists = [list]
85 attributes[.paragraphStyle] = style
86 }
87 text.append(NSAttributedString(string: s, attributes: attributes))
88 }
89 add("The "); add("flour", bold); add(" comes "); add("Thursday", italic); add(", see "); add("the order", link: "https://fennel.example/order"); add(".\n")
90 add("\n")
91 let bullets = NSTextList(markerFormat: .disc, options: 0)
92 add("\t•\tRye\n", list: bullets)
93 add("\t•\tSpelt\n", list: bullets)
94 add("Thanks")
95 let rtf = try text.data(from: NSRange(location: 0, length: text.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
96 XCTAssertEqual(PasteMarkdown.fromRTF(rtf), "The **flour** comes *Thursday*, see [the order](https://fennel.example/order).\n\n- Rye\n- Spelt\n\nThanks")
97 XCTAssertNil(PasteMarkdown.fromRTF(try NSAttributedString(string: "Plain\nlines", attributes: [.font: body])
98 .data(from: NSRange(location: 0, length: 11), documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])))
99 }
100}
101