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 } } } final class IngestSpaceTests: XCTestCase { func testRefusesWhenDestinationIsTooSmall() throws { let dir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: dir) } let free = try XCTUnwrap(IngestService.freeBytes(dir)) // a sparse "card file" larger than the free space of the destination let big = dir.appendingPathComponent("big.NEF") FileManager.default.createFile(atPath: big.path, contents: nil) let h = try FileHandle(forWritingTo: big) try h.truncate(atOffset: UInt64(free) + 1) try h.close() XCTAssertThrowsError(try IngestService.checkFreeSpace(entriesFiles: [[big]], destRoots: [dir.appendingPathComponent("INPUT")])) XCTAssertNoThrow(try IngestService.checkFreeSpace(entriesFiles: [], destRoots: [dir])) } }