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
@testable import PeelCore
import Testing
/// What moves when Move to Trash is pressed on any page of the storage tools: what is selected on every page the
/// person has seen, one part to a page, in one batch.
struct CarriedSelectionTests {
private typealias Page = CarriedSelection.Page
private typealias Part = CarriedSelection.Part
private let tools = ["orphans", "developer", "projects", "installers", "duplicates", "space", "fileSearch"]
private func part(_ tool: String, _ scope: String, _ sizes: [String: Int64?], source: String? = nil) -> Part {
Part(
page: Page(tool: tool, scope: scope),
title: scope,
source: source ?? scope,
sourceKey: nil,
sizes: Dictionary(uniqueKeysWithValues: sizes.map { (URL(filePath: "/Users/x/\($0.key)"), $0.value) })
)
}
/// A tool can select for the person on pages they never opened, so only a page they have seen is carried.
@Test func aPageNeverSeenIsNotCarried() {
var selection = CarriedSelection()
selection.saw(Page(tool: "developer", scope: "Xcode"))
let parts = selection.parts(from: [
part("developer", "Xcode", ["DerivedData": 900]),
part("developer", "npm", ["_cacache": 300]),
], order: tools)
#expect(parts.map(\.page.scope) == ["Xcode"])
}
@Test func knowsWhichPagesWereSeen() {
var selection = CarriedSelection()
selection.saw(Page(tool: "developer", scope: "npm"))
#expect(selection.hasSeen(Page(tool: "developer", scope: "npm")))
#expect(!selection.hasSeen(Page(tool: "developer", scope: "Xcode")))
}
/// The parts move in the sidebar's order of the tools, and within a tool in the order its pages were seen.
@Test func partsFollowTheToolsThenTheOrderSeen() {
var selection = CarriedSelection()
for page in [Page(tool: "projects", scope: "Peel"), Page(tool: "developer", scope: "npm"), Page(tool: "developer", scope: "Xcode")] {
selection.saw(page)
}
selection.saw(Page(tool: "developer", scope: "npm"))
let parts = selection.parts(from: [
part("developer", "Xcode", ["DerivedData": 900]),
part("projects", "Peel", ["node_modules": 300]),
part("developer", "npm", ["_cacache": 300]),
], order: tools)
#expect(parts.map(\.page) == [
Page(tool: "developer", scope: "npm"),
Page(tool: "developer", scope: "Xcode"),
Page(tool: "projects", scope: "Peel"),
])
}
@Test func aPartWhoseToolChecksWhatTheOthersLeftMovesLast() {
var selection = CarriedSelection()
for tool in ["duplicates", "fileSearch", "developer"] {
selection.saw(Page(tool: tool, scope: ""))
}
let duplicates = part("duplicates", "", ["Downloads/Film.mov": 900])
let parts = selection.parts(from: [
Part(page: duplicates.page, title: duplicates.title, source: duplicates.source, sourceKey: nil,
sizes: duplicates.sizes, movesLast: true),
part("fileSearch", "", ["Movies/Film.mov": 900]),
part("developer", "", ["Library/Caches/pip": 300]),
], order: tools)
#expect(parts.map(\.page.tool) == ["developer", "fileSearch", "duplicates"])
}
/// A page with nothing selected carries nothing.
@Test func aPageWithNothingSelectedIsLeftOut() {
var selection = CarriedSelection()
selection.saw(Page(tool: "duplicates", scope: ""))
#expect(selection.parts(from: [part("duplicates", "", [:])], order: tools).isEmpty)
}
/// The whole is every part's items together, and it says "Over" when one of their sizes is not known.
@Test func theWholeCountsEveryPart() {
let parts = [
part("developer", "Xcode", ["DerivedData": 900, "ModuleCache": 100]),
part("developer", "npm", ["_cacache": nil]),
part("space", "logs", ["a.log": 50]),
]
#expect(parts.itemCount == 4)
#expect(parts.total == SizeTotal(known: 1_050, isComplete: false))
#expect(parts.tools == ["developer", "space"])
}
/// Every part is tried in turn, whatever came of the ones before: a part whose tool kept it (an app it waits
/// for was opened) stays whole, and the others still move.
@Test func aPartKeptByItsToolDoesNotStopTheOthers() async {
let parts = [
part("developer", "Xcode", ["DerivedData": 900], source: "Xcode"),
part("projects", "Peel", ["node_modules": 300], source: "Peel"),
]
var tried: [String] = []
let pass = await CarriedSelection.pass(parts) { part in
tried.append(part.page.scope)
guard part.page.tool != "developer" else { return nil }
return TrashResult(
trashed: part.sizes.keys.map { TrashedItem(originalURL: $0, trashedURL: $0, date: .now) }
)
}
#expect(tried == ["Xcode", "Peel"])
#expect(pass.kept.map(\.page.scope) == ["Xcode"])
#expect(pass.moved.map(\.part.page.scope) == ["Peel"])
}
@Test func anItemAskedForThatChangedSinceIsSaidToHaveStayed() async {
let parts = [part("orphans", "org.example.chat", ["Chat": 100, "Chat/cache": 10, "chat.plist": 1, "gone": 5])]
let pass = await CarriedSelection.pass(parts) { part in
let folder = URL(filePath: "/Users/x/Chat")
let preferences = URL(filePath: "/Users/x/chat.plist")
return TrashResult(
trashed: [TrashedItem(originalURL: folder, trashedURL: folder, date: .now)],
failures: [TrashFailure(url: preferences, reason: .locked)]
)
}
#expect(pass.result.failures.map(\.url.lastPathComponent).sorted() == ["chat.plist", "gone"])
#expect(pass.result.failures.first { $0.url.lastPathComponent == "gone" }?.reason == .changedSinceScan)
}
/// What a pass did is told once, whichever parts it came from: every item the parts moved, and every one
/// their tools refused.
@Test func aPassTellsWhatEveryPartMovedAndRefused() async {
let parts = [
part("developer", "Xcode", ["DerivedData": 900, "ModuleCache": 100]),
part("space", "caches", ["com.gone.app": 50]),
]
let pass = await CarriedSelection.pass(parts) { part in
var result = TrashResult()
for url in part.sizes.keys {
if url.lastPathComponent == "ModuleCache" {
result.failures.append(TrashFailure(url: url, reason: .guarded(.protectedLocation)))
} else {
result.trashed.append(TrashedItem(originalURL: url, trashedURL: url, date: .now))
}
}
return result
}
#expect(Set(pass.result.trashed.map(\.originalURL.lastPathComponent)) == ["DerivedData", "com.gone.app"])
#expect(pass.result.failures.map(\.url.lastPathComponent) == ["ModuleCache"])
}
/// What a part's tool refused for the item itself leaves its selection, so the next Move to Trash, on any page,
/// does not carry it again. What was refused for Peel's own state stays selected for when that is put right.
@Test func whatWasRefusedForItselfLeavesTheSelection() async {
let parts = [part("space", "caches", ["com.apple.akd": 10, "Kept.app": 20, "Gone": 30, "Later": 40])]
let pass = await CarriedSelection.pass(parts) { part in
var result = TrashResult()
for url in part.sizes.keys {
switch url.lastPathComponent {
case "com.apple.akd": result.failures.append(TrashFailure(url: url, reason: .heldOpen(by: ["akd"])))
case "Kept.app": result.failures.append(TrashFailure(url: url, reason: .guarded(.protectedLocation)))
case "Later": result.failures.append(TrashFailure(url: url, reason: .historyUnreadable))
default: result.trashed.append(TrashedItem(originalURL: url, trashedURL: url, date: .now))
}
}
return result
}
#expect(pass.refused.map(\.page.scope) == ["caches"])
#expect(Set(pass.refused.flatMap(\.sizes.keys).map(\.lastPathComponent)) == ["com.apple.akd", "Kept.app"])
}
@Test func anItemTwoPagesSelectedCountsOnce() {
var selection = CarriedSelection()
selection.saw(Page(tool: "installers", scope: "updateDownload"))
selection.saw(Page(tool: "space", scope: "caches"))
let parts = selection.parts(from: [
part("installers", "updateDownload", ["Library/Caches/notes-updater": 300]),
part("space", "caches", ["Library/Caches/notes-updater/": 300, "Library/Caches/org.example.app": 50]),
], order: tools)
#expect(parts.map(\.page.scope) == ["updateDownload", "caches"])
#expect(parts.itemCount == 2)
#expect(parts.total.known == 350)
#expect(parts.last.map { Set($0.sizes.keys.map(\.lastPathComponent)) } == ["org.example.app"])
}
@Test func anItemTwoPagesSelectedCountsWithThePageAsking() {
var selection = CarriedSelection()
selection.saw(Page(tool: "installers", scope: "updateDownload"))
selection.saw(Page(tool: "space", scope: "caches"))
let parts = selection.parts(from: [
part("installers", "updateDownload", ["Library/Caches/notes-updater": 300]),
part("space", "caches", ["Library/Caches/notes-updater/": 300]),
], order: tools, asking: Page(tool: "space", scope: "caches"))
#expect(parts.map(\.page.scope) == ["caches"])
}
@Test func anItemInsideAFolderAnotherPageSelectedCountsWithTheFolder() {
var selection = CarriedSelection()
selection.saw(Page(tool: "installers", scope: "updateDownload"))
selection.saw(Page(tool: "space", scope: "caches"))
let parts = selection.parts(from: [
part("installers", "updateDownload", ["Library/Caches/org.example.notes/org.sparkle-project.Sparkle": 300]),
part("space", "caches", ["Library/Caches/org.example.notes": 900]),
], order: tools)
#expect(parts.map(\.page.scope) == ["caches"])
#expect(parts.total.known == 900)
}
@Test func onePagesSelectionIsLeftAsItIs() {
var selection = CarriedSelection()
selection.saw(Page(tool: "space", scope: "caches"))
let parts = selection.parts(from: [
part("space", "caches", ["Library/Caches/org.example.notes": 900, "Library/Caches/org.example.notes/a": 1]),
], order: tools)
#expect(parts.itemCount == 2)
}
@Test func aPagesOwnFolderAndItsItemStayBesideAnotherPage() {
var selection = CarriedSelection()
selection.saw(Page(tool: "installers", scope: "updateDownload"))
selection.saw(Page(tool: "space", scope: "caches"))
let parts = selection.parts(from: [
part("installers", "updateDownload", ["Library/Caches/notes-updater": 300]),
part("space", "caches", ["Library/Caches/org.example.notes": 900, "Library/Caches/org.example.notes/a": 1]),
], order: tools)
#expect(parts.itemCount == 3)
}
@Test func anItemUnderFoldersTwoPagesSelectedMovesWithTheOutermost() {
var selection = CarriedSelection()
selection.saw(Page(tool: "installers", scope: "updateDownload"))
selection.saw(Page(tool: "duplicates", scope: ""))
selection.saw(Page(tool: "space", scope: "caches"))
let sparkle = "Library/Caches/org.example.notes/org.sparkle-project.Sparkle"
let parts = selection.parts(from: [
part("installers", "updateDownload", [sparkle: 300]),
part("duplicates", "", ["\(sparkle)/1/update.zip": 200, "Downloads/a.dmg": 50]),
part("space", "caches", ["Library/Caches/org.example.notes": 900]),
], order: tools, asking: Page(tool: "duplicates", scope: ""))
#expect(parts.map(\.page.tool) == ["duplicates", "space"])
#expect(parts.total.known == 950)
}
@Test func findsThePartsHoldingWhatMoved() {
let parts = [
part("installers", "updateDownload", ["Library/Caches/notes-updater": 300]),
part("installers", "updateDownload2", ["Library/Caches/org.example.notes/org.sparkle-project.Sparkle": 9]),
part("developer", "npm", ["_cacache": 300]),
]
let moved = ["Library/Caches/notes-updater/", "Library/Caches/org.example.notes"].map {
URL(filePath: "/Users/x/\($0)")
}
#expect(parts.holding(moved).map(\.page.scope) == ["updateDownload", "updateDownload2"])
}
/// What a pass moved is one History batch, each record under its own part's source and tool, with the size
/// its page measured, and none for an item whose size was not known.
@Test func whatMovedIsOneBatchWithEachPartsName() async {
let parts = [
part("developer", "Xcode", ["DerivedData": 900], source: "Xcode"),
part("duplicates", "", ["copy.bin": nil], source: "Duplicates"),
]
let batch = UUID()
var records: [RemovalRecord] = []
_ = await CarriedSelection.pass(parts) { part in
let result = TrashResult(
trashed: part.sizes.keys.map { TrashedItem(originalURL: $0, trashedURL: $0, date: .now) }
)
records += part.removalPart.records(of: result, sizes: part.measuredSizes, batch: batch)
return result
}
#expect(records.allSatisfy { $0.batch == batch })
#expect(Set(records.map(\.tool)) == ["developer", "duplicates"])
#expect(records.first { $0.tool == "developer" }?.source == "Xcode")
#expect(records.first { $0.tool == "developer" }?.size == 900)
#expect(records.first { $0.tool == "duplicates" }?.size == nil)
#expect(RemovalRecord.grouped(records).first?.parts.map(\.source) == ["Xcode", "Duplicates"])
}
}