Repository Public
1// SP Drive iOS2// File: KitTests/ScanPipelineTests.swift3// Description: The scan pipeline on generated pages: page count and size, on-device text recognition, and the invisible text layer of the4// PDF (searchable, positioned over the printed line). No camera is involved: the pipeline takes images.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import PDFKit10import Testing11@testable import SPDriveKit1213@Suite struct ScanPipelineTests {14 func temp(_ name: String) -> URL { FileManager.default.temporaryDirectory.appendingPathComponent("scan-\(UUID().uuidString.prefix(6))-\(name)") }1516 @Test func recognitionReadsTheFixture() throws {17 let lines = try ScanPipeline.recognize(ScanFixtures.invoice()[0])18 let text = lines.map(\.text).joined(separator: "\n")19 #expect(text.contains("Facture 2026-1119"))20 #expect(text.localizedCaseInsensitiveContains("Atelier du Nord"))21 #expect(text.contains("234,56"))22 // top to bottom, boxes inside the page23 #expect(lines.first?.text.contains("Facture") == true)24 #expect(lines.allSatisfy { CGRect(x: 0, y: 0, width: 1, height: 1).contains($0.box) })25 }2627 @Test func searchablePDFHasATextLayerWhereTheTextIsPrinted() throws {28 let url = temp("ocr.pdf")29 let seen = Shared<[Int]>([])30 let result = try ScanPipeline.makePDF(pages: ScanFixtures.invoice(), to: url, options: .init(ocr: true)) { page, _ in seen.withLock { $0.append(page) } }31 #expect(result.pageCount == 2 && result.pagesWithText == 2 && result.recognizedCharacters > 60)32 #expect(seen.withLock { $0 } == [1, 2])33 let pdf = try #require(PDFDocument(url: url))34 #expect(pdf.pageCount == 2)35 let text = try #require(pdf.string)36 #expect(text.contains("Facture 2026-1119") && text.contains("Conditions de paiement"))37 // the page keeps the proportions of the scan (1240 × 1754 px at 200 dpi → 446 × 631 pt)38 let box = try #require(pdf.page(at: 0)).bounds(for: .mediaBox)39 #expect(abs(box.width - 446.4) < 1 && abs(box.height - 631.4) < 1)40 // searching finds the word, and the selection sits where the fixture printed it (first line, near the top left)41 let hits = pdf.findString("Facture", withOptions: [.caseInsensitive])42 let hit = try #require(hits.first)43 let where_ = hit.bounds(for: try #require(hit.pages.first))44 #expect(where_.minX > 20 && where_.minX < 80, "x = \(where_.minX)")45 #expect(where_.midY > box.height * 0.85, "y = \(where_.midY) of \(box.height)")46 #expect(pdf.findString("trente jours", withOptions: []).first?.pages.first == pdf.page(at: 1))47 }4849 @Test func withoutRecognitionThePDFIsImagesOnlyAndStaysSmall() throws {50 let url = temp("plain.pdf")51 let result = try ScanPipeline.makePDF(pages: ScanFixtures.invoice(), to: url, options: .init(ocr: false))52 #expect(result.pagesWithText == 0 && result.recognizedCharacters == 0)53 let pdf = try #require(PDFDocument(url: url))54 #expect(pdf.pageCount == 2)55 #expect((pdf.string ?? "").trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)56 #expect(result.bytes < 600_000, "two JPEG pages, not raw bitmaps: \(result.bytes) bytes")57 // a large page is brought down to the configured resolution58 let big = ScanFixtures.page(lines: ["Grand format"], size: CGSize(width: 4000, height: 5600), fontSize: 120)59 #expect(try ScanPipeline.compressed(big, maxPixel: 2400, quality: 0.6).height == 2400)60 #expect(throws: ScanPipeline.Failure.self) { try ScanPipeline.makePDF(pages: [], to: temp("empty.pdf")) }61 #expect(ScanPipeline.defaultName(date: Date(timeIntervalSince1970: 0)).hasSuffix(".pdf"))62 }63}64