6
6
//
7
7
8
8
import XCTest
9
- @testable import Hub
10
9
import Combine
10
+ @testable import Hub
11
+
12
+ /// Errors that can occur during the download process
11
13
enum DownloadError : Error {
12
14
case invalidDownloadLocation
13
15
case unexpectedError
14
16
}
17
+
15
18
final class DownloaderTests : XCTestCase {
16
19
var tempDir : URL !
17
20
@@ -26,6 +29,7 @@ final class DownloaderTests: XCTestCase {
26
29
super. tearDown ( )
27
30
}
28
31
32
+ /// This test downloads a known config file, verifies the download completes, checks the content matches expected value
29
33
func testSuccessfulDownload( ) async throws {
30
34
// Create a test file
31
35
let url = URL ( string: " https://huggingface.co/coreml-projects/Llama-2-7b-chat-coreml/resolve/main/config.json " ) !
@@ -75,6 +79,7 @@ final class DownloaderTests: XCTestCase {
75
79
XCTAssertEqual ( try String ( contentsOf: destination, encoding: . utf8) , fileContent)
76
80
}
77
81
82
+ /// This test attempts to download with incorrect expected file, verifies the download fails, ensures no partial file is left behind
78
83
func testDownloadFailsWithIncorrectSize( ) async throws {
79
84
let url = URL ( string: " https://huggingface.co/coreml-projects/Llama-2-7b-chat-coreml/resolve/main/config.json " ) !
80
85
let destination = tempDir. appendingPathComponent ( " config.json " )
@@ -97,6 +102,8 @@ final class DownloaderTests: XCTestCase {
97
102
XCTAssertFalse ( FileManager . default. fileExists ( atPath: destination. path) )
98
103
}
99
104
105
+ /// This test downloads an LFS file, interrupts the download at 50% and 75% progress,
106
+ /// verifies the download can resume and complete successfully, checks the final file exists and has content
100
107
func testSuccessfulInterruptedDownload( ) async throws {
101
108
let url = URL ( string: " https://huggingface.co/coreml-projects/sam-2-studio/resolve/main/SAM%202%20Studio%201.1.zip " ) !
102
109
let destination = tempDir. appendingPathComponent ( " SAM%202%20Studio%201.1.zip " )
@@ -108,18 +115,23 @@ final class DownloaderTests: XCTestCase {
108
115
let downloader = Downloader (
109
116
from: url,
110
117
to: destination,
111
- expectedSize: 73194001
118
+ expectedSize: 73194001 // Correct size for verification
112
119
)
120
+
121
+ // First interruption point at 50%
113
122
var threshold = 0.5
114
123
115
124
var subscriber : AnyCancellable ?
116
125
117
126
do {
127
+ // Monitor download progress and interrupt at thresholds to test if
128
+ // download continues from where it left off
118
129
try await withCheckedThrowingContinuation { ( continuation: CheckedContinuation < Void , Error > ) in
119
130
subscriber = downloader. downloadState. sink { state in
120
131
switch state {
121
132
case . downloading( let progress) :
122
133
if threshold != 1.0 && progress >= threshold {
134
+ // Move to next threshold and interrupt
123
135
threshold = threshold == 0.5 ? 0.75 : 1.0
124
136
downloader. cancel ( )
125
137
}
0 commit comments