Zum Inhalt

apple/PhysarumApp/Tests/CaptureCleanupTests.swift

import XCTest
@testable import Physarum

final class CaptureCleanupTests: XCTestCase {
    private func tmpDir() throws -> URL {
        let d = FileManager.default.temporaryDirectory.appendingPathComponent("cleanup-test-" + UUID().uuidString)
        try FileManager.default.createDirectory(at: d, withIntermediateDirectories: true)
        return d
    }

    func testFiltersOnlyOldPhysarumCaptures() throws {
        let dir = try tmpDir(); defer { try? FileManager.default.removeItem(at: dir) }
        let fm = FileManager.default
        let old = dir.appendingPathComponent("physarum-aurora-20260101-000000.png")
        let recent = dir.appendingPathComponent("physarum-aurora-20260613-000000.mov")
        let foreign = dir.appendingPathComponent("notes.txt")
        for u in [old, recent, foreign] { fm.createFile(atPath: u.path, contents: Data()) }
        let now = Date()
        try fm.setAttributes([.modificationDate: now.addingTimeInterval(-7200)], ofItemAtPath: old.path)
        try fm.setAttributes([.modificationDate: now.addingTimeInterval(-60)], ofItemAtPath: recent.path)
        let stale = CaptureCleanup.staleCaptureFiles(in: dir, now: now, olderThan: 3600)
        XCTAssertEqual(stale.map(\.lastPathComponent), ["physarum-aurora-20260101-000000.png"])
    }

    func testSweepRemovesStaleKeepsRecent() throws {
        let dir = try tmpDir(); defer { try? FileManager.default.removeItem(at: dir) }
        let fm = FileManager.default
        let old = dir.appendingPathComponent("physarum-x-20260101-000000.png")
        let recent = dir.appendingPathComponent("physarum-x-20260613-000000.png")
        for u in [old, recent] { fm.createFile(atPath: u.path, contents: Data()) }
        let now = Date()
        try fm.setAttributes([.modificationDate: now.addingTimeInterval(-7200)], ofItemAtPath: old.path)
        try fm.setAttributes([.modificationDate: now.addingTimeInterval(-60)], ofItemAtPath: recent.path)
        CaptureCleanup.sweep(now: now, olderThan: 3600, dir: dir)
        XCTAssertFalse(fm.fileExists(atPath: old.path))
        XCTAssertTrue(fm.fileExists(atPath: recent.path))
    }
}