Tests/OrgAppTests/PDFExportTests.swift
57 lines · 3634 bytes
1import Foundation
2import OrgCore
3import PDFKit
4import Testing
5@testable import OrgApp
6
7@MainActor
8struct PDFExportTests {
9 /// A long document comes out as several pages with its text, without Emacs.
10 @Test func paginatesTheHTMLExport() async throws {
11 let sections = (1...40).map { "* Section \($0)\n" + String(repeating: "Some words in a paragraph. ", count: 30) + "\n" }.joined()
12 let html = HTMLExport.page("#+TITLE: Long\n" + sections + "#+begin_src sh\necho done\n#+end_src\n", fallbackTitle: "t")
13 let data = try await PDFExport.pdf(html: html, directory: nil)
14 let document = try #require(PDFDocument(data: data))
15 #expect(document.pageCount > 3)
16 let text = document.string ?? ""
17 #expect(text.contains("Long") && text.contains("Section 40") && text.contains("echo done"))
18 let page = try #require(document.page(at: 0)).bounds(for: .mediaBox)
19 #expect(page.width < page.height)
20 // Page numbers, centred under the text, as annotations so the export's links stay.
21 let numbers = (0..<document.pageCount).compactMap { document.page(at: $0)?.annotations.first { $0.type == "FreeText" }?.contents }
22 #expect(numbers == (1...document.pageCount).map(String.init))
23 }
24
25 @Test func marginsFontAndNoNumbers() async throws {
26 let html = HTMLExport.page("#+TITLE: Short\nOne paragraph.\n", fallbackTitle: "t")
27 let data = try await PDFExport.pdf(html: html, directory: nil, options: PDFExport.Options(margin: 100, font: "Georgia", pageNumbers: false))
28 let document = try #require(PDFDocument(data: data))
29 #expect(document.page(at: 0)?.annotations.contains { $0.type == "FreeText" } == false)
30 let css = PDFExport.css(PDFExport.Options(margin: 100, font: "Georgia", pageNumbers: false))
31 #expect(css.contains("font-family: \"Georgia\""))
32 #expect(!PDFExport.css(PDFExport.Options()).contains("font-family"))
33 }
34
35 /// In the sandbox WebKit can't read the user's folders, so local images go in the page.
36 @Test func inlinesLocalImages() throws {
37 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-\(UUID().uuidString)")
38 try FileManager.default.createDirectory(at: folder.appendingPathComponent("img"), withIntermediateDirectories: true)
39 defer { try? FileManager.default.removeItem(at: folder) }
40 try Data([1, 2, 3]).write(to: folder.appendingPathComponent("img/a b.png"))
41 try Data([4]).write(to: folder.appendingPathComponent("c.svg"))
42 let absolute = folder.appendingPathComponent("c.svg").path
43 let html = #"<p><img src="img/a b.png" alt="a"> <img alt="c" src="\#(absolute)"> <img src="https://x.org/r.png"> <img src="gone.png"></p>"#
44 let inlined = PDFExport.inliningImages(html, directory: folder)
45 #expect(inlined.contains(#"<img src="data:image/png;base64,AQID" alt="a">"#))
46 #expect(inlined.contains(#"<img alt="c" src="data:image/svg+xml;base64,BA==">"#))
47 #expect(inlined.contains(#"<img src="https://x.org/r.png">"#))
48 #expect(inlined.contains(#"<img src="gone.png">"#))
49 #expect(PDFExport.inliningImages(#"<img src="img/a b.png">"#, directory: nil) == #"<img src="img/a b.png">"#)
50 }
51
52 /// The images to ask for access to before exporting.
53 @Test func localImageFiles() {
54 let html = #"<img src="img/a.png"> <img alt="c" src="/abs/c.svg"> <img src="https://x.org/r.png"> <img src="img/a.png">"#
55 #expect(PDFExport.imageFiles(html, directory: URL(fileURLWithPath: "/notes")) == ["/notes/img/a.png", "/abs/c.svg"])
56 }
57}