Repository Public License
1// ORIX Convert — Packages/EngineDocuments/Tests/EngineDocumentsTests/Fixtures.swift2// Description: Test fixtures authored in code: a Markdown master (headings, emphasis, lists, table, image, link,3// footnote, code, math) converted by the bundled tools to every document format, a hand-written flat4// ODF presentation converted to PPTX / ODP / PPT, and packages assembled by editing their XML.5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import Foundation7import AppKit8import CoreGraphics9import ImageIO10import UniformTypeIdentifiers11@testable import EngineDocuments12import ConvertCore1314enum Probe {15 static let words = ["Marmalade", "boldprobe", "italicprobe", "zebrafish", "walrus", "Kumquat", "Rambutan", "platypus", "xylophone", "codeprobe"]16 static let cells = ["Kumquat", "42", "Rambutan", "17"]17 static let link = "https://example.org/target-page"18 static let title = "Quantum Marmalade Report"19 static let author = "Ada Lovelace"20 static let headings = ["Chapter One Heading", "Second Level Heading", "Chapter Two Heading"]21}2223actor FixtureStore {24 static let shared = FixtureStore()25 private var built: Task<Fixtures, Error>?2627 func get() async throws -> Fixtures {28 if let built { return try await built.value }29 let task = Task { try await Fixtures.build() }30 built = task31 return try await task.value32 }33}3435struct Fixtures: Sendable {36 let root: URL37 let documents: [FormatID: URL]38 let appleDocx: URL39 var pandocRTF: URL { root.appendingPathComponent("pandoc.rtf") }40 let deck: [FormatID: URL]41 let deckFODP: URL42 let pixel: URL4344 static let master = """45 ---46 title: Quantum Marmalade Report47 author: Ada Lovelace48 subject: Fixture subject49 keywords: [alpha, beta]50 lang: en-CA51 ---5253 # Chapter One Heading5455 This paragraph has **boldprobe** text and *italicprobe* text, plus a [link to example](https://example.org/target-page).5657 ## Second Level Heading5859 - First bullet zebrafish60 - Second bullet walrus6162 1. Numbered one63 2. Numbered two6465 | Fruit | Count |66 |----------|-------|67 | Kumquat | 42 |68 | Rambutan | 17 |6970 7172 A sentence with a footnote.[^1]7374 ```python75 def codeprobe():76 return 777 ```7879 Inline math $E = mc^2$ and display:8081 $$\\int_0^1 x^2\\,dx = \\frac{1}{3}$$8283 # Chapter Two Heading8485 Closing paragraph xylophone.8687 [^1]: Footnote body platypus.8889 """9091 /// Removes test directories left by runs older than one hour (once per process).92 private static let staleRemoved: Bool = {93 let base = FileManager.default.temporaryDirectory.appendingPathComponent("orix-docs-tests", isDirectory: true)94 let cutoff = Date().addingTimeInterval(-3600)95 for dir in (try? FileManager.default.contentsOfDirectory(at: base, includingPropertiesForKeys: [.contentModificationDateKey])) ?? [] {96 if let date = try? dir.resourceValues(forKeys: [.contentModificationDateKey]).contentModificationDate, date < cutoff { try? FileManager.default.removeItem(at: dir) }97 }98 return true99 }()100101 static func newDirectory(_ label: String = "case") throws -> URL {102 _ = staleRemoved103 let dir = FileManager.default.temporaryDirectory.appendingPathComponent("orix-docs-tests/\(label)-\(UUID().uuidString.prefix(8))", isDirectory: true)104 try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)105 return dir106 }107108 static func writePNG(_ url: URL, width: Int = 64, height: Int = 48) throws {109 let cs = CGColorSpaceCreateDeviceRGB()110 guard let ctx = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: cs, bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue) else { throw ConversionError.internalError("ctx") }111 ctx.setFillColor(CGColor(red: 0.1, green: 0.4, blue: 0.9, alpha: 1)); ctx.fill(CGRect(x: 0, y: 0, width: width, height: height))112 ctx.setFillColor(CGColor(red: 0.95, green: 0.6, blue: 0.1, alpha: 1)); ctx.fillEllipse(in: CGRect(x: 8, y: 8, width: width - 16, height: height - 16))113 guard let image = ctx.makeImage(), let dest = CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil) else { throw ConversionError.internalError("png") }114 CGImageDestinationAddImage(dest, image, nil)115 guard CGImageDestinationFinalize(dest) else { throw ConversionError.internalError("png write") }116 }117118 static func build() async throws -> Fixtures {119 let root = try newDirectory("fixtures")120 let pixel = root.appendingPathComponent("pixel.png")121 try writePNG(pixel)122 let md = root.appendingPathComponent("master.md")123 try Data(master.utf8).write(to: md)124 var docs: [FormatID: URL] = [.markdown: md]125 // Pandoc authors the rich fixtures from the master.126 for (format, ext, writer) in [(FormatID.docx, "docx", "docx"), (.odt, "odt", "odt"), (.html, "html", "html5"), (.epub, "epub", "epub3"), (.latex, "tex", "latex"), (.rst, "rst", "rst")] {127 let out = root.appendingPathComponent("master.\(ext)")128 _ = try await Pandoc.run(["-f", "markdown", "-t", writer, "--standalone", "--resource-path", root.path, "-o", out.path, md.path], scratch: root, workingDirectory: root)129 docs[format] = out130 }131 // LibreOffice authors the legacy DOC from the DOCX.132 let doc = root.appendingPathComponent("master.doc")133 _ = try await LibreOffice.convert(docs[.docx]!, filter: "doc:MS Word 97", to: doc, scratch: root)134 docs[.doc] = doc135 // Two RTF producers: LibreOffice (with a stylesheet, like word processors write) and Pandoc.136 let rtf = root.appendingPathComponent("master.rtf")137 _ = try await LibreOffice.convert(docs[.docx]!, filter: "rtf:Rich Text Format", to: rtf, scratch: root)138 docs[.rtf] = rtf139 let pandocRTF = root.appendingPathComponent("pandoc.rtf")140 _ = try await Pandoc.run(["-f", "markdown", "-t", "rtf", "--standalone", "--resource-path", root.path, "-o", pandocRTF.path, md.path], scratch: root, workingDirectory: root)141 let txt = root.appendingPathComponent("master.txt")142 try Data("Quantum Marmalade Report\n\nPlain text with boldprobe italicprobe zebrafish walrus.\nKumquat 42 Rambutan 17\n\nFootnote platypus, closing xylophone codeprobe.\n".utf8).write(to: txt)143 docs[.txt] = txt144145 // A DOCX written by the Apple text system (second producer).146 let appleDocx = root.appendingPathComponent("apple.docx")147 let body = NSMutableAttributedString(string: "Apple Heading Narwhal\n", attributes: [.font: NSFont.boldSystemFont(ofSize: 20)])148 body.append(NSAttributedString(string: "Body text with ocelot and ", attributes: [.font: NSFont.systemFont(ofSize: 12)]))149 body.append(NSAttributedString(string: "strongword", attributes: [.font: NSFont.boldSystemFont(ofSize: 12)]))150 body.append(NSAttributedString(string: " and a link.\n", attributes: [.font: NSFont.systemFont(ofSize: 12), .link: URL(string: "https://example.org/apple-link")!]))151 let data = try body.data(from: NSRange(location: 0, length: body.length), documentAttributes: [.documentType: NSAttributedString.DocumentType.officeOpenXML, .title: "Apple Fixture Title", .author: "Grace Hopper"])152 try data.write(to: appleDocx)153154 // Presentations: LibreOffice converts the hand-written flat ODF deck.155 let fodp = root.appendingPathComponent("deck.fodp")156 try Data(deckSource(imageBase64: try Data(contentsOf: pixel).base64EncodedString()).utf8).write(to: fodp)157 var deck: [FormatID: URL] = [:]158 for (format, filter) in [(FormatID.pptx, "pptx:Impress MS PowerPoint 2007 XML"), (.odp, "odp:impress8"), (.ppt, "ppt:MS PowerPoint 97")] {159 let out = root.appendingPathComponent("deck.\(format.rawValue)")160 _ = try await LibreOffice.convert(fodp, filter: filter, to: out, scratch: root)161 deck[format] = out162 }163 return Fixtures(root: root, documents: docs, appleDocx: appleDocx, deck: deck, deckFODP: fodp, pixel: pixel)164 }165166 static func deckSource(imageBase64: String) -> String {167 """168 <?xml version="1.0" encoding="UTF-8"?>169 <office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.presentation">170 <office:meta><dc:title>Deck Title Capybara</dc:title><meta:initial-creator>Grace Hopper</meta:initial-creator><dc:creator>Grace Hopper</dc:creator></office:meta>171 <office:automatic-styles>172 <style:page-layout style:name="PM1"><style:page-layout-properties fo:margin-top="0cm" fo:margin-bottom="0cm" fo:margin-left="0cm" fo:margin-right="0cm" fo:page-width="25.4cm" fo:page-height="14.288cm" style:print-orientation="landscape"/></style:page-layout>173 <style:page-layout style:name="PMN"><style:page-layout-properties fo:page-width="21cm" fo:page-height="29.7cm" style:print-orientation="portrait"/></style:page-layout>174 <style:style style:name="dp1" style:family="drawing-page"><style:drawing-page-properties presentation:transition-type="manual" smil:type="fade" smil:subtype="crossfade"/></style:style>175 <style:style style:name="dp2" style:family="drawing-page"/>176 </office:automatic-styles>177 <office:master-styles>178 <style:master-page style:name="Default" style:page-layout-name="PM1" draw:style-name="dp2"><presentation:notes style:page-layout-name="PMN"/></style:master-page>179 </office:master-styles>180 <office:body><office:presentation>181 <draw:page draw:name="page1" draw:style-name="dp1" draw:master-page-name="Default">182 <draw:frame presentation:class="title" svg:x="1.2cm" svg:y="0.8cm" svg:width="23cm" svg:height="2.5cm"><draw:text-box><text:p>Opening Slide Aardvark</text:p></draw:text-box></draw:frame>183 <draw:frame presentation:class="outline" svg:x="1.2cm" svg:y="4cm" svg:width="23cm" svg:height="8cm"><draw:text-box><text:p>First body line ocelot</text:p><text:p>Second body line pangolin</text:p></draw:text-box></draw:frame>184 <presentation:notes><draw:frame presentation:class="notes" svg:x="2cm" svg:y="14cm" svg:width="17cm" svg:height="12cm"><draw:text-box><text:p>Speaker note one narwhal</text:p></draw:text-box></draw:frame></presentation:notes>185 </draw:page>186 <draw:page draw:name="page2" draw:style-name="dp2" draw:master-page-name="Default">187 <draw:frame presentation:class="title" svg:x="1.2cm" svg:y="0.8cm" svg:width="23cm" svg:height="2.5cm"><draw:text-box><text:p>Picture Slide Quokka</text:p></draw:text-box></draw:frame>188 <draw:frame svg:x="3cm" svg:y="4cm" svg:width="8cm" svg:height="6cm"><draw:image><office:binary-data>\(imageBase64)</office:binary-data></draw:image></draw:frame>189 <presentation:notes><draw:frame presentation:class="notes" svg:x="2cm" svg:y="14cm" svg:width="17cm" svg:height="12cm"><draw:text-box><text:p>Speaker note two axolotl</text:p></draw:text-box></draw:frame></presentation:notes>190 </draw:page>191 <draw:page draw:name="page3" draw:style-name="dp2" draw:master-page-name="Default">192 <draw:frame presentation:class="title" svg:x="1.2cm" svg:y="0.8cm" svg:width="23cm" svg:height="2.5cm"><draw:text-box><text:p>Table Slide Wombat</text:p></draw:text-box></draw:frame>193 <draw:frame svg:x="2cm" svg:y="4cm" svg:width="20cm" svg:height="4cm"><table:table><table:table-column/><table:table-column/><table:table-row><table:table-cell><text:p>Region</text:p></table:table-cell><table:table-cell><text:p>Revenue</text:p></table:table-cell></table:table-row><table:table-row><table:table-cell><text:p>Saguenay</text:p></table:table-cell><table:table-cell><text:p>9001</text:p></table:table-cell></table:table-row></table:table></draw:frame>194 </draw:page>195 </office:presentation></office:body>196 </office:document>197 """198 }199}200201/// Collects warnings and progress emitted by an engine during a test.202final class Recorder: @unchecked Sendable {203 private let lock = NSLock()204 private var _warnings: [ConversionWarning] = []205 private var _progress: [ProgressUpdate] = []206 var warnings: [ConversionWarning] { lock.lock(); defer { lock.unlock() }; return _warnings }207 var progress: [ProgressUpdate] { lock.lock(); defer { lock.unlock() }; return _progress }208 func context(_ dir: URL) -> ConversionContext {209 ConversionContext(workDirectory: dir, progress: { p in self.lock.lock(); self._progress.append(p); self.lock.unlock() },210 warn: { w in self.lock.lock(); self._warnings.append(w); self.lock.unlock() })211 }212 func has(_ code: String) -> Bool { warnings.contains { $0.code == code } }213}214215// MARK: - Test helpers216217enum Engines {218 static let all: [any ConversionEngine] = makeEngines()219 static let documents = DocumentsEngine()220 static let ebooks = EbooksEngine()221 static let presentations = PresentationsEngine()222223 static func owning(_ path: ConversionPath) -> any ConversionEngine { all.first { $0.id == path.engine }! }224 static func path(_ id: String) -> ConversionPath? { all.flatMap(\.paths).first { $0.id == id } }225}226227struct Run {228 let output: ConversionOutput229 let checks: [ValidationCheck]230 let recorder: Recorder231 let request: ConversionRequest232 let seconds: Double233 var file: URL { output.files[0] }234 var failedChecks: [ValidationCheck] { checks.filter { !$0.passed } }235}236237/// Converts through the engine that declares the path, then validates the output.238func convert(_ source: URL, _ s: FormatID, _ d: FormatID, operation: String? = nil, options: [String: OptionValue] = [:], engine: (any ConversionEngine)? = nil) async throws -> Run {239 let candidates = engine.map { [$0] } ?? Engines.all240 var request = ConversionRequest(source: source, sourceFormat: s, destinationFormat: d, operation: operation, options: ConversionOptions(options))241 request.outputBaseName = source.deletingPathExtension().lastPathComponent242 guard let owner = candidates.first(where: { $0.path(for: request) != nil }) else { throw ConversionError.unsupportedPath(source: s, destination: d) }243 let dir = try Fixtures.newDirectory("run")244 let recorder = Recorder()245 let started = Date()246 let output = try await owner.convert(request, context: recorder.context(dir))247 let seconds = Date().timeIntervalSince(started)248 let checks = try await owner.validate(output, request: request, source: nil)249 return Run(output: output, checks: checks, recorder: recorder, request: request, seconds: seconds)250}251252/// Rewrites a ZIP package, replacing / adding / removing parts (`nil` removes).253func repackage(_ source: URL, to destination: URL, edits: [String: Data?], transform: [String: (String) -> String] = [:]) throws {254 let zip = try ZipReader(url: source)255 var writer = ZipWriter()256 var done = Set<String>()257 for e in zip.entries.sorted(by: { ($0.name == "mimetype" ? 0 : 1) < ($1.name == "mimetype" ? 0 : 1) }) where !e.isDirectory {258 if let edit = edits[e.name] { done.insert(e.name); if let data = edit { try writer.add(e.name, data, store: e.name == "mimetype") }; continue }259 var data = try zip.data(for: e)260 if let t = transform[e.name] { data = Data(t(String(decoding: data, as: UTF8.self)).utf8) }261 try writer.add(e.name, data, store: e.name == "mimetype")262 }263 for (name, edit) in edits where !done.contains(name) { if let data = edit { try writer.add(name, data) } }264 try writer.finish().write(to: destination)265}266267func facts(_ url: URL, _ format: FormatID) throws -> DocumentFacts { try DocumentFacts.extract(url, format: format) }268269func normalized(_ s: String) -> String { s.precomposedStringWithCompatibilityMapping.lowercased() }270