Files
Lichtkasten DevandClaude Opus 5.5 244734c59f Ingest: freien Speicher auf externen Platten zuverlässig messen
Ein zweiter Ingest scheiterte nach dem Freiräumen mit denselben Zahlen: volumeAvailableCapacityForImportantUsage
liefert für externe Platten (exFAT/HFS+) keinen verlässlichen, aktuellen Wert. Jetzt:
- externe Platten: freier Platz per statfs, bei jedem Ingest frisch gelesen
- interne Platte: größerer Wert aus statfs und „important usage“ (löschbarer APFS-Speicher zählt)
- Reserve 2 GB nur auf der Mac-Platte, 256 MB auf externen Fotoplatten
- Haupt- und Backup-Ziel nur bei gleicher Volume-ID zusammengerechnet
- nicht angeschlossene Ziel-Platte (/Volumes/…) wird gemeldet statt auf die Mac-Platte zu messen
- Hinweis in der Meldung: gelöschte Dateien liegen bis zum Leeren im Papierkorb der Platte

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
2026-11-07 17:49:13 +00:00

140 lines
7.1 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 }
}
}
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]))
}
}
final class IngestDestinationTests: XCTestCase {
func testMissingExternalDiskIsReported() {
XCTAssertThrowsError(try IngestService.checkDestinationMounted(URL(fileURLWithPath: "/Volumes/Nicht-da-\(UUID().uuidString)/BILDER")))
XCTAssertNoThrow(try IngestService.checkDestinationMounted(FileManager.default.temporaryDirectory))
}
func testFreeSpaceIsReadFreshEachTime() 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 before = try XCTUnwrap(IngestService.freeBytes(dir.appendingPathComponent("noch/nicht/da")))
XCTAssertGreaterThan(before, 0)
}
}