Repository Public License
1// ORIX Scan2// File: Packages/ORIXKit/Tests/ORIXKitTests/LibraryTests.swift3// Description: Tests of the SwiftData library index: search, sorting, filtering, folders, Recently Deleted, reconciliation with the files.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import SwiftData9import Testing10@testable import ORIXKit1112@MainActor13@Suite struct LibraryTests {14 private func make() throws -> LibraryIndex {15 LibraryIndex(container: try Library.makeContainer(inMemory: true), processor: Fixture.processor())16 }1718 @Test func refreshIndexesTitleTextAndSize() async throws {19 let index = try make()20 let manifest = try await Fixture.document([Fixture.page(Fixture.french)], title: "Reçu d'Épicerie", in: index.processor)21 var record = try await index.refresh(manifest.id)22 #expect(record.pageCount == 1 && record.byteSize > 0 && !record.hasText && record.kind == .scan)23 try await index.processor.recognize(manifest.id, options: OCROptions())24 record = try await index.refresh(manifest.id)25 #expect(record.hasText && record.snippet.hasPrefix("Rapport trimestriel"))26 let all = index.allRecords()27 #expect(Library.query(all, search: "recu epicerie").count == 1)28 #expect(Library.query(all, search: "QUEBECOISE montreal").count == 1)29 #expect(Library.query(all, search: "zebra").isEmpty)30 #expect(index.allRecords().count == 1)31 }3233 @Test func sortingAndFiltering() throws {34 let t0 = Date(timeIntervalSince1970: 1_000_000)35 let a = DocumentRecord(id: UUID(), title: "Scan 10", createdAt: t0, modifiedAt: t0.addingTimeInterval(30), isFavorite: true, pageCount: 1, byteSize: 300, kind: .scan, hasText: true)36 let b = DocumentRecord(id: UUID(), title: "Scan 2", createdAt: t0.addingTimeInterval(10), modifiedAt: t0.addingTimeInterval(20), pageCount: 9, byteSize: 100, kind: .imported)37 let c = DocumentRecord(id: UUID(), title: "alpha", createdAt: t0.addingTimeInterval(20), modifiedAt: t0.addingTimeInterval(10), deletedAt: t0, pageCount: 4, byteSize: 200)38 let all = [a, b, c]39 #expect(Library.query(all).map(\.title) == ["Scan 10", "Scan 2"])40 #expect(Library.query(all, sort: .title, ascending: true).map(\.title) == ["Scan 2", "Scan 10"]) // numeric-aware41 #expect(Library.query(all, sort: .size, ascending: true).map(\.title) == ["Scan 2", "Scan 10"])42 #expect(Library.query(all, sort: .pages).map(\.title) == ["Scan 2", "Scan 10"])43 #expect(Library.query(all, sort: .created, ascending: true).map(\.title) == ["Scan 10", "Scan 2"])44 #expect(Library.query(all, scope: .recentlyDeleted).map(\.title) == ["alpha"])45 #expect(Library.query(all, scope: .favorites).map(\.title) == ["Scan 10"])46 #expect(Library.query(all, filter: .imports).map(\.title) == ["Scan 2"])47 #expect(Library.query(all, filter: .withText).map(\.title) == ["Scan 10"])48 #expect(Library.query(all, filter: .withoutText).map(\.title) == ["Scan 2"])49 }5051 @Test func uniqueTitles() {52 #expect(Library.uniqueTitle("Scan", existing: ["Other"]) == "Scan")53 #expect(Library.uniqueTitle("Scan", existing: ["scan", "Scan 2"]) == "Scan 3")54 }5556 @Test func trashRestoreAndPermanentDelete() async throws {57 let index = try make()58 let manifest = try await Fixture.document([Fixture.page("x")], in: index.processor)59 try await index.refresh(manifest.id)60 index.moveToTrash([manifest.id])61 #expect(Library.query(index.allRecords()).isEmpty)62 #expect(Library.query(index.allRecords(), scope: .recentlyDeleted).count == 1)63 #expect(await index.processor.store.exists(manifest.id)) // files stay while in Recently Deleted64 index.restore([manifest.id])65 #expect(Library.query(index.allRecords()).count == 1)66 await index.deletePermanently([manifest.id])67 #expect(index.allRecords().isEmpty)68 #expect(!(await index.processor.store.exists(manifest.id)))69 }7071 @Test func reconcileRebuildsFromFilesAndPurgesExpiredTrash() async throws {72 let index = try make()73 let kept = try await Fixture.document([Fixture.page("kept")], title: "Kept", in: index.processor)74 let expired = try await Fixture.document([Fixture.page("old")], title: "Expired", in: index.processor)75 let recent = try await Fixture.document([Fixture.page("recent")], title: "Recent", in: index.processor)76 // Nothing indexed yet (as after losing the index): reconcile rebuilds it from the manifests.77 var outcome = await index.reconcile()78 #expect(outcome.added == 3 && index.allRecords().count == 3)79 let now = Date()80 index.moveToTrash([expired.id], now: now.addingTimeInterval(-31 * 86_400))81 index.moveToTrash([recent.id], now: now.addingTimeInterval(-29 * 86_400))82 // A record whose files vanished is dropped.83 try await index.processor.store.deletePermanently(kept.id)84 outcome = await index.reconcile(now: now)85 #expect(outcome.removed == 1 && outcome.purged == 1)86 #expect(index.allRecords().map(\.title) == ["Recent"])87 #expect(!(await index.processor.store.exists(expired.id)))88 #expect(await index.processor.store.exists(recent.id))89 }9091 @Test func foldersRenameDuplicateAndFavorites() async throws {92 let index = try make()93 let manifest = try await Fixture.document([Fixture.page("x"), Fixture.page("y")], title: "Facture", in: index.processor)94 try await index.refresh(manifest.id)95 let folder = try #require(index.folder(named: "Reçus"))96 #expect(index.folder(named: "recus")?.id == folder.id) // same folder, not a second one97 index.move([manifest.id], to: folder)98 #expect(Library.query(index.allRecords(), scope: .folder(folder.id)).count == 1)99 #expect(Library.query(index.allRecords(), scope: .unfiled).isEmpty)100 try await index.rename(manifest.id, to: " Facture Hydro ")101 #expect(index.record(manifest.id)?.title == "Facture Hydro")102 #expect(try await index.processor.store.manifest(manifest.id).title == "Facture Hydro")103 let copy = try await index.duplicate(manifest.id)104 #expect(copy.title == "Facture Hydro 2" && copy.pageCount == 2 && copy.folder?.id == folder.id && copy.id != manifest.id)105 index.setFavorite([manifest.id], true)106 #expect(Library.query(index.allRecords(), scope: .favorites).map(\.id) == [manifest.id])107 index.deleteFolder(folder)108 #expect(index.allRecords().count == 2 && index.allRecords().allSatisfy { $0.folder == nil })109 }110111 @Test func workflowsPersist() throws {112 let container = try Library.makeContainer(inMemory: true)113 var workflow = Workflow(name: "Mine", steps: [ActionStep(.recognizeText), ActionStep(.exportText)])114 container.mainContext.insert(try WorkflowRecord(workflow))115 try container.mainContext.save()116 let stored = try #require(try container.mainContext.fetch(FetchDescriptor<WorkflowRecord>()).first)117 #expect(stored.workflow == workflow)118 workflow.name = "Renamed"119 stored.workflow = workflow120 #expect(stored.name == "Renamed" && stored.workflow?.name == "Renamed")121 }122123 @Test func indexPersistsOnDisk() async throws {124 let url = Fixture.tempDirectory().appendingPathComponent("library.store")125 let processor = Fixture.processor()126 let manifest = try await Fixture.document([Fixture.page("persist")], title: "Persistant", in: processor)127 do {128 let index = LibraryIndex(container: try Library.makeContainer(url: url), processor: processor)129 try await index.refresh(manifest.id)130 index.setFavorite([manifest.id], true)131 }132 let reopened = LibraryIndex(container: try Library.makeContainer(url: url), processor: processor)133 let record = try #require(reopened.record(manifest.id))134 #expect(record.title == "Persistant" && record.isFavorite)135 }136}137