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
import Foundation
import UIKit
extension MockBlueskyService {
static func mockFacet(in text: String, matching target: String, feature: PostFacet.Feature) -> PostFacet {
guard let range = text.range(of: target) else {
preconditionFailure("The mock facet text must exist in its post text.")
}
let byteStart = String(text[..<range.lowerBound]).utf8.count
let byteEnd = byteStart + String(text[range]).utf8.count
return PostFacet(byteStart: byteStart, byteEnd: byteEnd, feature: feature)
}
static let imageGridPNGData: Data = {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
let size =
ProcessInfo.processInfo.arguments.contains("-ui-testing-wide-image")
? CGSize(width: 320, height: 80) : CGSize(width: 32, height: 32)
return UIGraphicsImageRenderer(size: size, format: format)
.image { context in
UIColor(red: 0.2, green: 0.48, blue: 0.78, alpha: 1).setFill()
context.cgContext.fill(CGRect(origin: .zero, size: size))
}
.pngData()!
}()
static let viewerPNGData: Data = {
let format = UIGraphicsImageRendererFormat()
format.scale = 1
let size = CGSize(width: 1600, height: 800)
return UIGraphicsImageRenderer(size: size, format: format).image { context in
UIColor.blue.setFill()
context.fill(CGRect(origin: .zero, size: size))
}.pngData()!
}()
var imageGridImageCount: Int? {
let arguments = ProcessInfo.processInfo.arguments
guard let argumentIndex = arguments.firstIndex(of: "-ui-testing-image-grid"),
arguments.indices.contains(argumentIndex + 1),
let count = Int(arguments[argumentIndex + 1]),
(1...10).contains(count)
else { return nil }
return count
}
func withInteractions(_ original: Post) -> Post {
var post = original
post.isSaved = loadState.savedPosts.contains { $0.id == post.id }
if let liked = loadState.likes[post.id] {
if post.isLiked != liked { post.likeCount = max(0, post.likeCount + (liked ? 1 : -1)) }
post.isLiked = liked
post.likeURI = liked ? "mock-like-\(post.id)" : nil
}
if let reposted = loadState.reposts[post.id] {
if post.isReposted != reposted { post.repostCount = max(0, post.repostCount + (reposted ? 1 : -1)) }
post.isReposted = reposted
post.repostURI = reposted ? "mock-repost-\(post.id)" : nil
}
return post
}
}