import Foundation import Observation import UIKit import XCTest @testable import OldFella extension NotificationLoadingTests { func testWaitingRefreshLoadsAfterSuspendedRequestIsCancelled() async { let service = NotificationLoadingService() service.pages["initial"] = .success(.init(notifications: [notification("fresh")], cursor: nil)) service.suspendRequest(for: "initial") let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let first = Task { await model.loadNotifications() } await service.waitUntilSuspendedRequest() first.cancel() let second = Task { await model.loadNotifications() } await Task.yield() XCTAssertEqual(service.requests, ["initial"]) service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("discarded")], cursor: nil))) let firstAccepted = await first.value let secondAccepted = await second.value XCTAssertFalse(firstAccepted) XCTAssertTrue(secondAccepted) XCTAssertEqual(model.notifications.map(\.id), ["fresh"]) XCTAssertEqual(service.requests, ["initial", "initial"]) } func testInitialVisiblePagePublishesWithoutLoadingAnotherPage() async { let service = NotificationLoadingService() service.pages["initial"] = .success(.init(notifications: [notification("first")], cursor: "second")) service.suspendRequest(for: "second") let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let updates = ObservationProbe { model.notifications.map(\.id) } XCTAssertTrue(model.showsNotificationsPlaceholder) _ = await model.loadNotificationsIfNeeded() await Task.yield() XCTAssertEqual(model.visibleNotifications.map(\.id), ["first"]) XCTAssertEqual(updates.values, [["first"]]) XCTAssertEqual(service.requests, ["initial"]) XCTAssertTrue(model.hasMoreNotifications) XCTAssertFalse(model.showsNotificationsPlaceholder) } func testRefreshKeepsOldRowsUntilFirstPagePublishesOnce() 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() let updates = ObservationProbe { model.notifications.map(\.id) } service.suspendRequest(for: "initial") let loading = Task { await model.loadNotifications() } await service.waitUntilSuspendedRequest() XCTAssertEqual(model.notifications.map(\.id), ["old"]) await Task.yield() XCTAssertTrue(updates.values.isEmpty) service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("first")], cursor: "second"))) _ = await loading.value await Task.yield() XCTAssertEqual(updates.values, [["first"]]) XCTAssertEqual(service.requests, ["initial", "initial"]) XCTAssertTrue(model.hasMoreNotifications) } func testLoadMorePublishesFirstVisiblePageWithoutPrefetch() async { let service = NotificationLoadingService() let first = [notification("old")] service.pages["initial"] = .success(.init(notifications: first, cursor: "more")) service.pages["more"] = .success(.init(notifications: [notification("new")], cursor: "last")) service.suspendRequest(for: "last") let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() let updates = ObservationProbe { model.notifications.map(\.id) } await model.loadNextNotificationsPage() await Task.yield() XCTAssertEqual(updates.values, [["old", "new"]]) XCTAssertEqual(service.requests, ["initial", "more"]) XCTAssertTrue(model.hasMoreNotifications) } func testRefreshWithExistingRowsOnlyPublishesLoadingState() async { let service = NotificationLoadingService() service.pages["initial"] = .success(.init(notifications: [notification("old")], cursor: "more")) service.pages["more"] = .success(.init(notifications: [], cursor: nil)) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() let loadingState = ObservationProbe { model.isLoadingNotifications } let rows = ObservationProbe { model.notifications } service.suspendRequest(for: "initial") let loading = Task { await model.loadNotifications() } await service.waitUntilSuspendedRequest() await Task.yield() XCTAssertEqual(loadingState.values, [true]) XCTAssertTrue(rows.values.isEmpty) XCTAssertTrue(model.isLoadingNotifications) XCTAssertEqual(model.notifications.map(\.id), ["old"]) await model.loadNextNotificationsPage() XCTAssertEqual(service.requests, ["initial", "initial"]) service.resumeSuspendedRequest(with: .success(.init(notifications: model.notifications, cursor: nil))) _ = await loading.value await Task.yield() XCTAssertEqual(loadingState.values, [true, false]) XCTAssertTrue(rows.values.isEmpty) XCTAssertFalse(model.isLoadingNotifications) } func testEmptyStateRequiresACompletedLoad() async { let service = NotificationLoadingService() let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") XCTAssertFalse(model.hasLoadedNotifications) XCTAssertTrue(model.showsNotificationsPlaceholder) _ = await model.loadNotificationsIfNeeded() XCTAssertTrue(model.hasLoadedNotifications) XCTAssertFalse(model.showsNotificationsPlaceholder) XCTAssertTrue(model.notifications.isEmpty) } func testInitialFailureShowsErrorInsteadOfPermanentPlaceholder() async { let service = NotificationLoadingService() service.pages["initial"] = .failure(URLError(.timedOut)) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotificationsIfNeeded() XCTAssertFalse(model.hasLoadedNotifications) XCTAssertNotNil(model.notificationsErrorMessage) XCTAssertFalse(model.showsNotificationsPlaceholder) await model.signOut() await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") XCTAssertNil(model.notificationsErrorMessage) XCTAssertTrue(model.showsNotificationsPlaceholder) } func testTabReentryDoesNotRefreshLoadedNotifications() async { let service = NotificationLoadingService() service.pages["initial"] = .success(.init(notifications: [notification("first")], cursor: nil)) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotificationsIfNeeded() _ = await model.loadNotificationsIfNeeded() XCTAssertEqual(service.requests, ["initial"]) _ = await model.loadNotifications() XCTAssertEqual(service.requests, ["initial", "initial"]) } func testTabCancellationDoesNotCancelInitialLoadOrStartAnother() async { let service = NotificationLoadingService() service.suspendRequest(for: "initial") let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let firstVisit = Task { await model.loadNotificationsIfNeeded() } await service.waitUntilSuspendedRequest() firstVisit.cancel() let nextVisit = Task { await model.loadNotificationsIfNeeded() } service.resumeSuspendedRequest(with: .success(.init(notifications: [notification("first")], cursor: nil))) _ = await firstVisit.value let accepted = await nextVisit.value XCTAssertTrue(accepted) XCTAssertEqual(model.notifications.map(\.id), ["first"]) XCTAssertEqual(service.requests, ["initial"]) } func testCancellationDoesNotShowErrorAndRefreshFailureKeepsOldRows() async { let service = NotificationLoadingService() service.pages["initial"] = .success(.init(notifications: [notification("a")], cursor: nil)) let model = AppModel(service: service) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") _ = await model.loadNotifications() service.pages["initial"] = .failure(CancellationError()) let accepted = await model.loadNotifications() XCTAssertFalse(accepted) XCTAssertEqual(model.notifications.map(\.id), ["a"]) XCTAssertNil(model.notificationsErrorMessage) } }