SPB Git

Repository Public

ExternalValidationTests.swift 14.0 KB 216 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocDOCXTests/ExternalValidationTests.swift3// Description: Exported files judged by independent readers: pandoc (text, AST shape, footnotes, math), macOS textutil (text)4//              and LibreOffice headless (PDF and text; slow, behind ORIX_SLOW_TESTS=1). Microsoft Word is not used.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Testing10import DocModel11import DocContainer12@testable import DocDOCX1314@Suite("DOCX export — validated by independent tools")15struct ExternalValidationTests {16    func exported() throws -> (doc: DocumentModel, data: Data) {17        let doc = richDocument()18        var options = DocxExportOptions()19        options.citations = FixedFormatter()20        return (doc, try DocxExporter.export(doc, options: options).data)21    }2223    /// The text a reader should see: body paragraphs with tracked changes accepted, in order.24    func expectedBodyWords(_ doc: DocumentModel) -> [String] {25        var words: [String] = []26        for b in doc.body {27            switch b {28            case .paragraph(let p): words += wordSequence(p.runs.filter { !$0.isDeleted && $0.inline == nil }.map(\.text).joined())29            case .table(let t): for r in t.rows { for c in r.cells { words += wordSequence(c.plainText) } }30            default: break31            }32        }33        return words34    }3536    func walk(_ json: Any, _ visit: ([String: Any]) -> Void) {37        if let d = json as? [String: Any] {38            if d["t"] is String { visit(d) }39            for v in d.values { walk(v, visit) }40        } else if let a = json as? [Any] {41            for v in a { walk(v, visit) }42        }43    }4445    @Test func pandocReadsTextStructureFootnotesAndMath() throws {46        guard Tool.pandoc != nil else { recordSkip("pandoc missing: pandoc validation of exported DOCX not run"); return }47        let (doc, data) = try exported()48        let jsonText = try #require(Tool.pandocText(data, to: "json", extra: ["--track-changes=accept"]))49        let ast = try JSONSerialization.jsonObject(with: Data(jsonText.utf8))50        var counts: [String: Int] = [:]51        var headers: [(Int, String)] = []52        var tableShape: (rows: Int, columns: Int)?53        var math: [String] = [], links: [String] = [], images = 0, noteTexts: [String] = []54        func text(_ any: Any) -> String {55            var s = ""56            walk(any) { n in57                if n["t"] as? String == "Str", let c = n["c"] as? String { s += c }58                if ["Space", "SoftBreak"].contains(n["t"] as? String ?? "") { s += " " }59            }60            return s61        }62        func plain(_ inlines: Any) -> String {63            // Order-preserving text of a list of inlines.64            guard let a = inlines as? [Any] else { return "" }65            return a.map { item -> String in66                guard let d = item as? [String: Any], let t = d["t"] as? String else { return "" }67                switch t {68                case "Str": return d["c"] as? String ?? ""69                case "Space", "SoftBreak": return " "70                case "Strong", "Emph", "Underline", "Span", "Link", "Strikeout", "SmallCaps", "Superscript", "Subscript":71                    if let c = d["c"] as? [Any] { return c.map { plain($0) }.joined() }72                    return ""73                default: return ""74                }75            }.joined()76        }77        walk(ast) { n in78            let t = n["t"] as! String79            counts[t, default: 0] += 180            switch t {81            case "Header":82                if let c = n["c"] as? [Any], let level = c.first as? Int { headers.append((level, plain(c[2]))) }83            case "Table":84                if let c = n["c"] as? [Any], c.count >= 5, let colSpecs = c[2] as? [Any] {85                    var rows = 086                    if let head = c[3] as? [Any], let hr = head.last as? [Any] { rows += hr.count }87                    if let bodies = c[4] as? [Any] { for b in bodies { if let bb = b as? [Any], bb.count >= 4, let r = bb[3] as? [Any] { rows += r.count } } }88                    tableShape = (rows, colSpecs.count)89                }90            case "Math":91                if let c = n["c"] as? [Any], let s = c.last as? String { math.append(s) }92            case "Link":93                if let c = n["c"] as? [Any], let target = c.last as? [Any], let url = target.first as? String { links.append(url) }94            case "Image": images += 195            case "Note": noteTexts.append(text(n["c"] as Any))96            default: break97            }98        }99        // Headings with their levels.100        #expect(headers.map(\.0) == [1, 2, 1])101        #expect(headers.map(\.1) == ["Rich document", "Background", "Methods"])102        // Table shape: 4 rows × 3 columns.103        #expect(tableShape?.rows == 4 && tableShape?.columns == 3)104        // Footnotes and the endnote, with their text.105        #expect(noteTexts.count == 3)106        #expect(noteTexts.map { wordSequence($0) } == [wordSequence("First footnote text."), wordSequence("Second footnote, first paragraph. Second footnote, second paragraph."), wordSequence("The only endnote.")])107        // Math comes back as the same LaTeX (pandoc writes its own spacing).108        func squeeze(_ s: String) -> String { s.filter { !$0.isWhitespace } }109        #expect(math.map(squeeze) == ["E=mc^{2}", "\\frac{a+b}{c}=\\sqrt{x^{2}+y^{2}}"])110        #expect(links.contains("https://example.org/rich"))111        #expect(images == 1)112        #expect((counts["BulletList"] ?? 0) >= 1 && (counts["OrderedList"] ?? 0) >= 1)113        // Text: every word of the body, in order (pandoc drops nothing we wrote; it adds note, field and bibliography text).114        let plainText = try #require(Tool.pandocText(data, extra: ["--track-changes=accept"]))115        let integrity = textIntegrity(reference: expectedBodyWords(doc), candidate: wordSequence(plainText))116        #expect(integrity == 1.0, "pandoc sees \(integrity * 100) % of the body words")117        // Tracked changes: pandoc can show them; the deletion is marked as such, not as live text.118        let tracked = try #require(Tool.pandocText(data, to: "markdown", extra: ["--track-changes=all"]))119        #expect(tracked.contains("{.insertion") && tracked.contains("{.deletion") && tracked.contains("author=\"Editor\""))120        #expect(!plainText.contains("tracked deletion") && plainText.contains("tracked insertion"))121        // Comments with their author.122        #expect(tracked.contains("{.comment-start") && tracked.contains("author=\"Reviewer One\"") && tracked.contains("Is this right?"))123    }124125    @Test func pandocReadsCorpusReexportsWithTheSameText() throws {126        guard Tool.pandoc != nil else { recordSkip("pandoc missing: corpus re-export validation not run"); return }127        for name in Corpus.toolFiles {128            let original = try Corpus.data(name)129            let reexported = try DocxExporter.export(try DocxImporter.importDocument(original).document).data130            let before = wordSequence(try #require(Tool.pandocText(original)))131            let after = wordSequence(try #require(Tool.pandocText(reexported), "pandoc cannot read our export of \(name)"))132            // The contents list is regenerated from the headings, so compare without requiring cached-entry text; everything else must match.133            let integrity = textIntegrity(reference: before, candidate: after)134            #expect(integrity >= (name.contains("toc") ? 0.9 : 1.0), "\(name): \(integrity * 100) % of the words pandoc saw in the original are in the re-export")135        }136    }137138    @Test func textutilReadsTheSameText() throws {139        guard Tool.textutil != nil else { recordSkip("textutil missing: textutil validation not run"); return }140        let (doc, data) = try exported()141        let text = try #require(Tool.textutilText(data), "textutil cannot read the exported file")142        // textutil shows the text with tracked deletions left out and fields rendered from their cached results.143        let expected = expectedBodyWords(doc)144        let integrity = textIntegrity(reference: expected, candidate: wordSequence(text))145        #expect(integrity == 1.0, "textutil sees \(integrity * 100) % of the body words")146        for cell in ["Apples", "Merged across two", "9.50", "Pears"] { #expect(text.contains(cell)) }147        #expect(text.contains("tracked insertion") && !text.contains("tracked deletion"))148        #expect(text.contains("Table 1: Fruit prices") && text.contains("see Table 1.") && text.contains("cited by [1]"))149    }150151    @Test func libreOfficeConvertsExportsToPDFAndText() throws {152        guard Tool.slowTestsEnabled else { recordSkip("ORIX_SLOW_TESTS is not 1: LibreOffice conversion of exported DOCX not run"); return }153        guard Tool.soffice != nil else { recordSkip("soffice missing: LibreOffice validation not run"); return }154        let (doc, data) = try exported()155        let pdf = try #require(Tool.sofficeConvert(data, to: "pdf"), "LibreOffice could not convert the exported DOCX to PDF")156        #expect(pdf.prefix(5) == Data("%PDF-".utf8))157        #expect(pdf.count > 5_000)158        let txt = try #require(Tool.sofficeConvert(data, to: "txt:Text (encoded):UTF8"), "LibreOffice could not convert the exported DOCX to text")159        // LibreOffice's text export glues note marks to the preceding word ("note1", "herei"): the arabic ones are removed here,160        // the roman endnote mark costs one word, hence the threshold.161        let lo = String(decoding: txt, as: UTF8.self).replacingOccurrences(of: "(?<=\\p{L})\\d+", with: "", options: .regularExpression)162        let integrity = textIntegrity(reference: expectedBodyWords(doc), candidate: wordSequence(lo))163        #expect(integrity >= 0.98, "LibreOffice sees \(integrity * 100) % of the body words")164        #expect(lo.contains("Merged across two\t9.50") && lo.contains("1. Step one") && lo.contains("Table 1: Fruit prices"))165        // A document with the constructs LibreOffice is pickier about: watermark, page border, numbered headings, shapes, floating pictures, contents.166        var stress = richDocument()167        stress.pageSetup.watermark = "DRAFT"168        stress.pageSetup.border = PageBorder(line: BorderLine(width: 1, color: RGBA(hex: 0x4F46E5)), inset: 24)169        stress.pageSetup.background = RGBA(hex: 0xFFFDF5)170        for i in stress.styles.styles.indices where stress.styles.styles[i].id.hasPrefix("Heading") { stress.styles.styles[i].numbered = true }171        stress.body.insert(.toc(TOCBlock(kind: .contents, title: "Contents", maxLevel: 2)), at: 0)172        stress.body.append(.paragraph(Paragraph(style: StyleID.normal, runs: [173            Run("Objects "), Run(inline: .shape(ShapeObject(kind: .ellipse, width: 80, height: 40, fill: RGBA(hex: 0xFFEEDD), text: "Shape"))),174            Run(inline: .shape(ShapeObject(kind: .arrow, width: 80, height: 1, placement: .floating(wrap: .square, align: .right, xOffset: 0, yOffset: 10)))),175            Run(inline: .image(ImageObject(asset: "red", width: 60, height: 40, altText: "Floating", crop: Insets(top: 0.1, left: 0.1, bottom: 0.1, right: 0.1), rotation: 15,176                                           placement: .floating(wrap: .topAndBottom, align: .center, xOffset: 0, yOffset: 20)))),177        ])))178        stress.body.append(.table(Table(strings: [["last", "table"]], width: 200)))179        let stressData = try DocxExporter.export(stress).data180        let stressPDF = try #require(Tool.sofficeConvert(stressData, to: "pdf"), "LibreOffice could not convert the stress document to PDF")181        #expect(stressPDF.prefix(5) == Data("%PDF-".utf8) && stressPDF.count > 5_000)182        let stressBack = try DocxImporter.importDocument(try #require(Tool.sofficeConvert(stressData, to: "docx:MS Word 2007 XML"))).document183        #expect(stressBack.tables.count == 2)184        #expect(Numbering(stressBack).outline.map(\.text).contains("Background"))185        #expect(stressBack.pageSetup.border != nil)186        #expect(stressBack.inlines.filter { if case .image = $0 { return true } else { return false } }.count >= 2)187        // The re-exported corpus converts too.188        for name in ["pandoc-tables.docx", "libreoffice-pagesetup.docx", "pandoc-math.docx"] {189            let re = try DocxExporter.export(try DocxImporter.importDocument(try Corpus.data(name)).document).data190            let out = try #require(Tool.sofficeConvert(re, to: "pdf"), "LibreOffice could not convert the re-export of \(name)")191            #expect(out.prefix(5) == Data("%PDF-".utf8))192        }193    }194195    @Test func libreOfficeRoundTripKeepsStructure() throws {196        guard Tool.slowTestsEnabled else { recordSkip("ORIX_SLOW_TESTS is not 1: LibreOffice DOCX→DOCX check not run"); return }197        guard Tool.soffice != nil else { recordSkip("soffice missing: LibreOffice DOCX→DOCX check not run"); return }198        // Our file, opened and saved again by LibreOffice, then read by our importer: what an independent implementation understood.199        let (doc, data) = try exported()200        let resaved = try #require(Tool.sofficeConvert(data, to: "docx:MS Word 2007 XML"), "LibreOffice could not re-save the exported DOCX")201        let back = try DocxImporter.importDocument(resaved).document202        #expect(back.tables.count == 1)203        #expect(back.tables.first?.columnCount == 3 && back.tables.first?.rows.count == 4)204        #expect(back.tables.first?.rows[2].cells.first?.colSpan == 2)205        #expect(back.notes.count == 3)206        #expect(back.sectionSetups.count == 2 && back.sectionSetups[1].landscape && back.sectionSetups[1].columns == 2)207        #expect(back.pageSetup.header.default.map(\.visibleText) == ["Rich document header"])208        #expect(back.inlines.contains { if case .image = $0 { return true } else { return false } })209        #expect(back.inlines.contains { if case .equation = $0 { return true } else { return false } })210        #expect(back.comments.count == 1)211        #expect(back.bodyParagraphs.contains { $0.runs.contains { $0.revision?.kind == .deletion && $0.revision?.author == "Editor" } })212        let integrity = textIntegrity(reference: expectedBodyWords(doc), candidate: back.bodyLines.flatMap(wordSequence))213        #expect(integrity >= 0.99, "after LibreOffice: \(integrity * 100) % of the body words")214    }215}216
Files main
Menu

Theme