Files
lichtkasten/Tests/LichtkastenCoreTests/IngestRecoveryTests.swift
T
Lichtkasten DevandClaude Opus 5.5 c9d43aba9c Review-Korrekturen zu „Kartenimport, Metadaten und Versand absichern“
- Ingest-Verlauf wieder wie PM: Name + Größe + Datum, unabhängig von Ziel und Ordnermuster.
  Beim Sichten gelöschte Fotos kommen nicht zurück, neuer Spieltags-Ordner importiert keine alte Karte
  erneut, keine SHA-256-Prüfsumme mehr über die ganze Karte. Alter Verlauf (ingest-history.json) gilt wieder.
  Schutz vor Namenskollisionen, Rollback und Backup-Prüfung bleiben.
- FTP: „Vorhandene Datei auf dem Server ersetzen“ (wie PM, Standard an) – Korrekturen lassen sich erneut
  senden, kein Auflisten des Server-Ordners vor jeder Datei. Aus = Upload stoppt bei belegtem Namen.
- LR-Export: Pfadabgleich unabhängig von Unicode-Schreibweise (Umlaute), Groß/Klein und Symlinks
- Ordner-Scan: einzelne unlesbare oder verschwundene Dateien werden übersprungen statt alles abzubrechen
- Agentur-Check: Mindestgröße nur für die gelieferten Bilder, RAWs blockieren nicht; Senden nutzt die
  aktuellen Bilddaten
- Datum ohne Uhrzeit lässt IPTC TimeCreated stehen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
2026-11-05 08:46:36 +00:00

108 lines
5.3 KiB
Swift

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 testIncrementalSkipsPhotosDeletedWhileCulling() async throws {
let dirs = try fixture()
_ = 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 copy = dirs.primary.appendingPathComponent("photo-4.png")
try FileManager.default.removeItem(at: copy) // reject deleted in INPUT
let again = try await ingest(source: dirs.source, primary: dirs.primary, backup: dirs.backup, history: history, sequence: 5)
XCTAssertEqual(again.skipped, 1)
XCTAssertTrue(again.copied.isEmpty)
XCTAssertFalse(FileManager.default.fileExists(atPath: copy.path))
XCTAssertFalse(FileManager.default.fileExists(atPath: dirs.primary.appendingPathComponent("photo-5.png").path))
}
func testIncrementalIgnoresChangedFolderPattern() async throws {
let dirs = try fixture()
_ = 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)
let next = try await ingest(source: dirs.source, primary: dirs.primary, backup: dirs.backup, history: history,
folderPattern: "Spieltag 2")
XCTAssertEqual(next.skipped, 1)
XCTAssertTrue(next.copied.isEmpty)
}
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,
folderPattern: String = "") 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 = 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 }
}
}