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
334
335
336
337
338
339
340
341
342
343
344
345
@testable import Grain
import XCTest
/// Grouping is what keeps the notifications tab from being twenty identical
/// "someone favorited your gallery" rows. It collapses on three axes at once —
/// same reason, same subject, within two days — and only for the reasons that
/// read sensibly in the plural.
@MainActor
final class NotificationGroupingTests: GrainTestCase {
// MARK: - Builders
private func profile(_ id: String) -> GrainProfile {
GrainProfile(cid: "bafy\(id)", did: "did:plc:\(id)", handle: "\(id).test", displayName: id.capitalized)
}
/// `GroupedNotification` parses with fractional seconds, so timestamps have
/// to carry them or every notification lands on the same instant.
private func timestamp(hoursAfterEpoch hours: Double) -> String {
let date = Date(timeIntervalSince1970: hours * 3600)
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return formatter.string(from: date)
}
private func notification(
_ id: String,
reason: String,
author: String,
gallery: String? = "at://did:plc:me/social.grain.gallery/1",
story: String? = nil,
hours: Double = 0
) -> GrainNotification {
GrainNotification(
uri: "at://did:plc:me/notification/\(id)",
reason: reason,
createdAt: timestamp(hoursAfterEpoch: hours),
author: profile(author),
galleryUri: gallery,
storyUri: story
)
}
// MARK: - Reason parsing
func testEveryWireReasonMapsToACase() {
let pairs: [(String, NotificationReason)] = [
("gallery-favorite", .galleryFavorite),
("gallery-comment", .galleryComment),
("gallery-comment-mention", .galleryCommentMention),
("gallery-mention", .galleryMention),
("comment-favorite", .commentFavorite),
("story-favorite", .storyFavorite),
("story-comment", .storyComment),
("reply", .reply),
("follow", .follow),
]
for (wire, expected) in pairs {
XCTAssertEqual(notification("1", reason: wire, author: "a").reasonType, expected)
}
}
/// A reason the server adds later must not crash the tab.
func testAnUnrecognisedReasonFallsBackToUnknown() {
XCTAssertEqual(notification("1", reason: "gallery-reposted", author: "a").reasonType, .unknown)
XCTAssertFalse(NotificationReason.unknown.isGroupable)
}
func testOnlyTheCountableReasonsGroup() {
for reason in [NotificationReason.galleryFavorite, .storyFavorite, .commentFavorite, .follow] {
XCTAssertTrue(reason.isGroupable, "\(reason) reads as a count and should group")
}
for reason in [NotificationReason.galleryComment, .reply, .galleryMention, .galleryCommentMention, .storyComment] {
XCTAssertFalse(reason.isGroupable, "\(reason) carries its own text and must stay on its own row")
}
}
// MARK: - group
func testFavoritesOfTheSameGalleryCollapseIntoOneRow() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice"),
notification("2", reason: "gallery-favorite", author: "bob"),
notification("3", reason: "gallery-favorite", author: "carol"),
])
XCTAssertEqual(groups.count, 1)
XCTAssertTrue(groups[0].isGrouped)
XCTAssertEqual(groups[0].authorCount, 3)
XCTAssertEqual(groups[0].allAuthors.map(\.did), ["did:plc:alice", "did:plc:bob", "did:plc:carol"])
}
/// The row is identified by the notification it was seeded from, so the
/// first one has to stay the head.
func testTheGroupKeepsTheFirstNotificationAsItsHead() {
let head = notification("1", reason: "gallery-favorite", author: "alice")
let groups = GroupedNotification.group([
head,
notification("2", reason: "gallery-favorite", author: "bob"),
])
XCTAssertEqual(groups[0].notification.uri, head.uri)
XCTAssertEqual(groups[0].id, head.uri)
}
func testFavoritesOfDifferentGalleriesStayApart() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice", gallery: "at://g/1"),
notification("2", reason: "gallery-favorite", author: "bob", gallery: "at://g/2"),
])
XCTAssertEqual(groups.count, 2)
XCTAssertFalse(groups[0].isGrouped)
}
func testAFavoriteAndACommentOnTheSameGalleryStayApart() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice"),
notification("2", reason: "gallery-comment", author: "bob"),
])
XCTAssertEqual(groups.count, 2)
}
/// Two comments read as two separate things to go and read, so they never
/// collapse even when everything else matches.
func testCommentsOnTheSameGalleryEachKeepTheirOwnRow() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-comment", author: "alice"),
notification("2", reason: "gallery-comment", author: "bob"),
])
XCTAssertEqual(groups.count, 2)
XCTAssertTrue(groups.allSatisfy { !$0.isGrouped })
}
/// Someone appearing twice in one group's facepile would read as two
/// different people.
func testAPersonIsNeverCountedTwiceWithinAGroup() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice"),
notification("2", reason: "gallery-favorite", author: "bob"),
notification("3", reason: "gallery-favorite", author: "bob"),
])
XCTAssertEqual(groups.count, 1)
XCTAssertEqual(groups[0].authorCount, 2)
XCTAssertEqual(groups[0].allAuthors.map(\.did), ["did:plc:alice", "did:plc:bob"])
}
/// An unfavorite followed by a refavorite comes back as two notifications
/// from the same person. The second is absorbed into the row the first
/// seeded rather than opening a duplicate one beside it.
func testARepeatFromTheGroupsOwnAuthorIsAbsorbed() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice"),
notification("2", reason: "gallery-favorite", author: "alice"),
])
XCTAssertEqual(groups.count, 1)
XCTAssertEqual(groups[0].authorCount, 1)
XCTAssertFalse(groups[0].isGrouped)
}
/// The same, arriving on a later page rather than in the first batch.
func testARepeatFromTheGroupsOwnAuthorOnALaterPageIsAbsorbed() {
var groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice"),
])
GroupedNotification.mergeNewPage([
notification("2", reason: "gallery-favorite", author: "alice"),
], into: &groups)
XCTAssertEqual(groups.count, 1)
XCTAssertEqual(groups[0].authorCount, 1)
}
/// A repeat must not cost the row the other people already on it.
func testARepeatDoesNotDisturbTheRestOfTheFacepile() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice"),
notification("2", reason: "gallery-favorite", author: "bob"),
notification("3", reason: "gallery-favorite", author: "alice"),
notification("4", reason: "gallery-favorite", author: "carol"),
])
XCTAssertEqual(groups.count, 1)
XCTAssertEqual(groups[0].allAuthors.map(\.did), ["did:plc:alice", "did:plc:bob", "did:plc:carol"])
}
/// Follows have no subject, so they collapse across the board — but still
/// only inside the same two-day window.
func testFollowsGroupWithoutASubject() {
let groups = GroupedNotification.group([
notification("1", reason: "follow", author: "alice", gallery: nil),
notification("2", reason: "follow", author: "bob", gallery: nil),
])
XCTAssertEqual(groups.count, 1)
XCTAssertEqual(groups[0].authorCount, 2)
}
func testFavoritesMoreThanTwoDaysApartStayApart() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice", hours: 0),
notification("2", reason: "gallery-favorite", author: "bob", hours: 49),
])
XCTAssertEqual(groups.count, 2)
}
func testFavoritesJustInsideTwoDaysStillGroup() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice", hours: 0),
notification("2", reason: "gallery-favorite", author: "bob", hours: 47),
])
XCTAssertEqual(groups.count, 1)
}
func testStoryFavoritesGroupOnTheStoryTheyBelongTo() {
let groups = GroupedNotification.group([
notification("1", reason: "story-favorite", author: "alice", gallery: nil, story: "at://s/1"),
notification("2", reason: "story-favorite", author: "bob", gallery: nil, story: "at://s/1"),
notification("3", reason: "story-favorite", author: "carol", gallery: nil, story: "at://s/2"),
])
XCTAssertEqual(groups.count, 2)
XCTAssertEqual(groups[0].authorCount, 2)
XCTAssertEqual(groups[1].authorCount, 1)
}
func testGroupingAnEmptyListProducesNothing() {
XCTAssertTrue(GroupedNotification.group([]).isEmpty)
}
func testUngroupableNotificationsKeepTheirOriginalOrder() {
let groups = GroupedNotification.group([
notification("1", reason: "gallery-comment", author: "alice"),
notification("2", reason: "reply", author: "bob"),
notification("3", reason: "gallery-mention", author: "carol"),
])
XCTAssertEqual(groups.map { String($0.notification.uri.suffix(1)) }, ["1", "2", "3"])
}
// MARK: - mergeNewPage
/// The whole point of merging rather than regrouping is that page two's
/// favorites join the row page one already built.
func testASecondPageFoldsIntoTheGroupTheFirstPageBuilt() {
var groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice"),
])
GroupedNotification.mergeNewPage([
notification("2", reason: "gallery-favorite", author: "bob"),
], into: &groups)
XCTAssertEqual(groups.count, 1)
XCTAssertEqual(groups[0].authorCount, 2)
XCTAssertEqual(
String(groups[0].notification.uri.suffix(1)), "1",
"The head must stay the notification page one showed"
)
}
func testASecondPageAppendsWhenItMatchesNothing() {
var groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice", gallery: "at://g/1"),
])
GroupedNotification.mergeNewPage([
notification("2", reason: "gallery-favorite", author: "bob", gallery: "at://g/2"),
notification("3", reason: "gallery-comment", author: "carol"),
], into: &groups)
XCTAssertEqual(groups.count, 3)
}
func testMergingSomeoneAlreadyInTheGroupDoesNotCountThemTwice() {
var groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice"),
notification("2", reason: "gallery-favorite", author: "bob"),
])
GroupedNotification.mergeNewPage([
notification("3", reason: "gallery-favorite", author: "bob"),
], into: &groups)
XCTAssertEqual(groups.count, 1)
XCTAssertEqual(groups[0].authorCount, 2)
}
func testMergingAnEmptyPageChangesNothing() {
var groups = GroupedNotification.group([
notification("1", reason: "gallery-favorite", author: "alice"),
])
GroupedNotification.mergeNewPage([], into: &groups)
XCTAssertEqual(groups.count, 1)
XCTAssertEqual(groups[0].authorCount, 1)
}
// MARK: - Identity
/// Rows are diffed by URI, and the author count is part of equality so a
/// group that gains a face actually redraws.
func testTwoGroupsAreEqualOnlyWhenTheHeadAndTheCountMatch() {
let base = GroupedNotification(
notification: notification("1", reason: "gallery-favorite", author: "alice"),
additional: []
)
let same = GroupedNotification(
notification: notification("1", reason: "gallery-favorite", author: "alice"),
additional: []
)
let grown = GroupedNotification(
notification: notification("1", reason: "gallery-favorite", author: "alice"),
additional: [notification("2", reason: "gallery-favorite", author: "bob")]
)
let other = GroupedNotification(
notification: notification("9", reason: "gallery-favorite", author: "alice"),
additional: []
)
XCTAssertEqual(base, same)
XCTAssertNotEqual(base, grown)
XCTAssertNotEqual(base, other)
XCTAssertEqual(base.hashValue, same.hashValue)
}
func testTheHeadAuthorIsNotRepeatedInTheFacepile() {
let group = GroupedNotification(
notification: notification("1", reason: "gallery-favorite", author: "alice"),
additional: [
notification("2", reason: "gallery-favorite", author: "alice"),
notification("3", reason: "gallery-favorite", author: "bob"),
]
)
XCTAssertEqual(group.allAuthors.map(\.did), ["did:plc:alice", "did:plc:bob"])
}
}