Repository Public
1// SP Lecture Studio — Tests2// File: Tests/Unit/PDFTests.swift3// Description: Unit tests of the PDF layer: geometry, rasterization, search, vector ink rendering and annotated PDF export.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import XCTest8import PDFKit9@testable import LectureCore10@testable import LecturePDF11import LectureStorage12import LectureTesting1314final class PDFTests: XCTestCase {15 var dir: URL!16 override func setUp() { dir = Fixtures.tempDirectory() }17 override func tearDown() { try? FileManager.default.removeItem(at: dir) }1819 func testLoadGeometrySearchAndRaster() throws {20 let url = Fixtures.makePDF(at: dir.appendingPathComponent("a.pdf"), pages: 20, size: CGSize(width: 960, height: 540), title: "Méthode du coût")21 let doc = try SlideDocument(url: url)22 XCTAssertEqual(doc.pageCount, 20)23 XCTAssertEqual(doc.pageSize(3), CGSize(width: 960, height: 540))24 XCTAssertEqual(doc.search("numero 17").map(\.page), [16]) // diacritic-insensitive25 XCTAssertEqual(doc.search("methode").count, 20)26 XCTAssertEqual(doc.titleGuess(page: 4), "Page 5 — Méthode du coût")27 let img = try XCTUnwrap(doc.image(page: 0, pixelSize: CGSize(width: 480, height: 270)))28 let probe = try XCTUnwrap(PixelProbe(img))29 XCTAssertTrue(probe.isReddish(x: 0.5, y: 0.02)) // page 1 band is red, at the top30 XCTAssertTrue(probe.isNearWhite(x: 0.9, y: 0.9))31 let thumb = try XCTUnwrap(doc.thumbnail(page: 2, maxDimension: 160))32 XCTAssertEqual(thumb.width, 160); XCTAssertEqual(thumb.height, 90)33 XCTAssertThrowsError(try SlideDocument(url: dir.appendingPathComponent("missing.pdf")))34 }3536 func testStrokeRendererProgressiveAndTools() throws {37 let ctx = try XCTUnwrap(Bitmap.context(width: 400, height: 200))38 ctx.setFillColor(CGColor(gray: 1, alpha: 1)); ctx.fill(CGRect(x: 0, y: 0, width: 400, height: 200))39 let mapping = PageMapping(rect: CGRect(x: 0, y: 0, width: 400, height: 200), flipped: false)40 let line = Stroke(page: 0, tool: .pen, color: .red, width: 0.02, begin: .zero,41 points: (0...100).map { StrokePoint(x: 0.1 + 0.8 * Double($0) / 100, y: 0.25, dt: Int64($0) * 10_000_000) })42 StrokeRenderer.draw(line, count: 51, in: ctx, mapping: mapping) // first half only43 var probe = try XCTUnwrap(PixelProbe(ctx.makeImage()!))44 XCTAssertTrue(probe.isReddish(x: 0.3, y: 0.25)); XCTAssertTrue(probe.isNearWhite(x: 0.7, y: 0.25))45 StrokeRenderer.draw(line, in: ctx, mapping: mapping)46 let rect = Stroke(page: 0, tool: .rectangle, color: .red, width: 0.02, begin: .zero, points: [StrokePoint(x: 0.2, y: 0.5), StrokePoint(x: 0.6, y: 0.9, dt: 1)])47 StrokeRenderer.draw(rect, in: ctx, mapping: mapping)48 probe = try XCTUnwrap(PixelProbe(ctx.makeImage()!))49 XCTAssertTrue(probe.isReddish(x: 0.7, y: 0.25))50 XCTAssertTrue(probe.isReddish(x: 0.2, y: 0.7)); XCTAssertTrue(probe.isReddish(x: 0.4, y: 0.5)); XCTAssertTrue(probe.isNearWhite(x: 0.4, y: 0.7))51 // Eraser hit testing.52 XCTAssertTrue(StrokeRenderer.hit(line, x: 0.5, y: 0.26, radius: 0.01, aspect: 2)); XCTAssertFalse(StrokeRenderer.hit(line, x: 0.5, y: 0.4, radius: 0.01, aspect: 2))53 XCTAssertTrue(StrokeRenderer.hit(rect, x: 0.2, y: 0.7, radius: 0.01, aspect: 2)); XCTAssertFalse(StrokeRenderer.hit(rect, x: 0.4, y: 0.7, radius: 0.01, aspect: 2))54 // Mapping inverse.55 let flipped = PageMapping(rect: CGRect(x: 10, y: 20, width: 300, height: 100), flipped: true)56 let n = flipped.normalized(flipped.point(0.3, 0.8)); XCTAssertEqual(n.x, 0.3, accuracy: 1e-9); XCTAssertEqual(n.y, 0.8, accuracy: 1e-9)57 }5859 func testAnnotatedPDFMatchesFinalStateAndLeavesSourceUntouched() throws {60 let url = Fixtures.makePDF(at: dir.appendingPathComponent("src.pdf"), pages: 3)61 let before = try Hashing.sha256(of: url)62 let doc = try SlideDocument(url: url)63 let kept = Stroke(page: 1, tool: .pen, color: .red, width: 0.015, begin: T(5), points: [StrokePoint(x: 0.2, y: 0.6), StrokePoint(x: 0.8, y: 0.6, dt: 1_000_000_000)])64 let erased = Stroke(page: 1, tool: .pen, color: .red, width: 0.015, begin: T(8), points: [StrokePoint(x: 0.2, y: 0.8), StrokePoint(x: 0.8, y: 0.8, dt: 1_000_000_000)])65 let index = TimelineIndex(events: [TimelineEvent(seq: 1, t: T(6), e: .strokeAdded(kept)), TimelineEvent(seq: 2, t: T(9), e: .strokeAdded(erased)),66 TimelineEvent(seq: 3, t: T(12), e: .strokesRemoved(ids: [erased.id]))])67 for mode in AnnotatedPDFMode.allCases {68 let out = dir.appendingPathComponent("annotated-\(mode.rawValue).pdf")69 try AnnotatedPDFExporter.export(document: doc, strokes: { index.finalStrokes(page: $0) }, to: out, mode: mode, title: "Test")70 let result = try SlideDocument(url: out)71 XCTAssertEqual(result.pageCount, 3); XCTAssertEqual(result.pageSize(1), doc.pageSize(1))72 XCTAssertEqual(result.search("numéro 2").map(\.page), [1], "text layer kept (\(mode))")73 let img: CGImage74 if mode == .annotations {75 // PDFKit draws annotations; CGPDF alone does not. Render through PDFKit for this mode.76 let page = try XCTUnwrap(result.pdf.page(at: 1))77 XCTAssertEqual(page.annotations.filter { $0.type == "Ink" }.count, 1)78 let ctx = try XCTUnwrap(Bitmap.context(width: 480, height: 270))79 ctx.setFillColor(CGColor(gray: 1, alpha: 1)); ctx.fill(CGRect(x: 0, y: 0, width: 480, height: 270)); ctx.scaleBy(x: 0.5, y: 0.5)80 page.draw(with: .cropBox, to: ctx)81 img = ctx.makeImage()!82 } else {83 img = try XCTUnwrap(result.image(page: 1, pixelSize: CGSize(width: 480, height: 270)))84 }85 let probe = try XCTUnwrap(PixelProbe(img))86 XCTAssertTrue(probe.isReddish(x: 0.5, y: 0.6), "kept stroke present (\(mode))")87 XCTAssertFalse(probe.isReddish(x: 0.5, y: 0.8), "erased stroke absent (\(mode))")88 let other = try XCTUnwrap(PixelProbe(try XCTUnwrap(result.image(page: 0, pixelSize: CGSize(width: 480, height: 270)))))89 XCTAssertFalse(other.isReddish(x: 0.5, y: 0.6))90 }91 XCTAssertEqual(try Hashing.sha256(of: url), before)92 }93}94