SPB Git

Repository Public

DocumentAuditorTests.swift 32.4 KB 501 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocAuditTests/DocumentAuditorTests.swift3// Description: One trigger and at least one clean near-miss for every audit check; language, determinism, report and performance.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import DocModel10import DocAudit1112@Suite struct HeadingCheckTests {13    @Test func missingHeadings() {14        let long = makeDocument((0..<40).map { para(prose(45, seed: $0)) })15        let f = findings(long, .missingHeadings)16        #expect(f.count == 1 && f[0].severity == .warning)17        #expect(f[0].evidence.contains("1800 words") && f[0].evidence.contains("0 headings"))18        #expect(f[0].location?.path == ParaPath(block: 0))19        // Near-misses: the same text with one heading; a short text without headings.20        var withHeading = long21        withHeading.body.insert(heading(1, "Introduction"), at: 0)22        #expect(findings(withHeading, .missingHeadings).isEmpty)23        #expect(findings(makeDocument((0..<10).map { para(prose(45, seed: $0)) }), .missingHeadings).isEmpty)24    }2526    @Test func headingHierarchy() {27        let skip = makeDocument([heading(1, "Chapter"), para("Text."), heading(3, "Deep section"), para("Text.")])28        let f = findings(skip, .headingHierarchy)29        #expect(f.count == 1)30        #expect(f[0].evidence.contains("Deep section") && f[0].evidence.contains("level 3") && f[0].evidence.contains("level 2 is skipped"))31        #expect(f[0].location?.path == ParaPath(block: 2) && f[0].location?.paragraphID == skip.body[2].id)32        let late = makeDocument([heading(2, "Preface"), para("Text."), heading(1, "Chapter"), heading(2, "Section")])33        #expect(findings(late, .headingHierarchy).count == 1)34        // Near-misses: going back up several levels, and a document that only uses level 2.35        let fine = makeDocument([heading(1, "A"), heading(2, "B"), heading(3, "C"), para("Text."), heading(1, "D"), heading(2, "E")])36        #expect(findings(fine, .headingHierarchy).isEmpty)37        #expect(findings(makeDocument([heading(2, "Only"), para("Text."), heading(2, "Level two"), heading(3, "Then three")]), .headingHierarchy).isEmpty)38    }3940    @Test func emptyHeading() {41        let doc = makeDocument([heading(1, "Title"), para("Text."), heading(2, "   "), para("More.")])42        let f = findings(doc, .emptyHeading)43        #expect(f.count == 1 && f[0].location?.path == ParaPath(block: 2) && f[0].evidence.contains("Heading2"))44        // Near-miss: an empty body paragraph.45        #expect(findings(makeDocument([heading(1, "Title"), para(""), para("Text.")]), .emptyHeading).isEmpty)46    }4748    @Test func duplicateHeading() {49        let doc = makeDocument([heading(1, "Results"), heading(2, "Summary"), para("a"), heading(2, "Details"), heading(2, "summary "), para("b")])50        let f = findings(doc, .duplicateHeading)51        #expect(f.count == 1 && f[0].location?.path == ParaPath(block: 4))52        #expect(f[0].evidence.contains("block 2") && f[0].evidence.contains("block 5"))53        // Near-miss: the same title under two different parents.54        let fine = makeDocument([heading(1, "Chapter one"), heading(2, "Summary"), para("a"), heading(1, "Chapter two"), heading(2, "Summary"), para("b")])55        #expect(findings(fine, .duplicateHeading).isEmpty)56    }57}5859@Suite struct ReferenceCheckTests {60    @Test func brokenCrossReference() {61        let target = Paragraph("Methods", style: StyleID.heading(1))62        var good = Paragraph("See ")63        good.runs.append(Run(inline: .crossRef(CrossRef(target: target.id))))64        var bad = Paragraph("See also ")65        bad.runs.append(Run(inline: .crossRef(CrossRef(target: "gone"))))66        let doc = makeDocument([.paragraph(target), .paragraph(good), .paragraph(bad)])67        let f = findings(doc, .brokenCrossReference)68        #expect(f.count == 1 && f[0].severity == .error)69        #expect(f[0].evidence.contains("gone") && f[0].location?.path == ParaPath(block: 2))70        #expect(findings(makeDocument([.paragraph(target), .paragraph(good)]), .brokenCrossReference).isEmpty)71    }7273    @Test func brokenInternalLink() {74        var anchor = Paragraph("Anchor here")75        anchor.runs.append(Run(inline: .bookmark("intro")))76        var p = Paragraph()77        p.runs = [Run("go to intro", props: CharProps(link: "#intro")), Run(" or "), Run("nowhere", props: CharProps(link: "#missing")), Run(" or "),78                  Run("the web", props: CharProps(link: "https://example.org"))]79        let f = findings(makeDocument([.paragraph(anchor), .paragraph(p)]), .brokenCrossReference)80        #expect(f.count == 1 && f[0].evidence.contains("missing") && f[0].evidence.contains("nowhere"))81    }8283    @Test func missingFigureCaption() {84        let doc = makeDocument([para("Intro."), .paragraph(imageParagraph()), para("Text right after the image.")])85        let f = findings(doc, .missingFigureCaption)86        #expect(f.count == 1 && f[0].location?.path == ParaPath(block: 1) && f[0].evidence.contains("img1"))87        // Near-misses: a caption below, a caption above, two images sharing one caption, an image inside a sentence.88        #expect(findings(makeDocument([para("Intro."), .paragraph(imageParagraph()), .paragraph(captionParagraph("Figure", "A chart")), para("Text.")]), .missingFigureCaption).isEmpty)89        #expect(findings(makeDocument([.paragraph(captionParagraph("Figure", "A chart")), .paragraph(imageParagraph()), para("Text.")]), .missingFigureCaption).isEmpty)90        #expect(findings(makeDocument([.paragraph(imageParagraph()), .paragraph(imageParagraph()), .paragraph(captionParagraph("Figure", "Two panels"))]), .missingFigureCaption).isEmpty)91        var inline = Paragraph("Press the ")92        inline.runs.append(Run(inline: .image(ImageObject(asset: "icon", width: 12, height: 12, altText: "gear"))))93        inline.runs.append(Run(" button."))94        #expect(findings(makeDocument([.paragraph(inline)]), .missingFigureCaption).isEmpty)95        // A table caption next to an image is not a figure caption; a Caption-style paragraph without number field is mentioned.96        #expect(findings(makeDocument([.paragraph(imageParagraph()), .paragraph(captionParagraph("Table", "Wrong kind"))]), .missingFigureCaption).count == 1)97        let typed = findings(makeDocument([.paragraph(imageParagraph()), para("Figure 1: typed by hand", style: StyleID.caption)]), .missingFigureCaption)98        #expect(typed.count == 1 && typed[0].evidence.contains("no figure number field"))99    }100101    @Test func missingTableCaption() {102        let table = Table(strings: [["A", "B"], ["1", "2"], ["3", "4"]], width: 300)103        let doc = makeDocument([para("Intro."), .table(table), para("After.")])104        let f = findings(doc, .missingTableCaption)105        #expect(f.count == 1 && f[0].severity == .info && f[0].location?.path == ParaPath(block: 1) && f[0].evidence.contains("3 rows × 2 columns"))106        #expect(findings(doc, .missingTableCaption, options: .academic)[0].severity == .warning)107        // Near-misses: a caption above; a one-row layout table.108        #expect(findings(makeDocument([.paragraph(captionParagraph("Table", "Numbers")), .table(table), para("After.")]), .missingTableCaption).isEmpty)109        #expect(findings(makeDocument([.table(Table(strings: [["left", "right"]], width: 300)), para("After.")]), .missingTableCaption).isEmpty)110        // Another captioned table in the document makes the omission a warning.111        let mixed = makeDocument([.paragraph(captionParagraph("Table", "Numbers")), .table(table), para("Between."), .table(Table(strings: [["C", "D"], ["5", "6"]], width: 300)), para("End.")])112        let m = findings(mixed, .missingTableCaption)113        #expect(m.count == 1 && m[0].severity == .warning && m[0].location?.path == ParaPath(block: 3))114    }115116    @Test func missingAltText() {117        let doc = makeDocument([.paragraph(imageParagraph(alt: "  ")), .paragraph(captionParagraph("Figure", "x")), .paragraph(imageParagraph(alt: "Described")), .paragraph(captionParagraph("Figure", "y"))])118        let f = findings(doc, .missingAltText)119        #expect(f.count == 1 && f[0].location?.path == ParaPath(block: 0) && f[0].severity == .warning)120    }121122    @Test func bibliographyChecks() {123        var p = Paragraph("As shown by ")124        p.runs.append(Run(inline: .citation(Citation("smith2020", "ghost1999"))))125        p.runs.append(Run(" and again "))126        p.runs.append(Run(inline: .citation(Citation("ghost1999"))))127        var doc = makeDocument([.paragraph(p), .bibliography(BibliographyBlock())])128        doc.bibliography = [BibEntry(key: "smith2020", authors: [PersonName(family: "Smith")], year: 2020, fields: ["title": "On things"]),129                            BibEntry(key: "unused2001", fields: ["title": "Never cited"])]130        let missing = findings(doc, .missingBibliographyEntry)131        #expect(missing.count == 1 && missing[0].severity == .error && missing[0].id == "missingBibliographyEntry/ghost1999")132        #expect(missing[0].evidence.contains("ghost1999") && missing[0].evidence.contains("2 time"))133        let orphans = findings(doc, .orphanedCitation)134        #expect(orphans.count == 1 && orphans[0].evidence.contains("unused2001") && orphans[0].evidence.contains("Never cited") && orphans[0].location?.path == ParaPath(block: 1))135136        // incompleteReference only speaks through the injected closure, and only about cited entries.137        #expect(findings(doc, .incompleteReference).isEmpty)138        let incomplete = findings(doc, .incompleteReference) { $0.fields["volume"] == nil ? ["volume", "page"] : [] }139        #expect(incomplete.count == 1 && incomplete[0].id == "incompleteReference/smith2020" && incomplete[0].evidence.contains("volume, page"))140        #expect(findings(doc, .incompleteReference) { _ in [] }.isEmpty)141142        // Near-misses: every key known and cited; uncited entries without a bibliography block.143        var clean = doc144        clean.bibliography = [doc.bibliography[0], BibEntry(key: "ghost1999")]145        #expect(findings(clean, .missingBibliographyEntry).isEmpty && findings(clean, .orphanedCitation).isEmpty)146        var noBlock = doc147        noBlock.body.removeLast()148        #expect(findings(noBlock, .orphanedCitation).isEmpty)149    }150151    @Test func unreferencedFigureAndTable() {152        let fig = captionParagraph("Figure", "Yield curve")153        let tab = captionParagraph("Table", "Summary statistics")154        let table = Table(strings: [["A", "B"], ["1", "2"]], width: 300)155        let doc = makeDocument([para("Some text."), .paragraph(imageParagraph()), .paragraph(fig), .paragraph(tab), .table(table), para("Closing text.")])156        let f = findings(doc, .unreferencedFigure), t = findings(doc, .unreferencedTable)157        #expect(f.count == 1 && f[0].location?.paragraphID == fig.id && f[0].evidence.contains("Figure 1: Yield curve"))158        #expect(t.count == 1 && t[0].location?.paragraphID == tab.id)159        // Near-misses: a cross-reference to the figure; the table mentioned in the text by label and number.160        var ref = Paragraph("As ")161        ref.runs.append(Run(inline: .crossRef(CrossRef(target: fig.id))))162        ref.runs.append(Run(" shows, and Table 1 confirms."))163        var cited = doc164        cited.body[0] = .paragraph(ref)165        #expect(findings(cited, .unreferencedFigure).isEmpty && findings(cited, .unreferencedTable).isEmpty)166        // "Table 2" does not reference Table 1.167        var other = doc168        other.body[0] = para("See Table 2 and fig. 1 for details.")169        #expect(findings(other, .unreferencedTable).count == 1 && findings(other, .unreferencedFigure).isEmpty)170    }171}172173@Suite struct FormattingAndReviewCheckTests {174    @Test func inconsistentFormatting() {175        var blocks = (0..<12).map { para(prose(20, seed: $0)) }176        var odd = Paragraph(prose(20, seed: 99))177        odd.runs[0].props.fontSize = 16178        odd.runs[0].props.fontFamily = "Comic Sans MS"179        blocks.insert(.paragraph(odd), at: 5)180        var centred = Paragraph(prose(20, seed: 98))181        centred.props.alignment = .center182        blocks.insert(.paragraph(centred), at: 9)183        let f = findings(makeDocument(blocks), .inconsistentFormatting)184        #expect(f.count == 2)185        #expect(f[0].location?.paragraphID == odd.id && f[0].evidence.contains("16 pt") && f[0].evidence.contains("Comic Sans MS") && f[0].evidence.contains("13 of 14"))186        #expect(f[1].location?.paragraphID == centred.id && f[1].evidence.contains("alignment center"))187    }188189    @Test func ordinaryStylisticVariationIsNotFlagged() {190        // Emphasis inside paragraphs, a larger word, headings of another style: all ordinary.191        var blocks: [Block] = [heading(1, "Chapter")]192        for i in 0..<12 {193            var p = Paragraph(prose(25, seed: i))194            if i % 3 == 0 { p.transform(0..<6) { $0.props.bold = true } }195            if i % 4 == 0 { p.transform(8..<20) { $0.props.italic = true; $0.props.color = RGBA(hex: 0xAA0000) } }196            if i == 5 { p.transform(0..<5) { $0.props.fontSize = 18 } }197            if i == 6 { p.transform(0..<p.length) { $0.props.bold = true } }198            blocks.append(.paragraph(p))199        }200        #expect(findings(makeDocument(blocks), .inconsistentFormatting).isEmpty)201        // No clear majority (half and half): nothing is "the" formatting of the style.202        let split = (0..<12).map { i -> Block in203            var p = Paragraph(prose(20, seed: i))204            if i % 2 == 0 { p.props.alignment = .justified }205            return .paragraph(p)206        }207        #expect(findings(makeDocument(split), .inconsistentFormatting).isEmpty)208        // Too few paragraphs to speak of a norm.209        var few = (0..<4).map { para(prose(20, seed: $0)) }210        var odd = Paragraph(prose(20, seed: 9))211        odd.runs[0].props.fontSize = 16212        few.append(.paragraph(odd))213        #expect(findings(makeDocument(few), .inconsistentFormatting).isEmpty)214    }215216    @Test func unresolvedComment() {217        var p = Paragraph("A sentence with ")218        p.runs.append(Run("a doubtful claim", comments: ["c1", "c2"]))219        p.runs.append(Run("."))220        var doc = makeDocument([para("First."), .paragraph(p)])221        doc.comments = [Comment(id: "c1", author: "Anna", text: "Source?", replies: [CommentReply(author: "Ben", text: "Looking.")]),222                        Comment(id: "c2", author: "Ben", text: "Fixed", resolved: true)]223        let f = findings(doc, .unresolvedComment)224        #expect(f.count == 1 && f[0].id == "unresolvedComment/c1" && f[0].location?.path == ParaPath(block: 1))225        #expect(f[0].evidence.contains("Anna") && f[0].evidence.contains("Source?") && f[0].evidence.contains("a doubtful claim") && f[0].evidence.contains("1 reply"))226        doc.comments[0].resolved = true227        #expect(findings(doc, .unresolvedComment).isEmpty)228    }229230    @Test func pendingTrackedChange() {231        var p = Paragraph("Stable text")232        p.runs.append(Run(" added", revision: Revision(kind: .insertion, author: "Anna")))233        p.runs.append(Run(" removed", revision: Revision(kind: .deletion, author: "Ben")))234        let doc = makeDocument([para("First."), .paragraph(p)])235        let f = findings(doc, .pendingTrackedChange)236        #expect(f.count == 1 && f[0].severity == .warning && f[0].location?.path == ParaPath(block: 1))237        #expect(f[0].evidence.contains("1 insertion") && f[0].evidence.contains("1 deletion") && f[0].evidence.contains("Anna, Ben"))238        #expect(findings(makeDocument([para("First."), para("Stable text added")]), .pendingTrackedChange).isEmpty)239    }240241    @Test func missingMetadata() {242        let empty = DocumentModel(body: [para("Text.")])243        let f = findings(empty, .missingMetadata)244        #expect(f.count == 1 && f[0].evidence == "Empty properties: title, author." && f[0].location?.path == nil)245        #expect(findings(makeDocument([para("Text.")]), .missingMetadata).isEmpty)246        // The academic profile also expects keywords.247        #expect(findings(makeDocument([para("Text.")]), .missingMetadata, options: .academic).first?.evidence.contains("keywords") == true)248    }249}250251@Suite struct PageCheckTests {252    @Test func unusualPageBreak() {253        let blank = makeDocument([para("Page one."), .pageBreak(BreakBlock()), para(""), .pageBreak(BreakBlock()), para("Page three.")])254        let f = findings(blank, .unusualPageBreak)255        #expect(f.count == 1 && f[0].severity == .warning && f[0].location?.path == ParaPath(block: 1) && f[0].evidence.contains("blank page"))256        let afterHeading = makeDocument([para("Text."), heading(1, "Lonely heading"), .pageBreak(BreakBlock()), para("Its text.")])257        let h = findings(afterHeading, .unusualPageBreak)258        #expect(h.count == 1 && h[0].evidence.contains("Lonely heading"))259        #expect(findings(makeDocument([para("Text."), .pageBreak(BreakBlock())]), .unusualPageBreak).first?.evidence.contains("ends on an empty page") == true)260        var pbb = Paragraph("Starts a page")261        pbb.props.pageBreakBefore = true262        #expect(findings(makeDocument([para("Text."), .pageBreak(BreakBlock()), .paragraph(pbb)]), .unusualPageBreak).count == 1)263        // Near-misses: an ordinary break between two texts; a break before a heading; a column break.264        #expect(findings(makeDocument([para("End of chapter."), .pageBreak(BreakBlock()), heading(1, "Next chapter"), para("Text.")]), .unusualPageBreak).isEmpty)265        #expect(findings(makeDocument([heading(1, "Head"), .pageBreak(BreakBlock(kind: .column)), .pageBreak(BreakBlock(kind: .column)), para("Text.")]), .unusualPageBreak).isEmpty)266    }267268    @Test func nearlyEmptyPagesFromLayout() {269        let doc = makeDocument([para("One."), para("Two."), para("Three.")])270        let layout = LayoutFacts(pageCount: 3, pageOfBlock: [doc.body[0].id: 1, doc.body[1].id: 2, doc.body[2].id: 3], nearlyEmptyPages: [2])271        let f = findings(doc, .unusualPageBreak, layout: layout)272        #expect(f.count == 1 && f[0].location?.page == 2 && f[0].location?.path == ParaPath(block: 1) && f[0].evidence == "Page 2 of 3 is almost empty.")273        #expect(findings(doc, .unusualPageBreak, layout: LayoutFacts(pageCount: 3)).isEmpty)274    }275276    @Test func overflowingTable() {277        var wide = Table(strings: [["A", "B", "C"], ["1", "2", "3"]], width: 600)278        wide.id = "wide"279        var fits = Table(strings: [["A", "B", "C"], ["1", "2", "3"]], width: 468)280        fits.id = "fits"281        let doc = makeDocument([para("Text."), .table(wide), para("Between."), .table(fits)])282        let f = findings(doc, .overflowingTable)283        #expect(f.count == 1 && f[0].id == "overflowingTable/wide" && f[0].evidence.contains("600 pt") && f[0].evidence.contains("468 pt") && f[0].evidence.contains("132 pt"))284        // The layout can report a table the model finds acceptable (long unbreakable content); it is not reported twice.285        let both = findings(doc, .overflowingTable, layout: LayoutFacts(pageCount: 1, overflowingTableIDs: ["fits", "wide", "unknown"]))286        #expect(both.map(\.id) == ["overflowingTable/wide", "overflowingTable/fits"])287        // Near-miss: the wide table in a landscape section.288        let landscape = makeDocument([para("Text."), .sectionBreak(SectionBreak(setup: PageSetup(paper: .letter, landscape: true))), .table(wide)])289        #expect(findings(landscape, .overflowingTable).isEmpty)290    }291292    @Test func missingPageNumbers() {293        let doc = makeDocument([para("Text.")])294        let f = findings(doc, .missingPageNumbers, layout: LayoutFacts(pageCount: 6, pagesWithoutPageNumber: [2, 3, 4]))295        #expect(f.count == 1 && f[0].evidence == "3 of 6 pages show no page number: 2, 3, 4." && f[0].location?.page == 2)296        // Near-misses: a single page; only the cover page without number; every page numbered.297        #expect(findings(doc, .missingPageNumbers, layout: LayoutFacts(pageCount: 1, pagesWithoutPageNumber: [1])).isEmpty)298        #expect(findings(doc, .missingPageNumbers, layout: LayoutFacts(pageCount: 6, pagesWithoutPageNumber: [1])).isEmpty)299        #expect(findings(doc, .missingPageNumbers, layout: LayoutFacts(pageCount: 6)).isEmpty)300        // Without layout facts: only a long document with no page number field anywhere.301        #expect(findings(doc, .missingPageNumbers).isEmpty)302        var long = makeDocument([heading(1, "Report")] + (0..<30).map { para(prose(40, seed: $0)) })303        #expect(findings(long, .missingPageNumbers).count == 1)304        var footer = Paragraph(style: StyleID.footer)305        footer.runs = [Run("Page "), Run(inline: .field(.pageNumber))]306        long.pageSetup.footer.default = [footer]307        #expect(findings(long, .missingPageNumbers).isEmpty)308    }309}310311@Suite struct LanguageCheckTests {312    @Test func undefinedAcronym() {313        let doc = makeDocument([para("The GDP rose by two percent while the CPI stayed flat."), para("The consumer price index (CPI) is published monthly; GDP is quarterly.")])314        let f = findings(doc, .undefinedAcronym)315        #expect(f.map(\.id) == ["undefinedAcronym/GDP"])316        #expect(f[0].evidence.contains("2 time") && f[0].location?.path == ParaPath(block: 0))317        var o = AuditOptions()318        o.knownAcronyms = ["gdp"]319        #expect(findings(doc, .undefinedAcronym, options: o).isEmpty)320    }321322    @Test func longSentence() {323        let long = "This sentence " + (0..<50).map { _ in "keeps going" }.joined(separator: " and ") + " until the reader gives up."324        let doc = makeDocument([para("A short sentence. Another short one."), para("Fine. " + long + " Short again.")])325        let f = findings(doc, .longSentence)326        #expect(f.count == 1 && f[0].location?.path == ParaPath(block: 1))327        #expect(f[0].evidence.contains("words (threshold: 40)") && f[0].evidence.contains("This sentence keeps going"))328        // Near-miss: 38 words in one sentence; many short sentences in one long paragraph.329        #expect(findings(makeDocument([para((0..<38).map { _ in "word" }.joined(separator: " ") + ".")]), .longSentence).isEmpty)330        #expect(findings(makeDocument([para(prose(200))]), .longSentence).isEmpty)331    }332333    @Test func doubleSpaces() {334        let doc = makeDocument([para("One space only. Next sentence."), para("Two  spaces here.  And   there."), para("x  =  1", style: StyleID.code), para("Trailing spaces  ")])335        let f = findings(doc, .doubleSpaces)336        #expect(f.count == 1 && f[0].location?.path == ParaPath(block: 1) && f[0].evidence.contains("3 run(s)") && f[0].evidence.contains("Two··spaces"))337    }338339    @Test func repeatedWording() {340        let dense = "The volatility of returns matters. Higher volatility raises premia. When volatility falls, spreads tighten. Realised volatility differs from implied volatility. " +341                    "Traders watch volatility closely because volatility clusters."342        let doc = makeDocument([para(prose(60, seed: 1)), para(dense), para(prose(60, seed: 2))])343        let f = findings(doc, .repeatedWording)344        #expect(f.contains { $0.id == "repeatedWording/word#volatility" && $0.evidence.contains("7 times") && $0.location?.path == ParaPath(block: 1) })345        // Near-miss: the word used three times in a long passage.346        let sparse = makeDocument([para("The volatility of returns matters. " + prose(40, seed: 3)), para("Lower volatility helps. " + prose(40, seed: 4)), para("Implied volatility differs. " + prose(40, seed: 5))])347        #expect(!findings(sparse, .repeatedWording).contains { $0.id.contains("volatility") })348        // A repeated phrase.349        let phrase = makeDocument([para("At the end of the trading day the desk closes its positions. Prices settle. At the end of the trading day the risk report is sent.")])350        let p = findings(phrase, .repeatedWording)351        #expect(p.count == 1 && p[0].evidence.contains("at the end of the trading day") && p[0].evidence.contains("2 times"))352    }353}354355@Suite struct AuditBehaviourTests {356    private func messy() -> DocumentModel {357        var cite = Paragraph("The GDP figures come from ")358        cite.runs.append(Run(inline: .citation(Citation("ghost"))))359        cite.runs.append(Run("  and are  revised."))360        var xref = Paragraph("See ")361        xref.runs.append(Run(inline: .crossRef(CrossRef(target: "nothing"))))362        var doc = DocumentModel(body: [heading(1, "Report"), heading(3, "Skipped"), .paragraph(cite), .paragraph(xref), .paragraph(imageParagraph(alt: "")),363                                       heading(2, ""), .table(Table(strings: [["a", "b"], ["c", "d"]], width: 900)), .pageBreak(BreakBlock())])364        doc.comments = [Comment(id: "c", author: "A", text: "Check")]365        return doc366    }367368    @Test func findingsAreDeterministicAndOrdered() {369        let doc = messy()370        let a = DocumentAuditor.audit(doc), b = DocumentAuditor.audit(doc)371        #expect(a == b)372        #expect(Set(a.map(\.id)).count == a.count, "ids must be unique")373        // Document-level findings first, then by position in the body.374        let blocks = a.map { $0.location?.path?.block ?? -1 }375        #expect(blocks == blocks.sorted())376        let checks = Set(a.map(\.check))377        for expected in [AuditCheck.headingHierarchy, .missingBibliographyEntry, .brokenCrossReference, .missingAltText, .missingFigureCaption, .emptyHeading,378                         .overflowingTable, .unusualPageBreak, .unresolvedComment, .undefinedAcronym, .doubleSpaces, .missingMetadata, .missingTableCaption] {379            #expect(checks.contains(expected), "\(expected) expected")380        }381        // Every finding is complete.382        for f in a {383            #expect(!f.title.isEmpty && !f.explanation.isEmpty && !f.evidence.isEmpty && !f.suggestion.isEmpty && f.location != nil, "incomplete finding \(f.id)")384        }385    }386387    @Test func aCleanDocumentHasNoFinding() {388        let fig = captionParagraph("Figure", "The slope factor during the day")389        var ref = Paragraph("The gross domestic product (GDP) is shown in ")390        ref.runs.append(Run(inline: .crossRef(CrossRef(target: fig.id))))391        ref.runs.append(Run(". GDP growth was steady."))392        var doc = makeDocument([heading(1, "Introduction"), .paragraph(ref), .paragraph(imageParagraph()), .paragraph(fig), heading(2, "Background"),393                                para("Earlier studies measured the term structure once a day, usually at the close. That choice hides what happens between the opening bell and the afternoon auction."),394                                heading(1, "Results"),395                                para("The slope falls in the first hour, recovers before noon and drifts lower into the close. Announcement days look different: the level jumps within a minute and stays there."),396                                .pageBreak(BreakBlock()), heading(1, "Conclusion"),397                                para("Intraday estimates add information that daily data cannot provide. Further work should extend the sample to other currencies.")])398        var footer = Paragraph(style: StyleID.footer)399        footer.runs = [Run(inline: .field(.pageNumber))]400        doc.pageSetup.footer.default = [footer]401        let layout = LayoutFacts(pageCount: 2, pageOfBlock: Dictionary(uniqueKeysWithValues: doc.body.enumerated().map { ($1.id, $0 < 9 ? 1 : 2) }))402        let f = DocumentAuditor.audit(doc, layout: layout)403        #expect(f.isEmpty, "unexpected: \(f.map { "\($0.id): \($0.evidence)" })")404    }405406    @Test func enabledChecksAndProfiles() {407        let doc = messy()408        var only = AuditOptions()409        only.enabled = [.emptyHeading]410        #expect(Set(DocumentAuditor.audit(doc, options: only).map(\.check)) == [.emptyHeading])411        only.enabled = []412        #expect(DocumentAuditor.audit(doc, options: only).isEmpty)413        let minimal = Set(DocumentAuditor.audit(doc, options: .minimal).map(\.check))414        #expect(minimal.isSubset(of: AuditOptions.minimal.enabled) && minimal.contains(.brokenCrossReference) && !minimal.contains(.doubleSpaces))415        #expect(!Set(DocumentAuditor.audit(doc, options: .business).map(\.check)).contains(.missingBibliographyEntry))416        #expect(AuditOptions.academic.enabled == Set(AuditCheck.allCases))417    }418419    @Test func frenchTexts() {420        var doc = messy()421        doc.meta.language = "fr-CA"422        let fr = DocumentAuditor.audit(doc)423        let empty = fr.first { $0.check == .emptyHeading }!424        #expect(empty.title == "Titre vide" && empty.suggestion.contains("Saisissez") && empty.evidence.contains("Paragraphe vide de style Heading2"))425        #expect(fr.first { $0.check == .brokenCrossReference }?.title == "Renvoi brisé")426        // options.language wins over the document language.427        var o = AuditOptions()428        o.language = "en"429        #expect(DocumentAuditor.audit(doc, options: o).first { $0.check == .emptyHeading }?.title == "Empty heading")430        o.language = "fr"431        var english = doc432        english.meta.language = "en"433        #expect(DocumentAuditor.audit(english, options: o).first { $0.check == .emptyHeading }?.title == "Titre vide")434        // Every check has both languages.435        var all = AuditOptions()436        for lang in ["en", "fr"] {437            all.language = lang438            #expect(DocumentAuditor.audit(doc, options: all).allSatisfy { !$0.title.isEmpty })439        }440    }441442    @Test func findingsPerCheckAreCapped() {443        let doc = makeDocument((0..<30).map { para("Double  space number \($0).") })444        var o = AuditOptions()445        o.maxFindingsPerCheck = 5446        let f = findings(doc, .doubleSpaces, options: o)447        #expect(f.count == 6)448        #expect(f.filter { $0.id == "doubleSpaces/+more" }.first?.evidence.hasPrefix("25 more findings") == true)449    }450451    @Test func pagesComeFromLayoutFacts() {452        let doc = makeDocument([para("Fine."), heading(2, "")])453        let f = findings(doc, .emptyHeading, layout: LayoutFacts(pageCount: 4, pageOfBlock: [doc.body[1].id: 4]))454        #expect(f[0].location?.page == 4)455        #expect(findings(doc, .emptyHeading)[0].location?.page == nil)456    }457458    @Test func reportDocument() {459        let doc = messy()460        let all = DocumentAuditor.audit(doc)461        let report = DocumentAuditor.reportDocument(all, for: doc)462        let ps = report.body.compactMap(\.paragraph)463        #expect(ps[0].style == StyleID.title && ps[0].plainText == "Quality report")464        let h1 = ps.filter { $0.style == StyleID.heading(1) }.map(\.plainText)465        #expect(h1.first == "Summary" && h1.contains { $0.hasPrefix("Errors (") } && h1.contains { $0.hasPrefix("Warnings (") })466        let table = report.body.compactMap(\.table).first!467        #expect(table.rows.last!.cells[0].plainText == "Total")468        #expect(table.rows.last!.cells[1].plainText == String(all.filter { $0.severity == .error }.count))469        #expect(ps.filter { $0.plainText.hasPrefix("Evidence  ") }.count == all.count)470        #expect(ps.contains { $0.plainText.contains("Broken cross-reference") })471        // French document → French report; empty list → explicit sentence.472        var fr = doc473        fr.meta.language = "fr"474        let frReport = DocumentAuditor.reportDocument(DocumentAuditor.audit(fr), for: fr)475        #expect(frReport.body.compactMap(\.paragraph)[0].plainText == "Rapport de qualité")476        let none = DocumentAuditor.reportDocument([], for: doc)477        #expect(none.body.compactMap(\.paragraph).contains { $0.plainText.hasPrefix("No finding") })478        let data = try! JSONEncoder().encode(report)479        #expect((try? JSONDecoder().decode(DocumentModel.self, from: data))?.body == report.body)480    }481482    @Test func threeThousandParagraphAudit() {483        var blocks: [Block] = []484        for i in 0..<3000 {485            if i % 60 == 0 { blocks.append(heading(1, "Chapter \(i / 60 + 1)")) }486            else if i % 20 == 0 { blocks.append(heading(2, "Section \(i)")) }487            else if i % 97 == 0 { blocks.append(.table(Table(strings: [["Name", "Value"], ["alpha", "\(i)"], ["beta", "\(i + 1)"]], width: 400))) }488            else if i % 41 == 0 { blocks.append(para("The EBITDA margin and  the ROI of unit \(i) improved. " + prose(40, seed: i))) }489            else { blocks.append(para(prose(30 + i % 50, seed: i))) }490        }491        let doc = makeDocument(blocks)492        let clock = ContinuousClock()493        var result: [AuditFinding] = []494        let elapsed = clock.measure { result = DocumentAuditor.audit(doc, options: .academic) { _ in [] } }495        let seconds = Double(elapsed.components.seconds) + Double(elapsed.components.attoseconds) / 1e18496        print(String(format: "[DocAudit benchmark] 3 000 paragraphs, every check: %d findings in %.3f s", result.count, seconds))497        #expect(seconds < 10, "audit of 3 000 paragraphs took \(seconds) s")498        #expect(result.contains { $0.check == .undefinedAcronym } && result.contains { $0.check == .doubleSpaces } && result.contains { $0.check == .missingTableCaption })499    }500}501
Files main
Menu

Theme