Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

20.6 KB
Raw Blame
import Foundation
import Observation
@testable import Peel
@testable import PeelCore
import Synchronization
import Testing

@MainActor
struct RemovalHistoryStoreTests {
    private func moved(_ name: String) throws -> TrashResult {
        let json = #"{"originalURL": "file:///Users/me/\#(name)", "trashedURL": "file:///Users/me/.Trash/\#(name)", "date": 0}"#
        return TrashResult(trashed: [try JSONDecoder().decode(TrashedItem.self, from: Data(json.utf8))])
    }

    @Test func readsWhatPeelChangedBeforeTheRemovalsPageLooksInTheTrashAgain() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let url = folder.appending(path: "removals.json")
        let history = RemovalHistoryStore(
            log: RemovalLog(url: url),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true }
        )
        let part = RemovalPart(source: "Example", sourceKey: nil, tool: Tool.developer.rawValue)
        var removal = RemovalInProgress()
        await history.record(try moved("One"), part: part, sizes: [:], in: &removal)
        await history.record(try moved("Two"), part: part, sizes: [:], in: &removal)
        history.finish(removal)
        await history.load()
        let records = try #require(history.batches.first?.records)
        _ = await RemovalLog(url: url).remove([records[0].id])

        let seen = Mutex<Int?>(nil)
        withObservationTracking({ _ = history.trashLooks }, onChange: {
            MainActor.assumeIsolated { seen.withLock { $0 = history.batches.first?.records.count } }
        })
        await history.readAgainIfInUse()

        #expect(seen.withLock { $0 } == 1, "the page looked in the Trash before History was read again")
        _ = await RemovalLog(url: url).remove([records[1].id])
        await history.readAgainIfInUse()
        #expect(history.batches.isEmpty)
        #expect(history.undoTitle == nil, "Undo still offered what peel put back")
    }

    @Test func aJournalThatCannotBeReadHoldsNothingBackOnceChecked() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        try FileManager.default.createDirectory(
            at: folder.appending(path: "removals.journal"), withIntermediateDirectories: true
        )
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true }
        )

        await history.checkReadability()

        #expect(history.writeProblem == nil)
        #expect(history.mayMove)
        guard case .damaged = history.problem else {
            Issue.record("nothing said: \(String(describing: history.problem))")
            return
        }
    }

    @Test func saysOnceWhatHistoryNoLongerLists() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
        let prefs = try MadeUpDefaults()
        defer { prefs.forget() }
        func store() -> RemovalHistoryStore {
            RemovalHistoryStore(
                log: RemovalLog(url: folder.appending(path: "removals.json")),
                refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
                knowsTheExclusions: { true },
                defaults: prefs.defaults
            )
        }
        func leaveOut(_ count: Int) throws {
            let totals = #"{"apps":0,"biggest":{"isComplete":true,"known":0},"bytes":{"isComplete":true,"known":0},"items":0,"removalsLeftOut":\#(count),"tookInEarlier":false}"#
            try Data(totals.utf8).write(to: folder.appending(path: "totals.json"))
        }

        let history = store()
        await history.load()
        #expect(history.untoldNoLongerListed == nil, "said before anything was left out")
        try leaveOut(3)
        await history.load()
        #expect(history.untoldNoLongerListed == 3)
        history.tellNoLongerListed(3)
        #expect(history.untoldNoLongerListed == nil)

        let later = store()
        await later.load()
        #expect(later.untoldNoLongerListed == nil, "said again after it was said")
        try leaveOut(5)
        await later.load()
        #expect(later.untoldNoLongerListed == 5)
    }

    @Test func movesNothingWhilePeelRemovesItself() {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        final class Removal { var runs = false }
        let removal = Removal()
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true },
            removesPeel: { removal.runs }
        )

        #expect(history.mayMove)
        removal.runs = true
        #expect(history.holdingReason == .peelRemovesItself)
        #expect(!history.mayMove)
    }

    @Test func aHistoryThatCannotBeReadIsNotCalledEmpty() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let url = folder.appending(path: "removals.json")
        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
        let history = RemovalHistoryStore(
            log: RemovalLog(url: url),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true }
        )

        await history.load()

        #expect(history.hasLoaded && history.batches.isEmpty)
        #expect(history.shownProblem == .unreadable(.notAFile))
        #expect(!history.isKnownEmpty)
    }

    @Test func tellsVoiceOverWhatForgetForgot() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let heard = Heard()
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true },
            announcing: { heard.words.append(String($0.characters)) }
        )
        let part = RemovalPart(source: "Example", sourceKey: nil, tool: Tool.developer.rawValue)
        var removal = RemovalInProgress()
        await history.record(try moved("One"), part: part, sizes: [:], in: &removal)
        await history.record(try moved("Two"), part: part, sizes: [:], in: &removal)
        history.finish(removal)
        await history.load()

        await history.forget(history.records)

        #expect(heard.words.last == "Forgot 2 items.")
        #expect(history.batches.isEmpty)
    }

    @Test func answersWhatEachStartOverDid() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let removals = folder.appending(path: "removals.json")
        let refusals = folder.appending(path: "refusals.json")
        try FileManager.default.createDirectory(at: removals, withIntermediateDirectories: true)
        try FileManager.default.createDirectory(at: refusals, withIntermediateDirectories: true)
        let history = RemovalHistoryStore(
            log: RemovalLog(url: removals),
            refusals: RefusalLog(url: refusals),
            knowsTheExclusions: { true }
        )
        await history.load()

        #expect(await history.startOver() == .started)
        #expect(await history.startOverRefusals() == .started)
        #expect(await history.startOver() == .notNeeded)
        #expect(await history.startOverRefusals() == .notNeeded)
    }

    @Test func looksInTheTrashAgainAfterAPutBackHistoryCouldNotWrite() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        let path = folder.path(percentEncoded: false)
        defer {
            try? FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: path)
            try? FileManager.default.removeItem(at: folder)
        }
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true },
            restoring: { items, _ in
                // Back in place, and History's folder closed before History is told.
                try? FileManager.default.setAttributes([.posixPermissions: 0o500], ofItemAtPath: path)
                return TrashService.Restored(failures: items.map { _ in nil }, movedAside: items.map { _ in nil })
            }
        )
        var removal = RemovalInProgress()
        let part = RemovalPart(source: "Example", sourceKey: nil, tool: Tool.developer.rawValue)
        await history.record(try moved("Cache"), part: part, sizes: [:], in: &removal)
        history.finish(removal)
        let looks = history.trashLooks

        await history.restore(history.records, canUseHelper: false)

        let refusedWrite = SystemError(domain: NSCocoaErrorDomain, code: NSFileWriteNoPermissionError)
        #expect(history.problem == .couldNotUpdate(.failed(refusedWrite)))
        #expect(history.trashLooks == looks + 1, "the page kept saying it is in the Trash")
    }

    @Test func aPutBackWritesDownTheEditItMovedAsideUnderTheSamePart() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let edit = try JSONDecoder().decode(TrashedItem.Identity.self, from: Data(#"{"inode": 5, "birth": 6}"#.utf8))
        let aside = TrashedItem(
            originalURL: URL(filePath: "/Users/me/Library/LaunchAgents/org.example.edit.plist"),
            trashedURL: URL(filePath: "/Users/me/.Trash/org.example.edit 2.plist"), date: .now, replacedBy: edit
        )
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true },
            restoring: { items, _ in
                TrashService.Restored(failures: items.map { _ in nil }, movedAside: items.map { _ in aside })
            }
        )
        var removal = RemovalInProgress()
        let part = RemovalPart(source: "org.example.edit", sourceKey: nil, tool: Tool.backgroundItems.rawValue)
        await history.record(try moved("org.example.edit.plist"), part: part, sizes: [:], in: &removal)
        history.finish(removal)
        let undoable = history.undoTitle

        await history.restore(history.records, canUseHelper: false)

        #expect(history.records.map(\.originalURL) == [aside.originalURL])
        #expect(history.records.first?.part == part)
        #expect(history.records.first?.replacedBy == edit)
        #expect(history.undoTitle == nil, "nothing new to undo: the move aside is part of the Put Back")
        #expect(undoable != nil)
    }

    @Test func tellsAPutBackThatEndsAsTheOneBefore() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let outcome = RemovalOutcome()
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            outcome: outcome,
            knowsTheExclusions: { true },
            restoring: { items, _ in
                TrashService.Restored(failures: items.map { _ in .needsHelper }, movedAside: items.map { _ in nil })
            }
        )
        var removal = RemovalInProgress()
        let part = RemovalPart(source: "Example", sourceKey: nil, tool: Tool.developer.rawValue)
        await history.record(try moved("Driver"), part: part, sizes: [:], in: &removal)
        history.finish(removal)

        await history.restore(history.records, canUseHelper: false)
        let first = history.failures
        #expect(outcome.putBackFailures.count == 1)
        outcome.clear()
        await history.restore(history.records, canUseHelper: false)

        #expect(history.failures == first)
        #expect(outcome.putBackFailures.count == 1, "the second Put Back's alert never showed")
    }

    @Test func aPutBackKeepsItsReasonsWhateverPageIsOnScreenByThen() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let outcome = RemovalOutcome()
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            outcome: outcome,
            knowsTheExclusions: { true },
            restoring: { items, _ in
                TrashService.Restored(failures: items.map { _ in .needsHelper }, movedAside: items.map { _ in nil })
            }
        )
        var removal = RemovalInProgress()
        let part = RemovalPart(source: "Example", sourceKey: nil, tool: Tool.developer.rawValue)
        await history.record(try moved("Driver"), part: part, sizes: [:], in: &removal)
        history.finish(removal)
        let records = history.records

        await history.restore(records, canUseHelper: false)

        #expect(outcome.putBackFailures.map(\.failure) == [.needsHelper])
        #expect(records.allSatisfy { history.failures[$0.id] == .needsHelper })
    }

    @Test func saysOnceHowManyRemovalsNotMovedNoLongerLists() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let made = try MadeUpDefaults()
        defer { made.forget() }
        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true }, defaults: made.defaults
        )
        await history.load()
        #expect(history.untoldRefusalsNoLongerListed == nil)

        let totals = #"{"apps":0,"biggest":{"isComplete":true,"known":0},"bytes":{"isComplete":true,"known":0},"items":0,"refusalsLeftOut":1,"tookInEarlier":false}"#
        try Data(totals.utf8).write(to: folder.appending(path: "totals.json"))
        await history.load()
        #expect(history.untoldRefusalsNoLongerListed == 1)

        history.tellRefusalsNoLongerListed(1)
        #expect(history.untoldRefusalsNoLongerListed == nil)
    }

    @Test func aMoveTellsTheJournalWhatItIsCalled() async {
        let history = RemovalHistoryStore(knowsTheExclusions: { true })

        let recorded = await history.moving(tool: .plugins, source: "Reverb", sourceKey: "tool") {
            RecordedRemoval.current
        }

        #expect(recorded?.part == RemovalPart(source: "Reverb", sourceKey: "tool", tool: Tool.plugins.rawValue))
    }

    @Test func saysWhenStartingOverCouldNotSetTheOldFileAside() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        let path = folder.path(percentEncoded: false)
        try FileManager.default.createDirectory(at: folder.appending(path: "removals.json"), withIntermediateDirectories: true)
        try FileManager.default.createDirectory(at: folder.appending(path: "refusals.json"), withIntermediateDirectories: true)
        try FileManager.default.setAttributes([.posixPermissions: 0o500], ofItemAtPath: path)
        defer {
            try? FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: path)
            try? FileManager.default.removeItem(at: folder)
        }
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true }
        )
        await history.load()

        let startedOver = await history.startOver()
        let startedRefusalsOver = await history.startOverRefusals()

        let refusedMove = SystemError(domain: NSCocoaErrorDomain, code: NSFileWriteNoPermissionError)
        #expect(startedOver == .couldNotSetAside(.failed(refusedMove)))
        #expect(history.shownProblem == .unreadable(.notAFile))
        #expect(startedRefusalsOver == .couldNotSetAside(.failed(refusedMove)))
        #expect(history.refusalProblem == .unreadable(.notAFile))
    }

    @Test func startsOverANotMovedListItCannotRead() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let refusals = folder.appending(path: "refusals.json")
        try FileManager.default.createDirectory(at: refusals, withIntermediateDirectories: true)
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: refusals),
            knowsTheExclusions: { true }
        )
        await history.load()
        #expect(history.refusalProblem == .unreadable(.notAFile))

        await history.startOverRefusals()

        #expect(history.refusalProblem == nil)
        #expect(FileManager.default.fileExists(atPath: refusals.path(percentEncoded: false)) == false)
    }

    @Test func aRefusalThatCouldNotBeRecordedIsStillSaidWhenHistoryOpens() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        let path = folder.path(percentEncoded: false)
        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
        try FileManager.default.setAttributes([.posixPermissions: 0o500], ofItemAtPath: path)
        defer {
            try? FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: path)
            try? FileManager.default.removeItem(at: folder)
        }
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
            knowsTheExclusions: { true }
        )
        let refused = TrashResult(failures: [TrashFailure(url: URL(filePath: "/Users/me/kept"), reason: .lastCopy)])
        var removal = RemovalInProgress()
        await history.record(
            refused, part: RemovalPart(source: "Example", sourceKey: nil, tool: Tool.developer.rawValue), sizes: [:],
            in: &removal
        )

        await history.load()

        let refusedWrite = SystemError(domain: NSCocoaErrorDomain, code: NSFileWriteNoPermissionError)
        #expect(history.refusalProblem == .couldNotRecord(.failed(refusedWrite)))
    }

    @Test func aReadThatFindsNothingNewChangesNothing() async throws {
        let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
        defer { try? FileManager.default.removeItem(at: folder) }
        let refusals = RefusalLog(url: folder.appending(path: "refusals.json"))
        await refusals.add(
            [TrashFailure(url: URL(filePath: "/Users/me/kept"), reason: .lastCopy)], source: "Example", tool: "duplicates"
        )
        let history = RemovalHistoryStore(
            log: RemovalLog(url: folder.appending(path: "removals.json")),
            refusals: refusals,
            knowsTheExclusions: { true }
        )
        await history.load()
        let revision = history.revision

        await history.load()

        #expect(history.revision == revision)
        #expect(history.refusalBatches.count == 1)
    }
}

private final class Heard {
    var words: [String] = []
}