Tests/OrgAppTests/PDFExportTests.swift

34 lines · 1960 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}