SPB Git

Repository Public License

DocumentPathTests.swift 9.8 KB 157 lines swift

1// ORIX Convert — Packages/EngineDocuments/Tests/EngineDocumentsTests/DocumentPathTests.swift2// Description: Converts a real fixture through every declared document / ebook conversion path, validates the3//              output and asserts that what the path claims to preserve really survives.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Testing6import Foundation7@testable import EngineDocuments8import ConvertCore910let documentConversionPathIDs: [String] = (Engines.documents.paths + Engines.ebooks.paths).filter { $0.operation == nil }.map(\.id).sorted()1112struct AlternateRoute: Sendable, CustomTestStringConvertible {13    let source: FormatID, destination: FormatID, backend: Backend14    var testDescription: String { "\(source.rawValue)>\(destination.rawValue) [\(backend.rawValue)]" }15}1617let alternateRoutes: [AlternateRoute] = DocumentMatrix.sources.flatMap { s in18    DocumentMatrix.destinations.flatMap { d in DocumentMatrix.routes(s, d).dropFirst().map { AlternateRoute(source: s, destination: d, backend: $0.backend) } }19}2021@Suite("Document and ebook conversion paths")22struct DocumentPathTests {23    @Test func catalog() {24        let paths = Engines.all.flatMap(\.paths)25        #expect(Set(paths.map(\.id)).count == paths.count, "path identifiers are unique")26        #expect(documentConversionPathIDs.count == 99)27        // Declared but unsupported formats must not leak in.28        #expect(!paths.contains { $0.source == .pages || $0.source == .keynote || $0.destination == .pages || $0.destination == .keynote })29        #expect(!paths.contains { $0.source == .latex && $0.destination == .pdf }, "LaTeX → PDF needs TeX, which is not bundled")30        #expect(!paths.contains { $0.source == .pdf }, "PDF is a target only in this engine")31        for p in paths { #expect(!p.limitations.isEmpty, "\(p.id) states its limitations") }32        for engine in Engines.all { #expect(engine.availability() == .available) }33    }3435    @Test(arguments: documentConversionPathIDs)36    func declaredPath(_ id: String) async throws {37        let path = try #require(Engines.path(id))38        let fx = try await FixtureStore.shared.get()39        let s = path.source, d = path.destination40        let source = try #require(fx.documents[s])41        let run = try await convert(source, s, d)42        #expect(run.failedChecks.isEmpty, "validation: \(run.failedChecks)")43        #expect(!run.checks.isEmpty)4445        // Read the result back. DOC is read through an independent LibreOffice round trip to DOCX for structure.46        var out = try facts(run.file, d)47        var structural = out48        if d == .doc || d == .rtf {49            let back = try Fixtures.newDirectory("roundtrip").appendingPathComponent("back.docx")50            _ = try await LibreOffice.convert(run.file, filter: "docx:MS Word 2007 XML", to: back, scratch: back.deletingLastPathComponent())51            structural = try facts(back, .docx)52            out.title = out.title ?? structural.title53        }54        let text = normalized(out.searchText + "\n" + structural.searchText)55        let missingWords = Validation.missingProbes(Probe.words, in: text)56        if d == .pdf, DocumentMatrix.routes(s, d).first?.route == .libreoffice {57            // Known LibreOffice quirk: with some substituted fonts the PDF text layer maps an fi-ligature cluster58            // to extra letters ("zebrafissh"), although the page renders correctly. At most that one word may differ.59            #expect(missingWords.isEmpty || missingWords == ["zebrafish"], "missing: \(missingWords)")60        } else {61            #expect(missingWords.isEmpty, "probe words missing from the output: \(missingWords)")62        }6364        let p = path.preserves65        if p.contains(.textStyles) {66            #expect(structural.hasBold || out.hasBold, "bold survives")67            #expect(structural.hasItalic || out.hasItalic, "italic survives")68        }69        if p.contains(.tables) {70            #expect(structural.tableCount >= 1, "the table is still a table")71            if [.docx, .odt, .html, .epub, .markdown, .rst, .doc].contains(d) {72                for cell in Probe.cells { #expect(structural.tableCells.contains { $0.contains(cell) }, "cell \(cell) is in a table cell") }73            }74        } else if s != .txt {75            for cell in Probe.cells { #expect(text.contains(cell.lowercased()), "cell text \(cell) remains as text") }76        }77        if p.contains(.images) { #expect(structural.imageCount >= 1, "the image survives") }78        if p.contains(.links) { #expect((structural.links + out.links).contains { $0.hasPrefix(Probe.link) }, "the link target survives: \(structural.links)") }79        if p.contains(.metadata) {80            #expect(out.title == Probe.title, "title carried: \(out.title ?? "nil")")81            if [.docx, .odt, .epub, .pdf, .html, .doc].contains(d) { #expect(out.author == Probe.author, "author carried: \(out.author ?? "nil")") }82        }83        if p.contains(.chapters) {84            for heading in Probe.headings { #expect(structural.headings.contains { $0.text.contains(heading) }, "heading \(heading) survives: \(structural.headings)") }85        }86        if s != .txt, [.docx, .odt, .html, .markdown, .latex, .rst].contains(d), !(s == .rtf || s == .doc) || true {87            // Heading levels: two top-level chapters with one second-level heading between them.88            let levels = structural.headings.filter { h in Probe.headings.contains { h.text.contains($0) } }.map(\.level)89            if levels.count == 3 { #expect(levels[0] == levels[2] && levels[1] == levels[0] + 1, "heading levels kept: \(levels)") }90            else { #expect(levels.count == 3, "three headings found in \(d.rawValue): \(structural.headings)") }91        }92        // No silent loss: whatever the path does not preserve must have been announced when the source had it.93        if s != .txt, !p.contains(.images), d != .txt {94            #expect(run.recorder.has("images-not-written") || run.recorder.has("images-dropped"), "image loss is announced: \(run.recorder.warnings.map(\.code))")95        }96        if d == .txt, s != .txt { #expect(run.recorder.has("styles-dropped")) }97        // Outputs stay inside the work directory and nothing else is left behind.98        let workDir = run.file.deletingLastPathComponent()99        let leftovers = try FileManager.default.contentsOfDirectory(atPath: workDir.path)100        #expect(leftovers == [run.file.lastPathComponent], "only the output remains in the work directory: \(leftovers)")101    }102103    @Test(arguments: alternateRoutes)104    func alternateEngine(_ route: AlternateRoute) async throws {105        let fx = try await FixtureStore.shared.get()106        let source = try #require(fx.documents[route.source])107        let src = try facts(source, route.source)108        let run = try await convert(source, route.source, route.destination, options: ["engine": .string(route.backend.rawValue)])109        let structure = Validation.structure(run.file, format: route.destination)110        let structureFailures = structure.filter { !$0.passed }111        #expect(structureFailures.isEmpty, "structure: \(structureFailures)")112        let out = try facts(run.file, route.destination)113        let missing = Validation.missingProbes(Probe.words, in: out.searchText)114        #expect(missing.count <= 1, "at most the footnote word may be missing: \(missing)")115        // Whatever this engine loses must be reported.116        if !missing.isEmpty { #expect(run.recorder.has("text-missing") || run.recorder.has("footnotes"), "text loss announced: \(run.recorder.warnings.map(\.code))") }117        if [.docx, .odt, .html, .rtf].contains(route.destination) {118            if out.imageCount < src.imageCount { #expect(run.recorder.has("images-dropped")) }119            if out.tableCount < src.tableCount { #expect(run.recorder.has("tables-flattened")) }120        }121        // The option is offered for this pair.122        let path = try #require(Engines.all.flatMap(\.paths).first { $0.source == route.source && $0.destination == route.destination && $0.operation == nil })123        let option = try #require(Engines.owning(path).options(for: path).first { $0.id == "engine" })124        guard case .choice(_, let choices) = option.kind else { Issue.record("engine is a choice"); return }125        #expect(choices.contains { $0.value == route.backend.rawValue })126    }127128    @Test func engineOptionOnlyWhereSeveralWork() {129        for path in Engines.documents.paths + Engines.ebooks.paths where path.operation == nil {130            let has = Engines.owning(path).options(for: path).contains { $0.id == "engine" }131            #expect(has == (DocumentMatrix.routes(path.source, path.destination).count > 1), "\(path.id)")132        }133        #expect(throws: ConversionError.self) {134            _ = try DocumentConverter.pdfFilterOptions(ConversionOptions(["pdf.pages": .string("1-x")]))135        }136    }137138    @Test func secondProducers() async throws {139        // A DOCX written by the Apple text system and an RTF written by Pandoc go through the same paths.140        let fx = try await FixtureStore.shared.get()141        let pdf = try await convert(fx.appleDocx, .docx, .pdf)142        #expect(pdf.failedChecks.isEmpty)143        let f = try facts(pdf.file, .pdf)144        #expect(normalized(f.text).contains("narwhal") && normalized(f.text).contains("strongword"))145        #expect(f.title == "Apple Fixture Title" && f.author == "Grace Hopper")146        #expect(f.hasBold)147        let md = try await convert(fx.appleDocx, .docx, .markdown)148        let text = try String(contentsOf: md.file, encoding: .utf8)149        #expect(text.contains("**strongword**") && text.contains("ocelot"))150        // (The Apple DOCX writer does not store the link attribute at all, so there is no link to carry.)151        let rtf = try await convert(fx.pandocRTF, .rtf, .docx)152        #expect(rtf.failedChecks.isEmpty)153        let rtfFacts = try facts(rtf.file, .docx)154        #expect(rtfFacts.tableCells.contains { $0.contains("Kumquat") })155    }156}157
Files main
Menu

Theme