import Foundation import Observation import UIKit import XCTest @testable import OldFella extension NotificationLoadingTests { func testPadRefreshPublishesOnePageAndLeavesContinuationManual() 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: nil)) let before = service.feedRequests await model.loadPadFeed(id: PadColumn.following.id, refresh: true) let state = model.padFeedState(id: PadColumn.following.id) XCTAssertEqual(state.rowIDs, ["one"]) XCTAssertFalse(state.isLoading) XCTAssertTrue(state.hasMore) XCTAssertEqual(service.feedRequests, before + 1) await model.loadNextPadFeedPage(id: PadColumn.following.id) XCTAssertEqual(state.rowIDs, ["one", "two"]) XCTAssertEqual(service.feedRequests, before + 2) XCTAssertFalse(state.hasMore) } func testFeedRefreshWaitsForInFlightPageThenLoadsFirstPage() 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") 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: "more")) await model.loadFeed() service.suspendFeedRequest(for: "more") let page = Task { await model.loadNextPage() } await service.waitUntilSuspendedFeedRequest() service.suspendRequest(for: "initial") let refresh = Task { await model.refreshFeed() } await service.waitUntilSuspendedRequest() XCTAssertEqual(service.feedCursors.suffix(2), ["initial", "more"]) XCTAssertTrue(model.isRefreshingFeed) service.resumeSuspendedRequest(with: .success(.init(notifications: [], cursor: nil))) service.feedPages["initial"] = .success(.init(posts: [], cursor: nil)) service.resumeSuspendedFeedRequest(with: .success(.init(posts: [], cursor: nil))) await page.value await refresh.value XCTAssertEqual(service.feedCursors.suffix(3), ["initial", "more", "initial"]) XCTAssertFalse(model.hasMorePosts) XCTAssertFalse(model.isRefreshingFeed) } func testFeedMutedPostIDsDoNotRejectConcurrentNotifications() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.suspendRequest(for: "initial") service.feedPages["initial"] = .success(.init(posts: [], cursor: nil, mutedPostIDs: ["muted-post"])) let notifications = Task { await model.loadNotifications() } await service.waitUntilSuspendedRequest() await model.loadFeed() service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("new")], cursor: nil))) let accepted = await notifications.value XCTAssertTrue(accepted) XCTAssertEqual(model.notifications.map(\.id), ["new"]) XCTAssertTrue(model.hasUnreadNotifications) XCTAssertNil(model.notificationsErrorMessage) } func testFeedFailureStillLoadsNotificationsAndBadge() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.feedPages["initial"] = .failure(ServiceError.invalidResponse) service.pages["initial"] = .success(.init(notifications: [notification("new")], cursor: nil)) await model.refreshFeed() XCTAssertNotNil(model.feedErrorMessage) XCTAssertEqual(model.notifications.map(\.id), ["new"]) XCTAssertTrue(model.hasUnreadNotifications) XCTAssertFalse(model.isRefreshingFeed) } func testStaleNotificationResponseKeepsRowsReadAfterMarkRead() async throws { let service = NotificationLoadingService() let old = notification("old") service.pages["initial"] = .success(.init(notifications: [old], cursor: nil)) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() service.suspendRequest(for: "initial") let refresh = Task { await model.loadNotifications() } await service.waitUntilSuspendedRequest() try await model.markNotificationsRead(seenAt: old.indexedAt.addingTimeInterval(1)) XCTAssertTrue(model.notifications[0].isRead) XCTAssertFalse(model.hasUnreadNotifications) service.resumeSuspendedRequest(with: .success(.init(notifications: [old], cursor: nil))) let accepted = await refresh.value XCTAssertTrue(accepted) XCTAssertEqual(model.notifications.map(\.id), ["old"]) XCTAssertTrue(model.notifications[0].isRead) XCTAssertFalse(model.hasUnreadNotifications) } func testSignOutDuringRefreshDoesNotRestoreNotificationsOrBadge() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.suspendRequest(for: "initial") service.suspendFeedRequest(for: "initial") let refresh = Task { await model.refreshFeed() } await service.waitUntilSuspendedRequest() await service.waitUntilSuspendedFeedRequest() let stalePosts = model.posts await model.signOut() service.resumeSuspendedFeedRequest(with: .success(.init(posts: stalePosts, cursor: nil))) service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("stale")], cursor: nil))) await refresh.value XCTAssertNil(model.account) XCTAssertTrue(model.notifications.isEmpty) XCTAssertFalse(model.hasUnreadNotifications) XCTAssertTrue(model.posts.isEmpty) } func testReadAcknowledgmentsNeverSendAnOlderTimestamp() async throws { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let newer = Date() try await model.markNotificationsRead(seenAt: newer) try await model.markNotificationsRead(seenAt: newer.addingTimeInterval(-60)) XCTAssertEqual(service.readDates, [newer, newer]) } func testNotificationRefreshDoesNotRefreshFeed() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let feedRequests = service.feedRequests _ = await model.loadNotifications() XCTAssertEqual(service.feedRequests, feedRequests) } func testUnreadStateClearsOnlyAfterServerSuccessAndKeepsNewerActivity() async throws { let service = NotificationLoadingService() let old = notification("old") let seenAt = old.indexedAt.addingTimeInterval(1) let newer = FeedNotification( id: "new", author: old.author, reason: .follow, indexedAt: seenAt.addingTimeInterval(1), isRead: false, postID: nil) service.pages["initial"] = .success(.init(notifications: [old], cursor: nil)) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() service.readError = ServiceError.invalidResponse do { try await model.markNotificationsRead(seenAt: seenAt) XCTFail("Expected the server request to fail") } catch {} XCTAssertTrue(model.hasUnreadNotifications) service.readError = nil try await model.markNotificationsRead(seenAt: seenAt) XCTAssertFalse(model.hasUnreadNotifications) XCTAssertTrue(model.notifications[0].isRead) service.pages["initial"] = .success(.init(notifications: [newer, old], cursor: nil)) _ = await model.loadNotifications() XCTAssertTrue(model.hasUnreadNotifications) XCTAssertFalse(model.notifications[0].isRead) XCTAssertTrue(model.notifications[1].isRead) } func testFeedRefreshEndsAfterNotificationFailure() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") service.pages["initial"] = .failure(ServiceError.invalidResponse) await model.refreshFeed() XCTAssertFalse(model.isRefreshingFeed) XCTAssertNotNil(model.notificationsErrorMessage) XCTAssertFalse(model.posts.isEmpty) } }