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..

"#))
#expect(inlined.contains(#"
"#))
#expect(PDFExport.inliningImages(#"
"#, directory: nil) == #"
"#)
}
/// The images to ask for access to before exporting.
@Test func localImageFiles() {
let html = #"
"#
#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(#"