2022-04-27 02:57:46 +00:00
|
|
|
//
|
|
|
|
// NotificationGroupTests.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 4/26/22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import Pachyderm
|
|
|
|
|
2023-05-08 20:58:50 +00:00
|
|
|
@MainActor
|
2022-04-27 02:57:46 +00:00
|
|
|
class NotificationGroupTests: XCTestCase {
|
|
|
|
|
|
|
|
let decoder: JSONDecoder = {
|
|
|
|
let d = JSONDecoder()
|
|
|
|
d.dateDecodingStrategy = .iso8601
|
|
|
|
return d
|
|
|
|
}()
|
|
|
|
|
|
|
|
let statusA = """
|
|
|
|
{
|
|
|
|
"id": "1",
|
|
|
|
"created_at": "2019-11-23T07:28:34Z",
|
|
|
|
"account": {
|
|
|
|
"id": "2",
|
|
|
|
"username": "bar",
|
|
|
|
"acct": "bar",
|
|
|
|
"display_name": "bar",
|
|
|
|
"locked": false,
|
|
|
|
"created_at": "2019-11-01T01:01:01Z",
|
|
|
|
"followers_count": 0,
|
|
|
|
"following_count": 0,
|
|
|
|
"statuses_count": 0,
|
|
|
|
"note": "",
|
|
|
|
"url": "https://example.com/@bar",
|
|
|
|
"uri": "https://example.com/@bar",
|
|
|
|
},
|
|
|
|
"url": "https://example.com/@bar/1",
|
|
|
|
"uri": "https://example.com/@bar/1",
|
|
|
|
"content": "",
|
|
|
|
"emojis": [],
|
|
|
|
"reblogs_count": 0,
|
|
|
|
"favourites_count": 0,
|
|
|
|
"sensitive": false,
|
|
|
|
"spoiler_text": "",
|
|
|
|
"visibility": "public",
|
|
|
|
"media_attachments": [],
|
|
|
|
"mentions": [],
|
|
|
|
"tags": [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
lazy var likeA1Data = """
|
|
|
|
{
|
|
|
|
"id": "1",
|
|
|
|
"type": "favourite",
|
|
|
|
"created_at": "2019-11-23T07:29:18Z",
|
|
|
|
"account": {
|
|
|
|
"id": "1",
|
|
|
|
"username": "foo",
|
|
|
|
"acct": "foo",
|
|
|
|
"display_name": "foo",
|
|
|
|
"locked": false,
|
|
|
|
"created_at": "2019-11-01T01:01:01Z",
|
|
|
|
"followers_count": 0,
|
|
|
|
"following_count": 0,
|
|
|
|
"statuses_count": 0,
|
|
|
|
"note": "",
|
|
|
|
"url": "https://example.com/@foo",
|
|
|
|
"uri": "https://example.com/@foo",
|
|
|
|
},
|
|
|
|
"status": \(statusA)
|
|
|
|
""".data(using: .utf8)!
|
|
|
|
lazy var likeA1 = try! decoder.decode(Notification.self, from: likeA1Data)
|
|
|
|
lazy var likeA2Data = """
|
|
|
|
{
|
|
|
|
"id": "2",
|
|
|
|
"type": "favourite",
|
|
|
|
"created_at": "2019-11-23T07:30:00Z",
|
|
|
|
"account": {
|
|
|
|
"id": "2",
|
|
|
|
"username": "baz",
|
|
|
|
"acct": "baz",
|
|
|
|
"display_name": "baz",
|
|
|
|
"locked": false,
|
|
|
|
"created_at": "2019-11-01T01:01:01Z",
|
|
|
|
"followers_count": 0,
|
|
|
|
"following_count": 0,
|
|
|
|
"statuses_count": 0,
|
|
|
|
"note": "",
|
|
|
|
"url": "https://example.com/@baz",
|
|
|
|
"uri": "https://example.com/@baz",
|
|
|
|
},
|
|
|
|
"status": \(statusA)
|
|
|
|
""".data(using: .utf8)!
|
|
|
|
lazy var likeA2 = try! decoder.decode(Notification.self, from: likeA2Data)
|
|
|
|
let statusB = """
|
|
|
|
{
|
|
|
|
"id": "2",
|
|
|
|
"created_at": "2019-11-23T07:28:34Z",
|
|
|
|
"account": {
|
|
|
|
"id": "2",
|
|
|
|
"username": "bar",
|
|
|
|
"acct": "bar",
|
|
|
|
"display_name": "bar",
|
|
|
|
"locked": false,
|
|
|
|
"created_at": "2019-11-01T01:01:01Z",
|
|
|
|
"followers_count": 0,
|
|
|
|
"following_count": 0,
|
|
|
|
"statuses_count": 0,
|
|
|
|
"note": "",
|
|
|
|
"url": "https://example.com/@bar",
|
|
|
|
"uri": "https://example.com/@bar",
|
|
|
|
},
|
|
|
|
"url": "https://example.com/@bar/1",
|
|
|
|
"uri": "https://example.com/@bar/1",
|
|
|
|
"content": "",
|
|
|
|
"emojis": [],
|
|
|
|
"reblogs_count": 0,
|
|
|
|
"favourites_count": 0,
|
|
|
|
"sensitive": false,
|
|
|
|
"spoiler_text": "",
|
|
|
|
"visibility": "public",
|
|
|
|
"media_attachments": [],
|
|
|
|
"mentions": [],
|
|
|
|
"tags": [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
lazy var likeBData = """
|
|
|
|
{
|
|
|
|
"id": "3",
|
|
|
|
"type": "favourite",
|
|
|
|
"created_at": "2019-11-23T07:29:18Z",
|
|
|
|
"account": {
|
|
|
|
"id": "1",
|
|
|
|
"username": "foo",
|
|
|
|
"acct": "foo",
|
|
|
|
"display_name": "foo",
|
|
|
|
"locked": false,
|
|
|
|
"created_at": "2019-11-01T01:01:01Z",
|
|
|
|
"followers_count": 0,
|
|
|
|
"following_count": 0,
|
|
|
|
"statuses_count": 0,
|
|
|
|
"note": "",
|
|
|
|
"url": "https://example.com/@foo",
|
|
|
|
"uri": "https://example.com/@foo",
|
|
|
|
},
|
|
|
|
"status": \(statusB)
|
|
|
|
""".data(using: .utf8)!
|
|
|
|
lazy var likeB = try! decoder.decode(Notification.self, from: likeBData)
|
2022-05-10 00:11:15 +00:00
|
|
|
lazy var likeB2Data = """
|
2022-04-27 02:57:46 +00:00
|
|
|
{
|
|
|
|
"id": "4",
|
2022-05-10 00:11:15 +00:00
|
|
|
"type": "favourite",
|
|
|
|
"created_at": "2019-11-23T07:29:18Z",
|
|
|
|
"account": {
|
|
|
|
"id": "2",
|
|
|
|
"username": "bar",
|
|
|
|
"acct": "bar",
|
|
|
|
"display_name": "bar",
|
|
|
|
"locked": false,
|
|
|
|
"created_at": "2019-11-02T01:01:01Z",
|
|
|
|
"followers_count": 0,
|
|
|
|
"following_count": 0,
|
|
|
|
"statuses_count": 0,
|
|
|
|
"note": "",
|
|
|
|
"url": "https://example.com/@bar",
|
|
|
|
"uri": "https://example.com/@bar",
|
|
|
|
},
|
|
|
|
"status": \(statusB)
|
|
|
|
""".data(using: .utf8)!
|
|
|
|
lazy var likeB2 = try! decoder.decode(Notification.self, from: likeB2Data)
|
|
|
|
lazy var mentionBData = """
|
|
|
|
{
|
|
|
|
"id": "5",
|
2022-04-27 02:57:46 +00:00
|
|
|
"type": "mention",
|
|
|
|
"created_at": "2019-11-23T07:29:18Z",
|
|
|
|
"account": {
|
|
|
|
"id": "1",
|
|
|
|
"username": "foo",
|
|
|
|
"acct": "foo",
|
|
|
|
"display_name": "foo",
|
|
|
|
"locked": false,
|
|
|
|
"created_at": "2019-11-01T01:01:01Z",
|
|
|
|
"followers_count": 0,
|
|
|
|
"following_count": 0,
|
|
|
|
"statuses_count": 0,
|
|
|
|
"note": "",
|
|
|
|
"url": "https://example.com/@foo",
|
|
|
|
"uri": "https://example.com/@foo",
|
|
|
|
},
|
|
|
|
"status": \(statusB)
|
|
|
|
""".data(using: .utf8)!
|
|
|
|
lazy var mentionB = try! decoder.decode(Notification.self, from: mentionBData)
|
|
|
|
|
|
|
|
|
|
|
|
func testGroupSimple() {
|
|
|
|
let groups = NotificationGroup.createGroups(notifications: [likeA1, likeA2], only: [.favourite])
|
2024-12-15 18:52:26 +00:00
|
|
|
XCTAssertEqual(groups, [NotificationGroup(notifications: [likeA1, likeA2], kind: .favourite)!])
|
2022-04-27 02:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testGroupWithOtherGroupableInBetween() {
|
|
|
|
let groups = NotificationGroup.createGroups(notifications: [likeA1, likeB, likeA2], only: [.favourite])
|
|
|
|
XCTAssertEqual(groups, [
|
2024-12-15 18:52:26 +00:00
|
|
|
NotificationGroup(notifications: [likeA1, likeA2], kind: .favourite)!,
|
|
|
|
NotificationGroup(notifications: [likeB], kind: .favourite)!,
|
2022-04-27 02:57:46 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDontGroupWithUngroupableInBetween() {
|
|
|
|
let groups = NotificationGroup.createGroups(notifications: [likeA1, mentionB, likeA2], only: [.favourite])
|
|
|
|
XCTAssertEqual(groups, [
|
2024-12-15 18:52:26 +00:00
|
|
|
NotificationGroup(notifications: [likeA1], kind: .favourite)!,
|
|
|
|
NotificationGroup(notifications: [mentionB], kind: .mention)!,
|
|
|
|
NotificationGroup(notifications: [likeA2], kind: .favourite)!,
|
2022-04-27 02:57:46 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMergeSimpleGroups() {
|
2024-12-15 18:52:26 +00:00
|
|
|
let group1 = NotificationGroup(notifications: [likeA1], kind: .favourite)!
|
|
|
|
let group2 = NotificationGroup(notifications: [likeA2], kind: .favourite)!
|
2022-04-27 02:57:46 +00:00
|
|
|
let merged = NotificationGroup.mergeGroups(first: [group1], second: [group2], only: [.favourite])
|
|
|
|
XCTAssertEqual(merged, [
|
2024-12-15 18:52:26 +00:00
|
|
|
NotificationGroup(notifications: [likeA1, likeA2], kind: .favourite)!
|
2022-04-27 02:57:46 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMergeGroupsWithOtherGroupableInBetween() {
|
2024-12-15 18:52:26 +00:00
|
|
|
let group1 = NotificationGroup(notifications: [likeA1], kind: .favourite)!
|
|
|
|
let group2 = NotificationGroup(notifications: [likeB], kind: .favourite)!
|
|
|
|
let group3 = NotificationGroup(notifications: [likeA2], kind: .favourite)!
|
2022-04-27 02:57:46 +00:00
|
|
|
let merged = NotificationGroup.mergeGroups(first: [group1, group2], second: [group3], only: [.favourite])
|
|
|
|
XCTAssertEqual(merged, [
|
2024-12-15 18:52:26 +00:00
|
|
|
NotificationGroup(notifications: [likeA1, likeA2], kind: .favourite)!,
|
|
|
|
NotificationGroup(notifications: [likeB], kind: .favourite)!,
|
2022-04-27 02:57:46 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
let merged2 = NotificationGroup.mergeGroups(first: [group1], second: [group2, group3], only: [.favourite])
|
|
|
|
XCTAssertEqual(merged2, [
|
2024-12-15 18:52:26 +00:00
|
|
|
NotificationGroup(notifications: [likeA1, likeA2], kind: .favourite)!,
|
|
|
|
NotificationGroup(notifications: [likeB], kind: .favourite)!,
|
2022-04-27 02:57:46 +00:00
|
|
|
])
|
2022-05-10 00:11:15 +00:00
|
|
|
|
2024-12-15 18:52:26 +00:00
|
|
|
let group4 = NotificationGroup(notifications: [likeB2], kind: .favourite)!
|
|
|
|
let group5 = NotificationGroup(notifications: [mentionB], kind: .mention)!
|
2022-05-10 00:11:15 +00:00
|
|
|
let merged3 = NotificationGroup.mergeGroups(first: [group1, group5, group2], second: [group4, group3], only: [.favourite])
|
|
|
|
print(merged3.count)
|
|
|
|
XCTAssertEqual(merged3, [
|
|
|
|
group1,
|
|
|
|
group5,
|
2024-12-15 18:52:26 +00:00
|
|
|
NotificationGroup(notifications: [likeB, likeB2], kind: .favourite),
|
2022-05-10 00:11:15 +00:00
|
|
|
group3
|
|
|
|
])
|
2022-04-27 02:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testDontMergeWithUngroupableInBetween() {
|
2024-12-15 18:52:26 +00:00
|
|
|
let group1 = NotificationGroup(notifications: [likeA1], kind: .favourite)!
|
|
|
|
let group2 = NotificationGroup(notifications: [mentionB], kind: .mention)!
|
|
|
|
let group3 = NotificationGroup(notifications: [likeA2], kind: .favourite)!
|
2022-04-27 02:57:46 +00:00
|
|
|
let merged = NotificationGroup.mergeGroups(first: [group1, group2], second: [group3], only: [.favourite])
|
|
|
|
XCTAssertEqual(merged, [
|
2024-12-15 18:52:26 +00:00
|
|
|
NotificationGroup(notifications: [likeA1], kind: .favourite)!,
|
|
|
|
NotificationGroup(notifications: [mentionB], kind: .mention)!,
|
|
|
|
NotificationGroup(notifications: [likeA2], kind: .favourite)!,
|
2022-04-27 02:57:46 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|