import Foundation import OrgCore import OrgMathJax import PDFKit import Testing import WebKit @testable import OrgApp @MainActor struct PDFExportTests { init() { PDFExport.mathJaxURL = OrgMathJax.script } /// A long document comes out as several pages with its text, without Emacs. @Test func paginatesTheHTMLExport() async throws { let sections = (1...40).map { "* Section \($0)\n" + String(repeating: "Some words in a paragraph. ", count: 30) + "\n" }.joined() let html = HTMLExport.page("#+TITLE: Long\n" + sections + "#+begin_src sh\necho done\n#+end_src\n", fallbackTitle: "t") let data = try await PDFExport.pdf(html: html, directory: nil) let document = try #require(PDFDocument(data: data)) #expect(document.pageCount > 3) let text = document.string ?? "" #expect(text.contains("Long") && text.contains("Section 40") && text.contains("echo done")) let page = try #require(document.page(at: 0)).bounds(for: .mediaBox) #expect(page.width < page.height) // Page numbers, centred under the text, as annotations so the export's links stay. let numbers = (0..a c

"# let inlined = PDFExport.inliningImages(html, directory: folder) #expect(inlined.contains(#"a"#)) #expect(inlined.contains(#"c"#)) #expect(inlined.contains(#""#)) #expect(inlined.contains(#""#)) #expect(PDFExport.inliningImages(#""#, directory: nil) == #""#) } /// The images to ask for access to before exporting. @Test func localImageFiles() { let html = #" c "# #expect(PDFExport.imageFiles(html, directory: URL(fileURLWithPath: "/notes")) == ["/notes/img/a.png", "/abs/c.svg"]) } /// Export to HTML and Open on iOS: a temporary page with its images in it. @Test func previewHasItsImages() throws { let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-\(UUID().uuidString)") try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: folder) } try Data([1, 2, 3]).write(to: folder.appendingPathComponent("a.png")) let url = try HTMLPreview.write(#"

"#, directory: folder, name: "notes.html") defer { try? FileManager.default.removeItem(at: url.deletingLastPathComponent()) } #expect(url.lastPathComponent == "notes.html") #expect(try String(contentsOf: url, encoding: .utf8) == #"

"#) } /// The page PDF export renders has MathJax in it and no remote script; saved HTML keeps the CDN. @Test func bundlesMathJax() async throws { let html = HTMLExport.page("Math: \\(a^2\\)\n", fallbackTitle: "t") #expect(html.contains(HTMLExport.mathJaxScript)) let page = try PDFExport.bundlingMathJax(html) #expect(page.firstMatch(of: /(?i)]*\bsrc=/) == nil) #expect(page.contains("window.MathJax = { tex: ")) #expect(page.contains("