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
import Foundation
import XCTest
@testable import OldFella
extension ATProtoServiceTests {
func testNotificationPostHydrationRejectsFailedAndPartialBatches() async throws {
let fixture = try NotificationBatchFixture()
fixture.install()
defer { fixture.cleanUp() }
_ = try await fixture.service.signIn(
handle: "reader.example", appPassword: "dedicated-app-password", authFactorToken: nil)
try await assertNotificationBatchTimeout(fixture)
try await assertNotificationBatchFailure(fixture)
try await assertNotificationBatchRetry(fixture)
try await assertNotificationBatchFiltering(fixture)
try await assertNotificationBatchFullHydration(fixture)
try await fixture.service.signOut()
}
private func assertNotificationBatchTimeout(_ fixture: NotificationBatchFixture) async throws {
let service = fixture.service
StubURLProtocol.errorHandler = { $0.url?.path == "/xrpc/app.bsky.feed.getPosts" ? URLError(.timedOut) : nil }
do {
_ = try await service.loadNotifications(cursor: nil)
XCTFail("Expected the post request timeout")
} catch {
XCTAssertTrue(NetworkFailure.isConnectionFailure(error))
XCTAssertEqual(NetworkFailure.message(for: error), "The request timed out. Try again.")
}
let postRequest = try XCTUnwrap(
StubURLProtocol.recordedRequests.last { $0.url?.path == "/xrpc/app.bsky.feed.getPosts" })
XCTAssertEqual(postRequest.timeoutInterval, 30)
StubURLProtocol.errorHandler = nil
}
private func assertNotificationBatchFailure(_ fixture: NotificationBatchFixture) async throws {
let service = fixture.service
do {
_ = try await service.loadNotifications(cursor: nil)
XCTFail("Expected a failed batch to reject the page")
} catch ServiceError.notificationsUnavailable {}
XCTAssertFalse(fixture.batchSizes.isEmpty)
XCTAssertLessThanOrEqual(fixture.batchSizes.count, 3)
XCTAssertTrue(fixture.batchSizes.allSatisfy { (1...25).contains($0) })
}
private func assertNotificationBatchRetry(_ fixture: NotificationBatchFixture) async throws {
let service = fixture.service
fixture.failPostURI = nil
fixture.omitPostURI = NotificationBatchFixture.postURI(25)
fixture.omitOnlyFromBatch = true
fixture.batchSizes = []
let recovered = try await service.loadNotifications(cursor: nil)
XCTAssertEqual(fixture.batchSizes.sorted(), [1, 10, 25, 25])
XCTAssertEqual(recovered.notifications.count, 60)
XCTAssertEqual(
recovered.notifications.first { $0.postID == NotificationBatchFixture.postURI(25) }?.relatedPost?.id,
NotificationBatchFixture.postURI(25))
fixture.malformedPostURI = NotificationBatchFixture.postURI(25)
do {
_ = try await service.loadNotifications(cursor: nil)
XCTFail("Expected a malformed retry to reject the page")
} catch ServiceError.notificationsUnavailable {}
fixture.malformedPostURI = nil
}
private func assertNotificationBatchFiltering(_ fixture: NotificationBatchFixture) async throws {
let service = fixture.service
fixture.omitOnlyFromBatch = false
fixture.mutedPostURI = NotificationBatchFixture.postURI(26)
fixture.batchSizes = []
let filtered = try await service.loadNotifications(cursor: nil)
XCTAssertEqual(fixture.batchSizes.sorted(), [1, 10, 25, 25])
XCTAssertEqual(filtered.notifications.count, 58)
XCTAssertFalse(
filtered.notifications.contains {
$0.postID == NotificationBatchFixture.postURI(25) || $0.postID == NotificationBatchFixture.postURI(26)
})
XCTAssertTrue(filtered.notifications.allSatisfy { $0.relatedPost?.id == $0.postID })
}
private func assertNotificationBatchFullHydration(_ fixture: NotificationBatchFixture) async throws {
let service = fixture.service
fixture.omitPostURI = nil
fixture.mutedPostURI = nil
fixture.malformedPostURI = NotificationBatchFixture.postURI(25)
do {
_ = try await service.loadNotifications(cursor: nil)
XCTFail("Expected a malformed post to reject the page")
} catch ServiceError.notificationsUnavailable {}
fixture.malformedPostURI = nil
fixture.batchSizes = []
let fullyHydrated = try await service.loadNotifications(cursor: nil)
XCTAssertEqual(fixture.batchSizes.sorted(), [10, 25, 25])
XCTAssertEqual(fullyHydrated.notifications.count, 60)
XCTAssertTrue(fullyHydrated.notifications.allSatisfy { $0.relatedPost?.id == $0.postID })
}
}