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
import Foundation
import XCTest
@testable import OldFella
final class NotificationBatchFixture {
let suiteName: String
let defaults: UserDefaults
let session: URLSession
let token: String
var failPostURI: String? = NotificationBatchFixture.postURI(25)
var omitPostURI: String?
var omitOnlyFromBatch = false
var mutedPostURI: String?
var malformedPostURI: String?
var recordedBatchSizes: [Int] = []
let batchSizesLock = NSLock()
var batchSizes: [Int] {
get { batchSizesLock.withLock { recordedBatchSizes } }
set { batchSizesLock.withLock { recordedBatchSizes = newValue } }
}
lazy var service = ATProtoBlueskyService(defaults: defaults, session: session)
init() throws {
suiteName = "OldFellaNotificationBatchTests.\(UUID().uuidString)"
defaults = try XCTUnwrap(UserDefaults(suiteName: suiteName))
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [StubURLProtocol.self]
session = URLSession(configuration: configuration)
let futureExpiry = Int(Date().addingTimeInterval(3600).timeIntervalSince1970)
let payload = Data("{\"exp\":\(futureExpiry)}".utf8).base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: "=", with: "")
token = "header.\(payload).signature"
}
func install() {
StubURLProtocol.clearRequests()
StubURLProtocol.handler = response
}
func cleanUp() {
StubURLProtocol.errorHandler = nil
StubURLProtocol.handler = nil
session.invalidateAndCancel()
defaults.removePersistentDomain(forName: suiteName)
}
func response(_ request: URLRequest) throws -> (Int, Data) {
switch request.url?.path {
case "/xrpc/com.atproto.identity.resolveHandle":
return (200, Data("{\"did\":\"did:plc:reader\"}".utf8))
case "/did:plc:reader":
return (
200,
Data(
("{\"id\":\"did:plc:reader\",\"service\":[{\"id\":\"#atproto_pds\","
+ "\"type\":\"AtprotoPersonalDataServer\","
+ "\"serviceEndpoint\":\"https://pds.example.test\"}]}")
.utf8)
)
case "/xrpc/com.atproto.server.createSession":
return (
200,
Data(
("{\"handle\":\"reader.example\",\"did\":\"did:plc:reader\","
+ "\"accessJwt\":\"\(token)\",\"refreshJwt\":\"refresh-token\"}")
.utf8)
)
case "/xrpc/app.bsky.actor.getProfile":
return (
200,
Data("{\"did\":\"did:plc:reader\",\"handle\":\"reader.example\",\"displayName\":\"Reader\"}".utf8)
)
case "/xrpc/app.bsky.notification.listNotifications":
let response = try JSONSerialization.data(withJSONObject: ["notifications": Self.notifications])
return (200, response)
case "/xrpc/app.bsky.feed.getPosts":
return try posts(request)
default:
return (404, Data("{\"error\":\"NotFound\"}".utf8))
}
}
}