1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
@testable import Grain
import Nuke
import XCTest
final class ImagePrefetchPlanningTests: GrainTestCase {
// MARK: - Helpers
private func photo(index: Int) -> (thumb: String, fullsize: String) {
(thumb: "https://cdn.example.com/thumb/\(index).jpg", fullsize: "https://cdn.example.com/full/\(index).jpg")
}
private func photos(_ count: Int) -> [(thumb: String, fullsize: String)] {
(0 ..< count).map { photo(index: $0) }
}
private func urls(from requests: [ImageRequest]) -> [String] {
requests.compactMap { $0.url?.absoluteString }
}
// MARK: - Carousel: Empty / Edge Cases
func testCarousel_emptyPhotos_returnsEmpty() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: [], currentPage: 0)
XCTAssertTrue(result.all.isEmpty)
}
func testCarousel_outOfBoundsPage_returnsEmpty() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(3), currentPage: 10)
XCTAssertTrue(result.all.isEmpty)
}
func testCarousel_negativePage_returnsEmpty() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(3), currentPage: -1)
XCTAssertTrue(result.all.isEmpty)
}
// MARK: - Carousel: Page 0
func testCarousel_singlePhoto_page0_prefetchesOnlyThatFullsize() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(1), currentPage: 0)
XCTAssertEqual(urls(from: result.high), ["https://cdn.example.com/full/0.jpg"])
XCTAssertTrue(result.normal.isEmpty)
XCTAssertTrue(result.low.isEmpty)
}
func testCarousel_twoPhotos_page0_prefetchesBothFullsize() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(2), currentPage: 0)
XCTAssertEqual(urls(from: result.high), [
"https://cdn.example.com/full/0.jpg",
"https://cdn.example.com/full/1.jpg",
])
}
func testCarousel_fivePhotos_page0_prefetchesOnlyFirstTwo() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(5), currentPage: 0)
XCTAssertEqual(urls(from: result.high).count, 2)
XCTAssertTrue(result.normal.isEmpty)
}
// MARK: - Carousel: Page 1
func testCarousel_threePhotos_page1_prefetchesThirdFullsizeAndAllThumbs() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(3), currentPage: 1)
XCTAssertEqual(urls(from: result.high), ["https://cdn.example.com/full/2.jpg"])
XCTAssertEqual(urls(from: result.normal).count, 3) // all 3 thumbs
XCTAssertTrue(urls(from: result.normal).allSatisfy { $0.contains("/thumb/") })
}
func testCarousel_twoPhotos_page1_noThirdFullsize_stillPrefetchesThumbs() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(2), currentPage: 1)
XCTAssertTrue(result.high.isEmpty)
XCTAssertEqual(urls(from: result.normal).count, 2) // both thumbs
}
// MARK: - Carousel: Page 2+
func testCarousel_tenPhotos_page5_prefetchesNextAtHighRestAtNormal() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(10), currentPage: 5)
XCTAssertEqual(urls(from: result.high), ["https://cdn.example.com/full/6.jpg"])
XCTAssertEqual(urls(from: result.normal).count, 3) // 7, 8, 9
}
func testCarousel_lastPage_returnsEmpty() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(3), currentPage: 2)
XCTAssertTrue(result.high.isEmpty)
XCTAssertTrue(result.normal.isEmpty)
}
// MARK: - Feed: Edge Cases
func testFeed_emptyGalleries_returnsEmpty() {
let result = ImagePrefetchPlanning.feedPrefetchRequests(galleries: [], currentIndex: 0)
XCTAssertTrue(result.all.isEmpty)
}
func testFeed_atLastGallery_returnsEmpty() {
let galleries: [(firstThumb: String?, firstFullsize: String?)] = [
(firstThumb: "t0", firstFullsize: "f0"),
]
let result = ImagePrefetchPlanning.feedPrefetchRequests(galleries: galleries, currentIndex: 0)
XCTAssertTrue(result.all.isEmpty)
}
// MARK: - Feed: Look-ahead
func testFeed_tenGalleries_atIndex3_prefetchesNext3() {
let galleries: [(firstThumb: String?, firstFullsize: String?)] = (0 ..< 10).map { i in
(firstThumb: "https://cdn.example.com/thumb/\(i).jpg",
firstFullsize: "https://cdn.example.com/full/\(i).jpg")
}
let result = ImagePrefetchPlanning.feedPrefetchRequests(galleries: galleries, currentIndex: 3)
// High: thumb 4, fullsize 4, thumb 5
// Normal: fullsize 5
// Low: fullsize 6, thumb 6
let allUrls = urls(from: result.all)
XCTAssertTrue(allUrls.contains("https://cdn.example.com/full/4.jpg"))
XCTAssertTrue(allUrls.contains("https://cdn.example.com/full/5.jpg"))
XCTAssertTrue(allUrls.contains("https://cdn.example.com/full/6.jpg"))
XCTAssertTrue(allUrls.contains("https://cdn.example.com/thumb/4.jpg"))
}
func testFeed_galleryWithNoPhotos_skipsGracefully() {
let galleries: [(firstThumb: String?, firstFullsize: String?)] = [
(firstThumb: "t0", firstFullsize: "f0"),
(firstThumb: nil, firstFullsize: nil), // gallery with no photos
(firstThumb: "t2", firstFullsize: "f2"),
]
let result = ImagePrefetchPlanning.feedPrefetchRequests(galleries: galleries, currentIndex: 0)
// Should still get gallery at index 2
let allUrls = urls(from: result.all)
XCTAssertTrue(allUrls.contains("t2"))
XCTAssertFalse(allUrls.contains("nil"))
}
// MARK: - Stories: Edge Cases
func testStory_emptyCurrentStories_returnsEmpty() {
let result = ImagePrefetchPlanning.storyPrefetchRequests(
currentStories: [],
currentStoryIndex: 0,
nextAuthorStories: nil,
secondNextAuthorStories: nil,
thirdNextFirstStory: nil,
fourthNextFirstStory: nil
)
XCTAssertTrue(result.all.isEmpty)
}
// MARK: - Stories: Priority Order
func testStory_fiveStories_atIndex0_prefetchesCorrectPriorities() {
let stories = photos(5)
let nextAuthor = photos(3)
let result = ImagePrefetchPlanning.storyPrefetchRequests(
currentStories: stories,
currentStoryIndex: 0,
nextAuthorStories: nextAuthor,
secondNextAuthorStories: nil,
thirdNextFirstStory: nil,
fourthNextFirstStory: nil
)
let highUrls = Set(urls(from: result.high))
// High fullsizes: stories 1, 2 of current + first of next author
XCTAssertTrue(highUrls.contains("https://cdn.example.com/full/1.jpg"))
XCTAssertTrue(highUrls.contains("https://cdn.example.com/full/2.jpg"))
XCTAssertTrue(highUrls.contains("https://cdn.example.com/full/0.jpg"))
// High thumbs: every reachable thumb (current + next author)
for i in 0 ..< 5 {
XCTAssertTrue(highUrls.contains("https://cdn.example.com/thumb/\(i).jpg"))
}
// Normal: stories 3, 4 of current (rest of stack — fullsize only)
XCTAssertEqual(urls(from: result.normal), [
"https://cdn.example.com/full/3.jpg",
"https://cdn.example.com/full/4.jpg",
])
}
func testStory_lastStoryOfAuthor_onlyPrefetchesNextAuthor() {
let stories = photos(1)
let nextAuthor = photos(2)
let result = ImagePrefetchPlanning.storyPrefetchRequests(
currentStories: stories,
currentStoryIndex: 0,
nextAuthorStories: nextAuthor,
secondNextAuthorStories: nil,
thirdNextFirstStory: nil,
fourthNextFirstStory: nil
)
let highUrls = Set(urls(from: result.high))
// High fullsize: only next author's first (no more current stories)
XCTAssertTrue(highUrls.contains("https://cdn.example.com/full/0.jpg"))
XCTAssertFalse(highUrls.contains("https://cdn.example.com/full/1.jpg"))
// High thumbs: current + next author thumbs
XCTAssertTrue(highUrls.contains("https://cdn.example.com/thumb/0.jpg"))
XCTAssertTrue(highUrls.contains("https://cdn.example.com/thumb/1.jpg"))
}
func testStory_noNextAuthorData_skipsThatTier() {
let stories = photos(3)
let result = ImagePrefetchPlanning.storyPrefetchRequests(
currentStories: stories,
currentStoryIndex: 0,
nextAuthorStories: nil,
secondNextAuthorStories: nil,
thirdNextFirstStory: nil,
fourthNextFirstStory: nil
)
let highUrls = Set(urls(from: result.high))
// High fullsize: only current stories 1, 2 (no next author tier)
XCTAssertTrue(highUrls.contains("https://cdn.example.com/full/1.jpg"))
XCTAssertTrue(highUrls.contains("https://cdn.example.com/full/2.jpg"))
// High thumbs: only current author's thumbs
XCTAssertTrue(highUrls.contains("https://cdn.example.com/thumb/0.jpg"))
XCTAssertTrue(highUrls.contains("https://cdn.example.com/thumb/1.jpg"))
XCTAssertTrue(highUrls.contains("https://cdn.example.com/thumb/2.jpg"))
XCTAssertTrue(result.low.isEmpty)
}
func testStory_fullPriorityChain() {
let current = photos(2)
let nextAuthor = [(thumb: "https://next/t0", fullsize: "https://next/f0")]
let secondNext = [(thumb: "https://second/t0", fullsize: "https://second/f0"),
(thumb: "https://second/t1", fullsize: "https://second/f1")]
let third = (thumb: "https://third/t0", fullsize: "https://third/f0")
let fourth = (thumb: "https://fourth/t0", fullsize: "https://fourth/f0")
let result = ImagePrefetchPlanning.storyPrefetchRequests(
currentStories: current,
currentStoryIndex: 0,
nextAuthorStories: nextAuthor,
secondNextAuthorStories: secondNext,
thirdNextFirstStory: third,
fourthNextFirstStory: fourth
)
let highUrls = Set(urls(from: result.high))
// High fullsizes: current story 1 + next author first
XCTAssertTrue(highUrls.contains("https://cdn.example.com/full/1.jpg"))
XCTAssertTrue(highUrls.contains("https://next/f0"))
// High thumbs: every reachable thumb at every tier
XCTAssertTrue(highUrls.contains("https://cdn.example.com/thumb/0.jpg"))
XCTAssertTrue(highUrls.contains("https://cdn.example.com/thumb/1.jpg"))
XCTAssertTrue(highUrls.contains("https://next/t0"))
XCTAssertTrue(highUrls.contains("https://second/t0"))
XCTAssertTrue(highUrls.contains("https://second/t1"))
XCTAssertTrue(highUrls.contains("https://third/t0"))
XCTAssertTrue(highUrls.contains("https://fourth/t0"))
// Normal: second-next author stories 0, 1 (fullsize only)
XCTAssertEqual(urls(from: result.normal).count, 2)
// Low: third + fourth author firsts (fullsize only)
XCTAssertEqual(urls(from: result.low).count, 2)
}
// MARK: - Priority correctness
func testCarousel_page0_allRequestsAreHighPriority() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(5), currentPage: 0)
XCTAssertTrue(result.high.allSatisfy { $0.priority == .high })
XCTAssertTrue(result.normal.isEmpty)
XCTAssertTrue(result.low.isEmpty)
}
func testCarousel_page1_thumbsAreNormalPriority() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(5), currentPage: 1)
XCTAssertTrue(result.high.allSatisfy { $0.priority == .high })
XCTAssertTrue(result.normal.allSatisfy { $0.priority == .normal })
}
func testFeed_priorityDescentAcrossOffsets() {
let galleries: [(firstThumb: String?, firstFullsize: String?)] = (0 ..< 10).map { i in
(firstThumb: "https://cdn.example.com/thumb/\(i).jpg",
firstFullsize: "https://cdn.example.com/full/\(i).jpg")
}
let result = ImagePrefetchPlanning.feedPrefetchRequests(galleries: galleries, currentIndex: 3)
XCTAssertTrue(urls(from: result.high).contains("https://cdn.example.com/full/4.jpg"))
XCTAssertTrue(urls(from: result.normal).contains("https://cdn.example.com/full/5.jpg"))
XCTAssertTrue(urls(from: result.low).contains("https://cdn.example.com/full/6.jpg"))
}
// MARK: - Story boundary: currentStoryIndex at end
func testStory_atLastIndex_noCurrentPrefetchButNextAuthorStill() {
let stories = photos(3)
let nextAuthor = photos(2)
let result = ImagePrefetchPlanning.storyPrefetchRequests(
currentStories: stories,
currentStoryIndex: 2,
nextAuthorStories: nextAuthor,
secondNextAuthorStories: nil,
thirdNextFirstStory: nil,
fourthNextFirstStory: nil
)
let highUrls = urls(from: result.high)
XCTAssertFalse(highUrls.contains("https://cdn.example.com/full/3.jpg"))
XCTAssertTrue(highUrls.contains("https://cdn.example.com/full/0.jpg"))
}
// MARK: - No duplicate URLs
func testCarousel_page0_noDuplicateURLs() {
let result = ImagePrefetchPlanning.carouselPrefetchRequests(photos: photos(5), currentPage: 0)
let allUrls = urls(from: result.all)
XCTAssertEqual(allUrls.count, Set(allUrls).count)
}
func testFeed_noDuplicateURLs() {
let galleries: [(firstThumb: String?, firstFullsize: String?)] = (0 ..< 10).map { i in
(firstThumb: "https://cdn.example.com/thumb/\(i).jpg",
firstFullsize: "https://cdn.example.com/full/\(i).jpg")
}
let result = ImagePrefetchPlanning.feedPrefetchRequests(galleries: galleries, currentIndex: 3)
let allUrls = urls(from: result.all)
XCTAssertEqual(allUrls.count, Set(allUrls).count)
}
}