import Observation import XCTest @testable import OldFella extension AppModelTests { func testDemoSignInLoadsFollowingFeed() async throws { let suite = "demo-feed-\(UUID().uuidString)" let defaults = try XCTUnwrap(UserDefaults(suiteName: suite)) defer { defaults.removePersistentDomain(forName: suite) } let model = makeAppModel(service: MockBlueskyService(), feedDefaults: defaults) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") XCTAssertEqual(model.account?.handle, "demo.bsky.social") XCTAssertEqual(model.posts.map(\.id), ["post-1", "post-2", "post-3"]) XCTAssertTrue(model.hasMorePosts) } func testMockAccountRestorationLoadsFollowingFeed() async throws { let service = MockBlueskyService(restoresAccount: true) let account = try await service.restoreAccount() XCTAssertEqual(account?.handle, "demo.bsky.social") let page = try await service.loadFollowingFeed(cursor: nil) XCTAssertEqual(page.posts.map(\.id), ["post-1", "post-2", "post-3"]) XCTAssertEqual(page.cursor, "page-2") } func testSignInCanContinueAfterAuthFactorChallenge() async { let model = makeAppModel(service: MockBlueskyService(requiresAuthFactorToken: true)) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") XCTAssertTrue(model.requiresAuthFactorToken) XCTAssertNil(model.account) await model.signIn( handle: "demo.bsky.social", appPassword: "test-only", authFactorToken: "000000" ) XCTAssertTrue(model.requiresAuthFactorToken) XCTAssertNotNil(model.authFactorErrorMessage) XCTAssertNil(model.account) await model.signIn( handle: "demo.bsky.social", appPassword: "test-only", authFactorToken: "aB12c3" ) XCTAssertFalse(model.requiresAuthFactorToken) XCTAssertNil(model.authFactorErrorMessage) XCTAssertEqual(model.account?.handle, "demo.bsky.social") } func testLoadingNotificationsReturnsRecentActivity() async { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadNotifications() XCTAssertEqual(model.notifications.map(\.reason), [.like, .reply, .follow]) XCTAssertEqual(model.notifications.first?.author.name, "Mae Rivers") XCTAssertEqual(model.notifications.first?.postID, "post-1") XCTAssertEqual( model.notifications.first?.relatedPost?.text, "Morning walk along the water. The light changed every few minutes.") XCTAssertEqual(model.notifications.first?.relatedPost?.author.name, "Mae Rivers") XCTAssertEqual(model.notifications[1].postID, "reply-post-1") XCTAssertEqual(model.notifications[1].relatedPost?.text, "Thanks for the thoughtful reply.") XCTAssertTrue(model.hasMoreNotifications) XCTAssertFalse(model.isLoadingNotifications) XCTAssertNil(model.notificationsErrorMessage) await model.loadNextNotificationsPage() XCTAssertEqual( model.notifications.map(\.id), [ "notification-1", "notification-2", "notification-3", "notification-4", "notification-5", "notification-6", "notification-7" ]) XCTAssertFalse(model.hasMoreNotifications) } func testNotificationRefreshRetryAcceptsPageBeforeMarkingRead() async throws { let service = MockBlueskyService() let model = makeAppModel(service: service) let initial = await model.loadNotifications() XCTAssertTrue(initial) XCTAssertFalse(model.retryNotificationsPage) let accepted = await model.loadNotifications() let seenAt = Date(timeIntervalSince1970: 1_735_787_045) if accepted { try await model.markNotificationsRead(seenAt: seenAt) } XCTAssertTrue(accepted) XCTAssertEqual(service.notificationsSeenAt, [seenAt]) } func testMarkNotificationsReadIsExplicitAndPropagatesFailures() async throws { var service = MockBlueskyService() let model = makeAppModel(service: service) await model.loadNotifications() XCTAssertTrue(service.notificationsSeenAt.isEmpty) let seenAt = Date(timeIntervalSince1970: 1_735_787_045) try await model.markNotificationsRead(seenAt: seenAt) XCTAssertEqual(service.notificationsSeenAt, [seenAt]) XCTAssertFalse(model.notifications[0].isRead) service.failsMarkingNotificationsRead = true let failingModel = makeAppModel(service: service) await failingModel.loadNotifications() do { try await failingModel.markNotificationsRead(seenAt: seenAt) XCTFail("Expected the service error") } catch ServiceError.invalidResponse { XCTAssertFalse(failingModel.notifications[0].isRead) } } func testNotificationPostActionsUpdateRowsWithoutChangingFeedOrder() async throws { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadNotifications() let feedIDs = model.timelinePosts.map(\.id) let reply = try XCTUnwrap(model.post(id: "reply-post-1")) XCTAssertFalse(model.posts.contains { $0.id == reply.id }) await model.toggleLike(postID: reply.id) XCTAssertEqual(model.post(id: reply.id)?.isLiked, true) XCTAssertEqual(model.notifications[1].relatedPost?.likeCount, reply.likeCount + 1) await model.toggleLike(postID: reply.id) XCTAssertEqual(model.notifications[1].relatedPost?.isLiked, false) XCTAssertEqual(model.notifications[1].relatedPost?.likeCount, reply.likeCount) await model.toggleRepost(postID: reply.id) XCTAssertEqual(model.notifications[1].relatedPost?.isReposted, true) await model.toggleRepost(postID: reply.id) XCTAssertEqual(model.notifications[1].relatedPost?.isReposted, false) await model.toggleLike(postID: "post-1") XCTAssertEqual(model.notifications[0].relatedPost, model.post(id: "post-1")) XCTAssertEqual(model.timelinePosts.map(\.id), feedIDs) } func testLikePreservesNotificationListAndUnrelatedRows() async throws { let model = makeAppModel(service: MockBlueskyService()) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") await model.loadNotifications() let rows = model.notificationRows let target = try XCTUnwrap(rows.first { $0.id == "notification-1" }) let other = try XCTUnwrap(rows.first { $0.id == "notification-3" }) let list = ObservationProbe { (model.notificationRows.map(\.id), model.hasVisibleNotifications, model.showsNotificationsPlaceholder) } let targetChanges = ObservationProbe { target.group } let otherChanges = ObservationProbe { other.group } await model.toggleLike(postID: "post-1") await Task.yield() XCTAssertTrue(model.hasVisibleNotifications) XCTAssertEqual(model.notificationRows.map(\.id), rows.map(\.id)) XCTAssertTrue(zip(model.notificationRows, rows).allSatisfy { $0.0 === $0.1 }) XCTAssertTrue(list.values.isEmpty) XCTAssertEqual(targetChanges.values.count, 1) XCTAssertTrue(otherChanges.values.isEmpty) XCTAssertEqual(target.group.notifications[0].relatedPost?.isLiked, true) await model.signOut() XCTAssertTrue(model.notificationRows.isEmpty) XCTAssertFalse(model.hasVisibleNotifications) } func testRefreshKeepsCurrentFeedVisibleUntilItCompletes() async { let gate = ThreadGate() let model = makeAppModel(service: ProfilePostTestService(secondFeedGate: gate)) await model.signIn(handle: "demo.bsky.social", appPassword: "test-only") let originalIDs = model.timelinePosts.map(\.id) let refresh = Task { await model.loadFeed() } await gate.waitUntilStarted() XCTAssertFalse(model.isLoading) XCTAssertEqual(model.timelinePosts.map(\.id), originalIDs) XCTAssertTrue(model.hasMorePosts) await gate.release() await refresh.value XCTAssertEqual(model.timelinePosts.map(\.id), originalIDs) } func testPagePostTakesPrecedenceOverConflictingReplyCopies() async throws { let suite = "reply-copy-\(UUID().uuidString)" let defaults = try XCTUnwrap(UserDefaults(suiteName: suite)) defer { defaults.removePersistentDomain(forName: suite) } let service = ModerationTestService() let author = Post.Author(id: "other", name: "Other", handle: "other") let initial = (0..<15).map { Post( id: "initial-\($0)", author: author, text: "Initial", likeCount: 0, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) } service.feedPage = FeedPage(posts: initial, cursor: "next") let model = makeAppModel(service: service, feedDefaults: defaults) await model.signIn(handle: "demo", appPassword: "password") let originalIDs = model.timelineRowIDs let pagePost = Post( id: "new", author: author, text: "Page post", likeCount: 3, repostCount: 0, isLiked: false, isReposted: false, imageURLs: [], replyIDs: []) var contextA = pagePost contextA.text = "Context A" var contextB = pagePost contextB.text = "Context B" service.nextFeedPage = FeedPage( posts: [pagePost], cursor: nil, replyContexts: ["z": [contextB], "a": [contextA]]) await model.loadNextPage() XCTAssertEqual(model.timelineRowIDs, originalIDs + [pagePost.id]) XCTAssertEqual(model.posts.last, pagePost) XCTAssertEqual(model.post(id: pagePost.id), pagePost) XCTAssertEqual(model.postViewState(for: pagePost).post, pagePost) } }