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.

Test how the iCloud Drive page removes downloads and stops

Țugui Dragoș24beda213d0e
diff --git a/Peel/Model/CloudLibrary.swift b/Peel/Model/CloudLibrary.swift
index f4518233..d95c9a25 100644
--- a/Peel/Model/CloudLibrary.swift
+++ b/Peel/Model/CloudLibrary.swift
@@ -38,9 +38,21 @@ final class CloudLibrary {
}
private(set) var selectedSize: Int64 = 0
private let home: URL
+ private let scan: @MainActor (URL, Exclusions, Set<URL>) async -> CloudScan
+ private let removeDownloads: @MainActor ([CloudFile], Exclusions) async -> CloudFreeing
- init(home: URL = .homeDirectory) {
+ init(
+ home: URL = .homeDirectory,
+ scan: @escaping @MainActor (URL, Exclusions, Set<URL>) async -> CloudScan = {
+ await CloudStorage.downloaded(home: $0, exclusions: $1, desktopPictures: $2)
+ },
+ removeDownloads: @escaping @MainActor ([CloudFile], Exclusions) async -> CloudFreeing = {
+ await CloudStorage.free($0, exclusions: $1)
+ }
+ ) {
self.home = home
+ self.scan = scan
+ self.removeDownloads = removeDownloads
}
/// What the listed files take on this Mac, only the least it can be when there is more than the list shows, and
@@ -76,21 +88,17 @@ final class CloudLibrary {
func refresh() async {
refusals = []
let pictures = Set(NSScreen.screens.compactMap { NSWorkspace.shared.desktopImageURL(for: $0) })
- let home = home
+ let (home, scan) = (home, scan)
guard
- let scan = await scanRun.run({
- await CloudStorage.downloaded(
- home: home, exclusions: ExclusionsStore.shared.exclusions, desktopPictures: pictures
- )
- })
+ let found = await scanRun.run({ await scan(home, ExclusionsStore.shared.exclusions, pictures) })
else { return }
- folders = scan.folders
- searchable = CloudFolder.searchable(scan.files, in: scan.folders)
- files = scan.files
- cutShort = scan.cutShort
- couldNotRead = scan.couldNotRead
- sharedDownloads = scan.sharedDownloads
- selectedURLs.formIntersection(Set(scan.files.map(\.url)))
+ folders = found.folders
+ searchable = CloudFolder.searchable(found.files, in: found.folders)
+ files = found.files
+ cutShort = found.cutShort
+ couldNotRead = found.couldNotRead
+ sharedDownloads = found.sharedDownloads
+ selectedURLs.formIntersection(Set(found.files.map(\.url)))
}
/// Frees the local copies of the selected files and returns the bytes freed. Nothing goes to the Trash, since
@@ -100,8 +108,9 @@ final class CloudLibrary {
defer { isFreeing = false }
let chosen = files?.filter { selectedURLs.contains($0.url) } ?? []
let exclusions = ExclusionsStore.shared.exclusions
+ let removeDownloads = removeDownloads
let task = Task { [freeing] in
- await freeing.run(toMove: chosen.count) { await CloudStorage.free(chosen, exclusions: exclusions) }
+ await freeing.run(toMove: chosen.count) { await removeDownloads(chosen, exclusions) }
}
freeingTask = task
let done = await task.value
diff --git a/PeelTests/CloudLibraryTests.swift b/PeelTests/CloudLibraryTests.swift
index b36502a8..995e4904 100644
--- a/PeelTests/CloudLibraryTests.swift
+++ b/PeelTests/CloudLibraryTests.swift
@@ -1,10 +1,22 @@
import Foundation
@testable import Peel
-import PeelCore
+@testable import PeelCore
import Testing
@MainActor
struct CloudLibraryTests {
+ private final class Asked {
+ var scans = 0
+ var removals: [[URL]] = []
+ }
+
+ private static func download(_ name: String) -> CloudFile {
+ CloudFile(
+ url: URL(filePath: "/org.example/\(name)"), name: name, container: "iCloud Drive", size: 2_000_000,
+ modified: nil
+ )
+ }
+
@Test func hasNoTotalUntilICloudDriveIsRead() async throws {
let home = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
let drive = home.appending(path: "Library/Mobile Documents", directoryHint: .isDirectory)
@@ -32,4 +44,57 @@ struct CloudLibraryTests {
#expect(!cloud.couldNotRead)
#expect(cloud.total == SizeTotal(known: 0, isComplete: true))
}
+
+ @Test func saysWhatStayedOnceTheListIsReadAgain() async {
+ let freed = Self.download("Freed.mov")
+ let kept = Self.download("Kept.mov")
+ let asked = Asked()
+ let cloud = CloudLibrary(
+ scan: { _, _, _ in
+ asked.scans += 1
+ return CloudScan(files: asked.scans == 1 ? [freed, kept] : [kept])
+ },
+ removeDownloads: { files, _ in
+ asked.removals.append(files.map(\.url))
+ return CloudFreeing(freed: [freed], refused: [CloudRefusal(url: kept.url, reason: .changedSinceScan)])
+ }
+ )
+ await cloud.refresh()
+ cloud.selectedURLs = [freed.url, kept.url]
+
+ let size = await cloud.freeSelected()
+
+ #expect(asked.removals == [[freed.url, kept.url]])
+ #expect(size == freed.size)
+ #expect(cloud.files == [kept])
+ #expect(cloud.selectedURLs == [kept.url])
+ #expect(cloud.refusals.map(\.url) == [kept.url])
+ }
+
+ @Test func stopsRemovingDownloadsWhenAsked() async {
+ let download = Self.download("Film.mov")
+ let (started, starts) = AsyncStream.makeStream(of: Void.self)
+ let cloud = CloudLibrary(
+ scan: { _, _, _ in CloudScan(files: [download]) },
+ removeDownloads: { files, _ in
+ starts.yield()
+ do {
+ try await Task.sleep(for: .seconds(30))
+ return CloudFreeing(freed: files)
+ } catch {
+ return CloudFreeing()
+ }
+ }
+ )
+ await cloud.refresh()
+ cloud.selectedURLs = [download.url]
+ let removal = Task { await cloud.freeSelected() }
+ var waiting = started.makeAsyncIterator()
+ await waiting.next()
+
+ cloud.stopFreeing()
+
+ #expect(await removal.value == 0)
+ #expect(!cloud.isFreeing)
+ }
}