Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
import Foundation
import Testing
@testable import PeelCore
@Suite("What Peel makes of orphaned files")
struct OrphanConfidenceTests {
private let now = Date(timeIntervalSince1970: 1_800_000_000)
private func group(
identifier: String = "com.example.app",
kind: SearchLocation.Kind = .applicationSupport,
written: TimeInterval? = nil,
size: Int64 = 1_000,
remembered: RememberedApp? = nil
) -> OrphanGroup {
let item = OrphanItem(
url: URL(filePath: "/Users/someone/Library/Application Support/\(identifier)", directoryHint: .isDirectory),
kind: kind,
size: size,
modificationDate: written.map { now.addingTimeInterval(-$0) },
requiresPrivileges: false
)
return OrphanGroup(identifier: identifier, items: [item], rememberedApp: remembered)
}
private func left(_ name: String, team: String? = nil, daysAgo: Double = 40) -> RememberedApp {
RememberedApp(
bundleIdentifier: "com.example.app",
name: name,
teamIdentifier: team,
lastSeen: now.addingTimeInterval(-daysAgo * 24 * 60 * 60),
lastPath: "/Applications/\(name).app"
)
}
@Test func aPlugInOtherAppsLoadHoldsBackItselfAndNotTheRestOfItsGroup() {
let app = left("Example", daysAgo: 400)
var plugIn = OrphanItem(
url: URL(filePath: "/Users/someone/Library/Audio/Plug-Ins/Components/Example.component"),
kind: .plugIns, size: 1_000, modificationDate: now.addingTimeInterval(-400 * 24 * 60 * 60),
requiresPrivileges: false
)
plugIn.heldBack = .loadedByOtherApps
let settings = OrphanItem(
url: URL(filePath: "/Users/someone/Library/Preferences/com.example.app.plist"),
kind: .preferences, size: 1_000, modificationDate: now.addingTimeInterval(-401 * 24 * 60 * 60),
requiresPrivileges: false
)
var mixed = OrphanGroup(identifier: "com.example.app", items: [plugIn, settings], rememberedApp: app)
let alone = OrphanGroup(identifier: "com.example.app", items: [plugIn], rememberedApp: app)
mixed.confidence = OrphanConfidence.judge(mixed, now: now)
#expect(mixed.confidence.level == .certain)
#expect(mixed.selectableRows(canUseHelper: true).recommended == [settings.url])
#expect(OrphanConfidence.judge(alone, now: now).reasons == [.loadedByOtherApps])
}
@Test func whatOnlyANameTiesToAnIdentifierLeftBehindIsUnsureWithoutMemory() {
var item = OrphanItem(
url: URL(filePath: "/Users/someone/Library/Application Support/Tidewater", directoryHint: .isDirectory),
kind: .applicationSupport, size: 1_000, modificationDate: now.addingTimeInterval(-400 * 24 * 60 * 60),
requiresPrivileges: false
)
item.namedAfter = "org.example.Tidewater"
item.heldBack = .namedLikeTheApp
let named = OrphanGroup(identifier: "org.example.Tidewater", items: [item])
#expect(OrphanConfidence.judge(named, now: now) == OrphanConfidence(
level: .unsure, reasons: [.onlyTheName(of: "Tidewater")]
))
}
@Test("Something running under that identifier settles it, whatever else is true")
func runningOutranksEverything() {
let old = group(written: 400 * 24 * 60 * 60, remembered: left("Example"))
let judged = OrphanConfidence.judge(old, running: running("com.example.app", bundle: "Example"), now: now)
#expect(judged.level == .unsure)
#expect(judged.reasons == [.running("Example")])
}
@Test("What runs is named as Finder names its bundle, else by its identifier, the first of each identifier")
func whatRunsIsNamedAsFinderNamesIt() {
let old = group(identifier: "com.example.app", written: 400 * 24 * 60 * 60)
let processes = [
process("com.example.app.helper", bundle: "Example Helper"), process("com.example.app.helper", bundle: "Other"),
]
let byIdentifier = RunningCopies.byIdentifier(processes)
#expect(OrphanConfidence.judge(old, running: byIdentifier, now: now).reasons == [.running("Example Helper")])
#expect(OrphanConfidence.judge(old, running: running("com.example.app.helper"), now: now).reasons == [
.running("com.example.app.helper"),
])
}
private func process(_ identifier: String, bundle: String? = nil) -> RunningCopies.Process {
RunningCopies.Process(
identifier: 1, bundleIdentifier: identifier,
bundleURL: bundle.map { URL(filePath: "/Volumes/org.example.none/\($0).app", directoryHint: .isDirectory) }
)
}
private func running(_ identifier: String, bundle: String? = nil) -> [String: RunningCopies.Process] {
[identifier: process(identifier, bundle: bundle)]
}
@Test("A folder written this week belongs to something that still runs, even if no app claims it")
func recentWritingOutranksAGoneApp() {
let fresh = group(written: 2 * 24 * 60 * 60, remembered: left("Example"))
let judged = OrphanConfidence.judge(fresh, now: now)
#expect(judged.level == .unsure)
if case .writtenRecently = judged.reasons.first {} else {
Issue.record("expected the recent writing to be the reason, got \(judged.reasons)")
}
}
@Test("A write that was the uninstall itself is not read as something still running")
func theWriteWasTheRemoval() {
// Peel last saw the app four days ago; the folder was written four days ago too, when its files
// were taken out. That is the removal, not an app that is still working.
let removed = left("Example", daysAgo: 4)
let judged = OrphanConfidence.judge(group(written: 4 * 24 * 60 * 60, remembered: removed), now: now)
#expect(judged.level == .certain)
#expect(judged.reasons.contains { if case .appLeft = $0 { true } else { false } })
}
@Test("What the app wrote after Peel last listed it, before Peel saw it go, is no sign of something still running")
func writingBeforePeelSawItGo() {
var removed = left("Example", daysAgo: 7)
removed.goneSince = now.addingTimeInterval(-2 * 24 * 60 * 60)
let judged = OrphanConfidence.judge(group(written: 3 * 24 * 60 * 60, remembered: removed), now: now)
#expect(judged.level == .certain)
}
@Test("A write after the app was last seen is still something running")
func writingAfterwardsStillCounts() {
let removed = left("Example", daysAgo: 6)
let judged = OrphanConfidence.judge(group(written: 1 * 24 * 60 * 60, remembered: removed), now: now)
#expect(judged.level == .unsure)
}
@Test("What an app left is doubted while its maker still has another app installed")
func aMakerStillHere() {
let shared = group(written: 300 * 24 * 60 * 60, remembered: left("Example", team: "S8EX82NJP6"))
let judged = OrphanConfidence.judge(shared, installedTeams: ["S8EX82NJP6"], now: now)
#expect(judged.level == .unsure)
#expect(judged.reasons == [.sameMakerStillInstalled])
}
@Test("Having watched the app go does not settle it either: a maker's other app may only ever read what they share")
func aMakerStillHereOutranksHavingWatchedTheAppGo() {
let removed = left("Example", team: "S8EX82NJP6", daysAgo: 40)
let judged = OrphanConfidence.judge(
group(written: 40 * 24 * 60 * 60, remembered: removed),
installedTeams: ["S8EX82NJP6"],
now: now
)
#expect(judged.level == .unsure)
#expect(judged.reasons == [.sameMakerStillInstalled])
}
@Test func aHelperOfATwoPartAppRunningMakesItsGroupUnsure() {
let gone = RememberedApp(
bundleIdentifier: "md.example",
name: "Example",
teamIdentifier: nil,
lastSeen: now.addingTimeInterval(-40 * 24 * 60 * 60),
lastPath: "/Applications/Example.app"
)
let itsOwn = group(identifier: "md.example", written: 400 * 24 * 60 * 60, remembered: gone)
#expect(OrphanConfidence.judge(itsOwn, running: running("md.example.helper"), now: now).reasons == [.running("md.example.helper")])
let nobodysPrefix = group(identifier: "md.example", written: 400 * 24 * 60 * 60)
#expect(OrphanConfidence.judge(nobodysPrefix, running: running("md.example.helper"), now: now).level == .certain)
}
@Test("What is running is matched the way an app is: a helper runs under its app's identifier and a longer one")
func aRunningHelperOrItsAppBothCount() {
let old = group(identifier: "com.example.app", written: 400 * 24 * 60 * 60)
#expect(OrphanConfidence.judge(old, running: running("com.example.app.helper"), now: now).reasons == [.running("com.example.app.helper")])
#expect(OrphanConfidence.judge(old, running: running("COM.EXAMPLE.APP"), now: now).reasons == [.running("COM.EXAMPLE.APP")])
let helper = group(identifier: "com.example.app.helper", written: 400 * 24 * 60 * 60)
#expect(OrphanConfidence.judge(helper, running: running("com.example.app"), now: now).reasons == [.running("com.example.app")])
// Two components name a maker, not an app, and `com.example.apple` is not a helper of `com.example.app`.
let other = group(identifier: "com.example.other", written: 400 * 24 * 60 * 60)
#expect(OrphanConfidence.judge(other, running: running("com.example"), now: now).level == .certain)
#expect(OrphanConfidence.judge(old, running: running("com.example.apple"), now: now).level == .certain)
}
@Test("A Safari web app is its own app: Safari running, or another web app, says nothing about it")
func safariRunningSaysNothingOfAWebApp() {
let identifier = "com.apple.Safari.WebApp.0E4F6A2C-1B3D-4E5F-8A9B-0C1D2E3F4A5B"
let webApp = group(identifier: identifier, written: 400 * 24 * 60 * 60)
#expect(OrphanConfidence.judge(webApp, running: running("com.apple.Safari"), now: now).level == .certain)
#expect(OrphanConfidence.judge(webApp, running: running("com.apple.Safari.WebApp"), now: now).level == .certain)
let named = OrphanConfidence.judge(webApp, running: running(identifier, bundle: "Web App"), now: now)
#expect(named.reasons == [.running("Web App")])
}
@Test("An app Peel watched leave is the strongest thing it can say")
func peelWatchedItGo() {
let judged = OrphanConfidence.judge(group(written: 60 * 24 * 60 * 60, remembered: left("Numi")), now: now)
#expect(judged.level == .certain)
#expect(judged.reasons.contains { if case .appLeft(let name, _) = $0 { name == "Numi" } else { false } })
}
@Test("Something that wrote here weeks after the app was gone may still use the files, and the write is the reason")
func aWriteWeeksAfterTheAppLeft() {
let judged = OrphanConfidence.judge(
group(written: 20 * 24 * 60 * 60, remembered: left("Numi", daysAgo: 60)),
now: now
)
#expect(judged.level == .unsure)
#expect(judged.reasons == [.writtenAfterItLeft(name: "Numi", written: now.addingTimeInterval(-20 * 24 * 60 * 60))])
}
@Test("Months of nothing touching it settle it even when something wrote after the app was gone")
func aWriteAfterTheAppLeftLongAgo() throws {
let judged = OrphanConfidence.judge(
group(written: 250 * 24 * 60 * 60, remembered: left("Numi", daysAgo: 400)),
now: now
)
#expect(judged.level == .certain)
#expect(judged.reasons.count == 1)
guard case .untouched(let months) = try #require(judged.reasons.first) else {
Issue.record("expected the months without a write to be the reason, got \(judged.reasons)")
return
}
#expect(months >= OrphanConfidence.longUntouched)
}
@Test("Files that were not read in time say nothing about when they were written, so seeing the app go is not enough")
func aGoneAppWhoseFilesWereNotRead() {
let item = OrphanItem(
url: URL(filePath: "/Users/someone/Library/Application Support/com.example.app", directoryHint: .isDirectory),
kind: .applicationSupport,
size: nil,
modificationDate: now.addingTimeInterval(-400 * 24 * 60 * 60),
requiresPrivileges: false
)
let numi = left("Numi")
let judged = OrphanConfidence.judge(OrphanGroup(identifier: "com.example.app", items: [item], rememberedApp: numi), now: now)
#expect(judged.level == .likely)
#expect(judged.reasons == [.appLeft(name: "Numi", lastSeen: numi.lastSeen)])
}
@Test("Months of nothing touching it is enough on its own")
func longUntouched() {
let judged = OrphanConfidence.judge(group(written: 400 * 24 * 60 * 60), now: now)
#expect(judged.level == .certain)
#expect(
judged.reasons.contains {
if case .untouched(let months) = $0 { months >= OrphanConfidence.longUntouched } else { false }
}
)
}
@Test("A plug-in's date does not move when it is used, so its age says nothing")
func aPlugInsAgeSaysNothing() {
let judged = OrphanConfidence.judge(group(kind: .plugIns, written: 400 * 24 * 60 * 60), now: now)
#expect(judged.level == .likely)
#expect(judged.reasons == [.nothingClaimsIt])
}
@Test("A folder that did not answer was not read, so nothing is said about when it was last written")
func aFolderThatDidNotAnswerSaysNothingAboutItsDates() {
let item = OrphanItem(
url: URL(filePath: "/Users/someone/Library/Application Support/com.example.app", directoryHint: .isDirectory),
kind: .applicationSupport,
size: nil,
modificationDate: now.addingTimeInterval(-400 * 24 * 60 * 60),
requiresPrivileges: false
)
let untouched = OrphanConfidence.judge(OrphanGroup(identifier: "com.example.app", items: [item]), now: now)
#expect(untouched.level == .likely)
#expect(untouched.reasons == [.nothingClaimsIt])
// Nor is its own date read as the removal: what is inside may have been written since.
let removed = left("Example", daysAgo: 4)
let recent = OrphanItem(
url: item.url,
kind: .applicationSupport,
size: nil,
modificationDate: now.addingTimeInterval(-4 * 24 * 60 * 60),
requiresPrivileges: false
)
let judged = OrphanConfidence.judge(
OrphanGroup(identifier: "com.example.app", items: [recent], rememberedApp: removed),
now: now
)
#expect(judged.level == .unsure)
}
@Test("With nothing else to go on, Peel says only that nothing claims it")
func nothingElseToSay() {
let judged = OrphanConfidence.judge(group(written: 30 * 24 * 60 * 60), now: now)
#expect(judged.level == .likely)
#expect(judged.reasons == [.nothingClaimsIt])
}
@Test("A date in the future is not read as writing that has just happened")
func aClockThatRanAhead() {
let judged = OrphanConfidence.judge(group(written: -10 * 24 * 60 * 60), now: now)
#expect(judged.level == .likely)
// Nor as the removal of an app Peel saw go: that write would have to come before it.
let afterTheApp = OrphanConfidence.judge(group(written: -10 * 24 * 60 * 60, remembered: left("Numi")), now: now)
#expect(afterTheApp.level == .likely)
}
@Test("What Peel is sure about is listed first, and the largest of those first again")
func theOrder() {
let found: [(identifier: String, item: OrphanItem)] = [
("com.example.unsure", group(identifier: "com.example.unsure", written: 0, size: 900_000).items[0]),
("com.example.small", group(identifier: "com.example.small", written: 400 * 24 * 60 * 60, size: 10).items[0]),
("com.example.large", group(identifier: "com.example.large", written: 400 * 24 * 60 * 60, size: 500).items[0]),
]
let groups = OrphanScanner.group(found, now: now)
#expect(groups.map(\.identifier) == ["com.example.large", "com.example.small", "com.example.unsure"])
#expect(groups.first?.confidence.level == .certain)
#expect(groups.last?.confidence.level == .unsure)
}
}