import Foundation import Observation import UIKit import XCTest @testable import OldFella extension NotificationLoadingTests { func testFeedPaginationPublishesChangedCollectionsOnce() async { let service = NotificationLoadingService() let author = Post.Author(id: "other", name: "Other", handle: "other") func post(_ id: String, likes: Int = 0) -> Post { Post( id: id, author: author, text: id, likeCount: likes, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) } let first = post("first") let context = post("context") service.feedPages["initial"] = .success( FeedPage(posts: [first], cursor: "next", replyContexts: [first.id: [context]])) service.feedPages["next"] = .failure(ServiceError.invalidResponse) seedPaginationNotification(service, author: author, post: first) let model = AppModel(service: service) await model.signInWithDemo() await model.loadNotifications() let changed = post(first.id, likes: 3) let second = post("second") let third = post("third") service.feedPages["next"] = .success( FeedPage( posts: [changed, second, third, second], cursor: "duplicate", replyContexts: [first.id: [context], second.id: [post("new-context")]])) service.feedPages["duplicate"] = .failure(ServiceError.invalidResponse) let posts = ObservationProbe { model.posts } let replies = ObservationProbe { model.replyContexts } let notifications = ObservationProbe { model.notifications } let preparations = model.contentPreparationCount await model.loadNextPage() await Task.yield() XCTAssertEqual(posts.values.count, 1) XCTAssertEqual(replies.values.count, 1) XCTAssertEqual(notifications.values.count, 1) XCTAssertEqual(posts.values.first?.map(\.id), [first.id, second.id, third.id]) XCTAssertEqual(replies.values.first?[second.id]?.map(\.id), ["new-context"]) XCTAssertEqual(notifications.values.first?.first?.relatedPost?.likeCount, 3) XCTAssertEqual(model.posts.map(\.id), [first.id, second.id, third.id]) XCTAssertEqual(model.timelinePosts.map(\.id), [first.id, second.id, third.id]) XCTAssertEqual(model.visibleNotifications.first?.relatedPost?.likeCount, 3) XCTAssertEqual(model.contentPreparationCount, preparations + 1) let publicationCounts = [posts.values.count, replies.values.count, notifications.values.count] let evaluations = model.moderationEvaluationCount let prepared = model.contentPreparationCount service.feedPages["duplicate"] = .success( FeedPage(posts: [changed, second, third], cursor: nil, replyContexts: model.replyContexts)) await model.loadNextPage() await Task.yield() XCTAssertEqual([posts.values.count, replies.values.count, notifications.values.count], publicationCounts) XCTAssertEqual(model.contentPreparationCount, prepared) XCTAssertEqual(model.moderationEvaluationCount, evaluations) } func testManualPagesKeepRelatedNotificationsGroupedWithinEachPage() async { let service = NotificationLoadingService() service.pages = [ "initial": .success( .init(notifications: [notification("a", reason: .like, postID: "post")], cursor: "one")), "one": .success( .init( notifications: [ notification("b", reason: .like, postID: "post"), notification("c", reason: .like, postID: "post") ], cursor: "two")), "two": .success(.init(notifications: [notification("last")], cursor: nil)) ] let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() XCTAssertEqual(service.requests, ["initial"]) XCTAssertEqual(model.notificationGroups.first?.notifications.map(\.id), ["a"]) await model.loadNextNotificationsPage() XCTAssertEqual(service.requests, ["initial", "one"]) XCTAssertEqual(model.notificationGroups.map { $0.notifications.map(\.id) }, [["a"], ["b", "c"]]) await model.loadNextNotificationsPage() XCTAssertEqual(service.requests, ["initial", "one", "two"]) } func testFilteredNotificationsDoNotCountTowardPageSize() async { let service = NotificationLoadingService() service.mutedWords = [.init(value: "hidden")] let author = Post.Author(id: "author", name: "Author", handle: "author") let post = Post( id: "post", author: author, text: "hidden", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) let hidden = FeedNotification( id: "hidden", author: author, reason: .mention, indexedAt: Date(), isRead: false, postID: post.id, relatedPost: post) service.pages = [ "initial": .success(.init(notifications: [hidden], cursor: "one")), "one": .success(.init(notifications: [notification("last")], cursor: nil)) ] let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() XCTAssertEqual(model.visibleNotifications.map(\.id), ["last"]) XCTAssertEqual(service.requests, ["initial", "one"]) } func testEmptyDuplicatePagesAndCursorCycleStop() async { let service = NotificationLoadingService() let item = notification("a") service.pages = [ "initial": .success(.init(notifications: [item, item], cursor: "one")), "one": .success(.init(notifications: [], cursor: "two")), "two": .success(.init(notifications: [item], cursor: "one")) ] let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() XCTAssertEqual(model.notifications.map(\.id), ["a"]) XCTAssertEqual(service.requests, ["initial"]) await model.loadNextNotificationsPage() XCTAssertEqual(service.requests, ["initial", "one", "two"]) XCTAssertFalse(model.hasMoreNotifications) } func testPartialFailureRetriesFromFailedCursor() async { let service = NotificationLoadingService() service.pages = [ "initial": .success(.init(notifications: [notification("a")], cursor: "one")), "one": .failure(ServiceError.invalidResponse) ] let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() XCTAssertEqual(model.notifications.map(\.id), ["a"]) XCTAssertFalse(model.retryNotificationsPage) await model.loadNextNotificationsPage() XCTAssertTrue(model.retryNotificationsPage) service.pages["one"] = .success(.init(notifications: [notification("b")], cursor: nil)) await model.loadNextNotificationsPage() XCTAssertEqual(model.notifications.map(\.id), ["a", "b"]) XCTAssertEqual(service.requests, ["initial", "one", "one"]) } func testTenEmptyRefreshPagesPreserveCursorForLoadMore() async { let service = NotificationLoadingService() service.pages["initial"] = .success(.init(notifications: [], cursor: "cursor-1")) for page in 1..<10 { service.pages["cursor-\(page)"] = .success(.init(notifications: [], cursor: "cursor-\(page + 1)")) } service.pages["cursor-10"] = .success(.init(notifications: [notification("visible")], cursor: nil)) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() XCTAssertTrue(model.notifications.isEmpty) XCTAssertTrue(model.hasMoreNotifications) XCTAssertEqual(service.requests, ["initial"] + (1...9).map { "cursor-\($0)" }) await model.loadNextNotificationsPage() XCTAssertEqual(model.notifications.map(\.id), ["visible"]) XCTAssertFalse(model.hasMoreNotifications) XCTAssertEqual(service.requests, ["initial"] + (1...10).map { "cursor-\($0)" }) } func testRefreshPublishesBeforeLaterPageFailureAndRetriesFailedCursor() async { let service = NotificationLoadingService() service.pages["initial"] = .success(.init(notifications: [notification("old")], cursor: nil)) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() service.pages["initial"] = .success(.init(notifications: [notification("new")], cursor: "refresh-next")) service.pages["refresh-next"] = .failure(ServiceError.invalidResponse) let accepted = await model.loadNotifications() XCTAssertTrue(accepted) XCTAssertEqual(model.notifications.map(\.id), ["new"]) XCTAssertFalse(model.retryNotificationsPage) await model.loadNextNotificationsPage() XCTAssertEqual(model.notifications.map(\.id), ["new"]) XCTAssertTrue(model.retryNotificationsPage) XCTAssertTrue(model.hasMoreNotifications) service.pages["refresh-next"] = .success(.init(notifications: [notification("later")], cursor: nil)) await model.loadNextNotificationsPage() XCTAssertEqual(model.notifications.map(\.id), ["new", "later"]) XCTAssertEqual(service.requests, ["initial", "initial", "refresh-next", "refresh-next"]) } func testCancellationOnSecondRefreshPageKeepsOldRows() async { let service = NotificationLoadingService() service.pages["initial"] = .success(.init(notifications: [notification("old")], cursor: nil)) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() service.pages["initial"] = .success(.init(notifications: [], cursor: "second")) service.suspendRequest(for: "second") let refresh = Task { await model.loadNotifications() } await service.waitUntilSuspendedRequest() refresh.cancel() service.resumeSuspendedRequest(with: .failure(CancellationError())) let accepted = await refresh.value XCTAssertFalse(accepted) XCTAssertEqual(model.notifications.map(\.id), ["old"]) XCTAssertNil(model.notificationsErrorMessage) XCTAssertEqual(service.requests, ["initial", "initial", "second"]) } private func seedPaginationNotification( _ service: NotificationLoadingService, author: Post.Author, post: Post ) { service.pages["initial"] = .success( NotificationPage( notifications: [ FeedNotification( id: "notification", author: author, reason: .like, indexedAt: Date(), isRead: false, postID: post.id, relatedPost: post) ], cursor: nil)) } }