import Foundation import Observation import UIKit import XCTest @testable import OldFella @MainActor final class NotificationLoadingTests: XCTestCase { func notification(_ id: String, reason: FeedNotification.Reason = .follow, postID: String? = nil) -> FeedNotification { FeedNotification( id: id, author: Post.Author(id: id, name: id, handle: id), reason: reason, indexedAt: Date(), isRead: false, postID: postID) } func testNotificationFiltersCombineMutualsAndTypes() { var mutualLike = notification("mutual", reason: .like) mutualLike.isFollowedBy = true let otherLike = notification("other", reason: .like) var mutualReply = notification("reply", reason: .reply) mutualReply.isFollowedBy = true let filter = NotificationFilter(mutualsOnly: true, excludedTypesRaw: "reply,unknown-value") XCTAssertTrue(filter.includes(mutualLike, follows: true)) XCTAssertFalse(filter.includes(mutualLike, follows: false)) XCTAssertFalse(filter.includes(otherLike, follows: true)) XCTAssertFalse(filter.includes(mutualReply, follows: true)) XCTAssertEqual(filter.excludedTypes, [.reply]) XCTAssertTrue(filter.isActive) XCTAssertFalse(NotificationFilter(mutualsOnly: false, excludedTypesRaw: "").isActive) } func testNotificationTypeSelectionKeepsAtLeastOneType() { let all = NotificationFilter(mutualsOnly: false, excludedTypesRaw: "") XCTAssertTrue(all.showsAllTypes) XCTAssertFalse(all.typeIsSelected(.like)) let one = NotificationFilter(mutualsOnly: false, excludedTypesRaw: all.settingType(.like, enabled: true)) XCTAssertFalse(one.showsAllTypes) XCTAssertTrue(one.typeIsSelected(.like)) XCTAssertFalse(one.typeIsSelected(.reply)) XCTAssertEqual( one.settingType(.like, enabled: false), one.excludedTypes.map(\.rawValue).sorted().joined(separator: ",")) let two = NotificationFilter(mutualsOnly: false, excludedTypesRaw: one.settingType(.reply, enabled: true)) XCTAssertTrue(two.typeIsSelected(.like)) XCTAssertTrue(two.typeIsSelected(.reply)) let emptySavedSelection = FeedNotification.Reason.allCases.map(\.rawValue).joined(separator: ",") XCTAssertTrue(NotificationFilter(mutualsOnly: false, excludedTypesRaw: emptySavedSelection).showsAllTypes) } func testSharedRefreshDrivesNativeControlWithoutFiringManualRefresh() { let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844)) let container = UIView(frame: window.bounds) window.addSubview(container) let scroll = UIScrollView(frame: container.bounds) scroll.contentSize = CGSize(width: 390, height: 2000) let control = UIRefreshControl() scroll.refreshControl = control container.addSubview(scroll) let probe = ScrollRefreshControl.Probe(frame: container.bounds) container.addSubview(probe) window.isHidden = false defer { window.isHidden = true } probe.isActive = true probe.isRefreshing = true probe.synchronize() XCTAssertTrue(control.isRefreshing) XCTAssertLessThan(scroll.contentOffset.y, 0) probe.isRefreshing = false probe.synchronize() XCTAssertFalse(control.isRefreshing) } func testSharedRefreshDoesNotMoveOlderNotificationsOrEndManualRefresh() { let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844)) let container = UIView(frame: window.bounds) window.addSubview(container) let scroll = UIScrollView(frame: container.bounds) scroll.contentSize = CGSize(width: 390, height: 2000) let control = UIRefreshControl() scroll.refreshControl = control container.addSubview(scroll) let probe = ScrollRefreshControl.Probe(frame: container.bounds) container.addSubview(probe) window.isHidden = false defer { window.isHidden = true } scroll.contentOffset.y = 300 probe.isActive = true probe.isRefreshing = true probe.synchronize() XCTAssertEqual(scroll.contentOffset.y, 300) probe.finish() control.beginRefreshing() probe.synchronize() probe.finish() XCTAssertTrue(control.isRefreshing) control.endRefreshing() } func testFeedRefreshDoesNotWaitForNotifications() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.feedPages["initial"] = .success(.init(posts: [], cursor: nil)) let feedRequests = service.feedRequests service.suspendRequest(for: "initial") let refresh = Task { await model.refreshFeed() } await service.waitUntilSuspendedRequest() await refresh.value XCTAssertFalse(model.isRefreshingFeed) XCTAssertEqual(service.requests, ["initial"]) XCTAssertEqual(service.feedRequests, feedRequests + 1) service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("new")], cursor: nil))) for _ in 0..<100 where !model.hasUnreadNotifications { await Task.yield() } XCTAssertTrue(model.hasUnreadNotifications) } func testRefreshPublishesOnePageAndManualPaginationRetriesFailure() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let author = Post.Author(id: "a", name: "A", handle: "a") func post(_ id: String) -> Post { Post( id: id, author: author, text: id, likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) } service.feedPages["initial"] = .success(.init(posts: [post("one")], cursor: "second")) service.feedPages["second"] = .success(.init(posts: [post("two")], cursor: "third")) service.feedPages["third"] = .failure(ServiceError.invalidResponse) await model.refreshFeed() XCTAssertFalse(model.isRefreshingFeed) XCTAssertEqual(model.timelineRowIDs, ["one"]) XCTAssertEqual(service.feedCursors.last, "initial") await model.loadNextPage() XCTAssertEqual(model.timelineRowIDs, ["one", "two"]) XCTAssertNil(model.feedErrorMessage) await model.loadNextPage() XCTAssertTrue(model.retryFeedPage) XCTAssertEqual(model.timelineRowIDs, ["one", "two"]) service.feedPages["third"] = .success(.init(posts: [post("three")], cursor: nil)) await model.loadNextPage() XCTAssertEqual(model.timelineRowIDs, ["one", "two", "three"]) XCTAssertEqual(service.feedCursors.suffix(4), ["initial", "second", "third", "third"]) } func testRefreshDoesNotPrefetchOlderPagesBeforeAnotherRefresh() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let author = Post.Author(id: "a", name: "A", handle: "a") func post(_ id: String) -> Post { Post( id: id, author: author, text: id, likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) } service.feedPages["initial"] = .success(.init(posts: [post("one")], cursor: "second")) service.suspendFeedRequest(for: "second") let before = service.feedRequests await model.refreshFeed() XCTAssertEqual(model.timelineRowIDs, ["one"]) XCTAssertEqual(service.feedRequests, before + 1) service.feedPages["initial"] = .success(.init(posts: [post("fresh")], cursor: nil)) await model.refreshFeed() XCTAssertEqual(model.timelineRowIDs, ["fresh"]) XCTAssertEqual(service.feedRequests, before + 2) } func testRefreshDoesNotRepublishUnchangedPageOrRequestContinuation() async { let service = NotificationLoadingService() let author = Post.Author(id: "a", name: "A", handle: "a") let posts = (0..<15).map { Post( id: "p\($0)", author: author, text: "safe", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) } service.feedPages["initial"] = .success(.init(posts: posts, cursor: "second")) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let changes = ObservationProbe { model.posts } service.suspendFeedRequest(for: "second") await model.refreshFeed() await Task.yield() XCTAssertEqual(model.posts, posts) XCTAssertTrue(changes.values.isEmpty) XCTAssertEqual(service.feedCursors, ["initial", "initial"]) XCTAssertTrue(model.hasMorePosts) } func testRefreshStopsAfterFivePagesAndLeavesLoadMoreManual() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.feedPages["initial"] = .success(.init(posts: [], cursor: "1")) for page in 1...5 { service.feedPages["\(page)"] = .success(.init(posts: [], cursor: "\(page + 1)")) } await model.refreshFeed() for _ in 0..<100 where service.feedCursors.suffix(5) != ["initial", "1", "2", "3", "4"] { await Task.yield() } XCTAssertEqual(service.feedCursors.suffix(5), ["initial", "1", "2", "3", "4"]) XCTAssertTrue(model.hasMorePosts) await model.loadNextPage() XCTAssertTrue(service.feedCursors.contains("5")) } func testSignOutRejectsPendingEmptyPageFallbackResponse() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.feedPages["initial"] = .success(.init(posts: [], cursor: "second")) service.feedPages["second"] = .success(.init(posts: [], cursor: "third")) service.suspendFeedRequest(for: "third") let refresh = Task { await model.refreshFeed() } await service.waitUntilSuspendedFeedRequest() await model.signOut() let author = Post.Author(id: "a", name: "A", handle: "a") let stale = Post( id: "stale", author: author, text: "stale", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) service.resumeSuspendedFeedRequest(with: .success(.init(posts: [stale], cursor: nil))) await refresh.value XCTAssertNil(model.account) XCTAssertTrue(model.posts.isEmpty) } }