Kartenimport, Metadaten und Versand absichern

This commit is contained in:
Leon Tonhäuser committed 2026-11-05 09:23:44 +01:00
1 parent be6bd49da5
commit 427b6fa7b0
18 files changed
+643 -193

No files matched your search

@@ -0,0 +1,117 @@
import Foundation
import XCTest
@testable import LichtkastenCore
final class IngestRecoveryTests: XCTestCase {
private func fixture() throws -> (root: URL, source: URL, primary: URL, backup: URL) {
let fm = FileManager.default
let root = fm.temporaryDirectory.appendingPathComponent(UUID().uuidString, isDirectory: true)
let source = root.appendingPathComponent("card", isDirectory: true)
let primary = root.appendingPathComponent("primary", isDirectory: true)
let backup = root.appendingPathComponent("backup", isDirectory: true)
for dir in [source, primary, backup] { try fm.createDirectory(at: dir, withIntermediateDirectories: true) }
addTeardownBlock { try fm.removeItem(at: root) }
return (root, source, primary, backup)
}
func testEqualSizeDoesNotIdentifyDuplicate() throws {
let dirs = try fixture()
let src = dirs.source.appendingPathComponent("DSC_0001.NEF")
try Data([1, 2]).write(to: src)
try Data([2, 1]).write(to: dirs.primary.appendingPathComponent(src.lastPathComponent))
let result = IngestService.uniqueName("DSC_0001", files: [src], in: dirs.primary, skipDuplicates: true)
XCTAssertEqual(result.name, "DSC_0001-1")
XCTAssertFalse(result.skip)
}
func testMissingRecursiveSourceThrows() throws {
let dirs = try fixture()
XCTAssertThrowsError(try FolderScanner.scan(folder: dirs.root.appendingPathComponent("missing"), recursive: true))
}
func testRetryFillsBackupWhenPrimaryAlreadyExists() async throws {
let dirs = try fixture()
let src = try makePhoto(in: dirs.source)
try FileManager.default.copyItem(at: src, to: dirs.primary.appendingPathComponent(src.lastPathComponent))
try await ingest(source: dirs.source, primary: dirs.primary, backup: dirs.backup)
XCTAssertEqual(try Data(contentsOf: dirs.backup.appendingPathComponent(src.lastPathComponent)), try Data(contentsOf: src))
}
func testBackupNameCollisionAllocatesSameNewNameInBothDestinations() async throws {
let dirs = try fixture()
let src = try makePhoto(in: dirs.source)
let unrelated = Data("unrelated backup".utf8)
let existing = dirs.backup.appendingPathComponent(src.lastPathComponent)
try unrelated.write(to: existing)
try await ingest(source: dirs.source, primary: dirs.primary, backup: dirs.backup)
XCTAssertEqual(try Data(contentsOf: existing), unrelated)
for dir in [dirs.primary, dirs.backup] {
XCTAssertEqual(try Data(contentsOf: dir.appendingPathComponent("photo-1.png")), try Data(contentsOf: src))
}
}
func testIncrementalRepairsMissingBackupAndPreservesEditedPrimary() async throws {
let dirs = try fixture()
let src = try makePhoto(in: dirs.source)
let history = dirs.root.appendingPathComponent("history.json")
let primary = dirs.primary.appendingPathComponent("photo-4.png")
let backup = dirs.backup.appendingPathComponent("photo-4.png")
_ = try await ingest(source: dirs.source, primary: dirs.primary, backup: dirs.backup, history: history, sequence: 4)
let edited = Data("edited primary".utf8)
try edited.write(to: primary)
let complete = try await ingest(source: dirs.source, primary: dirs.primary, backup: dirs.backup, history: history, sequence: 5)
XCTAssertEqual(complete.skipped, 1)
try FileManager.default.removeItem(at: backup)
_ = try await ingest(source: dirs.source, primary: dirs.primary, backup: dirs.backup, history: history, sequence: 5)
XCTAssertEqual(try Data(contentsOf: primary), edited)
XCTAssertEqual(try Data(contentsOf: backup), try Data(contentsOf: src))
XCTAssertFalse(FileManager.default.fileExists(atPath: dirs.primary.appendingPathComponent("photo-5.png").path))
}
func testIncrementalDoesNotAcceptReplacedBackup() async throws {
let dirs = try fixture()
let src = try makePhoto(in: dirs.source)
let history = dirs.root.appendingPathComponent("history.json")
_ = try await ingest(source: dirs.source, primary: dirs.primary, backup: dirs.backup, history: history, sequence: 4)
let previousBackup = dirs.backup.appendingPathComponent("photo-4.png")
let unrelated = Data(repeating: 1, count: try Data(contentsOf: src).count)
try unrelated.write(to: previousBackup)
_ = try await ingest(source: dirs.source, primary: dirs.primary, backup: dirs.backup, history: history, sequence: 5)
XCTAssertEqual(try Data(contentsOf: previousBackup), unrelated)
for dir in [dirs.primary, dirs.backup] {
XCTAssertEqual(try Data(contentsOf: dir.appendingPathComponent("photo-4-1.png")), try Data(contentsOf: src))
}
}
private func makePhoto(in source: URL) throws -> URL {
let png = try XCTUnwrap(Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jP1sAAAAASUVORK5CYII="))
let photo = source.appendingPathComponent("photo.png")
try png.write(to: photo)
return photo
}
@discardableResult
private func ingest(source: URL, primary: URL, backup: URL, history: URL? = nil, sequence: Int? = nil) async throws -> IngestResult {
guard let exe = ExifTool.locate() else { throw XCTSkip("ExifTool fehlt") }
var settings = IngestSettings()
settings.primaryDestination = primary.path
settings.backupDestination = backup.path
settings.secondaryEnabled = true
settings.incremental = history != nil
settings.folderPattern = ""
if let sequence {
settings.startSequence = sequence
settings.renamePattern = "photo-{seqn}"
}
return try await IngestService.run(sources: [source], settings: settings,
exiftool: ExifTool(executable: exe), historyFile: history) { _ in }
}
}